DirHandle.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #ifndef MBED_DIRHANDLE_H
  17. #define MBED_DIRHANDLE_H
  18. #include <stdint.h>
  19. #include "platform/platform.h"
  20. #include "platform/FileHandle.h"
  21. #include "platform/NonCopyable.h"
  22. namespace mbed {
  23. /** \addtogroup platform */
  24. /** @{*/
  25. /**
  26. * \defgroup platform_DirHandle DirHandle functions
  27. * @{
  28. */
  29. /** Represents a directory stream. Objects of this type are returned
  30. * by an opendir function. The core functions are read and seek,
  31. * but only a subset needs to be provided.
  32. *
  33. * If a FileSystemLike class defines the opendir method, then the
  34. * directories of an object of that type can be accessed by
  35. * DIR *d = opendir("/example/directory") (or opendir("/example")
  36. * to open the root of the filesystem), and then using readdir(d) etc.
  37. *
  38. * The root directory is considered to contain all FileHandle and
  39. * FileSystem objects, so the DIR* returned by opendir("/") will
  40. * reflect this.
  41. *
  42. * @note to create a directory, @see Dir
  43. * @note Synchronization level: Set by subclass
  44. */
  45. class DirHandle : private NonCopyable<DirHandle> {
  46. public:
  47. virtual ~DirHandle() {}
  48. /** Read the next directory entry
  49. *
  50. * @param ent The directory entry to fill out
  51. * @return 1 on reading a filename, 0 at end of directory, negative error on failure
  52. */
  53. virtual ssize_t read(struct dirent *ent) = 0;
  54. /** Close a directory
  55. *
  56. * @return 0 on success, negative error code on failure
  57. */
  58. virtual int close() = 0;
  59. /** Set the current position of the directory
  60. *
  61. * @param offset Offset of the location to seek to,
  62. * must be a value returned from tell
  63. */
  64. virtual void seek(off_t offset) = 0;
  65. /** Get the current position of the directory
  66. *
  67. * @return Position of the directory that can be passed to rewind
  68. */
  69. virtual off_t tell() = 0;
  70. /** Rewind the current position to the beginning of the directory
  71. */
  72. virtual void rewind() = 0;
  73. /** Get the sizeof the directory
  74. *
  75. * @return Number of files in the directory
  76. */
  77. virtual size_t size()
  78. {
  79. off_t off = tell();
  80. size_t size = 0;
  81. struct dirent *ent = new struct dirent;
  82. rewind();
  83. while (read(ent) > 0) {
  84. size += 1;
  85. }
  86. seek(off);
  87. delete ent;
  88. return size;
  89. }
  90. /** Closes the directory.
  91. *
  92. * @returns
  93. * 0 on success,
  94. * -1 on error.
  95. * @deprecated Replaced by `int DirHandle::close()'
  96. */
  97. MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::close")
  98. virtual int closedir()
  99. {
  100. return close();
  101. };
  102. /** Return the directory entry at the current position, and
  103. * advances the position to the next entry.
  104. *
  105. * @returns
  106. * A pointer to a dirent structure representing the
  107. * directory entry at the current position, or NULL on reaching
  108. * end of directory or error.
  109. * @deprecated Replaced by `ssize_t DirHandle::read(struct dirent *ent)
  110. */
  111. MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::read")
  112. virtual struct dirent *readdir()
  113. {
  114. static struct dirent ent;
  115. return (read(&ent) > 0) ? &ent : NULL;
  116. }
  117. /** Resets the position to the beginning of the directory.
  118. * @deprecated Replaced by `void DirHandle::rewind()'
  119. */
  120. MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::rewind")
  121. virtual void rewinddir()
  122. {
  123. rewind();
  124. }
  125. /** Returns the current position of the DirHandle.
  126. *
  127. * @returns
  128. * the current position,
  129. * -1 on error.
  130. * @deprecated Replaced by `off_t DirHandle::tell()'
  131. */
  132. MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::tell")
  133. virtual off_t telldir()
  134. {
  135. return tell();
  136. }
  137. /** Sets the position of the DirHandle.
  138. *
  139. * @param location The location to seek to. Must be a value returned by telldir.
  140. * @deprecated Replaced by `void DirHandle::seek(off_t offset)'
  141. */
  142. MBED_DEPRECATED_SINCE("mbed-os-5.4", "Replaced by DirHandle::seek")
  143. virtual void seekdir(off_t location)
  144. {
  145. seek(location);
  146. }
  147. };
  148. /**@}*/
  149. /**@}*/
  150. } // namespace mbed
  151. #endif /* MBED_DIRHANDLE_H */