1 
2 bool PatternMatch(const char *p, const char *s);
3 bool PatternMatchMulti(const char *p, const char *s);
4 
5 const char  *GetFileNamePos(const char *path);
6 const char  *GetFileExtPos(const char *path);
7 
8 bool    HasFileExt(const char *path);
9 bool    HasWildcards(const char *path);
10 bool    IsFullPath(const char *path);
11 
12 String  GetFileDirectory(const char *path); // with DIR_SEP at the end
13 String  GetFileFolder(const char *path); // without DIR_SEP at the end, if not Win32 root
14 String  GetFileTitle(const char *path);
15 String  GetFileExt(const char *path);
16 String  GetFileName(const char *path);
17 
18 String  AppendFileName(const String& path, const char *filename);
19 
20 String WinPath(const char *path);
21 String UnixPath(const char *path);
22 
23 #ifdef  PLATFORM_WIN32
NativePath(const char * path)24 inline String  NativePath(const char *path) { return WinPath(path); }
25 #endif
26 
27 #ifdef  PLATFORM_POSIX
NativePath(const char * path)28 inline String  NativePath(const char *path) { return UnixPath(path); }
29 #endif
30 
31 String  AppendExt(const char *path, const char *ext);
32 String  ForceExt(const char *path, const char *ext);
33 
34 String  GetFileOnPath(const char *file, const char *paths, bool current = true, const char *curdir = NULL);
35 
36 #ifndef PLATFORM_WINCE
37 String  GetFullPath(const char *path);
38 String  GetCurrentDirectory();
39 #endif
40 
41 #ifdef PLATFORM_POSIX
42 bool SetCurrentDirectory(const char *path);
43 #endif
44 
45 struct FileTime;
46 
47 int Compare_FileTime(const FileTime& fa, const FileTime& fb);
48 
49 #ifdef PLATFORM_WIN32
50 
51 struct FileTime : FILETIME, CompareRelOps<const FileTime&, &Compare_FileTime> {
FileTimeFileTime52 	FileTime()                          {}
FileTimeFileTime53 	FileTime(const FILETIME& ft)        { dwLowDateTime = ft.dwLowDateTime;
54 	                                      dwHighDateTime = ft.dwHighDateTime; }
55 };
56 
57 class  FindFile : NoCopy {
58 	WIN32_FIND_DATAW  data[1];
59 	HANDLE            handle;
60 	String            pattern;
61 	String            path;
62 
63 	bool        Next0();
64 	void        Close();
65 
66 public:
67 	bool        Search(const char *path);
68 	bool        Next();
69 
GetAttributes()70 	dword       GetAttributes() const      { return data->dwFileAttributes; }
71 	String      GetName() const;
72 	String      GetPath() const;
73 	int64       GetLength() const;
GetCreationTime()74 	FileTime    GetCreationTime() const    { return data->ftCreationTime; }
GetLastAccessTime()75 	FileTime    GetLastAccessTime() const  { return data->ftLastAccessTime; }
GetLastWriteTime()76 	FileTime    GetLastWriteTime() const   { return data->ftLastWriteTime; }
77 
IsDirectory()78 	bool        IsDirectory() const        { return GetAttributes() & FILE_ATTRIBUTE_DIRECTORY; }
79 	bool        IsFolder() const;
IsFile()80 	bool        IsFile() const             { return !IsDirectory(); }
81 	bool        IsSymLink() const;
82 	bool        IsExecutable() const;
83 
IsArchive()84 	bool        IsArchive() const          { return GetAttributes() & FILE_ATTRIBUTE_ARCHIVE; }
IsCompressed()85 	bool        IsCompressed() const       { return GetAttributes() & FILE_ATTRIBUTE_COMPRESSED; }
IsHidden()86 	bool        IsHidden() const           { return GetAttributes() & FILE_ATTRIBUTE_HIDDEN; }
IsReadOnly()87 	bool        IsReadOnly() const         { return GetAttributes() & FILE_ATTRIBUTE_READONLY; }
IsSystem()88 	bool        IsSystem() const           { return GetAttributes() & FILE_ATTRIBUTE_SYSTEM; }
IsTemporary()89 	bool        IsTemporary() const        { return GetAttributes() & FILE_ATTRIBUTE_TEMPORARY; }
90 
91 	operator    bool() const               { return handle != INVALID_HANDLE_VALUE; }
92 
93 	FindFile();
94 	FindFile(const char *name);
95 	~FindFile();
96 };
97 
98 #endif
99 
100 #ifdef PLATFORM_POSIX
101 
102 struct FileTime : CompareRelOps<const FileTime&, &Compare_FileTime>
103 {
FileTimeFileTime104 	FileTime() {}
FileTimeFileTime105 	FileTime(time_t ft) : ft(ft) {}
106 
time_tFileTime107 	operator time_t () const { return ft; }
108 
109 	time_t ft;
110 };
111 
Compare_FileTime(const FileTime & f,const FileTime & g)112 inline int Compare_FileTime(const FileTime& f, const FileTime& g) { return f.ft < g.ft ? -1 : f.ft > g.ft ? 1 : 0; }
113 
114 class FindFile : NoCopy {
115 	bool           file;
116 	DIR           *dir;
117 	mutable bool   statis;
118 	mutable struct stat statf;
119 	String         path;
120 	String         name;
121 	String         pattern;
122 
123 	struct stat &Stat() const;
124 	bool CanMode(dword usr, dword grp, dword oth) const;
125 
126 public:
127 	bool        Search(const char *name);
128 	bool        Next();
129 	void        Close();
130 
GetMode()131 	dword       GetMode() const           { return Stat().st_mode; }
GetName()132 	String      GetName() const           { return name; }
133 	String      GetPath() const;
GetLength()134 	int64       GetLength() const         { return Stat().st_size; }
GetLastChangeTime()135 	FileTime    GetLastChangeTime() const { return Stat().st_ctime; }
GetLastAccessTime()136 	FileTime    GetLastAccessTime() const { return Stat().st_atime; }
GetLastWriteTime()137 	FileTime    GetLastWriteTime() const  { return Stat().st_mtime; }
138 
GetUid()139 	uid_t       GetUid()                  { return Stat().st_uid; }
GetGid()140 	gid_t       GetGid()                  { return Stat().st_gid; }
141 
CanRead()142 	bool        CanRead() const           { return CanMode(S_IRUSR, S_IRGRP, S_IROTH); }
CanWrite()143 	bool        CanWrite() const          { return CanMode(S_IWUSR, S_IWGRP, S_IWOTH); }
CanExecute()144 	bool        CanExecute() const        { return CanMode(S_IXUSR, S_IXGRP, S_IXOTH); }
145 
IsReadOnly()146 	bool        IsReadOnly() const        { return CanRead() && !CanWrite(); }
147 
IsHidden()148 	bool        IsHidden() const          { return *name == '.'; }
IsDirectory()149 	bool        IsDirectory() const       { return S_ISDIR(GetMode()); }
150 	bool        IsFolder() const;
IsFile()151 	bool        IsFile() const            { return S_ISREG(GetMode()); }
152 	bool        IsSymLink() const;
153 	bool        IsExecutable() const;
154 
155 	operator    bool() const              { return file; }
156 
FindFile()157 	FindFile()                            { file = false; dir = NULL; }
158 	FindFile(const char *name);
~FindFile()159 	~FindFile()                           { Close(); }
160 };
161 
162 // POSIX FileTime is unfortunately long int and clashes with Date::operator int()
163 inline bool operator==(Time a, FileTime b) { return a == Time(b); }
164 inline bool operator!=(Time a, FileTime b) { return a != Time(b); }
165 
166 inline bool operator==(FileTime a, Time b) { return Time(a) == b; }
167 inline bool operator!=(FileTime a, Time b) { return Time(a) != b; }
168 
169 #endif
170 
171 int64       GetFileLength(const char *path);
172 bool        FileExists(const char *path);
173 bool        DirectoryExists(const char *path);
174 
175 struct Time;
176 FileTime    GetFileTime(const char *path);
177 Time        FileGetTime(const char *path);
178 bool        SetFileTime(const char *path, FileTime ft);
179 bool        FileSetTime(const char *path, Time time);
180 FileTime    TimeToFileTime(Time time);
181 
182 bool        FileCopy(const char *oldpath, const char *newpath);
183 bool        FileMove(const char *oldpath, const char *newpath);
184 bool        FileDelete(const char *path);
185 
186 #ifdef PLATFORM_POSIX
187 bool        DirectoryCreate(const char *path, int mode = 0755);
188 bool        RealizeDirectory(const String& path, int mode = 0755);
189 bool        RealizePath(const String& path, int mode = 0755);
190 #else
191 bool        DirectoryCreate(const char *path);
192 bool        RealizeDirectory(const String& path);
193 bool        RealizePath(const String& path);
194 #endif
195 
196 bool        DirectoryDelete(const char *path);
197 
198 String      NormalizePath(const char *path, const char *currdir);
199 String      NormalizePath(const char *path);
200 
201 bool        PathIsEqual(const char *p1, const char *p2);
202 
203 #ifdef PLATFORM_POSIX
DeleteFile(const char * fn)204 inline bool DeleteFile(const char *fn)      { return unlink(fn) == 0; }
205 #endif
206 
207 bool    DeleteFolderDeep(const char *dir);
208 
209 #ifndef PLATFORM_WINCE
210 String  GetTempPath();
211 String  GetTempFileName(const char *prefix = NULL);
212 #endif
213 
214 String GetSymLinkPath(const char *linkpath);
215 
216 template <class T>
217 class Array;
218 
219 class FileSystemInfo {
220 public:
221 	enum
222 	{
223 		ROOT_UNKNOWN     = 0,
224 		ROOT_NO_ROOT_DIR = 1,
225 		ROOT_REMOVABLE   = 2,
226 		ROOT_FIXED       = 3,
227 		ROOT_REMOTE      = 4,
228 		ROOT_CDROM       = 5,
229 		ROOT_RAMDISK     = 6,
230 		ROOT_NETWORK     = 7,
231 		ROOT_COMPUTER    = 8,
232 	};
233 
234 	enum
235 	{
236 		STYLE_WIN32      = 0x0001,
237 		STYLE_POSIX      = 0x0002,
238 	};
239 
240 	struct FileInfo
241 	{
242 		FileInfo();
243 
244 		operator bool () const { return !IsNull(filename); }
245 
246 		String filename;
247 		String msdos_name;
248 		String root_desc;
249 		int64  length;
250 		Time   last_access_time;
251 		Time   last_write_time;
252 		Time   creation_time;
253 		bool   read_only;
254 		bool   is_directory;
255 		bool   is_folder;
256 		bool   is_file;
257 		bool   is_symlink;
258 		bool   is_archive;
259 		bool   is_compressed;
260 		bool   is_hidden;
261 		bool   is_read_only;
262 		bool   is_system;
263 		bool   is_temporary;
264 		char   root_style;
265 		dword  unix_mode;
266 	};
267 
268 	virtual int             GetStyle() const;
IsWin32()269 	bool                    IsWin32() const { return GetStyle() & STYLE_WIN32; }
IsPosix()270 	bool                    IsPosix() const { return GetStyle() & STYLE_POSIX; }
271 
272 	virtual Array<FileInfo> Find(String mask, int max_count = 1000000, bool unmounted = false) const; // mask = Null -> root
273 	virtual bool            CreateFolder(String path, String& error) const;
274 
275 	bool                    FolderExists(String path) const;
276 
~FileSystemInfo()277 	virtual ~FileSystemInfo() {}
278 };
279 
280 FileSystemInfo& StdFileSystemInfo();
281 
282 #ifdef PLATFORM_WIN32
283 
284 class NetNode : Moveable<NetNode> {
285 	NETRESOURCE net;
286 	String      local, remote, comment, provider;
287 
288 	String      name;
289 	String      path;
290 
291 	static void           Copy(String& t, char *s);
292 	static Array<NetNode> Enum0(HANDLE hEnum);
293 	static void           SetPtr(String& s, char *& ptr);
294 
295 	void SetPtrs();
296 
297 public:
298 	enum {
299 		UNKNOWN, NETWORK, GROUP, SERVER, SHARE
300 	};
GetName()301 	String         GetName() const     { return name; }
GetPath()302 	String         GetPath() const     { return path; }
303 	int            GetDisplayType() const;
GetRemote()304 	String         GetRemote() const   { return remote; }
GetLocal()305 	String         GetLocal() const    { return local; }
GetProvider()306 	String         GetProvider() const { return provider; }
GetComment()307 	String         GetComment() const  { return comment; }
308 	Array<NetNode> Enum() const;
309 
310 	void           Serialize(Stream& s);
311 
312 	static Array<NetNode> EnumRoot();
313 	static Array<NetNode> EnumRemembered();
314 
315 	NetNode();
NetNode(const NetNode & s)316 	NetNode(const NetNode& s)         { *this = s; }
317 
318 	NetNode& operator=(const NetNode& s);
319 };
320 
321 #endif
322