long mfseek(MEMFILE *mf,long offset,int whence) { long length; assert(mf!=NULL); if (mf->usedoffs == 0) return 0L; /* early exit: not a single byte in the file */ /* find the size of the memory file */ length=mflength(mf); /* convert the offset to an absolute position */ switch (whence) { case SEEK_SET: break; case SEEK_CUR: offset+=mf->offs; break; case SEEK_END: assert(offset<=0); offset+=length; break; } /* switch */ /* clamp to the file length limit */ if (offset<0) offset=0; else if (offset>length) offset=length; /* set new position and return it */ memfile_seek(mf, offset); return offset; }
int movie_save(char *filename) { memfile_t *file; u8 flags; if(filename) { if(nes->movie.filename) mem_free(nes->movie.filename); nes->movie.filename = mem_strdup(filename); } else if(nes->movie.filename == 0) { log_printf("movie_save: error! no filename given and movie doesnt have filename set\n"); return(0); } if(nes->movie.len == 0) { log_printf("movie_save: no movie data to save, just setting filename\n"); return(0); } if(nes->movie.mode & MOVIE_RECORD) { log_printf("movie_save: still recording, stopping...\n"); movie_stop(); } if((file = memfile_open(nes->movie.filename,"wb")) == 0) { log_printf("movie_save: error opening movie '%s'\n",filename); return(1); } memfile_seek(nes->movie.state,0,SEEK_SET); flags = (nes->movie.mode & MOVIE_TEST) ? 1 : 0; //header (just ident for now) memfile_write(movie_ident,1,4,file); memfile_write(&flags,1,sizeof(u8),file); //save input device configuration memfile_write(&nes->inputdev[0]->id,1,sizeof(int),file); memfile_write(&nes->inputdev[1]->id,1,sizeof(int),file); memfile_write(&nes->expdev->id,1,sizeof(int),file); //save movie data memfile_write(&nes->movie.startframe,1,sizeof(u32),file); memfile_write(&nes->movie.endframe,1,sizeof(u32),file); memfile_write(&nes->movie.crc32,1,sizeof(u32),file); memfile_write(&nes->movie.len,1,sizeof(u32),file); log_printf("movie_save: start, end = %d, %d :: len = %d bytes\n",nes->movie.startframe,nes->movie.endframe,nes->movie.len); memfile_write(nes->movie.data,1,nes->movie.len,file); //append savestate to end of movie memfile_copy(file,nes->movie.state,memfile_size(nes->movie.state)); memfile_close(file); return(0); }
//setup for playing data int movie_play() { if(nes->movie.state == 0) { log_printf("movie_play: cannot play movie, no data loaded\n"); return(1); } memfile_seek(nes->movie.state,0,SEEK_SET); state_load(nes->movie.state); nes->movie.mode &= ~7; nes->movie.mode |= MOVIE_PLAY; nes->movie.pos = 0; return(0); }
memfile_t *memfile_open(char *filename,char *mode) { FILE *fp; memfile_t *ret = 0; int n; //try to open the file if((fp = fopen(filename,mode)) == 0) { log_printf("memfile_open: error opening '%s'\n",filename); return(0); } //create new file ret = memfile_create(); //copy filename and mode ret->filename = mem_strdup(filename); ret->mode = mem_strdup(mode); //save file handle ret->handle = (void*)fp; //get file size fseek(fp,0,SEEK_END); ret->size = (u32)ftell(fp); fseek(fp,0,SEEK_SET); //allocate file data and read the file into it ret->data = (u8*)mem_alloc(ret->size); if((n = fread(ret->data,1,ret->size,fp)) != ret->size) { log_printf("memfile_open: error reading file, wanted %d bytes but read %d\n",ret->size,n); memfile_close(ret); return(0); } //if append mode, seek to end if(strchr(mode,'a')) { memfile_seek(ret,0,SEEK_END); } //initialize the current position and changed var ret->curpos = 0; ret->changed = 0; return(ret); }
/* pc_resetbin() * Can seek to any location in the file. * The offset is always from the start of the file. */ void pc_resetbin(void *handle,long offset) { memfile_seek((memfile_t *)handle, offset); }
void memfile_rewind(memfile_t *mf) { memfile_seek(mf,0,SEEK_SET); }
int mf_setpos(void *handle, size_t pos) { memfile_seek((memfile_t *)handle, (long)pos); return 1; }