|
From: <kin...@us...> - 2003-11-14 21:10:58
|
Update of /cvsroot/teem/teem/src/nrrd
In directory sc8-pr-cvs1:/tmp/cvs-serv15051
Modified Files:
keyvalue.c methodsNrrd.c
Log Message:
Nrrd (and the rest of teem) is supposed to obey a rule of thumb
with respect to strings (char*) and and their allocation:
Strings are always re-allocated, and can be free()ed without worry
char* pointers given to nrrdKeyValueAdd are airStrdup()d to make the
strings that are stored internally. But before now, nrrdKeyValueGet
and nrrdKeyValueIndex would return pointers to memory INSIDE the
nrrd, leading to problems if they were freed. Bad! Now, these
functions return NEWLY allocated copies of the strings, so that the
caller can (and should) free them.
However, power users may want to avoid this, so they can set
nrrdStateKeyValueReturnInternalPointers, which means that (as before)
internal pointers are returned.
Also, nrrdKeyValueCopy() was created, and now nrrdCopy() calls that
instead of copying key/value pairs by hand.
Index: keyvalue.c
===================================================================
RCS file: /cvsroot/teem/teem/src/nrrd/keyvalue.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** keyvalue.c 2 Oct 2003 12:07:10 -0000 1.4
--- keyvalue.c 14 Nov 2003 21:10:53 -0000 1.5
***************
*** 21,24 ****
--- 21,30 ----
#include "privateNrrd.h"
+ /***
+ **** NONE of the nrrdKeyValue functions use biff.
+ **** They don't use them now, and they never should.
+ **** Unless I change my mind.
+ ***/
+
/*
******** nrrdKeyValueSize
***************
*** 41,46 ****
** to put to the corresponding key and value.
**
! ** NOTE: like nrrdKeyValueGet, these results point to strings
! ** INTERNAL to the nrrd struct. DO NOT FREE THEM.
*/
void
--- 47,54 ----
** 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
***************
*** 53,58 ****
return;
}
! *keyP = nrrd->kvp[0 + 2*ki];
! *valueP = nrrd->kvp[1 + 2*ki];
return;
}
--- 61,71 ----
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;
}
***************
*** 145,151 ****
******** nrrdKeyValueGet
**
! ** This will return a pointer to a string INSIDE the given
! ** nrrd struct; DO NOT FREE IT. This is for the sake of
! ** convenience, not perfect safety (obviously)
*/
char *
--- 158,165 ----
******** nrrdKeyValueGet
**
! ** 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
*/
char *
***************
*** 159,163 ****
}
if (-1 != (ki = _nrrdKeyValueIdxFind(nrrd, key))) {
! ret = nrrd->kvp[1 + 2*ki];
} else {
ret = NULL;
--- 173,181 ----
}
if (-1 != (ki = _nrrdKeyValueIdxFind(nrrd, key))) {
! if (nrrdStateKeyValueReturnInternalPointers) {
! ret = nrrd->kvp[1 + 2*ki];
! } else {
! ret = airStrdup(nrrd->kvp[1 + 2*ki]);
! }
} else {
ret = NULL;
***************
*** 206,209 ****
--- 224,259 ----
_nrrdFwriteEscaped(file, value);
fprintf(file, "\n");
+ return 0;
+ }
+
+ /*
+ ******** nrrdKeyValueCopy()
+ **
+ ** copies key/value pairs from one nrrd to another
+ ** Existing key/value pairs in nout are blown away
+ */
+ int
+ nrrdKeyValueCopy(Nrrd *nout, const Nrrd *nin) {
+ char *key, *value;
+ int ki;
+
+ if (!(nout && nin)) {
+ /* got NULL pointer */
+ return 1;
+ }
+ if (nout == nin) {
+ /* can't satisfy semantics of copying with nout==nin */
+ return 2;
+ }
+
+ nrrdKeyValueClear(nout);
+ for (ki=0; ki<nin->kvpArr->len; ki++) {
+ key = nin->kvp[0 + 2*ki];
+ value = nin->kvp[1 + 2*ki];
+ if (nrrdKeyValueAdd(nout, key, value)) {
+ return 3;
+ }
+ }
+
return 0;
}
Index: methodsNrrd.c
===================================================================
RCS file: /cvsroot/teem/teem/src/nrrd/methodsNrrd.c,v
retrieving revision 1.30
retrieving revision 1.31
diff -C2 -d -r1.30 -r1.31
*** methodsNrrd.c 2 Oct 2003 08:22:37 -0000 1.30
--- methodsNrrd.c 14 Nov 2003 21:10:53 -0000 1.31
***************
*** 504,517 ****
}
! /* HEY: Replace this with something more efficient */
! for (i=0; i<nrrdKeyValueSize(nin); i++) {
! char *key, *value;
! nrrdKeyValueIndex(nin, &key, &value, i);
! if (NULL != key && NULL != value) {
! if (nrrdKeyValueAdd(nout, key, value)) {
! sprintf(err, "%s: couldn't add key/value pair", me);
! biffAdd(NRRD, err); return 1;
! }
! }
}
--- 504,510 ----
}
! if (nrrdKeyValueCopy(nout, nin)) {
! sprintf(err, "%s: trouble copying key/value pairs", me);
! biffAdd(NRRD, err); return 1;
}
|