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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
|
/*
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"
/* this has to default to false in order for airStrtok to be a
functional substitute for strtok() */
int airStrtokQuoting = AIR_FALSE;
/*
******** airStrdup()
**
** because they didn't put strdup() in ANSI ...
** This will return NULL if given NULL.
*/
char *
airStrdup(const char *s) {
char *ret;
if (!s) {
ret = NULL;
}
else {
ret = (char *)malloc(strlen(s)+1);
if (ret) {
strcpy(ret, s);
}
}
return ret;
}
/*
******** airStrlen()
**
** just like strlen, but safe to call on NULL (for which return is 0)
*/
size_t
airStrlen(const char *s) {
size_t ret;
if (!s) {
ret = 0;
}
else {
ret = strlen(s);
}
return ret;
}
/*
******** airStrtok()
**
** thread-safe strtok() replacement. Use just like strtok(), but on
** each call to parse a given string, pass as the last argument the
** address of a char*, to be used for saving state while the string is
** traversed. Like strtok(), this will alter the "s" array passed to
** it on the first call, and like strtok(), this returns pointers into
** this string (rather than allocating new strings for each token).
*/
char *
airStrtok(char *s, const char *ct, char **last) {
char *h, *e, *q;
if (!(ct && last)) {
/* can't do any work, bail */
return NULL;
}
h = s ? s : *last;
if (!airStrlen(h))
return NULL;
h += strspn(h, ct);
if ('\"' == *h && airStrtokQuoting) {
/* something is trying to be quoted, and, we'll respect that */
/* have to find the next un-escaped '\"' */
h++;
q = h;
while (*q && !('\"' == *q && '\\' != q[-1])) {
q++;
}
if (*q) {
/* we found an unescaped '\"' */
e = q;
} else {
/* give up; pretend we never tried to do this quoting stuff */
e = h + strcspn(h, ct);
}
} else {
e = h + strcspn(h, ct);
}
if ('\0' == *e) {
*last = e;
}
else {
*e = '\0';
*last = e + 1;
}
return h;
}
/*
******** 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;
}
char *
airStrtrans(char *s, char from, char to) {
size_t i, l;
if (s) {
l = strlen(s);
for (i=0; i<l; i++) {
s[i] = (s[i] == from ? to : s[i]);
}
}
return s;
}
/*
******** airEndsWith
**
** if "s" ends with "suff", then returns 1, 0 otherwise
*/
int
airEndsWith(const char *s, const char *suff) {
if (!(s && suff))
return 0;
if (!(strlen(s) >= strlen(suff)))
return 0;
if (!strncmp(s + strlen(s) - strlen(suff), suff, strlen(suff)))
return 1;
else
return 0;
}
/*
******** airUnescape()
**
** unescapes \\ and \n in place in a given string.
**
*/
char *
airUnescape(char *s) {
size_t i, j, len;
int found=0;
len = airStrlen(s);
if (!len)
return s;
for (i=1, j=0; i<len; i++, j++) {
if (s[i-1] == '\\' && s[i] == '\\') {
s[j] = '\\'; i++; found = 1;
} else if (s[i-1] == '\\' && s[i] == 'n') {
s[j] = '\n'; i++; found = 1;
} else {
s[j] = s[i-1]; found = 0;
}
}
if (i == len || !found) s[j++] = s[len-1];
s[j] = 0;
return s;
}
/*
******** airOneLinify()
**
** converts all contiguous white space (as determined by isspace()) to
** a single ' ', entirely removes non-printable (as determined by
** isprint()) characters, and entirely removes white space contiguous
** with the end of the string, even if that means shrinking the string
** to "".
**
** Useful for cleaning up lines of text to be saved as strings in
** fields of other structs
*/
char *
airOneLinify(char *s) {
size_t i, j, len;
len = airStrlen(s);
if (!len)
return s;
/* convert white space to space (' '), and delete unprintables */
for (i=0; i<len; i++) {
if (isspace(AIR_CAST(int, s[i]))) {
s[i] = ' ';
continue;
}
if (!isprint(AIR_CAST(int, s[i]))) {
for (j=i; j<len; j++) {
/* this will copy the '\0' at the end */
s[j] = s[j+1];
}
i--;
continue;
}
}
/* compress all contiguous spaces into one */
for (i=0; i<len; i++) {
while (' ' == s[i] && ' ' == s[i+1]) {
for (j=i+1; j<len; j++) {
s[j] = s[j+1];
}
}
}
/* lose trailing white space */
i = airStrlen(s);
if (' ' == s[i-1]) {
s[i-1] = '\0';
}
return s;
}
/*
******** airToLower()
**
** calls tolower() on all characters in a string, and returns the same
** pointer that it was given
*/
char *
airToLower(char *str) {
char *c;
if (str) {
c = str;
while (*c) {
*c = tolower((int) *c);
c++;
}
}
return str;
}
/*
******** airToUpper()
**
** calls toupper() on all characters in a string, and returns the same
** pointer that it was given
*/
char *
airToUpper(char *str) {
char *c;
if (str) {
c = str;
while (*c) {
*c = toupper((int) *c);
c++;
}
}
return str;
}
/*
******** airOneLine()
**
** gets one line from "file", putting it into an array if given size.
** "size" must be the size of line buffer "line": the size which
** "line" was allocated for, not the number of non-null characters it
** was meant to hold. "size" must be at least 3. Always
** null-terminates the contents of the array (except if the arguments
** are invalid). The idea is that the null-termination replaces the
** line termination.
**
** 0: if saw EOF before seeing a newline, or arguments are invalid
** 1: if line was a single newline
** n; n <= size: if line was n-1 characters followed by newline
** size+1: if didn't see a newline within size-1 characters
**
** So except for returns of -1 and size+1, the return is the number of
** characters comprising the line, including the newline character.
**
** For all you DOS\Windows\Cygwin users, this will quietly pretend that
** a "\r\n" pair is really just "\n", including the way that characters
** comprising the line are counted. However, there is no pretension
** that on those platforms, "\n" by itself does not actually count as
** a newline.
*/
unsigned int
airOneLine(FILE *file, char *line, int size) {
int c=0, i;
if (!(size >= 3 /* need room for a character and a Windows newline */
&& line && file)) {
return 0;
}
/* c is always set at least once, but not so for any char in line[] */
for (i=0;
(i <= size-2 /* room for line[i] and \0 after that */
&& EOF != (c=getc(file)) /* didn't hit EOF trying to read char */
&& c != '\n'); /* char isn't newline */
++i) {
line[i] = c;
}
if (EOF == c) {
/* for-loop terminated because we hit EOF */
line[0] = '\0';
return 0;
} else if ('\n' == c) {
/* for-loop terminated because we hit '\n' */
if (i >= 1 && '\r' == line[i-1]) {
/* newline was "\r\n" */
i--;
}
line[i] = '\0';
return i+1;
} else {
/* for-loop terminated because we got to end of buffer (i == size-1) */
c = getc(file);
/* but see if we were about to get a "\n" */
if ('\n' == c) {
if ('\r' == line[i-1]) {
/* newline was "\r\n" */
i--;
}
line[i] = '\0';
return i+1;
} else {
/* weren't about to get a "\n", we really did run out of buffer */
if (EOF != c) {
ungetc(c, file); /* we're allowed one ungetc on ANY stream */
}
line[size-1] = '\0';
return size+1;
}
}
}
|