Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

filesystem.hpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. namespace CodeDweller {
  30. /** Abstracts OS specifics for identifying a file and provides
  31. access to meta data.
  32. */
  33. class FileReference {
  34. public:
  35. /** Initialize with the name of the file.
  36. The constructor updates the FileReference object with the file
  37. status except for the full path.
  38. @param[in] fileName is the name of the file.
  39. */
  40. FileReference(std::string fileName);
  41. /** Return timestamp.
  42. @returns the epoch modification time of the file if the file
  43. existed when the FileReference object was created, or the last
  44. time refersh() was called (whichever is more recent), 0
  45. otherwise.
  46. */
  47. time_t ModTimestamp() const;
  48. /** Get the file size.
  49. @returns the size of the file if the file existed when the
  50. FileReference object was created, or the last time refersh()
  51. was called (whichever is more recent), 0 otherwise.
  52. */
  53. size_t Size() const;
  54. /** Get full path if the file exists.
  55. This method access the filesystem to get the full path of the
  56. file.
  57. @warning This method might fail under Windows in a
  58. multi-threaded application if the current working directory of
  59. the application is being changed by a thread while this method
  60. is invoked.
  61. @returns a fully referenced path to the file if the file
  62. exists. Otherwise, "" is returned.
  63. */
  64. std:: string FullPath();
  65. /** Refreshes the FileReference object.
  66. This method updates the FileReference object with the current
  67. data on the file system, except for the full path.
  68. */
  69. void refresh();
  70. /** Check if the file exists.
  71. @returns true if the file reference name existed in the file
  72. system when the FileReference object was created, or the last
  73. time refersh() was called (whichever is more recent), false
  74. otherwise.
  75. @throws std::runtime_error if an error occurs.
  76. */
  77. bool exists() const;
  78. /** Check if the file is a directory.
  79. @returns true if the file reference name existed and was a
  80. directory in the file system when the FileReference object was
  81. created, or the last time refersh() was called (whichever is
  82. more recent), false otherwise.
  83. */
  84. bool isDirectory() const;
  85. private:
  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. /// Reset all members but the name. */
  92. void reset();
  93. /** Name provided to constructor. */
  94. std::string name;
  95. /** Full path. */
  96. std::string path;
  97. /** Timestamp. */
  98. time_t modTimestamp;
  99. /** Size in bytes. */
  100. long size_bytes;
  101. /** True if the file exists, false otherwise. */
  102. bool fileExists;
  103. /** True if the file is a directory, false otherwise. */
  104. bool fileIsDirectory;
  105. };
  106. }
  107. #endif // FILESYSTEM_HPP