| 
									
										
										
										
											2013-04-17 23:09:55 -04:00
										 |  |  | // Copyright 2013 Dolphin Emulator Project
 | 
					
						
							|  |  |  | // Licensed under GPLv2
 | 
					
						
							|  |  |  | // Refer to the license.txt file included.
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-28 08:57:34 +00:00
										 |  |  | #ifndef _FILEUTIL_H_
 | 
					
						
							|  |  |  | #define _FILEUTIL_H_
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | #include <fstream>
 | 
					
						
							|  |  |  | #include <cstdio>
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | #include <string>
 | 
					
						
							|  |  |  | #include <vector>
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "Common.h"
 | 
					
						
							| 
									
										
										
										
											2013-02-28 19:33:39 -06:00
										 |  |  | #include "StringUtil.h"
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | // User directory indices for GetUserPath
 | 
					
						
							|  |  |  | enum { | 
					
						
							|  |  |  | 	D_USER_IDX, | 
					
						
							|  |  |  | 	D_GCUSER_IDX, | 
					
						
							| 
									
										
										
										
											2010-02-03 03:27:32 +00:00
										 |  |  | 	D_WIIROOT_IDX, | 
					
						
							| 
									
										
										
										
											2011-05-24 19:12:18 +00:00
										 |  |  | 	D_WIIUSER_IDX, | 
					
						
							| 
									
										
										
										
											2013-09-07 23:02:49 +02:00
										 |  |  | 	D_CONFIG_IDX, // global settings
 | 
					
						
							|  |  |  | 	D_GAMESETTINGS_IDX, // user-specified settings which override both the global and the default settings (per game)
 | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | 	D_MAPS_IDX, | 
					
						
							|  |  |  | 	D_CACHE_IDX, | 
					
						
							|  |  |  | 	D_SHADERCACHE_IDX, | 
					
						
							|  |  |  | 	D_SHADERS_IDX, | 
					
						
							|  |  |  | 	D_STATESAVES_IDX, | 
					
						
							|  |  |  | 	D_SCREENSHOTS_IDX, | 
					
						
							| 
									
										
										
										
											2010-12-19 14:59:57 +00:00
										 |  |  | 	D_OPENCL_IDX, | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | 	D_HIRESTEXTURES_IDX, | 
					
						
							|  |  |  | 	D_DUMP_IDX, | 
					
						
							|  |  |  | 	D_DUMPFRAMES_IDX, | 
					
						
							| 
									
										
										
										
											2011-02-11 18:59:42 +00:00
										 |  |  | 	D_DUMPAUDIO_IDX, | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | 	D_DUMPTEXTURES_IDX, | 
					
						
							|  |  |  | 	D_DUMPDSP_IDX, | 
					
						
							|  |  |  | 	D_LOGS_IDX, | 
					
						
							|  |  |  | 	D_MAILLOGS_IDX, | 
					
						
							|  |  |  | 	D_WIISYSCONF_IDX, | 
					
						
							| 
									
										
										
										
											2013-01-04 22:35:48 +13:00
										 |  |  | 	D_WIIWC24_IDX, | 
					
						
							| 
									
										
										
										
											2013-01-13 14:06:15 -06:00
										 |  |  | 	D_THEMES_IDX, | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | 	F_DOLPHINCONFIG_IDX, | 
					
						
							|  |  |  | 	F_DEBUGGERCONFIG_IDX, | 
					
						
							|  |  |  | 	F_LOGGERCONFIG_IDX, | 
					
						
							|  |  |  | 	F_MAINLOG_IDX, | 
					
						
							|  |  |  | 	F_WIISYSCONF_IDX, | 
					
						
							|  |  |  | 	F_RAMDUMP_IDX, | 
					
						
							|  |  |  | 	F_ARAMDUMP_IDX, | 
					
						
							| 
									
										
										
										
											2012-06-19 13:51:29 +02:00
										 |  |  | 	F_FAKEVMEMDUMP_IDX, | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | 	F_GCSRAM_IDX, | 
					
						
							| 
									
										
										
										
											2011-03-01 03:06:14 +00:00
										 |  |  | 	NUM_PATH_INDICES | 
					
						
							| 
									
										
										
										
											2010-02-02 21:56:29 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | namespace File | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // FileSystem tree node/ 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | struct FSTEntry | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	bool isDirectory; | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 	u64 size;						// file length or number of entries from children
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | 	std::string physicalName;		// name on disk
 | 
					
						
							|  |  |  | 	std::string virtualName;		// name in FST names table
 | 
					
						
							|  |  |  | 	std::vector<FSTEntry> children; | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +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); | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-03 12:42:01 +00:00
										 |  |  | // Overloaded GetSize, accepts file descriptor
 | 
					
						
							|  |  |  | u64 GetSize(const int fd); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Overloaded GetSize, accepts FILE*
 | 
					
						
							|  |  |  | u64 GetSize(FILE *f); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // Returns true if successful, or path already exists.
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool CreateDir(const std::string &filename); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-15 02:52:06 -04:00
										 |  |  | // ditto, but syncs the source file and, on Unix, syncs the directories after rename
 | 
					
						
							|  |  |  | bool RenameSync(const std::string &srcFilename, const std::string &destFilename); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +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); | 
					
						
							| 
									
										
										
										
											2009-06-09 19:40:47 +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); | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +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); | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Returns the current directory
 | 
					
						
							|  |  |  | std::string GetCurrentDir(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-12 04:26:32 +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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 01:26:56 +00:00
										 |  |  | // Set the current directory to given directory
 | 
					
						
							| 
									
										
										
										
											2011-03-01 05:16:32 +00:00
										 |  |  | bool SetCurrentDir(const std::string &directory); | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-15 02:52:06 -04:00
										 |  |  | // Get a filename that can hopefully be atomically renamed to the given path.
 | 
					
						
							|  |  |  | std::string GetTempFilenameForAtomicWrite(const std::string &path); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 23:21:51 +00:00
										 |  |  | // Returns a pointer to a string with a Dolphin data dir in the user's home
 | 
					
						
							|  |  |  | // directory. To be used in "multi-user" mode (that is, installed).
 | 
					
						
							| 
									
										
										
										
											2013-04-01 23:17:15 -05:00
										 |  |  | const std::string& GetUserPath(const unsigned int DirIDX, const std::string &newPath=""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // probably doesn't belong here
 | 
					
						
							| 
									
										
										
										
											2013-04-02 13:04:40 -05:00
										 |  |  | std::string GetThemeDir(const std::string& theme_name); | 
					
						
							| 
									
										
										
										
											2009-08-06 06:18:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-28 23:21:51 +00:00
										 |  |  | // Returns the path to where the sys file are
 | 
					
						
							|  |  |  | std::string GetSysDirectory(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef __APPLE__
 | 
					
						
							|  |  |  | std::string GetBundleDirectory(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-11 20:45:09 +00:00
										 |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | std::string &GetExeDirectory(); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-12 13:12:42 +00:00
										 |  |  | bool WriteStringToFile(bool text_file, const std::string &str, const char *filename); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | // simple wrapper for cstdlib file functions to
 | 
					
						
							|  |  |  | // hopefully will make error checking easier
 | 
					
						
							|  |  |  | // and make forgetting an fclose() harder
 | 
					
						
							| 
									
										
										
										
											2013-08-29 18:25:12 -04:00
										 |  |  | class IOFile : public NonCopyable | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  | 	IOFile(); | 
					
						
							|  |  |  | 	IOFile(std::FILE* file); | 
					
						
							|  |  |  | 	IOFile(const std::string& filename, const char openmode[]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	~IOFile(); | 
					
						
							| 
									
										
										
										
											2013-02-17 13:30:04 -06:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	IOFile(IOFile&& other); | 
					
						
							| 
									
										
										
										
											2013-02-20 17:30:34 -06:00
										 |  |  | 	IOFile& operator=(IOFile&& other); | 
					
						
							| 
									
										
										
										
											2013-02-17 13:30:04 -06:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	void Swap(IOFile& other); | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	bool Open(const std::string& filename, const char openmode[]); | 
					
						
							|  |  |  | 	bool Close(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	template <typename T> | 
					
						
							| 
									
										
										
										
											2013-09-16 06:56:49 +02:00
										 |  |  | 	bool ReadArray(T* data, size_t length, size_t* pReadBytes = NULL) | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2013-09-16 06:56:49 +02:00
										 |  |  | 		size_t read_bytes = 0; | 
					
						
							|  |  |  | 		if (!IsOpen() || length != (read_bytes = std::fread(data, sizeof(T), length, m_file))) | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 			m_good = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-16 06:56:49 +02:00
										 |  |  | 		if (pReadBytes) | 
					
						
							|  |  |  | 			*pReadBytes = read_bytes; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | 		return m_good; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	template <typename T> | 
					
						
							|  |  |  | 	bool WriteArray(const T* data, size_t length) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		if (!IsOpen() || length != std::fwrite(data, sizeof(T), length, m_file)) | 
					
						
							|  |  |  | 			m_good = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return m_good; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool ReadBytes(void* data, size_t length) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return ReadArray(reinterpret_cast<char*>(data), length); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool WriteBytes(const void* data, size_t length) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		return WriteArray(reinterpret_cast<const char*>(data), length); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool IsOpen() { return NULL != m_file; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// m_good is set to false when a read, write or other function fails
 | 
					
						
							|  |  |  | 	bool IsGood() {	return m_good; } | 
					
						
							|  |  |  | 	operator void*() { return m_good ? m_file : NULL; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	std::FILE* ReleaseHandle(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	std::FILE* GetHandle() { return m_file; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	void SetHandle(std::FILE* file); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bool Seek(s64 off, int origin); | 
					
						
							|  |  |  | 	u64 Tell(); | 
					
						
							|  |  |  | 	u64 GetSize(); | 
					
						
							|  |  |  | 	bool Resize(u64 size); | 
					
						
							|  |  |  | 	bool Flush(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// clear error state
 | 
					
						
							|  |  |  | 	void Clear() { m_good = true; std::clearerr(m_file); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	std::FILE* m_file; | 
					
						
							|  |  |  | 	bool m_good; | 
					
						
							| 
									
										
										
										
											2013-08-31 23:44:26 -04:00
										 |  |  | private: | 
					
						
							|  |  |  | 	IOFile(IOFile&); | 
					
						
							|  |  |  | 	IOFile& operator=(IOFile& other); | 
					
						
							| 
									
										
										
										
											2011-03-11 10:21:46 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | }  // namespace
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-02-28 19:33:39 -06:00
										 |  |  | // To deal with Windows being dumb at unicode:
 | 
					
						
							|  |  |  | template <typename T> | 
					
						
							|  |  |  | void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #ifdef _WIN32
 | 
					
						
							|  |  |  | 	fstream.open(UTF8ToTStr(filename).c_str(), openmode); | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2013-02-28 19:42:29 -06:00
										 |  |  | 	fstream.open(filename.c_str(), openmode); | 
					
						
							| 
									
										
										
										
											2013-02-28 19:33:39 -06:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-08 04:46:09 +00:00
										 |  |  | #endif
 |