//Open a file for reading, set up a buffer PHYSFS_file *PHYSFSX_openReadBuffered(const char *filename) { PHYSFS_file *fp; PHYSFS_uint64 bufSize; char filename2[PATH_MAX]; if (filename[0] == '\x01') { //FIXME: don't look in dir, only in hogfile filename++; } snprintf(filename2, sizeof(filename2), "%s", filename); PHYSFSEXT_locateCorrectCase(filename2); fp = PHYSFS_openRead(filename2); if (!fp) return NULL; bufSize = PHYSFS_fileLength(fp); while (!PHYSFS_setBuffer(fp, bufSize) && bufSize) bufSize /= 2; // even if the error isn't memory full, for a 20MB file it'll only do this 8 times return fp; }
fs_file fs_open(const char *path, const char *mode) { fs_file fh; assert((mode[0] == 'r' && !mode[1]) || (mode[0] == 'w' && (!mode[1] || mode[1] == '+'))); if ((fh = malloc(sizeof (*fh)))) { switch (mode[0]) { case 'r': fh->handle = PHYSFS_openRead(path); break; case 'w': fh->handle = (mode[1] == '+' ? PHYSFS_openAppend(path) : PHYSFS_openWrite(path)); break; } if (fh->handle) { PHYSFS_setBuffer(fh->handle, 0x2000); } else { free(fh); fh = NULL; } } return fh; }
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 */
/**Sets the internal buffer for read/write operations. * \return Nonzero on success */ int File::SetBuffer( int bufSize ){ #ifdef USE_PHYSICSFS if ( PHYSFS_setBuffer( fp, bufSize ) == 0 ){ LogMsg(ERR,"Could not create internal buffer for file: %s.\n%s", validName.c_str(),PHYSFS_getLastError()); PHYSFS_close( fp ); return 0; } return 1; #else return 0; // No idea how this works??? ~ Matt Zweig #endif }
bool File::setBuffer(BufferMode bufmode, int64 size) { // No negativity allowed! if (size < 0) return false; // If the file isn't open, we'll make sure the buffer values are set in // File::open. if (file == 0 || mode == CLOSED) { bufferMode = bufmode; bufferSize = size; return true; } int ret = 1; switch (bufmode) { case BUFFER_NONE: default: ret = PHYSFS_setBuffer(file, 0); size = 0; break; case BUFFER_LINE: case BUFFER_FULL: ret = PHYSFS_setBuffer(file, size); break; } if (ret == 0) return false; bufferMode = bufmode; bufferSize = size; return true; }
//Open a file for writing, set up a buffer PHYSFS_file *PHYSFSX_openWriteBuffered(const char *filename) { PHYSFS_file *fp; PHYSFS_uint64 bufSize = 1024*1024; // hmm, seems like an OK size. fp = PHYSFS_openWrite(filename); if (!fp) return NULL; while (!PHYSFS_setBuffer(fp, bufSize) && bufSize) bufSize /= 2; return fp; }
static int cmd_write(char *args) { PHYSFS_File *f; if (*args == '\"') { args++; args[strlen(args) - 1] = '\0'; } /* if */ f = PHYSFS_openWrite(args); if (f == NULL) printf("failed to open. Reason: [%s].\n", PHYSFS_getLastError()); else { size_t bw; PHYSFS_sint64 rc; 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 */ bw = strlen(WRITESTR); rc = PHYSFS_write(f, WRITESTR, 1, bw); if (rc != (int)bw) { printf("Wrote (%d) of (%d) bytes. Reason: [%s].\n", (int) rc, (int) bw, PHYSFS_getLastError()); } /* if */ else { printf("Successful.\n"); } /* else */ PHYSFS_close(f); } /* else */ return(1); } /* cmd_write */
static int cmd_stressbuffer(char *args) { int num; if (*args == '\"') { args++; args[strlen(args) - 1] = '\0'; } /* if */ num = atoi(args); if (num < 0) printf("buffer must be greater than or equal to zero.\n"); else { PHYSFS_File *f; int rndnum; printf("Stress testing with (%d) byte buffer...\n", num); f = PHYSFS_openWrite("test.txt"); if (f == NULL) printf("Couldn't open test.txt for writing: %s.\n", PHYSFS_getLastError()); else { int i, j; char buf[37]; char buf2[37]; if (!PHYSFS_setBuffer(f, num)) { printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError()); PHYSFS_close(f); PHYSFS_delete("test.txt"); return(1); } /* if */ strcpy(buf, "abcdefghijklmnopqrstuvwxyz0123456789"); srand((unsigned int) time(NULL)); for (i = 0; i < 10; i++) { for (j = 0; j < 10000; j++) { PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0)); PHYSFS_uint32 left = 36 - right; if (PHYSFS_write(f, buf, left, 1) != 1) { printf("PHYSFS_write() failed: %s.\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0)); if (rndnum == 42) { if (!PHYSFS_flush(f)) { printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ } /* if */ if (PHYSFS_write(f, buf + left, 1, right) != right) { printf("PHYSFS_write() failed: %s.\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0)); if (rndnum == 42) { if (!PHYSFS_flush(f)) { printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ } /* if */ } /* for */ if (!PHYSFS_flush(f)) { printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ } /* for */ if (!PHYSFS_close(f)) { printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError()); return(1); /* oh well. */ } /* if */ printf(" ... test file written ...\n"); f = PHYSFS_openRead("test.txt"); if (f == NULL) { printf("Failed to reopen stress file for reading: %s.\n", PHYSFS_getLastError()); return(1); } /* if */ if (!PHYSFS_setBuffer(f, num)) { printf("PHYSFS_setBuffer() failed: %s.\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ for (i = 0; i < 10; i++) { for (j = 0; j < 10000; j++) { PHYSFS_uint32 right = 1 + (PHYSFS_uint32) (35.0 * rand() / (RAND_MAX + 1.0)); PHYSFS_uint32 left = 36 - right; if (PHYSFS_read(f, buf2, left, 1) != 1) { printf("PHYSFS_read() failed: %s.\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0)); if (rndnum == 42) { if (!PHYSFS_flush(f)) { printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ } /* if */ if (PHYSFS_read(f, buf2 + left, 1, right) != right) { printf("PHYSFS_read() failed: %s.\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ rndnum = 1 + (int) (1000.0 * rand() / (RAND_MAX + 1.0)); if (rndnum == 42) { if (!PHYSFS_flush(f)) { printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ } /* if */ if (memcmp(buf, buf2, 36) != 0) { printf("readback is mismatched on iterations (%d, %d).\n", i, j); printf("wanted: ["); for (i = 0; i < 36; i++) printf("%c", buf[i]); printf("]\n"); printf(" got: ["); for (i = 0; i < 36; i++) printf("%c", buf2[i]); printf("]\n"); PHYSFS_close(f); return(1); } /* if */ } /* for */ if (!PHYSFS_flush(f)) { printf("PHYSFS_flush() failed: %s.\n", PHYSFS_getLastError()); PHYSFS_close(f); return(1); } /* if */ } /* for */ printf(" ... test file read ...\n"); if (!PHYSFS_eof(f)) printf("PHYSFS_eof() returned true! That's wrong.\n"); if (!PHYSFS_close(f)) { printf("PHYSFS_close() failed: %s.\n", PHYSFS_getLastError()); return(1); /* oh well. */ } /* if */ PHYSFS_delete("test.txt"); printf("stress test completed successfully.\n"); } /* else */ } /* else */ return(1); } /* cmd_stressbuffer */
void File::setBuffer(uint64_t bufsize) { if(!PHYSFS_setBuffer(file, bufsize)) throw Exception("couldn't adjust buffer size: %s", PHYSFS_getLastError()); }