file_util.h 6.6 KB

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