11#include " Platform/Core/DefaultFileLoader.h"
2- #include " Platform/Core/StringToWString.h"
32#include " Platform/Core/RuntimeError.h"
3+ #include " Platform/Core/StringToWString.h"
44#include < iostream>
55#if defined(_MSC_VER) && defined(_WIN32)
6- #include < sys/types.h>
7- #include < sys/stat.h>
86#include < direct.h>
97#include < io.h>
10- #else
11- #include < sys/types.h>
128#include < sys/stat.h>
9+ #include < sys/types.h>
10+ #else
1311#include < sys/param.h>
12+ #include < sys/stat.h>
13+ #include < sys/types.h>
1414#include < unistd.h>
1515#endif
16- #include < stdio.h> // for fopen, seek, fclose
1716#include < cstdlib> // for malloc, free
17+ #include < stdio.h> // for fopen, seek, fclose
1818#include < time.h>
1919typedef struct stat Stat;
2020using namespace platform ;
@@ -31,37 +31,38 @@ namespace fs = std::experimental::filesystem;
3131static int do_mkdir (const char *path_utf8)
3232{
3333 ALWAYS_ERRNO_CHECK
34- int status = 0 ;
34+ int status = 0 ;
3535 // TO-DO: this
3636#ifndef NN_NINTENDO_SDK
3737#ifdef _MSC_VER
38- struct _stat64i32 st;
39- std::wstring wstr= platform::core::Utf8ToWString (path_utf8);
38+ struct _stat64i32 st;
39+ std::wstring wstr = platform::core::Utf8ToWString (path_utf8);
4040 if (_wstat (wstr.c_str (), &st) != 0 )
4141#else
42- Stat st;
43- if (stat (path_utf8, &st)!= 0 )
42+ Stat st;
43+ if (stat (path_utf8, &st) != 0 )
4444#endif
4545 {
4646 /* Directory does not exist. EEXIST for race condition */
4747#ifdef _MSC_VER
4848 if (_wmkdir (wstr.c_str ()) != 0 && errno != EEXIST)
4949#else
50- if (mkdir (path_utf8,S_IRWXU) != 0 && errno != EEXIST)
50+ if (mkdir (path_utf8, S_IRWXU) != 0 && errno != EEXIST)
5151#endif
5252 status = -1 ;
5353 }
5454 else if (!(st.st_mode & S_IFDIR))
5555 {
56- // errno = ENOTDIR;
56+ // errno = ENOTDIR;
5757 status = -1 ;
5858 }
5959#endif
6060
61- errno= 0 ;
62- return (status);
61+ errno = 0 ;
62+ return (status);
6363}
64- static int mkpath (const std::string &filename_utf8)
64+
65+ static int mkpath (std::string filename_utf8)
6566{
6667 int status = 0 ;
6768 int pos = 0 ;
@@ -73,7 +74,6 @@ static int mkpath(const std::string &filename_utf8)
7374 return (status);
7475}
7576
76-
7777DefaultFileLoader::DefaultFileLoader ()
7878{
7979}
@@ -82,36 +82,39 @@ bool DefaultFileLoader::FileExists(const char *filename_utf8) const
8282{
8383 enum access_mode
8484 {
85- NO_FILE=-1 ,EXIST=0 ,WRITE=2 ,READ=4
85+ NO_FILE = -1 ,
86+ EXIST = 0 ,
87+ WRITE = 2 ,
88+ READ = 4
8689 };
8790#if defined(_MSC_VER) && defined(_WIN32)
88- std::wstring wstr= platform::core::Utf8ToWString (filename_utf8);
89- access_mode res= (access_mode)_waccess (wstr.c_str (),READ);
90- bool bExists = (res!= NO_FILE);
91- if (errno== EACCES)
91+ std::wstring wstr = platform::core::Utf8ToWString (filename_utf8);
92+ access_mode res = (access_mode)_waccess (wstr.c_str (), READ);
93+ bool bExists = (res != NO_FILE);
94+ if (errno == EACCES)
9295 {
93- SIMUL_CERR<< " DefaultFileLoader::FileExists Access denied: for " << filename_utf8<< " the file's permission setting does not allow specified access." << std::endl;
96+ SIMUL_CERR << " DefaultFileLoader::FileExists Access denied: for " << filename_utf8 << " the file's permission setting does not allow specified access." << std::endl;
9497 }
95- else if (errno== EINVAL)
98+ else if (errno == EINVAL)
9699 {
97- SIMUL_CERR<< " DefaultFileLoader::FileExists Invalid parameter: for " << filename_utf8<< " .\n " ;
100+ SIMUL_CERR << " DefaultFileLoader::FileExists Invalid parameter: for " << filename_utf8 << " .\n " ;
98101 }
99- else if (errno== ENOENT)
102+ else if (errno == ENOENT)
100103 {
101104 // ENOENT: File name or path not found.
102105 // This is an acceptable error as we are just checking whether the file exists.
103106 }
104107 // If res is NO_FILE, errno will now be set, so we must clear it.
105- errno= 0 ;
108+ errno = 0 ;
106109#elif NN_NINTENDO_SDK
107110 // TO-DO: this
108111 bool bExists = false ;
109112#elif __COMMODORE__
110113 bool bExists = fs::exists (filename_utf8);
111114#else
112115 Stat st;
113- bool bExists= (stat (filename_utf8, &st)== 0 );
114- errno= 0 ;
116+ bool bExists = (stat (filename_utf8, &st) == 0 );
117+ errno = 0 ;
115118#endif
116119 return bExists;
117120}
@@ -171,7 +174,7 @@ uint64_t DefaultFileLoader::GetFileDateUnixTimeMs(const char *filename_utf8) con
171174 }
172175 fclose (fp);
173176
174- #if 0 // PLATFORM_STD_FILESYSTEM
177+ #if 0 // PLATFORM_STD_FILESYSTEM
175178 // https://stackoverflow.com/questions/61030383/how-to-convert-stdfilesystemfile-time-type-to-time-t
176179 // https://stackoverflow.com/questions/51273205/how-to-compare-time-t-and-stdfilesystemfile-time-type
177180 fs::file_time_type fileTime = fs::last_write_time(filename_utf8);
@@ -195,40 +198,40 @@ uint64_t DefaultFileLoader::GetFileDateUnixTimeMs(const char *filename_utf8) con
195198 struct stat buf;
196199 stat (filename_utf8, &buf);
197200 time_t t = buf.st_mtime ;
198- return t* 1000 ;
201+ return t * 1000 ;
199202#endif
200203 return 0 ;
201204}
202205
203- bool DefaultFileLoader::Save (const void * pointer, unsigned int bytes, const char * filename_utf8,bool save_as_text)
206+ bool DefaultFileLoader::Save (const void * pointer, unsigned int bytes, const char * filename_utf8, bool save_as_text)
204207{
205- std::wstring wstr=platform::core::Utf8ToWString (filename_utf8);
206208 FILE *fp = nullptr ;
207209 // Ensure the directory exists:
208210 {
209211 mkpath (filename_utf8);
210212 }
211-
213+
212214#if defined(_MSC_VER) && defined(_WIN32)
213- _wfopen_s (&fp,wstr.c_str (),L" wb" );// w, ccs=UTF-8
215+ std::wstring wstr = platform::core::Utf8ToWString (filename_utf8);
216+ _wfopen_s (&fp, wstr.c_str (), L" wb" ); // w, ccs=UTF-8
214217#else
215- fp = fopen (filename_utf8," wb" );// w, ccs=UTF-8
218+ fp = fopen (filename_utf8, " wb" ); // w, ccs=UTF-8
216219#endif
217-
218- if (!fp)
220+
221+ if (!fp)
219222 {
220- std::cerr<< " Failed to open file " << filename_utf8<< std::endl;
223+ std::cerr << " Failed to open file " << filename_utf8 << std::endl;
221224 return false ;
222225 }
223226 fseek (fp, 0 , SEEK_SET);
224227 fwrite (pointer, 1 , bytes, fp);
225- if (save_as_text)
228+ if (save_as_text)
226229 {
227- char c= 0 ;
230+ char c = 0 ;
228231 fwrite (&c, 1 , 1 , fp);
229232 }
230233 fclose (fp);
231-
234+
232235 return true ;
233236}
234237
0 commit comments