int parseMultiPart(char *boundary) { int bound_len=strlen(boundary), type, startat, finish=false; char *name=NULL, *ctyp=NULL, *fname=NULL; MFILE *mf=mfopen(); while((startat=miscFReadLn(stdin, mf))!=-1) { if(strncmp(boundary, mfGetDataAt(mf, startat), bound_len)) continue; /* new part found - insert old part and init new one... * no insert at FIRST header ... * at end of multipart ??? "--boundary--" line ? */ if(!strncmp("--", mfGetDataAt(mf, mfGetLength(mf)-4), 2)) finish=true; mfSetLength(mf, startat); if(name!=NULL) { /* strip memfile, since one more <cr><lf> or only <lf> at end */ mf->used--; if(*(char*)((int)mf->data+mf->used-1) == '\r') mf->used--; if(type==CgiKindFile) { listAddData(type, name, fname, ctyp, mf); mf=mfopen(); } else listAddData(type, name, mfGetData(mf), ctyp, NULL); } if(finish==true) return(true); type=parseMultiHead(&name, &fname, &ctyp); mfSetLength(mf, 0); } mfclose(mf); free(name); free(fname); free(ctyp); return true; }
/* * Read a sequence from a file "fnin" of format "format". If "format" is 0 * (ANY_FORMAT), we automatically determine the correct format. * Returns: * Read * for success * NULLRead for failure */ Read *read_reading(char *fn, int format) { Read *read; mFILE *fp; #ifdef USE_BIOLIMS if( !strncmp(fn,BIOLIMS_TAG,strlen(BIOLIMS_TAG))){ return spReadBiolimsReading(fn); } #endif if (NULL == (fp = open_trace_mfile(fn, NULL))) { errout("'%s': couldn't open\n", fn); return NULL; } read = mfread_reading(fp, fn, format); mfclose(fp); return read; }
/*! * Read a sequence from a file "fnin" of format "format". If "format" is 0 * (ANY_FORMAT), we automatically determine the correct format. * Returns: * Read * for success * NULLRead for failure */ Read *read_reading(char *fn, int format) { Read *read; mFILE *fp; #ifdef USE_BIOLIMS if( !strncmp(fn,BIOLIMS_TAG,strlen(BIOLIMS_TAG))){ return spReadBiolimsReading(fn); } #endif /* * If we're asking for an Experiment file, read it. * If the format is ANY then attempt EXP first following by trace. * Otherwise use the trace search mechanism. * * Note this is purely for locating files and not for forcing the file * format. It's here so that experiment files and trace files may be * given identical names but accessed through different search paths * (as is the case with the trace server). */ if (format == TT_EXP) { if (NULL == (fp = open_exp_mfile(fn, NULL))) { errout("'%s': couldn't open\n", fn); return NULL; } } else { fp = NULL; if (format == TT_ANY) fp = open_exp_mfile(fn, NULL); if (!fp && NULL == (fp = open_trace_mfile(fn, NULL))) { errout("'%s': couldn't open\n", fn); return NULL; } } read = mfread_reading(fp, fn, format); mfclose(fp); return read; }
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); }
/* * Read a sequence from a FILE *fp of format "format". If "format" is 0 * (ANY_FORMAT), we automatically determine the correct format. * We still pass a filename 'fn' although this isn't used other than for * filling in the read->trace_name field. * * NB this function should NOT be used when Biolims support is required * (as biolims readings are not stored in a file) * * Returns: * Read * for success * NULLRead for failure */ Read *mfread_reading(mFILE *fp, char *fn, int format) { Read *read; mFILE *newfp; if (!fn) fn = "(unknown)"; newfp = freopen_compressed(fp, NULL); if (newfp != fp) { fp = newfp; } else { newfp = NULL; } #ifdef _WIN32 /* * jkb 16/05/00 comment below * * On windows "prog < file.abi" will work wrongly (compared to * "prog file.abi") because windows is rather stupid. It treats ascii * and binary streams differently, it considers stdin to be ascii unless * told otherwise, and it can only be told otherwise by using non-ansi * windows-specific function calls. */ if (format != TT_EXP && format != TT_PLN && fp->fp) _setmode(_fileno(fp->fp), _O_BINARY); #endif if (format == TT_ANY) { format = fdetermine_trace_type(fp); mrewind(fp); } switch (format) { case TT_UNK: case TT_ERR: errout("File '%s' has unknown trace type\n", fn); read = NULLRead; break; #ifdef IOLIB_SCF case TT_SCF: { Scf *scf; scf = mfread_scf(fp); if (scf) { read = scf2read(scf); scf_deallocate(scf); } else read = NULLRead; break; } #endif #ifdef IOLIB_CTF case TT_CTF: read = mfread_ctf(fp); break; #endif #ifdef IOLIB_ZTR case TT_ZTR: case TT_ZTR1: case TT_ZTR2: case TT_ZTR3: { ztr_t *ztr; if ((ztr = mfread_ztr(fp))) { uncompress_ztr(ztr); read = ztr2read(ztr); delete_ztr(ztr); } else { read = NULLRead; } break; } #endif #ifdef IOLIB_ABI case TT_ABI: read = mfread_abi(fp); break; #endif #ifdef IOLIB_ALF case TT_ALF: read = mfread_alf(fp); break; #endif #ifdef IOLIB_EXP case TT_EXP: { /* FIXME: we shouldn't redirect like this */ Exp_info *e = exp_mfread_info(fp); read = e ? exp2read(e,fn) : NULLRead; break; } #endif #ifdef IOLIB_PLN case TT_PLN: read = mfread_pln(fp); break; #endif default: errout("Unknown format %d specified to read_reading()\n", format); read = NULLRead; } if (read != NULLRead && (read->trace_name = (char *)xmalloc(strlen(fn)+1))) strcpy(read->trace_name, fn); if (newfp) mfclose(newfp); return read; }
/* this is REALLY a f**k ... why didnt they put the fname in a own line :( */ int parseMultiHead(char **name, char **fname, char **ctyp) { char *endchars; const char *contt="Content-Type: ", *line; const char *contd="Content-Disposition: form-data; name="; int i, ret=0; MFILE *mf=mfopen(), *mfname=mfopen(); free(*ctyp); (*ctyp)=strdup(""); /* read till empty line appears - end of header ... */ while((miscFReadLn(stdin, mf)>=0) && (line=mfGetData(mf)) && !(line[0]=='\n' || (line[0]!=0 && line[0]=='\r' && line[1]=='\n'))) { /* make sure, next lines starts at beginn of file again... */ mfSetLength(mf, 0); /* "Content-Type: what/ever" line */ if(!strncasecmp(line, contt, strlen(contt))) { free(*ctyp); (*ctyp)=miscStringDelCrLf((char*)strdup((char*)(line+strlen(contt)))); } /* "Content-Disposition: form-data; name="whatever"; filename="C:\f**k.txt"" - line */ if(!strncasecmp(line, contd, strlen(contd))) { i=strlen(contd); if(line[i]=='"') { endchars="\"\r\n\0"; i++; } else endchars=";\r\n\0"; /* parse name */ while(strchr(endchars, line[i])==NULL) mfputc(line[i++], mfname); *name=realloc(*name, mfGetLength(mfname)+1); strcpy(*name, mfGetData(mfname)); mfSetLength(mfname, 0); if(line[i]=='\"') i++; if(line[i]!=';') { ret=CgiKindValue; continue; } else ret=CgiKindFile; /* we have a filename= part here - parse filename */ while(line[i]!=0 && line[i]!='=') i++; i++; if(line[i]=='\"') { endchars="\"\r\n\0"; i++; } else endchars=";\r\n\0"; while(strchr(endchars, line[i])==NULL) mfputc(line[i++], mfname); if(mfGetLength(mfname)>0) { *fname=realloc(*fname, mfGetLength(mfname)+1); strcpy(*fname, mfGetData(mfname)); mfSetLength(mfname, 0); } else { *fname=realloc(*fname, 16); (*fname)[0]=0; } } } mfclose(mf); mfclose(mfname); return(ret); }
int main(int argc, char **argv) { int format = TT_ANY, r, prec = 0, version = 3, silent = 0; int compress_mode = -1; char *inf = NULL; char *outf = NULL; mFILE *ofp = mstdout(); int normalise = 0; for (argc--, argv++; argc > 0; argc--, argv++) { if (strcmp(*argv, "-8") == 0) { prec = 1; } else if (strcmp(*argv, "-2") == 0) { version = 2; } else if (strcmp(*argv, "-3") == 0) { version = 3; } else if (strcmp(*argv, "-normalise") == 0) { normalise = 1; } else if (strcmp(*argv, "-s") == 0) { silent = 1; } else if (strcasecmp(*argv, "-abi") == 0) { format = TT_ABI; inf = *++argv; argc--; } else if (strcasecmp(*argv, "-alf") == 0) { format = TT_ALF; inf = *++argv; argc--; } else if (strcasecmp(*argv, "-scf") == 0) { format = TT_SCF; inf = *++argv; argc--; } else if (strcasecmp(*argv, "-ztr") == 0) { format = TT_ZTR; inf = *++argv; argc--; } else if (strcasecmp(*argv, "-any") == 0) { format = TT_ANY; inf = *++argv; argc--; } else if (strcasecmp(*argv, "-output") == 0) { outf = *++argv; argc--; } else if (strcasecmp(*argv, "-compress") == 0) { compress_mode = compress_str2int(*++argv); argc--; } else { break; } } /* if no args left than input file must have been specified */ if (!argc && !inf) usage(); /* if outfile set, then using original syntax, so don't expect any extra args */ if (argc && outf) usage(); if (!silent) { printf("makeSCF v3.06\n"); printf("Copyright (c) MRC Laboratory of Molecular Biology, 2001. All rights reserved.\n"); } set_scf_version(version); if(!argc) { /* original calling syntax */ if (outf) { ofp = mfopen(outf, "wb+"); if (NULL == ofp) { perror(outf); return 1; } } r = convert(inf, ofp, outf, format, prec, compress_mode, normalise); mfclose(ofp); return r; } /* else */ { /* new calling syntax, handling multiple files */ int result=0; for (; argc > 0; argc--, argv++) { if (inf) { /* got infile, so get outfile and process */ outf= *argv; ofp = mfopen(outf, "wb+"); if (NULL == ofp) { perror(outf); if(!result) result=1; continue; } r = convert(inf, ofp, outf, format, prec, compress_mode, normalise); mfclose(ofp); if(!result) /* keep track of the first error */ result=r; /* now need to get another infile */ inf=NULL; } else { /* need infile */ inf= *argv; } } return result; } }
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; }
int mfcloseall(void) { while ( __memstreambase ) mfclose( __memstreambase ); return 0; }
int main(int argc, char *argv[]){ MFILE* fp = mfopen(argv[1], "a"); mfputs(argv[2], fp); mfclose(fp); return 0; }