/** * Try to read offset and vertices data from file, return false on failure * TODO: Read-error-check. */ bool CPathEstimator::ReadFile(const std::string& cacheFileName, const std::string& map) { const unsigned int hash = Hash(); char hashString[50]; sprintf(hashString, "%u", hash); std::string filename = std::string(pathDir) + map + hashString + "." + cacheFileName + ".zip"; // open file for reading from a suitable location (where the file exists) CArchiveZip* pfile = new CArchiveZip(filesystem.LocateFile(filename)); if (!pfile || !pfile->IsOpen()) { delete pfile; return false; } std::auto_ptr<CArchiveZip> auto_pfile(pfile); CArchiveZip& file(*pfile); const unsigned fid = file.FindFile("pathinfo"); if (fid < file.NumFiles()) { pathChecksum = file.GetCrc32(fid); std::vector<boost::uint8_t> buffer; file.GetFile(fid, buffer); if (buffer.size() < 4) return false; unsigned filehash = *((unsigned*)&buffer[0]); if (filehash != hash) return false; unsigned pos = sizeof(unsigned); // Read block-center-offset data. const unsigned blockSize = moveinfo->moveData.size() * sizeof(int2); if (buffer.size() < pos + blockSize * blockStates.GetSize()) return false; for (int blocknr = 0; blocknr < blockStates.GetSize(); blocknr++) { std::memcpy(&blockStates[blocknr].nodeOffsets[0], &buffer[pos], blockSize); pos += blockSize; } // Read vertices data. if (buffer.size() < pos + vertices.size() * sizeof(float)) return false; std::memcpy(&vertices[0], &buffer[pos], vertices.size() * sizeof(float)); // File read successful. return true; } else { return false; } }
/** * Try to write offset and vertex data to file. */ void CPathEstimator::WriteFile(const std::string& cacheFileName, const std::string& map) { // We need this directory to exist if (!filesystem.CreateDirectory(pathDir)) return; const unsigned int hash = Hash(); char hashString[64] = {0}; sprintf(hashString, "%u", hash); const std::string filename = std::string(pathDir) + map + hashString + "." + cacheFileName + ".zip"; zipFile file; // open file for writing in a suitable location file = zipOpen(filesystem.LocateFile(filename, FileSystem::WRITE).c_str(), APPEND_STATUS_CREATE); if (file) { zipOpenNewFileInZip(file, "pathinfo", NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION); // Write hash. zipWriteInFileInZip(file, (void*) &hash, 4); // Write block-center-offsets. for (int blocknr = 0; blocknr < blockStates.GetSize(); blocknr++) zipWriteInFileInZip(file, (void*) &blockStates[blocknr].nodeOffsets[0], moveinfo->moveData.size() * sizeof(int2)); // Write vertices. zipWriteInFileInZip(file, &vertices[0], vertices.size() * sizeof(float)); zipCloseFileInZip(file); zipClose(file, NULL); // get the CRC over the written path data CArchiveZip* pfile = new CArchiveZip(filesystem.LocateFile(filename)); if (!pfile || !pfile->IsOpen()) { delete pfile; return; } std::auto_ptr<CArchiveZip> auto_pfile(pfile); CArchiveZip& file(*pfile); const unsigned fid = file.FindFile("pathinfo"); assert(fid < file.NumFiles()); pathChecksum = file.GetCrc32(fid); } }
/* Trying to read offset and vertices data from file. Return false if failed. TODO: Read-error-check. */ bool CPathEstimator::ReadFile(string name) { unsigned int hash=Hash(); char hashString[50]; sprintf(hashString,"%u",hash); string filename = string("maps/paths/") + stupidGlobalMapname.substr(0, stupidGlobalMapname.find_last_of('.') + 1) + hashString + "." + name + ".zip"; // open file for reading from a suitable location (where the file exists) CArchiveZip* pfile = NULL; std::vector<std::string> filenames = filesystem.GetNativeFilenames(filename, true); for (std::vector<std::string>::iterator it = filenames.begin(); it != filenames.end(); ++it) { pfile = new CArchiveZip(*it); if (pfile->IsOpen()) break; delete pfile; pfile = NULL; } if (!pfile) return false; std::auto_ptr<CArchiveZip> auto_pfile(pfile); CArchiveZip& file(*pfile); int fh = file.OpenFile("pathinfo"); if (fh) { unsigned int filehash = 0; //Check hash. // info->AddLine("%i",hash); file.ReadFile(fh, &filehash, 4); if(filehash != hash) return false; //Read block-center-offset data. int blocknr; for(blocknr = 0; blocknr < nbrOfBlocks; blocknr++) { file.ReadFile(fh, blockState[blocknr].sqrCenter, moveinfo->moveData.size() * sizeof(int2)); } //Read vertices data. file.ReadFile(fh, vertex, nbrOfVertices * sizeof(float)); //File read successful. return true; } else { return false; } }
/* * try to write offset and vertex data to file */ void CPathEstimator::WriteFile(std::string name) { // We need this directory to exist if (!filesystem.CreateDirectory("maps/paths")) return; unsigned int hash = Hash(); char hashString[50]; sprintf(hashString,"%u",hash); std::string filename = std::string("maps/paths/") + stupidGlobalMapname.substr(0, stupidGlobalMapname.find_last_of('.') + 1) + hashString + "." + name + ".zip"; zipFile file; // open file for writing in a suitable location file = zipOpen(filesystem.LocateFile(filename, FileSystem::WRITE).c_str(), APPEND_STATUS_CREATE); if (file) { zipOpenNewFileInZip(file, "pathinfo", NULL, NULL, 0, NULL, 0, NULL, Z_DEFLATED, Z_BEST_COMPRESSION); // Write hash. unsigned int hash = Hash(); zipWriteInFileInZip(file, (void*) &hash, 4); // Write block-center-offsets. for (int blocknr = 0; blocknr < nbrOfBlocks; blocknr++) { zipWriteInFileInZip(file, (void*) blockState[blocknr].sqrCenter, moveinfo->moveData.size() * sizeof(int2)); } // Write vertices. zipWriteInFileInZip(file, (void*) vertex, nbrOfVertices * sizeof(float)); zipCloseFileInZip(file); zipClose(file, NULL); // get the CRC over the written path data CArchiveZip* pfile = SAFE_NEW CArchiveZip(filesystem.LocateFile(filename)); if (!pfile || !pfile->IsOpen()) { delete pfile; return; } std::auto_ptr<CArchiveZip> auto_pfile(pfile); CArchiveZip& file(*pfile); pathChecksum = file.GetCrc32("pathinfo"); } }
/* Trying to read offset and vertices data from file. Return false if failed. TODO: Read-error-check. */ bool CPathEstimator::ReadFile(string name) { unsigned int hash=Hash(); char hashString[50]; sprintf(hashString,"%u",hash); string filename = string("maps/paths/") + stupidGlobalMapname.substr(0, stupidGlobalMapname.find_last_of('.') + 1) + hashString + "." + name + ".zip"; // open file for reading from a suitable location (where the file exists) CArchiveZip* pfile = SAFE_NEW CArchiveZip(filesystem.LocateFile(filename)); if (!pfile || !pfile->IsOpen()) { delete pfile; return false; } std::auto_ptr<CArchiveZip> auto_pfile(pfile); CArchiveZip& file(*pfile); int fh = file.OpenFile("pathinfo"); if (fh) { unsigned int filehash = 0; //Check hash. // logOutput.Print("%i",hash); file.ReadFile(fh, &filehash, 4); if(filehash != hash) return false; //Read block-center-offset data. int blocknr; for(blocknr = 0; blocknr < nbrOfBlocks; blocknr++) { file.ReadFile(fh, blockState[blocknr].sqrCenter, moveinfo->moveData.size() * sizeof(int2)); } //Read vertices data. file.ReadFile(fh, vertex, nbrOfVertices * sizeof(float)); //File read successful. return true; } else { return false; } }