FilePath.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "platform/FilePath.h"
  17. namespace mbed {
  18. FilePath::FilePath(const char *file_path) : file_name(NULL), fb(NULL)
  19. {
  20. // skip slashes
  21. file_path += strspn(file_path, "/");
  22. const char *file_system = file_path;
  23. file_name = file_system;
  24. int len = 0;
  25. while (true) {
  26. char c = *file_name;
  27. if (c == '/') { // end of object name
  28. file_name++; // point to one char after the '/'
  29. break;
  30. }
  31. if (c == 0) { // end of object name, with no filename
  32. break;
  33. }
  34. len++;
  35. file_name++;
  36. }
  37. MBED_ASSERT(len != 0);
  38. fb = FileBase::lookup(file_system, len);
  39. }
  40. const char *FilePath::fileName(void)
  41. {
  42. return file_name;
  43. }
  44. bool FilePath::isFileSystem(void)
  45. {
  46. if (NULL == fb) {
  47. return false;
  48. }
  49. return (fb->getPathType() == FileSystemPathType);
  50. }
  51. FileSystemLike *FilePath::fileSystem(void)
  52. {
  53. if (isFileSystem()) {
  54. return static_cast<FileSystemLike *>(fb);
  55. }
  56. return NULL;
  57. }
  58. bool FilePath::isFile(void)
  59. {
  60. if (NULL == fb) {
  61. return false;
  62. }
  63. return (fb->getPathType() == FilePathType);
  64. }
  65. FileLike *FilePath::file(void)
  66. {
  67. if (isFile()) {
  68. return (FileLike *)fb;
  69. }
  70. return NULL;
  71. }
  72. bool FilePath::exists(void)
  73. {
  74. return fb != NULL;
  75. }
  76. } // namespace mbed