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

filesystem.hpp 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. namespace CodeDweller {
  31. /** Abstracts OS specifics for identifying a file and provides
  32. access to meta data.
  33. */
  34. class FileReference {
  35. public:
  36. /** Initialize with the name of the file.
  37. The constructor updates the FileReference object with the file
  38. status except for the full path.
  39. @param[in] fileName is the name of the file.
  40. */
  41. FileReference(std::string fileName);
  42. /** Return timestamp.
  43. @returns the epoch modification time of the file if the file
  44. existed when the FileReference object was created, or the last
  45. time refersh() was called (whichever is more recent), 0
  46. otherwise.
  47. */
  48. time_t ModTimestamp() const;
  49. /** Get the file size.
  50. @returns the size of the file if the file existed when the
  51. FileReference object was created, or the last time refersh()
  52. was called (whichever is more recent), 0 otherwise.
  53. */
  54. size_t Size() const;
  55. /** Get full path if the file exists.
  56. This method access the filesystem to get the full path of the
  57. file.
  58. @warning This method might fail under Windows in a
  59. multi-threaded application if the current working directory of
  60. the application is being changed by a thread while this method
  61. is invoked.
  62. @returns a fully referenced path to the file if the file
  63. exists. Otherwise, "" is returned.
  64. */
  65. std:: string FullPath();
  66. /** Refreshes the FileReference object.
  67. This method updates the FileReference object with the current
  68. data on the file system, except for the full path.
  69. */
  70. void refresh();
  71. /** Check if the file exists.
  72. @returns true if the file reference name existed in the file
  73. system when the FileReference object was created, or the last
  74. time refersh() was called (whichever is more recent), false
  75. otherwise.
  76. @throws std::runtime_error if an error occurs.
  77. */
  78. bool exists() const;
  79. /** Check if the file is a directory.
  80. @returns true if the file reference name existed and was a
  81. directory in the file system when the FileReference object was
  82. created, or the last time refersh() was called (whichever is
  83. more recent), false otherwise.
  84. */
  85. bool isDirectory() const;
  86. /// Return text for the most recent error.
  87. //
  88. // \returns Human-readable description of the most recent error.
  89. //
  90. static std::string getErrorText();
  91. private:
  92. /// Reset all members but the name. */
  93. void reset();
  94. /** Name provided to constructor. */
  95. std::string name;
  96. /** Full path. */
  97. std::string path;
  98. /** Timestamp. */
  99. time_t modTimestamp;
  100. /** Size in bytes. */
  101. long size_bytes;
  102. /** True if the file exists, false otherwise. */
  103. bool fileExists;
  104. /** True if the file is a directory, false otherwise. */
  105. bool fileIsDirectory;
  106. };
  107. /** Abstracts OS specifics for scanning a directory and provides
  108. access to meta data.
  109. */
  110. class DirectoryReference : public std::vector<FileReference> {
  111. public:
  112. /** Initialize by scanning the directory.
  113. The constructor updates the DirectoryReference object with the
  114. listing of the specified directory, filtered by the specified
  115. function.
  116. @param[in] dirName is the name of the directory.
  117. @param[in] filter is the function used to filter the directory
  118. listing. It inputs the name of the file, and returns true if
  119. the file is is to be included in the listing, and false
  120. otherwise.
  121. */
  122. DirectoryReference(std::string dirName, bool (*dirFilter)(std::string) = 0);
  123. /** Refreshes the DirectoryReference object.
  124. This method updates the DirectoryReference object with the
  125. current listing on the file system.
  126. */
  127. void refresh();
  128. private:
  129. /** Directory name provided to constructor. */
  130. std::string name;
  131. /** Filter function. */
  132. bool (*filter)(std::string);
  133. };
  134. }
  135. #endif // FILESYSTEM_HPP