From 1ca352265667d9f1d244fa751be6b631654582a1 Mon Sep 17 00:00:00 2001 From: albertzeyer Date: Fri, 24 Jul 2009 15:42:10 +0000 Subject: [PATCH] wrapper for ifstream/ofstream git-svn-id: https://clonekeenplus.svn.sourceforge.net/svnroot/clonekeenplus/cgenius/trunk@150 4df4b0f3-56ce-47cb-b001-ed939b7d65a6 --- src/FindFile.cpp | 36 ++++++++++++++++++++++-------------- src/FindFile.h | 9 ++++++--- src/vorticon/CHighScores.cpp | 2 +- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/FindFile.cpp b/src/FindFile.cpp index 19b24618c..dbb6a4e79 100644 --- a/src/FindFile.cpp +++ b/src/FindFile.cpp @@ -23,6 +23,7 @@ #pragma warning(disable: 4503) // WARNING: decorated name length exceeded, name was truncated #endif +#include #include "FindFile.h" #include "StringUtils.h" #include "Debug.h" @@ -647,30 +648,37 @@ FILE *OpenGameFile(const std::string& path, const char *mode) { } - -std::ifstream* OpenGameFileR(const std::string& path) { - // TODO: unicode support! (?) - +bool OpenGameFileR(std::ifstream& f, const std::string& path, std::ios_base::openmode mode) { if(path.size() == 0) - return NULL; + return false; std::string fullfn = GetFullFileName(path); if(fullfn.size() != 0) { try { - std::ifstream* f = new std::ifstream(fullfn.c_str(), std::ios::in | std::ios::binary); - if (f->is_open()) - return f; - else { - delete f; - return NULL; - } + f.open(Utf8ToSystemNative(fullfn).c_str(), mode); + return f.is_open(); } catch(...) {} - return NULL; + return false; } - return NULL; + return false; } +bool OpenGameFileW(std::ofstream& f, const std::string& path, std::ios_base::openmode mode) { + if(path.size() == 0) + return false; + + std::string fullfn = GetWriteFullFileName(path, true); + if(fullfn.size() != 0) { + try { + f.open(Utf8ToSystemNative(fullfn).c_str(), mode); + return f.is_open(); + } catch(...) {} + return false; + } + + return false; +} void AddToFileList(searchpathlist* l, const std::string& f) { diff --git a/src/FindFile.h b/src/FindFile.h index bd08d8827..2244ea14b 100644 --- a/src/FindFile.h +++ b/src/FindFile.h @@ -20,7 +20,7 @@ #ifndef __FINDFILE_H__ #define __FINDFILE_H__ -#include +#include #include #include #include @@ -80,9 +80,10 @@ extern std::string binary_dir; #endif -class drive_t { public: +struct drive_t { std::string name; unsigned int type; + drive_t() : type(0) {} }; typedef std::vector drive_list; @@ -183,7 +184,9 @@ FILE* OpenGameFile(const std::string& path, const char *mode); FILE* OpenAbsFile(const std::string& path, const char *mode); -std::ifstream* OpenGameFileR(const std::string& path); +bool OpenGameFileR(std::ifstream& f, const std::string& path, std::ios_base::openmode mode = std::ios_base::in); +bool OpenGameFileW(std::ofstream& f, const std::string& path, std::ios_base::openmode mode = std::ios_base::in); + std::string GetFileContents(const std::string& path, bool absolute = false); std::string ExtractDirectory(const std::string& path); diff --git a/src/vorticon/CHighScores.cpp b/src/vorticon/CHighScores.cpp index c398b7a8f..434ae4a06 100644 --- a/src/vorticon/CHighScores.cpp +++ b/src/vorticon/CHighScores.cpp @@ -374,7 +374,7 @@ char CHighScores::loadHighScoreTable(void) sBuf.append(chBuf); sBuf.append(".dat"); - ifstream ScoreTableFile (sBuf.data(), ios::binary); + ifstream ScoreTableFile (sBuf.c_str(), ios::binary); if(ScoreTableFile == NULL) {