Minetest  5.4.0
filesys.h
Go to the documentation of this file.
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 <set>
23 #include <string>
24 #include <vector>
25 #include "exceptions.h"
26 
27 #ifdef _WIN32 // WINDOWS
28 #define DIR_DELIM "\\"
29 #define DIR_DELIM_CHAR '\\'
30 #define FILESYS_CASE_INSENSITIVE true
31 #define PATH_DELIM ";"
32 #else // POSIX
33 #define DIR_DELIM "/"
34 #define DIR_DELIM_CHAR '/'
35 #define FILESYS_CASE_INSENSITIVE false
36 #define PATH_DELIM ":"
37 #endif
38 
39 namespace fs
40 {
41 
43 {
44  std::string name;
45  bool dir;
46 };
47 
48 std::vector<DirListNode> GetDirListing(const std::string &path);
49 
50 // Returns true if already exists
51 bool CreateDir(const std::string &path);
52 
53 bool PathExists(const std::string &path);
54 
55 bool IsPathAbsolute(const std::string &path);
56 
57 bool IsDir(const std::string &path);
58 
59 bool IsDirDelimiter(char c);
60 
61 // Only pass full paths to this one. True on success.
62 // NOTE: The WIN32 version returns always true.
63 bool RecursiveDelete(const std::string &path);
64 
65 bool DeleteSingleFileOrEmptyDirectory(const std::string &path);
66 
67 // Returns path to temp directory, can return "" on error
68 std::string TempPath();
69 
70 /* Returns a list of subdirectories, including the path itself, but excluding
71  hidden directories (whose names start with . or _)
72 */
73 void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir);
74 std::vector<std::string> GetRecursiveDirs(const std::string &dir);
75 
76 /* Multiplatform */
77 
78 /* The path itself not included, returns a list of all subpaths.
79  dst - vector that contains all the subpaths.
80  list files - include files in the list of subpaths.
81  ignore - paths that start with these charcters will not be listed.
82 */
83 void GetRecursiveSubPaths(const std::string &path,
84  std::vector<std::string> &dst,
85  bool list_files,
86  const std::set<char> &ignore = {});
87 
88 // Only pass full paths to this one. True on success.
89 bool RecursiveDeleteContent(const std::string &path);
90 
91 // Create all directories on the given path that don't already exist.
92 bool CreateAllDirs(const std::string &path);
93 
94 // Copy a regular file
95 bool CopyFileContents(const std::string &source, const std::string &target);
96 
97 // Copy directory and all subdirectories
98 // Omits files and subdirectories that start with a period
99 bool CopyDir(const std::string &source, const std::string &target);
100 
101 // Check if one path is prefix of another
102 // For example, "/tmp" is a prefix of "/tmp" and "/tmp/file" but not "/tmp2"
103 // Ignores case differences and '/' vs. '\\' on Windows
104 bool PathStartsWith(const std::string &path, const std::string &prefix);
105 
106 // Remove last path component and the dir delimiter before and/or after it,
107 // returns "" if there is only one path component.
108 // removed: If non-NULL, receives the removed component(s).
109 // count: Number of components to remove
110 std::string RemoveLastPathComponent(const std::string &path,
111  std::string *removed = NULL, int count = 1);
112 
113 // Remove "." and ".." path components and for every ".." removed, remove
114 // the last normal path component before it. Unlike AbsolutePath,
115 // this does not resolve symlinks and check for existence of directories.
116 std::string RemoveRelativePathComponents(std::string path);
117 
118 // Returns the absolute path for the passed path, with "." and ".." path
119 // components and symlinks removed. Returns "" on error.
120 std::string AbsolutePath(const std::string &path);
121 
122 // Returns the filename from a path or the entire path if no directory
123 // delimiter is found.
124 const char *GetFilenameFromPath(const char *path);
125 
126 bool safeWriteToFile(const std::string &path, const std::string &content);
127 
128 bool ReadFile(const std::string &path, std::string &out);
129 
130 bool Rename(const std::string &from, const std::string &to);
131 
132 } // namespace fs
static v2f dir(const v2f &pos_dist)
Definition: camera.cpp:240
Definition: filesys.cpp:32
void GetRecursiveDirs(std::vector< std::string > &dirs, const std::string &dir)
Definition: filesys.cpp:369
bool IsPathAbsolute(const std::string &path)
Definition: filesys.cpp:269
bool IsDirDelimiter(char c)
Definition: filesys.cpp:282
bool CopyDir(const std::string &source, const std::string &target)
Definition: filesys.cpp:491
std::string RemoveRelativePathComponents(std::string path)
Definition: filesys.cpp:602
std::string AbsolutePath(const std::string &path)
Definition: filesys.cpp:655
bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
Definition: filesys.cpp:331
void GetRecursiveSubPaths(const std::string &path, std::vector< std::string > &dst, bool list_files, const std::set< char > &ignore)
Definition: filesys.cpp:385
bool safeWriteToFile(const std::string &path, const std::string &content)
Definition: filesys.cpp:680
bool RecursiveDeleteContent(const std::string &path)
Definition: filesys.cpp:402
std::string TempPath()
Definition: filesys.cpp:348
bool RecursiveDelete(const std::string &path)
Definition: filesys.cpp:287
bool IsDir(const std::string &path)
Definition: filesys.cpp:274
bool ReadFile(const std::string &path, std::string &out)
Definition: filesys.cpp:730
bool PathExists(const std::string &path)
Definition: filesys.cpp:263
const char * GetFilenameFromPath(const char *path)
Definition: filesys.cpp:668
bool CreateAllDirs(const std::string &path)
Definition: filesys.cpp:419
bool Rename(const std::string &from, const std::string &to)
Definition: filesys.cpp:745
std::vector< DirListNode > GetDirListing(const std::string &pathstring)
Definition: filesys.cpp:196
std::string RemoveLastPathComponent(const std::string &path, std::string *removed, int count)
Definition: filesys.cpp:570
bool CopyFileContents(const std::string &source, const std::string &target)
Definition: filesys.cpp:437
bool CreateDir(const std::string &path)
Definition: filesys.cpp:249
bool PathStartsWith(const std::string &path, const std::string &prefix)
Definition: filesys.cpp:520
Definition: filesys.h:43
std::string name
Definition: filesys.h:44
bool dir
Definition: filesys.h:45