char* DataManager::readLine() { if (bufferIndex >= bufferEnd) reloadBuffer(); while (true) { for (auto i = bufferIndex; i < bufferEnd; i++) { if (buffer[i] == '\r') { buffer[i] = '\0'; } else if (buffer[i] == '\n') { buffer[i] = '\0'; auto line = buffer + bufferIndex; bufferIndex = i + 1; return line; } } if (!reloadBuffer()) { if (bufferIndex >= bufferEnd) return nullptr; buffer[bufferEnd] = '\0'; auto line = buffer + bufferIndex; bufferIndex = bufferEnd; return line; } } }
void moveForward(FileBuffer *b) { if (b->current == (b->buf1) + b->size_of_buffer - 1) { reloadBuffer(b, 2); b->current = b->buf2; b->curBuf = 2; } else if (b->current == (b->buf2) + b->size_of_buffer - 1) { reloadBuffer(b, 1); b->current = b->buf1; b->curBuf = 1; } else { b->current += 1; } }
FileReader::FileReader(std::string inPath, size_t size) { path=inPath; bufferLength = size; //fs = fopen(path.c_str(), "rb"); fs.open(path.c_str(), std::ifstream::in | std::ifstream::binary); if (!(fs)) { std::cerr << "ERROR FileReader::FileReader - Couldnt open file: " << path << std::endl; exit(EXIT_FAILURE); } loadBuffer = new char[bufferLength]; workBuffer = new char[bufferLength]; localLoad = true; endOfLoadBuf = 0; blockSize = BLOCKSIZE; readSize = blockSize; reloadBuffer(); globalAdress = 0; offset = 0; asyncReload(); }
int IPSget() { int retVal; if (IPSPatch.current == IPSPatch.data + IPSPatch.buffer_total) { if (!reloadBuffer()) { return(-1); } } IPSPatch.proccessed++; retVal = *IPSPatch.current; IPSPatch.current++; return(retVal); }
bool findZipIPS(char *compressedfile, const char *ext) { bool FoundIPS = false; unz_file_info cFileInfo; //Create variable to hold info for a compressed file int cFile; memset(&IPSPatch, 0, sizeof(IPSPatch)); IPSPatch.zipfile = unzopen_dir(ZRomPath, compressedfile); //Open zip file cFile = unzGoToFirstFile(IPSPatch.zipfile); //Set cFile to first compressed file while(cFile == UNZ_OK) //While not at end of compressed file list { //Temporary char array for file name char cFileName[256]; //Gets info on current file, and places it in cFileInfo unzGetCurrentFileInfo(IPSPatch.zipfile, &cFileInfo, cFileName, 256, NULL, 0, NULL, 0); //Find IPS file if (isextension(cFileName, ext)) { FoundIPS = true; break; } //Go to next file in zip file cFile = unzGoToNextFile(IPSPatch.zipfile); } if (FoundIPS) { //Open file unzOpenCurrentFile(IPSPatch.zipfile); IPSPatch.file_size = (unsigned int)cFileInfo.uncompressed_size; if ((IPSPatch.data = (unsigned char *)malloc(BUFFER_SIZE))) { reloadBuffer(); return(PatchUsingIPS(0)); } else { deinitPatch(); } } else { unzClose(IPSPatch.zipfile); IPSPatch.zipfile = 0; } return(false); }
bool initPatch(const char *ext) { memset(&IPSPatch, 0, sizeof(IPSPatch)); setextension(ZSaveName, ext); IPSPatch.fp = fopen_dir(ZIpsPath, ZSaveName, "rb"); if (!IPSPatch.fp) { IPSPatch.fp = fopen_dir(ZRomPath, ZSaveName, "rb"); } if (!IPSPatch.fp) { return(false); } fseek(IPSPatch.fp, 0, SEEK_END); IPSPatch.file_size = (unsigned int)ftell(IPSPatch.fp); rewind(IPSPatch.fp); if ((IPSPatch.data = (unsigned char *)malloc(BUFFER_SIZE))) { return(reloadBuffer()); } return(false); }
void FileReader::reset() { localMtx.lock(); while (localLoad == false) { localMtx.unlock(); usleep(1000*5); localMtx.lock(); } localLoad = false; localMtx.unlock(); //rewind(fs); fs.clear(); fs.seekg(0,fs.beg); reloadBuffer(); globalAdress = 0; localMtx.lock(); localLoad = true; localMtx.unlock(); asyncReload(); }
bool FileReader::asyncReload() { auto lambda = [this] () -> bool { bool ret = reloadBuffer(); localMtx.lock(); localLoad = true; localMtx.unlock(); return ret; }; localMtx.lock(); while (localLoad == false) { localMtx.unlock(); usleep(1000*5); localMtx.lock(); } localLoad = false; //localMtx.unlock(); std::future_status status; if (threadSync.valid()) { while (true) { localMtx.unlock(); try { status = threadSync.wait_for(std::chrono::milliseconds(20)); } catch (std::future_error &e) { continue; } if (status == std::future_status::ready) { break; } localMtx.lock(); } if (threadSync.get()==false || endOfLoadBuf==0 /*|| fs.bad() || fs.eof()*/) { localLoad = true; localMtx.unlock(); return false; } } /* if (fs.bad() || endOfLoadBuf==0 || fs.eof()) { localMtx.lock(); localLoad = true; localMtx.unlock(); return false; } */ mtx.lock(); while (loadAvail == false) { mtx.unlock(); usleep(100*5); mtx.lock(); } char *tmp = loadBuffer; loadBuffer = workBuffer; workBuffer = tmp; block = &workBuffer[0]; offset = 0; endOfWorkBuf = endOfLoadBuf; readSize = blockSize; globalAdress += bufferLength; mtx.unlock(); //std::cout << globalAdress << "\t" << endOfWorkBuf << std::endl; localLoad = false; localMtx.unlock(); //even when not used, have to use future object, otherwise the out-of-scope destructor will wait for .get() (therefore no ASYNC LAUNCH ANYMORE) threadSync = std::async(std::launch::async, lambda); return true; }