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