file_util.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <cstddef>
  8. #include <cstdio>
  9. #include <string>
  10. #include <vector>
  11. #include "common/common_types.h"
  12. // User directory indices for GetUserPath
  13. enum {
  14. D_USER_IDX,
  15. D_ROOT_IDX,
  16. D_CONFIG_IDX,
  17. D_GAMECONFIG_IDX,
  18. D_MAPS_IDX,
  19. D_CACHE_IDX,
  20. D_SHADERCACHE_IDX,
  21. D_SHADERS_IDX,
  22. D_STATESAVES_IDX,
  23. D_SCREENSHOTS_IDX,
  24. D_SDMC_IDX,
  25. D_NAND_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. // 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. size_t WriteStringToFile(bool text_file, const std::string &str, const char *filename);
  103. size_t ReadFileToString(bool text_file, const char *filename, std::string &str);
  104. /**
  105. * Splits the filename into 8.3 format
  106. * Loosely implemented following https://en.wikipedia.org/wiki/8.3_filename
  107. * @param filename The normal filename to use
  108. * @param short_name A 9-char array in which the short name will be written
  109. * @param extension A 4-char array in which the extension will be written
  110. */
  111. void SplitFilename83(const std::string& filename, std::array<char, 9>& short_name,
  112. std::array<char, 4>& extension);
  113. // simple wrapper for cstdlib file functions to
  114. // hopefully will make error checking easier
  115. // and make forgetting an fclose() harder
  116. class IOFile : public NonCopyable
  117. {
  118. public:
  119. IOFile();
  120. IOFile(std::FILE* file);
  121. IOFile(const std::string& filename, const char openmode[]);
  122. ~IOFile();
  123. IOFile(IOFile&& other);
  124. IOFile& operator=(IOFile&& other);
  125. void Swap(IOFile& other);
  126. bool Open(const std::string& filename, const char openmode[]);
  127. bool Close();
  128. template <typename T>
  129. size_t ReadArray(T* data, size_t length)
  130. {
  131. if (!IsOpen()) {
  132. m_good = false;
  133. return -1;
  134. }
  135. size_t items_read = std::fread(data, sizeof(T), length, m_file);
  136. if (items_read != length)
  137. m_good = false;
  138. return items_read;
  139. }
  140. template <typename T>
  141. size_t WriteArray(const T* data, size_t length)
  142. {
  143. static_assert(std::is_standard_layout<T>::value, "Given array does not consist of standard layout objects");
  144. // TODO: gcc 4.8 does not support is_trivially_copyable, but we really should check for it here.
  145. //static_assert(std::is_trivially_copyable<T>::value, "Given array does not consist of trivially copyable objects");
  146. if (!IsOpen()) {
  147. m_good = false;
  148. return -1;
  149. }
  150. size_t items_written = std::fwrite(data, sizeof(T), length, m_file);
  151. if (items_written != length)
  152. m_good = false;
  153. return items_written;
  154. }
  155. size_t ReadBytes(void* data, size_t length)
  156. {
  157. return ReadArray(reinterpret_cast<char*>(data), length);
  158. }
  159. size_t WriteBytes(const void* data, size_t length)
  160. {
  161. return WriteArray(reinterpret_cast<const char*>(data), length);
  162. }
  163. template<typename T>
  164. size_t WriteObject(const T& object) {
  165. static_assert(!std::is_pointer<T>::value, "Given object is a pointer");
  166. return WriteArray(&object, 1);
  167. }
  168. bool IsOpen() { return nullptr != m_file; }
  169. // m_good is set to false when a read, write or other function fails
  170. bool IsGood() { return m_good; }
  171. operator void*() { return m_good ? m_file : nullptr; }
  172. std::FILE* ReleaseHandle();
  173. std::FILE* GetHandle() { return m_file; }
  174. void SetHandle(std::FILE* file);
  175. bool Seek(s64 off, int origin);
  176. u64 Tell();
  177. u64 GetSize();
  178. bool Resize(u64 size);
  179. bool Flush();
  180. // clear error state
  181. void Clear() { m_good = true; std::clearerr(m_file); }
  182. std::FILE* m_file;
  183. bool m_good;
  184. private:
  185. IOFile(IOFile&);
  186. IOFile& operator=(IOFile& other);
  187. };
  188. } // namespace
  189. // To deal with Windows being dumb at unicode:
  190. template <typename T>
  191. void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode)
  192. {
  193. #ifdef _WIN32
  194. fstream.open(Common::UTF8ToTStr(filename).c_str(), openmode);
  195. #else
  196. fstream.open(filename.c_str(), openmode);
  197. #endif
  198. }