int main(int argc, char *argv[]) { char *me, *fileS; FILE *file; unsigned int llen; NrrdIoState *io; me = argv[0]; if (2 != argc) { /* 0 1 (2) */ fprintf(stderr, "usage: %s <file>\n", me); exit(1); } fileS = argv[1]; if (!( file = myopen(fileS) )) { fprintf(stderr, "%s: couldn't open \"%s\" for reading\n", me, fileS); exit(1); } io = nrrdIoStateNew(); do { if (_nrrdOneLine(&llen, io, file)) { fprintf(stderr, "%s: trouble:\n%s", me, biffGet(NRRD)); exit(1); } if (llen) { printf("%2u |%s|\n", llen, io->line); } } while(llen > 0); nrrdIoStateNix(io); myclose(file); exit(0); }
/* ** Sat Jan 29 16:44:50 EST 2005: this used to "open the seperate ** datafile, and set the FILE* in nio->dataFile, which otherwise will ** stay NULL", but now we support multiple detached data files. So. ** ** The job of this function is to map the "data file" specification to ** one or more filenames that can be passed direction to fopen for ** reading in the data. This involves parsing the various formats for ** identifying multiple data files, and possibly prefixing them with ** nio->path. */ int _nrrdReadNrrdParse_data_file (FILE *ffile, Nrrd *nrrd, NrrdIoState *nio, int useBiff) { char me[]="_nrrdReadNrrdParse_data_file", err[BIFF_STRLEN]; char *info, *nums; unsigned int linelen; int tmp; airArray *mop; mop = airMopNew(); info = airStrdup(nio->line + nio->pos); if (!info) { sprintf(err, "%s: couldn't copy line!", me); biffMaybeAdd(NRRD, err, useBiff); return 1; } airMopAdd(mop, info, airFree, airMopAlways); if (_nrrdContainsPercentDAndMore(info)) { /* ---------------------------------------------------------- */ /* --------- format.%d <min> <max> <step> [<dim>] ----------- */ /* ---------------------------------------------------------- */ nums = info + strcspn(info, _nrrdFieldSep); tmp = strspn(nums, _nrrdFieldSep); nums[0] = 0; /* terminate so that format is now in info */ nums += tmp; if (!( 3 == sscanf(nums, "%d %d %d",&(nio->dataFNMin), &(nio->dataFNMax), &(nio->dataFNStep)) )) { sprintf(err, "%s: couldn't parse three ints (min, max, step) after " "data filename template", me); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } if ( 4 == sscanf(nums, "%d %d %d %u", &(nio->dataFNMin), &(nio->dataFNMax), &(nio->dataFNStep), &(nio->dataFileDim)) ) { if (!( nio->dataFileDim >= 1 && nio->dataFileDim <= nrrd->dim )) { sprintf(err, "%s: datafile dimension %d outside valid range [1,%d]", me, nio->dataFileDim, nrrd->dim); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } } else { nio->dataFileDim = nrrd->dim-1; } if (0 == nio->dataFNStep) { sprintf(err, "%s: file number step must be non-zero", me); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } if ((nio->dataFNMax - nio->dataFNMin)*(nio->dataFNStep) < 0) { sprintf(err, "%s: file number max %d not approached from min %d " "by step %d", me, nio->dataFNMax, nio->dataFNMin, nio->dataFNStep); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } if (!( nio->dataFNFormat = airStrdup(info) )) { sprintf(err, "%s: couldn't copy data filename format", me); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } } else if (!strncmp(info, NRRD_LIST_FLAG, strlen(NRRD_LIST_FLAG))) { /* ---------------------------------------------------------- */ /* ------------------------- LIST --------------------------- */ /* ---------------------------------------------------------- */ if (_nrrdHeaderCheck(nrrd, nio, AIR_TRUE)) { sprintf(err, "%s: NRRD header is incomplete. \"" NRRD_LIST_FLAG "\" data file specification must be contiguous with " "end of header!", me); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } info += strlen(NRRD_LIST_FLAG); if (info[0]) { if (1 == sscanf(info, "%u", &(nio->dataFileDim))) { if (!( nio->dataFileDim >= 1 && nio->dataFileDim <= nrrd->dim )) { sprintf(err, "%s: datafile dimension %d outside valid range [1,%d]", me, nio->dataFileDim, nrrd->dim); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } } else { sprintf(err, "%s: couldn't parse info after \"" NRRD_LIST_FLAG "\" as an int", me); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } } else { /* nothing after NRRD_LIST_FLAG, so dataFileDim is implicit */ nio->dataFileDim = nrrd->dim-1; } /* read in all the datafile names */ do { /* yes, nio->line is re-used/over-written here, but I don't think that's a problem */ if (_nrrdOneLine(&linelen, nio, ffile)) { sprintf(err, "%s: trouble getting file name line", me); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } if (linelen > 0) { tmp = airArrayLenIncr(nio->dataFNArr, 1); nio->dataFN[tmp] = airStrdup(nio->line); } } while (linelen > 0); } else { /* ---------------------------------------------------------- */ /* -------------------- (single filename) ------------------- */ /* ---------------------------------------------------------- */ /* there is apparently only a single detached data file */ tmp = airArrayLenIncr(nio->dataFNArr, 1); nio->dataFN[tmp] = airStrdup(info); nio->dataFileDim = nrrd->dim; } if (_nrrdDataFNCheck(nio, nrrd, useBiff)) { sprintf(err, "%s: trouble with number of datafiles", me); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } airMopOkay(mop); return 0; }