Debian Codegraph @

pconf/utils.c

raw symbols SCIP
1 #include "pconf.h"
2
3 void FREE(void *p);
4 int STRLEN(const char *s);
5 int STRISYES(const char *s);
6 char *STRDUP(const char *s);
7 const char *STRCHR(const char *s, int c);
8 void STRSTRIP(char *s);
9 int STRCASECMP(const char *s1, const char *s2);
10 int STRPFX(const char *str, const char *pfx);
11 int STRCASEPFX(const char *str, const char *pfx);
12 extern char *STRLISTAPPEND(char ***list, int *total, const char *s);
13 extern void STRFREEARRAY(char **list, int total);
14
15 char *GETENV(const char *parameter);
16
17 FILE *FOPEN(const char *path, const char *mode);
18 void FCLOSE(FILE *fp);
19 int FGETC(FILE *fp);
20 int FPUTC(int c, FILE *fp);
21 void FSEEKNEXTLINE(FILE *fp);
22 void FSEEKPASTSPACES(FILE *fp);
23 char *FGETLINE(FILE *fp);
24 char *FSEEKNEXTPARAMETER(
25 FILE *fp, char *buf, char comment, char delim
26 );
27
28
29
30 /*
31 * Safer form of free().
32 */
33 void FREE(void *p)
34 {
35 if(p == NULL)
36 return;
37
38 free(p);
39
40 return;
41 }
42
43 /*
44 * Returns length of string, does not distingush error.
45 */
46 int STRLEN(const char *s)
47 {
48 int len = 0;
49
50 if(s == NULL)
51 return(0);
52
53 while((*s) != '\0')
54 {
55 len++;
56 s++;
57 }
58
59 return(len);
60 }
61
62 /*
63 * Returns true if string s implies a `yes'.
64 */
65 int STRISYES(const char *s)
66 {
67 if(s == NULL)
68 return(0);
69
70 while(ISBLANK(*s))
71 s++;
72
73 /* Yes? */
74 if(toupper(*s) == 'Y')
75 return(1);
76 /* On? */
77 if(STRCASEPFX(s, "ON"))
78 return(1);
79 /* True? */
80 if(toupper(*s) == 'T')
81 return(1);
82 /* All else non-zero number? */
83 if((*s) != '0')
84 return(1);
85
86 return(0);
87 }
88
89 /*
90 * Safer form of strdup().
91 */
92 char *STRDUP(const char *s)
93 {
94 int len;
95 char *sr = NULL, *sr_ptr;
96
97 if(s == NULL)
98 return(sr);
99
100 len = STRLEN(s);
101 sr = sr_ptr = (char *)malloc((len + 1) * sizeof(char));
102 if(sr == NULL)
103 return(sr);
104
105 while((*s) != '\0')
106 {
107 *sr_ptr++ = *s++;
108 }
109 (*sr_ptr) = '\0';
110
111 return(sr);
112 }
113
114 /*
115 * Returns the position of c in s or NULL on no match.
116 */
117 const char *STRCHR(const char *s, int c)
118 {
119 if((s == NULL) || ((char)c == '\0'))
120 return(NULL);
121
122 while((*s) != '\0')
123 {
124 if((*s) == (char)c)
125 return(s);
126 s++;
127 }
128
129 return(NULL);
130 }
131
132 /*
133 * Strips blank characters leading and tailing string s.
134 */
135 void STRSTRIP(char *s)
136 {
137 int tar, src, lead, tail;
138
139 if(s == NULL)
140 return;
141 if((*s) == '\0')
142 return;
143
144 /* Strip leading blank characters. */
145 lead = 0;
146 while(ISBLANK(s[lead]))
147 lead++;
148
149 if(lead > 0)
150 {
151 for(tar = 0, src = lead; s[src] != '\0'; tar++, src++)
152 s[tar] = s[src];
153
154 s[tar] = '\0';
155
156 /* Calculate tail position. */
157 tail = (tar > 0) ? tar - 1 : 0;
158 }
159 else
160 {
161 /* Calculate tail position. */
162 tar = STRLEN(s);
163 tail = (tar > 0) ? tar - 1 : 0;
164 }
165
166 /* Strip tailing blank characters. */
167 for(tar = tail; tar >= 0; tar--)
168 {
169 if(ISBLANK(s[tar]))
170 s[tar] = '\0';
171 else
172 break;
173 }
174
175 return;
176 }
177
178
179 /*
180 * Returns 1 for no match and 0 for match.
181 */
182 int STRCASECMP(const char *s1, const char *s2)
183 {
184 if((s1 == NULL) ||
185 (s2 == NULL)
186 )
187 return(1); /* False. */
188
189 while((*s1) && (*s2))
190 {
191 if(toupper(*s1) != toupper(*s2))
192 return(1); /* False. */
193
194 s1++;
195 s2++;
196 }
197
198 if((*s1) == (*s2))
199 return(0); /* True. */
200 else
201 return(1); /* False. */
202 }
203
204 /*
205 * Returns true if pfx is a prefix of str (case insensitive),
206 * otherwise returns false.
207 */
208 int STRPFX(const char *str, const char *pfx)
209 {
210 /* If either strings is NULL, return false. */
211 if((str == NULL) ||
212 (pfx == NULL)
213 )
214 return(0);
215
216 /* If pfx contains no characters, return false. */
217 if((*pfx) == '\0')
218 return(0);
219
220 /* Begin prefix matching. */
221 while((*pfx) != '\0')
222 {
223 if((*str) != (*pfx))
224 return(0);
225
226 str++; pfx++;
227 }
228
229 return(1);
230 }
231
232 /*
233 * Returns true if pfx is a prefix of str (case insensitive),
234 * otherwise returns false.
235 */
236 int STRCASEPFX(const char *str, const char *pfx)
237 {
238 /* If either strings is NULL, return false. */
239 if((str == NULL) ||
240 (pfx == NULL)
241 )
242 return(0);
243
244 /* If pfx contains no characters, return false. */
245 if((*pfx) == '\0')
246 return(0);
247
248 /* Begin prefix matching. */
249 while((*pfx) != '\0')
250 {
251 if(toupper(*str) != toupper(*pfx))
252 return(0);
253
254 str++; pfx++;
255 }
256
257 return(1);
258 }
259
260 /*
261 * Dupliates string s (may not be NULL) and adds it to the given
262 * list.
263 *
264 * Reallocates the given list and increases the total.
265 *
266 * Returns pointer to duplicated string or NULL on error.
267 */
268 char *STRLISTAPPEND(char ***list, int *total, const char *s)
269 {
270 int i;
271 char *ns;
272
273 if((list == NULL) || (total == NULL) || (s == NULL))
274 return(NULL);
275
276 if((*total) < 0)
277 (*total) = 0;
278
279 ns = STRDUP(s);
280 if(ns == NULL)
281 return(NULL);
282
283 i = (*total);
284 (*total) = i + 1;
285
286 (*list) = (char **)realloc(
287 *list, (*total) * sizeof(char *)
288 );
289 if((*list) == NULL)
290 {
291 (*total) = 0;
292 FREE(ns);
293 return(NULL);
294 }
295
296 (*list)[i] = ns;
297
298 return(ns);
299 }
300
301 /*
302 * Deallocates the given list of strings.
303 */
304 void STRFREEARRAY(char **list, int total)
305 {
306 int i;
307
308 if(list == NULL)
309 return;
310
311 for(i = 0; i < total; i++)
312 FREE(list[i]);
313
314 FREE(list);
315
316 return;
317 }
318
319
320 /*
321 * Safe form of getenv().
322 */
323 char *GETENV(const char *parameter)
324 {
325 if(parameter == NULL)
326 return(NULL);
327
328 return(getenv(parameter));
329 }
330
331
332 /*
333 * Opens a file, returns its handle or NULL on failure. Works
334 * just like ansi fopen().
335 */
336 FILE *FOPEN(const char *path, const char *mode)
337 {
338 if((path == NULL) || (mode == NULL))
339 return(NULL);
340 else
341 return(fopen(path, mode));
342 }
343
344 /*
345 * Closes a file opened by FOPEN.
346 */
347 void FCLOSE(FILE *fp)
348 {
349 if(fp == NULL)
350 return;
351
352 fclose(fp);
353
354 return;
355 }
356
357 /*
358 * Safe form of fgetc.
359 */
360 int FGETC(FILE *fp)
361 {
362 if(fp == NULL)
363 return(EOF);
364 else
365 return(fgetc(fp));
366 }
367
368 /*
369 * Safe form of fputc.
370 */
371 int FPUTC(int c, FILE *fp)
372 {
373 if(fp == NULL)
374 return(EOF);
375 else
376 return(fputc(c, fp));
377 }
378
379
380 /*
381 * Seeks to next line, escape sequences will be parsed.
382 */
383 void FSEEKNEXTLINE(FILE *fp)
384 {
385 int c;
386
387 if(fp == NULL)
388 return;
389
390 do
391 {
392 c = FGETC(fp);
393
394 /* Escape sequence? */
395 if(c == '\\')
396 c = FGETC(fp);
397 /* New line? */
398 else if(ISCR(c))
399 break;
400
401 } while(c != EOF);
402
403 return;
404 }
405
406 /*
407 * Seeks fp past any spaces.
408 */
409 void FSEEKPASTSPACES(FILE *fp)
410 {
411 int c;
412
413 if(fp == NULL)
414 return;
415
416 while(1)
417 {
418 c = FGETC(fp);
419 if(c == EOF)
420 break;
421
422 if(ISBLANK(c))
423 continue;
424
425 fseek(fp, -1, SEEK_CUR);
426 break;
427 }
428
429 return;
430 }
431
432 /*
433 * String is loaded literally and only escape sequence handled is
434 * the occurance of two characters '\\' '\n' and where the '\n'
435 * character will be saved into the return string.
436 *
437 * Spaces will not be striped, the fp will be positioned after the
438 * non-escaped newline or EOF (whichever is encountered first).
439 *
440 * Return must be deallocated by calling function.
441 */
442 char *FGETLINE(FILE *fp)
443 {
444 int c, i, len = 0;
445 char *strptr = NULL;
446 char *strptr2;
447
448 if(fp == NULL)
449 return(strptr);
450
451 /* Begin reading string from file. */
452
453 /* Get first character. */
454 c = FGETC(fp);
455 if(c == EOF)
456 return(strptr);
457
458 /* Read string. */
459 while(1)
460 {
461 i = len; /* Current string index i. */
462 len++; /* Current string length. */
463
464 /* Reallocate string buffer. */
465 strptr = (char *)realloc(strptr, len * sizeof(char));
466 if(strptr == NULL)
467 break;
468
469 strptr2 = &(strptr[i]); /* Pointer to current string index. */
470 (*strptr2) = c; /* Set new character value. */
471
472 /* End of the line? */
473 if((c == EOF) ||
474 ISCR(c)
475 )
476 {
477 (*strptr2) = '\0';
478 break;
479 }
480 /* Escape sequence? */
481 else if(c == '\\')
482 {
483 /* Read next character after backslash. */
484 c = FGETC(fp);
485 if(c == EOF)
486 {
487 continue;
488 }
489 else if(ISCR(c))
490 {
491 /* New line, store it as is. */
492 (*strptr2) = c;
493 }
494 else if(c == '\\')
495 {
496 /* Another backslash, store as a single backslash. */
497 (*strptr2) = c;
498 }
499 else
500 {
501 /* All other escaped characters leave as is
502 * it will be set on the next loop.
503 */
504 continue;
505 }
506
507 /* Read next character. */
508 c = FGETC(fp);
509 }
510 /* Regular character. */
511 else
512 {
513 /* Read next character. */
514 c = FGETC(fp);
515 }
516 }
517
518 return(strptr);
519 }
520
521 /*
522 * Fetches the next parameter found at the file position fp.
523 *
524 * If buf is NULL then a newly allocated string will be returned
525 * containing the fetched parm. If buf is not NULL, then buf will
526 * be realloc()'ed and returned as a new pointer containing
527 * the fetched parm.
528 *
529 * If EOF is reached by the given fp position, then NULL will
530 * be returned and the given buf will have been free()ed by this
531 * function.
532 */
533 char *FSEEKNEXTPARAMETER(
534 FILE *fp, char *buf, char comment, char delim
535 )
536 {
537 #ifndef FREAD_ALLOC_CHUNK_SIZE
538 # define FREAD_ALLOC_CHUNK_SIZE 8
539 #endif
540 int c, buf_pos = 0, buf_len, buf_inc = FREAD_ALLOC_CHUNK_SIZE;
541
542 if(fp == NULL)
543 {
544 FREE(buf);
545 return(NULL);
546 }
547
548 /* Get length of buf (less than actual allocated is okay). */
549 buf_len = ((buf == NULL) ? 0 : STRLEN(buf));
550
551 /* Seek past spaces and comments to next parameter. */
552 while(1)
553 {
554 FSEEKPASTSPACES(fp);
555 c = FGETC(fp);
556 if(c == EOF)
557 {
558 FREE(buf);
559 return(NULL);
560 }
561 else if(c == comment)
562 {
563 FSEEKNEXTLINE(fp);
564 continue;
565 }
566 else if(ISCR(c))
567 {
568 continue;
569 }
570 else
571 {
572 fseek(fp, -1, SEEK_CUR);
573 break;
574 }
575 }
576
577 /* Begin fetching this parm. */
578 while(1)
579 {
580 /* Get next char. */
581 c = FGETC(fp);
582 if(c == EOF)
583 {
584 break;
585 }
586
587 /* Blank character reached? */
588 if(ISBLANK(c))
589 {
590 /* Blank char reached, seek past delimiantor and position
591 * fp at beginning of value.
592 */
593 if(delim == '\0')
594 {
595 FSEEKPASTSPACES(fp);
596 }
597 else
598 {
599 FSEEKPASTSPACES(fp);
600
601 /* Seek to deim or newline. */
602 do
603 {
604 c = FGETC(fp);
605 if((c == EOF) || (c == delim))
606 break;
607
608 if(ISCR(c))
609 {
610 fseek(fp, -1, SEEK_CUR);
611 break;
612 }
613
614 } while(1);
615
616 FSEEKPASTSPACES(fp);
617 }
618 break;
619 }
620
621 /* CR reached? */
622 if(ISCR(c))
623 {
624 fseek(fp, -1, SEEK_CUR);
625 break;
626 }
627
628 /* Deliminator reached? */
629 if(c == delim)
630 {
631 FSEEKPASTSPACES(fp);
632 break;
633 }
634
635 /* Need to allocate buffer? */
636 if(buf_pos <= buf_len)
637 {
638 buf_len += buf_inc;
639
640 buf = (char *)realloc(buf, buf_len * sizeof(char));
641 if(buf == NULL)
642 {
643 FSEEKNEXTLINE(fp);
644 return(NULL);
645 }
646 }
647
648 buf[buf_pos] = (char)c;
649 buf_pos++;
650 }
651
652 /* Put null terminating byte on buffer. */
653 if(c == EOF)
654 {
655 FREE(buf);
656 buf = NULL;
657 }
658 else
659 {
660 if(buf_pos <= buf_len)
661 {
662 buf_len = buf_pos + 1;
663
664 buf = (char *)realloc(buf, buf_len * sizeof(char));
665 if(buf == NULL)
666 return(NULL);
667 }
668 buf[buf_pos] = '\0';
669 }
670
671 return(buf);
672 }