File: biffbiff.c

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 80,652 kB
  • sloc: cpp: 458,133; ansic: 196,223; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 430; csh: 220; perl: 193; xml: 20
file content (465 lines) | stat: -rw-r--r-- 10,192 bytes parent folder | download | duplicates (4)
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*
  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"

static biffMsg **
_bmsg=NULL;            /* master array of biffMsg pointers */
static unsigned int 
_bmsgNum=0;            /* length of _biffErr == # keys maintained */
static airArray *
_bmsgArr=NULL;         /* air array of _biffErr and _biffNum */

#define __INCR 2

typedef union {
  biffMsg ***b;
  void **v;
} _beu;

/*
** _bmsgStart()
**
** allocates data structers needed by biff.  Panics and exit(1)s if 
** anything goes wrong.
**
** NOTE: Can be harmlessly called multiple times.
*/
void
_bmsgStart(void) {
  static const char me[]="[biff] _bmsgStart";
  _beu uu;

  if (_bmsgArr) {
    /* its non-NULL, must have been called already */
    return;
  }
  uu.b = &_bmsg;
  _bmsgArr = airArrayNew(uu.v, &_bmsgNum, sizeof(biffMsg*), __INCR);
  if (!_bmsgArr) {
    fprintf(stderr, "%s: PANIC: couldn't allocate internal data\n", me);
    exit(1);
  }
  /* airArrayPointerCB(_bmsgArr, NULL, biffMsgNix); */
  /* HEY: not using any pointer callbacks here? */
  return;
}

void
_bmsgFinish(void) {

  if (_bmsgArr) {
    /* setting _bmsgArr to NULL is needed to put biff back in initial state
       so that next calls to biff re-trigger _bmsgStart() */
    _bmsgArr = airArrayNuke(_bmsgArr);
  }
  return;
}

/*
** _bmsgFind()
**
** returns the biffMsg (in _bmsg) of the entry with the given key, or
** NULL if it was not found
*/
biffMsg *
_bmsgFind(const char *key) {
  static const char me[]="[biff] _bmsgFind";
  biffMsg *msg;
  unsigned int ii;

  if (!key) {
    fprintf(stderr, "%s: PANIC got NULL key", me);
    exit(1);
  }
  msg = NULL;
  if (_bmsgNum) {
    for (ii=0; ii<_bmsgNum; ii++) {
      if (!strcmp(_bmsg[ii]->key, key)) {
        msg = _bmsg[ii];
        break;
      }
    }
  }
  return msg;
}

/*
** assumes that msg really is in _bmsg[]
*/
unsigned int
_bmsgFindIdx(biffMsg *msg) {
  unsigned int ii;
  
  for (ii=0; ii<_bmsgNum; ii++) {
    if (msg == _bmsg[ii]) {
      break;
    }
  }
  return ii;
}

/*
** _bmsgAdd()
**
** if given key already has a biffMsg in _bmsg, returns that.
** otherise, adds a new biffMsg for given key to _bmsg, and returns it
** panics and exit(1)s if there is a problem
*/
biffMsg *
_bmsgAdd(const char *key) {
  static const char me[]="[biff] _bmsgAdd";
  unsigned int ii;
  biffMsg *msg;

  msg = NULL;
  /* find if key exists already */
  for (ii=0; ii<_bmsgNum; ii++) {
    if (!strcmp(key, _bmsg[ii]->key)) {
      msg = _bmsg[ii];
      break;
    }
  }
  if (!msg) {
    /* have to add new biffMsg */
    ii = airArrayLenIncr(_bmsgArr, 1);
    if (!_bmsg) {
      fprintf(stderr, "%s: PANIC: couldn't accomodate one more key\n", me);
      exit(1);
    }
    msg = _bmsg[ii] = biffMsgNew(key);
  }
  return msg;
}

/***********************************************************************/
/***********************************************************************/

/*
******** biffAdd()
**
** Adds string "err" at key "key", whether or not there are any 
** existing messages there.  Since biffSet() was killed 
** Wed Apr 20 11:11:51 EDT 2005, this has become the main biff
** function.
*/
void
biffAdd(const char *key, const char *err) {
  biffMsg *msg;

  _bmsgStart();
  msg = _bmsgAdd(key);
  biffMsgAdd(msg, err);
  return;
}

void
biffAddVL(const char *key, const char *errfmt, va_list args) {
  biffMsg *msg;

  _bmsgStart();
  msg = _bmsgAdd(key);
  biffMsgAddVL(msg, errfmt, args);
  return;
}

/*
******** biffAddf()
**
** Adds string "err" at key "key", whether or not there are any
** existing messages there.  This version accepts a printf style
** format string as input.
*/
void
biffAddf(const char *key, const char *errfmt, ...) {
  va_list args;

  va_start(args, errfmt);
  biffAddVL(key, errfmt, args);
  va_end(args);
  return;
}

#if 0
/*
******** biffAddf_e
**
** calls (eventually) biffMsgAdd if msg is non-NULL, otherwise calls
** biffAdd if msg is NULL.
*/
void
biffAddf_e(biffMsg *msg, const char *key, const char *errfmt, ...) {
  va_list args;

  va_start(args, errfmt);
  if (msg) {
    biffMsgAddVL(msg, errfmt, args);
  } else {
    biffAddVL(key, errfmt, args);
  }
  va_end(args);
  return;
}
#endif

/*
******** biffMaybeAdd()
**
** wrapper around biffAdd() but doesn't actually do anything if !useBiff
*/
void
biffMaybeAdd(const char *key, const char *err, int useBiff) {

  if (useBiff) {
    biffAdd(key, err);
  }
  return;
}

void
biffMaybeAddf(int useBiff, const char *key, const char *errfmt, ...) {
  va_list args;

  va_start(args, errfmt);
  if (useBiff) {
    biffAddVL(key, errfmt, args);
  }
  va_end(args);
  return;
}

/*
******** biffGet()
**
** creates a string which records all the errors at given key and
** returns it.  Returns NULL in case of error.  This function should
** be considered a glorified strdup(): it is the callers responsibility
** to free() this string later
*/
char *
biffGet(const char *key) {
  static const char me[]="biffGet";
  char *ret;
  biffMsg *msg;

  _bmsgStart();
  msg = _bmsgFind(key);
  if (!msg) {
    static const char err[]="[%s] No information for this key!";
    size_t errlen;
    fprintf(stderr, "%s: WARNING: no information for key \"%s\"\n", me, key);
    errlen = strlen(err)+strlen(key)+1;
    ret = AIR_CALLOC(errlen, char);
    if (!ret) {
      fprintf(stderr, "%s: PANIC: unable to allocate buffer\n", me);
      exit(1);
    }
#if defined(WIN32) || defined(_WIN32)
    _snprintf(ret, errlen, err, key);
#else
    snprintf(ret, errlen, err, key);
#endif
    return ret;
  }

  ret = AIR_CALLOC(biffMsgStrlen(msg)+1, char);
  if (!ret) {
    fprintf(stderr, "%s: PANIC: unable to allocate buffer\n", me);
    exit(1);
  }
  biffMsgStrSet(ret, msg);
  return ret;
}

/*
******** biffGetStrlen()
**
** for when you want to allocate the buffer for the biff string, this is
** how you learn its length
*/
int
biffGetStrlen(const char *key) {
  static const char me[]="biffGetStrlen";
  biffMsg *msg;
  unsigned int len;

  _bmsgStart();
  msg = _bmsgFind(key);
  if (!msg) {
    fprintf(stderr, "%s: WARNING: no information for key \"%s\"\n", me, key);
    return 0;
  }
  len = biffMsgStrlen(msg);
  len += 1;  /* GLK forgets if the convention is that the caller allocates
                for one more to include '\0'; this is safer */
  return len;
}

/*
******** biffSetStr()
**
** for when you want to allocate the buffer for the biff string, this is
** how you get the error message itself
*/
void
biffSetStr(char *str, const char *key) {
  static const char me[]="biffSetStr";
  biffMsg *msg;

  if (!str) {
    fprintf(stderr, "%s: ERROR: got NULL buffer for \"%s\"\n", me, key);
    return;
  }

  _bmsgStart();
  msg = _bmsgFind(key);
  if (!msg) {
    fprintf(stderr, "%s: WARNING: no information for key \"%s\"\n", me, key);
    return;
  }
  biffMsgStrSet(str, msg);

  return;
}

/*
******** biffCheck()
**
** sees how many messages there are for a given key
** returns 0 if the key doesn't exist.
*/
int
biffCheck(const char *key) {
  biffMsg *msg;

  _bmsgStart();
  msg = _bmsgFind(key);
  if (!msg) {
    return 0;
  }
  
  return msg->errNum;
}

/*
******** biffDone()
**
** frees everything associated with given key, and shrinks list of keys,
** and calls _bmsgFinish() if there are no keys left
*/
void
biffDone(const char *key) {
  static const char me[]="biffDone";
  unsigned int idx;
  biffMsg *msg;

  _bmsgStart();
  
  msg = _bmsgFind(key);
  if (!msg) {
    fprintf(stderr, "%s: WARNING: no information for key \"%s\"\n", me, key);
    return;
  }
  idx = _bmsgFindIdx(msg);
  biffMsgNix(msg);
  if (_bmsgNum > 1) {
    /* if we have more than one key in action, move the last biffMsg
       to the position that was just cleared up */
    _bmsg[idx] = _bmsg[_bmsgNum-1];
  }
  airArrayLenIncr(_bmsgArr, -1);
  /* if that was the last key, close shop */
  if (!_bmsgArr->len) {
    _bmsgFinish();
  }

  return;
}

void
biffMove(const char *destKey, const char *err, const char *srcKey) {
  static const char me[]="biffMove";
  biffMsg *dest, *src;

  _bmsgStart();
  dest = _bmsgAdd(destKey);
  src = _bmsgFind(srcKey);
  if (!src) {
    fprintf(stderr, "%s: WARNING: key \"%s\" unknown\n", me, srcKey);
    return;
  }
  biffMsgMove(dest, src, err);
  return;
}

void
biffMoveVL(const char *destKey, const char *srcKey,
           const char *errfmt, va_list args) {
  static const char me[]="biffMovev";
  biffMsg *dest, *src;

  _bmsgStart();
  dest = _bmsgAdd(destKey);
  src = _bmsgFind(srcKey);
  if (!src) {
    fprintf(stderr, "%s: WARNING: key \"%s\" unknown\n", me, srcKey);
    return;
  }
  biffMsgMoveVL(dest, src, errfmt, args);
  return;
}

void
biffMovef(const char *destKey, const char *srcKey,
          const char *errfmt, ...) {
  va_list args;

  va_start(args, errfmt);
  biffMoveVL(destKey, srcKey, errfmt, args);
  va_end(args);
  return;
}

char *
biffGetDone(const char *key) {
  char *ret;

  _bmsgStart();

  ret = biffGet(key);
  biffDone(key);  /* will call _bmsgFinish if this is the last key */

  return ret;
}

void
biffSetStrDone(char *str, const char *key) {

  _bmsgStart();

  biffSetStr(str, key);
  biffDone(key);  /* will call _bmsgFinish if this is the last key */

  return;
}