filesys.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #pragma once
  17. #include <set>
  18. #include <string>
  19. #include <vector>
  20. #include "exceptions.h"
  21. #ifdef _WIN32 // WINDOWS
  22. #define DIR_DELIM "\\"
  23. #define DIR_DELIM_CHAR '\\'
  24. #define FILESYS_CASE_INSENSITIVE true
  25. #define PATH_DELIM ";"
  26. #else // POSIX
  27. #define DIR_DELIM "/"
  28. #define DIR_DELIM_CHAR '/'
  29. #define FILESYS_CASE_INSENSITIVE false
  30. #define PATH_DELIM ":"
  31. #endif
  32. namespace fs
  33. {
  34. struct DirListNode
  35. {
  36. std::string name;
  37. bool dir;
  38. };
  39. std::vector<DirListNode> GetDirListing(const std::string &path);
  40. // Returns true if already exists
  41. bool CreateDir(const std::string &path);
  42. bool PathExists(const std::string &path);
  43. bool IsPathAbsolute(const std::string &path);
  44. bool IsDir(const std::string &path);
  45. bool IsDirDelimiter(char c);
  46. // Only pass full paths to this one. True on success.
  47. // NOTE: The WIN32 version returns always true.
  48. bool RecursiveDelete(const std::string &path);
  49. bool DeleteSingleFileOrEmptyDirectory(const std::string &path);
  50. // Returns path to temp directory, can return "" on error
  51. std::string TempPath();
  52. /* Returns a list of subdirectories, including the path itself, but excluding
  53. hidden directories (whose names start with . or _)
  54. */
  55. void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir);
  56. std::vector<std::string> GetRecursiveDirs(const std::string &dir);
  57. /* Multiplatform */
  58. /* The path itself not included, returns a list of all subpaths.
  59. dst - vector that contains all the subpaths.
  60. list files - include files in the list of subpaths.
  61. ignore - paths that start with these charcters will not be listed.
  62. */
  63. void GetRecursiveSubPaths(const std::string &path,
  64. std::vector<std::string> &dst,
  65. bool list_files,
  66. const std::set<char> &ignore = {});
  67. // Tries to delete all, returns false if any failed
  68. bool DeletePaths(const std::vector<std::string> &paths);
  69. // Only pass full paths to this one. True on success.
  70. bool RecursiveDeleteContent(const std::string &path);
  71. // Create all directories on the given path that don't already exist.
  72. bool CreateAllDirs(const std::string &path);
  73. // Copy a regular file
  74. bool CopyFileContents(const std::string &source, const std::string &target);
  75. // Copy directory and all subdirectories
  76. // Omits files and subdirectories that start with a period
  77. bool CopyDir(const std::string &source, const std::string &target);
  78. // Check if one path is prefix of another
  79. // For example, "/tmp" is a prefix of "/tmp" and "/tmp/file" but not "/tmp2"
  80. // Ignores case differences and '/' vs. '\\' on Windows
  81. bool PathStartsWith(const std::string &path, const std::string &prefix);
  82. // Remove last path component and the dir delimiter before and/or after it,
  83. // returns "" if there is only one path component.
  84. // removed: If non-NULL, receives the removed component(s).
  85. // count: Number of components to remove
  86. std::string RemoveLastPathComponent(const std::string &path,
  87. std::string *removed = NULL, int count = 1);
  88. // Remove "." and ".." path components and for every ".." removed, remove
  89. // the last normal path component before it. Unlike AbsolutePath,
  90. // this does not resolve symlinks and check for existence of directories.
  91. std::string RemoveRelativePathComponents(std::string path);
  92. // Returns the absolute path for the passed path, with "." and ".." path
  93. // components and symlinks removed. Returns "" on error.
  94. std::string AbsolutePath(const std::string &path);
  95. // Returns the filename from a path or the entire path if no directory
  96. // delimiter is found.
  97. const char *GetFilenameFromPath(const char *path);
  98. bool safeWriteToFile(const std::string &path, const std::string &content);
  99. bool ReadFile(const std::string &path, std::string &out);
  100. bool Rename(const std::string &from, const std::string &to);
  101. } // namespace fs