1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
/*
NrrdIO: stand-alone code for basic nrrd functionality
Copyright (C) 2008, 2007, 2006, 2005 Gordon Kindlmann
Copyright (C) 2004, 2003, 2002, 2001, 2000, 1999, 1998 University of Utah
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "NrrdIO.h"
#include "teem32bit.h"
/* timer functions */
#ifdef _WIN32
#include <io.h>
#include <fcntl.h>
#include <time.h>
#else
#include <sys/time.h>
#endif
/*
******** airTeemVersion
******** airTeemReleaseDate
**
** updated with each release to contain a string representation of
** the Teem version number and release date. Originated in version 1.5;
** use of TEEM_VERSION #defines started in 1.9
*/
const char *
airTeemVersion = TEEM_VERSION_STRING;
const char *
airTeemReleaseDate = "late 2009 or early 2010";
double
_airSanityHelper(double val) {
return val*val*val;
}
/*
******** airNull()
**
** returns NULL
*/
void *
airNull(void) {
return NULL;
}
/*
******** airSetNull
**
** dereferences and sets to NULL, returns NULL
*/
void *
airSetNull(void **ptrP) {
if (ptrP) {
*ptrP = NULL;
}
return NULL;
}
/*
******** airFree()
**
** to facilitate setting a newly free()'d pointer; always returns NULL.
** also makes sure that NULL is not passed to free().
*/
void *
airFree(void *ptr) {
if (ptr) {
free(ptr);
}
return NULL;
}
/*
******** airFopen()
**
** encapsulates that idea that "-" is either standard in or stardard
** out, and does McRosopht stuff required to make piping work
**
** Does not error checking. If fopen fails, then C' errno and strerror are
** left untouched for the caller to access.
*/
FILE *
airFopen(const char *name, FILE *std, const char *mode) {
FILE *ret;
if (!strcmp(name, "-")) {
ret = std;
#ifdef _MSC_VER
if (strchr(mode, 'b')) {
_setmode(_fileno(ret), _O_BINARY);
}
#endif
} else {
ret = fopen(name, mode);
}
return ret;
}
/*
******** airFclose()
**
** just to facilitate setting a newly fclose()'d file pointer to NULL
** also makes sure that NULL is not passed to fclose(), and won't close
** stdin, stdout, or stderr (its up to the user to open these correctly)
*/
FILE *
airFclose(FILE *file) {
if (file) {
if (!( stdin == file || stdout == file || stderr == file )) {
fclose(file);
}
}
return NULL;
}
/*
******** 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, buff[AIR_STRLEN_LARGE];
double val=0, gVal, fVal;
int ret, isF, isD, cls;
char *conv=NULL, *p0, *p1, *p2, *p3, *p4, *p5;
va_list ap;
va_start(ap, _fmt);
fmt = airStrdup(_fmt);
/* this is needlessly complicated; the "l" modifier is a no-op */
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;
/* the code here says "isF" and "isD" as if it means "is float" or
"is double". It really should be "is2" or "is3", as in,
"is 2-character conversion sequence, or "is 3-character..." */
if (isF) {
conv = p0 ? p0 : (p1 ? p1 : p2);
}
if (isD) {
conv = p3 ? p3 : (p4 ? p4 : p5);
}
if (isF || isD) {
/* use "double" instead of "float" because var args are _always_
subject to old-style C type promotions: float promotes to double */
val = va_arg(ap, double);
cls = airFPClass_d(val);
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 (p2 || p5) {
/* got "%g" or "%lg", see if it would be better to use "%f" */
sprintf(buff, "%f", val);
sscanf(buff, "%lf", &fVal);
sprintf(buff, "%g", val);
sscanf(buff, "%lf", &gVal);
if (fVal != gVal) {
/* using %g (or %lg) lost precision!! Use %f (or %lf) instead */
if (p2) {
memcpy(conv, "%f", 2);
} else {
memcpy(conv, "%lf", 3);
}
}
}
ret = PRINT(file, str, fmt, val);
break;
}
} else {
/* conversion sequence is neither for float nor double */
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
|