Provides functions for scanning and parsing free-format data.
This file contains functions such as get_double(), get_float(), get_long(), get_token(), and get_token_buf() to facilitate easy scanning and parsing of numerical and token-based data from strings.
- Copyright
- (c) 2002 The University of Chicago, as Operator of Argonne National Laboratory.
- (c) 2002 The Regents of the University of California, as Operator of Los Alamos National Laboratory.
- License
- This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
- Author
- M. Borland, C. Saunders, R. Soliday, H. Shang
Definition in file data_scan.c.
#include "mdb.h"
#include <ctype.h>
Go to the source code of this file.
|
| int | get_double (double *dptr, char *s) |
| | Parses a double value from the given string.
|
| |
| int | get_longdouble (long double *dptr, char *s) |
| | Parses a long double value from the given string.
|
| |
| int | get_double1 (double *dptr, char *s) |
| | Parses a double value from the given string without modifying the string.
|
| |
| int | get_double1_old (double *dptr, char *s) |
| |
| int | get_float (float *fptr, char *s) |
| | Parses a float value from the given string.
|
| |
| int | get_long (long *iptr, char *s) |
| | Parses a long integer value from the given string.
|
| |
| int | get_long1 (long *lptr, char *s) |
| | Parses a long integer value from the given string without modifying the string.
|
| |
| int | get_long1_old (long *iptr, char *s) |
| |
| int | get_short (short *iptr, char *s) |
| | Parses a short integer value from the given string.
|
| |
| int | get_int (int *iptr, char *s) |
| | Parses an integer value from the given string.
|
| |
| char * | get_token (char *s) |
| | Extracts the next token from the input string.
|
| |
| char * | get_token_buf (char *s, char *buf, long lbuf) |
| | Extracts the next token from the input string into a provided buffer.
|
| |
| long | tokenIsInteger (char *token) |
| | Checks if the given token represents a valid integer.
|
| |
| long | tokenIsNumber (char *token) |
| | Checks if the given token represents a valid number.
|
| |
◆ get_double()
| int get_double |
( |
double * | dptr, |
|
|
char * | s ) |
Parses a double value from the given string.
This function scans the input string for a valid double-precision floating-point number, stores the result in the provided pointer, and updates the string pointer to the position following the parsed number.
- Parameters
-
| [out] | dptr | Pointer to store the parsed double value. |
| [in,out] | s | Input string to scan. It will be modified to point after the parsed number. |
- Returns
- Returns 1 if a double was successfully parsed, 0 otherwise.
Definition at line 40 of file data_scan.c.
40 {
41 register char *ptr0;
42 register int was_point = 0;
43
44
45 ptr0 = s;
46 while (!float_start(s) && *s)
47 s++;
48 if (*s == 0)
49 return (0);
50
51
52 sscanf(s, "%lf", dptr);
53
54
55
56
57 if (*s == '-' || *s == '+')
58 s++;
59
60
61 while (isdigit(*s) || (*s == '.' && !was_point))
62 if (*s++ == '.')
63 was_point = 1;
64
65 if (*s == 'e' || *s == 'E') {
66 s++;
67 if (*s == '+' || *s == '-')
68 s++;
69 while (isdigit(*s))
70 s++;
71 }
72
74 return (1);
75}
char * strcpy_ss(char *dest, const char *src)
Safely copies a string, handling memory overlap.
◆ get_double1()
| int get_double1 |
( |
double * | dptr, |
|
|
char * | s ) |
Parses a double value from the given string without modifying the string.
This function scans the input string for a valid double-precision floating-point number, stores the result in the provided pointer, and ensures that the string remains unchanged after parsing.
- Parameters
-
| [out] | dptr | Pointer to store the parsed double value. |
| [in] | s | Input string to scan. It remains unchanged after parsing. |
- Returns
- Returns 1 if a double was successfully parsed, 0 otherwise.
Definition at line 133 of file data_scan.c.
133 {
134 char *endptr;
135 double val;
136
137 if (s == NULL || *s == '\0')
138 return 0;
139
140
141 char *p = s + strlen(s) - 1;
142 while (p >= s && isspace((unsigned char)*p))
143 p--;
144 *(p + 1) = '\0';
145
146
147 char *start = s;
148 while (*start) {
149 val = strtod(start, &endptr);
150
151 if (endptr != start) {
152
153 if (*endptr == '\0') {
154 *dptr = val;
155 return 1;
156 }
157 }
158 start++;
159 }
160 return 0;
161}
◆ get_double1_old()
| int get_double1_old |
( |
double * | dptr, |
|
|
char * | s ) |
Definition at line 163 of file data_scan.c.
163 {
164 register int was_point = 0;
165
166
167 while (!float_start(s) && *s)
168 s++;
169 if (*s == 0)
170 return (0);
171
172
173 sscanf(s, "%lf", dptr);
174
175
176
177
178 if (*s == '-' || *s == '+')
179 s++;
180
181
182 while (isdigit(*s) || (*s == '.' && !was_point))
183 if (*s++ == '.')
184 was_point = 1;
185
186 if (*s == 'e' || *s == 'E') {
187 s++;
188 if (*s == '+' || *s == '-')
189 s++;
190 while (isdigit(*s))
191 s++;
192 }
193
194 return (1);
195}
◆ get_float()
| int get_float |
( |
float * | fptr, |
|
|
char * | s ) |
Parses a float value from the given string.
This function scans the input string for a valid single-precision floating-point number, stores the result in the provided pointer, and updates the string pointer to the position following the parsed number.
- Parameters
-
| [out] | fptr | Pointer to store the parsed float value. |
| [in,out] | s | Input string to scan. It will be modified to point after the parsed number. |
- Returns
- Returns 1 if a float was successfully parsed, 0 otherwise.
Definition at line 208 of file data_scan.c.
208 {
209 register char *ptr0;
210 register int was_point = 0;
211
212
213 ptr0 = s;
214 while (!float_start(s) && *s)
215 s++;
216 if (*s == 0)
217 return (0);
218
219
220 sscanf(s, "%f", fptr);
221
222
223
224
225 if (*s == '-' || *s == '+')
226 s++;
227
228
229 while (isdigit(*s) || (*s == '.' && !was_point))
230 if (*s++ == '.')
231 was_point = 1;
232
233 if (*s == 'e' || *s == 'E') {
234 s++;
235 if (*s == '+' || *s == '-')
236 s++;
237 while (isdigit(*s))
238 s++;
239 }
240
242 return (1);
243}
◆ get_int()
| int get_int |
( |
int * | iptr, |
|
|
char * | s ) |
Parses an integer value from the given string.
This function scans the input string for a valid integer, stores the result in the provided pointer, and updates the string pointer to the position following the parsed number.
- Parameters
-
| [out] | iptr | Pointer to store the parsed integer value. |
| [in,out] | s | Input string to scan. It will be modified to point after the parsed number. |
- Returns
- Returns 1 if an integer was successfully parsed, 0 otherwise.
Definition at line 380 of file data_scan.c.
380 {
381 char *ptr0;
382
383
384 ptr0 = s;
385 while (!int_start(s) && *s)
386 s++;
387 if (*s == 0)
388 return (0);
389
390
391 sscanf(s, "%d", iptr);
392
393
394 if (*s == '-' || *s == '+')
395 s++;
396 while (isdigit(*s))
397 s++;
398
400 return (1);
401}
◆ get_long()
| int get_long |
( |
long * | iptr, |
|
|
char * | s ) |
Parses a long integer value from the given string.
This function scans the input string for a valid long integer, stores the result in the provided pointer, and updates the string pointer to the position following the parsed number.
- Parameters
-
| [out] | iptr | Pointer to store the parsed long integer value. |
| [in,out] | s | Input string to scan. It will be modified to point after the parsed number. |
- Returns
- Returns 1 if a long integer was successfully parsed, 0 otherwise.
Definition at line 255 of file data_scan.c.
255 {
256 char *ptr0;
257
258
259 ptr0 = s;
260 while (!int_start(s) && *s)
261 s++;
262 if (*s == 0)
263 return (0);
264
265
266 sscanf(s, "%ld", iptr);
267
268
269 if (*s == '-' || *s == '+')
270 s++;
271 while (isdigit(*s))
272 s++;
273
275 return (1);
276}
◆ get_long1()
| int get_long1 |
( |
long * | lptr, |
|
|
char * | s ) |
Parses a long integer value from the given string without modifying the string.
This function scans the input string for a valid long integer, stores the result in the provided pointer, and ensures that the string remains unchanged after parsing.
- Parameters
-
| [out] | lptr | Pointer to store the parsed long integer value. |
| [in] | s | Input string to scan. It remains unchanged after parsing. |
- Returns
- Returns 1 if a long integer was successfully parsed, 0 otherwise.
Definition at line 288 of file data_scan.c.
288 {
289 char *endptr;
290 long val;
291
292 if (s == NULL || *s == '\0')
293 return 0;
294
295
296 char *p = s + strlen(s) - 1;
297 while (p >= s && isspace((unsigned char)*p))
298 p--;
299 *(p + 1) = '\0';
300
301
302 char *start = s;
303 while (*start) {
304 val = strtol(start, &endptr, 10);
305
306 if (endptr != start) {
307
308 if (*endptr == '\0') {
309 *lptr = val;
310 return 1;
311 }
312 }
313 start++;
314 }
315 return 0;
316}
◆ get_long1_old()
| int get_long1_old |
( |
long * | iptr, |
|
|
char * | s ) |
Definition at line 318 of file data_scan.c.
318 {
319
320
321 while (!int_start(s) && *s)
322 s++;
323 if (*s == 0)
324 return (0);
325
326
327 sscanf(s, "%ld", iptr);
328
329
330 if (*s == '-' || *s == '+')
331 s++;
332 while (isdigit(*s))
333 s++;
334 return (1);
335}
◆ get_longdouble()
| int get_longdouble |
( |
long double * | dptr, |
|
|
char * | s ) |
Parses a long double value from the given string.
This function scans the input string for a valid long double floating-point number, stores the result in the provided pointer, and updates the string pointer to the position following the parsed number.
- Parameters
-
| [out] | dptr | Pointer to store the parsed long double value. |
| [in,out] | s | Input string to scan. It will be modified to point after the parsed number. |
- Returns
- Returns 1 if a long double was successfully parsed, 0 otherwise.
Definition at line 88 of file data_scan.c.
88 {
89 register char *ptr0;
90 register int was_point = 0;
91
92
93 ptr0 = s;
94 while (!float_start(s) && *s)
95 s++;
96 if (*s == 0)
97 return (0);
98
99 sscanf(s, "%Lf", dptr);
100
101
102
103 if (*s == '-' || *s == '+')
104 s++;
105
106
107 while (isdigit(*s) || (*s == '.' && !was_point))
108 if (*s++ == '.')
109 was_point = 1;
110
111 if (*s == 'e' || *s == 'E') {
112 s++;
113 if (*s == '+' || *s == '-')
114 s++;
115 while (isdigit(*s))
116 s++;
117 }
119 return (1);
120}
◆ get_short()
| int get_short |
( |
short * | iptr, |
|
|
char * | s ) |
Parses a short integer value from the given string.
This function scans the input string for a valid short integer, stores the result in the provided pointer, and updates the string pointer to the position following the parsed number.
- Parameters
-
| [out] | iptr | Pointer to store the parsed short integer value. |
| [in,out] | s | Input string to scan. It will be modified to point after the parsed number. |
- Returns
- Returns 1 if a short integer was successfully parsed, 0 otherwise.
Definition at line 347 of file data_scan.c.
347 {
348 char *ptr0;
349
350
351 ptr0 = s;
352 while (!int_start(s) && *s)
353 s++;
354 if (*s == 0)
355 return (0);
356
357
358 sscanf(s, "%hd", iptr);
359
360
361 if (*s == '-' || *s == '+')
362 s++;
363 while (isdigit(*s))
364 s++;
365
367 return (1);
368}
◆ get_token()
| char * get_token |
( |
char * | s | ) |
|
Extracts the next token from the input string.
This function scans the input string for the next token, which can be a quoted string or a sequence of non-separator characters. It allocates memory for the token, copies it to the allocated space, and updates the input string pointer to the position following the token.
- Parameters
-
| [in,out] | s | Input string to scan. It will be modified to point after the extracted token. |
- Returns
- Returns a pointer to the newly allocated token string, or NULL if no token is found.
Definition at line 413 of file data_scan.c.
413 {
414 char *ptr0, *ptr1, *ptr;
415
416
417 ptr0 = s;
418 while (skip_it(*s))
419 s++;
420 if (*s == 0)
421 return (NULL);
422 ptr1 = s;
423
424 if (*s == '"' && (ptr0 == s || *(s - 1) != '\\')) {
425 ptr1 = s + 1;
426
427 do {
428 s++;
429 } while (*s && (*s != '"' || *(s - 1) == '\\'));
430 if (*s == '"' && *(s - 1) != '\\')
431 *s = ' ';
432 } else {
433
434 do {
435 s++;
436 if (*s == '"' && *(s - 1) != '\\') {
437 while (*++s && (*s != '"' || *(s - 1) == '\\'))
438 ;
439 }
440 } while (*s && nskip_it(*s));
441 }
442
443 ptr =
tmalloc((
unsigned)
sizeof(*ptr) * (s - ptr1 + 1));
444 strncpy(ptr, ptr1, s - ptr1);
445 ptr[s - ptr1] = 0;
446
448 return (ptr);
449}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
◆ get_token_buf()
| char * get_token_buf |
( |
char * | s, |
|
|
char * | buf, |
|
|
long | lbuf ) |
Extracts the next token from the input string into a provided buffer.
This function scans the input string for the next token, which can be a quoted string or a sequence of non-separator characters. It copies the token into the provided buffer if it fits, and updates the input string pointer to the position following the token.
- Parameters
-
| [in,out] | s | Input string to scan. It will be modified to point after the extracted token. |
| [out] | buf | Buffer to store the extracted token. |
| [in] | lbuf | Length of the buffer to prevent overflow. |
- Returns
- Returns a pointer to the buffer containing the token, or NULL if no token is found.
Definition at line 463 of file data_scan.c.
463 {
464 char *ptr0, *ptr1;
465
466
467 ptr0 = s;
468 while (skip_it(*s))
469 s++;
470 if (*s == 0)
471 return (NULL);
472 ptr1 = s;
473
474 if (*s == '"') {
475 ptr1 = s + 1;
476
477 do {
478 s++;
479 } while (*s != '"' && *s);
480 if (*s == '"')
481 *s = ' ';
482 } else {
483
484 do {
485 s++;
486 } while (*s && nskip_it(*s));
487 }
488
489 if ((s - ptr1 + 1) > lbuf) {
490 printf("buffer overflow in get_token_buf()\nstring was %s\n", ptr0);
491 exit(1);
492 }
493 strncpy(buf, ptr1, s - ptr1);
494 buf[s - ptr1] = 0;
495
497 return (buf);
498}
◆ tokenIsInteger()
| long tokenIsInteger |
( |
char * | token | ) |
|
Checks if the given token represents a valid integer.
This function verifies whether the input token string is a valid integer, which may include an optional leading '+' or '-' sign followed by digits.
- Parameters
-
| [in] | token | The token string to check. |
- Returns
- Returns a non-zero value if the token is a valid integer, 0 otherwise.
Definition at line 509 of file data_scan.c.
509 {
510 if (!isdigit(*token) && *token != '-' && *token != '+')
511 return 0;
512 if (!isdigit(*token) && !isdigit(*(token + 1)))
513 return 0;
514 token++;
515 while (*token)
516 if (!isdigit(*token++))
517 return 0;
518 return 1;
519}
◆ tokenIsNumber()
| long tokenIsNumber |
( |
char * | token | ) |
|
Checks if the given token represents a valid number.
This function verifies whether the input token string is a valid numerical value, which may include integers, floating-point numbers, and numbers in exponential notation.
- Parameters
-
| [in] | token | The token string to check. |
- Returns
- Returns a non-zero value if the token is a valid number, 0 otherwise.
Definition at line 530 of file data_scan.c.
530 {
531 long pointSeen, digitSeen;
532
533 if (!(digitSeen = isdigit(*token)) && *token != '-' && *token != '+' && *token != '.')
534 return 0;
535 pointSeen = *token == '.';
536 token++;
537
538 while (*token) {
539 if (isdigit(*token)) {
540 digitSeen = 1;
541 token++;
542 } else if (*token == '.') {
543 if (pointSeen)
544 return 0;
545 pointSeen = 1;
546 token++;
547 } else if (*token == 'e' || *token == 'E') {
548 if (!digitSeen)
549 return 0;
551 } else
552 return 0;
553 }
554 return digitSeen;
555}
long tokenIsInteger(char *token)
Checks if the given token represents a valid integer.