Debian Codegraph @

src/Base/FileInfo.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2005 Jürgen Riegel <[email protected]> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This program is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Library General Public License (LGPL) *
10 * as published by the Free Software Foundation; either version 2 of *
11 * the License, or (at your option) any later version. *
12 * for detail see the LICENCE text file. *
13 * *
14 * FreeCAD is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU Library General Public License for more details. *
18 * *
19 * You should have received a copy of the GNU Library General Public *
20 * License along with FreeCAD; if not, write to the Free Software *
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
22 * USA *
23 * *
24 ***************************************************************************/
25
26
27 #ifndef BASE_FILEINFO_H
28 #define BASE_FILEINFO_H
29
30 #include <filesystem>
31 #include <string>
32 #include <vector>
33 #include <optional>
34
35 #include <FCGlobal.h>
36
37
38 namespace Base
39 {
40 class TimeInfo;
41
42 /// When reading and writing a character stream, the incoming data can be dumped into the stream
43 /// unaltered (if it contains only data that is valid in the current XML character set), or it can
44 /// be Base64-encoded. This enum is used by Reader and Writer to distinguish the two cases.
45 enum class CharStreamFormat : std::uint8_t
46 {
47 Raw,
48 Base64Encoded
49 };
50
51 /** File name unification
52 * This class handles everything related to file names
53 * the file names are internal generally UTF-8 encoded on
54 * all platforms.
55 */
56 class BaseExport FileInfo
57 {
58 public:
59 enum Permissions : std::uint8_t
60 {
61 WriteOnly = 0x01,
62 ReadOnly = 0x02,
63 ReadWrite = 0x03,
64 };
65
66 /// Construction
67 explicit FileInfo(const char* fileName = "");
68 explicit FileInfo(const std::string& fileName);
69 /// Set a new file name
70 void setFile(const char* name);
71 /// Set a new file name
72 void setFile(const std::string& name)
73 {
74 setFile(name.c_str());
75 }
76
77
78 /** @name extraction of information */
79 //@{
80 /// Returns the file name, including the path (which may be absolute or relative).
81 std::string filePath() const;
82 /// Returns the dir path name (which may be absolute or relative).
83 std::string dirPath() const;
84 /// Returns the name of the file, excluding the path, including the extension.
85 std::string fileName() const;
86 /// Returns the name of the file, excluding the path and the extension.
87 std::string fileNamePure() const;
88 /// Convert the path name into a UTF-16 encoded wide string format.
89 /// @note: Use this function on Windows only.
90 std::wstring toStdWString() const;
91 /** Returns the extension of the file.
92 * The extension consists of all characters in the file after (but not including)
93 * the last '.' character.
94 *@code
95 * FileInfo fi( "/tmp/archive.tar.gz" );
96 * ext = fi.extension(); // ext = "gz"
97 *@endcode
98 */
99 std::string extension() const;
100 /** Returns the complete extension of the file.
101 * The complete extension consists of all characters in the file after (but not including)
102 * the first '.' character.
103 *@code
104 * FileInfo fi( "/tmp/archive.tar.gz" );
105 * ext = fi.completeExtension(); // ext = "tar.gz"
106 *@endcode
107 */
108 std::string completeExtension() const;
109 /// Checks for a special extension, NOT case sensitive
110 bool hasExtension(const char* Ext) const;
111 /// Checks for any of the special extensions, NOT case sensitive
112 bool hasExtension(std::initializer_list<const char*> Exts) const;
113 //@}
114
115 /** @name methods to test the status of the file or dir */
116 //@{
117 /// Does the file exist?
118 bool exists() const;
119 /// Checks if the file exist and is readable
120 bool isReadable() const;
121 /// Checks if the file exist and is writable
122 bool isWritable() const;
123 /// Tries to set the file permission
124 bool setPermissions(Permissions);
125 /// Checks if it is a file (not a directory)
126 bool isFile() const;
127 /// Checks if it is a directory (not a file)
128 bool isDir() const;
129 /// Checks if it is a symbolic link (returns false if the file doesn't exist)
130 bool isSymlink() const;
131 /// The size of the file
132 unsigned int size() const;
133 /// Returns the time when the file was last modified.
134 TimeInfo lastModified() const;
135 //@}
136
137 /** @name Directory management*/
138 //@{
139 /// Creates a directory. Returns true if successful; otherwise returns false.
140 bool createDirectory() const;
141 /// Creates a directory and all its parent directories. Returns true if successful; otherwise
142 /// returns false.
143 bool createDirectories() const;
144 /// Get a list of the directory content
145 std::vector<Base::FileInfo> getDirectoryContent() const;
146 /// Delete an empty directory
147 bool deleteDirectory() const;
148 /// Delete a directory and all its content.
149 bool deleteDirectoryRecursive() const;
150 //@}
151
152 /// Delete the file
153 bool deleteFile() const;
154 /// Rename the file
155 bool renameFile(const char* NewName);
156 /// Rename the file
157 bool copyTo(const char* NewName) const;
158
159 /// Returns the folder or directory the symlink points
160 std::optional<std::string> getSymlinkTarget();
161 /// Returns the absolute path without any "..", "." or symlinks
162 std::optional<std::string> getCannonicalPath();
163
164 /** @name Tools */
165 //@{
166 /// Get a unique File Name in the given or (if 0) in the temp path
167 static std::string getTempFileName(const char* FileName = nullptr, const char* path = nullptr);
168 /// Get the path to the dir which is considered to temp files
169 static const std::string& getTempPath();
170 /// Convert from filesystem path to string
171 static std::string pathToString(const std::filesystem::path& path);
172 /// Convert from string to filesystem path
173 static std::filesystem::path stringToPath(const std::string& str);
174 //@}
175
176 private:
177 std::string FileName;
178 };
179
180 } // namespace Base
181
182
183 #endif // BASE_FILEINFO_H