int _mossHestTransformParse (void *ptr, char *_str, char err[AIR_STRLEN_HUGE]) { char me[]="_mossHestTransformParse", *str; double **matP, tx, ty, sx, sy, angle, mat[6], shf, sha; airArray *mop; if (!(ptr && _str)) { sprintf(err, "%s: got NULL pointer", me); return 1; } matP = (double **)ptr; mop = airMopNew(); *matP = (double *)calloc(6, sizeof(double)); airMopMem(mop, matP, airMopOnError); str = airToLower(airStrdup(_str)); airMopMem(mop, &str, airMopAlways); if (!strcmp("identity", str)) { mossMatIdentitySet(*matP); } else if (1 == sscanf(str, "flip:%lf", &angle)) { mossMatFlipSet(*matP, angle); } else if (2 == sscanf(str, "translate:%lf,%lf", &tx, &ty)) { mossMatTranslateSet(*matP, tx, ty); } else if (2 == sscanf(str, "t:%lf,%lf", &tx, &ty)) { mossMatTranslateSet(*matP, tx, ty); } else if (1 == sscanf(str, "rotate:%lf", &angle)) { mossMatRotateSet(*matP, angle); } else if (1 == sscanf(str, "r:%lf", &angle)) { mossMatRotateSet(*matP, angle); } else if (2 == sscanf(str, "scale:%lf,%lf", &sx, &sy)) { mossMatScaleSet(*matP, sx, sy); } else if (2 == sscanf(str, "s:%lf,%lf", &sx, &sy)) { mossMatScaleSet(*matP, sx, sy); } else if (2 == sscanf(str, "shear:%lf,%lf", &shf, &sha)) { mossMatShearSet(*matP, shf, sha); } else if (6 == sscanf(str, "%lf,%lf,%lf,%lf,%lf,%lf", mat+0, mat+1, mat+2, mat+3, mat+4, mat+5)) { MOSS_MAT_COPY(*matP, mat); } else { sprintf(err, "%s: couldn't parse \"%s\" as a transform", me, _str); airMopError(mop); return 1; } airMopOkay(mop); return 0; }
unsigned int airParseStrS(char **out, const char *_s, const char *ct, unsigned int n, ...) { unsigned int i; int greedy; char *tmp, *s, *last; airArray *mop; va_list ap; /* grab "greedy" every time, prior to error checking */ va_start(ap, n); greedy = va_arg(ap, int); va_end(ap); /* if we got NULL, there's nothing to do */ if (!(out && _s && ct)) return 0; mop = airMopNew(); /* copy the input so that we don't change it */ s = airStrdup(_s); airMopMem(mop, &s, airMopAlways); /* keep calling airStrtok() until we have everything */ for (i=0; i<n; i++) { /* if n == 1, then with greediness, the whole string is used, and without greediness, we use airStrtok() to get only the first part of it */ if (n > 1 || !greedy) { tmp = airStrtok(i ? NULL : s, ct, &last); } else { tmp = s; } if (!tmp) { airMopError(mop); return i; } out[i] = airStrdup(tmp); if (!out[i]) { airMopError(mop); return i; } airMopMem(mop, out+i, airMopOnError); } airMopOkay(mop); return n; }
unsigned int airParseStrE(int *out, const char *_s, const char *ct, unsigned int n, ...) { unsigned int i; char *tmp, *s, *last; airArray *mop; va_list ap; airEnum *enm; /* grab the enum every time, prior to error checking */ va_start(ap, n); enm = va_arg(ap, airEnum *); va_end(ap); /* if we got NULL, there's nothing to do */ if (!(out && _s && ct)) { return 0; } mop = airMopNew(); /* copy the input so that we don't change it */ s = airStrdup(_s); airMopMem(mop, &s, airMopAlways); if (1 == n) { /* Because it should be permissible to have spaces in what is intended to be only a single string from an airEnum, we treat 1==n differently, and do NOT use airStrtok to tokenize the input string s into spaces. We check the whole s string */ out[0] = airEnumVal(enm, s); if (airEnumUnknown(enm) == out[0]) { airMopError(mop); return 0; } } else { /* keep calling airStrtok() until we have everything */ for (i=0; i<n; i++) { tmp = airStrtok(i ? NULL : s, ct, &last); if (!tmp) { airMopError(mop); return i; } out[i] = airEnumVal(enm, tmp); if (airEnumUnknown(enm) == out[i]) { airMopError(mop); return i; } } } airMopOkay(mop); return n; }
/* ******** unrrduHestEncodingCB ** ** for parsing output encoding, including compression flags ** enc[0]: which encoding, from nrrdEncodingType* enum ** enc[1]: for compressions: zlib "level" and bzip2 "blocksize" ** enc[2]: for zlib: strategy, from nrrdZlibStrategy* enum */ int unrrduParseEncoding(void *ptr, char *_str, char err[AIR_STRLEN_HUGE]) { char me[]="unrrduParseEncoding", *str, *opt; int *enc; airArray *mop; if (!(ptr && _str)) { sprintf(err, "%s: got NULL pointer", me); return 1; } enc = (int *)ptr; /* these are the defaults, they may not get over-written */ enc[1] = -1; enc[2] = nrrdZlibStrategyDefault; enc[0] = airEnumVal(nrrdEncodingType, _str); if (nrrdEncodingTypeUnknown != enc[0]) { /* we're done; encoding was simple: "raw" or "gz" */ return 0; } mop = airMopNew(); str = airStrdup(_str); airMopMem(mop, &str, airMopAlways); opt = strchr(str, ':'); if (!opt) { /* couldn't parse string as nrrdEncodingType, but there wasn't a colon */ sprintf(err, "%s: didn't recognize \"%s\" as an encoding", me, str); airMopError(mop); return 1; } else { *opt = '\0'; opt++; enc[0] = airEnumVal(nrrdEncodingType, str); if (nrrdEncodingTypeUnknown == enc[0]) { sprintf(err, "%s: didn't recognize \"%s\" as an encoding", me, str); airMopError(mop); return 1; } if (!nrrdEncodingArray[enc[0]]->isCompression) { sprintf(err, "%s: only compression encodings have parameters", me); airMopError(mop); return 1; } while (*opt) { if (isdigit(*opt)) { enc[1] = *opt - '0'; } else if ('d' == tolower(*opt)) { enc[2] = nrrdZlibStrategyDefault; } else if ('h' == tolower(*opt)) { enc[2] = nrrdZlibStrategyHuffman; } else if ('f' == tolower(*opt)) { enc[2] = nrrdZlibStrategyFiltered; } else { sprintf(err, "%s: parameter char \"%c\" not a digit or 'd','h','f'", me, *opt); airMopError(mop); return 1; } opt++; } } airMopOkay(mop); return 0; }
int main(int argc, const char *argv[]) { airArray *mop; hestOpt *hopt=NULL; int i, N, M, P, yes, *year; unsigned int E; const char *me; double crct; me = argv[0]; mop = airMopNew(); hestOptAdd(&hopt, "N", "days", airTypeInt, 1, 1, &N, "365", "# of days in year"); /* E != P */ hestOptAdd(&hopt, "E", "exps", airTypeInt, 1, 1, &P, "100000", "number of experiments after which to print out newly " "computed probability"); hestOptAdd(&hopt, NULL, "people", airTypeInt, 1, 1, &M, NULL, "# of people in room"); hestParseOrDie(hopt, argc-1, argv+1, NULL, me, info, AIR_TRUE, AIR_TRUE, AIR_TRUE); airMopAdd(mop, hopt, (airMopper)hestOptFree, airMopAlways); airMopAdd(mop, hopt, (airMopper)hestParseFree, airMopAlways); if (!( N > 1 && M > 0 && P > 1)) { fprintf(stderr, "%s: need both M, P all > 1, M > 0\n", me); airMopError(mop); exit(1); } if (!(year = (int*)calloc(N, sizeof(int)))) { fprintf(stderr, "%s: couldn't calloc(%d,sizeof(int))\n", me, N); airMopError(mop); exit(1); } airMopMem(mop, year, airMopAlways); crct = 1; for (i=0; i<M; i++) { crct *= (double)(N-i)/N; } crct = 1-crct; yes = 0; E = 1; airSrandMT((int)airTime()); while (E) { yes += runexp(year, N, M); if (!(E % P)) { printf("P = %10d/%10d = %22.20f =?= %22.20f\n", yes, E, (double)yes/E, crct); } E++; } airMopOkay(mop); exit(0); }
/* ** _mossHestOriginParse() ** ** parse an origin specification ** p(x,y): absolute pixel position --> val[3] = (0,x,y) ** u(x,y): position in unit box [0,1]x[0,1] --> val[3] = (1,x,y) */ int _mossHestOriginParse (void *ptr, char *str, char err[AIR_STRLEN_HUGE]) { char me[]="_mossHestOriginParse"; double **valP; airArray *mop; valP = (double **)ptr; mop = airMopNew(); *valP = (double *)calloc(3, sizeof(double)); airMopMem(mop, valP, airMopOnError); if (2 == sscanf(str, "p:%lf,%lf", *valP + 1, *valP + 2)) { (*valP)[0] = 0; } else if (2 == sscanf(str, "u:%lf,%lf", *valP + 1, *valP + 2)) { (*valP)[0] = 1; } else { sprintf(err, "%s: couldn't parse \"%s\" as origin", me, str); airMopError(mop); return 1; } airMopOkay(mop); return 0; }
int main(int argc, char *argv[]) { void *ptr; int i = 111, j = 222, k = 333, l = 444; airArray *mop; char *str, *me; AIR_UNUSED(argc); me = argv[0]; printf("%s: -------------------------------------\n", me); mop = airMopNew(); str = airStrdup("this is just a test"); printf("%s: str = \"%s\", str = 0x%p, &str = 0x%p\n", me, str, str, AIR_CAST(void*, &str)); airMopMem(mop, &str, airMopAlways); airMopDebug(mop); airMopOkay(mop); printf("%s: -------------------------------------\n", me); mop = airMopNew(); ptr = calloc(1024, sizeof(char)); airMopMem(mop, &ptr, airMopAlways); airMopAdd(mop, &i, print, airMopNever); airMopAdd(mop, &j, print, airMopOnError); airMopAdd(mop, &k, print, airMopOnOkay); airMopAdd(mop, &l, print, airMopAlways); airMopPrint(mop, "this is a joke", airMopOnError); airMopDebug(mop); airMopError(mop); printf("%s: -------------------------------------\n", me); mop = airMopNew(); ptr = calloc(1024, sizeof(char)); airMopMem(mop, &ptr, airMopAlways); airMopAdd(mop, &i, print, airMopNever); airMopAdd(mop, &j, print, airMopOnError); airMopAdd(mop, &k, print, airMopOnOkay); airMopPrint(mop, "this is a joke", airMopOnError); airMopAdd(mop, &l, print, airMopAlways); airMopDebug(mop); airMopOkay(mop); printf("%s: -------------------------------------\n", me); mop = airMopNew(); ptr = calloc(1024, sizeof(char)); airMopMem(mop, &ptr, airMopAlways); free(ptr); airMopUnMem(mop, &ptr); airMopDebug(mop); airMopOkay(mop); printf("%s: -------------------------------------\n", me); exit(0); }
int main(int argc, char **argv) { int i, ret; char *me, *argv0 = NULL, *err; hestParm *hparm; airArray *mop; me = argv[0]; /* parse environment variables first, in case they break nrrdDefault* or nrrdState* variables in a way that nrrdSanity() should see */ nrrdDefaultGetenv(); nrrdStateGetenv(); /* no harm done in making sure we're sane */ if (!nrrdSanity()) { fprintf(stderr, "******************************************\n"); fprintf(stderr, "******************************************\n"); fprintf(stderr, "\n"); fprintf(stderr, " %s: nrrd sanity check FAILED.\n", me); fprintf(stderr, "\n"); fprintf(stderr, " This means that either nrrd can't work on this " "platform, or (more likely)\n"); fprintf(stderr, " there was an error in the compilation options " "and variable definitions\n"); fprintf(stderr, " for how Teem was built here.\n"); fprintf(stderr, "\n"); fprintf(stderr, " %s\n", err = biffGetDone(NRRD)); fprintf(stderr, "\n"); fprintf(stderr, "******************************************\n"); fprintf(stderr, "******************************************\n"); free(err); return 1; } mop = airMopNew(); hparm = hestParmNew(); airMopAdd(mop, hparm, (airMopper)hestParmFree, airMopAlways); hparm->elideSingleEnumType = AIR_TRUE; hparm->elideSingleOtherType = AIR_TRUE; hparm->elideSingleOtherDefault = AIR_FALSE; hparm->elideSingleNonExistFloatDefault = AIR_TRUE; hparm->elideMultipleNonExistFloatDefault = AIR_TRUE; hparm->elideSingleEmptyStringDefault = AIR_TRUE; hparm->elideMultipleEmptyStringDefault = AIR_TRUE; hparm->cleverPluralizeOtherY = AIR_TRUE; hparm->columns = 78; /* if there are no arguments, then we give general usage information */ if (1 >= argc) { tendUsage(TEND, hparm); airMopError(mop); exit(1); } /* else, we see if its --version */ if (!strcmp("--version", argv[1])) { printf("Teem version %s (%s)\n", airTeemVersion, airTeemReleaseDate); exit(0); } /* else, we should see if they're asking for a command we know about */ for (i=0; tendCmdList[i]; i++) { if (!strcmp(argv[1], tendCmdList[i]->name)) break; if (!strcmp("--help", argv[1]) && !strcmp("about", tendCmdList[i]->name)) { break; } } if (tendCmdList[i]) { /* yes, we have that command */ /* initialize variables used by the various commands */ argv0 = (char *)calloc(strlen(TEND) + strlen(argv[1]) + 2, sizeof(char)); airMopMem(mop, &argv0, airMopAlways); sprintf(argv0, "%s %s", TEND, argv[1]); /* run the individual unu program, saving its exit status */ ret = tendCmdList[i]->main(argc-2, argv+2, argv0, hparm); } else { fprintf(stderr, "%s: unrecognized command: \"%s\"; type \"%s\" for " "complete list\n", me, argv[1], me); ret = 1; } airMopDone(mop, ret); return ret; }
int nrrdHistoDraw(Nrrd *nout, const Nrrd *nin, size_t sy, int showLog, double max) { static const char me[]="nrrdHistoDraw", func[]="dhisto"; char cmt[AIR_STRLEN_MED]; unsigned int k, sx, x, y, maxhitidx, E, numticks, *Y, *logY, tick, *ticks; double hits, maxhits, usemaxhits; unsigned char *pgmData; airArray *mop; if (!(nin && nout && sy > 0)) { biffAddf(NRRD, "%s: invalid args", me); return 1; } if (nout == nin) { biffAddf(NRRD, "%s: nout==nin disallowed", me); return 1; } if (nrrdHistoCheck(nin)) { biffAddf(NRRD, "%s: input nrrd not a histogram", me); return 1; } sx = AIR_CAST(unsigned int, nin->axis[0].size); nrrdBasicInfoInit(nout, NRRD_BASIC_INFO_DATA_BIT); if (nrrdPGM(nout, sx, sy)) { biffAddf(NRRD, "%s: failed to allocate histogram image", me); return 1; } /* perhaps I should be using nrrdAxisInfoCopy */ nout->axis[0].spacing = nout->axis[1].spacing = AIR_NAN; nout->axis[0].thickness = nout->axis[1].thickness = AIR_NAN; nout->axis[0].min = nin->axis[0].min; nout->axis[0].max = nin->axis[0].max; nout->axis[0].center = nout->axis[1].center = nrrdCenterCell; nout->axis[0].label = (char *)airStrdup(nin->axis[0].label); nout->axis[1].label = (char *)airFree(nout->axis[1].label); pgmData = (unsigned char *)nout->data; maxhits = maxhitidx = 0; for (x=0; x<sx; x++) { hits = nrrdDLookup[nin->type](nin->data, x); if (maxhits < hits) { maxhits = hits; maxhitidx = x; } } if (AIR_EXISTS(max) && max > 0) { usemaxhits = max; } else { usemaxhits = maxhits; } nout->axis[1].min = usemaxhits; nout->axis[1].max = 0; numticks = (unsigned int)log10(usemaxhits + 1); mop = airMopNew(); ticks = (unsigned int*)calloc(numticks, sizeof(unsigned int)); airMopMem(mop, &ticks, airMopAlways); Y = (unsigned int*)calloc(sx, sizeof(unsigned int)); airMopMem(mop, &Y, airMopAlways); logY = (unsigned int*)calloc(sx, sizeof(unsigned int)); airMopMem(mop, &logY, airMopAlways); if (!(ticks && Y && logY)) { biffAddf(NRRD, "%s: failed to allocate temp arrays", me); airMopError(mop); return 1; } for (k=0; k<numticks; k++) { ticks[k] = airIndex(0, log10(pow(10,k+1) + 1), log10(usemaxhits+1), AIR_CAST(unsigned int, sy)); } for (x=0; x<sx; x++) { hits = nrrdDLookup[nin->type](nin->data, x); Y[x] = airIndex(0, hits, usemaxhits, AIR_CAST(unsigned int, sy)); logY[x] = airIndex(0, log10(hits+1), log10(usemaxhits+1), AIR_CAST(unsigned int, sy)); /* printf("%d -> %d,%d", x, Y[x], logY[x]); */ } for (y=0; y<sy; y++) { tick = 0; for (k=0; k<numticks; k++) tick |= ticks[k] == y; for (x=0; x<sx; x++) { pgmData[x + sx*(sy-1-y)] = (2 == showLog /* HACK: draw log curve, but not log tick marks */ ? (y >= logY[x] ? 0 /* above log curve */ : (y >= Y[x] ? 128 /* below log curve, above normal curve */ : 255 /* below log curve, below normal curve */ ) ) : (!showLog ? (y >= Y[x] ? 0 : 255) : (y >= logY[x] /* above log curve */ ? (!tick ? 0 /* not on tick mark */ : 255) /* on tick mark */ : (y >= Y[x] /* below log curve, above normal curve */ ? (!tick ? 128 /* not on tick mark */ : 0) /* on tick mark */ :255 /* below log curve, below normal curve */ ) ) ) ); } } E = AIR_FALSE; sprintf(cmt, "min value: %g\n", nout->axis[0].min); if (!E) E |= nrrdCommentAdd(nout, cmt); sprintf(cmt, "max value: %g\n", nout->axis[0].max); if (!E) E |= nrrdCommentAdd(nout, cmt); sprintf(cmt, "max hits: %g, in bin %d, around value %g\n", maxhits, maxhitidx, nrrdAxisInfoPos(nout, 0, maxhitidx)); if (!E) E |= nrrdCommentAdd(nout, cmt); if (!E) E |= nrrdContentSet_va(nout, func, nin, "%d", sy); if (E) { biffAddf(NRRD, "%s:", me); airMopError(mop); return 1; } /* bye */ airMopOkay(mop); return 0; }
int main(int argc, char **argv) { int i, ret; char *me, *argv0 = NULL, *err; hestParm *hparm; airArray *mop; me = argv[0]; /* no harm done in making sure we're sane */ if (!nrrdSanity()) { fprintf(stderr, "******************************************\n"); fprintf(stderr, "******************************************\n"); fprintf(stderr, "\n"); fprintf(stderr, " %s: nrrd sanity check FAILED.\n", me); fprintf(stderr, "\n"); fprintf(stderr, " This means that either nrrd can't work on this " "platform, or (more likely)\n"); fprintf(stderr, " there was an error in the compilation options " "and variable definitions\n"); fprintf(stderr, " for Teem.\n"); fprintf(stderr, "\n"); fprintf(stderr, " %s\n", err = biffGetDone(NRRD)); fprintf(stderr, "\n"); fprintf(stderr, "******************************************\n"); fprintf(stderr, "******************************************\n"); free(err); return 1; } mop = airMopNew(); hparm = hestParmNew(); airMopAdd(mop, hparm, (airMopper)hestParmFree, airMopAlways); hparm->elideSingleEnumType = AIR_TRUE; hparm->elideSingleOtherType = AIR_TRUE; hparm->elideSingleOtherDefault = AIR_FALSE; hparm->elideSingleNonExistFloatDefault = AIR_TRUE; hparm->elideMultipleNonExistFloatDefault = AIR_TRUE; hparm->elideSingleEmptyStringDefault = AIR_TRUE; hparm->elideMultipleEmptyStringDefault = AIR_TRUE; hparm->cleverPluralizeOtherY = AIR_TRUE; hparm->columns = 78; /* if there are no arguments, then we give general usage information */ if (1 >= argc) { baneGkmsUsage(GKMS, hparm); airMopError(mop); exit(1); } /* else, we should see if they're asking for a command we know about */ /* baneGkmsCmdList[] is NULL-terminated */ for (i=0; baneGkmsCmdList[i]; i++) { if (!strcmp(argv[1], baneGkmsCmdList[i]->name)) break; } if (baneGkmsCmdList[i]) { /* yes, we have that command */ /* initialize variables used by the various commands */ argv0 = AIR_CAST(char*, malloc(strlen(GKMS) + strlen(argv[1]) + 2)); airMopMem(mop, &argv0, airMopAlways); sprintf(argv0, "%s %s", GKMS, argv[1]); /* run the individual unu program, saving its exit status */ ret = baneGkmsCmdList[i]->main(argc-2, argv+2, argv0, hparm); if (1 == ret) { airMopAdd(mop, err=biffGetDone(BANE), airFree, airMopAlways); fprintf(stderr, "%s: error:\n%s", argv0, err); } else if (2 == ret) { /* gkms command has already handled printing error messages */ ret = 1; } } else {
int main(int argc, char **argv) { int i, ret; char *me, *argv0 = NULL, *err; hestParm *hparm; airArray *mop; me = argv[0]; /* parse environment variables first, in case they break nrrdDefault* or nrrdState* variables in a way that nrrdSanity() should see */ nrrdDefaultGetenv(); nrrdStateGetenv(); /* if user hasn't tried to set nrrdStateKindNoop by an environment variable, we set it to false, since its probably what people expect */ if (!getenv("NRRD_STATE_KIND_NOOP")) { nrrdStateKindNoop = AIR_FALSE; } /* no harm done in making sure we're sane */ if (!nrrdSanity()) { fprintf(stderr, "******************************************\n"); fprintf(stderr, "******************************************\n"); fprintf(stderr, "\n"); fprintf(stderr, " %s: nrrd sanity check FAILED.\n", me); fprintf(stderr, "\n"); fprintf(stderr, " This means that either nrrd can't work on this " "platform, or (more likely)\n"); fprintf(stderr, " there was an error in the compilation options " "and variable definitions\n"); fprintf(stderr, " for how Teem was built here.\n"); fprintf(stderr, "\n"); fprintf(stderr, " %s\n", err = biffGetDone(NRRD)); fprintf(stderr, "\n"); fprintf(stderr, "******************************************\n"); fprintf(stderr, "******************************************\n"); free(err); return 1; } mop = airMopNew(); hparm = hestParmNew(); airMopAdd(mop, hparm, (airMopper)hestParmFree, airMopAlways); hparm->elideSingleEnumType = AIR_TRUE; hparm->elideSingleOtherType = AIR_TRUE; hparm->elideSingleOtherDefault = AIR_TRUE; hparm->elideSingleNonExistFloatDefault = AIR_TRUE; hparm->elideMultipleNonExistFloatDefault = AIR_TRUE; hparm->elideSingleEmptyStringDefault = AIR_TRUE; hparm->elideMultipleEmptyStringDefault = AIR_TRUE; hparm->columns = unrrduDefNumColumns; /* if there are no arguments, then we give general usage information */ if (1 >= argc) { unrrduUsage("unu", hparm); airMopError(mop); exit(1); } /* else, we should see if they're asking for a command we know about */ for (i=0; unrrduCmdList[i]; i++) { if (!strcmp(argv[1], unrrduCmdList[i]->name)) break; } /* unrrduCmdList[] is NULL-terminated */ if (unrrduCmdList[i]) { /* yes, we have that command */ /* initialize variables used by the various commands */ argv0 = (char *)calloc(strlen(UNU) + strlen(argv[1]) + 2, sizeof(char)); airMopMem(mop, &argv0, airMopAlways); sprintf(argv0, "%s %s", UNU, argv[1]); /* run the individual unu program, saving its exit status */ ret = unrrduCmdList[i]->main(argc-2, argv+2, argv0, hparm); } else { fprintf(stderr, "%s: unrecognized command: \"%s\"; type \"%s\" for " "complete list\n", me, argv[1], me); ret = 1; } airMopDone(mop, ret); return ret; }