您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

filesystem.hpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // \file filesystem.hpp
  2. //
  3. // Copyright (C) 2015 MicroNeil Research Corporation.
  4. //
  5. // This program is part of the MicroNeil Research Open Library Project. For
  6. // more information go to http://www.microneil.com/OpenLibrary/index.html
  7. //
  8. // This program is free software; you can redistribute it and/or modify it
  9. // under the terms of the GNU General Public License as published by the
  10. // Free Software Foundation; either version 2 of the License, or (at your
  11. // option) any later version.
  12. //
  13. // This program is distributed in the hope that it will be useful, but WITHOUT
  14. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. // more details.
  17. //
  18. // You should have received a copy of the GNU General Public License along with
  19. // this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  20. // Place, Suite 330, Boston, MA 02111-1307 USA
  21. //==============================================================================
  22. /*
  23. \brief The filesystem module provides classes to access the filesystem.
  24. */
  25. #ifndef FILESYSTEM_HPP
  26. #define FILESYSTEM_HPP
  27. #include <ctime>
  28. #include <string>
  29. #include <vector>
  30. #include <initializer_list>
  31. namespace CodeDweller {
  32. /** Abstracts OS specifics for manipulating file paths. */
  33. class FilePath {
  34. public:
  35. /// Directory separator character.
  36. static char const DirectorySeparator;
  37. /** Join path components.
  38. This method joins a veriable number of std::string inputs to
  39. form a path.
  40. @param[in] component... is one or more components of a
  41. path (i.e. directory names or a file name).
  42. @returns the path consisting of all the pathComponent inputs.
  43. */
  44. static std::string join(std::initializer_list<std::string> components);
  45. };
  46. /** Abstracts OS specifics for identifying a file and provides
  47. access to meta data.
  48. */
  49. class FileReference {
  50. public:
  51. /** Initialize with the name of the file.
  52. The constructor updates the FileReference object with the file
  53. status except for the full path.
  54. @param[in] fileName is the name of the file.
  55. */
  56. FileReference(std::string fileName);
  57. /** Get the name passed to the constructor.
  58. @returns the name passed to the constructor.
  59. */
  60. std::string FileName() const;
  61. /** Return timestamp.
  62. @returns the epoch modification time of the file if the file
  63. existed when the FileReference object was created, or the last
  64. time refersh() was called (whichever is more recent), 0
  65. otherwise.
  66. */
  67. time_t ModTimestamp() const;
  68. /** Get the file size.
  69. @returns the size of the file if the file existed when the
  70. FileReference object was created, or the last time refersh()
  71. was called (whichever is more recent), 0 otherwise.
  72. */
  73. size_t Size() const;
  74. /** Get full path if the file exists.
  75. This method access the filesystem to get the full path of the
  76. file.
  77. @warning This method might fail under Windows in a
  78. multi-threaded application if the current working directory of
  79. the application is being changed by a thread while this method
  80. is invoked.
  81. @returns a fully referenced path to the file if the file
  82. exists. Otherwise, "" is returned.
  83. */
  84. std::string FullPath();
  85. /** Refreshes the FileReference object.
  86. This method updates the FileReference object with the current
  87. data on the file system, except for the full path.
  88. */
  89. void refresh();
  90. /** Check if the file exists.
  91. @returns true if the file reference name existed in the file
  92. system when the FileReference object was created, or the last
  93. time refersh() was called (whichever is more recent), false
  94. otherwise.
  95. @throws std::runtime_error if an error occurs.
  96. */
  97. bool exists() const;
  98. /** Check if the file is a directory.
  99. @returns true if the file reference name existed and was a
  100. directory in the file system when the FileReference object was
  101. created, or the last time refersh() was called (whichever is
  102. more recent), false otherwise.
  103. */
  104. bool isDirectory() const;
  105. /// Return text for the most recent error.
  106. //
  107. // \returns Human-readable description of the most recent error.
  108. //
  109. static std::string getErrorText();
  110. private:
  111. /// Reset all members but the name. */
  112. void reset();
  113. /** Name provided to constructor. */
  114. std::string name;
  115. /** Full path. */
  116. std::string path;
  117. /** Timestamp. */
  118. time_t modTimestamp;
  119. /** Size in bytes. */
  120. long size_bytes;
  121. /** True if the file exists, false otherwise. */
  122. bool fileExists;
  123. /** True if the file is a directory, false otherwise. */
  124. bool fileIsDirectory;
  125. };
  126. /** Abstracts OS specifics for scanning a directory and provides
  127. access to meta data.
  128. */
  129. class DirectoryReference : public std::vector<FileReference> {
  130. public:
  131. /** Initialize by scanning the directory.
  132. The constructor updates the DirectoryReference object with the
  133. listing of the specified directory, filtered by the specified
  134. function.
  135. @param[in] dirName is the name of the directory.
  136. @param[in] filter is the function used to filter the directory
  137. listing. It inputs the name of the file, and returns true if
  138. the file is is to be included in the listing, and false
  139. otherwise.
  140. */
  141. DirectoryReference(std::string dirName, bool (*dirFilter)(std::string) = 0);
  142. /** Refreshes the DirectoryReference object.
  143. This method updates the DirectoryReference object with the
  144. current listing on the file system.
  145. */
  146. void refresh();
  147. private:
  148. /** Directory name provided to constructor. */
  149. std::string name;
  150. /** Filter function. */
  151. bool (*filter)(std::string);
  152. };
  153. }
  154. #endif // FILESYSTEM_HPP