int OSBasics::seek(OSFILE * stream, long int offset, int origin ) { switch(stream->fileType) { case OSFILE::TYPE_FILE: return fseek(stream->file, offset, origin); break; case OSFILE::TYPE_ARCHIVE_FILE: switch(origin) { case SEEK_SET: return PHYSFS_seek(stream->physFSFile, offset); break; case SEEK_CUR: { PHYSFS_sint64 curoffset = PHYSFS_tell(stream->physFSFile); return PHYSFS_seek(stream->physFSFile, curoffset+offset); } break; case SEEK_END: { PHYSFS_sint64 fileLength = PHYSFS_fileLength(stream->physFSFile); return PHYSFS_seek(stream->physFSFile, fileLength-offset); } break; } break; } return 0; }
int OggSoundFile::cb_seek(void* source, ogg_int64_t offset, int whence) { PHYSFS_file* file = reinterpret_cast<PHYSFS_file*> (source); switch(whence) { case SEEK_SET: if(PHYSFS_seek(file, static_cast<PHYSFS_uint64> (offset)) == 0) return -1; break; case SEEK_CUR: if(PHYSFS_seek(file, PHYSFS_tell(file) + offset) == 0) return -1; break; case SEEK_END: if(PHYSFS_seek(file, PHYSFS_fileLength(file) + offset) == 0) return -1; break; default: #ifdef DEBUG assert(false); #else return -1; #endif } return 0; }
static int funcSeek(struct SDL_RWops* context, int offset, int whence) { PHYSFS_file* file = (PHYSFS_file*) context->hidden.unknown.data1; int res; switch(whence) { case SEEK_SET: res = PHYSFS_seek(file, offset); break; case SEEK_CUR: res = PHYSFS_seek(file, PHYSFS_tell(file) + offset); break; case SEEK_END: res = PHYSFS_seek(file, PHYSFS_fileLength(file) + offset); break; default: res = 0; assert(false); break; } if(res == 0) { std::cerr << "Error seeking in file: " << PHYSFS_getLastError() << "\n"; return -1; } return (int) PHYSFS_tell(file); }
int state_get_game_id(char *filename) { int version; PHYSFS_file *fp; int between_levels; char mission[16]; char desc[DESC_LENGTH+1]; char id[5]; int dumbint; mprintf((0, "Restoring multigame from [%s]\n", filename)); fp = PHYSFS_openRead(filename); if (!fp) return 0; //Read id //FIXME: check for swapped file, react accordingly... PHYSFS_read(fp, id, sizeof(char) * 4, 1); if ( memcmp( id, dgss_id, 4 )) { PHYSFS_close(fp); return 0; } //Read version PHYSFS_read(fp, &version, sizeof(int), 1); if (version < STATE_COMPATIBLE_VERSION) { PHYSFS_close(fp); return 0; } // Read description PHYSFS_read(fp, desc, sizeof(char) * DESC_LENGTH, 1); // Skip the current screen shot... PHYSFS_seek(fp, PHYSFS_tell(fp) + THUMBNAIL_W * THUMBNAIL_H); // And now...skip the palette stuff that somebody forgot to add PHYSFS_seek(fp, PHYSFS_tell(fp) + 768); // Read the Between levels flag... PHYSFS_read(fp, &between_levels, sizeof(int), 1); Assert(between_levels == 0); //between levels save ripped out // Read the mission info... PHYSFS_read(fp, mission, sizeof(char) * 9, 1); //Read level info PHYSFS_read(fp, &dumbint, sizeof(int), 1); PHYSFS_read(fp, &dumbint, sizeof(int), 1); //Restore GameTime PHYSFS_read(fp, &dumbint, sizeof(fix), 1); PHYSFS_read(fp, &state_game_id, sizeof(int), 1); return (state_game_id); }
static Sint64 SDL_RWopsSeek(SDL_RWops *ops, int64_t offset, int whence) { PHYSFS_File *f = sdlPHYS(ops); if (!f) return -1; int64_t base; switch (whence) { default: case RW_SEEK_SET : base = 0; break; case RW_SEEK_CUR : base = PHYSFS_tell(f); break; case RW_SEEK_END : base = PHYSFS_fileLength(f); break; } int result = PHYSFS_seek(f, base + offset); return (result != 0) ? PHYSFS_tell(f) : -1; }
bool vsFile::PeekRecord( vsRecord *r ) { vsAssert(r != NULL, "Called vsFile::Record with a NULL vsRecord!"); if ( m_mode == MODE_Write ) { vsAssert( m_mode != MODE_Write, "Error: Not legal to PeekRecord() when writing a file!" ); return false; } else { bool succeeded = false; vsString line; r->Init(); PHYSFS_sint64 filePos = PHYSFS_tell(m_file); if ( r->Parse(this) ) succeeded = true; PHYSFS_seek(m_file, filePos); return succeeded; } return false; }
void File::ReadWholeFile(std::vector<char>& bytes) { if (PHYSFS_seek(impl->physfsFileHandle, 0) == 0) { throw Exception(std::string("Seek failed on file ") + mountedFilePath.path + "\n" + PHYSFS_getLastError()); } int64_t fileSizeInBytes = SizeInBytes(); if (fileSizeInBytes == -1) { throw Exception(std::string("File sizing failed on file") + mountedFilePath.path + "\n" + PHYSFS_getLastError()); } std::size_t bytesToRead = static_cast<size_t>(fileSizeInBytes); std::size_t maxBytesToRead = std::numeric_limits<std::size_t>::max(); if (static_cast<uint64_t>(fileSizeInBytes) > maxBytesToRead) { bytesToRead = maxBytesToRead; } bytes.resize(bytesToRead); int64_t bytesRead = Read(bytes.data(), bytesToRead); if (static_cast<uint64_t>(bytesRead) != bytesToRead) { throw Exception(std::string("Failed to read whole file") + mountedFilePath.path + "\n" + PHYSFS_getLastError()); } }
bool File::Seek(std::size_t offset, DataStream::SeekType seekType) { std::size_t seekPosition = 0; switch (seekType) { case SeekType::Beginning: seekPosition = offset; break; case SeekType::Current: seekPosition = CurrentPosition() + offset; break; case SeekType::End: { std::size_t sizeInBytes = SizeInBytes(); if (offset > sizeInBytes) { return false; } seekPosition = (sizeInBytes - offset); } break; default: return false; } return (PHYSFS_seek(impl->physfsFileHandle, seekPosition) != 0); }
SoundFile* load_sound_file(const std::string& filename) { if(filename.length() > 6 && filename.compare(filename.length()-6, 6, ".music") == 0) { return load_music_file(filename); } PHYSFS_file* file = PHYSFS_openRead(filename.c_str()); if(!file) { std::stringstream msg; msg << "Couldn't open '" << filename << "': " << PHYSFS_getLastError(); throw std::runtime_error(msg.str()); } try { char magic[4]; if(PHYSFS_read(file, magic, sizeof(magic), 1) != 1) throw std::runtime_error("Couldn't read magic, file too short"); PHYSFS_seek(file, 0); if(strncmp(magic, "RIFF", 4) == 0) return new WavSoundFile(file); else if(strncmp(magic, "OggS", 4) == 0) return new OggSoundFile(file, 0, -1); else throw std::runtime_error("Unknown file format"); } catch(std::exception& e) { std::stringstream msg; msg << "Couldn't read '" << filename << "': " << e.what(); throw std::runtime_error(msg.str()); } }
bool FileReadObject::seek(long finalPos, bool relativeMovement) { if (relativeMovement) finalPos += PHYSFS_tell(mFileHandle); return PHYSFS_seek(mFileHandle, finalPos); }
sf::Int64 VFile::seek(sf::Int64 position) { auto ipos = static_cast<PHYSFS_uint64>(position); if (!m_f || !check(PHYSFS_seek(m_f, ipos), 1, "seek")) return -1; return tell(); }
int fs_seek(fs_file fh, long offset, int whence) { PHYSFS_uint64 pos = 0; PHYSFS_sint64 cur = PHYSFS_tell(fh->handle); PHYSFS_sint64 len = PHYSFS_fileLength(fh->handle); switch (whence) { case SEEK_SET: pos = offset; break; case SEEK_CUR: if (cur < 0) return -1; pos = cur + offset; break; case SEEK_END: if (len < 0) return -1; pos = len + offset; break; } return PHYSFS_seek(fh->handle, pos); }
sf::Int64 PhysFSInputStream::seek(sf::Int64 position) { if ( File ) { if ( PHYSFS_seek(File, (PHYSFS_uint64)position) == 0 ) return -1; } return position; }
bool vsFile::PeekLine( vsString *line ) { PHYSFS_sint64 filePos = PHYSFS_tell(m_file); bool result = ReadLine(line); PHYSFS_seek(m_file, filePos); return result; }
bool physfsFile::prepareRead() { PHYSFS_uint64 pos = PHYSFS_tell(fhandle); PHYSFS_close(fhandle); fhandle = PHYSFS_openRead(pname); PHYSFS_seek(fhandle, pos); //LOG_MSG("Goto read (%s at %i)",pname,PHYSFS_tell(fhandle)); return true; }
bool physfsFile::prepareWrite() { const char *wdir = PHYSFS_getWriteDir(); if (wdir == NULL) { LOG_MSG("PHYSFS could not fulfill write request: no write directory set."); return false; } //LOG_MSG("Goto write (%s at %i)",pname,PHYSFS_tell(fhandle)); const char *fdir = PHYSFS_getRealDir(pname); PHYSFS_uint64 pos = PHYSFS_tell(fhandle); char *slash = strrchr(pname,'/'); if (slash && slash != pname) { *slash = 0; PHYSFS_mkdir(pname); *slash = '/'; } if (strcmp(fdir,wdir)) { /* we need COW */ //LOG_MSG("COW",pname,PHYSFS_tell(fhandle)); PHYSFS_file *whandle = PHYSFS_openWrite(pname); if (whandle == NULL) { LOG_MSG("PHYSFS copy-on-write failed: %s.",PHYSFS_getLastError()); return false; } char buffer[65536]; PHYSFS_sint64 size; PHYSFS_seek(fhandle, 0); while ((size = PHYSFS_read(fhandle,buffer,1,65536)) > 0) { if (PHYSFS_write(whandle,buffer,1,(PHYSFS_uint32)size) != size) { LOG_MSG("PHYSFS copy-on-write failed: %s.",PHYSFS_getLastError()); PHYSFS_close(whandle); return false; } } PHYSFS_seek(whandle, pos); PHYSFS_close(fhandle); fhandle = whandle; } else { // megayuck - physfs on posix platforms uses O_APPEND. We illegally access the fd directly and clear that flag. //LOG_MSG("noCOW",pname,PHYSFS_tell(fhandle)); PHYSFS_close(fhandle); fhandle = PHYSFS_openAppend(pname); #ifndef WIN32 fcntl(**(int**)fhandle->opaque,F_SETFL,0); #endif PHYSFS_seek(fhandle, pos); } return true; }
sf::Int64 sf::PhysFSStream::seek(sf::Int64 position) { if (PHYSFS_seek(file, position) == 0) { return -1; } return position; }
void File::seek(uint32_t target) { check_file_open(); if(!PHYSFS_seek( reinterpret_cast<PHYSFS_file*>(mHandle), target)) { BOOST_THROW_EXCEPTION( PhysfsFileException(mFileName) ); } }
static int wz_oggVorbis_seek(void *datasource, ogg_int64_t offset, int whence) { PHYSFS_file* fileHandle; BOOL allowSeeking; int newPos; ASSERT(datasource != NULL, "NULL decoder passed!"); fileHandle = ((struct OggVorbisDecoderState*)datasource)->fileHandle; ASSERT(fileHandle != NULL, "Bad PhysicsFS file handle passed in"); allowSeeking = ((struct OggVorbisDecoderState*)datasource)->allowSeeking; if (!allowSeeking) return -1; switch (whence) { // Seek to absolute position case SEEK_SET: newPos = offset; break; // Seek `offset` ahead case SEEK_CUR: { int curPos = PHYSFS_tell(fileHandle); if (curPos == -1) return -1; newPos = curPos + offset; break; } // Seek backwards from the end of the file case SEEK_END: { int fileSize = PHYSFS_fileLength(fileHandle); if (fileSize == -1) return -1; newPos = fileSize - 1 - offset; break; } // unrecognized seek instruction default: // indicate failure return -1; } // PHYSFS_seek return value of non-zero means success if (PHYSFS_seek(fileHandle, newPos) != 0) return newPos; // success else return -1; // failure }
static void PhysFSSkip( void* user, int offset ) { PHYSFS_File* file = (PHYSFS_File*)user; const int currentPosition = PHYSFS_tell(file); assert(currentPosition >= 0); const int newPosition = currentPosition + offset; const int success = PHYSFS_seek(file, newPosition); assert(success); }
bool File::seek(int pos) { if(file == 0) return false; if(!PHYSFS_seek(file, (PHYSFS_uint64)pos)) return false; return true; }
pos_type seekpos(pos_type pos, std::ios_base::openmode mode) { PHYSFS_seek(file, pos); if (mode & std::ios_base::in) { setg(egptr(), egptr(), egptr()); } if (mode & std::ios_base::out) { setp(buffer, buffer); } return PHYSFS_tell(file); }
IFileStreambuf::pos_type IFileStreambuf::seekpos(pos_type pos, std::ios_base::openmode) { if(PHYSFS_seek(file, static_cast<PHYSFS_uint64> (pos)) == 0) { return pos_type(off_type(-1)); } // the seek invalidated the buffer setg(buf, buf, buf); return pos; }
void FileStream::seek(uint32 pos) { if(!m_caching) { if(!PHYSFS_seek(m_fileHandle, pos)) throwError("seek failed", true); } else { if(pos > m_data.size()) throwError("seek failed"); m_pos = pos; } }
bool physfsFile::Seek(Bit32u * pos,Bit32u type) { PHYSFS_sint64 mypos = (Bit32s)*pos; switch (type) { case DOS_SEEK_SET:break; case DOS_SEEK_CUR:mypos += PHYSFS_tell(fhandle); break; case DOS_SEEK_END:mypos += PHYSFS_fileLength(fhandle); break; default: //TODO Give some doserrorcode; return false;//ERROR } if (!PHYSFS_seek(fhandle,mypos)) { // Out of file range, pretend everythings ok // and move file pointer top end of file... ?! (Black Thorne) PHYSFS_seek(fhandle,PHYSFS_fileLength(fhandle)); }; //LOG_MSG("Seek to %i (%i at %x) of %s (%s)",(int)mypos,(int)*pos,type,name,PHYSFS_getLastError()); *pos=(Bit32u)PHYSFS_tell(fhandle); return true; }
pos_type seekoff(off_type pos, ios_base::seekdir dir, ios_base::openmode mode) { switch (dir) { case std::ios_base::beg: PHYSFS_seek(file, pos); break; case std::ios_base::cur: // subtract characters currently in buffer from seek position PHYSFS_seek(file, (PHYSFS_tell(file) + pos) - (egptr() - gptr())); break; case std::ios_base::end: PHYSFS_seek(file, PHYSFS_fileLength(file) + pos); break; } if (mode & std::ios_base::in) { setg(egptr(), egptr(), egptr()); } if (mode & std::ios_base::out) { setp(buffer, buffer); } return PHYSFS_tell(file); }
// All scripts, binary or otherwise are now passed through this routine static bool dataScriptLoad(const char* fileName, void **ppData) { static const bool printHack = false; SCRIPT_CODE** psProg = (SCRIPT_CODE**)ppData; PHYSFS_file* fileHandle; uint8_t *pBuffer; PHYSFS_sint64 fileSize = 0; debug(LOG_WZ, "COMPILING SCRIPT ...%s", GetLastResourceFilename()); fileHandle = PHYSFS_openRead(fileName); debug(LOG_WZ, "Reading...[directory: %s] %s", PHYSFS_getRealDir(fileName), fileName); if (fileHandle == NULL) { return false; } // due to the changes in r2531 we must do this routine a bit different. fileSize = PHYSFS_fileLength(fileHandle); pBuffer = malloc(fileSize * sizeof(char)); if (pBuffer == NULL) { debug(LOG_FATAL, "Fatal memory allocation, couldn't allocate %lld buffer", fileSize); abort(); } PHYSFS_read(fileHandle, pBuffer, 1, fileSize); calcDataHash(pBuffer, fileSize, DATA_SCRIPT); free(pBuffer); PHYSFS_seek(fileHandle, 0); //reset position *psProg = scriptCompile(fileHandle, SCRIPTTYPE); PHYSFS_close(fileHandle); if (!*psProg) // see script.h { debug(LOG_ERROR, "Script %s did not compile", GetLastResourceFilename()); return false; } if (printHack) { cpPrintProgram(*psProg); } return true; }
// Load a script variable values file static bool dataScriptLoadVals(const char* fileName, void **ppData) { bool success; PHYSFS_file* fileHandle; uint8_t *pBuffer; PHYSFS_sint64 fileSize = 0; *ppData = NULL; // don't load anything if a saved game is being loaded if (saveFlag) { return true; } debug(LOG_WZ, "Loading script data %s", GetLastResourceFilename()); fileHandle = PHYSFS_openRead(fileName); debug(LOG_WZ, "Reading...[directory: %s] %s", PHYSFS_getRealDir(fileName), fileName); if (fileHandle == NULL) { return false; } // due to the changes in r2532 we must do this routine a bit different. fileSize = PHYSFS_fileLength(fileHandle); pBuffer = malloc(fileSize * sizeof(char)); if (pBuffer == NULL) { debug(LOG_FATAL, "Fatal memory allocation, couldn't allocate %lld buffer", fileSize); abort(); } PHYSFS_read(fileHandle, pBuffer, 1, fileSize); calcDataHash(pBuffer, fileSize, DATA_SCRIPTVAL); free(pBuffer); PHYSFS_seek(fileHandle, 0); //reset position success = scrvLoad(fileHandle); if (!success) debug(LOG_FATAL, "Script %s did not compile", GetLastResourceFilename()); PHYSFS_close(fileHandle); return success; }
static Sint64 physfsrwops_seek(SDL_RWops *rw, Sint64 offset, int whence) { PHYSFS_file *handle = (PHYSFS_file *) rw->hidden.unknown.data1; int pos = 0; if (whence == SEEK_SET) { pos = offset; } else if (whence == SEEK_CUR) { int current = PHYSFS_tell(handle); if (current == -1) { SDL_SetError("Can't find position in file: %s", PHYSFS_getLastError()); return(-1); } if (offset == 0) // this is a "tell" call. We're done. return( current ); pos = current + offset; } else if (whence == SEEK_END) { int len = PHYSFS_fileLength(handle); if (len == -1) { SDL_SetError("Can't find end of file: %s", PHYSFS_getLastError()); return(-1); } pos = len + offset; } else { SDL_SetError("Invalid 'whence' parameter."); return(-1); } if (!PHYSFS_seek(handle, pos)) { SDL_SetError("PhysicsFS error: %s", PHYSFS_getLastError()); return(-1); } return(pos); }
// Load a script variable values file static bool dataScriptLoadVals(const char *fileName, void **ppData) { bool success; PHYSFS_file *fileHandle; uint8_t *pBuffer; PHYSFS_sint64 fileSize = 0; *ppData = nullptr; // don't load anything if a saved game is being loaded if (saveFlag) { return true; } debug(LOG_WZ, "Loading script data %s", GetLastResourceFilename()); fileHandle = PHYSFS_openRead(fileName); debug(LOG_WZ, "Reading...[directory: %s] %s", PHYSFS_getRealDir(fileName), fileName); if (fileHandle == nullptr) { return false; } // due to the changes in r2532 we must do this routine a bit different. fileSize = PHYSFS_fileLength(fileHandle); pBuffer = (uint8_t *)malloc(fileSize * sizeof(uint8_t)); ASSERT_OR_RETURN(false, pBuffer, "Out of memory"); WZ_PHYSFS_readBytes(fileHandle, pBuffer, fileSize); calcDataHash(pBuffer, fileSize, DATA_SCRIPTVAL); free(pBuffer); PHYSFS_seek(fileHandle, 0); //reset position success = scrvLoad(fileHandle); if (!success) { debug(LOG_FATAL, "Script %s did not compile", GetLastResourceFilename()); } PHYSFS_close(fileHandle); return success; }