MFILE *fileRead(char *fname)
{
	MFILE *mfin=mfopen(), *mfout=mfopen(), *mfbuf=mfopen();
	FILE *f=fopen(fname, "r");
	const char *flastok, *fhit, *fend;
	if(f==NULL) pexit("Cannot read input file ", fname);
	
	mfFileToMFile(f, mfin);
	fclose(f);
	
	flastok=fhit=fend=mfGetData(mfin);
	while((fhit=strstr(flastok, "<!--#"))!=NULL){
		searchInput(flastok, fhit);
	
		fend=strstr(fhit, "-->");
		if(fend==NULL) pexit("Parse error - '-->' expected - exiting\n", fhit);
		if(commdepth==0)
			escapeWrite((void*)flastok, fhit-flastok, mfout);
		flastok=(const char*)(fend+3);
		mfSetLength(mfbuf, 0);

		mfwrite((void*)(fhit+5), 1, fend-(fhit+5), mfbuf);
		parseMeta(mfout, mfGetData(mfbuf));
	}
	searchInput(flastok, (char*)0x8FFFFFFF);
	escapeWrite((void*)flastok, strlen(flastok), mfout);
	mfprintf(mfout, "\";\n");
	
	mfclose(mfin);
	return(mfout);
}
Exemple #2
0
/*
 * Write a sequence to a file "fn" of format "format". If "format" is 0,
 * we choose our favourite - SCF.
 *
 * Returns:
 *   0 for success
 *  -1 for failure
 */
int write_reading(char *fn, Read *read, int format) {
    mFILE *fp = mfopen(fn, "wb");
    if (!fp)
	return -1;
    
    return mfwrite_reading(fp, read, format);
}
void searchInput(const char *flastok, const char *fhit)
{
	MFILE *mfbuf=mfopen();
	const char *input, *ta, *hit, *select, *end, *test;

	hit=flastok;
	do{
		input=strstr(hit, "<input");
		ta=strstr(hit, "<textarea");
		select=strstr(hit, "<select");
		hit=(char*)0x8FFFFFFF;
		if(input!=NULL && input<hit) hit=input;
		if(ta!=NULL && ta<hit) hit=ta;
		if(select!=NULL && select < hit) hit=select;
		if(hit!=(char*)0x8FFFFFFF && hit<fhit){
			end=strchr(hit, '>');
			test=strchr(hit+1, '<');
			if(test!=NULL && test<end) end=strchr(end+1, '>');
			mfSetLength(mfbuf, 0);
			mfwrite((void*)hit+1, 1, end-hit-1, mfbuf);
			printf("Input: %s\n", mfGetData(mfbuf));
			parseInput(mfGetData(mfbuf));
			hit++;
		}
	}while(hit!=(char*)0x8FFFFFFF && hit<fhit);

	mfclose(mfbuf);
}
/* ---------------- ac_read  ----------------------------------
 * Given a pointer to a filename, read in the data.
 * This is an interface function, visible to the outside world.
 */
struct aa_clssfcn *
ac_read (const char *fname)
{
    FILE *fp;
    size_t n_class, n_att;
    struct aa_clssfcn *aa_clssfcn = NULL;
    size_t i;
    const char *this_sub = "ac_read";

#   ifndef BUFSIZ
        enum {BUFSIZ = 1024};
#   endif
    char buf [BUFSIZ];

    if ((fp = mfopen (fname, "r", this_sub)) == NULL)
        return (NULL);

    if ((n_class = get_n_class (fp, buf, BUFSIZ)) == 0)
        goto end;
    if ((n_att = get_n_att (fp, buf, BUFSIZ)) == 0)
        goto end;

    aa_clssfcn = new_aa_clssfcn ( n_class, n_att);

    for (i = 0; i < n_class; i++) {
        if (read_class (fp, buf, BUFSIZ, aa_clssfcn, i, MIN_AA)==EXIT_FAILURE){
            aa_clssfcn_destroy (aa_clssfcn);
            goto end;
        }
    }
 end:
    fclose (fp);
    return (aa_clssfcn);
}
Exemple #5
0
int main(int argc, char **argv) {
    int a = 1;
    int format = TT_ANY;
    mFILE *ofp = mstdout();
    char *name = NULL, *oname = NULL;
    int output_conf = 0;

    read_sections(READ_BASES);

    while (a < argc) {
        if (strcasecmp(argv[a], "-abi") == 0)
            format = TT_ABI;

        else if (strcasecmp(argv[a], "-alf") == 0)
            format = TT_ALF;

        else if (strcasecmp(argv[a], "-scf") == 0)
            format = TT_SCF;

        else if (strcasecmp(argv[a], "-pln") == 0)
            format = TT_PLN;

        else if (strcasecmp(argv[a], "-name") == 0) {
            if (a == argc)
                usage();

            name = argv[++a];

        } else if (strcasecmp(argv[a], "-output") == 0) {
            if (a == argc)
                usage();

            ofp = mfopen(oname = argv[++a], "wb");
            if (NULL == ofp) {
                perror(argv[a]);
                return 1;
            }

        } else if (strcasecmp(argv[a], "-conf") == 0) {
            output_conf = 1;

        } else if (argv[a][0] == '-')
            usage();
        else
            break;

        a++;
    }

    if (a+1 != argc)
        usage();

    if (!name)
        name = oname;
    if (!name)
        name = argv[a];

    return convert(argv[a], format, ofp, name, output_conf);
}
/* ---------------- prob_vec_write ----------------------------
 * Write a probability vector a specified file name.
 * We have to write some sizes first, so we know how many bytes
 * we may need, but we 
 */
int
prob_vec_write (struct prob_vec *p_v, const char *fname)
{
    FILE *fp;
    unsigned short *nc, *nc_last;
    unsigned total = 0;
    const char *this_sub = "prob_vec_write";

    if (! (fp = mfopen(fname, "w", this_sub)))
        return EXIT_FAILURE;
    if (p_v -> cmpct_prob == NULL)
        prob_vec_compact (p_v);

    nc      = p_v->cmpct_n;
    nc_last = nc + p_v->n_pvec;
    for ( ; nc < nc_last; nc++)
        total += *nc;
    write_magic_num (fp);

    if (xfwrt ( &PVEC_VERSION,  sizeof(PVEC_VERSION),  1, fp, this_sub, fname)
        == EXIT_FAILURE) goto error;    
    if (write_4 (p_v->frag_len, fp, this_sub, fname) == EXIT_FAILURE)
        goto error;
    if (write_4 (p_v->prot_len, fp, this_sub, fname) == EXIT_FAILURE)
        goto error;
    if (write_4 (p_v->n_pvec,   fp, this_sub, fname) == EXIT_FAILURE)
        goto error;
    if (write_4 (p_v->n_class,  fp, this_sub, fname) == EXIT_FAILURE)
        goto error;
    if (xfwrt( p_v->cmpct_n, sizeof (p_v->cmpct_n[0]), p_v->n_pvec, fp,
               this_sub, fname) == EXIT_FAILURE)
        goto error;
    if (xfwrt( p_v->cmpct_prob, sizeof(p_v->cmpct_prob[0]), total, fp,
               this_sub, fname) == EXIT_FAILURE)
        goto error;
    if (xfwrt( p_v->cmpct_ndx, sizeof(p_v->cmpct_ndx[0]), total, fp,
               this_sub, fname) == EXIT_FAILURE)
        goto error;
        
    if (xfwrt ( &p_v->norm_type, sizeof(p_v->norm_type), 1, fp,
                this_sub, fname) == EXIT_FAILURE) goto error;

    /*store compound information*/
    if (write_4 (p_v->compnd_len,  fp, this_sub, fname) == EXIT_FAILURE)
        goto error;

    if (xfwrt ( p_v->compnd, sizeof(char), p_v->compnd_len, fp,
                this_sub, fname) == EXIT_FAILURE) goto error;


 
    fclose (fp);
    return EXIT_SUCCESS;
 error:
    fclose (fp);
    return EXIT_FAILURE;
}
Exemple #7
0
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;
}
Exemple #8
0
/*
 * Searches for file in the directory 'dirname'. If it finds it, it opens
 * it. This also searches for compressed versions of the file in dirname
 * too.
 *
 * Returns mFILE pointer if found
 *         NULL if not
 */
static mFILE *find_file_dir(char *file, char *dirname) {
    char path[PATH_MAX+1];
    size_t len = strlen(dirname);
    char *cp;

    if (dirname[len-1] == '/')
	len--;

    /* Special case for "./" or absolute filenames */
    if (*file == '/' || (len==1 && *dirname == '.')) {
	sprintf(path, "%s", file);
    } else {
	/* Handle %[0-9]*s expansions, if required */
	char *path_end = path;
	*path = 0;
	while ((cp = strchr(dirname, '%'))) {
	    char *endp;
	    long l = strtol(cp+1, &endp, 10);
	    if (*endp != 's') {
		strncpy(path_end, dirname, (endp+1)-dirname);
		path_end += (endp+1)-dirname;
		dirname = endp+1;
		continue;
	    }
	    
	    strncpy(path_end, dirname, cp-dirname);
	    path_end += cp-dirname;
	    if (l) {
		strncpy(path_end, file, l);
		path_end += MIN(strlen(file), l);
		file     += MIN(strlen(file), l);
	    } else {
		strcpy(path_end, file);
		path_end += strlen(file);
		file     += strlen(file);
	    }
	    len -= (endp+1) - dirname;
	    dirname = endp+1;
	}
	strncpy(path_end, dirname, len);
	path_end += MIN(strlen(dirname), len);
	*path_end = 0;
	if (*file) {
	    *path_end++ = '/';
	    strcpy(path_end, file);
	}

	//fprintf(stderr, "*PATH=\"%s\"\n", path);
    }

    if (is_file(path)) {
	return mfopen(path, "rb");
    }

    return NULL;
}
Exemple #9
0
/*!
 * Write a sequence to a file "fn" of format "format". If "format" is 0,
 * we choose our favourite - SCF.
 *
 * Returns:
 *   0 for success
 *  -1 for failure
 */
int write_reading(char *fn, Read *read, int format) {
    int ret;
    mFILE *fp = mfopen(fn, "wb");
    if (!fp)
	return -1;
    
    ret = mfwrite_reading(fp, read, format);
    mfclose(fp);
    return ret;
}
Exemple #10
0
/*
 * Searches for file in the directory 'dirname'. If it finds it, it opens
 * it. This also searches for compressed versions of the file in dirname
 * too.
 *
 * Returns mFILE pointer if found
 *         NULL if not
 */
static mFILE *find_file_dir(char *file, char *dirname) {
    char *path;
    mFILE *mf = NULL;

    path = expand_path(file, dirname);

    if (is_file(path))
	mf = mfopen(path, "rbm");

    free(path);
    return mf;
}
/* ---------------- score_mat_read   --------------------------
 * Read a score matrix from a file. Return a newly allocated
 * score_mat pointer.
 */
struct score_mat *
score_mat_read ( const char *fname)
{
    float **mat;
    struct score_mat *ret_mat = NULL;
    FILE *fp;
    unsigned to_read;
    unsigned int n_rows, n_cols, ret;
    int err = 0;
    static int first = 1;
    const char *this_sub = "score_mat_read";
    const char *read_fail = "Read fail on %s in %s\n";
    extern const char *prog_bug;
    if (sizeof(n_rows) != 4) {
        err_printf (this_sub, prog_bug, __FILE__, __LINE__);
        return NULL;
    }
    if ((fp = mfopen (fname, "r", this_sub)) == NULL)
        return NULL;

    if ((err = file_no_cache(fp)) != 0) {
        if (first) {
            const char *no_cache = "cannot disable read cache: %s: %s";
            first = 0;
            err_printf (this_sub, no_cache, fname, strerror (err));
        }
    }

    if (fread (&n_rows, sizeof (n_rows), 1, fp) != 1) {
        err_printf (this_sub, read_fail, "n_rows", fname);
        goto end;
    }
    if (fread (&n_cols, sizeof (n_cols), 1, fp) != 1) {
        err_printf (this_sub, read_fail, "n_cols", fname);
        goto end;
    }

    ret_mat = score_mat_new (n_rows - 2, n_cols - 2);
    mat = ret_mat->mat;
    to_read = (n_rows ) * (n_cols);
    if ((ret = fread (mat[0], sizeof (mat[0][0]), to_read, fp)) != to_read) {
        err_printf (this_sub, "Failed reading %s. Wanted %lu items. Got %lu\n",
                    fname, (long unsigned) to_read, (long unsigned) ret);
        score_mat_destroy (ret_mat);
        goto end;
    }

    end:
    fclose (fp);
    return ret_mat;
}
Exemple #12
0
int main(int argc, char **argv) {
    ztr_t *ztr;
    mFILE *fp;
    int i;

    if (argc >= 2) {
	if (NULL == (fp = mfopen(argv[1], "rb"))) {
	    perror(argv[1]);
	    return 1;
	}
    } else {
	fp = mstdin();
    }

    if (NULL == (ztr = mfread_ztr(fp))) {
	perror("fread_ztr");
	return 1;
    }

    printf("Nchunks = %d\n", ztr->nchunks);
    for (i = 0; i < ztr->nchunks; i++) {
	char str[5];
	int complen;
	int rawlen;
	char *val;

	(void)ZTR_BE2STR(ztr->chunk[i].type, str);
	complen = ztr->chunk[i].dlength;
	val = ztr_lookup_mdata_value(ztr, &ztr->chunk[i], "TYPE");
	if (val)
	    printf("-- %s (%s) --\n", str, val);
	else
	    printf("-- %s --\n", str);
	explode_chunk(ztr, &ztr->chunk[i]);
	rawlen = ztr->chunk[i].dlength;
	printf("SUMMARY %s  mlen %3d, dlen %6d, rawlen %6d, ratio %f\n",
	       str, ztr->chunk[i].mdlength,
	       complen, rawlen, (double)complen/rawlen);
#if 0
	fflush(stdout);
	puts("\n========================================");
	write(1, ztr->chunk[i].data, ztr->chunk[i].dlength);
	puts("\n========================================");
#endif
    }

    delete_ztr(ztr);
    
    return 0;
}
/* ---------------- score_mat_write  --------------------------
 * Dump a score matrix to a file.
 */
int
score_mat_write (const struct score_mat *smat, const char *fname)
{
    FILE *fp;
    float **mat;
    unsigned int n_rows, n_cols;
    size_t to_write;
    const char *this_sub = "score_mat_write";
    extern const char *null_point, *prog_bug;

    if (sizeof(n_rows) != 4) {
        err_printf (this_sub, prog_bug, __FILE__, __LINE__);
        return EXIT_FAILURE;
    }
    if ( ! smat ) {
        err_printf (this_sub, null_point);
        return EXIT_FAILURE;
    }

    if ( smat->n_rows == 0 || smat->n_cols == 0) {
        err_printf (this_sub, "n_rows or columns is zero for %s\n", fname);
        return EXIT_FAILURE;
    }
    if ((fp = mfopen ( fname, "w", this_sub)) == NULL)
        return EXIT_FAILURE;

    n_rows = (unsigned int) smat->n_rows;
    n_cols = (unsigned int) smat->n_cols;

    if (fwrite (&n_rows, sizeof (n_rows), 1, fp) != 1)
        goto error;
    if (fwrite (&n_cols, sizeof (n_cols), 1, fp) != 1)
        goto error;

    mat = smat->mat;
    to_write = n_rows * n_cols;
    if (fwrite (mat[0], sizeof (mat[0][0]), to_write, fp) != to_write)
        goto error;
    fclose (fp);
    return EXIT_SUCCESS;
    error:
        mperror (this_sub);
        err_printf (this_sub, "Failed writing to %s", fname);
        fclose (fp);
        return EXIT_FAILURE;
}
Exemple #14
0
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);
}
/* ------------------------ score_mat_write_gnuplot ----------------------
 * For Plotting an alignment traceback, e.g. with gnuplot, you need the
 * smat->mat scores. This function writes them into an ASCII file with
 * filename as param with output in each line:
 * 	i j value
 */
int
score_mat_write_gnuplot( const struct score_mat *smat, const char *fname, const char *protA, const char *protB ){
    const char *this_sub = "score_mat_write_gnuplot";
    float **mat = smat->mat;
    size_t i,j;

    FILE *fp;
    if ((fp = mfopen ( fname, "w", this_sub)) == NULL)
        return EXIT_FAILURE;

    if ( mfprintf ( fp , "%s%s%s%s%s\n","# Data from ", protA, ".pdb and ", protB ,".pdb") < 0)
        goto error;

    if ( mfprintf ( fp, "# %u entries per side \n", (unsigned int) smat->n_cols ) < 0)
        goto error;

    if ( mfprintf ( fp, "# Total entries %u\n", (unsigned int)(smat->n_rows * smat->n_cols) ) < 0)
        goto error;

    for (i = 0; i < smat->n_rows; i++ ){
        for (j = 0; j < smat->n_cols; j++ ){
     	    if ( mfprintf ( fp, "%d %d %f \n", i, j , mat[i][j] ) < 0)
                goto error;
     	}
     	if ( mfprintf ( fp, "\n") < 0)
            goto error;
    }
    if ( mfprintf ( fp, "\n" )  < 0)
        goto error;

    fclose (fp);
    return EXIT_SUCCESS;

    error:
        mperror (this_sub);
        err_printf (this_sub, "Failed writing to %s", fname);
        fclose (fp);
        return EXIT_FAILURE;
}
static int ResolveEntity(void *UserData, LPXMLENTITY entity, LPBUFFEREDISTREAM reader)
{
	FILE *f;
	XMLCH r[FILENAME_MAX]; 
	XMLCH *filename;

	if (entity->publicID && !strcmp(entity->publicID,
		"-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN")) 
		return XML_OK;

	filename = ResolveBaseUri(parser, entity->systemID, AppBase);
	if (filename != entity->systemID) {
		strcpy(r, filename);
		filename = strcat(r, entity->systemID);
	}
	if (!(f = mfopen(filename, "rb"))) {
		fprintf(stderr, "error opening file '%s'\n", filename);
		return XML_ABORT;
	}
	reader->inputData = f; 
	return XML_OK;
}
Exemple #17
0
int cgiLoadDebugData(char *fname)
{
    FILE *f=fopen(fname, "r");
    int count, subcount, type, mflen, envcount;
    MFILE *mf;
    char *name, *ctyp;
    CgiElement *where;
    if(init_complete==true || f==NULL) return(false);
    listInit();

    fread(&count, 1, 4, f);
    while(count-->0) {
        fread(&type, 1, 4, f);
        name=miscReadData(f);
        ctyp=miscReadData(f);
        fread(&mflen, 1, 4, f);
        if(mflen>0) {
            mf=mfopen();
            mfFileToMFileN(f, mf, mflen);
        } else
            mf=NULL;

        where=listAppendElement(type, name, ctyp, mf);
        fread(&subcount, 1, 4, f);

        while(subcount-->0)
            listAppendValue(where, miscReadData(f));
    }

    fread(&envcount, 1, 4, f);
    while(envcount-->0)
        miscReadSetEnviron(f);

    init_called = true;
    init_complete = true;
    fclose(f);
    return(true);
}
Exemple #18
0
int
doglobal(			/* execute a global command */
	PRIMITIVE  *g
)
{
	FILE  *fp = NULL;

	switch (g->com) {

	case PEOF:
		return(0);

	case PPAUS:
		break;

	case PINCL:
		if (g->args == NULL)
		    error(USER, "missing include file name in include");
		if (g->arg0 == 2 || (fp = fopen(g->args, "r")) == NULL) {
		    if (g->arg0 != 0)
			fp = mfopen(g->args, "r");
		    else {
			sprintf(errmsg, "cannot open user include file \"%s\"",
					g->args);
			error(USER, errmsg);
		    }
		}
		plot(fp);
		fclose(fp);
		break;

	case PDRAW:
		fflush(stdout);
		break;

	case PEOP:
		endpage();
		newpage = TRUE;
		break;

	case PSET:
		set(g->arg0, g->args);
		break;

	case PUNSET:
		unset(g->arg0);
		break;

	case PRESET:
		reset(g->arg0);
		break;

	case POPEN:
		segopen(g->args);
		break;

	case PCLOSE:
		segclose();
		break;

	default:
		sprintf(errmsg, "unknown command '%c' in doglobal", g->com);
		error(WARNING, errmsg);
		break;
	}

	return(1);
}
int Catalogs_Init(void)
{	
	LPXMLDTDVALIDATOR dtd;
	FILE *f;
	int ret=1, i=0;
	char t[FILENAME_MAX];
	char *env = getenv("XML_CATALOG_FILES");
	
	if (!env) doc = "/etc/xml/catalog";

	if (!XMLParser_Create(&parser)) {
			fputs("Out of memory\n", stderr);
			return 0;
	}
	if (!(dtd = XMLParser_CreateDTDValidator())) {
			XMLParser_Free(parser);
			fputs("Out of memory\n", stderr);
			return 0;
	}

	XMLParser_SetExternalSubset(parser, NULL, "catalog_flat.dtd");
	parser->startElementHandler = StartElement;
	parser->errorHandler = ErrorHandler;
	parser->resolveEntityHandler = ResolveEntity;
	parser->externalEntityParsedHandler = FreeInputData;
	
	while (ret) {
		if (env) doc = mstrtok(&env, t, sizeof(t)/sizeof(t[0]), ';');
		if (!doc) {
			if (env && *env) {
				ret = 0;
				fprintf(stderr, "Erroneous token in XML_CATALOG FILES: %s\n", env);
			}
			break;
		}

		while (doc) {
			if (!(f = mfopen(doc, "rb"))) {
				fprintf(stderr, "Error opening file %s\n", doc);
				ret = 0;
				break;
			}
			ret = XMLParser_ParseValidateDTD(dtd, parser, filestream, f, 0);
			fclose(f);
			
			if (!ret || !nextCatalog || i == nextCatalog->length) 
				doc = NULL;
			else {
				char **uri = XMLVector_Get(nextCatalog, i++);
				doc = *uri;
			}
		}
	}
	
	if (ret) {
		if (publicIDS && publicIDS->length > 1) 
			qsort(publicIDS->array, publicIDS->length, sizeof(struct IDs), 
            (int(*)(const void*, const void*))idsort);
		if (systemIDS && systemIDS->length > 1) 
			qsort(systemIDS->array, publicIDS->length, sizeof(struct IDs), 
            (int(*)(const void*, const void*))idsort);
	}
	XMLParser_FreeDTDValidator(dtd);
	XMLParser_Free(parser);
	return ret;
}
Exemple #20
0
/* 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);
}
/* ---------------- prob_vec_read  ----------------------------
 * Here is a thought... When we read from one of these files,
 * we are always going to read the whole thing. It might be a
 * good idea to read into a relatively large ( 24 K ) ? buffer
 * using setvbuf().
 */
struct prob_vec *
prob_vec_read (const char *fname)
{
    enum { FBUFSIZ = 32768 };
    char fbuf [FBUFSIZ];
    FILE *fp;
    struct prob_vec *p_v = NULL;
    int version, err;
    unsigned total = 0;
    unsigned int n_pvec, n_class, prot_len, frag_len;
    static unsigned char first = (char) 1;
    const char *this_sub = "prob_vec_read";
    const char *no_swap = "Byte swapping not written yet. Reading from %s\n";
    const char *broken  = "File error reading magic number from %s\n";
    const char *s = "Turning off caching for %s:\n\"%s\"\n";

    if (sizeof (n_pvec) != 4) {
        err_printf (this_sub, mismatch, sizeof (n_pvec)); return NULL; }

    if (! (fp = mfopen(fname, "r", this_sub)))
        return NULL;
    if (setvbuf(fp, fbuf, _IOFBF, FBUFSIZ))
        err_printf (this_sub, "warning setvbuf() call failed\n");

    if ((err = file_no_cache(fp)) != 0) {
        if (first) {
            first = 0;
            err_printf (this_sub, s, fname, strerror (err));
        }
    }

    switch (read_magic_num (fp)) {
    case BYTE_REVERSE:
        err_printf (this_sub, no_swap, fname); fclose (fp); return NULL;
    case BYTE_BROKEN:
        err_printf (this_sub, broken, fname);  fclose (fp); return NULL;
    case BYTE_STRAIGHT:
        break;                         /* fall through, no problem occurred */ 
    }

    if( xfrd ( &version, sizeof (version),       1, fp, this_sub, fname)
        == EXIT_FAILURE) goto error;
    if (version != 1) {
        err_printf (this_sub, "error reading version num, got %d\n", version);
        goto error;
    }
    if( xfrd ( &frag_len, sizeof (frag_len), 1, fp, this_sub, fname)
        == EXIT_FAILURE) goto error;
    if( xfrd ( &prot_len, sizeof (prot_len), 1, fp, this_sub, fname)
        == EXIT_FAILURE) goto error;
    if( xfrd ( &n_pvec,   sizeof (n_pvec),   1, fp, this_sub, fname)
        == EXIT_FAILURE) goto error;
    if( xfrd ( &n_class,  sizeof (n_class),  1, fp, this_sub, fname)
        == EXIT_FAILURE) goto error;
    err_printf(this_sub,"blabla\n");
	p_v = new_pvec (frag_len, prot_len, n_pvec, 0);
    p_v->n_class = n_class;
    p_v->cmpct_n = E_MALLOC (n_pvec * sizeof (p_v->cmpct_n[0]));
    if (xfrd (p_v->cmpct_n, sizeof (p_v->cmpct_n[0]), n_pvec, fp,
              this_sub, fname) == EXIT_FAILURE) goto error;
    {
        unsigned short *nc            = p_v->cmpct_n;
        const unsigned short *nc_last = nc + n_pvec;
        for ( ; nc < nc_last; nc++)
            total += *nc;
    }
    p_v->cmpct_prob = E_MALLOC (total * sizeof (p_v->cmpct_prob[0]));
    if (xfrd (p_v->cmpct_prob, sizeof (p_v->cmpct_prob[0]), total, fp,
              this_sub, fname) == EXIT_FAILURE) goto error;
    p_v->cmpct_ndx  = E_MALLOC (total * sizeof (p_v->cmpct_ndx[0]));
    if (xfrd (p_v->cmpct_ndx, sizeof (p_v->cmpct_ndx[0]), total, fp,
              this_sub, fname) == EXIT_FAILURE) goto error;
    if( xfrd (&p_v->norm_type, sizeof (p_v->norm_type), 1, fp, this_sub, fname)
        == EXIT_FAILURE) goto error;
 
   /*read compound information*/
    if (xfrd (&p_v->compnd_len,  sizeof(p_v->compnd_len),1, fp, this_sub, fname) 
	== EXIT_FAILURE) goto error;

    p_v->compnd  = E_MALLOC (p_v->compnd_len * sizeof (char));
    if (xfrd ( p_v->compnd, sizeof(char), p_v->compnd_len, fp,
                this_sub, fname) == EXIT_FAILURE) goto error;



   fclose (fp);
    return p_v;
 error:
    if (new_pvec)
        prob_vec_destroy (p_v);
    fclose (fp);
    return NULL;
}
Exemple #22
0
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(int argc, char *argv[]){
	MFILE* fp = mfopen(argv[1], "a");
	mfputs(argv[2], fp);
	mfclose(fp);
	return 0;
}
/* ---------------- sub_mat_read ------------------------------
 */
struct sub_mat *
sub_mat_read (const char *fname)
{
#   ifndef BUFSIZ
#       error please define BUFSIZ to around 1024
#   endif
    fpos_t pos;
    struct sub_mat *smat = NULL;
    FILE *fp;
    char buf [BUFSIZ];
    char aanames [ MAX_AA + 3];
    const char *this_sub = "read_mat";
    const char comment = '#';
    if ((fp = mfopen (fname, "r", this_sub)) == NULL)
        return NULL;

    /* Portably initialise the fpos_t structure
     * because =0 is insufficient
     */
    if (fgetpos(fp, &pos)) {
        mperror (this_sub);
        goto broken;
    }


    smat = E_MALLOC ( sizeof (*smat));
    smat -> fname   = NULL;
    smat -> comment = NULL;
    smat->fname = save_str (fname);

    while (fgets (buf, BUFSIZ, fp) != NULL) {
        if ( buf[0] != comment) {
            if (fsetpos (fp, &pos)) {
                mperror (this_sub);
                goto broken;
            }
            break;
        }
        smat->comment = save_str_append (smat->comment, buf);
        if (fgetpos(fp, &pos)) {
            mperror (this_sub);
            goto broken;
        }
    }

    if (fgets (buf, BUFSIZ, fp) == NULL)
        goto broken;
    memset (aanames, BAD_AA, MAX_AA);
    if (get_aa_names (aanames, buf, BAD_AA) == EXIT_FAILURE)
        goto broken;
    {
        int n, m;
        for (n = 0; n < MAX_AA; n++)
            for ( m = 0; m < MAX_AA; m++)
                smat->data[n][m] = (mat_t) INVALID;
    }
    {
        int n = 0;
        for ( ;fgets(buf, BUFSIZ, fp) != NULL; n++) {
            if (aanames [n] == BAD_AA)
                continue;
            if (add_to_mat (smat, aanames, buf, n) == EXIT_FAILURE)
                err_printf (this_sub, "Ignoring matrix line\n");
        }
    }

    fclose (fp);
    return smat;
 broken:
    fclose (fp);
    free_if_not_null (smat->comment);
    free_if_not_null (smat->fname);
    free_if_not_null (smat);
    return NULL;
}
Exemple #25
0
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;
}