void LoadScriptFile(pzllst *lst,FManNode *filename, bool control, MList *controlst) { #ifdef TRACE printf("Loading script file %s\n",filename->File); #endif if (control) { Rend_SetRenderer(RENDER_FLAT); } mfile *fl=mfopen(filename); if (fl == NULL) { printf("Error opening file %s\n",filename->File); exit(1); return; } char buf[FILE_LN_BUF]; while(!mfeof(fl)) { mfgets(buf,FILE_LN_BUF,fl); char *str=PrepareString(buf); if (strCMP(str,"puzzle")==0) { Parse_Puzzle(lst,fl,str); } else if (strCMP(str,"control")==0 && control ) { Parse_Control(controlst,fl,str); } } mfclose(fl); }
size_t mfread(mfile f, void *buf, size_t len, int64_t deadline) { /* If there's enough data in the buffer it's easy. */ if(f->ilen >= len) { memcpy(buf, &f->ibuf[f->ifirst], len); f->ifirst += len; f->ilen -= len; errno = 0; return len; } /* Let's move all the data from the buffer first. */ char *pos = (char*)buf; size_t remaining = len; memcpy(pos, &f->ibuf[f->ifirst], f->ilen); pos += f->ilen; remaining -= f->ilen; f->ifirst = 0; f->ilen = 0; mill_assert(remaining); while(1) { if(remaining > MILL_FILE_BUFLEN) { /* If we still have a lot to read try to read it in one go directly into the destination buffer. */ ssize_t sz = read(f->fd, pos, remaining); if(!sz) { return len - remaining; } if(sz == -1) { if(errno != EAGAIN && errno != EWOULDBLOCK) return len - remaining; sz = 0; } if((size_t)sz == remaining) { errno = 0; return len; } pos += sz; remaining -= sz; if (sz != 0 && mfeof(f)) { return len - remaining; } } else { /* If we have just a little to read try to read the full connection buffer to minimise the number of system calls. */ ssize_t sz = read(f->fd, f->ibuf, MILL_FILE_BUFLEN); if(!sz) { return len - remaining; } if(sz == -1) { if(errno != EAGAIN && errno != EWOULDBLOCK) return len - remaining; sz = 0; } if((size_t)sz < remaining) { memcpy(pos, f->ibuf, sz); pos += sz; remaining -= sz; f->ifirst = 0; f->ilen = 0; } else { memcpy(pos, f->ibuf, remaining); f->ifirst = remaining; f->ilen = sz - remaining; errno = 0; return len; } if (sz != 0 && mfeof(f)) { return len - remaining; } } /* Wait till there's more data to read. */ int res = fdwait(f->fd, FDW_IN, deadline); if (!res) { errno = ETIMEDOUT; return len - remaining; } } }
int main(void) { MEMFILE *file, *file2, *file3; char test[] = "This is a very long string that just goes on and on, but it can surely tell us a few things about how this code is working."; char test2[] = "This is the second string, and should get cat'd right up behind the first string."; char buffer[512]; printf("\n--------- open should fail (file doesn't exist)\n"); file = mfopen( "dummy", "r+b" ); printf("fopen(\"dummy\",\"r+b\") -> 0x%x\n", file ); printf("#of open memstreams: %d\n", count_open_streams() ); printf("\n--------- open should succeed (truncated on open)\n"); file = mfopen( "dummy", "w+b" ); printf("fopen(\"dummy\",\"w+b\") -> 0x%x\n", file ); printf("#of open memstreams: %d\n", count_open_streams() ); printf("growth increment was %d\n", _mfsetgrowincrement( file, 64 ) ); printf("growth increment is now %d\n", _mfsetgrowincrement( file, 0 ) ); printf("\n--------- should be zero offset, and eof == false\n"); printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) ); printf("\n--------- should write %d bytes\n", strlen(test)); printf( "fwrite( [], %d, %d, file ) -> %d\n", strlen(test), sizeof(char), mfwrite( test, strlen(test), sizeof(char), file ) ); printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) ); printf("\n--------- should read the string back in\n"); printf( "rewind(file) \n" ); mrewind(file); printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) ); memset( buffer, 0, sizeof( buffer )); printf( "fread( [], %d, %d, file ) -> %d\n", sizeof(buffer), sizeof(char), mfread( buffer, sizeof(buffer), sizeof(char), file ) ); printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) ); printf("\n--------- should cat the second string to the first\n"); printf( "fwrite( [], %d, %d, file ) -> %d\n", sizeof(char), strlen(test2), mfwrite( test2, strlen(test2), sizeof(char), file ) ); printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) ); printf("\n--------- should read zero (fp is at eof)\n"); memset( buffer, 0, sizeof( buffer )); printf( "fread( [], %d, %d, file ) -> %d\n", sizeof(buffer), sizeof(char), mfread( buffer, sizeof(buffer), sizeof(char), file ) ); printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) ); printf("\n--------- seek test\n"); printf( "filelength( fileno( file ) )-> %ld\n", mfilelength( mfileno( file ) ) ); printf( "seek(file,64,0)-> %d\n", mfseek( file, 64, SEEK_SET ) ); printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) ); printf("\n--------- read after seek test, should read filelen-64 bytes\n"); memset( buffer, 0, sizeof( buffer )); printf( "fread( [], %d, %d, file ) -> %d\n", sizeof(buffer), sizeof(char), mfread( buffer, sizeof(buffer), sizeof(char), file ) ); printf( "-> \"%s\"\n", buffer ); printf("\n--------- change size test\n"); printf( "filelength( fileno( file ) )-> %ld\n", mfilelength( mfileno( file ) ) ); printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) ); printf( "ftruncate(fileno(file),10) -> %d\n", mftruncate( mfileno( file ), 10 ) ); printf( "filelength( fileno( file ) )-> %ld\n", mfilelength( mfileno( file ) ) ); printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) ); printf("\n--------- negative seek test\n"); printf( "fseek(file,-5,2)->%d\n", mfseek( file, -5, SEEK_END ) ); printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) ); printf("\n--------- read after seek test, should read 5 bytes\n"); memset( buffer, 0, sizeof( buffer )); printf( "fread( [], %d, %d, file ) -> %d\n", sizeof(buffer), sizeof(char), mfread( buffer, sizeof(buffer), sizeof(char), file ) ); printf( "-> \"%s\"\n", buffer ); printf("\n--------- open and write a second file\n"); file2 = mfopen( "dummy2", "w+b" ); printf("fopen(\"dummy2\",\"w+b\") -> 0x%x\n", file2 ); printf( "fwrite( [], %d, %d, file2 ) -> %d\n", strlen("TEST"), sizeof(char), mfwrite( "TEST", strlen("TEST"), sizeof(char), file2 ) ); printf( "filelength( fileno( file2 ) )-> %ld\n", mfilelength( mfileno( file2 ) ) ); printf( "ftell(file2) -> %ld feof(file2)-> %d\n", mftell( file2 ), mfeof( file2 ) ); printf("#of open memstreams: %d\n", count_open_streams() ); printf("\n--------- open and write a third file\n"); file3 = mfopen( "dummy3", "w+b" ); printf("fopen(\"dummy3\",\"w+b\") -> 0x%x\n", file3 ); printf( "fwrite( [], %d, %d, file3 ) -> %d\n", strlen("BLAHBLAH"), sizeof(char), mfwrite( "BLAHBLAH", strlen("BLAHBLAH"), sizeof(char), file3 ) ); printf( "filelength( fileno( file3 ) )-> %ld\n", mfilelength( mfileno( file3 ) ) ); printf( "ftell(file3) -> %ld feof(file3)-> %d\n", mftell( file3 ), mfeof( file3 ) ); printf("#of open memstreams: %d\n", count_open_streams() ); printf("\n--------- close second\n"); printf( "fclose(file2) -> %d\n", mfclose( file2 ) ); printf( "filelength( fileno( file2 ) )-> %ld\n", mfilelength( mfileno( file2 ) ) ); printf( "ftell(file2) -> %ld feof(file2)-> %d\n", mftell( file2 ), mfeof( file2 ) ); printf("#of open memstreams: %d\n", count_open_streams() ); printf("\n--------- check first\n"); printf( "filelength( fileno( file ) )-> %ld\n", mfilelength( mfileno( file ) ) ); printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) ); printf("#of open memstreams: %d\n", count_open_streams() ); printf("\n--------- truncate to zero test\n"); printf( "ftruncate(fileno(file),0) -> %d\n", mftruncate( mfileno( file ), 0 ) ); printf( "filelength( fileno( file ) )-> %ld\n", mfilelength( mfileno( file ) ) ); printf("#of open memstreams: %d\n", count_open_streams() ); printf("\n--------- close first\n"); printf( "fclose(file) -> %d\n", mfclose( file ) ); printf( "filelength( fileno( file ) )-> %ld\n", mfilelength( mfileno( file ) ) ); printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) ); printf("#of open memstreams: %d\n", count_open_streams() ); printf("\n--------- close third\n"); printf( "fclose(file3) -> %d\n", mfclose( file3 ) ); printf( "filelength( fileno( file3 ) )-> %ld\n", mfilelength( mfileno( file3 ) ) ); printf( "ftell(file3) -> %ld feof(file3)-> %d\n", mftell( file3 ), mfeof( file3 ) ); printf("#of open memstreams: %d\n", count_open_streams() ); return 0; }