void covariance_read_order_nvecs(char *infile, int *order, int *nvecs) { FILE *fp; char file_type, asc_or_bin, str[200]; int i, j; if(!(fp = fopen(infile, "rb"))) fatalerr("covariance_read_order_nvecs", "fopen for reading \ failed", infile); for(i = 0; (j = getc(fp)) != '\n'; i++) if(j == EOF) fatalerr("covariance_read_order_nvecs", "file ends partway \ through description field", infile); fgets(str, 200, fp); sscanf(str, "%c %c", &file_type, &asc_or_bin); if(file_type != PCASYS_COVARIANCE_FILE) fatalerr("covariance_read_order_nvecs", "file is not of type \ PCASYS_COVARIANCE_FILE", infile); if(asc_or_bin == PCASYS_ASCII_FILE) fscanf(fp, "%d %d", order, nvecs); else if(asc_or_bin == PCASYS_BINARY_FILE) { fread(order, sizeof(int), 1, fp); fread(nvecs, sizeof(int), 1, fp); #ifdef __NBISLE__ swap_int_bytes(*order); swap_int_bytes(*nvecs); #endif } else fatalerr("covariance_read_order_nvecs", "illegal ascii-or-binary \ code", infile); fclose(fp); }
void covariance_write(char *outfile, char *desc, const int ascii, int order, int nvecs, float *the_floats) { FILE *fp; int r, c; float *p; float *tfloats; if(!(fp = fopen(outfile, "wb"))) fatalerr("covariance_write", "fopen for writing failed", outfile); if(strchr(desc, '\n')) fatalerr("covariance_write", "description string contains a \ newline", outfile); fprintf(fp, "%s\n%c %c\n", desc, PCASYS_COVARIANCE_FILE, (ascii ? PCASYS_ASCII_FILE : PCASYS_BINARY_FILE)); if(ascii) { fprintf(fp, "%d %d\n\n", order, nvecs); for(r = 0, p = the_floats; r < order; r++) { for(c = 0; c < r; c++) fprintf(fp, "%14.7e%c", *p++, ((c % 5 < 4) ? ' ' : '\n')); fprintf(fp, "%14.7e\n\n", *p++); } } else { /* binary */ #ifdef __NBISLE__ swap_int_bytes(order); swap_int_bytes(nvecs); #endif fwrite(&order, sizeof(int), 1, fp); fwrite(&nvecs, sizeof(int), 1, fp); #ifdef __NBISLE__ swap_int_bytes(order); swap_int_bytes(nvecs); swap_float_bytes_vec_cpy(the_floats, (order * (order+1))/2, &tfloats); p = the_floats; the_floats = tfloats; #endif fwrite(the_floats, sizeof(float), (order * (order + 1)) / 2, fp); #ifdef __NBISLE__ free(tfloats); the_floats = p; #endif } fclose(fp); }
/*********************************************************************** ************************************************************************ #cat: read_num_pats - Routine opens a binary patterns file, reads, and #cat: returns the number of pattern feature vectors #cat: in the file. Input: fn - name of input patterns file Return Code: Non-negative - number of pattern feature vectors in the file Negative - system error ************************************************************************/ int read_num_pats(char *fn) { FILE *fp; int n, idum, nPats; if ((fp = fopen(fn, "rb")) == (FILE *)NULL){ fprintf(stderr, "ERROR : read_num_patterns : fopen : %s\n", fn); return(-2); } if ((n = fread(&idum, sizeof(int ), 1, fp)) != 1){ fprintf(stderr, "ERROR : read_num_patterns : fread : idum1a\n"); fclose(fp); return(-3); } if ((n = fread(&nPats, sizeof(int ), 1, fp)) != 1){ fprintf(stderr, "ERROR : read_num_patterns : fread : nPats\n"); fclose(fp); return(-4); } fclose(fp); #ifdef __NBISLE__ swap_int_bytes(nPats); #endif return(nPats); }
void classes_read_n(char *infile, int *n) { FILE *fp; char file_type, asc_or_bin, str[200]; int i; if(!(fp = fopen(infile, "rb"))) fatalerr("classes_read_n", "fopen for reading failed", infile); while((i = getc(fp)) != '\n') if(i == EOF) fatalerr("classes_read_n", "file ends partway through \ description field", infile); fgets(str, 200, fp); sscanf(str, "%c %c", &file_type, &asc_or_bin); if(file_type != PCASYS_CLASSES_FILE) fatalerr("classes_read_n", "file is not of type \ PCASYS_CLASSES_FILE", infile); if(asc_or_bin == PCASYS_ASCII_FILE) fscanf(fp, "%d", n); else if(asc_or_bin == PCASYS_BINARY_FILE) { fread(n, sizeof(int), 1, fp); #ifdef __NBISLE__ swap_int_bytes(*n); #endif } else fatalerr("classes_read_n", "illegal ascii-or-binary code", infile); fclose(fp); }
int putc_uint( unsigned int iint, /* input unsigned int */ unsigned char *odata, /* output byte buffer */ const int oalloc, /* allocated size of buffer */ int *olen) /* filled length of buffer */ { int ret; unsigned char *cptr; #if defined(__i386__) || defined(__x86_64__) || defined(_WIN32) swap_int_bytes(iint); #endif cptr = (unsigned char *)(&iint); ret = putc_bytes(cptr, sizeof(unsigned int), odata, oalloc, olen); if(ret) return(ret); return(0); }
int getc_uint( unsigned int *oint_dat, /* pointer to returned unsigned int */ unsigned char **cbufptr, /* pointer to next byte in buffer */ unsigned char *ebufptr) /* pointer to end of buffer */ { int ret; unsigned int int_dat; unsigned char *cptr; cptr = (unsigned char *)(&int_dat); ret = getc_bytes(&cptr, sizeof(unsigned int), cbufptr, ebufptr); if(ret) return(ret); #if defined(__i386__) || defined(__x86_64__) || defined(_WIN32) swap_int_bytes(int_dat); #endif *oint_dat = int_dat; return(0); }
void classes_write_ind(char *outfile, char *desc, const int ascii, int n, unsigned char *the_classes, int ncls, char **long_classnames) { FILE *fp; int i; unsigned char *ucp; int pindex = 1; if(!(fp = fopen(outfile, "wb"))) fatalerr("classes_write", "fopen for writing failed", outfile); if(strchr(desc, '\n')) fatalerr("classes_write", "description string contains a \ newline", outfile); fprintf(fp, "%s\n%c %c\n", desc, PCASYS_CLASSES_FILE, (ascii ? PCASYS_ASCII_FILE : PCASYS_BINARY_FILE)); if(ascii) { fprintf(fp, "%d\n", n); fprintf(fp, "%d\n", pindex); fprintf(fp, "%d\n", ncls); for(i = 0; i < ncls; i++) fprintf(fp, "%s\n", long_classnames[i]); for(i = 0, ucp = the_classes; i < n; i++) fprintf(fp, "%d\n", ucp[i]); } else { /* binary */ #ifdef __NBISLE__ swap_int_bytes(n); swap_int_bytes(pindex); swap_int_bytes(ncls); #endif fwrite(&n, sizeof(int), 1, fp); fwrite(&pindex, sizeof(int), 1, fp); fwrite(&ncls, sizeof(int), 1, fp); #ifdef __NBISLE__ swap_int_bytes(n); swap_int_bytes(pindex); swap_int_bytes(ncls); #endif for(i = 0; i < ncls; i++) fwrite(long_classnames[i], sizeof(char), LONG_CLASSNAME_MAXSTRLEN, fp); fwrite(the_classes, sizeof(unsigned char), n, fp); } fclose(fp); }
void covariance_read_old(char *infile, char **desc, int *order, int *nvecs, float **the_floats) { FILE *fp; char file_type, asc_or_bin, str[200], *cp, achar; int i, j, the_order; float *p, *the_floats_yow, *abuf; if(!(fp = fopen(infile, "rb"))) { fprintf(stderr, "fopen for reading failed %s\n", infile); exit(-1); } for(i = 0; (j = getc(fp)) != '\n'; i++) if(j == EOF) { fprintf(stderr, "file ends partway through description field %s\n", infile); exit(-1); } if(!(*desc = malloc(i + 1))) { fprintf(stderr, "malloc of description buffer failed %s\n", infile); exit(-1); } rewind(fp); for(cp = *desc; (achar = getc(fp)) != '\n';) *cp++ = achar; *cp = 0; fgets(str, 200, fp); sscanf(str, "%c %c", &file_type, &asc_or_bin); if(file_type != PCASYS_COVARIANCE_FILE) { fprintf(stderr, "file is not of type PCASYS_COVARIANCE_FILE %s\n", infile); exit(-1); } if(asc_or_bin == PCASYS_ASCII_FILE) { fscanf(fp, "%d %d", order, nvecs); the_order = *order; if(!(*the_floats = (float *)malloc(the_order * the_order * sizeof(float)))) { fprintf(stderr, "malloc of *the_floats failed %s\n", infile); exit(-1); } the_floats_yow = *the_floats; for(j = 0; j < the_order; j++) for(i = 0; i <= j; i++) fscanf(fp, "%f", the_floats_yow + i * the_order + j); } else if(asc_or_bin == PCASYS_BINARY_FILE) { if(fread(order, sizeof(int), 1, fp) != 1) { fprintf(stderr, "Error reading order\n"); exit(-1); } if(fread(nvecs, sizeof(int), 1, fp) != 1) { fprintf(stderr, "Error reading nvecs\n"); exit(-1); } #ifdef __NBISLE__ swap_int_bytes(*order); swap_int_bytes(*nvecs); #endif the_order = *order; if(!(*the_floats = (float *)malloc(the_order * the_order * sizeof(float)))) { fprintf(stderr, "malloc of *the_floats failed %s\n", infile); exit(-1); } the_floats_yow = *the_floats; if(!(abuf = (float *)malloc(the_order * sizeof(float)))) { fprintf(stderr, "malloc of abuf failed %s\n", infile); exit(-1); } for(j = 0; j < the_order; j++) { fread(abuf, sizeof(float), j + 1, fp); for(i = 0, p = abuf; i <= j; i++) { #ifdef __NBISLE__ swap_float_bytes(*p); #endif *(the_floats_yow + i * the_order + j) = *p++; } } } else { fprintf(stderr, "illegal ascii-or-binary code %s\n", infile); exit(-1); } fclose(fp); }
/*********************************************************************** ************************************************************************ #cat: read_bin_nnpats - Routine reads feature vectors and their classes #cat: from a file using fast binary io. Input: ifile - name of input patterns file Output: ofeats - feature vectors read from file otargs - target vectors read from file oclasses - class index read for each feature vector oclass_set - the symbol (string) for each class onPats - number of feature vectors read onInps - length of each feature vector (number of coefs) onOuts - length of each target vector (number of classes) Return Code: Zero - successful completion Negative - system error ************************************************************************/ int read_bin_nnpats(char *ifile, float **ofeats, float **otargs, int **oclasses, char ***oclass_set, int *onPats, int *onInps, int *onOuts) { char **class_set; int *classes; float *feats, *targs; int nPats, nInps, nOuts; int nFeats, nTargs, nClass; int PINPS, POUTS; float *featsptr, /* temporary pointer to input featss */ *targsptr; /* temporary pointer to class outputs */ int *anintptr; FILE *fp; /* source input pattern input file */ int i, j, k, n; int idum, tree, targ_typ, targid; if ((fp = fopen(ifile, "rb")) == NULL){ fprintf(stderr, "ERROR : read_bin_nnpats : fopen : %s\n", ifile); return(-2); } if ((n = fread(&idum, sizeof(int), 1, fp)) != 1){ fprintf(stderr, "ERROR : read_bin_nnpats : fread : idum1a\n"); return(-3); } if ((n = fread(&nPats, sizeof(int), 1, fp)) != 1){ fprintf(stderr, "ERROR : read_bin_nnpats : fread : nPats\n"); return(-4); } #ifdef __NBISLE__ swap_int_bytes(nPats); #endif if ((n = fread(&nInps, sizeof(int), 1, fp)) != 1){ fprintf(stderr, "ERROR : read_bin_nnpats : fread : nInps\n"); return(-5); } #ifdef __NBISLE__ swap_int_bytes(nInps); #endif if ((n = fread(&nOuts, sizeof(int), 1, fp)) != 1){ fprintf(stderr, "ERROR : read_bin_nnpats : fread : nOuts\n"); return(-6); } #ifdef __NBISLE__ swap_int_bytes(nOuts); #endif if ((n = fread(&targ_typ, sizeof(int), 1, fp)) != 1){ fprintf(stderr, "ERROR : read_bin_nnpats : fread : targ_typ\n"); return(-7); } #ifdef __NBISLE__ swap_int_bytes(targ_typ); #endif if ((n = fread(&tree, sizeof(int), 1, fp)) != 1){ fprintf(stderr, "ERROR : read_bin_nnpats : fread : tree\n"); return(-8); } #ifdef __NBISLE__ swap_int_bytes(tree); #endif if ((n = fread(&idum, sizeof(int), 1, fp)) != 1){ fprintf(stderr, "ERROR : read_bin_nnpats : fread : nPats\n"); return(-9); } if ((n = fread(&idum, sizeof(int), 1, fp)) != 1){ fprintf(stderr, "ERROR : read_bin_nnpats : fread : idum1b\n"); return(-10); } if (tree == TREEPATSFILE){ fprintf(stderr, "ERROR : read_bin_nnpats : "); fprintf(stderr, "tree patsfile not supported at this time\n"); return(-11); } PINPS = nInps; POUTS = nOuts; nFeats = nPats * PINPS; nTargs = nPats * POUTS; nClass = nPats; if ((feats = (float *)calloc(nFeats, sizeof(float))) == (float *)NULL){ fprintf(stderr, "ERROR : read_bin_nnpats : calloc : feats\n"); return(-12); } if ((targs = (float *)calloc(nTargs, sizeof(float))) == (float *)NULL){ fprintf(stderr, "ERROR : read_bin_nnpats : calloc : targs\n"); return(-13); } if ((classes = (int *)calloc(nClass, sizeof(int))) == (int *)NULL){ fprintf(stderr, "ERROR : read_bin_nnpats : calloc : classes\n"); return(-14); } if ((class_set = (char **)calloc(nOuts, sizeof(char *))) == (char **)NULL){ fprintf(stderr, "ERROR : read_bin_nnpats : calloc : class_set\n"); return(-15); } if (tree == JUSTPATSFILE){ if ((n = fread(&idum, sizeof(int), 1, fp)) != 1){ free(feats); free(targs); free(classes); free(class_set); fprintf(stderr, "ERROR : read_bin_nnpats : fread : idum2a\n"); return(-16); } for(i = 0; i < nOuts; i++){ if ((class_set[i] = (char *)calloc(32, sizeof(char))) == NULL){ free(feats); free(targs); free(classes); free(class_set); fprintf(stderr, "ERROR : read_bin_nnpats : calloc : class_set[%d]\n", i); return(-17); } if ((n = fread(class_set[i], sizeof(char), 32, fp)) != 32){ for(k = 0; k < nOuts; k++) free(class_set[k]); free(feats); free(targs); free(classes); free(class_set); fprintf(stderr, "ERROR : read_bin_nnpats : fread : class_set[%d]\n", i); return(-18); } } if ((n = fread(&idum, sizeof(int), 1, fp)) != 1){ for(k = 0; k < nOuts; k++) free(class_set[k]); free(feats); free(targs); free(classes); free(class_set); fprintf(stderr, "ERROR : read_bin_nnpats : fread : idum2b\n"); return(-19); } featsptr = feats; targsptr = targs; anintptr = classes; for (i = 0; i < nPats; i++){ if ((n = fread(&idum, sizeof(int), 1, fp)) != 1){ for(k = 0; k < nOuts; k++) free(class_set[k]); free(feats); free(targs); free(classes); free(class_set); fprintf(stderr, "ERROR : read_bin_nnpats : fread : idum3a\n"); return(-20); } if ((n = fread(featsptr, sizeof(float), nInps, fp)) != nInps){ for(k = 0; k < nOuts; k++) free(class_set[k]); free(feats); free(targs); free(classes); free(class_set); fprintf(stderr, "ERROR : read_bin_nnpats : fread : feature vector\n"); return(-21); } #ifdef __NBISLE__ for(j = 0; j < nInps; j++) swap_float_bytes(featsptr[j]); #endif featsptr += PINPS; if ((n = fread(&idum, sizeof(int), 1, fp)) != 1){ for(k = 0; k < nOuts; k++) free(class_set[k]); free(feats); free(targs); free(classes); free(class_set); fprintf(stderr, "ERROR : read_bin_nnpats : fread : idum3b\n"); return(-22); } if ((n = fread(&idum, sizeof(int), 1, fp)) != 1){ for(k = 0; k < nOuts; k++) free(class_set[k]); free(feats); free(targs); free(classes); free(class_set); fprintf(stderr, "ERROR : read_bin_nnpats : fread : idum4a\n"); return(-23); } if(targ_typ){ if ((n = fread(&targid, sizeof(int), 1, fp)) != 1){ for(k = 0; k < nOuts; k++) free(class_set[k]); free(feats); free(targs); free(classes); free(class_set); fprintf(stderr, "ERROR : read_bin_nnpats : fread : targid\n"); return(-24); } #ifdef __NBISLE__ swap_int_bytes(targid); #endif anintptr[i] = (targid - 1); for(j = 0; j < nOuts; j++) if(anintptr[i] == j) *targsptr++ = 1.0; else *targsptr++ = 0.0; targsptr += POUTS - nOuts; } else { if ((n = fread(targsptr, sizeof(float), nOuts, fp)) != nOuts){ for(k = 0; k < nOuts; k++) free(class_set[k]); free(feats); free(targs); free(classes); free(class_set); fprintf(stderr, "ERROR : read_bin_nnpats : fread : target vector\n"); return(-25); } #ifdef __NBISLE__ for(j = 0; j < nOuts; j++) swap_float_bytes(targsptr[j]); #endif targsptr += POUTS; /* pad to dimension with the */ /* zeroes returned by calloc */ } if ((n = fread(&idum, sizeof(int), 1, fp)) != 1){ for(k = 0; k < nOuts; k++) free(class_set[k]); free(feats); free(targs); free(classes); free(class_set); fprintf(stderr, "ERROR : read_bin_nnpats : fread : idum4b\n"); return(-26); } } /* end for */ if(!targ_typ){ targsptr = targs; anintptr = classes; for(i = 0; i < nPats; i++){ /* obtain from the target list */ /* the node index (ie class) */ anintptr[i] = -1; /* of the node near 1.0 */ for(j = 0; j < nOuts; j++) {/* 0 0 0 1 0 is class 4 */ if (*targsptr++ >= 0.99){ if (anintptr[i] == -1) /* error: class -1 if no*/ anintptr[i] = j; /* target near 1 is defined */ else anintptr[i] = -2; /* error: class is -2 if more */ } /* than one target is 1 */ } targsptr += POUTS - nOuts; /* jump over zeroes used to */ /* fill dap dimension */ } } } /* Otherwise, unrecognized tree type ... */ else{ for(k = 0; k < nOuts; k++) free(class_set[k]); free(feats); free(targs); free(classes); free(class_set); fprintf(stderr, "ERROR : read_bin_nnpats : unrecognized tree flag\n"); return(-27); } fclose(fp); *ofeats = feats; *otargs = targs; *oclass_set = class_set; *oclasses = classes; *onPats = nPats; *onInps = nInps; *onOuts = nOuts; return(0); }
void classes_read_ind(char *infile, char **desc, int *n, unsigned char **the_classes, int *ncls, char ***long_classnames) { FILE *fp; char file_type, asc_or_bin, str[200], *cp, achar; unsigned char *ucp; int i, j, pindex; char **lcnptr; if(!(fp = fopen(infile, "rb"))) fatalerr("classes_read_ind", "fopen for reading failed", infile); for(i = 0; (j = getc(fp)) != '\n'; i++) if(j == EOF) fatalerr("classes_read_ind", "file ends partway through \ description field", infile); if(!(*desc = malloc(i + 1))) fatalerr("classes_read_ind", "malloc of description buffer \ failed", infile); rewind(fp); for(cp = *desc; (achar = getc(fp)) != '\n';) *cp++ = achar; *cp = 0; fgets(str, 200, fp); sscanf(str, "%c %c", &file_type, &asc_or_bin); if(file_type != PCASYS_CLASSES_FILE) fatalerr("classes_read_ind", "file is not of type \ PCASYS_CLASSES_FILE", infile); if(asc_or_bin == PCASYS_ASCII_FILE) { fscanf(fp, "%d", n); fscanf(fp, "%d", &pindex); fscanf(fp, "%d", ncls); /* Read the long (full) names of the classes. */ malloc_dbl_char(&lcnptr, *ncls, LONG_CLASSNAME_MAXSTRLEN + 1, "classes_read_ind long_classnames"); *long_classnames = lcnptr; for(i = 0; i < *ncls; i++) fscanf(fp, "%s", lcnptr[i]); if(!pindex) fatalerr("classes_read_ind","pindex = 0","Need class indices not vectors"); if(!(*the_classes = (unsigned char *)malloc(*n * sizeof(unsigned char)))) fatalerr("classes_read_ind", "malloc of classes buffer failed", infile); for(i = 0, ucp = *the_classes; i < *n; i++) { fscanf(fp, "%d", &j); *ucp++ = j; } } else if(asc_or_bin == PCASYS_BINARY_FILE) { fread(n, sizeof(int), 1, fp); fread(&pindex, sizeof(int), 1, fp); fread(ncls, sizeof(int), 1, fp); #ifdef __NBISLE__ swap_int_bytes(*n); swap_int_bytes(pindex); swap_int_bytes(*ncls); #endif /* Read the long (full) names of the classes. */ malloc_dbl_char(&lcnptr, *ncls, LONG_CLASSNAME_MAXSTRLEN + 1, "classes_read_ind long_classnames"); *long_classnames = lcnptr; for(i = 0; i < *ncls; i++) fread(lcnptr[i], sizeof(char), LONG_CLASSNAME_MAXSTRLEN, fp); if(!pindex) fatalerr("classes_read_ind","pindex = 0","Need class indices not vectors"); if(!(*the_classes = (unsigned char *)malloc(*n * sizeof(unsigned char)))) fatalerr("classes_read_ind", "malloc of classes buffer failed", infile); fread(*the_classes, sizeof(unsigned char), *n, fp); } else fatalerr("classes_read_ind", "illegal ascii-or-binary code", infile); fclose(fp); }