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