file_util.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2013 Dolphin Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #ifndef _FILEUTIL_H_
  5. #define _FILEUTIL_H_
  6. #include <fstream>
  7. #include <cstdio>
  8. #include <string>
  9. #include <vector>
  10. #include <string.h>
  11. #include "common/common.h"
  12. #include "common/string_util.h"
  13. // User directory indices for GetUserPath
  14. enum {
  15. D_USER_IDX,
  16. D_ROOT_IDX,
  17. D_CONFIG_IDX,
  18. D_GAMECONFIG_IDX,
  19. D_MAPS_IDX,
  20. D_CACHE_IDX,
  21. D_SHADERCACHE_IDX,
  22. D_SHADERS_IDX,
  23. D_STATESAVES_IDX,
  24. D_SCREENSHOTS_IDX,
  25. D_HIRESTEXTURES_IDX,
  26. D_DUMP_IDX,
  27. D_DUMPFRAMES_IDX,
  28. D_DUMPAUDIO_IDX,
  29. D_DUMPTEXTURES_IDX,
  30. D_DUMPDSP_IDX,
  31. D_LOGS_IDX,
  32. D_SYSCONF_IDX,
  33. F_EMUCONFIG_IDX,
  34. F_DEBUGGERCONFIG_IDX,
  35. F_LOGGERCONFIG_IDX,
  36. F_MAINLOG_IDX,
  37. F_RAMDUMP_IDX,
  38. F_ARAMDUMP_IDX,
  39. F_SYSCONF_IDX,
  40. NUM_PATH_INDICES
  41. };
  42. namespace File
  43. {
  44. // FileSystem tree node/
  45. struct FSTEntry
  46. {
  47. bool isDirectory;
  48. u64 size; // file length or number of entries from children
  49. std::string physicalName; // name on disk
  50. std::string virtualName; // name in FST names table
  51. std::vector<FSTEntry> children;
  52. };
  53. // Returns true if file filename exists
  54. bool Exists(const std::string &filename);
  55. // Returns true if filename is a directory
  56. bool IsDirectory(const std::string &filename);
  57. // Returns the size of filename (64bit)
  58. u64 GetSize(const std::string &filename);
  59. // Overloaded GetSize, accepts file descriptor
  60. u64 GetSize(const int fd);
  61. // Overloaded GetSize, accepts FILE*
  62. u64 GetSize(FILE *f);
  63. // Returns true if successful, or path already exists.
  64. bool CreateDir(const std::string &filename);
  65. // Creates the full path of fullPath returns true on success
  66. bool CreateFullPath(const std::string &fullPath);
  67. // Deletes a given filename, return true on success
  68. // Doesn't supports deleting a directory
  69. bool Delete(const std::string &filename);
  70. // Deletes a directory filename, returns true on success
  71. bool DeleteDir(const std::string &filename);
  72. // renames file srcFilename to destFilename, returns true on success
  73. bool Rename(const std::string &srcFilename, const std::string &destFilename);
  74. // copies file srcFilename to destFilename, returns true on success
  75. bool Copy(const std::string &srcFilename, const std::string &destFilename);
  76. // creates an empty file filename, returns true on success
  77. bool CreateEmptyFile(const std::string &filename);
  78. // Scans the directory tree gets, starting from _Directory and adds the
  79. // results into parentEntry. Returns the number of files+directories found
  80. u32 ScanDirectoryTree(const std::string &directory, FSTEntry& parentEntry);
  81. // deletes the given directory and anything under it. Returns true on success.
  82. bool DeleteDirRecursively(const std::string &directory);
  83. // Returns the current directory
  84. std::string GetCurrentDir();
  85. // Create directory and copy contents (does not overwrite existing files)
  86. void CopyDir(const std::string &source_path, const std::string &dest_path);
  87. // Set the current directory to given directory
  88. bool SetCurrentDir(const std::string &directory);
  89. // Returns a pointer to a string with a Dolphin data dir in the user's home
  90. // directory. To be used in "multi-user" mode (that is, installed).
  91. const std::string& GetUserPath(const unsigned int DirIDX, const std::string &newPath="");
  92. // probably doesn't belong here
  93. //std::string GetThemeDir(const std::string& theme_name);
  94. // Returns the path to where the sys file are
  95. std::string GetSysDirectory();
  96. #ifdef __APPLE__
  97. std::string GetBundleDirectory();
  98. #endif
  99. #ifdef _WIN32
  100. std::string &GetExeDirectory();
  101. #endif
  102. bool WriteStringToFile(bool text_file, const std::string &str, const char *filename);
  103. bool ReadFileToString(bool text_file, const char *filename, std::string &str);
  104. // simple wrapper for cstdlib file functions to
  105. // hopefully will make error checking easier
  106. // and make forgetting an fclose() harder
  107. class IOFile : public NonCopyable
  108. {
  109. public:
  110. IOFile();
  111. IOFile(std::FILE* file);
  112. IOFile(const std::string& filename, const char openmode[]);
  113. ~IOFile();
  114. IOFile(IOFile&& other);
  115. IOFile& operator=(IOFile&& other);
  116. void Swap(IOFile& other);
  117. bool Open(const std::string& filename, const char openmode[]);
  118. bool Close();
  119. template <typename T>
  120. bool ReadArray(T* data, size_t length)
  121. {
  122. if (!IsOpen() || length != std::fread(data, sizeof(T), length, m_file))
  123. m_good = false;
  124. return m_good;
  125. }
  126. template <typename T>
  127. bool WriteArray(const T* data, size_t length)
  128. {
  129. if (!IsOpen() || length != std::fwrite(data, sizeof(T), length, m_file))
  130. m_good = false;
  131. return m_good;
  132. }
  133. bool ReadBytes(void* data, size_t length)
  134. {
  135. return ReadArray(reinterpret_cast<char*>(data), length);
  136. }
  137. bool WriteBytes(const void* data, size_t length)
  138. {
  139. return WriteArray(reinterpret_cast<const char*>(data), length);
  140. }
  141. bool IsOpen() { return NULL != m_file; }
  142. // m_good is set to false when a read, write or other function fails
  143. bool IsGood() { return m_good; }
  144. operator void*() { return m_good ? m_file : NULL; }
  145. std::FILE* ReleaseHandle();
  146. std::FILE* GetHandle() { return m_file; }
  147. void SetHandle(std::FILE* file);
  148. bool Seek(s64 off, int origin);
  149. u64 Tell();
  150. u64 GetSize();
  151. bool Resize(u64 size);
  152. bool Flush();
  153. // clear error state
  154. void Clear() { m_good = true; std::clearerr(m_file); }
  155. std::FILE* m_file;
  156. bool m_good;
  157. private:
  158. IOFile(IOFile&);
  159. IOFile& operator=(IOFile& other);
  160. };
  161. } // namespace
  162. // To deal with Windows being dumb at unicode:
  163. template <typename T>
  164. void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode)
  165. {
  166. #ifdef _WIN32
  167. fstream.open(UTF8ToTStr(filename).c_str(), openmode);
  168. #else
  169. fstream.open(filename.c_str(), openmode);
  170. #endif
  171. }
  172. #endif