file_util.h 6.6 KB

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