|
From: <kin...@us...> - 2003-12-21 02:43:10
|
Update of /cvsroot/teem/teem/src/air
In directory sc8-pr-cvs1:/tmp/cvs-serv5616/air
Modified Files:
miscAir.c
Log Message:
Yet more changes and re-arrangements for NrrdIO
Index: miscAir.c
===================================================================
RCS file: /cvsroot/teem/teem/src/air/miscAir.c,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -d -r1.18 -r1.19
*** miscAir.c 30 Oct 2003 21:30:07 -0000 1.18
--- miscAir.c 21 Dec 2003 02:43:07 -0000 1.19
***************
*** 29,40 ****
#endif
- #if TEEM_32BIT == 1
- const int airMy32Bit = 1;
- const char airMyFmt_size_t[] = "%u";
- #else
- const int airMy32Bit = 0;
- const char airMyFmt_size_t[] = "%lu";
- #endif
-
/*
******** airTeemVersion
--- 29,32 ----
***************
*** 50,122 ****
/*
- ******** airSrand()
- **
- ** uses clock to seed srand()
- */
- void
- airSrand(void) {
-
- srand((unsigned int)airTime());
- }
-
- /*
- ******** airRand()
- **
- ** returns double in range [0.0,1.0)
- */
- double
- airRand(void) {
- double val, sc = 1.0/(RAND_MAX+1.0);
-
- val = sc*rand();
-
- /* repeat as needed */
- val = sc*(val + rand());
- val = sc*(val + rand());
- val = sc*(val + rand());
-
- return val;
- }
-
- /*
- ******** airRandInt
- **
- ** returns a random integer in range [0, N-1]
- */
- int
- airRandInt(int N) {
- int i;
-
- AIR_INDEX(0.0, airRand(), 1.0, N, i);
- i = AIR_CLAMP(0, i, N-1);
- return i;
- }
-
- /*
- ******** airShuffle()
- **
- ** generates a random permutation of integers [0..N-1] if perm is non-zero,
- ** otherwise, just fills buff with [0..N-1] in order
- */
- void
- airShuffle(int *buff, int N, int perm) {
- int i, swp, tmp;
-
- if (!(buff && N > 0))
- return;
-
- for (i=0; i<N; i++)
- buff[i] = i;
- if (perm) {
- for (i=0; i<N; i++) {
- swp = i + airRandInt(N - i);
- tmp = buff[swp];
- buff[swp] = buff[i];
- buff[i] = tmp;
- }
- }
- }
-
- /*
******** airNull()
**
--- 42,45 ----
***************
*** 219,222 ****
--- 142,330 ----
/*
+ ******** airSinglePrintf
+ **
+ ** a complete stand-in for {f|s}printf(), as long as the given format
+ ** string contains exactly one conversion sequence. The utility of
+ ** this is to standardize the printing of IEEE 754 special values:
+ ** QNAN, SNAN -> "NaN"
+ ** POS_INF -> "+inf"
+ ** NEG_INF -> "-inf"
+ ** The format string can contain other things besides just the
+ ** conversion sequence: airSingleFprintf(f, " (%f)\n", AIR_NAN)
+ ** will be the same as fprintf(f, " (%s)\n", "NaN");
+ **
+ ** To get fprintf behavior, pass "str" as NULL
+ ** to get sprintf bahavior, pass "file" as NULL
+ **
+ ** Someday I'll find/write a complete {f|s|}printf replacement ...
+ */
+ int
+ airSinglePrintf(FILE *file, char *str, const char *_fmt, ...) {
+ char *fmt;
+ float valF=0;
+ double valD=0;
+ int ret, isF, isD, cls;
+ char *conv=NULL, *p0, *p1, *p2, *p3, *p4, *p5;
+ va_list ap;
+
+ va_start(ap, _fmt);
+ fmt = airStrdup(_fmt);
+
+ p0 = strstr(fmt, "%e");
+ p1 = strstr(fmt, "%f");
+ p2 = strstr(fmt, "%g");
+ p3 = strstr(fmt, "%le");
+ p4 = strstr(fmt, "%lf");
+ p5 = strstr(fmt, "%lg");
+ isF = p0 || p1 || p2;
+ isD = p3 || p4 || p5;
+ if (isF) {
+ conv = p0 ? p0 : (p1 ? p1 : p2);
+ }
+ if (isD) {
+ conv = p3 ? p3 : (p4 ? p4 : p5);
+ }
+ if (isF || isD) {
+ if (isF) {
+ /* use "double" instead of "float" because var args are _always_
+ subject to old-style C type promotions: float promotes to double */
+ valF = (float)(va_arg(ap, double));
+ cls = airFPClass_f(valF);
+ }
+ else {
+ valD = va_arg(ap, double);
+ cls = airFPClass_d(valD);
+ }
+ switch (cls) {
+ case airFP_SNAN:
+ case airFP_QNAN:
+ case airFP_POS_INF:
+ case airFP_NEG_INF:
+ if (isF) {
+ memcpy(conv, "%s", 2);
+ }
+ else {
+ /* this sneakiness allows us to replace a 3-character conversion
+ sequence for a double (such as %lg) with a 3-character conversion
+ for a string, which we know has at most 4 characters */
+ memcpy(conv, "%4s", 3);
+ }
+ break;
+ }
+ #define PRINT(F, S, C, V) ((F) ? fprintf((F),(C),(V)) : sprintf((S),(C),(V)))
+ switch (cls) {
+ case airFP_SNAN:
+ case airFP_QNAN:
+ ret = PRINT(file, str, fmt, "NaN");
+ break;
+ case airFP_POS_INF:
+ ret = PRINT(file, str, fmt, "+inf");
+ break;
+ case airFP_NEG_INF:
+ ret = PRINT(file, str, fmt, "-inf");
+ break;
+ default:
+ if (isF) {
+ ret = PRINT(file, str, fmt, valF);
+ }
+ else {
+ ret = PRINT(file, str, fmt, valD);
+ }
+ break;
+ }
+ }
+ else {
+ ret = file ? vfprintf(file, fmt, ap) : vsprintf(str, fmt, ap);
+ }
+
+ va_end(ap);
+ free(fmt);
+ return ret;
+ }
+
+ #if TEEM_32BIT == 1
+ const int airMy32Bit = 1;
+ #else
+ const int airMy32Bit = 0;
+ #endif
+
+ /* ---- BEGIN non-NrrdIO */
+
+ #if TEEM_32BIT == 1
+ const char airMyFmt_size_t[] = "%u";
+ #else
+ const char airMyFmt_size_t[] = "%lu";
+ #endif
+
+ /*
+ ******** airSrand()
+ **
+ ** uses clock to seed srand()
+ */
+ void
+ airSrand(void) {
+
+ srand((unsigned int)airTime());
+ }
+
+ /*
+ ******** airRand()
+ **
+ ** returns double in range [0.0,1.0)
+ */
+ double
+ airRand(void) {
+ double val, sc = 1.0/(RAND_MAX+1.0);
+
+ val = sc*rand();
+
+ /* repeat as needed */
+ val = sc*(val + rand());
+ val = sc*(val + rand());
+ val = sc*(val + rand());
+
+ return val;
+ }
+
+ /*
+ ******** airRandInt
+ **
+ ** returns a random integer in range [0, N-1]
+ */
+ int
+ airRandInt(int N) {
+ int i;
+
+ AIR_INDEX(0.0, airRand(), 1.0, N, i);
+ i = AIR_CLAMP(0, i, N-1);
+ return i;
+ }
+
+ /*
+ ******** airShuffle()
+ **
+ ** generates a random permutation of integers [0..N-1] if perm is non-zero,
+ ** otherwise, just fills buff with [0..N-1] in order
+ */
+ void
+ airShuffle(int *buff, int N, int perm) {
+ int i, swp, tmp;
+
+ if (!(buff && N > 0))
+ return;
+
+ for (i=0; i<N; i++)
+ buff[i] = i;
+ if (perm) {
+ for (i=0; i<N; i++) {
+ swp = i + airRandInt(N - i);
+ tmp = buff[swp];
+ buff[swp] = buff[i];
+ buff[i] = tmp;
+ }
+ }
+ }
+
+ /*
******* airDoneStr()
**
***************
*** 337,442 ****
}
- /*
- ******** airSinglePrintf
- **
- ** a complete stand-in for {f|s}printf(), as long as the given format
- ** string contains exactly one conversion sequence. The utility of
- ** this is to standardize the printing of IEEE 754 special values:
- ** QNAN, SNAN -> "NaN"
- ** POS_INF -> "+inf"
- ** NEG_INF -> "-inf"
- ** The format string can contain other things besides just the
- ** conversion sequence: airSingleFprintf(f, " (%f)\n", AIR_NAN)
- ** will be the same as fprintf(f, " (%s)\n", "NaN");
- **
- ** To get fprintf behavior, pass "str" as NULL
- ** to get sprintf bahavior, pass "file" as NULL
- **
- ** Someday I'll find/write a complete {f|s|}printf replacement ...
- */
- int
- airSinglePrintf(FILE *file, char *str, const char *_fmt, ...) {
- char *fmt;
- float valF=0;
- double valD=0;
- int ret, isF, isD, cls;
- char *conv=NULL, *p0, *p1, *p2, *p3, *p4, *p5;
- va_list ap;
-
- va_start(ap, _fmt);
- fmt = airStrdup(_fmt);
-
- p0 = strstr(fmt, "%e");
- p1 = strstr(fmt, "%f");
- p2 = strstr(fmt, "%g");
- p3 = strstr(fmt, "%le");
- p4 = strstr(fmt, "%lf");
- p5 = strstr(fmt, "%lg");
- isF = p0 || p1 || p2;
- isD = p3 || p4 || p5;
- if (isF) {
- conv = p0 ? p0 : (p1 ? p1 : p2);
- }
- if (isD) {
- conv = p3 ? p3 : (p4 ? p4 : p5);
- }
- if (isF || isD) {
- if (isF) {
- /* use "double" instead of "float" because var args are _always_
- subject to old-style C type promotions: float promotes to double */
- valF = (float)(va_arg(ap, double));
- cls = airFPClass_f(valF);
- }
- else {
- valD = va_arg(ap, double);
- cls = airFPClass_d(valD);
- }
- switch (cls) {
- case airFP_SNAN:
- case airFP_QNAN:
- case airFP_POS_INF:
- case airFP_NEG_INF:
- if (isF) {
- memcpy(conv, "%s", 2);
- }
- else {
- /* this sneakiness allows us to replace a 3-character conversion
- sequence for a double (such as %lg) with a 3-character conversion
- for a string, which we know has at most 4 characters */
- memcpy(conv, "%4s", 3);
- }
- break;
- }
- #define PRINT(F, S, C, V) ((F) ? fprintf((F),(C),(V)) : sprintf((S),(C),(V)))
- switch (cls) {
- case airFP_SNAN:
- case airFP_QNAN:
- ret = PRINT(file, str, fmt, "NaN");
- break;
- case airFP_POS_INF:
- ret = PRINT(file, str, fmt, "+inf");
- break;
- case airFP_NEG_INF:
- ret = PRINT(file, str, fmt, "-inf");
- break;
- default:
- if (isF) {
- ret = PRINT(file, str, fmt, valF);
- }
- else {
- ret = PRINT(file, str, fmt, valD);
- }
- break;
- }
- }
- else {
- ret = file ? vfprintf(file, fmt, ap) : vsprintf(str, fmt, ap);
- }
-
- va_end(ap);
- free(fmt);
- return ret;
- }
-
void
airBinaryPrintUInt(FILE *file, int digits, unsigned int N) {
--- 445,448 ----
***************
*** 582,584 ****
--- 588,592 ----
}
}
+
+ /* ---- END non-NrrdIO */
|