@ -9,66 +9,63 @@
# include "common/logging/log.h"
# ifdef _WIN32
# include <windows.h>
# include <shlobj.h> // for SHGetFolderPath
# include <shellapi.h>
# include <commdlg.h> // for GetSaveFileName
# include <io.h>
# include <direct.h> // getcwd
# include <tchar.h>
# include <windows.h>
// windows.h needs to be included before other windows headers
# include <commdlg.h> // for GetSaveFileName
# include <direct.h> // getcwd
# include <io.h>
# include <shellapi.h>
# include <shlobj.h> // for SHGetFolderPath
# include <tchar.h>
# include "common/string_util.h"
# include "common/string_util.h"
// 64 bit offsets for windows
# define fseeko _fseeki64
# define ftello _ftelli64
# define atoll _atoi64
# define stat64 _stat64
# define fstat64 _fstat64
# define fileno _fileno
// 64 bit offsets for windows
# define fseeko _fseeki64
# define ftello _ftelli64
# define atoll _atoi64
# define stat64 _stat64
# define fstat64 _fstat64
# define fileno _fileno
# else
# ifdef __APPLE__
# include <sys/param.h>
# endif
# include <cctype>
# include <cerrno>
# include <cstdlib>
# include <cstring>
# include <dirent.h>
# include <pwd.h>
# include <unistd.h>
# ifdef __APPLE__
# include <sys/param.h>
# endif
# include <cctype>
# include <cerrno>
# include <cstdlib>
# include <cstring>
# include <dirent.h>
# include <pwd.h>
# include <unistd.h>
# endif
# if defined(__APPLE__)
# include <CoreFoundation/CF String .h>
# include <CoreFoundation/CF URL .h>
# include <CoreFoundation/CF Bundle .h>
# include <CoreFoundation/CF Bundle .h>
# include <CoreFoundation/CF String .h>
# include <CoreFoundation/CF URL .h>
# endif
# include <algorithm>
# include <sys/stat.h>
# ifndef S_ISDIR
# define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
# define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
# endif
# ifdef BSD4_4
# define stat64 stat
# define fstat64 fstat
# define stat64 stat
# define fstat64 fstat
# endif
// This namespace has various generic functions related to files and paths.
// The code still needs a ton of cleanup.
// REMEMBER: strdup considered harmful!
namespace FileUtil
{
namespace FileUtil {
// Remove any ending forward slashes from directory paths
// Modifies argument.
static void StripTailDirSlashes ( std : : string & fname )
{
if ( fname . length ( ) > 1 )
{
static void StripTailDirSlashes ( std : : string & fname ) {
if ( fname . length ( ) > 1 ) {
size_t i = fname . length ( ) ;
while ( i > 0 & & fname [ i - 1 ] = = DIR_SEP_CHR )
- - i ;
@ -78,8 +75,7 @@ static void StripTailDirSlashes(std::string &fname)
}
// Returns true if file filename exists
bool Exists ( const std : : string & filename )
{
bool Exists ( const std : : string & filename ) {
struct stat64 file_info ;
std : : string copy ( filename ) ;
@ -99,8 +95,7 @@ bool Exists(const std::string &filename)
}
// Returns true if filename is a directory
bool IsDirectory ( const std : : string & filename )
{
bool IsDirectory ( const std : : string & filename ) {
struct stat64 file_info ;
std : : string copy ( filename ) ;
@ -117,8 +112,8 @@ bool IsDirectory(const std::string &filename)
# endif
if ( result < 0 ) {
LOG_WARNING ( Common_Filesystem , " stat failed on %s: %s " ,
filename . c_str ( ) , GetLastErrorMsg ( ) ) ;
LOG_WARNING ( Common_Filesystem , " stat failed on %s: %s " , filename . c_str ( ) ,
GetLastErrorMsg ( ) ) ;
return false ;
}
@ -127,36 +122,32 @@ bool IsDirectory(const std::string &filename)
// Deletes a given filename, return true on success
// Doesn't supports deleting a directory
bool Delete ( const std : : string & filename )
{
bool Delete ( const std : : string & filename ) {
LOG_INFO ( Common_Filesystem , " file %s " , filename . c_str ( ) ) ;
// Return true because we care about the file no
// being there, not the actual delete.
if ( ! Exists ( filename ) )
{
if ( ! Exists ( filename ) ) {
LOG_WARNING ( Common_Filesystem , " %s does not exist " , filename . c_str ( ) ) ;
return true ;
}
// We can't delete a directory
if ( IsDirectory ( filename ) )
{
if ( IsDirectory ( filename ) ) {
LOG_ERROR ( Common_Filesystem , " Failed: %s is a directory " , filename . c_str ( ) ) ;
return false ;
}
# ifdef _WIN32
if ( ! DeleteFileW ( Common : : UTF8ToUTF16W ( filename ) . c_str ( ) ) )
{
LOG_ERROR ( Common_Filesystem , " DeleteFile failed on %s: %s " ,
filename . c_str ( ) , GetLastErrorMsg ( ) ) ;
if ( ! DeleteFileW ( Common : : UTF8ToUTF16W ( filename ) . c_str ( ) ) ) {
LOG_ERROR ( Common_Filesystem , " DeleteFile failed on %s: %s " , filename . c_str ( ) ,
GetLastErrorMsg ( ) ) ;
return false ;
}
# else
if ( unlink ( filename . c_str ( ) ) = = - 1 ) {
LOG_ERROR ( Common_Filesystem , " unlink failed on %s: %s " ,
filename . c_str ( ) , GetLastErrorMsg ( ) ) ;
LOG_ERROR ( Common_Filesystem , " unlink failed on %s: %s " , filename . c_str ( ) ,
GetLastErrorMsg ( ) ) ;
return false ;
}
# endif
@ -165,16 +156,15 @@ bool Delete(const std::string &filename)
}
// Returns true if successful, or path already exists.
bool CreateDir ( const std : : string & path )
{
bool CreateDir ( const std : : string & path ) {
LOG_TRACE ( Common_Filesystem , " directory %s " , path . c_str ( ) ) ;
# ifdef _WIN32
if ( : : CreateDirectoryW ( Common : : UTF8ToUTF16W ( path ) . c_str ( ) , nullptr ) )
return true ;
DWORD error = GetLastError ( ) ;
if ( error = = ERROR_ALREADY_EXISTS )
{
LOG_WARNING ( Common_Filesystem , " CreateDirectory failed on %s: already exists " , path . c_str ( ) ) ;
if ( error = = ERROR_ALREADY_EXISTS ) {
LOG_WARNING ( Common_Filesystem , " CreateDirectory failed on %s: already exists " ,
path . c_str ( ) ) ;
return true ;
}
LOG_ERROR ( Common_Filesystem , " CreateDirectory failed on %s: %i " , path . c_str ( ) , error ) ;
@ -185,8 +175,7 @@ bool CreateDir(const std::string &path)
int err = errno ;
if ( err = = EEXIST )
{
if ( err = = EEXIST ) {
LOG_WARNING ( Common_Filesystem , " mkdir failed on %s: already exists " , path . c_str ( ) ) ;
return true ;
}
@ -197,20 +186,17 @@ bool CreateDir(const std::string &path)
}
// Creates the full path of fullPath returns true on success
bool CreateFullPath ( const std : : string & fullPath )
{
bool CreateFullPath ( const std : : string & fullPath ) {
int panicCounter = 100 ;
LOG_TRACE ( Common_Filesystem , " path %s " , fullPath . c_str ( ) ) ;
if ( FileUtil : : Exists ( fullPath ) )
{
if ( FileUtil : : Exists ( fullPath ) ) {
LOG_WARNING ( Common_Filesystem , " path exists %s " , fullPath . c_str ( ) ) ;
return true ;
}
size_t position = 0 ;
while ( true )
{
while ( true ) {
// Find next sub path
position = fullPath . find ( DIR_SEP_CHR , position ) ;
@ -227,8 +213,7 @@ bool CreateFullPath(const std::string &fullPath)
// A safety check
panicCounter - - ;
if ( panicCounter < = 0 )
{
if ( panicCounter < = 0 ) {
LOG_ERROR ( Common , " CreateFullPath: directory structure is too deep " ) ;
return false ;
}
@ -236,15 +221,12 @@ bool CreateFullPath(const std::string &fullPath)
}
}
// Deletes a directory filename, returns true on success
bool DeleteDir ( const std : : string & filename )
{
bool DeleteDir ( const std : : string & filename ) {
LOG_INFO ( Common_Filesystem , " directory %s " , filename . c_str ( ) ) ;
// check if a directory
if ( ! FileUtil : : IsDirectory ( filename ) )
{
if ( ! FileUtil : : IsDirectory ( filename ) ) {
LOG_ERROR ( Common_Filesystem , " Not a directory %s " , filename . c_str ( ) ) ;
return false ;
}
@ -262,71 +244,63 @@ bool DeleteDir(const std::string &filename)
}
// renames file srcFilename to destFilename, returns true on success
bool Rename ( const std : : string & srcFilename , const std : : string & destFilename )
{
LOG_TRACE ( Common_Filesystem , " %s --> %s " ,
srcFilename . c_str ( ) , destFilename . c_str ( ) ) ;
bool Rename ( const std : : string & srcFilename , const std : : string & destFilename ) {
LOG_TRACE ( Common_Filesystem , " %s --> %s " , srcFilename . c_str ( ) , destFilename . c_str ( ) ) ;
# ifdef _WIN32
if ( _wrename ( Common : : UTF8ToUTF16W ( srcFilename ) . c_str ( ) , Common : : UTF8ToUTF16W ( destFilename ) . c_str ( ) ) = = 0 )
if ( _wrename ( Common : : UTF8ToUTF16W ( srcFilename ) . c_str ( ) ,
Common : : UTF8ToUTF16W ( destFilename ) . c_str ( ) ) = = 0 )
return true ;
# else
if ( rename ( srcFilename . c_str ( ) , destFilename . c_str ( ) ) = = 0 )
return true ;
# endif
LOG_ERROR ( Common_Filesystem , " failed %s --> %s: %s " ,
srcFilename. c_str ( ) , destFilename . c_str ( ) , GetLastErrorMsg( ) ) ;
LOG_ERROR ( Common_Filesystem , " failed %s --> %s: %s " , srcFilename . c_str ( ) , destFilename . c_str ( ) ,
GetLastErrorMsg( ) ) ;
return false ;
}
// copies file srcFilename to destFilename, returns true on success
bool Copy ( const std : : string & srcFilename , const std : : string & destFilename )
{
LOG_TRACE ( Common_Filesystem , " %s --> %s " ,
srcFilename . c_str ( ) , destFilename . c_str ( ) ) ;
bool Copy ( const std : : string & srcFilename , const std : : string & destFilename ) {
LOG_TRACE ( Common_Filesystem , " %s --> %s " , srcFilename . c_str ( ) , destFilename . c_str ( ) ) ;
# ifdef _WIN32
if ( CopyFileW ( Common : : UTF8ToUTF16W ( srcFilename ) . c_str ( ) , Common : : UTF8ToUTF16W ( destFilename ) . c_str ( ) , FALSE ) )
if ( CopyFileW ( Common : : UTF8ToUTF16W ( srcFilename ) . c_str ( ) ,
Common : : UTF8ToUTF16W ( destFilename ) . c_str ( ) , FALSE ) )
return true ;
LOG_ERROR ( Common_Filesystem , " failed %s --> %s: %s " ,
srcFilename . c_str ( ) , destFilename . c_str ( ) , GetLastErrorMsg ( ) ) ;
LOG_ERROR ( Common_Filesystem , " failed %s --> %s: %s " , srcFilename . c_str ( ) , destFilename . c_str ( ) ,
GetLastErrorMsg ( ) ) ;
return false ;
# else
// buffer size
// buffer size
# define BSIZE 1024
char buffer [ BSIZE ] ;
// Open input file
FILE * input = fopen ( srcFilename . c_str ( ) , " rb " ) ;
if ( ! input )
{
LOG_ERROR ( Common_Filesystem , " opening input failed %s --> %s: %s " ,
srcFilename . c_str ( ) , destFilename . c_str ( ) , GetLastErrorMsg ( ) ) ;
FILE * input = fopen ( srcFilename . c_str ( ) , " rb " ) ;
if ( ! input ) {
LOG_ERROR ( Common_Filesystem , " opening input failed %s --> %s: %s " , srcFilename . c_str ( ) ,
destFilename . c_str ( ) , GetLastErrorMsg ( ) ) ;
return false ;
}
// open output file
FILE * output = fopen ( destFilename . c_str ( ) , " wb " ) ;
if ( ! output )
{
FILE * output = fopen ( destFilename . c_str ( ) , " wb " ) ;
if ( ! output ) {
fclose ( input ) ;
LOG_ERROR ( Common_Filesystem , " opening output failed %s --> %s: %s " ,
srcFilename . c_str ( ) , destFilename . c_str ( ) , GetLastErrorMsg ( ) ) ;
LOG_ERROR ( Common_Filesystem , " opening output failed %s --> %s: %s " , srcFilename . c_str ( ) ,
destFilename . c_str ( ) , GetLastErrorMsg ( ) ) ;
return false ;
}
// copy loop
while ( ! feof ( input ) )
{
while ( ! feof ( input ) ) {
// read input
int rnum = fread ( buffer , sizeof ( char ) , BSIZE , input ) ;
if ( rnum ! = BSIZE )
{
if ( ferror ( input ) ! = 0 )
{
LOG_ERROR ( Common_Filesystem ,
" failed reading from source, %s --> %s: %s " ,
if ( rnum ! = BSIZE ) {
if ( ferror ( input ) ! = 0 ) {
LOG_ERROR ( Common_Filesystem , " failed reading from source, %s --> %s: %s " ,
srcFilename . c_str ( ) , destFilename . c_str ( ) , GetLastErrorMsg ( ) ) ;
goto bail ;
}
@ -334,10 +308,8 @@ bool Copy(const std::string &srcFilename, const std::string &destFilename)
// write output
int wnum = fwrite ( buffer , sizeof ( char ) , rnum , output ) ;
if ( wnum ! = rnum )
{
LOG_ERROR ( Common_Filesystem ,
" failed writing to output, %s --> %s: %s " ,
if ( wnum ! = rnum ) {
LOG_ERROR ( Common_Filesystem , " failed writing to output, %s --> %s: %s " ,
srcFilename . c_str ( ) , destFilename . c_str ( ) , GetLastErrorMsg ( ) ) ;
goto bail ;
}
@ -356,16 +328,13 @@ bail:
}
// Returns the size of filename (64bit)
u64 GetSize ( const std : : string & filename )
{
if ( ! Exists ( filename ) )
{
u64 GetSize ( const std : : string & filename ) {
if ( ! Exists ( filename ) ) {
LOG_ERROR ( Common_Filesystem , " failed %s: No such file " , filename . c_str ( ) ) ;
return 0 ;
}
if ( IsDirectory ( filename ) )
{
if ( IsDirectory ( filename ) ) {
LOG_ERROR ( Common_Filesystem , " failed %s: is a directory " , filename . c_str ( ) ) ;
return 0 ;
}
@ -377,65 +346,54 @@ u64 GetSize(const std::string &filename)
if ( stat64 ( filename . c_str ( ) , & buf ) = = 0 )
# endif
{
LOG_TRACE ( Common_Filesystem , " %s: %lld " ,
filename . c_str ( ) , ( long long ) buf . st_size ) ;
LOG_TRACE ( Common_Filesystem , " %s: %lld " , filename . c_str ( ) , ( long long ) buf . st_size ) ;
return buf . st_size ;
}
LOG_ERROR ( Common_Filesystem , " Stat failed %s: %s " ,
filename . c_str ( ) , GetLastErrorMsg ( ) ) ;
LOG_ERROR ( Common_Filesystem , " Stat failed %s: %s " , filename . c_str ( ) , GetLastErrorMsg ( ) ) ;
return 0 ;
}
// Overloaded GetSize, accepts file descriptor
u64 GetSize ( const int fd )
{
u64 GetSize ( const int fd ) {
struct stat64 buf ;
if ( fstat64 ( fd , & buf ) ! = 0 ) {
LOG_ERROR ( Common_Filesystem , " GetSize: stat failed %i: %s " ,
fd , GetLastErrorMsg ( ) ) ;
LOG_ERROR ( Common_Filesystem , " GetSize: stat failed %i: %s " , fd , GetLastErrorMsg ( ) ) ;
return 0 ;
}
return buf . st_size ;
}
// Overloaded GetSize, accepts FILE*
u64 GetSize ( FILE * f )
{
u64 GetSize ( FILE * f ) {
// can't use off_t here because it can be 32-bit
u64 pos = ftello ( f ) ;
if ( fseeko ( f , 0 , SEEK_END ) ! = 0 ) {
LOG_ERROR ( Common_Filesystem , " GetSize: seek failed %p: %s " ,
f , GetLastErrorMsg ( ) ) ;
LOG_ERROR ( Common_Filesystem , " GetSize: seek failed %p: %s " , f , GetLastErrorMsg ( ) ) ;
return 0 ;
}
u64 size = ftello ( f ) ;
if ( ( size ! = pos ) & & ( fseeko ( f , pos , SEEK_SET ) ! = 0 ) ) {
LOG_ERROR ( Common_Filesystem , " GetSize: seek failed %p: %s " ,
f , GetLastErrorMsg ( ) ) ;
LOG_ERROR ( Common_Filesystem , " GetSize: seek failed %p: %s " , f , GetLastErrorMsg ( ) ) ;
return 0 ;
}
return size ;
}
// creates an empty file filename, returns true on success
bool CreateEmptyFile ( const std : : string & filename )
{
bool CreateEmptyFile ( const std : : string & filename ) {
LOG_TRACE ( Common_Filesystem , " %s " , filename . c_str ( ) ) ;
if ( ! FileUtil : : IOFile ( filename , " wb " ) )
{
LOG_ERROR ( Common_Filesystem , " failed %s: %s " ,
filename . c_str ( ) , GetLastErrorMsg ( ) ) ;
if ( ! FileUtil : : IOFile ( filename , " wb " ) ) {
LOG_ERROR ( Common_Filesystem , " failed %s: %s " , filename . c_str ( ) , GetLastErrorMsg ( ) ) ;
return false ;
}
return true ;
}
bool ForeachDirectoryEntry ( unsigned * num_entries_out , const std : : string & directory , DirectoryEntryCallable callback )
{
bool ForeachDirectoryEntry ( unsigned * num_entries_out , const std : : string & directory ,
DirectoryEntryCallable callback ) {
LOG_TRACE ( Common_Filesystem , " directory %s " , directory . c_str ( ) ) ;
// How many files + directories we found
@ -457,7 +415,7 @@ bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directo
do {
const std : : string virtual_name ( Common : : UTF16ToUTF8 ( ffd . cFileName ) ) ;
# else
DIR * dirp = opendir ( directory . c_str ( ) ) ;
DIR * dirp = opendir ( directory . c_str ( ) ) ;
if ( ! dirp )
return false ;
@ -493,8 +451,8 @@ bool ForeachDirectoryEntry(unsigned* num_entries_out, const std::string &directo
return true ;
}
unsigned ScanDirectoryTree ( const std : : string & directory , FSTEntry & parent_entry , unsigned int recursion )
{
unsigned ScanDirectoryTree ( const std : : string & directory , FSTEntry & parent_entry ,
unsigned int recursion ) {
const auto callback = [ recursion , & parent_entry ] ( unsigned * num_entries_out ,
const std : : string & directory ,
const std : : string & virtual_name ) - > bool {
@ -526,11 +484,8 @@ unsigned ScanDirectoryTree(const std::string &directory, FSTEntry& parent_entry,
return ForeachDirectoryEntry ( & num_entries , directory , callback ) ? num_entries : 0 ;
}
bool DeleteDirRecursively ( const std : : string & directory , unsigned int recursion )
{
const auto callback = [ recursion ] ( unsigned * num_entries_out ,
const std : : string & directory ,
bool DeleteDirRecursively ( const std : : string & directory , unsigned int recursion ) {
const auto callback = [ recursion ] ( unsigned * num_entries_out , const std : : string & directory ,
const std : : string & virtual_name ) - > bool {
std : : string new_path = directory + DIR_SEP_CHR + virtual_name ;
@ -551,53 +506,53 @@ bool DeleteDirRecursively(const std::string &directory, unsigned int recursion)
}
// Create directory and copy contents (does not overwrite existing files)
void CopyDir ( const std : : string & source_path , const std : : string & dest_path )
{
void CopyDir ( const std : : string & source_path , const std : : string & dest_path ) {
# ifndef _WIN32
if ( source_path = = dest_path ) return ;
if ( ! FileUtil : : Exists ( source_path ) ) return ;
if ( ! FileUtil : : Exists ( dest_path ) ) FileUtil : : CreateFullPath ( dest_path ) ;
if ( source_path = = dest_path )
return ;
if ( ! FileUtil : : Exists ( source_path ) )
return ;
if ( ! FileUtil : : Exists ( dest_path ) )
FileUtil : : CreateFullPath ( dest_path ) ;
DIR * dirp = opendir ( source_path . c_str ( ) ) ;
if ( ! dirp ) return ;
DIR * dirp = opendir ( source_path . c_str ( ) ) ;
if ( ! dirp )
return ;
while ( struct dirent * result = readdir ( dirp ) ) {
const std : : string virtualName ( result - > d_name ) ;
// check for "." and ".."
if ( ( ( virtualName [ 0 ] = = ' . ' ) & & ( virtualName [ 1 ] = = ' \0 ' ) ) | |
( ( virtualName [ 0 ] = = ' . ' ) & & ( virtualName [ 1 ] = = ' . ' ) & &
( virtualName [ 2 ] = = ' \0 ' ) ) )
( ( virtualName [ 0 ] = = ' . ' ) & & ( virtualName [ 1 ] = = ' . ' ) & & ( virtualName [ 2 ] = = ' \0 ' ) ) )
continue ;
std : : string source , dest ;
source = source_path + virtualName ;
dest = dest_path + virtualName ;
if ( IsDirectory ( source ) )
{
if ( IsDirectory ( source ) ) {
source + = ' / ' ;
dest + = ' / ' ;
if ( ! FileUtil : : Exists ( dest ) ) FileUtil : : CreateFullPath ( dest ) ;
if ( ! FileUtil : : Exists ( dest ) )
FileUtil : : CreateFullPath ( dest ) ;
CopyDir ( source , dest ) ;
}
else if ( ! FileUtil : : Exists ( dest ) ) FileUtil : : Copy ( source , dest ) ;
} else if ( ! FileUtil : : Exists ( dest ) )
FileUtil : : Copy ( source , dest ) ;
}
closedir ( dirp ) ;
# endif
}
// Returns the current directory
std : : string GetCurrentDir ( )
{
// Get the current working directory (getcwd uses malloc)
std : : string GetCurrentDir ( ) {
// Get the current working directory (getcwd uses malloc)
# ifdef _WIN32
wchar_t * dir ;
wchar_t * dir ;
if ( ! ( dir = _wgetcwd ( nullptr , 0 ) ) ) {
# else
char * dir ;
char * dir ;
if ( ! ( dir = getcwd ( nullptr , 0 ) ) ) {
# endif
LOG_ERROR ( Common_Filesystem , " GetCurrentDirectory failed: %s " ,
GetLastErrorMsg ( ) ) ;
LOG_ERROR ( Common_Filesystem , " GetCurrentDirectory failed: %s " , GetLastErrorMsg ( ) ) ;
return nullptr ;
}
# ifdef _WIN32
@ -610,8 +565,7 @@ std::string GetCurrentDir()
}
// Sets the current directory to the given directory
bool SetCurrentDir ( const std : : string & directory )
{
bool SetCurrentDir ( const std : : string & directory ) {
# ifdef _WIN32
return _wchdir ( Common : : UTF8ToUTF16W ( directory ) . c_str ( ) ) = = 0 ;
# else
@ -620,8 +574,7 @@ bool SetCurrentDir(const std::string &directory)
}
# if defined(__APPLE__)
std : : string GetBundleDirectory ( )
{
std : : string GetBundleDirectory ( ) {
CFURLRef BundleRef ;
char AppBundlePath [ MAXPATHLEN ] ;
// Get the main bundle for the app
@ -636,11 +589,9 @@ std::string GetBundleDirectory()
# endif
# ifdef _WIN32
std : : string & GetExeDirectory ( )
{
std : : string & GetExeDirectory ( ) {
static std : : string exe_path ;
if ( exe_path . empty ( ) )
{
if ( exe_path . empty ( ) ) {
wchar_t wchar_exe_path [ 2048 ] ;
GetModuleFileNameW ( nullptr , wchar_exe_path , 2048 ) ;
exe_path = Common : : UTF16ToUTF8 ( wchar_exe_path ) ;
@ -660,7 +611,8 @@ static const std::string& GetHomeDirectory() {
home_path = envvar ;
} else {
auto pw = getpwuid ( getuid ( ) ) ;
ASSERT_MSG ( pw , " $HOME isn’ t defined, and the current user can’ t be found in /etc/passwd. " ) ;
ASSERT_MSG ( pw ,
" $HOME isn’ t defined, and the current user can’ t be found in /etc/passwd. " ) ;
home_path = pw - > pw_dir ;
}
}
@ -699,11 +651,10 @@ static const std::string GetUserDirectory(const std::string& envvar) {
}
# endif
std : : string GetSysDirectory ( )
{
std : : string GetSysDirectory ( ) {
std : : string sysDir ;
# if defined (__APPLE__)
# if defined (__APPLE__)
sysDir = GetBundleDirectory ( ) ;
sysDir + = DIR_SEP ;
sysDir + = SYSDATA_DIR ;
@ -718,13 +669,11 @@ std::string GetSysDirectory()
// Returns a string with a Citra data dir or file in the user's home
// directory. To be used in "multi-user" mode (that is, installed).
const std : : string & GetUserPath ( const unsigned int DirIDX , const std : : string & newPath )
{
const std : : string & GetUserPath ( const unsigned int DirIDX , const std : : string & newPath ) {
static std : : string paths [ NUM_PATH_INDICES ] ;
// Set up all paths and files on the first run
if ( paths [ D_USER_IDX ] . empty ( ) )
{
if ( paths [ D_USER_IDX ] . empty ( ) ) {
# ifdef _WIN32
paths [ D_USER_IDX ] = GetExeDirectory ( ) + DIR_SEP USERDATA_DIR DIR_SEP ;
paths [ D_CONFIG_IDX ] = paths [ D_USER_IDX ] + CONFIG_DIR DIR_SEP ;
@ -764,20 +713,15 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string &new
paths [ F_MAINLOG_IDX ] = paths [ D_LOGS_IDX ] + MAIN_LOG ;
}
if ( ! newPath . empty ( ) )
{
if ( ! FileUtil : : IsDirectory ( newPath ) )
{
if ( ! newPath . empty ( ) ) {
if ( ! FileUtil : : IsDirectory ( newPath ) ) {
LOG_ERROR ( Common_Filesystem , " Invalid path specified %s " , newPath . c_str ( ) ) ;
return paths [ DirIDX ] ;
}
else
{
} else {
paths [ DirIDX ] = newPath ;
}
switch ( DirIDX )
{
switch ( DirIDX ) {
case D_ROOT_IDX :
paths [ D_USER_IDX ] = paths [ D_ROOT_IDX ] + DIR_SEP ;
paths [ D_SYSCONF_IDX ] = paths [ D_USER_IDX ] + SYSCONF_DIR + DIR_SEP ;
@ -828,13 +772,11 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string &new
return paths [ DirIDX ] ;
}
size_t WriteStringToFile ( bool text_file , const std : : string & str , const char * filename )
{
size_t WriteStringToFile ( bool text_file , const std : : string & str , const char * filename ) {
return FileUtil : : IOFile ( filename , text_file ? " w " : " wb " ) . WriteBytes ( str . data ( ) , str . size ( ) ) ;
}
size_t ReadFileToString ( bool text_file , const char * filename , std : : string & str )
{
size_t ReadFileToString ( bool text_file , const char * filename , std : : string & str ) {
IOFile file ( filename , text_file ? " r " : " rb " ) ;
if ( ! file )
@ -886,42 +828,35 @@ void SplitFilename83(const std::string& filename, std::array<char, 9>& short_nam
}
}
IOFile : : IOFile ( )
{
}
IOFile : : IOFile ( ) { }
IOFile : : IOFile ( const std : : string & filename , const char openmode [ ] )
{
IOFile : : IOFile ( const std : : string & filename , const char openmode [ ] ) {
Open ( filename , openmode ) ;
}
IOFile : : ~ IOFile ( )
{
IOFile : : ~ IOFile ( ) {
Close ( ) ;
}
IOFile : : IOFile ( IOFile & & other )
{
IOFile : : IOFile ( IOFile & & other ) {
Swap ( other ) ;
}
IOFile & IOFile : : operator = ( IOFile & & other )
{
IOFile & IOFile : : operator = ( IOFile & & other ) {
Swap ( other ) ;
return * this ;
}
void IOFile : : Swap ( IOFile & other )
{
void IOFile : : Swap ( IOFile & other ) {
std : : swap ( m_file , other . m_file ) ;
std : : swap ( m_good , other . m_good ) ;
}
bool IOFile : : Open ( const std : : string & filename , const char openmode [ ] )
{
bool IOFile : : Open ( const std : : string & filename , const char openmode [ ] ) {
Close ( ) ;
# ifdef _WIN32
_wfopen_s ( & m_file , Common : : UTF8ToUTF16W ( filename ) . c_str ( ) , Common : : UTF8ToUTF16W ( openmode ) . c_str ( ) ) ;
_wfopen_s ( & m_file , Common : : UTF8ToUTF16W ( filename ) . c_str ( ) ,
Common : : UTF8ToUTF16W ( openmode ) . c_str ( ) ) ;
# else
m_file = fopen ( filename . c_str ( ) , openmode ) ;
# endif
@ -930,8 +865,7 @@ bool IOFile::Open(const std::string& filename, const char openmode[])
return m_good ;
}
bool IOFile : : Close ( )
{
bool IOFile : : Close ( ) {
if ( ! IsOpen ( ) | | 0 ! = std : : fclose ( m_file ) )
m_good = false ;
@ -939,41 +873,37 @@ bool IOFile::Close()
return m_good ;
}
u64 IOFile : : GetSize ( ) const
{
u64 IOFile : : GetSize ( ) const {
if ( IsOpen ( ) )
return FileUtil : : GetSize ( m_file ) ;
return 0 ;
}
bool IOFile : : Seek ( s64 off , int origin )
{
bool IOFile : : Seek ( s64 off , int origin ) {
if ( ! IsOpen ( ) | | 0 ! = fseeko ( m_file , off , origin ) )
m_good = false ;
return m_good ;
}
u64 IOFile : : Tell ( ) const
{
u64 IOFile : : Tell ( ) const {
if ( IsOpen ( ) )
return ftello ( m_file ) ;
return - 1 ;
}
bool IOFile : : Flush ( )
{
bool IOFile : : Flush ( ) {
if ( ! IsOpen ( ) | | 0 ! = std : : fflush ( m_file ) )
m_good = false ;
return m_good ;
}
bool IOFile : : Resize ( u64 size )
{
if ( ! IsOpen ( ) | | 0 ! =
bool IOFile : : Resize ( u64 size ) {
if ( ! IsOpen ( ) | |
0 ! =
# ifdef _WIN32
// ector: _chsize sucks, not 64-bit safe
// F|RES: changed to _chsize_s. i think it is 64-bit safe