/* ******** nrrdKeyValueAdd ** ** This will COPY the given strings, and so does not depend on ** them existing past the return of this function ** ** NOTE: Despite what might be most logical, there is no effort made ** here to cleanup key or value, including any escaping or filtering ** that might be warranted for white space other than \n ** ** does NOT use BIFF */ int nrrdKeyValueAdd(Nrrd *nrrd, const char *key, const char *value) { unsigned int ki; int found; if (!( nrrd && key && value )) { /* got NULL pointer */ return 1; } if (!strlen(key)) { /* reject empty keys */ return 1; } ki = _kvpIdxFind(nrrd, key, &found); if (found) { /* over-writing value for an existing key, so have to free old value */ airFree(nrrd->kvp[1 + 2*ki]); nrrd->kvp[1 + 2*ki] = airStrdup(value); } else { /* adding value for a new key */ ki = airArrayLenIncr(nrrd->kvpArr, 1); nrrd->kvp[0 + 2*ki] = airStrdup(key); nrrd->kvp[1 + 2*ki] = airStrdup(value); } 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; }
tkwbSlide * tkwbSlideNew(char *title, char *image, char *text) { tkwbSlide *ret; ret = (tkwbSlide*)calloc(1, sizeof(tkwbSlide)); if (ret) { ret->title = airStrdup(title); ret->image = airStrdup(image); ret->text = airStrdup(text); } return ret; }
int baneGkms_miteMain(int argc, const char **argv, const char *me, hestParm *hparm) { hestOpt *opt = NULL; char *out, *perr; Nrrd *nin, *nout; airArray *mop; int pret, E; hestOptAdd(&opt, "i", "opacIn", airTypeOther, 1, 1, &nin, NULL, "input opacity function (1 or 2 dimensional), from " "\"gkms opac\"", NULL, NULL, nrrdHestNrrd); hestOptAdd(&opt, "o", "opacOut", airTypeString, 1, 1, &out, NULL, "output opacity function filename"); mop = airMopNew(); airMopAdd(mop, opt, (airMopper)hestOptFree, airMopAlways); USAGE(_baneGkms_miteInfoL); PARSE(); airMopAdd(mop, opt, (airMopper)hestParseFree, airMopAlways); if (1 == nin->axis[0].size && nin->axis[0].label && !strcmp("A", nin->axis[0].label)) { fprintf(stderr, "%s: already\n", me); nout = nin; } else { nout = nrrdNew(); airMopAdd(mop, nout, (airMopper)nrrdNuke, airMopAlways); E = 0; if (!E) E |= nrrdAxesInsert(nout, nin, 0); if (!E) E |= !(nout->axis[0].label = airStrdup("A")); if (!E) E |= !(nout->axis[1].label = airStrdup("gage(v)")); if (3 == nout->dim) { if (!E) E |= !(nout->axis[2].label = airStrdup("gage(gm)")); } if (E) { biffMovef(BANE, NRRD, "%s: trouble modifying opacity function nrrd", me); airMopError(mop); return 1; } } if (nrrdSave(out, nout, NULL)) { biffMovef(BANE, NRRD, "%s: trouble saving opacity function", me); airMopError(mop); return 1; } airMopOkay(mop); return 0; }
/* ** _nrrdContentGet ** ** ALLOCATES a string for the content of a given nrrd ** panics if allocation failed */ char * _nrrdContentGet(const Nrrd *nin) { static const char me[]="_nrrdContentGet"; char *ret; ret = ((nin && nin->content) ? airStrdup(nin->content) : airStrdup(nrrdStateUnknownContent)); if (!ret) { fprintf(stderr, "%s: PANIC: content strdup failed!\n", me); return NULL; } return ret; }
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; }
unsigned int airParseStrC(char *out, const char *_s, const char *ct, unsigned int n, ...) { unsigned int i; char *tmp, *s, *last; /* if we got NULL, there's nothing to do */ if (!(out && _s && ct)) return 0; /* copy the input so that we don't change it */ s = airStrdup(_s); /* keep calling airStrtok() until we have everything */ for (i=0; i<n; i++) { tmp = airStrtok(i ? NULL : s, ct, &last); if (!tmp) { free(s); return i; } out[i] = tmp[0]; } free(s); return n; }
int _nrrdReadNrrdParse_keyvalue (FILE *file, Nrrd *nrrd, NrrdIoState *nio, int useBiff) { char me[]="_nrrdReadNrrdParse_keyvalue", err[BIFF_STRLEN]; char *keysep, *line, *key, *value; AIR_UNUSED(file); /* we know this will find something */ line = airStrdup(nio->line); if (!line) { sprintf(err, "%s: can't allocate parse line", me); biffMaybeAdd(NRRD, err, useBiff); return 1; } keysep = strstr(line, ":="); if (!keysep) { sprintf(err, "%s: didn't see \":=\" key/value delimiter in \"%s\"", me, line); free(line); biffMaybeAdd(NRRD, err, useBiff); return 1; } keysep[0] = 0; keysep[1] = 0; key = line; value = keysep+2; /* convert escape sequences */ airUnescape(key); airUnescape(value); nrrdKeyValueAdd(nrrd, key, value); free(line); return 0; }
biffMsg * biffMsgNew(const char *key) { static const char me[]="biffMsgNew"; biffMsg *msg; if (!key) { fprintf(stderr, "%s: PANIC got NULL key\n", me); return NULL; /* exit(1); */ } msg = AIR_CALLOC(1, biffMsg); if (msg) { airPtrPtrUnion appu; msg->key = airStrdup(key); msg->err = NULL; msg->errNum = 0; appu.cp = &(msg->err); msg->errArr = airArrayNew(appu.v, &(msg->errNum), sizeof(char*), _MSG_INCR); if (msg->errArr) { airArrayPointerCB(msg->errArr, NULL, airFree); } } if (!( msg && msg->key && msg->errArr )) { fprintf(stderr, "%s: PANIC couldn't calloc new msg\n", me); return NULL; /* exit(1); */ } return msg; }
/* ** _biffAddErr() ** ** adds a given message to the given entry. The message is processed to ** convert all whitespace into ' ', and to eliminate whitespace at the ** end of the message. ** panics and exit(1)s if there is a problem */ void _biffAddErr(_biffEntry *e, const char *err) { char *buf, me[]="_biffAddErr"; size_t ii, len; /* printf("%s: HEY(before): err[%s]->num = %d\n", me, e->key, e->num); */ airArrayLenIncr(e->AA, 1); if (!e->AA->data) { fprintf(stderr, "%s: PANIC: couldn't add message for key %s\n", me, e->key); exit(1); } /* printf("%s: HEY(after): err[%s]->num = %d\n", me, e->key, e->num); */ buf = airStrdup(err); len = strlen(buf); for (ii=0; ii<=len-1; ii++) { if (isspace(buf[ii])) { buf[ii] = ' '; } } ii = len-1; while (isspace(buf[ii])) { buf[ii--] = 0; } /* printf("%s: HEY(after): err[%s]->num = %d\n", me, e->key, e->num); */ /* printf("%s: HEY: err[%s][%d] now \"%s\"\n", me, e->key, e->num-1, buf); */ e->err[e->num-1] = buf; return; }
/* ******** 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; }
gageKind * _meetGageKindParse(const char *_str, int constOnly) { char *str; gageKind *ret; if (!_str) { return NULL; } str = airToLower(airStrdup(_str)); if (!str) { return NULL; } if (!strcmp(gageKindScl->name, str)) { ret = gageKindScl; } else if (!strcmp(gageKindVec->name, str)) { ret = gageKindVec; } else if (!strcmp(tenGageKind->name, str)) { ret = tenGageKind; } else if (!constOnly && !strcmp(TEN_DWI_GAGE_KIND_NAME, str)) { ret = tenDwiGageKindNew(); } else { ret = NULL; } airFree(str); return ret; }
gageKind * pullGageKindParse(const char *_str) { char me[]="pullGageKindParse", err[BIFF_STRLEN], *str; gageKind *ret; airArray *mop; if (!_str) { sprintf(err, "%s: got NULL pointer", me); return NULL; } mop = airMopNew(); str = airStrdup(_str); airMopAdd(mop, str, airFree, airMopAlways); airToLower(str); if (!strcmp(gageKindScl->name, str)) { ret = gageKindScl; } else if (!strcmp(gageKindVec->name, str)) { ret = gageKindVec; } else if (!strcmp(tenGageKind->name, str)) { ret = tenGageKind; } else /* not allowing DWIs for now */ { sprintf(err, "%s: not \"%s\", \"%s\", or \"%s\"", me, gageKindScl->name, gageKindVec->name, tenGageKind->name); airMopError(mop); return NULL; } airMopOkay(mop); return ret; }
int dyeColorParse(dyeColor *col, char *_str) { char me[]="dyeColorParse", err[128], *str; char *colon, *valS; float v0, v1, v2; int spc; if (!(col && _str)) { sprintf(err, "%s: got NULL pointer", me); biffAdd(DYE, err); return 1; } if (!(str = airStrdup(_str))) { sprintf(err, "%s: couldn't strdup!", me); biffAdd(DYE, err); return 1; } if (!(colon = strchr(str, ':'))) { sprintf(err, "%s: given string \"%s\" didn't contain colon", me, str); biffAdd(DYE, err); return 1; } *colon = '\0'; valS = colon+1; if (3 != sscanf(valS, "%g,%g,%g", &v0, &v1, &v2)) { sprintf(err, "%s: couldn't parse three floats from \"%s\"", me, valS); biffAdd(DYE, err); return 1; } spc = dyeStrToSpace(str); if (dyeSpaceUnknown == spc) { sprintf(err, "%s: couldn't parse colorspace from \"%s\"", me, str); biffAdd(DYE, err); return 1; } str = (char *)airFree(str); dyeColorSet(col, spc, v0, v1, v2); return 0; }
char * _nrrdGetQuotedString(char **hP, int useBiff) { char me[]="_nrrdGetQuotedString", err[BIFF_STRLEN], *h, *buff, *ret; airArray *buffArr; int pos; _chpu uu; h = *hP; /* skip past space */ /* printf("!%s: h |%s|\n", me, h);*/ h += strspn(h, _nrrdFieldSep); /* printf("!%s: h |%s|\n", me, h);*/ /* make sure we have something */ if (!*h) { sprintf(err, "%s: hit end of string before seeing opening \"", me); biffMaybeAdd(NRRD, err, useBiff); return NULL; } /* make sure we have a starting quote */ if ('"' != *h) { sprintf(err, "%s: didn't start with \"", me); biffMaybeAdd(NRRD, err, useBiff); return NULL; } h++; /* parse string until end quote */ buff = NULL; uu.c = &buff; buffArr = airArrayNew(uu.v, NULL, sizeof(char), 2); if (!buffArr) { sprintf(err, "%s: couldn't create airArray", me); biffMaybeAdd(NRRD, err, useBiff); return NULL; } pos = airArrayLenIncr(buffArr, 1); /* pos should get 0 */ while (h[pos]) { /* printf("!%s: h+%d |%s|\n", me, pos, h+pos); */ if ('\"' == h[pos]) { break; } if ('\\' == h[pos] && '\"' == h[pos+1]) { h += 1; } buff[pos] = h[pos]; pos = airArrayLenIncr(buffArr, 1); } if ('\"' != h[pos]) { sprintf(err, "%s: didn't see ending \" soon enough", me); biffMaybeAdd(NRRD, err, useBiff); return NULL; } h += pos + 1; buff[pos] = 0; ret = airStrdup(buff); airArrayNuke(buffArr); *hP = h; return ret; }
/* ** _nrrdAxisInfoCopy ** ** HEY: we have a void return even though this function potentially ** involves calling airStrdup!! */ void _nrrdAxisInfoCopy(NrrdAxisInfo *dest, const NrrdAxisInfo *src, int bitflag) { int ii; if (!(NRRD_AXIS_INFO_SIZE_BIT & bitflag)) { dest->size = src->size; } if (!(NRRD_AXIS_INFO_SPACING_BIT & bitflag)) { dest->spacing = src->spacing; } if (!(NRRD_AXIS_INFO_THICKNESS_BIT & bitflag)) { dest->thickness = src->thickness; } if (!(NRRD_AXIS_INFO_MIN_BIT & bitflag)) { dest->min = src->min; } if (!(NRRD_AXIS_INFO_MAX_BIT & bitflag)) { dest->max = src->max; } if (!(NRRD_AXIS_INFO_SPACEDIRECTION_BIT & bitflag)) { for (ii=0; ii<NRRD_SPACE_DIM_MAX; ii++) { dest->spaceDirection[ii] = src->spaceDirection[ii]; } } if (!(NRRD_AXIS_INFO_CENTER_BIT & bitflag)) { dest->center = src->center; } if (!(NRRD_AXIS_INFO_KIND_BIT & bitflag)) { dest->kind = src->kind; } if (!(NRRD_AXIS_INFO_LABEL_BIT & bitflag)) { if (dest->label != src->label) { dest->label = (char *)airFree(dest->label); dest->label = (char *)airStrdup(src->label); } } if (!(NRRD_AXIS_INFO_UNITS_BIT & bitflag)) { if (dest->units != src->units) { dest->units = (char *)airFree(dest->units); dest->units = (char *)airStrdup(src->units); } } return; }
int airSingleSscanf(const char *str, const char *fmt, void *ptr) { char *tmp; double val; int ret; if (!strcmp(fmt, "%e") || !strcmp(fmt, "%f") || !strcmp(fmt, "%g") || !strcmp(fmt, "%le") || !strcmp(fmt, "%lf") || !strcmp(fmt, "%lg")) { tmp = airStrdup(str); if (!tmp) { return 0; } airToLower(tmp); if (strstr(tmp, "nan")) { val = AIR_NAN; } else if (strstr(tmp, "-inf")) { val = AIR_NEG_INF; } else if (strstr(tmp, "inf")) { val = AIR_POS_INF; } else { /* nothing special matched; pass it off to sscanf() */ ret = sscanf(str, fmt, ptr); free(tmp); return ret; } /* else we matched "nan", "-inf", or "inf", and set val accordingly */ if (!strncmp(fmt, "%l", 2)) { /* we were given a double pointer */ *((double *)(ptr)) = val; } else { /* we were given a float pointer */ *((float *)(ptr)) = (float)val; } free(tmp); return 1; } else { /* this is neither a float nor double */ return sscanf(str, fmt, ptr); } }
int main(int argc, char **argv) { char *me, *err; Nrrd *nrrd; NrrdIoState *nio; char hstr[] = "NRRD0001\n" "# Complete NRRD file format specification at:\n" "# http://teem.sourceforge.net/nrrd/format.html\n" "# one comment\n" "# two comment\n" "# three comment\n" "type: float\n" "dimension: 2\n" "sizes: 91 114\n" "centerings: node node\n" "endian: big\n" "encoding: raw\n" "bingo:=bob\n" "foo:=super duper fancy bar with corona\n" /* "data file: tmp.raw\n" */; char *wstr; AIR_UNUSED(argc); me = argv[0]; nrrdStateVerboseIO = 10; nio = nrrdIoStateNew(); nrrd = nrrdNew(); nio->path = airStrdup("."); if (nrrdStringRead(nrrd, hstr, nio)) { fprintf(stderr, "%s: trouble reading string:\n%s", me, err = biffGet(NRRD)); free(err); exit(1); } fprintf(stderr, "%s: nrrd->data = %p\n", me, nrrd->data); nrrdSave("out.nrrd", nrrd, NULL); if (nrrdStringWrite(&wstr, nrrd, NULL)) { fprintf(stderr, "%s: trouble writing string:\n%s", me, err = biffGet(NRRD)); free(err); exit(1); } fprintf(stderr, "%s: |%s|\n", me, wstr); free(wstr); nrrdIoStateNix(nio); nrrdNuke(nrrd); exit(0); }
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; }
void airMopPrint(airArray *arr, const void *_str, int when) { char *copy; if (!(arr && _str)) return; copy = airStrdup((char*)_str); airMopAdd(arr, copy, airFree, airMopAlways); airMopAdd(arr, copy, _airMopPrint, when); return; }
/* ** as of Sept 2013 this returns information: the index of the ** option just added. Returns UINT_MAX in case of error. */ unsigned int hestOptAdd(hestOpt **optP, const char *flag, const char *name, int type, int min, int max, void *valueP, const char *dflt, const char *info, ...) { hestOpt *ret = NULL; int num; va_list ap; unsigned int retIdx; if (!optP) return UINT_MAX; num = *optP ? _hestNumOpts(*optP) : 0; if (!( ret = AIR_CALLOC(num+2, hestOpt) )) { return UINT_MAX; } if (num) memcpy(ret, *optP, num*sizeof(hestOpt)); retIdx = AIR_UINT(num); ret[num].flag = airStrdup(flag); ret[num].name = airStrdup(name); ret[num].type = type; ret[num].min = min; ret[num].max = max; ret[num].valueP = valueP; ret[num].dflt = airStrdup(dflt); ret[num].info = airStrdup(info); /* initialize the things that may be set below */ ret[num].sawP = NULL; ret[num].enm = NULL; ret[num].CB = NULL; /* seems to be redundant with above _hestOptInit() */ ret[num].source = hestSourceUnknown; /* deal with var args */ if (5 == _hestKind(&(ret[num]))) { va_start(ap, info); ret[num].sawP = va_arg(ap, unsigned int*); va_end(ap); }
int parse(void *_ptr, char *str, char *err) { char **ptrP; ptrP = _ptr; *ptrP = airStrdup(str); if (!(*ptrP)) { sprintf(err, "couldn't strdup() str"); return 1; } airToUpper(*ptrP); return 0; }
/* ******** nrrdKeyValueIndex ** ** given an int in [0 .. #key/value pairs - 1], sets *keyP and *valueP ** to put to the corresponding key and value. ** ** NOTE: whether or not *keyP and *valueP are set to pointers to memory ** "inside" the nrrd struct (pointers which you had better not free()!) ** is controlled by nrrdStateKeyValueReturnInternalPointers, which defaults ** to AIR_FALSE */ void nrrdKeyValueIndex(const Nrrd *nrrd, char **keyP, char **valueP, unsigned int ki) { if (!( nrrd && keyP && valueP && ki < nrrd->kvpArr->len )) { if (keyP) { *keyP = NULL; } if (valueP) { *valueP = NULL; } return; } if (nrrdStateKeyValueReturnInternalPointers) { *keyP = nrrd->kvp[0 + 2*ki]; *valueP = nrrd->kvp[1 + 2*ki]; } else { *keyP = airStrdup(nrrd->kvp[0 + 2*ki]); *valueP = airStrdup(nrrd->kvp[1 + 2*ki]); } return; }
/* ******** nrrdKeyValueAdd ** ** This will COPY the given strings, and so does not depend on ** them existing past the return of this function */ int nrrdKeyValueAdd(Nrrd *nrrd, const char *key, const char *value) { int ki; if (!( nrrd && key && value )) { /* got NULL pointer */ return 1; } if (!strlen(key)) { /* reject empty keys */ return 1; } if (-1 != (ki = _nrrdKeyValueIdxFind(nrrd, key))) { nrrd->kvp[1 + 2*ki] = (char *)airFree(nrrd->kvp[1 + 2*ki]); nrrd->kvp[1 + 2*ki] = airStrdup(value); } else { ki = airArrayLenIncr(nrrd->kvpArr, 1); nrrd->kvp[0 + 2*ki] = airStrdup(key); nrrd->kvp[1 + 2*ki] = airStrdup(value); } return 0; }
int _nrrdReadNrrdParse_content (FILE *file, Nrrd *nrrd, NrrdIoState *nio, int useBiff) { char me[]="_nrrdReadNrrdParse_content", err[BIFF_STRLEN]; char *info; AIR_UNUSED(file); info = nio->line + nio->pos; if (strlen(info) && !(nrrd->content = airStrdup(info))) { sprintf(err, "%s: couldn't strdup() content", me); biffMaybeAdd(NRRD, err, useBiff); return 1; } return 0; }
int _nrrdReadNrrdParse_kinds (FILE *file, Nrrd *nrrd, NrrdIoState *nio, int useBiff) { char me[]="_nrrdReadNrrdParse_kinds", err[BIFF_STRLEN]; unsigned int ai; char *info, *tok, *last; airArray *mop; AIR_UNUSED(file); mop = airMopNew(); info = airStrdup(nio->line + nio->pos); airMopAdd(mop, info, airFree, airMopAlways); _CHECK_HAVE_DIM; for (ai=0; ai<nrrd->dim; ai++) { tok = airStrtok(!ai ? info : NULL, _nrrdFieldSep, &last); if (!tok) { sprintf(err, "%s: couldn't extract string for kind %d of %d", me, ai+1, nrrd->dim); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } if (!strcmp(tok, NRRD_UNKNOWN)) { nrrd->axis[ai].kind = nrrdKindUnknown; continue; } if (!strcmp(tok, NRRD_NONE)) { nrrd->axis[ai].center = nrrdKindUnknown; continue; } if (!(nrrd->axis[ai].kind = airEnumVal(nrrdKind, tok))) { sprintf(err, "%s: couldn't parse \"%s\" kind %d of %d", me, tok, ai+1, nrrd->dim); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } } if (airStrtok(!ai ? info : NULL, _nrrdFieldSep, &last)) { sprintf(err, "%s: seem to have more than expected %d kinds", me, nrrd->dim); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } /* can't run this now because kinds can come before sizes, in which case the kind/size check in _nrrdFieldCheck_kinds will incorrectly flag an error ... if (_nrrdFieldCheck[nrrdField_kinds](nrrd, useBiff)) { sprintf(err, "%s: trouble", me); biffMaybeAdd(NRRD, err, useBiff); airMopError(mop); return 1; } */ airMopOkay(mop); return 0; }
/* ******** nrrdCommentAdd() ** ** Adds a given string to the list of comments ** Leading spaces (' ') and comment chars ('#') are not included. ** ** This function does NOT use biff. */ int nrrdCommentAdd(Nrrd *nrrd, const char *_str) { char /* me[]="nrrdCommentAdd", err[512], */ *str; int i; if (!(nrrd && _str)) { /* sprintf(err, "%s: got NULL pointer", me); biffMaybeAdd(NRRD, err, useBiff); */ return 1; } _str += strspn(_str, " #"); if (!strlen(_str)) { /* we don't bother adding comments with no length */ return 0; } if (!strcmp(_str, _nrrdFormatURLLine0) || !strcmp(_str, _nrrdFormatURLLine1)) { /* sneaky hack: don't store the format URL comment lines */ return 0; } str = airStrdup(_str); if (!str) { /* sprintf(err, "%s: couldn't strdup given string", me); biffMaybeAdd(NRRD, err, useBiff); */ return 1; } /* clean out carraige returns that would screw up reader */ airOneLinify(str); i = airArrayLenIncr(nrrd->cmtArr, 1); if (!nrrd->cmtArr->data) { /* sprintf(err, "%s: couldn't lengthen comment array", me); biffMaybeAdd(NRRD, err, useBiff); */ return 1; } nrrd->cmt[i] = str; return 0; }
/* ** DOES use biff */ meetPullVol * meetPullVolCopy(const meetPullVol *mpv) { static const char me[]="meetPullVolCopy"; meetPullVol *ret; unsigned int si; airArray *mop; mop = airMopNew(); ret = meetPullVolNew(); airMopAdd(mop, ret, (airMopper)meetPullVolNix, airMopOnError); /* HEY: hope this is okay for dynamic kinds */ ret->kind = mpv->kind; ret->fileName = airStrdup(mpv->fileName); ret->volName = airStrdup(mpv->volName); if (mpv->sbp) { ret->sbp = gageStackBlurParmNew(); if (gageStackBlurParmCopy(ret->sbp, mpv->sbp)) { biffMovef(MEET, GAGE, "%s: problem", me); airMopError(mop); return NULL; } } ret->leeching = AIR_FALSE; ret->derivNormSS = mpv->derivNormSS; ret->recomputedSS = AIR_FALSE; ret->derivNormBiasSS = mpv->derivNormBiasSS; if (mpv->sbp) { ret->nin = NULL; ret->ninSS = AIR_CALLOC(ret->sbp->num, Nrrd *); for (si=0; si<mpv->sbp->num; si++) { ret->ninSS[si] = nrrdNew(); if (nrrdCopy(ret->ninSS[si], mpv->ninSS[si])) { biffMovef(MEET, NRRD, "%s: problem with ninSS[%u]", me, si); airMopError(mop); return NULL; } } } else {
/* ******** airStrntok() ** ** returns the number of tokens parsable by airStrtok(), but does ** NOT alter the given string */ unsigned int airStrntok(const char *_s, const char *ct) { char *s, *t, *l=NULL; unsigned int n = 0; if (_s && ct) { s = airStrdup(_s); t = airStrtok(s, ct, &l); while (t) { n++; t = airStrtok(NULL, ct, &l); } airFree(s); /* no NULL assignment to s, else compile warnings */ } return n; }
int airEnumVal(const airEnum *enm, const char *str) { char *strCpy, test[AIR_STRLEN_SMALL]; unsigned int ii; if (!str) { return airEnumUnknown(enm); } strCpy = airStrdup(str); if (!enm->sense) { airToLower(strCpy); } if (enm->strEqv) { /* want strlen and not airStrlen here because the strEqv array should be terminated by a non-null empty string */ for (ii=0; strlen(enm->strEqv[ii]); ii++) { strncpy(test, enm->strEqv[ii], AIR_STRLEN_SMALL); test[AIR_STRLEN_SMALL-1] = '\0'; if (!enm->sense) { airToLower(test); } if (!strcmp(test, strCpy)) { free(strCpy); return enm->valEqv[ii]; } } } else { /* enm->strEqv NULL */ for (ii=1; ii<=enm->M; ii++) { strncpy(test, enm->str[ii], AIR_STRLEN_SMALL); test[AIR_STRLEN_SMALL-1] = '\0'; if (!enm->sense) { airToLower(test); } if (!strcmp(test, strCpy)) { free(strCpy); return enm->val ? enm->val[ii] : (int)ii; /* HEY scrutinize cast */ } } } /* else we never matched a string */ free(strCpy); return airEnumUnknown(enm); }