MinitScript  0.9.31 PRE-BETA
FileSystem.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 
9 
10 using std::string;
11 using std::vector;
12 
14 
15 /**
16  * File system
17  * @author Andreas Drewke
18  */
20 {
21 
22 public:
23  /**
24  * File system exception
25  * @author Andreas Drewke
26  */
28  {
29  public:
30  /**
31  * @brief Public constructor
32  * @param message message Exception message
33  */
34  FileSystemException(const string& message) throw(): _ExceptionBase(message) {
35  //
36  }
37  };
38 
39  /**
40  * File system file name filter interface
41  * @author Andreas Drewke
42  */
44  {
45  /**
46  * Destructor
47  */
48  virtual ~FileNameFilter() {}
49 
50  /**
51  * Accept a file
52  * @param path path
53  * @param file file
54  * @return if to accept file
55  */
56  virtual bool accept(const string& path, const string& file) = 0;
57  };
58 
59  /**
60  * Compose URI from path name and file name
61  * @param pathName path name
62  * @param fileName file name
63  * @return complete file URI with path name and file name
64  */
65  static const string composeURI(const string& pathName, const string& fileName);
66 
67  /**
68  * Return file size of given file
69  * @param pathName path name
70  * @param fileName file name
71  * @return file size
72  * @throws tdme::os::filesystem::FileSystemException
73  */
74  static uint64_t getFileSize(const string& pathName, const string& fileName);
75 
76  /**
77  * Get content as string
78  * @param pathName path name
79  * @param fileName file name
80  * @return string
81  * @throws tdme::os::filesystem::FileSystemException
82  */
83  static const string getContentAsString(const string& pathName, const string& fileName);
84 
85  /**
86  * Set content from string
87  * @param pathName path name
88  * @param fileName file name
89  * @param content content string
90  * @throws tdme::os::filesystem::FileSystemException
91  */
92  static void setContentFromString(const string& pathName, const string& fileName, const string& content);
93 
94  /**
95  * Get file content
96  * @param pathName path name
97  * @param fileName file name
98  * @param content content vector
99  * @throws tdme::os::filesystem::FileSystemException
100  */
101  static void getContent(const string& pathName, const string& fileName, vector<uint8_t>& content);
102 
103  /**
104  * Set file content
105  * @param pathName path name
106  * @param fileName file name
107  * @param content content vector
108  * @throws tdme::os::filesystem::FileSystemException
109  */
110  static void setContent(const string& pathName, const string& fileName, const vector<uint8_t>& content);
111 
112  /**
113  * Get file content as string array
114  * @param pathName path name
115  * @param fileName file name
116  * @param content content vector
117  * @throws tdme::os::filesystem::FileSystemException
118  */
119  static void getContentAsStringArray(const string& pathName, const string& fileName, vector<string>& content);
120 
121  /**
122  * Set file content as string array
123  * @param pathName path name
124  * @param fileName file name
125  * @param content string array
126  * @return byte array
127  * @throws tdme::os::filesystem::FileSystemException
128  */
129  static void setContentFromStringArray(const string& pathName, const string& fileName, const vector<string>& content);
130 
131  /**
132  * List files for given path and filter by a file name filter if not null
133  * @param pathName path name
134  * @param files files
135  * @param filter filter or null, this filter can be created on stack as ownership will not be taken over
136  * @param addDrives add drives to list(applies to Microsoft Windows only)
137  * @return file names
138  * @throws tdme::os::filesystem::FileSystemException
139  */
140  static void list(const string& pathName, vector<string>& files, FileNameFilter* filter = nullptr, bool addDrives = false);
141 
142  /**
143  * Check if file is a path
144  * @param uri uniform resource identifier
145  * @return if file is a path
146  * @throws tdme::os::filesystem::FileSystemException
147  */
148  static bool isPath(const string& uri);
149 
150  /**
151  * Check if file is a drive (applies to Microsoft Windows only)
152  * @param uri uniform resource identifier
153  * @return if file is a drive
154  */
155  static bool isDrive(const string& uri);
156 
157  /**
158  * Check if file exists
159  * @param uri uniform resource identifier
160  * @return bool if file exists
161  * @throws tdme::os::filesystem::FileSystemException
162  */
163  static bool exists(const string& uri);
164 
165  /**
166  * Get canonical URI from given path name and file name
167  * @param pathName path name
168  * @param fileName file name
169  * @return canonical URI
170  */
171  static const string getCanonicalURI(const string& pathName, const string& fileName);
172 
173  /**
174  * Get current working path name
175  * @return current working path
176  * @throws tdme::os::filesystem::FileSystemException
177  */
178  static const string getCurrentWorkingPathName();
179 
180  /**
181  * Change path
182  * @param pathName path name
183  * @throws tdme::os::filesystem::FileSystemException
184  */
185  static void changePath(const string& pathName);
186 
187  /**
188  * Get path name
189  * @param uri uniform resource identifier
190  * @return canonical path
191  */
192  static const string getPathName(const string& uri);
193 
194  /**
195  * Get file name
196  * @param uri uniform resource identifier
197  * @return canonical path
198  */
199  static const string getFileName(const string& uri);
200 
201  /**
202  * Remove file extension, e.g. .dae, .fbx, ...
203  * @param fileName file name
204  * @return file name
205  */
206  static const string removeFileExtension(const string& fileName);
207 
208  /**
209  * Create path
210  * @param pathName path name
211  * @throws tdme::os::filesystem::FileSystemException
212  */
213  static void createPath(const string& pathName);
214 
215  /**
216  * Remove path
217  * @param pathName path name
218  * @param recursive remove recursive
219  * @return success
220  * @throws tdme::os::filesystem::FileSystemException
221  */
222  static void removePath(const string& pathName, bool recursive);
223 
224  /**
225  * Remove file
226  * @param pathName path name
227  * @param fileName file name
228  * @return success
229  * @throws tdme::os::filesystem::FileSystemException
230  */
231  static void removeFile(const string& pathName, const string& fileName);
232 
233  /**
234  * Rename file
235  * @param fileNameFrom file name from
236  * @param fileNameTo file name to
237  * @throws tdme::os::filesystem::FileSystemException
238  */
239  static void rename(const string& fileNameFrom, const string& fileNameTo);
240 
241 };
minitscript::utilities::ExceptionBase _ExceptionBase
Definition: FileSystem.h:13
FileSystemException(const string &message)
Public constructor.
Definition: FileSystem.h:34
static const string removeFileExtension(const string &fileName)
Remove file extension, e.g.
Definition: FileSystem.cpp:291
static bool isDrive(const string &uri)
Check if file is a drive (applies to Microsoft Windows only)
Definition: FileSystem.cpp:97
static void setContentFromString(const string &pathName, const string &fileName, const string &content)
Set content from string.
Definition: FileSystem.cpp:132
static const string composeURI(const string &pathName, const string &fileName)
Compose URI from path name and file name.
Definition: FileSystem.cpp:36
static const string getPathName(const string &uri)
Get path name.
Definition: FileSystem.cpp:277
static const string getCanonicalURI(const string &pathName, const string &fileName)
Get canonical URI from given path name and file name.
Definition: FileSystem.cpp:195
static void createPath(const string &pathName)
Create path.
Definition: FileSystem.cpp:300
static void list(const string &pathName, vector< string > &files, FileNameFilter *filter=nullptr, bool addDrives=false)
List files for given path and filter by a file name filter if not null.
Definition: FileSystem.cpp:40
static void changePath(const string &pathName)
Change path.
Definition: FileSystem.cpp:269
static void removePath(const string &pathName, bool recursive)
Remove path.
Definition: FileSystem.cpp:310
static void setContent(const string &pathName, const string &fileName, const vector< uint8_t > &content)
Set file content.
Definition: FileSystem.cpp:156
static void getContent(const string &pathName, const string &fileName, vector< uint8_t > &content)
Get file content.
Definition: FileSystem.cpp:142
static void setContentFromStringArray(const string &pathName, const string &fileName, const vector< string > &content)
Set file content as string array.
Definition: FileSystem.cpp:180
static void removeFile(const string &pathName, const string &fileName)
Remove file.
Definition: FileSystem.cpp:326
static uint64_t getFileSize(const string &pathName, const string &fileName)
Return file size of given file.
Definition: FileSystem.cpp:111
static void getContentAsStringArray(const string &pathName, const string &fileName, vector< string > &content)
Get file content as string array.
Definition: FileSystem.cpp:165
static const string getFileName(const string &uri)
Get file name.
Definition: FileSystem.cpp:284
static bool isPath(const string &uri)
Check if file is a path.
Definition: FileSystem.cpp:86
static bool exists(const string &uri)
Check if file exists.
Definition: FileSystem.cpp:101
static const string getCurrentWorkingPathName()
Get current working path name.
Definition: FileSystem.cpp:256
static const string getContentAsString(const string &pathName, const string &fileName)
Get content as string.
Definition: FileSystem.cpp:121
static void rename(const string &fileNameFrom, const string &fileNameTo)
Rename file.
Definition: FileSystem.cpp:336
File system file name filter interface.
Definition: FileSystem.h:44
virtual bool accept(const string &path, const string &file)=0
Accept a file.