22#define __FileSystem_h__
33
44#include " StringTools.h"
5- #include " Logger.h"
65#include " extern/md5/md5.h"
7- #if WIN32
6+ #include < sys/stat.h>
7+ #ifdef WIN32
88#include < direct.h>
99#define NOMINMAX
1010#include " windows.h"
11+ #include < commdlg.h>
1112#else
12- #include < sys/stat.h>
1313#include < unistd.h>
14+ #include < dirent.h>
1415#endif
1516
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)
1819#endif
1920
21+ #ifndef S_ISREG
22+ #define S_ISREG (mode ) (((mode) & S_IFMT) == S_IFREG)
23+ #endif
2024
2125namespace Utilities
2226{
@@ -171,6 +175,8 @@ namespace Utilities
171175
172176 static std::string normalizePath (const std::string &path)
173177 {
178+ if (path.size () == 0 )
179+ return path;
174180 std::string result = path;
175181 std::replace (result.begin (), result.end (), ' \\ ' , ' /' );
176182 std::vector<std::string> tokens;
@@ -181,7 +187,7 @@ namespace Utilities
181187 if ((tokens[index] == " .." ) && (index > 0 ))
182188 {
183189 tokens.erase (tokens.begin () + index - 1 , tokens.begin () + index + 1 );
184- index-- ;
190+ index-= 2 ;
185191 }
186192 index++;
187193 }
@@ -244,6 +250,54 @@ namespace Utilities
244250
245251 return true ;
246252 }
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+
247301
248302 /* * Compute the MD5 hash of a file.
249303 */
@@ -252,14 +306,14 @@ namespace Utilities
252306 std::ifstream file (filename);
253307
254308 if (!file)
255- LOG_ERR << " Cannot open file: " << filename;
309+ std::cerr << " Cannot open file: " << filename << std::endl ;
256310 else
257311 {
258312 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 ;
263317 }
264318 return " " ;
265319 }
@@ -272,7 +326,7 @@ namespace Utilities
272326 fstream.open (md5File.c_str (), std::ios::out);
273327 if (fstream.fail ())
274328 {
275- LOG_ERR << " Failed to open file: " << md5File;
329+ std::cerr << " Failed to open file: " << md5File << " \n " ;
276330 return false ;
277331 }
278332 std::string md5 = getFileMD5 (fileName);
@@ -290,7 +344,7 @@ namespace Utilities
290344 fstream.open (md5File.c_str (), std::ios::in);
291345 if (fstream.fail ())
292346 {
293- LOG_ERR << " Failed to open file: " << md5File;
347+ std::cerr << " Failed to open file: " << md5File << " \n " ;
294348 return false ;
295349 }
296350 std::string str ((std::istreambuf_iterator<char >(fstream)),
@@ -299,6 +353,47 @@ namespace Utilities
299353
300354 return str == md5Hash;
301355 }
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
302397 };
303398}
304399
0 commit comments