2
2
#define __FileSystem_h__
3
3
4
4
#include " StringTools.h"
5
- #include " Logger.h"
6
5
#include " extern/md5/md5.h"
7
- #if WIN32
6
+ #include < sys/stat.h>
7
+ #ifdef WIN32
8
8
#include < direct.h>
9
9
#define NOMINMAX
10
10
#include " windows.h"
11
+ #include < commdlg.h>
11
12
#else
12
- #include < sys/stat.h>
13
13
#include < unistd.h>
14
+ #include < dirent.h>
14
15
#endif
15
16
16
- #if (defined(__MINGW32__) || defined(__MINGW64__))
17
- #include < sys/stat.h >
17
+ #ifndef S_ISDIR
18
+ #define S_ISDIR ( mode ) (((mode) & S_IFMT) == S_IFDIR)
18
19
#endif
19
20
21
+ #ifndef S_ISREG
22
+ #define S_ISREG (mode ) (((mode) & S_IFMT) == S_IFREG)
23
+ #endif
20
24
21
25
namespace Utilities
22
26
{
@@ -171,6 +175,8 @@ namespace Utilities
171
175
172
176
static std::string normalizePath (const std::string &path)
173
177
{
178
+ if (path.size () == 0 )
179
+ return path;
174
180
std::string result = path;
175
181
std::replace (result.begin (), result.end (), ' \\ ' , ' /' );
176
182
std::vector<std::string> tokens;
@@ -181,7 +187,7 @@ namespace Utilities
181
187
if ((tokens[index ] == " .." ) && (index > 0 ))
182
188
{
183
189
tokens.erase (tokens.begin () + index - 1 , tokens.begin () + index + 1 );
184
- index -- ;
190
+ index -= 2 ;
185
191
}
186
192
index ++;
187
193
}
@@ -244,6 +250,54 @@ namespace Utilities
244
250
245
251
return true ;
246
252
}
253
+
254
+ static bool isFile (const std::string &path)
255
+ {
256
+ struct stat st;
257
+ if (!stat (path.c_str (), &st))
258
+ return S_ISREG (st.st_mode );
259
+ return false ;
260
+ }
261
+
262
+ static bool isDirectory (const std::string &path)
263
+ {
264
+ struct stat st;
265
+ if (!stat (path.c_str (), &st))
266
+ return S_ISDIR (st.st_mode );
267
+ return false ;
268
+ }
269
+
270
+ static bool getFilesInDirectory (const std::string& path, std::vector<std::string> &res)
271
+ {
272
+ #ifdef WIN32
273
+ std::string p = path + " \\ *" ;
274
+ WIN32_FIND_DATA data;
275
+ HANDLE hFind = FindFirstFile (p.c_str (), &data);
276
+ if (hFind != INVALID_HANDLE_VALUE)
277
+ {
278
+ do
279
+ {
280
+ res.push_back (data.cFileName );
281
+ }
282
+ while (FindNextFile (hFind, &data) != 0 );
283
+ FindClose (hFind);
284
+ return true ;
285
+ }
286
+ return false ;
287
+ #else
288
+ DIR* dir = opendir (path.c_str ());
289
+ if (dir != NULL )
290
+ {
291
+ struct dirent *dp;
292
+ while ((dp = readdir (dir)) != NULL )
293
+ res.push_back (dp->d_name );
294
+ closedir (dir);
295
+ return true ;
296
+ }
297
+ return false ;
298
+ #endif
299
+ }
300
+
247
301
248
302
/* * Compute the MD5 hash of a file.
249
303
*/
@@ -252,14 +306,14 @@ namespace Utilities
252
306
std::ifstream file (filename);
253
307
254
308
if (!file)
255
- LOG_ERR << " Cannot open file: " << filename;
309
+ std::cerr << " Cannot open file: " << filename << std::endl ;
256
310
else
257
311
{
258
312
MD5 context (file);
259
- char * hexDigestPtr ( context.hex_digest () );
260
- std::string digest (hexDigestPtr );
261
- delete hexDigestPtr ;
262
- return digest ;
313
+ char *md5hex = context.hex_digest ();
314
+ std::string res (md5hex );
315
+ delete[] md5hex ;
316
+ return res ;
263
317
}
264
318
return " " ;
265
319
}
@@ -272,7 +326,7 @@ namespace Utilities
272
326
fstream.open (md5File.c_str (), std::ios::out);
273
327
if (fstream.fail ())
274
328
{
275
- LOG_ERR << " Failed to open file: " << md5File;
329
+ std::cerr << " Failed to open file: " << md5File << " \n " ;
276
330
return false ;
277
331
}
278
332
std::string md5 = getFileMD5 (fileName);
@@ -290,7 +344,7 @@ namespace Utilities
290
344
fstream.open (md5File.c_str (), std::ios::in);
291
345
if (fstream.fail ())
292
346
{
293
- LOG_ERR << " Failed to open file: " << md5File;
347
+ std::cerr << " Failed to open file: " << md5File << " \n " ;
294
348
return false ;
295
349
}
296
350
std::string str ((std::istreambuf_iterator<char >(fstream)),
@@ -299,6 +353,47 @@ namespace Utilities
299
353
300
354
return str == md5Hash;
301
355
}
356
+
357
+ #ifdef WIN32
358
+ /* * Open windows file dialog.\n
359
+ * dialogType 0 = Open file dialog\n
360
+ * dialogType 1 = Save file dialog\n
361
+ */
362
+ static const std::string fileDialog (int dialogType,
363
+ const std::string &initialDir,
364
+ const std::string &filter)
365
+ {
366
+ std::string initDir = normalizePath (initialDir);
367
+ std::replace (initDir.begin (), initDir.end (), ' /' , ' \\ ' );
368
+
369
+ OPENFILENAME ofn; // common dialog box structure
370
+ char fileNameBuffer[512 ];
371
+ fileNameBuffer[0 ] = ' \0 ' ;
372
+
373
+ const std::string filterWithEscape = filter + ' \0 ' ;
374
+
375
+ ZeroMemory (&ofn, sizeof (ofn));
376
+ ofn.lStructSize = sizeof (ofn);
377
+ ofn.lpstrFile = fileNameBuffer;
378
+ ofn.nMaxFile = sizeof (fileNameBuffer);
379
+ ofn.lpstrFilter = filterWithEscape.c_str ();
380
+ ofn.nFilterIndex = 1 ;
381
+ ofn.lpstrInitialDir = (LPSTR)initDir.c_str ();
382
+ ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_NOCHANGEDIR;
383
+
384
+ if (dialogType == 0 )
385
+ {
386
+ if (GetOpenFileName (&ofn))
387
+ return std::string (fileNameBuffer);
388
+ }
389
+ else
390
+ {
391
+ if (GetSaveFileName (&ofn))
392
+ return std::string (fileNameBuffer);
393
+ }
394
+ return " " ;
395
+ }
396
+ #endif
302
397
};
303
398
}
304
399
0 commit comments