-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.cpccFileSystem.h
294 lines (240 loc) · 9.46 KB
/
fs.cpccFileSystem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/* *****************************************
* File: fs.cpccFileSystem.h
* Purpose: Portable (cross-platform), light-weight, file system library.
* It was created because the boost::filesystem is too complicated and
* needs considerable effort to be included in your application.
* This header only class, has only a few cross platform file system
* functions, but it is a stand alone header include.
* Help: The character type is passed as template parameter, so you can define
* that from your code according to UNICODE or other considerations
* e.g. typedef cpccFileSystemL1<wchar_t> cpccFS;
* *****************************************
* Library: Cross Platform C++ Classes (cpcc)
* Copyright: StarMessage software.
* License: Free for opensource projects.
* Commercial license for closed source projects.
* Web: http://www.StarMessageSoftware.com/cpcclibrary
* Download: https://github.com/starmessage/cpcc
* email: sales -at- starmessage.info
* *****************************************
*/
#pragma once
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctime>
#ifdef __APPLE__
#include <unistd.h> // for rmdir()
#endif
#ifdef _WIN32
#include <direct.h> // for rmdir()
#include <Windows.h> // for GetFileAttributesEx()
#endif
// /////////////////////////////////
//
// cpccFileSystem
//
// /////////////////////////////////
class cpccFileSystem
{
public: // functions
template<typename aPCharType>
static long long getFileSize(const aPCharType *aFilename);
template<typename aPCharType>
static time_t getModificationDate(const aPCharType* aFilename);
template<typename aPCharType>
static bool fileExists(const aPCharType *aFilename);
template<typename aPCharType>
static bool folderExists(const aPCharType* aFoldername);
template<typename aPCharType>
static bool deleteFile(const aPCharType* aFilename);
template<typename aPCharType>
static bool deleteFolder(const aPCharType * aFoldername);
template<typename aPCharType>
static bool renameFile(const aPCharType* filenameOld, const aPCharType* filenameNew);
public: // utility functions
#ifdef _WIN32
static const time_t filetime2time_t(const FILETIME& ft);
#endif
};
// /////////////////////////////////
//
// cpccFileSystem implementation
//
// /////////////////////////////////
#ifdef _WIN32
inline const time_t cpccFileSystem::filetime2time_t(const FILETIME& ft)
{
// A FILETIME is the number of 100-nanosecond intervals since January 1, 1601.
// A time_t is the number of 1 - second intervals since January 1, 1970.
ULARGE_INTEGER ull;
ull.LowPart = ft.dwLowDateTime;
ull.HighPart = ft.dwHighDateTime;
return ull.QuadPart / 10000000ULL - 11644473600ULL;
}
#endif
template<typename aPCharType>
inline time_t cpccFileSystem::getModificationDate(const aPCharType* aFilename)
{ // http://linux.die.net/man/2/stat
if (!fileExists(aFilename) && !folderExists(aFilename))
return 0;
#ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA fileInfoWin;
BOOL result = GetFileAttributesEx(aFilename, GetFileExInfoStandard, &fileInfoWin); // Returns non zero if succeed
if (!result) // If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
{
// GetLastError();
return 0;
}
return filetime2time_t(fileInfoWin.ftLastWriteTime);
#else
// _stat does not work on XP
cpcc_struct_stat attrib; // create a file attribute structure
cpcc_stat(aFilename, &attrib); // get the attributes
return attrib.st_mtime;
#endif
}
template<typename aPCharType>
inline long long cpccFileSystem::getFileSize(const aPCharType *aFilename)
{
// long is 4 byte in Visual Studio, so you have to use long long to get correct file size for big files on Windows
// On a Windows system this is implemented with a Windows specific GetFileAttributesEx(), or getfilesizeex(),
// on linux this is implemented as a lstat64(), and
// on the Macintosh it uses the Mac specific call getattrlist().
// for windows use getfilesizeex()
// https://docs.microsoft.com/en-gb/windows/win32/api/fileapi/nf-fileapi-getfilesizeex
#ifdef _WIN32
/*
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions
// seems to fail under windows server 2003 32 bit
struct _stat64 stat_buf;
#ifdef UNICODE
int rc = _wstat64(aFilename, &stat_buf);
#else
int rc = _stat64(aFilename, &stat_buf);
#endif
return (rc == 0) ? stat_buf.st_size : -1;
*/
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/ns-fileapi-win32_file_attribute_data
WIN32_FILE_ATTRIBUTE_DATA fileInfoWin;
BOOL result = GetFileAttributesEx(aFilename, GetFileExInfoStandard, &fileInfoWin); // Returns non zero if succeed
if (!result) // If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
{
// GetLastError();
return 0;
}
LARGE_INTEGER size;
size.HighPart = fileInfoWin.nFileSizeHigh;
size.LowPart = fileInfoWin.nFileSizeLow;
return size.QuadPart;
#else
struct stat stat_buf;
int rc = stat(aFilename, &stat_buf);
return (rc == 0) ? stat_buf.st_size : -1;
#endif
}
template<typename aPCharType>
inline bool cpccFileSystem::fileExists(const aPCharType *aFilename)
{
if (!aFilename)
return false;
#ifdef _WIN32
// _stat() does not work on WinXP.
/*
struct _stat fileinfo;
// On success, zero is returned.
// On error, -1 is returned, and errno is set appropriately.
if (_tstat(aFilename, &fileinfo) == -1)
return false;
return ((fileinfo.st_mode & _S_IFREG) != 0);
*/
// check that the file system object exists and that it is not a directory
DWORD dwAttrib = GetFileAttributes(aFilename);
return (dwAttrib != INVALID_FILE_ATTRIBUTES && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY));
#elif defined(__APPLE__)
struct stat fileinfo;
// On success, zero is returned.
// On error, -1 is returned, and errno is set appropriately.
if (stat(aFilename, &fileinfo) == -1)
return false;
return (S_ISREG(fileinfo.st_mode));
#else
#error Error #5458: unsupported platform for fileExists()
#endif
return false;
}
template<typename aPCharType>
inline bool cpccFileSystem::renameFile(const aPCharType* filenameOld, const aPCharType* filenameNew)
{
if ((!filenameOld) || (!filenameNew))
return false;
/*
If the file referenced by dest_file exists prior to calling rename(),
the behavior is implementation-defined. On POSIX systems, the destination
file is removed. On Windows systems, the rename() fails.
This creates issues when trying to write portable code.
*/
#ifdef UNICODE
return (_trename(filenameOld, filenameNew)==0);
#else
return (std::rename(filenameOld, filenameNew)==0);
#endif
}
template<typename aPCharType>
inline bool cpccFileSystem::deleteFile(const aPCharType* aFilename)
{
if (!aFilename)
return false;
/*
If the file is successfully deleted, a zero value is returned.
On failure, a nonzero value is returned and the errno variable is
set to the corresponding error code. A string interpreting
this value can be printed to the standard error stream by a call to perror.
*/
#ifdef UNICODE
return (_wremove(aFilename)==0);
#else
return (remove(aFilename)==0);
#endif
}
template<typename aPCharType>
inline bool cpccFileSystem::deleteFolder(const aPCharType * aFoldername)
{
if (!aFoldername)
return false;
// Windows: To recursively delete the files in a directory, use the SHFileOperation function.
#ifdef UNICODE // for Unicode Windows
return _wrmdir(aFoldername) == 0;
#else
#ifdef _WIN32
// 'rmdir': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _rmdir.
return _rmdir(aFoldername) == 0;
#else
return rmdir(aFoldername) == 0;
#endif
#endif
}
template<typename aPCharType>
inline bool cpccFileSystem::folderExists(const aPCharType * aFoldername)
{
if (!aFoldername)
return false;
#ifdef _WIN32
DWORD attrib = GetFileAttributes(aFoldername);
return (! ( attrib == 0xFFFFFFFF || !(attrib & FILE_ATTRIBUTE_DIRECTORY) ) );
// Other way:
// return (PathIsDirectory( aFilename ) == FILE_ATTRIBUTE_DIRECTORY);
#elif defined(__APPLE__)
struct stat fileinfo;
if (stat(aFoldername, &fileinfo) == -1)
{ // On success, zero is returned.
// On error, -1 is returned, and errno is set appropriately.
return false;
}
else
return (S_ISDIR(fileinfo.st_mode));
#else
#error Error #5414: unsupported platform for folderExists()
#endif
return false;
}