Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

filesystem.hpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 files. */
  33. class FileOps {
  34. public:
  35. /** Move a file.
  36. @param[in] from is the path of the file to move.
  37. @param[in] to is the path the move the file to.
  38. @throws std::runtime_error if an error occurs.
  39. */
  40. static void moveFile(std::string const &from, std::string const &to);
  41. };
  42. /** Abstracts OS specifics for manipulating file paths. */
  43. class FilePath {
  44. public:
  45. /// Directory separator character.
  46. static char const DirectorySeparator;
  47. /** Check whether a path is absolute.
  48. This method whether the specified path is absolute or not.
  49. @param[in] path is the path to check.
  50. @returns true if the path is an absolute path, false
  51. otherwise.
  52. */
  53. static bool isAbsolute(std::string const &path);
  54. /** Join path components.
  55. This method joins a veriable number of std::string inputs to
  56. form a path.
  57. @param[in] component... is one or more components of a
  58. path (i.e. directory names or a file name).
  59. @returns the path consisting of all the pathComponent inputs.
  60. */
  61. static std::string join(std::initializer_list<std::string> components);
  62. };
  63. /** Abstracts OS specifics for identifying a file and provides
  64. access to meta data.
  65. */
  66. class FileReference {
  67. public:
  68. /** Initialize with the name of the file.
  69. The constructor updates the FileReference object with the file
  70. status except for the full path.
  71. @param[in] fileName is the name of the file.
  72. */
  73. FileReference(std::string fileName);
  74. /** Get the name passed to the constructor.
  75. @returns the name passed to the constructor.
  76. */
  77. std::string FileName() const;
  78. /** Return timestamp.
  79. @returns the epoch modification time of the file if the file
  80. existed when the FileReference object was created, or the last
  81. time refersh() was called (whichever is more recent), 0
  82. otherwise.
  83. */
  84. time_t ModTimestamp() const;
  85. /** Get the file size.
  86. @returns the size of the file if the file existed when the
  87. FileReference object was created, or the last time refersh()
  88. was called (whichever is more recent), 0 otherwise.
  89. */
  90. size_t Size() const;
  91. /** Get full path if the file exists.
  92. This method access the filesystem to get the full path of the
  93. file.
  94. @warning This method might fail under Windows in a
  95. multi-threaded application if the current working directory of
  96. the application is being changed by a thread while this method
  97. is invoked.
  98. @returns a fully referenced path to the file if the file
  99. exists. Otherwise, "" is returned.
  100. */
  101. std::string FullPath();
  102. /** Refreshes the FileReference object.
  103. This method updates the FileReference object with the current
  104. data on the file system, except for the full path.
  105. */
  106. void refresh();
  107. /** Check if the file exists.
  108. @returns true if the file reference name existed in the file
  109. system when the FileReference object was created, or the last
  110. time refersh() was called (whichever is more recent), false
  111. otherwise.
  112. @throws std::runtime_error if an error occurs.
  113. */
  114. bool exists() const;
  115. /** Check if the file is a directory.
  116. @returns true if the file reference name existed and was a
  117. directory in the file system when the FileReference object was
  118. created, or the last time refersh() was called (whichever is
  119. more recent), false otherwise.
  120. */
  121. bool isDirectory() const;
  122. /// Return text for the most recent error.
  123. //
  124. // \returns Human-readable description of the most recent error.
  125. //
  126. static std::string getErrorText();
  127. private:
  128. /// Reset all members but the name. */
  129. void reset();
  130. /** Name provided to constructor. */
  131. std::string name;
  132. /** Full path. */
  133. std::string path;
  134. /** Timestamp. */
  135. time_t modTimestamp;
  136. /** Size in bytes. */
  137. long size_bytes;
  138. /** True if the file exists, false otherwise. */
  139. bool fileExists;
  140. /** True if the file is a directory, false otherwise. */
  141. bool fileIsDirectory;
  142. };
  143. /** Abstracts OS specifics for scanning a directory and provides
  144. access to meta data.
  145. */
  146. class DirectoryReference : public std::vector<FileReference> {
  147. public:
  148. /** Initialize by scanning the directory.
  149. The constructor updates the DirectoryReference object with the
  150. listing of the specified directory, filtered by the specified
  151. function.
  152. @param[in] dirName is the name of the directory.
  153. @param[in] filter is the function used to filter the directory
  154. listing. It inputs the name of the file, and returns true if
  155. the file is is to be included in the listing, and false
  156. otherwise.
  157. */
  158. DirectoryReference(std::string dirName, bool (*dirFilter)(std::string) = 0);
  159. /** Refreshes the DirectoryReference object.
  160. This method updates the DirectoryReference object with the
  161. current listing on the file system.
  162. */
  163. void refresh();
  164. private:
  165. /** Directory name provided to constructor. */
  166. std::string name;
  167. /** Filter function. */
  168. bool (*filter)(std::string);
  169. };
  170. }
  171. #endif // FILESYSTEM_HPP