bool FileToBuffer(MPQHANDLE &hMpq, const std::string &fileName, buffer &buf) { if ( hMpq == nullptr ) CHKD_ERR("NULL MPQ file specified for opening %s", fileName.c_str()); else { u32 bytesRead = 0; HANDLE openFile = NULL; if ( SFileGetFileInfo(hMpq, SFILE_INFO_NUM_FILES) != 0xFFFFFFFF ) { if ( SFileOpenFileEx(hMpq, fileName.c_str(), SFILE_SEARCH_CURRENT_ONLY, &openFile) ) { u32 fileSize = (u32)SFileGetFileSize(openFile, NULL); if ( buf.setSize(fileSize) ) { buf.sizeUsed = fileSize; SFileReadFile(openFile, (LPVOID)buf.data, buf.sizeUsed, (LPDWORD)(&bytesRead), NULL); SFileCloseFile(openFile); if ( buf.sizeUsed == bytesRead ) return true; } else SFileCloseFile(openFile); } else CHKD_ERR("Failed to get %s from MPQ file", fileName.c_str()); } else CHKD_ERR("File is already open", fileName.c_str()); } return false; }
bool FileToBuffer(const std::string &fileName, buffer &buf) { bool success = false; if ( fileName.length() > 0 ) { FILE* pFile = std::fopen(fileName.c_str(), "rb"); if ( pFile != nullptr ) { buf.flush(); std::fseek(pFile, 0, SEEK_END); u32 fileSize = (u32)std::ftell(pFile); if ( buf.setSize(fileSize) ) { buf.sizeUsed = fileSize; std::rewind(pFile); size_t lengthRead = std::fread(buf.data, 1, buf.sizeUsed, pFile); success = (lengthRead == buf.sizeUsed); } std::fclose(pFile); } } return success; }