| 
									
										
										
										
											2009-07-28 21:32:10 +00:00
										 |  |  | // Copyright (C) 2003 Dolphin Project.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify
 | 
					
						
							|  |  |  | // it under the terms of the GNU General Public License as published by
 | 
					
						
							|  |  |  | // the Free Software Foundation, version 2.0.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful,
 | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
					
						
							|  |  |  | // GNU General Public License 2.0 for more details.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // A copy of the GPL 2.0 should have been included with the program.
 | 
					
						
							|  |  |  | // If not, see http://www.gnu.org/licenses/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Official SVN repository and contact information can be found at
 | 
					
						
							|  |  |  | // http://code.google.com/p/dolphin-emu/
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Common.h"
 | 
					
						
							| 
									
										
										
										
											2010-07-20 03:23:25 +00:00
										 |  |  | #include "CommonPaths.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #include "FileUtil.h"
 | 
					
						
							| 
									
										
										
										
											2009-07-12 21:58:32 +00:00
										 |  |  | #include "StringUtil.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2009-01-16 02:58:34 +00:00
										 |  |  | #include <windows.h>
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #include <shlobj.h>		// for SHGetFolderPath
 | 
					
						
							|  |  |  | #include <shellapi.h>
 | 
					
						
							|  |  |  | #include <commdlg.h>	// for GetSaveFileName
 | 
					
						
							|  |  |  | #include <io.h>
 | 
					
						
							|  |  |  | #include <direct.h>		// getcwd
 | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-07-23 23:51:34 +00:00
										 |  |  | #include <sys/param.h>
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #include <sys/types.h>
 | 
					
						
							|  |  |  | #include <dirent.h>
 | 
					
						
							|  |  |  | #include <errno.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 23:21:51 +00:00
										 |  |  | #if defined(__APPLE__)
 | 
					
						
							|  |  |  | #include <CoreFoundation/CFString.h>
 | 
					
						
							| 
									
										
										
										
											2010-05-06 10:06:15 +00:00
										 |  |  | #include <CoreFoundation/CFURL.h>
 | 
					
						
							| 
									
										
										
										
											2009-02-28 23:21:51 +00:00
										 |  |  | #include <CoreFoundation/CFBundle.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #include <fstream>
 | 
					
						
							|  |  |  | #include <sys/stat.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef S_ISDIR
 | 
					
						
							|  |  |  | #define S_ISDIR(m)  (((m)&S_IFMT) == S_IFDIR)
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-07-23 23:51:34 +00:00
										 |  |  | #ifdef BSD4_4
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | #define stat64 stat
 | 
					
						
							|  |  |  | #define fstat64 fstat
 | 
					
						
							| 
									
										
										
										
											2010-07-22 03:29:35 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | // This namespace has various generic functions related to files and paths.
 | 
					
						
							|  |  |  | // The code still needs a ton of cleanup.
 | 
					
						
							|  |  |  | // REMEMBER: strdup considered harmful!
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | namespace File | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Remove any ending forward slashes from directory paths
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // Modifies argument.
 | 
					
						
							| 
									
										
										
										
											2011-02-28 20:40:15 +00:00
										 |  |  | static void StripTailDirSlashes(std::string &fname) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-02-28 20:40:15 +00:00
										 |  |  | 	if (fname.length() > 1) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		size_t i = fname.length() - 1; | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		while (fname[i] == DIR_SEP_CHR) | 
					
						
							|  |  |  | 			fname[i--] = '\0'; | 
					
						
							| 
									
										
										
										
											2011-02-28 20:40:15 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	return; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // Returns true if file filename exists
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool Exists(const std::string &filename) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-02-01 18:22:58 +00:00
										 |  |  | 	struct stat64 file_info; | 
					
						
							| 
									
										
										
										
											2011-02-28 20:40:15 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	std::string copy(filename); | 
					
						
							|  |  |  | 	StripTailDirSlashes(copy); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int result = stat64(copy.c_str(), &file_info); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return (result == 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // Returns true if filename is a directory
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool IsDirectory(const std::string &filename) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-02-08 09:57:52 +00:00
										 |  |  | 	struct stat64 file_info; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-28 20:40:15 +00:00
										 |  |  | 	std::string copy(filename); | 
					
						
							|  |  |  | 	StripTailDirSlashes(copy); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-02-28 20:40:15 +00:00
										 |  |  | 	int result = stat64(copy.c_str(), &file_info); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (result < 0) { | 
					
						
							|  |  |  | 		WARN_LOG(COMMON, "IsDirectory: stat failed on %s: %s",  | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 				 filename.c_str(), GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return S_ISDIR(file_info.st_mode); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // Deletes a given filename, return true on success
 | 
					
						
							|  |  |  | // Doesn't supports deleting a directory
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool Delete(const std::string &filename) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	INFO_LOG(COMMON, "Delete: file %s", filename.c_str()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	// Return true because we care about the file no 
 | 
					
						
							|  |  |  | 	// being there, not the actual delete.
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (!Exists(filename)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		WARN_LOG(COMMON, "Delete: %s does not exists", filename.c_str()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// We can't delete a directory
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (IsDirectory(filename)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		WARN_LOG(COMMON, "Delete: %s is a directory", filename.c_str()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (!DeleteFile(filename.c_str())) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		WARN_LOG(COMMON, "Delete: DeleteFile failed on %s: %s",  | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 				 filename.c_str(), GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (unlink(filename.c_str()) == -1) { | 
					
						
							| 
									
										
										
										
											2010-04-08 16:59:35 +00:00
										 |  |  | 		WARN_LOG(COMMON, "Delete: unlink failed on %s: %s",  | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 				 filename.c_str(), GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Returns true if successful, or path already exists.
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool CreateDir(const std::string &path) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	INFO_LOG(COMMON, "CreateDir: directory %s", path.c_str()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (::CreateDirectory(path.c_str(), NULL)) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	DWORD error = GetLastError(); | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (error == ERROR_ALREADY_EXISTS) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		WARN_LOG(COMMON, "CreateDir: CreateDirectory failed on %s: already exists", path.c_str()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	ERROR_LOG(COMMON, "CreateDir: CreateDirectory failed on %s: %i", path.c_str(), error); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	return false; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (mkdir(path.c_str(), 0755) == 0) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	int err = errno; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (err == EEXIST) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		WARN_LOG(COMMON, "CreateDir: mkdir failed on %s: already exists", path.c_str()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	ERROR_LOG(COMMON, "CreateDir: mkdir failed on %s: %s", path.c_str(), strerror(err)); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	return false; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // Creates the full path of fullPath returns true on success
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool CreateFullPath(const std::string &fullPath) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	int panicCounter = 100; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	INFO_LOG(COMMON, "CreateFullPath: path %s", fullPath.c_str()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (File::Exists(fullPath)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		INFO_LOG(COMMON, "CreateFullPath: path exists %s", fullPath.c_str()); | 
					
						
							| 
									
										
										
										
											2009-03-03 00:21:08 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	size_t position = 0; | 
					
						
							|  |  |  | 	while (true) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-04-16 17:24:06 +00:00
										 |  |  | 		// Find next sub path
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		position = fullPath.find(DIR_SEP_CHR, position); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		// we're done, yay!
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		if (position == fullPath.npos) | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 			return true; | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		std::string subPath = fullPath.substr(0, position); | 
					
						
							|  |  |  | 		if (!File::IsDirectory(subPath)) | 
					
						
							|  |  |  | 			File::CreateDir(subPath); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		// A safety check
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		panicCounter--; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		if (panicCounter <= 0) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-04-16 17:24:06 +00:00
										 |  |  | 			ERROR_LOG(COMMON, "CreateFullPath: directory structure too deep"); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		position++; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // Deletes a directory filename, returns true on success
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool DeleteDir(const std::string &filename) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	INFO_LOG(COMMON, "DeleteDir: directory %s", filename.c_str()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	// check if a directory
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (!File::IsDirectory(filename)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		ERROR_LOG(COMMON, "DeleteDir: Not a directory %s", filename.c_str()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (::RemoveDirectory(filename.c_str())) | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (rmdir(filename.c_str()) == 0) | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	ERROR_LOG(COMMON, "DeleteDir: %s: %s", filename.c_str(), GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // renames file srcFilename to destFilename, returns true on success 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool Rename(const std::string &srcFilename, const std::string &destFilename) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	INFO_LOG(COMMON, "Rename: %s --> %s",  | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 			srcFilename.c_str(), destFilename.c_str()); | 
					
						
							|  |  |  | 	if (rename(srcFilename.c_str(), destFilename.c_str()) == 0) | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		return true; | 
					
						
							|  |  |  | 	ERROR_LOG(COMMON, "Rename: failed %s --> %s: %s",  | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 			  srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // copies file srcFilename to destFilename, returns true on success 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool Copy(const std::string &srcFilename, const std::string &destFilename) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	INFO_LOG(COMMON, "Copy: %s --> %s",  | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 			srcFilename.c_str(), destFilename.c_str()); | 
					
						
							| 
									
										
										
										
											2009-01-15 06:48:15 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (CopyFile(srcFilename.c_str(), destFilename.c_str(), FALSE)) | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		return true; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	ERROR_LOG(COMMON, "Copy: failed %s --> %s: %s",  | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 			srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	// buffer size
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #define BSIZE 1024
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	char buffer[BSIZE]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Open input file
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	FILE *input = fopen(srcFilename.c_str(), "rb"); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	if (!input) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		ERROR_LOG(COMMON, "Copy: input failed %s --> %s: %s",  | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 				srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	// open output file
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	FILE *output = fopen(destFilename.c_str(), "wb"); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	if (!output) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		fclose(input); | 
					
						
							|  |  |  | 		ERROR_LOG(COMMON, "Copy: output failed %s --> %s: %s",  | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 				srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-09 05:37:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	// copy loop
 | 
					
						
							|  |  |  | 	while (!feof(input)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		// read input
 | 
					
						
							|  |  |  | 		int rnum = fread(buffer, sizeof(char), BSIZE, input); | 
					
						
							|  |  |  | 		if (rnum != BSIZE) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 			if (ferror(input) != 0) | 
					
						
							|  |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 				ERROR_LOG(COMMON,  | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 						"Copy: failed reading from source, %s --> %s: %s",  | 
					
						
							|  |  |  | 						srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 				return false; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2010-04-06 15:02:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		// write output
 | 
					
						
							| 
									
										
										
										
											2009-02-28 08:38:37 +00:00
										 |  |  | 		int wnum = fwrite(buffer, sizeof(char), rnum, output); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		if (wnum != rnum) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			ERROR_LOG(COMMON,  | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 					"Copy: failed writing to output, %s --> %s: %s",  | 
					
						
							|  |  |  | 					srcFilename.c_str(), destFilename.c_str(), GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 			return false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	// close flushs
 | 
					
						
							|  |  |  | 	fclose(input); | 
					
						
							|  |  |  | 	fclose(output); | 
					
						
							|  |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // Returns the size of filename (64bit)
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | u64 GetSize(const std::string &filename) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (!Exists(filename)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		WARN_LOG(COMMON, "GetSize: failed %s: No such file", filename.c_str()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2008-12-09 22:54:57 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (IsDirectory(filename)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		WARN_LOG(COMMON, "GetSize: failed %s: is a directory", filename.c_str()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	struct stat64 buf; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (stat64(filename.c_str(), &buf) == 0) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2010-12-05 15:59:11 +00:00
										 |  |  | 		DEBUG_LOG(COMMON, "GetSize: %s: %lld", | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 				filename.c_str(), (long long)buf.st_size); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		return buf.st_size; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	ERROR_LOG(COMMON, "GetSize: Stat failed %s: %s", | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 			filename.c_str(), GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	return 0; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-03 12:42:01 +00:00
										 |  |  | // Overloaded GetSize, accepts file descriptor
 | 
					
						
							|  |  |  | u64 GetSize(const int fd) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct stat64 buf; | 
					
						
							|  |  |  | 	if (fstat64(fd, &buf) != 0) { | 
					
						
							|  |  |  | 		ERROR_LOG(COMMON, "GetSize: stat failed %i: %s", | 
					
						
							|  |  |  | 			fd, GetLastErrorMsg()); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return buf.st_size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Overloaded GetSize, accepts FILE*
 | 
					
						
							|  |  |  | u64 GetSize(FILE *f) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-12-31 10:40:49 +00:00
										 |  |  | 	// can't use off_t here because it can be 32-bit
 | 
					
						
							|  |  |  | 	u64 pos = ftello(f); | 
					
						
							| 
									
										
										
										
											2010-12-21 18:03:44 +00:00
										 |  |  | 	if (fseeko(f, 0, SEEK_END) != 0) { | 
					
						
							|  |  |  | 		ERROR_LOG(COMMON, "GetSize: seek failed %p: %s", | 
					
						
							|  |  |  | 			  f, GetLastErrorMsg()); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-12-31 10:40:49 +00:00
										 |  |  | 	u64 size = ftello(f); | 
					
						
							| 
									
										
										
										
											2010-12-21 18:03:44 +00:00
										 |  |  | 	if ((size != pos) && (fseeko(f, pos, SEEK_SET) != 0)) { | 
					
						
							|  |  |  | 		ERROR_LOG(COMMON, "GetSize: seek failed %p: %s", | 
					
						
							|  |  |  | 			  f, GetLastErrorMsg()); | 
					
						
							|  |  |  | 		return 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return size; | 
					
						
							| 
									
										
										
										
											2010-12-03 12:42:01 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // creates an empty file filename, returns true on success 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool CreateEmptyFile(const std::string &filename) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	INFO_LOG(COMMON, "CreateEmptyFile: %s", filename.c_str());  | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	FILE *pFile = fopen(filename.c_str(), "wb"); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	if (!pFile) { | 
					
						
							|  |  |  | 		ERROR_LOG(COMMON, "CreateEmptyFile: failed %s: %s", | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 				  filename.c_str(), GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	fclose(pFile); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // Scans the directory tree gets, starting from _Directory and adds the
 | 
					
						
							|  |  |  | // results into parentEntry. Returns the number of files+directories found
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	INFO_LOG(COMMON, "ScanDirectoryTree: directory %s", directory.c_str()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	// How many files + directories we found
 | 
					
						
							| 
									
										
										
										
											2008-12-29 02:11:56 +00:00
										 |  |  | 	u32 foundEntries = 0; | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2008-12-29 02:11:56 +00:00
										 |  |  | 	// Find the first file in the directory.
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	WIN32_FIND_DATA ffd; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	HANDLE hFind = FindFirstFile((directory + "\\*").c_str(), &ffd); | 
					
						
							|  |  |  | 	if (hFind == INVALID_HANDLE_VALUE) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		FindClose(hFind); | 
					
						
							|  |  |  | 		return foundEntries; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	// windows loop
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	do | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		FSTEntry entry; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		const std::string virtualName(ffd.cFileName); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 	struct dirent dirent, *result = NULL; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	DIR *dirp = opendir(directory.c_str()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	if (!dirp) | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2009-01-15 06:48:15 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	// non windows loop
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	while (!readdir_r(dirp, &dirent, &result) && result) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2008-12-29 02:11:56 +00:00
										 |  |  | 		FSTEntry entry; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		const std::string virtualName(result->d_name); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 		// check for "." and ".."
 | 
					
						
							|  |  |  | 		if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 				((virtualName[0] == '.') && (virtualName[1] == '.') &&  | 
					
						
							|  |  |  | 				 (virtualName[2] == '\0'))) | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 			continue; | 
					
						
							|  |  |  | 		entry.virtualName = virtualName; | 
					
						
							|  |  |  | 		entry.physicalName = directory; | 
					
						
							|  |  |  | 		entry.physicalName += DIR_SEP + entry.virtualName; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (IsDirectory(entry.physicalName.c_str())) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2008-12-29 02:11:56 +00:00
										 |  |  | 			entry.isDirectory = true; | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 			// is a directory, lets go inside
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 			entry.size = ScanDirectoryTree(entry.physicalName, entry); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 			foundEntries += (u32)entry.size; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ // is a file 
 | 
					
						
							| 
									
										
										
										
											2008-12-29 02:11:56 +00:00
										 |  |  | 			entry.isDirectory = false; | 
					
						
							|  |  |  | 			entry.size = GetSize(entry.physicalName.c_str()); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		++foundEntries; | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		// Push into the tree
 | 
					
						
							|  |  |  | 		parentEntry.children.push_back(entry);		 | 
					
						
							|  |  |  | #ifdef _WIN32 
 | 
					
						
							|  |  |  | 	} while (FindNextFile(hFind, &ffd) != 0); | 
					
						
							|  |  |  | 	FindClose(hFind); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2008-12-29 02:11:56 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	closedir(dirp); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	// Return number of entries found.
 | 
					
						
							|  |  |  | 	return foundEntries; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | // Deletes the given directory and anything under it. Returns true on success.
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool DeleteDirRecursively(const std::string &directory) | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	INFO_LOG(COMMON, "DeleteDirRecursively: %s", directory.c_str()); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	// Find the first file in the directory.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	WIN32_FIND_DATA ffd; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	HANDLE hFind = FindFirstFile((directory + "\\*").c_str(), &ffd); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (hFind == INVALID_HANDLE_VALUE) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		FindClose(hFind); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 	// windows loop
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	do | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		const std::string virtualName = ffd.cFileName; | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | #else
 | 
					
						
							|  |  |  | 	struct dirent dirent, *result = NULL; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	DIR *dirp = opendir(directory.c_str()); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	if (!dirp) | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	// non windows loop
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	while (!readdir_r(dirp, &dirent, &result) && result) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		const std::string virtualName = result->d_name; | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 		// check for "." and ".."
 | 
					
						
							|  |  |  | 		if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || | 
					
						
							|  |  |  | 			((virtualName[0] == '.') && (virtualName[1] == '.') &&  | 
					
						
							|  |  |  | 			 (virtualName[2] == '\0'))) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		std::string newPath = directory + DIR_SEP_CHR + virtualName; | 
					
						
							|  |  |  | 		if (IsDirectory(newPath)) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 			if (!DeleteDirRecursively(newPath)) | 
					
						
							|  |  |  | 				return false; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 			if (!File::Delete(newPath)) | 
					
						
							|  |  |  | 				return false; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	} while (FindNextFile(hFind, &ffd) != 0); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | 	FindClose(hFind); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-02-03 20:29:49 +00:00
										 |  |  | 	closedir(dirp); | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	File::DeleteDir(directory); | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 	return true; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | // Create directory and copy contents (does not overwrite existing files)
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | void CopyDir(const std::string &source_path, const std::string &dest_path) | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-12 04:26:32 +00:00
										 |  |  | #ifndef _WIN32
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (source_path == dest_path) return; | 
					
						
							| 
									
										
										
										
											2010-04-06 15:02:09 +00:00
										 |  |  | 	if (!File::Exists(source_path)) return; | 
					
						
							|  |  |  | 	if (!File::Exists(dest_path)) File::CreateFullPath(dest_path); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	struct dirent dirent, *result = NULL; | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	DIR *dirp = opendir(source_path.c_str()); | 
					
						
							| 
									
										
										
										
											2010-04-06 15:02:09 +00:00
										 |  |  | 	if (!dirp) return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while (!readdir_r(dirp, &dirent, &result) && result) | 
					
						
							|  |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		const std::string virtualName(result->d_name); | 
					
						
							| 
									
										
										
										
											2010-04-06 15:02:09 +00:00
										 |  |  | 		// check for "." and ".."
 | 
					
						
							|  |  |  | 		if (((virtualName[0] == '.') && (virtualName[1] == '\0')) || | 
					
						
							|  |  |  | 			((virtualName[0] == '.') && (virtualName[1] == '.') && | 
					
						
							|  |  |  | 			(virtualName[2] == '\0'))) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		std::string source, dest; | 
					
						
							|  |  |  | 		source = source_path + virtualName; | 
					
						
							|  |  |  | 		dest = dest_path + virtualName; | 
					
						
							| 
									
										
										
										
											2010-04-06 15:02:09 +00:00
										 |  |  | 		if (IsDirectory(source)) | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 			source += '/'; | 
					
						
							|  |  |  | 			dest += '/'; | 
					
						
							| 
									
										
										
										
											2010-04-06 15:02:09 +00:00
										 |  |  | 			if (!File::Exists(dest)) File::CreateFullPath(dest); | 
					
						
							|  |  |  | 			CopyDir(source, dest); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else if (!File::Exists(dest)) File::Copy(source, dest); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	closedir(dirp); | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2010-05-12 04:26:32 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +00:00
										 |  |  | // Returns the current directory
 | 
					
						
							|  |  |  | std::string GetCurrentDir() | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +00:00
										 |  |  | 	char *dir; | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	// Get the current working directory (getcwd uses malloc) 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | 	if (!(dir = __getcwd(NULL, 0))) { | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		ERROR_LOG(COMMON, "GetCurrentDirectory failed: %s", | 
					
						
							|  |  |  | 				  GetLastErrorMsg()); | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +00:00
										 |  |  | 	std::string strDir = dir; | 
					
						
							|  |  |  | 	free(dir); | 
					
						
							|  |  |  | 	return strDir; | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // Sets the current directory to the given directory
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool SetCurrentDir(const std::string &directory) | 
					
						
							| 
									
										
										
										
											2009-02-24 07:18:08 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	return __chdir(directory.c_str()) == 0; | 
					
						
							| 
									
										
										
										
											2009-02-24 07:18:08 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 23:21:51 +00:00
										 |  |  | #if defined(__APPLE__)
 | 
					
						
							|  |  |  | std::string GetBundleDirectory()  | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	CFURLRef BundleRef; | 
					
						
							|  |  |  | 	char AppBundlePath[MAXPATHLEN]; | 
					
						
							|  |  |  | 	// Get the main bundle for the app
 | 
					
						
							|  |  |  | 	BundleRef = CFBundleCopyBundleURL(CFBundleGetMainBundle()); | 
					
						
							|  |  |  | 	CFStringRef BundlePath = CFURLCopyFileSystemPath(BundleRef, kCFURLPOSIXPathStyle); | 
					
						
							|  |  |  | 	CFStringGetFileSystemRepresentation(BundlePath, AppBundlePath, sizeof(AppBundlePath)); | 
					
						
							|  |  |  | 	CFRelease(BundleRef); | 
					
						
							|  |  |  | 	CFRelease(BundlePath); | 
					
						
							| 
									
										
										
										
											2011-01-15 10:33:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 23:21:51 +00:00
										 |  |  | 	return AppBundlePath; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | std::string GetSysDirectory() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::string sysDir; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if defined (__APPLE__)
 | 
					
						
							|  |  |  | 	sysDir = GetBundleDirectory(); | 
					
						
							|  |  |  | 	sysDir += DIR_SEP; | 
					
						
							|  |  |  | 	sysDir += SYSDATA_DIR; | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-06-05 18:52:56 +00:00
										 |  |  | 	sysDir = SYSDATA_DIR; | 
					
						
							| 
									
										
										
										
											2009-02-28 23:21:51 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2009-08-06 12:32:07 +00:00
										 |  |  | 	sysDir += DIR_SEP; | 
					
						
							| 
									
										
										
										
											2010-06-05 18:52:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 23:21:51 +00:00
										 |  |  | 	INFO_LOG(COMMON, "GetSysDirectory: Setting to %s:", sysDir.c_str()); | 
					
						
							|  |  |  | 	return sysDir; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-04-18 11:31:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | // Returns a string with a Dolphin data dir or file in the user's home
 | 
					
						
							| 
									
										
										
										
											2009-02-28 23:21:51 +00:00
										 |  |  | // directory. To be used in "multi-user" mode (that is, installed).
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | std::string &GetUserPath(const unsigned int DirIDX) | 
					
						
							| 
									
										
										
										
											2009-02-28 23:21:51 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	static std::string paths[NUM_PATH_INDICES]; | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Set up all paths and files on the first run
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	if (paths[D_USER_IDX].empty()) | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		paths[D_USER_IDX] = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP; | 
					
						
							| 
									
										
										
										
											2010-07-22 07:55:35 +00:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2010-06-13 14:52:11 +00:00
										 |  |  | 		if (File::Exists(ROOT_DIR DIR_SEP USERDATA_DIR)) | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 			paths[D_USER_IDX] = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP; | 
					
						
							| 
									
										
										
										
											2010-06-13 14:52:11 +00:00
										 |  |  | 		else | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 			paths[D_USER_IDX] = std::string(getenv("HOME")) + DIR_SEP DOLPHIN_DATA_DIR DIR_SEP; | 
					
						
							| 
									
										
										
										
											2009-02-28 23:21:51 +00:00
										 |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 		INFO_LOG(COMMON, "GetUserPath: Setting user directory to %s:", paths[D_USER_IDX].c_str()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		paths[D_GCUSER_IDX]			= paths[D_USER_IDX] + GC_USER_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_WIIUSER_IDX]		= paths[D_USER_IDX] + WII_USER_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_WIIROOT_IDX]		= paths[D_USER_IDX] + WII_USER_DIR; | 
					
						
							|  |  |  | 		paths[D_CONFIG_IDX]			= paths[D_USER_IDX] + CONFIG_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_GAMECONFIG_IDX]		= paths[D_USER_IDX] + GAMECONFIG_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_MAPS_IDX]			= paths[D_USER_IDX] + MAPS_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_CACHE_IDX]			= paths[D_USER_IDX] + CACHE_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_SHADERCACHE_IDX]	= paths[D_USER_IDX] + SHADERCACHE_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_SHADERS_IDX]		= paths[D_USER_IDX] + SHADERS_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_STATESAVES_IDX]		= paths[D_USER_IDX] + STATESAVES_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_SCREENSHOTS_IDX]	= paths[D_USER_IDX] + SCREENSHOTS_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_OPENCL_IDX]			= paths[D_USER_IDX] + OPENCL_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_HIRESTEXTURES_IDX]	= paths[D_USER_IDX] + HIRES_TEXTURES_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_DUMP_IDX]			= paths[D_USER_IDX] + DUMP_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_DUMPFRAMES_IDX]		= paths[D_USER_IDX] + DUMP_FRAMES_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_DUMPAUDIO_IDX]		= paths[D_USER_IDX] + DUMP_AUDIO_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_DUMPTEXTURES_IDX]	= paths[D_USER_IDX] + DUMP_TEXTURES_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_DUMPDSP_IDX]		= paths[D_USER_IDX] + DUMP_DSP_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_LOGS_IDX]			= paths[D_USER_IDX] + LOGS_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_MAILLOGS_IDX]		= paths[D_USER_IDX] + MAIL_LOGS_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[D_WIISYSCONF_IDX]		= paths[D_USER_IDX] + WII_SYSCONF_DIR DIR_SEP; | 
					
						
							|  |  |  | 		paths[F_DOLPHINCONFIG_IDX]	= paths[D_CONFIG_IDX] + DOLPHIN_CONFIG; | 
					
						
							|  |  |  | 		paths[F_DSPCONFIG_IDX]		= paths[D_CONFIG_IDX] + DSP_CONFIG; | 
					
						
							|  |  |  | 		paths[F_DEBUGGERCONFIG_IDX]	= paths[D_CONFIG_IDX] + DEBUGGER_CONFIG; | 
					
						
							|  |  |  | 		paths[F_LOGGERCONFIG_IDX]	= paths[D_CONFIG_IDX] + LOGGER_CONFIG; | 
					
						
							|  |  |  | 		paths[F_MAINLOG_IDX]		= paths[D_LOGS_IDX] + MAIN_LOG; | 
					
						
							|  |  |  | 		paths[F_WIISYSCONF_IDX]		= paths[D_WIISYSCONF_IDX] + WII_SYSCONF; | 
					
						
							|  |  |  | 		paths[F_RAMDUMP_IDX]		= paths[D_DUMP_IDX] + RAM_DUMP; | 
					
						
							|  |  |  | 		paths[F_ARAMDUMP_IDX]		= paths[D_DUMP_IDX] + ARAM_DUMP; | 
					
						
							|  |  |  | 		paths[F_GCSRAM_IDX]			= paths[D_GCUSER_IDX] + GC_SRAM; | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	return paths[DirIDX]; | 
					
						
							| 
									
										
										
										
											2009-02-28 23:21:51 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-12 13:12:42 +00:00
										 |  |  | bool WriteStringToFile(bool text_file, const std::string &str, const char *filename) | 
					
						
							| 
									
										
										
										
											2009-04-12 10:21:40 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	FILE *f = fopen(filename, text_file ? "w" : "wb"); | 
					
						
							|  |  |  | 	if (!f) | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2009-04-12 13:12:42 +00:00
										 |  |  | 	size_t len = str.size(); | 
					
						
							|  |  |  | 	if (len != fwrite(str.data(), 1, str.size(), f)) | 
					
						
							| 
									
										
										
										
											2009-04-12 10:21:40 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		fclose(f); | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	fclose(f); | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-25 10:38:26 +00:00
										 |  |  | bool ReadFileToString(bool text_file, const char *filename, std::string &str) | 
					
						
							| 
									
										
										
										
											2009-04-12 10:21:40 +00:00
										 |  |  | { | 
					
						
							|  |  |  | 	FILE *f = fopen(filename, text_file ? "r" : "rb"); | 
					
						
							|  |  |  | 	if (!f) | 
					
						
							|  |  |  | 		return false; | 
					
						
							| 
									
										
										
										
											2010-12-03 12:42:01 +00:00
										 |  |  | 	size_t len = (size_t)GetSize(f); | 
					
						
							| 
									
										
										
										
											2009-04-12 10:21:40 +00:00
										 |  |  | 	char *buf = new char[len + 1]; | 
					
						
							|  |  |  | 	buf[fread(buf, 1, len, f)] = 0; | 
					
						
							| 
									
										
										
										
											2009-04-25 10:38:26 +00:00
										 |  |  | 	str = std::string(buf, len); | 
					
						
							| 
									
										
										
										
											2009-04-12 10:21:40 +00:00
										 |  |  | 	fclose(f); | 
					
						
							|  |  |  | 	delete [] buf; | 
					
						
							|  |  |  | 	return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 05:30:24 +00:00
										 |  |  | } // namespace
 |