3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
20 #ifndef FILESYS_HEADER
21 #define FILESYS_HEADER
25 #include "exceptions.h"
27 #ifdef _WIN32 // WINDOWS
28 #define DIR_DELIM "\\"
29 #define DIR_DELIM_CHAR '\\'
30 #define FILESYS_CASE_INSENSITIVE 1
31 #define PATH_DELIM ";"
34 #define DIR_DELIM_CHAR '/'
35 #define FILESYS_CASE_INSENSITIVE 0
36 #define PATH_DELIM ":"
48 std::vector<DirListNode> GetDirListing(const std::string &path);
50 // Returns true if already exists
51 bool CreateDir(const std::string &path);
53 bool PathExists(const std::string &path);
55 bool IsPathAbsolute(const std::string &path);
57 bool IsDir(const std::string &path);
59 bool IsDirDelimiter(char c);
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);
65 bool DeleteSingleFileOrEmptyDirectory(const std::string &path);
67 // Returns path to temp directory, can return "" on error
68 std::string TempPath();
72 // The path itself not included
73 void GetRecursiveSubPaths(const std::string &path, std::vector<std::string> &dst);
75 // Tries to delete all, returns false if any failed
76 bool DeletePaths(const std::vector<std::string> &paths);
78 // Only pass full paths to this one. True on success.
79 bool RecursiveDeleteContent(const std::string &path);
81 // Create all directories on the given path that don't already exist.
82 bool CreateAllDirs(const std::string &path);
84 // Copy a regular file
85 bool CopyFileContents(const std::string &source, const std::string &target);
87 // Copy directory and all subdirectories
88 // Omits files and subdirectories that start with a period
89 bool CopyDir(const std::string &source, const std::string &target);
91 // Check if one path is prefix of another
92 // For example, "/tmp" is a prefix of "/tmp" and "/tmp/file" but not "/tmp2"
93 // Ignores case differences and '/' vs. '\\' on Windows
94 bool PathStartsWith(const std::string &path, const std::string &prefix);
96 // Remove last path component and the dir delimiter before and/or after it,
97 // returns "" if there is only one path component.
98 // removed: If non-NULL, receives the removed component(s).
99 // count: Number of components to remove
100 std::string RemoveLastPathComponent(const std::string &path,
101 std::string *removed = NULL, int count = 1);
103 // Remove "." and ".." path components and for every ".." removed, remove
104 // the last normal path component before it. Unlike AbsolutePath,
105 // this does not resolve symlinks and check for existence of directories.
106 std::string RemoveRelativePathComponents(std::string path);
108 // Returns the absolute path for the passed path, with "." and ".." path
109 // components and symlinks removed. Returns "" on error.
110 std::string AbsolutePath(const std::string &path);
112 // Returns the filename from a path or the entire path if no directory
113 // delimiter is found.
114 const char *GetFilenameFromPath(const char *path);
116 bool safeWriteToFile(const std::string &path, const std::string &content);
118 bool Rename(const std::string &from, const std::string &to);