2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2014-02-10 11:54:46 -07:00
|
|
|
#pragma once
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <cstddef>
|
2011-03-11 03:21:46 -07:00
|
|
|
#include <cstdio>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <fstream>
|
2008-12-07 21:46:09 -07:00
|
|
|
#include <string>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <vector>
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2015-09-26 15:13:07 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2015-09-26 14:13:54 -06:00
|
|
|
#include "Common/NonCopyable.h"
|
2014-02-19 20:11:52 -07:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "Common/StringUtil.h"
|
2014-02-19 20:11:52 -07:00
|
|
|
#endif
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2010-02-02 14:56:29 -07:00
|
|
|
// User directory indices for GetUserPath
|
2016-06-24 02:43:46 -06:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
D_USER_IDX,
|
|
|
|
D_GCUSER_IDX,
|
|
|
|
D_WIIROOT_IDX, // always points to User/Wii or global user-configured directory
|
|
|
|
D_SESSION_WIIROOT_IDX, // may point to minimal temporary directory for determinism
|
|
|
|
D_CONFIG_IDX, // global settings
|
|
|
|
D_GAMESETTINGS_IDX, // user-specified settings which override both the global and the default
|
|
|
|
// settings (per game)
|
|
|
|
D_MAPS_IDX,
|
|
|
|
D_CACHE_IDX,
|
|
|
|
D_SHADERCACHE_IDX,
|
|
|
|
D_SHADERS_IDX,
|
|
|
|
D_STATESAVES_IDX,
|
|
|
|
D_SCREENSHOTS_IDX,
|
|
|
|
D_HIRESTEXTURES_IDX,
|
|
|
|
D_DUMP_IDX,
|
|
|
|
D_DUMPFRAMES_IDX,
|
|
|
|
D_DUMPAUDIO_IDX,
|
|
|
|
D_DUMPTEXTURES_IDX,
|
|
|
|
D_DUMPDSP_IDX,
|
2015-07-11 02:31:03 -06:00
|
|
|
D_DUMPSSL_IDX,
|
2016-06-24 02:43:46 -06:00
|
|
|
D_LOAD_IDX,
|
|
|
|
D_LOGS_IDX,
|
|
|
|
D_MAILLOGS_IDX,
|
|
|
|
D_THEMES_IDX,
|
|
|
|
D_PIPES_IDX,
|
|
|
|
D_MEMORYWATCHER_IDX,
|
|
|
|
F_DOLPHINCONFIG_IDX,
|
|
|
|
F_DEBUGGERCONFIG_IDX,
|
|
|
|
F_LOGGERCONFIG_IDX,
|
|
|
|
F_MAINLOG_IDX,
|
|
|
|
F_RAMDUMP_IDX,
|
|
|
|
F_ARAMDUMP_IDX,
|
|
|
|
F_FAKEVMEMDUMP_IDX,
|
|
|
|
F_GCSRAM_IDX,
|
|
|
|
F_MEMORYWATCHERLOCATIONS_IDX,
|
|
|
|
F_MEMORYWATCHERSOCKET_IDX,
|
2016-07-16 13:40:19 -06:00
|
|
|
F_WIISDCARD_IDX,
|
2016-06-24 02:43:46 -06:00
|
|
|
NUM_PATH_INDICES
|
2010-02-02 14:56:29 -07:00
|
|
|
};
|
|
|
|
|
2008-12-07 21:46:09 -07:00
|
|
|
namespace File
|
|
|
|
{
|
2013-10-28 23:23:17 -06:00
|
|
|
// FileSystem tree node/
|
2008-12-07 21:46:09 -07:00
|
|
|
struct FSTEntry
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
bool isDirectory;
|
|
|
|
u64 size; // File length, or for directories, recursive count of children
|
|
|
|
std::string physicalName; // Name on disk
|
|
|
|
std::string virtualName; // Name in FST names table
|
|
|
|
std::vector<FSTEntry> children;
|
2008-12-07 21:46:09 -07:00
|
|
|
};
|
2009-08-06 00:18:22 -06:00
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// Returns true if file filename exists
|
2016-01-21 13:27:56 -07:00
|
|
|
bool Exists(const std::string& filename);
|
2009-08-06 00:18:22 -06:00
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// Returns true if filename is a directory
|
2016-01-21 13:27:56 -07:00
|
|
|
bool IsDirectory(const std::string& filename);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
|
|
|
// Returns the size of filename (64bit)
|
2016-01-21 13:27:56 -07:00
|
|
|
u64 GetSize(const std::string& filename);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
2010-12-03 05:42:01 -07:00
|
|
|
// Overloaded GetSize, accepts file descriptor
|
|
|
|
u64 GetSize(const int fd);
|
|
|
|
|
|
|
|
// Overloaded GetSize, accepts FILE*
|
2016-01-21 13:16:51 -07:00
|
|
|
u64 GetSize(FILE* f);
|
2010-12-03 05:42:01 -07:00
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// Returns true if successful, or path already exists.
|
2016-01-21 13:27:56 -07:00
|
|
|
bool CreateDir(const std::string& filename);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
|
|
|
// Creates the full path of fullPath returns true on success
|
2016-01-21 13:27:56 -07:00
|
|
|
bool CreateFullPath(const std::string& fullPath);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
|
|
|
// Deletes a given filename, return true on success
|
|
|
|
// Doesn't supports deleting a directory
|
2016-01-21 13:27:56 -07:00
|
|
|
bool Delete(const std::string& filename);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
|
|
|
// Deletes a directory filename, returns true on success
|
2016-01-21 13:27:56 -07:00
|
|
|
bool DeleteDir(const std::string& filename);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
2013-10-28 23:23:17 -06:00
|
|
|
// renames file srcFilename to destFilename, returns true on success
|
2016-01-21 13:27:56 -07:00
|
|
|
bool Rename(const std::string& srcFilename, const std::string& destFilename);
|
2009-02-27 18:26:56 -07:00
|
|
|
|
2013-10-15 00:52:06 -06:00
|
|
|
// ditto, but syncs the source file and, on Unix, syncs the directories after rename
|
2016-01-21 13:27:56 -07:00
|
|
|
bool RenameSync(const std::string& srcFilename, const std::string& destFilename);
|
2013-10-15 00:52:06 -06:00
|
|
|
|
2013-10-28 23:23:17 -06:00
|
|
|
// copies file srcFilename to destFilename, returns true on success
|
2016-01-21 13:27:56 -07:00
|
|
|
bool Copy(const std::string& srcFilename, const std::string& destFilename);
|
2009-08-06 00:18:22 -06:00
|
|
|
|
2013-10-28 23:23:17 -06:00
|
|
|
// creates an empty file filename, returns true on success
|
2016-01-21 13:27:56 -07:00
|
|
|
bool CreateEmptyFile(const std::string& filename);
|
2009-06-09 13:40:47 -06:00
|
|
|
|
2016-12-25 13:57:33 -07:00
|
|
|
// Recursive or non-recursive list of files and directories under directory.
|
2016-01-21 13:27:56 -07:00
|
|
|
FSTEntry ScanDirectoryTree(const std::string& directory, bool recursive);
|
2009-08-06 00:18:22 -06:00
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// deletes the given directory and anything under it. Returns true on success.
|
2016-01-21 13:27:56 -07:00
|
|
|
bool DeleteDirRecursively(const std::string& directory);
|
2009-08-06 00:18:22 -06:00
|
|
|
|
|
|
|
// Returns the current directory
|
|
|
|
std::string GetCurrentDir();
|
|
|
|
|
2010-05-11 22:26:32 -06:00
|
|
|
// Create directory and copy contents (does not overwrite existing files)
|
2016-01-21 13:27:56 -07:00
|
|
|
void CopyDir(const std::string& source_path, const std::string& dest_path);
|
2010-02-02 14:56:29 -07:00
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// Set the current directory to given directory
|
2016-01-21 13:27:56 -07:00
|
|
|
bool SetCurrentDir(const std::string& directory);
|
2009-08-06 00:18:22 -06:00
|
|
|
|
2014-11-17 18:59:14 -07:00
|
|
|
// Creates and returns the path to a new temporary directory.
|
|
|
|
std::string CreateTempDir();
|
|
|
|
|
2013-10-15 00:52:06 -06:00
|
|
|
// Get a filename that can hopefully be atomically renamed to the given path.
|
2016-01-21 13:27:56 -07:00
|
|
|
std::string GetTempFilenameForAtomicWrite(const std::string& path);
|
2013-10-15 00:52:06 -06:00
|
|
|
|
2015-03-15 07:51:13 -06:00
|
|
|
// Gets a set user directory path
|
|
|
|
// Don't call prior to setting the base user directory
|
|
|
|
const std::string& GetUserPath(unsigned int dir_index);
|
|
|
|
|
|
|
|
// Sets a user directory path
|
|
|
|
// Rebuilds internal directory structure to compensate for the new directory
|
|
|
|
void SetUserPath(unsigned int dir_index, const std::string& path);
|
2013-04-01 22:17:15 -06:00
|
|
|
|
|
|
|
// probably doesn't belong here
|
2013-04-02 12:04:40 -06:00
|
|
|
std::string GetThemeDir(const std::string& theme_name);
|
2009-08-06 00:18:22 -06:00
|
|
|
|
2009-02-28 16:21:51 -07:00
|
|
|
// Returns the path to where the sys file are
|
|
|
|
std::string GetSysDirectory();
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
std::string GetBundleDirectory();
|
|
|
|
#endif
|
|
|
|
|
2016-01-21 13:27:56 -07:00
|
|
|
std::string& GetExeDirectory();
|
2011-06-11 14:45:09 -06:00
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
bool WriteStringToFile(const std::string& str, const std::string& filename);
|
|
|
|
bool ReadFileToString(const std::string& filename, std::string& str);
|
2009-04-12 04:21:40 -06:00
|
|
|
|
2011-03-11 03:21:46 -07:00
|
|
|
// simple wrapper for cstdlib file functions to
|
|
|
|
// hopefully will make error checking easier
|
|
|
|
// and make forgetting an fclose() harder
|
2013-08-29 16:25:12 -06:00
|
|
|
class IOFile : public NonCopyable
|
2011-03-11 03:21:46 -07:00
|
|
|
{
|
|
|
|
public:
|
2016-06-24 02:43:46 -06:00
|
|
|
IOFile();
|
|
|
|
IOFile(std::FILE* file);
|
|
|
|
IOFile(const std::string& filename, const char openmode[]);
|
2013-10-28 23:23:17 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
~IOFile();
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
IOFile(IOFile&& other);
|
|
|
|
IOFile& operator=(IOFile&& other);
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
void Swap(IOFile& other);
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
bool Open(const std::string& filename, const char openmode[]);
|
|
|
|
bool Close();
|
2013-09-15 22:56:49 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
template <typename T>
|
|
|
|
bool ReadArray(T* data, size_t length, size_t* pReadBytes = nullptr)
|
|
|
|
{
|
|
|
|
size_t read_bytes = 0;
|
|
|
|
if (!IsOpen() || length != (read_bytes = std::fread(data, sizeof(T), length, m_file)))
|
|
|
|
m_good = false;
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
if (pReadBytes)
|
|
|
|
*pReadBytes = read_bytes;
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return m_good;
|
|
|
|
}
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
template <typename T>
|
|
|
|
bool WriteArray(const T* data, size_t length)
|
|
|
|
{
|
|
|
|
if (!IsOpen() || length != std::fwrite(data, sizeof(T), length, m_file))
|
|
|
|
m_good = false;
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return m_good;
|
|
|
|
}
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
bool ReadBytes(void* data, size_t length)
|
|
|
|
{
|
|
|
|
return ReadArray(reinterpret_cast<char*>(data), length);
|
|
|
|
}
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
bool WriteBytes(const void* data, size_t length)
|
|
|
|
{
|
|
|
|
return WriteArray(reinterpret_cast<const char*>(data), length);
|
|
|
|
}
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
bool IsOpen() const { return nullptr != m_file; }
|
|
|
|
// m_good is set to false when a read, write or other function fails
|
|
|
|
bool IsGood() const { return m_good; }
|
|
|
|
operator void*() { return m_good ? m_file : nullptr; }
|
|
|
|
std::FILE* ReleaseHandle();
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
std::FILE* GetHandle() { return m_file; }
|
|
|
|
void SetHandle(std::FILE* file);
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
bool Seek(s64 off, int origin);
|
|
|
|
u64 Tell() const;
|
|
|
|
u64 GetSize();
|
|
|
|
bool Resize(u64 size);
|
|
|
|
bool Flush();
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
// clear error state
|
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
m_good = true;
|
|
|
|
std::clearerr(m_file);
|
|
|
|
}
|
2011-03-11 03:21:46 -07:00
|
|
|
|
2017-01-04 15:48:44 -07:00
|
|
|
private:
|
2016-06-24 02:43:46 -06:00
|
|
|
std::FILE* m_file;
|
|
|
|
bool m_good;
|
2011-03-11 03:21:46 -07:00
|
|
|
};
|
|
|
|
|
2008-12-07 21:46:09 -07:00
|
|
|
} // namespace
|
|
|
|
|
2013-02-28 18:33:39 -07:00
|
|
|
// To deal with Windows being dumb at unicode:
|
|
|
|
template <typename T>
|
|
|
|
void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2016-06-24 02:43:46 -06:00
|
|
|
fstream.open(UTF8ToTStr(filename).c_str(), openmode);
|
2013-02-28 18:33:39 -07:00
|
|
|
#else
|
2016-06-24 02:43:46 -06:00
|
|
|
fstream.open(filename.c_str(), openmode);
|
2013-02-28 18:33:39 -07:00
|
|
|
#endif
|
|
|
|
}
|