filesys.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef FILESYS_HEADER
  17. #define FILESYS_HEADER
  18. #include <string>
  19. #include <vector>
  20. #include "exceptions.h"
  21. #ifdef _WIN32 // WINDOWS
  22. #define DIR_DELIM "\\"
  23. #define FILESYS_CASE_INSENSITIVE 1
  24. #else // POSIX
  25. #define DIR_DELIM "/"
  26. #define FILESYS_CASE_INSENSITIVE 0
  27. #endif
  28. namespace fs
  29. {
  30. struct DirListNode
  31. {
  32. std::string name;
  33. bool dir;
  34. };
  35. std::vector<DirListNode> GetDirListing(std::string path);
  36. // Returns true if already exists
  37. bool CreateDir(std::string path);
  38. bool PathExists(std::string path);
  39. bool IsDir(std::string path);
  40. bool IsDirDelimiter(char c);
  41. // Only pass full paths to this one. True on success.
  42. // NOTE: The WIN32 version returns always true.
  43. bool RecursiveDelete(std::string path);
  44. bool DeleteSingleFileOrEmptyDirectory(std::string path);
  45. // Returns path to temp directory, can return "" on error
  46. std::string TempPath();
  47. /* Multiplatform */
  48. // The path itself not included
  49. void GetRecursiveSubPaths(std::string path, std::vector<std::string> &dst);
  50. // Tries to delete all, returns false if any failed
  51. bool DeletePaths(const std::vector<std::string> &paths);
  52. // Only pass full paths to this one. True on success.
  53. bool RecursiveDeleteContent(std::string path);
  54. // Create all directories on the given path that don't already exist.
  55. bool CreateAllDirs(std::string path);
  56. // Copy a regular file
  57. bool CopyFileContents(std::string source, std::string target);
  58. // Copy directory and all subdirectories
  59. // Omits files and subdirectories that start with a period
  60. bool CopyDir(std::string source, std::string target);
  61. // Check if one path is prefix of another
  62. // For example, "/tmp" is a prefix of "/tmp" and "/tmp/file" but not "/tmp2"
  63. // Ignores case differences and '/' vs. '\\' on Windows
  64. bool PathStartsWith(std::string path, std::string prefix);
  65. // Remove last path component and the dir delimiter before and/or after it,
  66. // returns "" if there is only one path component.
  67. // removed: If non-NULL, receives the removed component(s).
  68. // count: Number of components to remove
  69. std::string RemoveLastPathComponent(std::string path,
  70. std::string *removed = NULL, int count = 1);
  71. // Remove "." and ".." path components and for every ".." removed, remove
  72. // the last normal path component before it. Unlike AbsolutePath,
  73. // this does not resolve symlinks and check for existence of directories.
  74. std::string RemoveRelativePathComponents(std::string path);
  75. bool safeWriteToFile(const std::string &path, const std::string &content);
  76. }//fs
  77. #endif