File.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. //
  2. // File.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/File.cpp#3 $
  5. //
  6. // Library: Foundation
  7. // Package: Filesystem
  8. // Module: File
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/File.h"
  16. #include "Poco/Path.h"
  17. #include "Poco/DirectoryIterator.h"
  18. #if defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8)
  19. #if defined(_WIN32_WCE)
  20. #include "File_WINCE.cpp"
  21. #else
  22. #include "File_WIN32U.cpp"
  23. #endif
  24. #elif defined(POCO_OS_FAMILY_WINDOWS)
  25. #include "File_WIN32.cpp"
  26. #elif defined(POCO_VXWORKS)
  27. #include "File_VX.cpp"
  28. #elif defined(POCO_OS_FAMILY_UNIX)
  29. #include "File_UNIX.cpp"
  30. #else
  31. #include "File_VMS.cpp"
  32. #endif
  33. namespace Poco {
  34. File::File()
  35. {
  36. }
  37. File::File(const std::string& path): FileImpl(path)
  38. {
  39. }
  40. File::File(const char* path): FileImpl(std::string(path))
  41. {
  42. }
  43. File::File(const Path& path): FileImpl(path.toString())
  44. {
  45. }
  46. File::File(const File& file): FileImpl(file.getPathImpl())
  47. {
  48. }
  49. File::~File()
  50. {
  51. }
  52. File& File::operator = (const File& file)
  53. {
  54. setPathImpl(file.getPathImpl());
  55. return *this;
  56. }
  57. File& File::operator = (const std::string& path)
  58. {
  59. setPathImpl(path);
  60. return *this;
  61. }
  62. File& File::operator = (const char* path)
  63. {
  64. poco_check_ptr (path);
  65. setPathImpl(path);
  66. return *this;
  67. }
  68. File& File::operator = (const Path& path)
  69. {
  70. setPathImpl(path.toString());
  71. return *this;
  72. }
  73. void File::swap(File& file)
  74. {
  75. swapImpl(file);
  76. }
  77. bool File::exists() const
  78. {
  79. return existsImpl();
  80. }
  81. bool File::canRead() const
  82. {
  83. return canReadImpl();
  84. }
  85. bool File::canWrite() const
  86. {
  87. return canWriteImpl();
  88. }
  89. bool File::canExecute() const
  90. {
  91. return canExecuteImpl();
  92. }
  93. bool File::isFile() const
  94. {
  95. return isFileImpl();
  96. }
  97. bool File::isDirectory() const
  98. {
  99. return isDirectoryImpl();
  100. }
  101. bool File::isLink() const
  102. {
  103. return isLinkImpl();
  104. }
  105. bool File::isDevice() const
  106. {
  107. return isDeviceImpl();
  108. }
  109. bool File::isHidden() const
  110. {
  111. return isHiddenImpl();
  112. }
  113. Timestamp File::created() const
  114. {
  115. return createdImpl();
  116. }
  117. Timestamp File::getLastModified() const
  118. {
  119. return getLastModifiedImpl();
  120. }
  121. File& File::setLastModified(const Timestamp& ts)
  122. {
  123. setLastModifiedImpl(ts);
  124. return *this;
  125. }
  126. File::FileSize File::getSize() const
  127. {
  128. return getSizeImpl();
  129. }
  130. File& File::setSize(FileSizeImpl size)
  131. {
  132. setSizeImpl(size);
  133. return *this;
  134. }
  135. File& File::setWriteable(bool flag)
  136. {
  137. setWriteableImpl(flag);
  138. return *this;
  139. }
  140. File& File::setReadOnly(bool flag)
  141. {
  142. setWriteableImpl(!flag);
  143. return *this;
  144. }
  145. File& File::setExecutable(bool flag)
  146. {
  147. setExecutableImpl(flag);
  148. return *this;
  149. }
  150. void File::copyTo(const std::string& path) const
  151. {
  152. Path src(getPathImpl());
  153. Path dest(path);
  154. File destFile(path);
  155. if ((destFile.exists() && destFile.isDirectory()) || dest.isDirectory())
  156. {
  157. dest.makeDirectory();
  158. dest.setFileName(src.getFileName());
  159. }
  160. if (isDirectory())
  161. copyDirectory(dest.toString());
  162. else
  163. copyToImpl(dest.toString());
  164. }
  165. void File::copyDirectory(const std::string& path) const
  166. {
  167. File target(path);
  168. target.createDirectories();
  169. Path src(getPathImpl());
  170. src.makeFile();
  171. DirectoryIterator it(src);
  172. DirectoryIterator end;
  173. for (; it != end; ++it)
  174. {
  175. it->copyTo(path);
  176. }
  177. }
  178. void File::moveTo(const std::string& path)
  179. {
  180. copyTo(path);
  181. remove(true);
  182. setPathImpl(path);
  183. }
  184. void File::renameTo(const std::string& path)
  185. {
  186. renameToImpl(path);
  187. setPathImpl(path);
  188. }
  189. void File::remove(bool recursive)
  190. {
  191. if (recursive && !isLink() && isDirectory())
  192. {
  193. std::vector<File> files;
  194. list(files);
  195. for (std::vector<File>::iterator it = files.begin(); it != files.end(); ++it)
  196. {
  197. it->remove(true);
  198. }
  199. }
  200. removeImpl();
  201. }
  202. bool File::createFile()
  203. {
  204. return createFileImpl();
  205. }
  206. bool File::createDirectory()
  207. {
  208. return createDirectoryImpl();
  209. }
  210. void File::createDirectories()
  211. {
  212. if (!exists())
  213. {
  214. Path p(getPathImpl());
  215. p.makeDirectory();
  216. if (p.depth() > 1)
  217. {
  218. p.makeParent();
  219. File f(p);
  220. f.createDirectories();
  221. }
  222. createDirectoryImpl();
  223. }
  224. }
  225. void File::list(std::vector<std::string>& files) const
  226. {
  227. files.clear();
  228. DirectoryIterator it(*this);
  229. DirectoryIterator end;
  230. while (it != end)
  231. {
  232. files.push_back(it.name());
  233. ++it;
  234. }
  235. }
  236. void File::list(std::vector<File>& files) const
  237. {
  238. files.clear();
  239. DirectoryIterator it(*this);
  240. DirectoryIterator end;
  241. while (it != end)
  242. {
  243. files.push_back(*it);
  244. ++it;
  245. }
  246. }
  247. void File::handleLastError(const std::string& path)
  248. {
  249. handleLastErrorImpl(path);
  250. }
  251. } // namespace Poco