"Fossies" - the Fresh Open Source Software Archive 
Member "gamgi0.17.5x/src/io/gamgi_io_token.c" (23 Feb 2022, 24360 Bytes) of package /linux/misc/gamgi-all-0.17.5x.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*******************************************
2 *
3 * $GAMGI/src/io/gamgi_io_token.c
4 *
5 * Copyright (C) 2001, 2004 Carlos Pereira
6 *
7 * Distributed under the terms of the GNU
8 * General Public License: $GAMGI/LICENSE
9 *
10 */
11
12 #include "gamgi_engine.h"
13 #include "gamgi_io.h"
14
15 #include <errno.h>
16 #include <ctype.h>
17
18 /************* external function ***********
19 * *
20 * GAMGI_IO_TOKEN_CUT *
21 * *
22 *******************************************/
23
24 void gamgi_io_token_cut (const char *string, char *token, int length)
25 {
26 /********************************************************
27 * length is the true output array length, so the *
28 * maximum number of characters that can be effectively *
29 * copied is length - 1, to save the last position to *
30 * the end character: output[length - 1] = '\0'; *
31 * *
32 * standard strncpy function truncates strings with the *
33 * maximum size (length - 1) without ending the string, *
34 * so adding the terminating '\0' character is required *
35 ********************************************************/
36
37 strncpy (token, string, length - 1);
38 token[length - 1] = '\0';
39 }
40
41 /************* external function ***********
42 * *
43 * GAMGI_IO_TOKEN_CLEAN *
44 * *
45 * Remove all delimiter char around string *
46 *******************************************/
47
48 void gamgi_io_token_clean (const char *string, char *token)
49 {
50 char *delimiters = GAMGI_IO_DELIMITERS;
51 char buffer[GAMGI_ENGINE_LINE];
52
53 strcpy (buffer, string);
54 strcpy (token, strtok (buffer, delimiters));
55 }
56
57 /************** external function ************
58 * *
59 * GAMGI_IO_TOKEN_LOWER *
60 * *
61 * Convert all char in a token to lower case *
62 *********************************************/
63
64 void gamgi_io_token_lower (const char *token, char *lower)
65 {
66 while (*token != '\0') *lower++ = tolower(*token++);
67 *lower = '\0';
68 }
69
70 /****************** external function ***************
71 * *
72 * GAMGI_IO_TOKEN_SIZE_CHECK *
73 * *
74 * Check if the token length is larger than length *
75 * and return the result in a flag TRUE/FALSE. *
76 ****************************************************/
77
78 gamgi_bool gamgi_io_token_size_check (const char *token, int length)
79 {
80 /***********************************************
81 * Check whether token is larger than max char *
82 * To disable this test make length = INT_MAX. *
83 ***********************************************/
84
85 if (strlen (token) > length - 1) return FALSE;
86
87 return TRUE;
88 }
89
90 /*********** external function ***********
91 * *
92 * GAMGI_IO_TOKEN_CHECK *
93 * *
94 * Check string contents: *
95 * 1) return FALSE when only delimiter *
96 * char or no char at all were found. *
97 * 2) return TRUE when something other *
98 * than delimiter char were found. *
99 * This function does NOT check for good *
100 * or bad tokens, only if they exist! *
101 *****************************************/
102
103 gamgi_bool gamgi_io_token_check (const char *string)
104 {
105 char *delimiters = GAMGI_IO_DELIMITERS;
106 char buffer[GAMGI_ENGINE_LINE];
107
108 /****************************************************
109 * If string has more char than GAMGI_ENGINE_LINE, *
110 * then something is wrong. Passing TRUE means that *
111 * something is there, which should trigger a scan *
112 * function, which will report the error. *
113 ****************************************************/
114
115 if (gamgi_io_token_size_check (string, GAMGI_ENGINE_LINE) == FALSE) return TRUE;
116
117 /*************************************
118 * strtok is a destructive function, *
119 * thus use a buffer to save string. *
120 *************************************/
121
122 strcpy (buffer, string);
123 if (strtok (buffer, delimiters) == NULL) return FALSE;
124 else return TRUE;
125 }
126
127 /****************** external function ************
128 * *
129 * GAMGI_IO_TOKEN_ALPHA_CHECK *
130 * *
131 * Check: *
132 * 1) if the token length is larger than length; *
133 * 2) if the token has non accepted char; *
134 * and return the result in a flag TRUE/FALSE. *
135 *************************************************/
136
137 gamgi_bool gamgi_io_token_alpha_check (const char *token,
138 const char *valid, const int length)
139 {
140 /*********************************************************
141 * Argument valid lists every char that can be present *
142 * in argument token. As strspn () returns the number of *
143 * skipped char until an unlisted char is found, in a *
144 * valid token this number must equal the token length. *
145 *********************************************************/
146
147 if (strspn (token, valid) != strlen (token)) return FALSE;
148
149 /***********************************************
150 * Check whether token is larger than max char *
151 ***********************************************/
152
153 if (gamgi_io_token_size_check (token, length) == FALSE) return FALSE;
154
155 return TRUE;
156 }
157
158 /************** external function ************
159 * *
160 * GAMGI_IO_TOKEN_BOOL_GET *
161 * *
162 * Convert a token to bool without checking. *
163 *********************************************/
164
165 void gamgi_io_token_bool_get (const char *token, gamgi_bool *bool)
166 {
167 if (strcmp (token, "yes") == 0) *bool = TRUE;
168 else if (strcmp (token, "no") == 0) *bool = FALSE;
169 }
170
171 /**************** external function **********
172 * *
173 * GAMGI_IO_TOKEN_LONG_GET *
174 * *
175 * Convert a token to long without checking. *
176 *********************************************/
177
178 void gamgi_io_token_long_get (const char *token, long *integer)
179 {
180 char *end;
181
182 *integer = strtol (token, &end, 10);
183 }
184
185 /*************** external function **********
186 * *
187 * GAMGI_IO_TOKEN_INT_GET *
188 * *
189 * Convert a token to int without checking. *
190 *********************************************/
191
192 void gamgi_io_token_integer_get (const char *token, int *integer)
193 {
194 char *end;
195
196 *integer = (int) strtol (token, &end, 10);
197 }
198
199 /**************** external function ************
200 * *
201 * GAMGI_IO_TOKEN_DOUBLE_GET *
202 * *
203 * Convert a token to double without checking. *
204 ***********************************************/
205
206 void gamgi_io_token_double_get (const char *token, double *real)
207 {
208 char *end;
209
210 *real = strtod (token, &end);
211 }
212
213 /*************** external function ************
214 * *
215 * GAMGI_IO_TOKEN_FLOAT_GET *
216 * *
217 * Convert a token to float without checking. *
218 ***********************************************/
219
220 void gamgi_io_token_float_get (const char *token, float *real)
221 {
222 char *end;
223
224 *real = (float) strtod (token, &end);
225 }
226
227 /*********** external function ********
228 * *
229 * GAMGI_IO_TOKEN_LONG_GET_CHECK *
230 * *
231 * Convert a token to long, checking: *
232 * 1) if it contains wrong char; *
233 * 2) if it is out of range; *
234 * 3) if it is smaller than min; *
235 * 4) if it is larger than max; *
236 **************************************/
237
238 gamgi_bool gamgi_io_token_long_get_check (const char *token,
239 long *integer, const long min, const long max)
240 {
241 char *end;
242
243 /*****************************************************
244 * Reset errno, the error variable. This is required *
245 * because some C functions set errno when things go *
246 * wrong but don't set errno when things go right. *
247 *****************************************************/
248
249 errno = 0;
250
251 *integer = strtol (token, &end, 10);
252
253 /*****************************************************
254 * Check to guarantee that the input token is clean. *
255 * If a invalid char is found then strtol () stops *
256 * and *end contains the faulty char, otherwise it *
257 * will contain the '\0' char provided by strtok (). *
258 *****************************************************/
259
260 if (*end != '\0') return FALSE;
261
262 /********************************************************
263 * Check to guarantee that the converted integer is *
264 * inside the range of values accepted by the compiler. *
265 ********************************************************/
266
267 if (errno == ERANGE) return FALSE;
268
269 /**************************************************
270 * Check to guarantee that the converted integer *
271 * is not smaller than min or larger than max. *
272 * To disable these tests, make min = -LONG_MAX *
273 * or max = LONG_MAX when calling this function. *
274 **************************************************/
275
276 if (*integer < min) return FALSE;
277 if (*integer > max) return FALSE;
278
279 return TRUE;
280 }
281
282 /*********** external function ********
283 * *
284 * GAMGI_IO_TOKEN_INT_GET_CHECK *
285 * *
286 * Convert a token to int, checking: *
287 * 1) if it contains wrong char; *
288 * 2) if it is out of range; *
289 * 3) if it is smaller than min; *
290 * 4) if it is larger than max; *
291 **************************************/
292
293 gamgi_bool gamgi_io_token_int_get_check (const char *token,
294 int *integer, const int min, const int max)
295 {
296 char *end;
297
298 /*****************************************************
299 * Reset errno, the error variable. This is required *
300 * because some C functions set errno when things go *
301 * wrong but don't set errno when things go right. *
302 *****************************************************/
303
304 errno = 0;
305
306 *integer = (int) strtol (token, &end, 10);
307
308 /*****************************************************
309 * Check to guarantee that the input token is clean. *
310 * If a invalid char is found then strtol () stops *
311 * and *end contains the faulty char, otherwise it *
312 * will contain the '\0' char provided by strtok (). *
313 *****************************************************/
314
315 if (*end != '\0') return FALSE;
316
317 /********************************************************
318 * Check to guarantee that the converted integer is *
319 * inside the range of values accepted by the compiler. *
320 ********************************************************/
321
322 if (errno == ERANGE) return FALSE;
323
324 /*************************************************
325 * Check to guarantee that the converted integer *
326 * is not smaller than min or larger than max. *
327 * To disable these tests, make min = INT_MIN *
328 * or max = INT_MAX when calling this function. *
329 *************************************************/
330
331 if (*integer < min) return FALSE;
332 if (*integer > max) return FALSE;
333
334 return TRUE;
335 }
336
337 /*********** external function **********
338 * *
339 * GAMGI_IO_TOKEN_DOUBLE_GET_CHECK *
340 * *
341 * Convert a token to double, checking: *
342 * 1) if it contains wrong char; *
343 * 2) if it is out of range; *
344 * 3) if it is smaller than min; *
345 * 4) if it is larger than max; *
346 ****************************************/
347
348 gamgi_bool gamgi_io_token_double_get_check (const char *token,
349 double *real, const double min, const double max)
350 {
351 char *end;
352
353 /*****************************************************
354 * Reset errno, the error variable. This is required *
355 * because some C functions set errno when things go *
356 * wrong but don't set errno when things go right. *
357 *****************************************************/
358
359 errno = 0;
360
361 *real = strtod (token, &end);
362
363 /*****************************************************
364 * Check to guarantee that the input token is clean. *
365 * If a invalid char is found then strtod () stops *
366 * and *end contains the faulty char, otherwise it *
367 * will contain the '\0' char provided by strtok (). *
368 *****************************************************/
369
370 if (*end != '\0') return FALSE;
371
372 /********************************************************
373 * Check to guarantee that the converted real is *
374 * inside the range of values accepted by the compiler. *
375 ********************************************************/
376
377 if (errno == ERANGE) return FALSE;
378
379 /*****************************************
380 * Check to guarantee that the converted *
381 * real is not a NAN (Not A Number) *
382 *****************************************/
383
384 if (*real != *real) return FALSE;
385
386 /*************************************************
387 * Check to guarantee that the converted real *
388 * is not smaller than min or larger than max. *
389 * To disable these tests, make min = -DBL_MAX *
390 * or max = DBL_MAX when calling this function. *
391 *************************************************/
392
393 if (*real < min) return FALSE;
394 if (*real > max) return FALSE;
395
396 return TRUE;
397 }
398
399 /*********** external function **********
400 * *
401 * GAMGI_IO_TOKEN_FLOAT_GET_CHECK *
402 * *
403 * Convert a token to float, checking: *
404 * 1) if it contains wrong char; *
405 * 2) if it is out of range; *
406 * 3) if it is smaller than min; *
407 * 4) if it is larger than max; *
408 ****************************************/
409
410 gamgi_bool gamgi_io_token_float_get_check (const char *token,
411 float *real, const float min, const float max)
412 {
413 char *end;
414
415 /*****************************************************
416 * Reset errno, the error variable. This is required *
417 * because some C functions set errno when things go *
418 * wrong but don't set errno when things go right. *
419 *****************************************************/
420
421 errno = 0;
422
423 *real = (float) strtod (token, &end);
424
425 /*****************************************************
426 * Check to guarantee that the input token is clean. *
427 * If a invalid char is found then strtod () stops *
428 * and *end contains the faulty char, otherwise it *
429 * will contain the '\0' char provided by strtok (). *
430 *****************************************************/
431
432 if (*end != '\0') return FALSE;
433
434 /********************************************************
435 * Check to guarantee that the converted real is *
436 * inside the range of values accepted by the compiler. *
437 ********************************************************/
438
439 if (errno == ERANGE) return FALSE;
440
441 /*****************************************
442 * Check to guarantee that the converted *
443 * real is not a NAN (Not A Number) *
444 *****************************************/
445
446 if (*real != *real) return FALSE;
447
448 /*************************************************
449 * Check to guarantee that the converted integer *
450 * is not smaller than min or larger than max. *
451 * To disable these tests, make min = -FLT_MAX *
452 * or max = FLT_MAX when calling this function. *
453 *************************************************/
454
455 if (*real < min) return FALSE;
456 if (*real > max) return FALSE;
457
458 return TRUE;
459 }
460
461 /*********** external function **********
462 * *
463 * GAMGI_IO_TOKEN_ALPHA_SCAN *
464 * *
465 * Convert a string to alpha, checking: *
466 * 1) if it contains less than 1 token; *
467 * 2) if it is a good alpha; *
468 * 3) if it contains more than 1 token; *
469 ****************************************/
470
471 gamgi_bool gamgi_io_token_alpha_scan (const char *string,
472 char *alpha, const char *valid, const int length)
473 {
474 char *delimiters = GAMGI_IO_DELIMITERS;
475 char buffer[GAMGI_ENGINE_LINE];
476 char *token;
477
478 if (gamgi_io_token_size_check (string,
479 GAMGI_ENGINE_LINE) == FALSE) return FALSE;
480
481 /*************************************
482 * strtok is a destructive function, *
483 * thus use a buffer to save string. *
484 *************************************/
485
486 strcpy (buffer, string);
487 token = strtok (buffer, delimiters);
488 if (token == NULL) return FALSE;
489
490 if (gamgi_io_token_alpha_check (token,
491 valid, length) == FALSE) return FALSE;
492
493 strcpy (alpha, token);
494
495 token = strtok (NULL, delimiters);
496 if (token != NULL) return FALSE;
497
498 return TRUE;
499 }
500
501 /************** external function ************
502 * *
503 * GAMGI_IO_TOKEN_BOOL_SCAN *
504 * *
505 * Convert a string to bool, checking: *
506 * 1) if it contains exactly one good alpha; *
507 * 2) if this alpha is "yes" or "no"; *
508 *********************************************/
509
510 gamgi_bool gamgi_io_token_bool_scan (const char *string, gamgi_bool *bool)
511 {
512 char token[GAMGI_ENGINE_TOKEN];
513
514 if (gamgi_io_token_alpha_scan (string, token,
515 GAMGI_IO_TEXT, GAMGI_ENGINE_TOKEN) == FALSE) return FALSE;
516
517 if (strcmp (token, "yes") == 0) *bool = TRUE;
518 else if (strcmp (token, "no") == 0) *bool = FALSE;
519 else return FALSE;
520
521 return TRUE;
522 }
523
524 /************* external function ********
525 * *
526 * GAMGI_IO_TOKEN_LONG_SCAN *
527 * *
528 * Convert a string to long, checking: *
529 * 1) if it contains less than 1 token; *
530 * 2) if it is a good long; *
531 * 3) if it contains more than 1 token; *
532 ****************************************/
533
534 gamgi_bool gamgi_io_token_long_scan (const char *string,
535 long *integer, const long min, const long max)
536 {
537 char *delimiters = GAMGI_IO_DELIMITERS;
538 char *token, buffer[GAMGI_ENGINE_LINE];
539
540 if (gamgi_io_token_size_check (string, GAMGI_ENGINE_LINE) == FALSE) return FALSE;
541
542 /*************************************
543 * strtok is a destructive function, *
544 * thus use a buffer to save string. *
545 *************************************/
546
547 strcpy (buffer, string);
548 token = strtok (buffer, delimiters);
549 if (token == NULL) return FALSE;
550
551 if (gamgi_io_token_long_get_check (token, integer, min, max) == FALSE) return FALSE;
552
553 token = strtok (NULL, delimiters);
554 if (token != NULL) return FALSE;
555
556 return TRUE;
557 }
558
559 /************* external function ********
560 * *
561 * GAMGI_IO_TOKEN_INT_SCAN *
562 * *
563 * Convert a string to int, checking: *
564 * 1) if it contains less than 1 token; *
565 * 2) if it is a good long; *
566 * 3) if it contains more than 1 token; *
567 ****************************************/
568
569 gamgi_bool gamgi_io_token_int_scan (const char *string,
570 int *integer, const int min, const int max)
571 {
572 char *delimiters = GAMGI_IO_DELIMITERS;
573 char *token, buffer[GAMGI_ENGINE_LINE];
574
575 if (gamgi_io_token_size_check (string, GAMGI_ENGINE_LINE) == FALSE) return FALSE;
576
577 /*************************************
578 * strtok is a destructive function, *
579 * thus use a buffer to save string. *
580 *************************************/
581
582 strcpy (buffer, string);
583 token = strtok (buffer, delimiters);
584 if (token == NULL) return FALSE;
585
586 if (gamgi_io_token_int_get_check (token, integer, min, max) == FALSE) return FALSE;
587
588 token = strtok (NULL, delimiters);
589 if (token != NULL) return FALSE;
590
591 return TRUE;
592 }
593
594 /************ external function **********
595 * *
596 * GAMGI_IO_TOKEN_DOUBLE_SCAN *
597 * *
598 * Convert a string to double, checking: *
599 * 1) if it contains less than 1 token; *
600 * 2) if it is a good double; *
601 * 3) if it contains more than 1 token; *
602 *****************************************/
603
604 gamgi_bool gamgi_io_token_double_scan (const char *string,
605 double *real, const double min, const double max)
606 {
607 char *delimiters = GAMGI_IO_DELIMITERS;
608 char *token, buffer[GAMGI_ENGINE_LINE];
609
610 if (gamgi_io_token_size_check (string, GAMGI_ENGINE_LINE) == FALSE) return FALSE;
611
612 /*************************************
613 * strtok is a destructive function, *
614 * thus use a buffer to save string. *
615 *************************************/
616
617 strcpy (buffer, string);
618 token = strtok (buffer, delimiters);
619 if (token == NULL) return FALSE;
620
621 if (gamgi_io_token_double_get_check (token, real, min, max) == FALSE) return FALSE;
622
623 token = strtok (NULL, delimiters);
624 if (token != NULL) return FALSE;
625
626 return TRUE;
627 }
628
629 /************ external function **********
630 * *
631 * GAMGI_IO_TOKEN_FLOAT_SCAN *
632 * *
633 * Convert a string to float, checking: *
634 * 1) if it contains less than 1 token; *
635 * 2) if it is a good double; *
636 * 3) if it contains more than 1 token; *
637 *****************************************/
638
639 gamgi_bool gamgi_io_token_float_scan (const char *string,
640 float *real, const float min, const float max)
641 {
642 char *delimiters = GAMGI_IO_DELIMITERS;
643 char *token, buffer[GAMGI_ENGINE_LINE];
644
645 if (gamgi_io_token_size_check (string, GAMGI_ENGINE_LINE) == FALSE) return FALSE;
646
647 /*************************************
648 * strtok is a destructive function, *
649 * thus use a buffer to save string. *
650 *************************************/
651
652 strcpy (buffer, string);
653 token = strtok (buffer, delimiters);
654 if (token == NULL) return FALSE;
655
656 if (gamgi_io_token_float_get_check (token, real, min, max) == FALSE) return FALSE;
657
658 token = strtok (NULL, delimiters);
659 if (token != NULL) return FALSE;
660
661 return TRUE;
662 }
663
664 /*************** external function ***************
665 * *
666 * GAMGI_IO_TOKEN_ALPHA_LONG_SCAN *
667 * *
668 * Convert a string to alpha and long, checking: *
669 * 1) if it contains less than 1 token; *
670 * 2) if it is a good alpha; *
671 * 3) if it contains only than 1 token; *
672 * 4) if it is a good long; *
673 * 5) if it contains more than 2 token; *
674 *************************************************/
675
676 gamgi_bool gamgi_io_token_alpha_long_scan (const char *string,
677 char *alpha, long *integer, const int length,
678 const long min, const long max)
679 {
680 char *delimiters = GAMGI_IO_DELIMITERS;
681 char *token, buffer[GAMGI_ENGINE_LINE];
682
683 if (gamgi_io_token_size_check (string, GAMGI_ENGINE_LINE) == FALSE) return FALSE;
684
685 /*************************************
686 * strtok is a destructive function, *
687 * thus use a buffer to save string. *
688 *************************************/
689
690 strcpy (buffer, string);
691 token = strtok (buffer, delimiters);
692 if (token == NULL) return FALSE;
693
694 if (gamgi_io_token_alpha_check (token, GAMGI_IO_TEXT, length) == FALSE) return FALSE;
695
696 strcpy (alpha, token);
697
698 token = strtok (NULL, delimiters);
699 if (token == NULL) return FALSE;
700
701 if (gamgi_io_token_long_get_check (token, integer, min, max) == FALSE) return FALSE;
702
703 token = strtok (NULL, delimiters);
704 if (token != NULL) return FALSE;
705
706 return TRUE;
707 }
708
709 void gamgi_io_token_remove (char **new)
710 {
711 free (*new);
712 *new = NULL;
713 }
714
715 void gamgi_io_token_create (const char *old, char **new)
716 {
717 int length;
718
719 /****************************************
720 * copying NULL to NULL is not an error *
721 ****************************************/
722
723 if (old == NULL) *new = NULL;
724 else
725 {
726 /**********************************
727 * create and start the new token *
728 **********************************/
729
730 length = strlen (old);
731 *new = (char *) malloc ((length + 1) * sizeof (char));
732 strcpy (*new, old);
733 }
734 }
735
736 gamgi_bool gamgi_io_token_check_create (const char *old, char **new, const int max)
737 {
738 int length;
739
740 /****************************************
741 * copying NULL to NULL is not an error *
742 ****************************************/
743
744 if (old == NULL)
745 {
746 *new = NULL;
747 return TRUE;
748 }
749
750 /***********************************************
751 * Check whether token is larger than max char *
752 ***********************************************/
753
754 if (gamgi_io_token_size_check (old, max) == FALSE)
755 {
756 *new = NULL;
757 return FALSE;
758 }
759
760 /**********************************
761 * create and start the new token *
762 **********************************/
763
764 length = strlen (old);
765 *new = (char *) malloc ((length + 1) * sizeof (char));
766 strcpy (*new, old);
767
768 return TRUE;
769 }