bool ResourceManager::copyFile(const std::string &src, const std::string &dst) { PHYSFS_file *srcFile = PHYSFS_openRead(src.c_str()); if (!srcFile) { logger->log("Read error: %s", PHYSFS_getLastError()); return false; } PHYSFS_file *dstFile = PHYSFS_openWrite(dst.c_str()); if (!dstFile) { logger->log("Write error: %s", PHYSFS_getLastError()); PHYSFS_close(srcFile); return false; } int fileSize = PHYSFS_fileLength(srcFile); void *buf = malloc(fileSize); PHYSFS_read(srcFile, buf, 1, fileSize); PHYSFS_write(dstFile, buf, 1, fileSize); PHYSFS_close(srcFile); PHYSFS_close(dstFile); free(buf); return true; }
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 int cmd_cat(char *args) { PHYSFS_File *f; if (*args == '\"') { args++; args[strlen(args) - 1] = '\0'; } /* if */ f = PHYSFS_openRead(args); if (f == NULL) printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError()); else { if (do_buffer_size) { if (!PHYSFS_setBuffer(f, do_buffer_size)) { printf("failed to set file buffer. Reason: [%s].\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ } /* if */ while (1) { char buffer[128]; PHYSFS_sint64 rc; PHYSFS_sint64 i; rc = PHYSFS_read(f, buffer, 1, sizeof (buffer)); for (i = 0; i < rc; i++) fputc((int) buffer[i], stdout); if (rc < (int)sizeof (buffer)) { printf("\n\n"); if (!PHYSFS_eof(f)) { printf("\n (Error condition in reading. Reason: [%s])\n\n", PHYSFS_getLastError()); } /* if */ PHYSFS_close(f); return(1); } /* if */ } /* while */ } /* else */ return(1); } /* cmd_cat */
void LoadInputs() { Sint64 size; char *buffer; char buf[512]; char input[16]; int s; PHYSFS_File *PSfile; FILE *file; PSfile = PHYSFS_openRead("system/controls.cfg"); if(PSfile == NULL) { fprintf(stderr,"Unable to open control configuration file!\n"); return; } size = PHYSFS_fileLength(PSfile); buffer = (char *)malloc(size); if(buffer == NULL) { fprintf(stderr,"Unable to allocate space for control configuration file\n"); PHYSFS_close(PSfile); return; } NumInputs = 0; PHYSFS_read(PSfile, buffer, size, 1); file = fmemopen (buffer, size, "r"); while(fscanf(file, "%s", buf) != EOF) { if(strcmp(buf,"#") ==0) { fgets(buf, sizeof(buf), file); continue;/*ignore the rest of the line.*/ } if(strcmp(buf,"<input>") ==0) { fscanf(file, "%s %s %s",InputList[NumInputs].name,InputList[NumInputs].type,input); s = ParseInput(InputList[NumInputs].type,input); if(s != -1) { InputList[NumInputs].id = s; NumInputs++; } continue; } } fclose(file); PHYSFS_close(PSfile); free(buffer); }
// ----------------------------------------------------------------------------------- // Imagine if C had a function to copy a file... int copy_file(char *old_file, char *new_file) { sbyte *buf = NULL; int buf_size; PHYSFS_file *in_file, *out_file; out_file = PHYSFS_openWrite(new_file); if (out_file == NULL) return -1; in_file = PHYSFS_openRead(old_file); if (in_file == NULL) return -2; buf_size = (int)PHYSFS_fileLength(in_file); while (buf_size && !(buf = d_malloc(buf_size))) buf_size /= 2; if (buf_size == 0) return -5; // likely to be an empty file while (!PHYSFS_eof(in_file)) { int bytes_read; bytes_read = (int)PHYSFS_read(in_file, buf, 1, buf_size); if (bytes_read < 0) Error("Cannot read from file <%s>: %s", old_file, PHYSFS_getLastError()); Assert(bytes_read == buf_size || PHYSFS_eof(in_file)); if (PHYSFS_write(out_file, buf, 1, bytes_read) < bytes_read) Error("Cannot write to file <%s>: %s", new_file, PHYSFS_getLastError()); } d_free(buf); if (!PHYSFS_close(in_file)) { PHYSFS_close(out_file); return -3; } if (!PHYSFS_close(out_file)) return -4; return 0; }
static void writeSurfaces( const char *filename, std::vector< RenderSurface > &surfaces ) { uint64_t hashedName = HashedString( 0, filename ); char outfile[256]; PHYSFS_mkdir( "static_models/" ); sprintf( outfile, "static_models/%08x_%08x.sm", (uint32_t)(hashedName>>32), (uint32_t)(hashedName&0xffffffff) ); PHYSFS_File *model = PHYSFS_openWrite( outfile ); if ( model ) { PHYSFS_writeULE32( model, 0x090 ); PHYSFS_writeULE32( model, surfaces.size() ); for ( uint32_t i=0; i<surfaces.size(); i++ ) { RenderSurface const &surf = surfaces[i]; PHYSFS_writeCStr( model, surf.name.c_str() ); PHYSFS_writeCStr( model, surf.mat->Name() ); const ModelGeometry *geom = surf.geom; PHYSFS_writeSLE32( model, geom->m_numIndices ); PHYSFS_writeSLE32( model, geom->m_numVerts ); PHYSFS_write( model, geom->m_indices, sizeof(uint16_t)*geom->m_numIndices, 1 ); PHYSFS_write( model, geom->m_verts, sizeof(geom->m_verts[0])*geom->m_numVerts, 1 ); } PHYSFS_close( model ); } }
bool NETstopLogging(void) { static const char dash_line[] = "-----------------------------------------------------------\n"; char buf[256]; int i; UDWORD totalBytessent = 0, totalBytesrecv = 0, totalPacketsent = 0, totalPacketrecv = 0; if (!pFileHandle) { return false; } /* Output stats */ for (i = 0; i < NUM_GAME_PACKETS; i++) { snprintf(buf, sizeof(buf), "%-24s:\t received %u times, %u bytes; sent %u times, %u bytes\n", messageTypeToString(i), packetcount[1][i], packetsize[1][i], packetcount[0][i], packetsize[0][i]); PHYSFS_write(pFileHandle, buf, strlen(buf), 1); totalBytessent += packetsize[0][i]; totalBytesrecv += packetsize[1][i]; totalPacketsent += packetcount[0][i]; totalPacketrecv += packetcount[1][i]; } snprintf(buf, sizeof(buf), "== Total bytes sent %u, Total bytes received %u ==\n", totalBytessent, totalBytesrecv); PHYSFS_write(pFileHandle, buf, strlen(buf), 1); snprintf(buf, sizeof(buf), "== Total packets sent %u, recv %u ==\n", totalPacketsent, totalPacketrecv); PHYSFS_write(pFileHandle, buf, strlen(buf), 1); snprintf(buf, sizeof(buf), "\n-Sync statistics -\n"); PHYSFS_write(pFileHandle, buf, strlen(buf), 1); PHYSFS_write(pFileHandle, dash_line, strlen(dash_line), 1); snprintf(buf, sizeof(buf), "joins: %hhu, kicks: %hhu, drops: %hhu, left %hhu\n", sync_counter.joins, sync_counter.kicks, sync_counter.drops, sync_counter.left ); PHYSFS_write(pFileHandle, buf, strlen(buf), 1); snprintf(buf, sizeof(buf), "banned: %hhu, cantjoin: %hhu, rejected: %hhu\n", sync_counter.banned, sync_counter.cantjoin, sync_counter.rejected ); PHYSFS_write(pFileHandle, buf, strlen(buf), 1); if (sync_counter.banned && IPlist) { snprintf(buf, sizeof(buf), "Banned list:\n"); PHYSFS_write(pFileHandle, buf, strlen(buf), 1); for (i = 0; i < MAX_BANS; i++) { if (IPlist[i].IPAddress[0] != '\0') { snprintf(buf, sizeof(buf), "player %s, IP: %s\n", IPlist[i].pname, IPlist[i].IPAddress); PHYSFS_write(pFileHandle, buf, strlen(buf), 1); } } } PHYSFS_write(pFileHandle, dash_line, strlen(dash_line), 1); PHYSFS_write(pFileHandle, dash_line, strlen(dash_line), 1); if (!PHYSFS_close(pFileHandle)) { debug(LOG_ERROR, "Could not close net log: %s", PHYSFS_getLastError()); return false; } pFileHandle = NULL; return true; }
void piggy_close_file() { if ( Piggy_fp ) { PHYSFS_close( Piggy_fp ); Piggy_fp = NULL; } }
bool physfsDrive::FileOpen(DOS_File * * file,const char * name,Bit32u flags) { char newname[CROSS_LEN]; strcpy(newname,basedir); strcat(newname,name); CROSS_FILENAME(newname); dirCache.ExpandName(newname); normalize(newname,basedir); PHYSFS_file * hand; if (!PHYSFS_exists(newname)) return false; if ((flags&0xf) == OPEN_READ) { hand = PHYSFS_openRead(newname); } else { /* open for reading, deal with writing later */ hand = PHYSFS_openRead(newname); } if (!hand) { if((flags&0xf) != OPEN_READ) { PHYSFS_file *hmm = PHYSFS_openRead(newname); if (hmm) { PHYSFS_close(hmm); LOG_MSG("Warning: file %s exists and failed to open in write mode.\nPlease mount a write directory (see docs).",newname); } } return false; } *file=new physfsFile(name,hand,0x202,newname,false); (*file)->flags=flags; //for the inheritance flag and maybe check for others. return true; }
/* Load an audio file */ static bool dataAudioCfgLoad(const char* fileName, void **ppData) { bool success; PHYSFS_file* fileHandle; *ppData = NULL; if (audio_Disabled()) { return true; } debug(LOG_WZ, "Reading...[directory: %s] %s", PHYSFS_getRealDir(fileName), fileName); fileHandle = PHYSFS_openRead(fileName); if (fileHandle == NULL) { return false; } success = ParseResourceFile(fileHandle); PHYSFS_close(fileHandle); return success; }
sf::PhysFSStream::~PhysFSStream() { if (file != NULL) { PHYSFS_close(file); } }
void Technique::Load( const char *filename ) { char buffer[512]; PHYSFS_File *f = PHYSFS_openRead( fs::MakeCanonicalForm( buffer, filename ) ); if ( f ) { char line[512]; while ( PHYSFS_gets( line, sizeof(line), f ) ) { char str[256]; if ( sscanf( line, "pass %s", str ) == 1 ) { TechniquePass pass; if ( ParsePass( pass, f, str ) ) { passes.push_back( pass ); } } } PHYSFS_close(f); } else { TechniquePass pass; pass.pass = RP_Opaque; pass.shader = shaderManager()->Load( "_default" ); pass.blendSrc = hw::BLEND_ONE; pass.blendDst = hw::BLEND_ZERO; passes.push_back( pass ); } }
void SetWindowIcon(photon_window &window, const std::string &filename){ if(PHYSFS_exists(filename.c_str())){ auto fp = PHYSFS_openRead(filename.c_str()); intmax_t length = PHYSFS_fileLength(fp); if(length > 0){ uint8_t *buffer = new uint8_t[length]; PHYSFS_read(fp, buffer, 1, length); PHYSFS_close(fp); SDL_RWops *rw = SDL_RWFromMem(buffer, length); SDL_Surface *icon = IMG_Load_RW(rw, 1); if(icon == nullptr){ PrintToLog("ERROR: icon loading failed! %s", IMG_GetError()); } SDL_SetWindowIcon(window.window_SDL, icon); SDL_FreeSurface(icon); delete[] buffer; }else{ PrintToLog("ERROR: Unable to open image file \"%s\"!"); } }else{ PrintToLog("ERROR: Image file \"%s\" does not exist!", filename.c_str()); } }
void AppendIniArgs(void) { PHYSFS_file *f; char *line, *token; char separator[] = " "; f = PHYSFSX_openReadBuffered(INI_FILENAME); if(f) { while(!PHYSFS_eof(f) && Num_args < MAX_ARGS) { line=fgets_unlimited(f); token = strtok(line, separator); /* first token in current line */ if (token) Args[Num_args++] = d_strdup(token); while( token != NULL ) { token = strtok(NULL, separator); /* next tokens in current line */ if (token) Args[Num_args++] = d_strdup(token); } d_free(line); } PHYSFS_close(f); } }
static bool readSurfaces( const char *filename, std::vector< RenderSurface > &surfaces ) { uint64_t hashedName = HashedString( 0, filename ); char outfile[256]; PHYSFS_mkdir( "static_models/" ); sprintf( outfile, "static_models/%08x_%08x.sm", (uint32_t)(hashedName>>32), (uint32_t)(hashedName&0xffffffff) ); PHYSFS_File *model = PHYSFS_openRead( outfile ); if ( model ) { unsigned int ver, numSurf; PHYSFS_readULE32( model, &ver ); PHYSFS_readULE32( model, &numSurf ); surfaces.resize( numSurf ); for ( uint32_t i=0; i<surfaces.size(); i++ ) { RenderSurface &surf = surfaces[i]; PHYSFS_readCStr( model, surf.name ); std::string mat; PHYSFS_readCStr( model, mat ); surf.mat = materialManager()->Load( mat.c_str() ); ModelGeometry *geom = new ModelGeometry; surf.geom = geom; PHYSFS_readSLE32( model, &geom->m_numIndices ); PHYSFS_readSLE32( model, &geom->m_numVerts ); geom->m_indices = new unsigned short[geom->m_numIndices]; geom->m_verts = new ModelVert[geom->m_numVerts]; PHYSFS_read( model, geom->m_indices, sizeof(uint16_t)*geom->m_numIndices, 1 ); PHYSFS_read( model, geom->m_verts, sizeof(geom->m_verts[0])*geom->m_numVerts, 1 ); } PHYSFS_close( model ); return true; } return false; }
void medkey_init() { PHYSFS_file * keyfile; char keypress[100]; char line_buffer[200]; int key; int i; //, size; int np; char * LispCommand; MALLOC( LispCommand, char, DIAGNOSTIC_MESSAGE_MAX ); for (i=0; i<2048; i++ ) KeyFunction[i] = NULL; keyfile = PHYSFSX_openReadBuffered( "GLOBAL.KEY" ); if (keyfile) { while (PHYSFSX_fgets(line_buffer, 200, keyfile)) { sscanf(line_buffer, " %s %s ", keypress, LispCommand); //ReadLispMacro( keyfile, LispCommand ); if ( (key=DecodeKeyText( keypress ))!= -1 ) { Assert( key < 2048); KeyFunction[key] = func_get( LispCommand, &np ); } else { Error( "Bad key %s in GLOBAL.KEY!", keypress ); } } PHYSFS_close(keyfile); } d_free( LispCommand ); }
// Read a whole file and put it in a new buffer unsigned int j1FileSystem::Load(const char* file, char** buffer) const { unsigned int ret = 0; PHYSFS_file* fs_file = PHYSFS_openRead(file); if(fs_file != NULL) { PHYSFS_sint32 size = PHYSFS_fileLength(fs_file); if(size > 0) { *buffer = new char[size]; int readed = PHYSFS_read(fs_file, *buffer, 1, size); if(readed != size) { LOG("File System error while reading from file %s: %s\n", file, PHYSFS_getLastError()); RELEASE(buffer); } else ret = readed; } if(PHYSFS_close(fs_file) == 0) LOG("File System error while closing file %s: %s\n", file, PHYSFS_getLastError()); } else LOG("File System error while opening file %s: %s\n", file, PHYSFS_getLastError()); return ret; }
char *ResourceManager::loadFile(const std::string &fileName, int &fileSize) { // Attempt to open the specified file using PhysicsFS PHYSFS_file *file = PHYSFS_openRead(fileName.c_str()); // If the handler is an invalid pointer indicate failure if (file == nullptr) { LOG_WARN("Failed to load '" << fileName << "': " << PHYSFS_getLastError()); return nullptr; } // Get the size of the file fileSize = PHYSFS_fileLength(file); // Allocate memory and load the file char *buffer = (char *) malloc(fileSize + 1); if (PHYSFS_read(file, buffer, 1, fileSize) != fileSize) { free(buffer); LOG_WARN("Failed to load '" << fileName << "': " << PHYSFS_getLastError()); return nullptr; } // Close the file and let the user deallocate the memory PHYSFS_close(file); // Add a trailing null character, so that the file can be used as a string buffer[fileSize] = 0; return buffer; }
static int cmd_filelength(char *args) { PHYSFS_File *f; if (*args == '\"') { args++; args[strlen(args) - 1] = '\0'; } /* if */ f = PHYSFS_openRead(args); if (f == NULL) printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError()); else { PHYSFS_sint64 len = PHYSFS_fileLength(f); if (len == -1) printf("failed to determine length. Reason: [%s].\n", PHYSFS_getLastError()); else printf(" (cast to int) %d bytes.\n", (int) len); PHYSFS_close(f); } /* else */ return(1); } /* cmd_filelength */
bool File::close() { if(!PHYSFS_close(file)) return false; file = 0; return true; }
void* ResourceManager::loadFile(const std::string &fileName, int &fileSize) { // Attempt to open the specified file using PhysicsFS PHYSFS_file *file = PHYSFS_openRead(fileName.c_str()); // If the handler is an invalid pointer indicate failure if (file == NULL) { logger->log("Warning: Failed to load %s: %s", fileName.c_str(), PHYSFS_getLastError()); return NULL; } // Log the real dir of the file logger->log("Loaded %s/%s", PHYSFS_getRealDir(fileName.c_str()), fileName.c_str()); // Get the size of the file fileSize = PHYSFS_fileLength(file); // Allocate memory and load the file void *buffer = malloc(fileSize); PHYSFS_read(file, buffer, 1, fileSize); // Close the file and let the user deallocate the memory PHYSFS_close(file); return buffer; }
int FS_ReadFile(const char *path, void **buffer) { auto f = PHYSFS_openRead(path); if (f == nullptr) { return -1; } auto sz = PHYSFS_fileLength(f); if (buffer == nullptr) { return (int)sz; } *buffer = malloc((size_t)sz+1); memset(*buffer, 0, (size_t)sz + 1); auto read_sz = PHYSFS_read(f, *buffer, (PHYSFS_uint32)1, (PHYSFS_uint32)sz); if (read_sz == -1) { auto lastErr = PHYSFS_getLastError(); Con_Printf("FS err: %s", lastErr); } PHYSFS_close(f); return (int)read_sz; }
int FS_loadFile(lua_State *L, const char *filename) { char *buffer; /* buffer for the file */ char *entryPoint; /* entry point of file (differs from buffer, if "#!" in the first line is skipped */ int err; PHYSFS_file *Hndfile = NULL; PHYSFS_sint64 fileLength, size; if (PHYSFS_exists(filename) == 0) { lua_pushfstring(L, "Error loading '%s': file not found.", filename); return FILEIO_ERROR; } Hndfile = PHYSFS_openRead(filename); /* open file to read! */ if (Hndfile == NULL) { lua_pushfstring(L, "Error while reading from '%s': %s", filename, PHYSFS_getLastError()); return FILEIO_ERROR; } size = PHYSFS_fileLength(Hndfile); if (size == -1) { lua_pushfstring(L, "Error while determining the size of %s.", filename); return FILEIO_ERROR; } buffer = (char *)malloc((unsigned int)size); if (buffer == NULL) { lua_pushfstring(L, "Error loading %s: Insufficient memory available.", filename); return FILEIO_ERROR; } fileLength = PHYSFS_read(Hndfile, buffer, 1, (unsigned int)size); if (fileLength < size) { free(buffer); lua_pushfstring(L, "Error while reading from %s: %s", filename, PHYSFS_getLastError()); return FILEIO_ERROR; } /* close the file */ err = PHYSFS_close(Hndfile); if (err == 0) { free(buffer); lua_pushfstring(L, "Error closing %s: %s", filename, PHYSFS_getLastError()); return FILEIO_ERROR; } /* skip #! if nescessary */ entryPoint = buffer; if (buffer[0] == '#') { while ((*entryPoint != 0x0D) && (*entryPoint != 0x0A) && (*entryPoint != EOF)) { entryPoint++; fileLength--; } } err = luaL_loadbuffer(L, entryPoint, (size_t)fileLength, filename); free(buffer); if (err != 0) { /* error message is on the stack */ return FILEIO_ERROR; } return FILEIO_SUCCESS; }
// write values from netgame_info to ngp file void write_netgame_profile(netgame_info *ng) { char filename[PATH_MAX]; PHYSFS_file *file; memset(filename, '\0', PATH_MAX); snprintf(filename, PATH_MAX, GameArg.SysUsePlayersDir? "Players/%.8s.ngp" : "%.8s.ngp", Players[Player_num].callsign); file = PHYSFSX_openWriteBuffered(filename); if (!file) return; PHYSFSX_printf(file, "game_name=%s\n", ng->game_name); PHYSFSX_printf(file, "gamemode=%i\n", ng->gamemode); PHYSFSX_printf(file, "RefusePlayers=%i\n", ng->RefusePlayers); PHYSFSX_printf(file, "difficulty=%i\n", ng->difficulty); PHYSFSX_printf(file, "game_flags=%i\n", ng->game_flags); PHYSFSX_printf(file, "AllowedItems=%i\n", ng->AllowedItems); PHYSFSX_printf(file, "ShowEnemyNames=%i\n", ng->ShowEnemyNames); PHYSFSX_printf(file, "BrightPlayers=%i\n", ng->BrightPlayers); PHYSFSX_printf(file, "InvulAppear=%i\n", ng->InvulAppear); PHYSFSX_printf(file, "KillGoal=%i\n", ng->KillGoal); PHYSFSX_printf(file, "PlayTimeAllowed=%i\n", ng->PlayTimeAllowed); PHYSFSX_printf(file, "control_invul_time=%i\n", ng->control_invul_time); PHYSFSX_printf(file, "PacketsPerSec=%i\n", ng->PacketsPerSec); PHYSFSX_printf(file, "NoFriendlyFire=%i\n", ng->NoFriendlyFire); #ifdef USE_TRACKER PHYSFSX_printf(file, "Tracker=%i\n", ng->Tracker); #else PHYSFSX_printf(file, "Tracker=0\n"); #endif PHYSFSX_printf(file, "ngp version=%s\n",VERSION); PHYSFS_close(file); }
/* The hackishness level is quite low, but to get perfect, here is my personal wishlist for PHYSFS: - mounting zip files at arbitrary locations (already in CVS, I think) - rename support - a better API for stat() infos - more stdio-like API for seek, open and truncate - perhaps a ramdisk as write dir? */ PHYSFS_sint64 PHYSFS_fileLength(const char *name) { PHYSFS_file *f = PHYSFS_openRead(name); if (f == NULL) return 0; PHYSFS_sint64 size = PHYSFS_fileLength(f); PHYSFS_close(f); return size; }
static void feed_file_http(const char *ipstr, int sock, const char *fname) { PHYSFS_File *in = PHYSFS_openRead(fname); char buffer[1024]; printf("%s: requested [%s].\n", ipstr, fname); if (in == NULL) { printf("%s: Can't open [%s]: %s.\n", ipstr, fname, PHYSFS_getLastError()); write(sock, txt404, strlen(txt404)); /* !!! FIXME: Check retval */ } /* if */ else { do { PHYSFS_sint64 br = PHYSFS_read(in, buffer, 1, sizeof (buffer)); if (br == -1) { printf("%s: Read error: %s.\n", ipstr, PHYSFS_getLastError()); break; } /* if */ write(sock, buffer, (int) br); /* !!! FIXME: CHECK THIS RETVAL! */ } while (!PHYSFS_eof(in)); PHYSFS_close(in); } /* else */ } /* feed_file_http */
static int l_filesystem_require(lua_State* state) { if(!lua_isstring(state, 1)) return luaL_error(state, "The argument must be a string."); const char* filename = lua_tostring(state, 1); char myBuf[2048] = {0}; PHYSFS_file* myfile = PHYSFS_openRead(filename); if(!myfile) printf("%s %s \n", "No .lua file named in directory ", filename); PHYSFS_sint64 fileLngth = PHYSFS_fileLength(myfile); PHYSFS_read (myfile, myBuf, 1, fileLngth); int status = 0; status = luaL_loadbuffer(state, (const char *)myBuf, fileLngth, filename) || lua_pcall(state, 0,0,0); if(status != 0) luaL_error(state,lua_tostring(state,-1)); PHYSFS_close(myfile); switch (status) { case LUA_ERRMEM: return luaL_error(state, "Memory allocation error: %s\n", lua_tostring(state, -1)); case LUA_ERRSYNTAX: return luaL_error(state, "Syntax error: %s\n", lua_tostring(state, -1)); default: // success return 1; } return 1; }
boost::shared_ptr<ResourceInfo> Resource::LoadResource(const std::string& name) { boost::shared_ptr<ResourceInfo> resource_info = m_resources.LoadResource(name); if (resource_info.get() == NULL) { const char* name_cstr = name.c_str(); // Check if resource exists if (PHYSFS_exists(name_cstr) && !PHYSFS_isDirectory(name_cstr)) { // Load the resource PHYSFS_File* fp = PHYSFS_openRead(name_cstr); // Load file data into ResourceInfo class Sint64 length = (Sint64)PHYSFS_fileLength(fp); char* data = new char[length]; PHYSFS_read(fp, (void*)data, (PHYSFS_uint32)length, 1); resource_info = m_resources.Construct(name, data, length); // Close PHYSFS_File PHYSFS_close(fp); fp = NULL; } else { BOOST_THROW_EXCEPTION(ResourceNotFoundException() << StringErrorInfo("The requested resource could not be found: " + name)); } } return resource_info; }
int OggSoundFile::cb_close(void* source) { auto file = reinterpret_cast<PHYSFS_file*> (source); PHYSFS_close(file); return 0; }
/** Plays the given audio file as a stream and reports back when it has finished * playing. * \param fileName the (OggVorbis) file to play from * \param volume the volume to use while playing this file (in the range of * 0.0 - 1.0) * \param onFinished a callback function to invoke when playing of this stream * has been finished. You can use NULL to specifiy no callback function. * \param user_data a pointer to contain some user data to pass along to the * finished callback. * \return a pointer to the currently playing stream when the stream is playing * (and as such the callback will be invoked some time in the future), * NULL when the stream didn't start playing (and the callback won't be * invoked). * \note The returned pointer will become invalid/dangling immediately after * the \c onFinished callback is invoked. * \note You must _never_ manually free() the memory used by the returned * pointer. */ AUDIO_STREAM* audio_PlayStream(const char* fileName, float volume, void (*onFinished)(void*), void* user_data) { PHYSFS_file* fileHandle; AUDIO_STREAM* stream; // If audio is not enabled return false to indicate that the given callback // will not be invoked. if (g_bAudioEnabled == false) { return NULL; } // Open up the file fileHandle = PHYSFS_openRead(fileName); debug(LOG_WZ, "Reading...[directory: %s] %s", PHYSFS_getRealDir(fileName), fileName); if (fileHandle == NULL) { debug(LOG_ERROR, "sound_LoadTrackFromFile: PHYSFS_openRead(\"%s\") failed with error: %s\n", fileName, PHYSFS_getLastError()); return NULL; } stream = sound_PlayStream(fileHandle, volume, onFinished, user_data); if (stream == NULL) { PHYSFS_close(fileHandle); return NULL; } return stream; }