int unrrdu_dataMain(int argc, const char **argv, const char *me, hestParm *hparm) { hestOpt *opt = NULL; char *err, *inS=NULL; Nrrd *nin; NrrdIoState *nio; airArray *mop; int car, pret; mop = airMopNew(); hestOptAdd(&opt, NULL, "nin", airTypeString, 1, 1, &inS, NULL, "input nrrd"); airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways); USAGE(_unrrdu_dataInfoL); PARSE(); airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways); nio = nrrdIoStateNew(); airMopAdd(mop, nio, (airMopper)nrrdIoStateNix, airMopAlways); nio->skipData = AIR_TRUE; nio->keepNrrdDataFileOpen = AIR_TRUE; nin = nrrdNew(); airMopAdd(mop, nin, (airMopper)nrrdNuke, airMopAlways); if (nrrdLoad(nin, inS, nio)) { airMopAdd(mop, err = biffGetDone(NRRD), airFree, airMopAlways); fprintf(stderr, "%s: error reading header:\n%s", me, err); airMopError(mop); return 1; } if (_nrrdDataFNNumber(nio) > 1) { fprintf(stderr, "%s: sorry, currently can't operate with multiple " "detached datafiles\n", me); airMopError(mop); return 1; } if (!( nrrdFormatNRRD == nio->format )) { fprintf(stderr, "%s: can only print data of NRRD format files\n", me); airMopError(mop); return 1; } car = fgetc(nio->dataFile); #ifdef _MSC_VER /* needed because otherwise printing a carraige return will automatically also produce a newline */ _setmode(_fileno(stdout), _O_BINARY); #endif while (EOF != car) { fputc(car, stdout); car = fgetc(nio->dataFile); } airFclose(nio->dataFile); airMopOkay(mop); return 0; }
/* ******** unrrduHestFileCB ** ** for parsing a filename, which means opening it in "rb" mode and ** getting a FILE *. "-" is interpreted as stdin, which is not ** fclose()ed at the end, unlike all other files. */ void * unrrduMaybeFclose(void *_file) { FILE *file; file = (FILE *)_file; if (stdin != file) { file = airFclose(file); } return NULL; }
void undosConvert(char *me, char *name, int reverse, int mac, int quiet, int noAction) { airArray *mop; FILE *fin, *fout; char *data=NULL; airArray *dataArr; unsigned int ci; int car, numBad, willConvert; _undosU uu; mop = airMopNew(); if (!airStrlen(name)) { fprintf(stderr, "%s: empty filename\n", me); airMopError(mop); return; } /* -------------------------------------------------------- */ /* open input file */ fin = airFopen(name, stdin, "rb"); if (!fin) { if (!quiet) { fprintf(stderr, "%s: couldn't open \"%s\" for reading: \"%s\"\n", me, name, strerror(errno)); } airMopError(mop); return; } airMopAdd(mop, fin, (airMopper)airFclose, airMopOnError); /* -------------------------------------------------------- */ /* create buffer */ uu.c = &data; dataArr = airArrayNew(uu.v, NULL, sizeof(char), AIR_STRLEN_HUGE); if (!dataArr) { if (!quiet) { fprintf(stderr, "%s: internal allocation error #1\n", me); } airMopError(mop); return; } airMopAdd(mop, dataArr, (airMopper)airArrayNuke, airMopAlways); /* -------------------------------------------------------- */ /* read input file, testing for binary-ness along the way */ numBad = 0; car = getc(fin); if (EOF == car) { if (!quiet) { fprintf(stderr, "%s: \"%s\" is empty, skipping ...\n", me, name); } airMopError(mop); return; } do { ci = airArrayLenIncr(dataArr, 1); if (!dataArr->data) { if (!quiet) { fprintf(stderr, "%s: internal allocation error #2\n", me); } airMopError(mop); return; } data[ci] = car; numBad += !(isprint(data[ci]) || isspace(data[ci])); car = getc(fin); } while (EOF != car && BAD_PERC > 100.0*numBad/dataArr->len); if (EOF != car) { if (!quiet) { fprintf(stderr, "%s: more than %g%% of \"%s\" is non-printing, " "skipping ...\n", me, BAD_PERC, name); } airMopError(mop); return; } fin = airFclose(fin); /* -------------------------------------------------------- */ /* see if we really need to do anything */ willConvert = AIR_FALSE; if (!strcmp("-", name)) { willConvert = AIR_TRUE; } else if (reverse) { for (ci=0; ci<dataArr->len; ci++) { if (mac) { if (CR == data[ci]) { willConvert = AIR_TRUE; break; } } else { if (CR == data[ci] && (ci && LF != data[ci-1])) { willConvert = AIR_TRUE; break; } } } } else { for (ci=0; ci<dataArr->len; ci++) { if (mac) { if (LF == data[ci]) { willConvert = AIR_TRUE; break; } } else { if (LF == data[ci] && (ci+1<dataArr->len && CR == data[ci+1])) { willConvert = AIR_TRUE; break; } } } } if (!willConvert) { /* no, we don't need to do anything; quietly quit */ airMopOkay(mop); return; } else { if (!quiet) { fprintf(stderr, "%s: %s \"%s\" %s %s ... \n", me, noAction ? "would convert" : "converting", name, reverse ? "to" : "from", mac ? "MAC" : "DOS"); } } if (noAction) { /* just joking, we won't actually write anything. (yes, even if input was stdin) */ airMopOkay(mop); return; } /* -------------------------------------------------------- */ /* open output file */ fout = airFopen(name, stdout, "wb"); if (!fout) { if (!quiet) { fprintf(stderr, "%s: couldn't open \"%s\" for writing: \"%s\"\n", me, name, strerror(errno)); } airMopError(mop); return; } airMopAdd(mop, fout, (airMopper)airFclose, airMopOnError); /* -------------------------------------------------------- */ /* write output file */ car = 'a'; if (reverse) { for (ci=0; ci<dataArr->len; ci++) { if ((mac && CR == data[ci]) || (CR == data[ci] && (ci && LF != data[ci-1]))) { car = putc(LF, fout); if (!mac && EOF != car) { car = putc(CR, fout); } } else { car = putc(data[ci], fout); } } } else { for (ci=0; EOF != car && ci<dataArr->len; ci++) { if ((mac && LF == data[ci]) || (LF == data[ci] && (ci+1<dataArr->len && CR == data[ci+1]))) { car = putc(CR, fout); ci += !mac; } else { car = putc(data[ci], fout); } } } if (EOF == car) { if (!quiet) { fprintf(stderr, "%s: ERROR writing \"%s\" possible data loss !!! " "(sorry)\n", me, name); } } fout = airFclose(fout); airMopOkay(mop); return; }
int main(int argc, char *argv[]) { char *me, *outS; hestOpt *hopt; hestParm *hparm; airArray *mop; int fidx, aidx, num, frames; double psc, width[2], arrowWidth, lineWidth, angle, seglen, x0, y0, x1, y1, cc, ss; wheelPS wps; char filename[AIR_STRLEN_MED]; me = argv[0]; mop = airMopNew(); hparm = hestParmNew(); hparm->elideMultipleNonExistFloatDefault = AIR_TRUE; hopt = NULL; airMopAdd(mop, hparm, (airMopper)hestParmFree, airMopAlways); hestOptAdd(&hopt, "w", "arrowWidth lineWidth", airTypeDouble, 2, 2, width, "1.0 0.2", "widths"); hestOptAdd(&hopt, "n", "number", airTypeInt, 1, 1, &num, "10", "number of arrows"); hestOptAdd(&hopt, "f", "frames", airTypeInt, 1, 1, &frames, "10", "number of frames"); hestOptAdd(&hopt, "psc", "scale", airTypeDouble, 1, 1, &psc, "200", "scaling from world space to PostScript points"); hestOptAdd(&hopt, "o", "prefix", airTypeString, 1, 1, &outS, NULL, "prefix of file names"); hestParseOrDie(hopt, argc-1, argv+1, hparm, me, interInfo, AIR_TRUE, AIR_TRUE, AIR_TRUE); airMopAdd(mop, hopt, (airMopper)hestOptFree, airMopAlways); airMopAdd(mop, hopt, (airMopper)hestParseFree, airMopAlways); for (fidx=0; fidx<frames; fidx++) { sprintf(filename, "%s%03d.eps", outS, fidx); if (!(wps.file = airFopen(filename, stdout, "wb"))) { fprintf(stderr, "%s: couldn't open output file\n", me); airMopError(mop); return 1; } lineWidth = width[0]; arrowWidth = width[1]; wps.psc = psc; ELL_4V_SET(wps.bbox, -0.45, -0.85, 1.1, 0.85); wheelPreamble(&wps); fprintf(wps.file, "0 setlinecap\n"); wheelGray(&wps, 0.4); wheelWidth(&wps, lineWidth); x0 = 0; y0 = 0; seglen = 1.0/num; angle = AIR_AFFINE(0, fidx, frames, 0, 2*AIR_PI); for (aidx=1; aidx<=num; aidx++) { cc = cos(angle*aidx)*seglen; ss = sin(angle*aidx)*seglen; x1 = x0 + 0.90*cc; y1 = y0 + 0.90*ss; wheelArrow(&wps, x0, y0, x1, y1, arrowWidth, arrowWidth*0.4); x0 += cc; y0 += ss; } wheelGray(&wps, 0.0); wheelArrow(&wps, 0, 0, x0, y0, arrowWidth, arrowWidth*0.4); wheelEpilog(&wps); airFclose(wps.file); } airMopOkay(mop); exit(0); }