From 615ce82030e50937bd6b68ffc588dc942e184ba5 Mon Sep 17 00:00:00 2001 From: anders_k Date: Mon, 27 Feb 2012 22:16:45 +0000 Subject: [PATCH] ifstream does not have a wchar_t constructor, use FILE* and our FOPEN wrapper. git-svn-id: https://svn.code.sf.net/p/nsis/code/NSIS/trunk@6215 212acab6-be3b-0410-9dea-997c60f758d6 --- Source/Plugins.cpp | 42 +++++++++++++++--------------------------- 1 file changed, 15 insertions(+), 27 deletions(-) diff --git a/Source/Plugins.cpp b/Source/Plugins.cpp index 167c9345..a4d97146 100644 --- a/Source/Plugins.cpp +++ b/Source/Plugins.cpp @@ -63,38 +63,25 @@ struct NSISException : public std::runtime_error }; namespace { -size_t file_size(ifstream& file) { - const ifstream::pos_type pos = file.tellg(); - - file.seekg(0, ios::end); - - ifstream::pos_type result = file.tellg(); - assert(result >= (ifstream::pos_type)0); - - file.seekg(pos); - - return (size_t)result; -} - // This function slurps the whole file into the vector. // Modified so the huge vector isn't returned by value. void read_file(const tstring& filename, vector& data) { - ifstream file(filename.c_str(), ios::binary); + FILE*file = FOPEN(filename.c_str(), _T("rb")); - if (!file) { - throw NSISException(_T("Can't open file '") + filename + _T("'")); - } - - // get the file size - size_t filesize = file_size(file); - - data.resize(filesize); - - file.read(reinterpret_cast(&data[0]), filesize); - - if (size_t(file.tellg()) != filesize) { // ifstream::eof doesn't return true here - throw NSISException(_T("Couldn't read entire file '") + filename + _T("'")); + if (!file) throw NSISException(_T("Can't open file '") + filename + _T("'")); + + MANAGE_WITH(file, fclose); + bool succ = false; + + if (!fseek(file, 0, SEEK_END)) + { + const long filesize = ftell(file); + rewind(file); + data.resize(filesize); + size_t cbio = fread(reinterpret_cast(&data[0]), 1, filesize, file); + succ = cbio == filesize; } + if (!succ) throw NSISException(_T("Couldn't read entire file '") + filename + _T("'")); } } @@ -104,6 +91,7 @@ void Plugins::GetExports(const tstring &pathToDll, bool displayInfo) PIMAGE_NT_HEADERS NTHeaders; try { read_file(pathToDll, dlldata); + if (dlldata.empty()) return; NTHeaders = CResourceEditor::GetNTHeaders(&dlldata[0]); } catch (std::runtime_error&) { return;