filesys.h 3.8 KB

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