diff --git a/Source/lang.cpp b/Source/lang.cpp index 1994741b..273bd134 100644 --- a/Source/lang.cpp +++ b/Source/lang.cpp @@ -912,7 +912,7 @@ TCHAR SkipComments(FILE *f) { // NSIS Language File parser LanguageTable * CEXEBuild::LoadLangFile(TCHAR *filename) { - FILE *f = FOPENTEXT(filename, _T("r")); + FILE *f = FOPENTEXT(filename, "r"); if (!f) { ERROR_MSG(_T("Error: Can't open language file - \"%s\"!\n"),filename); return 0; diff --git a/Source/makenssi.cpp b/Source/makenssi.cpp index eccead14..8a716680 100644 --- a/Source/makenssi.cpp +++ b/Source/makenssi.cpp @@ -189,7 +189,7 @@ static tstring get_home() static int process_config(CEXEBuild& build, tstring& conf) { - FILE *cfg=FOPENTEXT(conf.c_str(),_T("rt")); + FILE *cfg=FOPENTEXT(conf.c_str(),"rt"); if (cfg) { if (build.display_script) @@ -298,7 +298,7 @@ int _tmain(int argc, TCHAR **argv) { if (argc > tmpargpos && IS_OPT(argv[tmpargpos]) && (argv[tmpargpos][1]==_T('o') || argv[tmpargpos][1]==_T('O')) && argv[tmpargpos][2]) { - g_output=FOPENTEXT(argv[tmpargpos]+2,_T("w")); + g_output=FOPENTEXT(argv[tmpargpos]+2,"w"); if (!g_output) { _tprintf(_T("Error opening output log for writing. Using stdout.\n")); @@ -344,7 +344,7 @@ int _tmain(int argc, TCHAR **argv) { if (!outputtried) { - g_output=FOPENTEXT(argv[argpos]+2,_T("w")); + g_output=FOPENTEXT(argv[argpos]+2,"w"); if (!g_output) { if (build.display_errors) _tprintf(_T("Error opening output log for writing. Using stdout.\n")); @@ -479,11 +479,11 @@ int _tmain(int argc, TCHAR **argv) else { _tcscpy(sfile,argv[argpos]); - fp=FOPENTEXT(sfile,_T("rt")); + fp=FOPENTEXT(sfile,"rt"); if (!fp) { _stprintf(sfile,_T("%s.nsi"),argv[argpos]); - fp=FOPENTEXT(sfile,_T("rt")); + fp=FOPENTEXT(sfile,"rt"); if (!fp) { if (build.display_errors) diff --git a/Source/script.cpp b/Source/script.cpp index 0fbdbb83..bc3629b0 100644 --- a/Source/script.cpp +++ b/Source/script.cpp @@ -743,7 +743,7 @@ int CEXEBuild::parseScript() int CEXEBuild::includeScript(TCHAR *f) { SCRIPT_MSG(_T("!include: \"%s\"\n"),f); - FILE *incfp=FOPENTEXT(f,_T("rt")); + FILE *incfp=FOPENTEXT(f,"rt"); if (!incfp) { ERROR_MSG(_T("!include: could not open file: \"%s\"\n"),f); @@ -1222,7 +1222,7 @@ int CEXEBuild::doCommand(int which_token, LineParser &line) TCHAR *file = line.gettoken_str(1); TCHAR *text = line.gettoken_str(2); - FILE *fp = FOPENTEXT(file, _T("a")); + FILE *fp = FOPENTEXT(file, "a"); if (!fp) { ERROR_MSG(_T("!appendfile: \"%s\" couldn't be opened.\n"), file); @@ -2834,7 +2834,7 @@ int CEXEBuild::doCommand(int which_token, LineParser &line) define=line.gettoken_str(2); const TCHAR *filename=line.gettoken_str(3); - FILE *fp=FOPENTEXT(filename,_T("r")); + FILE *fp=FOPENTEXT(filename,"r"); if (!fp && _tcsicmp(define,_T("/file_noerr"))) { ERROR_MSG(_T("!define /file: file not found (\"%s\")\n"),filename); @@ -3122,7 +3122,7 @@ int CEXEBuild::doCommand(int which_token, LineParser &line) if (isFile) { - FILE *fp=FOPENTEXT(source_string,_T("r")); + FILE *fp=FOPENTEXT(source_string,"r"); if (!fp) { ERROR_MSG(_T("!searchparse /file: error opening \"%s\"\n"),source_string); diff --git a/Source/tstring.cpp b/Source/tstring.cpp index 7fcbd228..3aa703a7 100644 --- a/Source/tstring.cpp +++ b/Source/tstring.cpp @@ -16,31 +16,9 @@ #include "tstring.h" #include "validateunicode.h" +#include "util.h" #include -// Simple RAII for C-styled FILE pointers. -class ScopedFile -{ - public: - ScopedFile(FILE* file) : m_file(file) {} - - ~ScopedFile() - { - if (this->m_file != NULL) - { - fflush(this->m_file); - fclose(this->m_file); - } - } - - operator FILE*(){ return this->m_file; } - - operator bool() { return this->m_file != NULL; } - - private: - FILE* m_file; -}; - FILE* FileOpenUnicodeText(const TCHAR* file, const TCHAR* mode) { extern FILE *g_output; @@ -51,10 +29,11 @@ FILE* FileOpenUnicodeText(const TCHAR* file, const TCHAR* mode) if (_tcsstr(mode, _T("w+")) || _tcsstr(mode, _T("r"))) { - ScopedFile fp(_tfopen(file, _T("rb"))); + FILE* fp = _tfopen(file, _T("rb")); if (fp) { + MANAGE_WITH(fp, fclose); fseek(fp, 0, SEEK_END); size_t fileSize = ftell(fp); if (fileSize == 0) diff --git a/Source/tstring.h b/Source/tstring.h index 25175b63..066ee961 100644 --- a/Source/tstring.h +++ b/Source/tstring.h @@ -30,7 +30,7 @@ typedef std::wofstream tofstream; typedef std::wifstream tifstream; // Use the following macros to open text files. FILE* FileOpenUnicodeText(const TCHAR* file, const TCHAR* mode); -#define FOPENTEXT(file, mode) FileOpenUnicodeText(file, mode) +#define FOPENTEXT(file, mode) FileOpenUnicodeText(file, _T(mode)) #else typedef std::string tstring; typedef std::ofstream tofstream;