From edaf59a759f26f2caf6ba70de47f52b667743e5c Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 12 Sep 2014 00:16:54 +0200 Subject: =?UTF-8?q?Common:=20Return=20the=20number=20of=20items=20read/wri?= =?UTF-8?q?tten=20in=20IOFile=E2=80=99s=20methods=20instead=20of=20a=20boo?= =?UTF-8?q?lean.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/file_util.h | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'src/common') diff --git a/src/common/file_util.h b/src/common/file_util.h index fdae5c9c8..8efff4958 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -151,29 +151,41 @@ public: bool Close(); template - bool ReadArray(T* data, size_t length) + size_t ReadArray(T* data, size_t length) { - if (!IsOpen() || length != std::fread(data, sizeof(T), length, m_file)) + if (!IsOpen()) { m_good = false; + return -1; + } - return m_good; + size_t items_read = std::fread(data, sizeof(T), length, m_file); + if (items_read != length) + m_good = false; + + return items_read; } template - bool WriteArray(const T* data, size_t length) + size_t WriteArray(const T* data, size_t length) { - if (!IsOpen() || length != std::fwrite(data, sizeof(T), length, m_file)) + if (!IsOpen()) { + m_good = false; + return -1; + } + + size_t items_written = std::fwrite(data, sizeof(T), length, m_file); + if (items_written != length) m_good = false; - return m_good; + return items_written; } - bool ReadBytes(void* data, size_t length) + size_t ReadBytes(void* data, size_t length) { return ReadArray(reinterpret_cast(data), length); } - bool WriteBytes(const void* data, size_t length) + size_t WriteBytes(const void* data, size_t length) { return WriteArray(reinterpret_cast(data), length); } -- cgit v1.2.3 From 6b7b36a8745ddfd0e24fa51ef74dbc3058b3dacc Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 12 Sep 2014 00:17:15 +0200 Subject: Common: Rename the File namespace to FileUtil, to match the filename and prevent collisions. --- src/common/chunk_file.h | 8 ++++---- src/common/file_util.cpp | 38 +++++++++++++++++++------------------- src/common/file_util.h | 2 +- src/common/log_manager.cpp | 2 +- 4 files changed, 25 insertions(+), 25 deletions(-) (limited to 'src/common') diff --git a/src/common/chunk_file.h b/src/common/chunk_file.h index 2b0f120e6..7a3b537c7 100644 --- a/src/common/chunk_file.h +++ b/src/common/chunk_file.h @@ -671,7 +671,7 @@ public: _failureReason->clear(); _failureReason->append("LoadStateWrongVersion"); - if (!File::Exists(_rFilename)) { + if (!FileUtil::Exists(_rFilename)) { _failureReason->clear(); _failureReason->append("LoadStateDoesntExist"); ERROR_LOG(COMMON, "ChunkReader: File doesn't exist"); @@ -679,7 +679,7 @@ public: } // Check file size - const u64 fileSize = File::GetSize(_rFilename); + const u64 fileSize = FileUtil::GetSize(_rFilename); static const u64 headerSize = sizeof(SChunkHeader); if (fileSize < headerSize) { @@ -687,7 +687,7 @@ public: return ERROR_BAD_FILE; } - File::IOFile pFile(_rFilename, "rb"); + FileUtil::IOFile pFile(_rFilename, "rb"); if (!pFile) { ERROR_LOG(COMMON,"ChunkReader: Can't open file for reading"); @@ -765,7 +765,7 @@ public: { INFO_LOG(COMMON, "ChunkReader: Writing %s" , _rFilename.c_str()); - File::IOFile pFile(_rFilename, "wb"); + FileUtil::IOFile pFile(_rFilename, "wb"); if (!pFile) { ERROR_LOG(COMMON,"ChunkReader: Error opening file for write"); diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index 970041007..ecfccbd66 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -39,7 +39,7 @@ // This namespace has various generic functions related to files and paths. // The code still needs a ton of cleanup. // REMEMBER: strdup considered harmful! -namespace File +namespace FileUtil { // Remove any ending forward slashes from directory paths @@ -172,7 +172,7 @@ bool CreateFullPath(const std::string &fullPath) int panicCounter = 100; INFO_LOG(COMMON, "CreateFullPath: path %s", fullPath.c_str()); - if (File::Exists(fullPath)) + if (FileUtil::Exists(fullPath)) { INFO_LOG(COMMON, "CreateFullPath: path exists %s", fullPath.c_str()); return true; @@ -190,8 +190,8 @@ bool CreateFullPath(const std::string &fullPath) // Include the '/' so the first call is CreateDir("/") rather than CreateDir("") std::string const subPath(fullPath.substr(0, position + 1)); - if (!File::IsDirectory(subPath)) - File::CreateDir(subPath); + if (!FileUtil::IsDirectory(subPath)) + FileUtil::CreateDir(subPath); // A safety check panicCounter--; @@ -211,7 +211,7 @@ bool DeleteDir(const std::string &filename) INFO_LOG(COMMON, "DeleteDir: directory %s", filename.c_str()); // check if a directory - if (!File::IsDirectory(filename)) + if (!FileUtil::IsDirectory(filename)) { ERROR_LOG(COMMON, "DeleteDir: Not a directory %s", filename.c_str()); return false; @@ -386,7 +386,7 @@ bool CreateEmptyFile(const std::string &filename) { INFO_LOG(COMMON, "CreateEmptyFile: %s", filename.c_str()); - if (!File::IOFile(filename, "wb")) + if (!FileUtil::IOFile(filename, "wb")) { ERROR_LOG(COMMON, "CreateEmptyFile: failed %s: %s", filename.c_str(), GetLastErrorMsg()); @@ -519,7 +519,7 @@ bool DeleteDirRecursively(const std::string &directory) } else { - if (!File::Delete(newPath)) + if (!FileUtil::Delete(newPath)) { #ifndef _WIN32 closedir(dirp); @@ -536,7 +536,7 @@ bool DeleteDirRecursively(const std::string &directory) } closedir(dirp); #endif - File::DeleteDir(directory); + FileUtil::DeleteDir(directory); return true; } @@ -546,8 +546,8 @@ void CopyDir(const std::string &source_path, const std::string &dest_path) { #ifndef _WIN32 if (source_path == dest_path) return; - if (!File::Exists(source_path)) return; - if (!File::Exists(dest_path)) File::CreateFullPath(dest_path); + if (!FileUtil::Exists(source_path)) return; + if (!FileUtil::Exists(dest_path)) FileUtil::CreateFullPath(dest_path); struct dirent dirent, *result = NULL; DIR *dirp = opendir(source_path.c_str()); @@ -569,10 +569,10 @@ void CopyDir(const std::string &source_path, const std::string &dest_path) { source += '/'; dest += '/'; - if (!File::Exists(dest)) File::CreateFullPath(dest); + if (!FileUtil::Exists(dest)) FileUtil::CreateFullPath(dest); CopyDir(source, dest); } - else if (!File::Exists(dest)) File::Copy(source, dest); + else if (!FileUtil::Exists(dest)) FileUtil::Copy(source, dest); } closedir(dirp); #endif @@ -660,7 +660,7 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string &new #ifdef _WIN32 paths[D_USER_IDX] = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP; #else - if (File::Exists(ROOT_DIR DIR_SEP USERDATA_DIR)) + if (FileUtil::Exists(ROOT_DIR DIR_SEP USERDATA_DIR)) paths[D_USER_IDX] = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP; else paths[D_USER_IDX] = std::string(getenv("HOME") ? @@ -688,7 +688,7 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string &new if (!newPath.empty()) { - if (!File::IsDirectory(newPath)) + if (!FileUtil::IsDirectory(newPath)) { WARN_LOG(COMMON, "Invalid path specified %s", newPath.c_str()); return paths[DirIDX]; @@ -750,11 +750,11 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string &new //std::string GetThemeDir(const std::string& theme_name) //{ -// std::string dir = File::GetUserPath(D_THEMES_IDX) + theme_name + "/"; +// std::string dir = FileUtil::GetUserPath(D_THEMES_IDX) + theme_name + "/"; // //#if !defined(_WIN32) // // If theme does not exist in user's dir load from shared directory -// if (!File::Exists(dir)) +// if (!FileUtil::Exists(dir)) // dir = SHARED_USER_DIR THEMES_DIR "/" + theme_name + "/"; //#endif // @@ -763,12 +763,12 @@ const std::string& GetUserPath(const unsigned int DirIDX, const std::string &new bool WriteStringToFile(bool text_file, const std::string &str, const char *filename) { - return File::IOFile(filename, text_file ? "w" : "wb").WriteBytes(str.data(), str.size()); + return FileUtil::IOFile(filename, text_file ? "w" : "wb").WriteBytes(str.data(), str.size()); } bool ReadFileToString(bool text_file, const char *filename, std::string &str) { - File::IOFile file(filename, text_file ? "r" : "rb"); + FileUtil::IOFile file(filename, text_file ? "r" : "rb"); auto const f = file.GetHandle(); if (!f) @@ -854,7 +854,7 @@ void IOFile::SetHandle(std::FILE* file) u64 IOFile::GetSize() { if (IsOpen()) - return File::GetSize(m_file); + return FileUtil::GetSize(m_file); else return 0; } diff --git a/src/common/file_util.h b/src/common/file_util.h index 8efff4958..897cbd77e 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -43,7 +43,7 @@ enum { NUM_PATH_INDICES }; -namespace File +namespace FileUtil { // FileSystem tree node/ diff --git a/src/common/log_manager.cpp b/src/common/log_manager.cpp index 43346f279..28b72fa20 100644 --- a/src/common/log_manager.cpp +++ b/src/common/log_manager.cpp @@ -75,7 +75,7 @@ LogManager::LogManager() m_Log[LogTypes::MEMCARD_MANAGER] = new LogContainer("MemCard Manager", "MemCard Manager"); m_Log[LogTypes::NETPLAY] = new LogContainer("NETPLAY", "Netplay"); - m_fileLog = new FileLogListener(File::GetUserPath(F_MAINLOG_IDX).c_str()); + m_fileLog = new FileLogListener(FileUtil::GetUserPath(F_MAINLOG_IDX).c_str()); m_consoleLog = new ConsoleListener(); m_debuggerLog = new DebuggerLogListener(); -- cgit v1.2.3