FreeCalypso > hg > fc-pcsc-tools
comparison libutil/shorthand.c @ 160:b86b3f8890ba
libutil: shorthand decimal string parsing implemented
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 26 Feb 2021 21:04:11 +0000 |
parents | libutil/decimal_str.c@2557012666ea |
children |
comparison
equal
deleted
inserted
replaced
159:2557012666ea | 160:b86b3f8890ba |
---|---|
1 /* | |
2 * This module implements the function for parsing shorthand decimal strings. | |
3 */ | |
4 | |
5 #include <sys/types.h> | |
6 #include <ctype.h> | |
7 #include <stdio.h> | |
8 #include <string.h> | |
9 #include <strings.h> | |
10 | |
11 parse_decimal_shorthand(arg, dest, maxdigits) | |
12 char *arg; | |
13 u_char *dest; | |
14 unsigned maxdigits; | |
15 { | |
16 unsigned n, ntail; | |
17 | |
18 if (!*arg) { | |
19 fprintf(stderr, | |
20 "error: empty argument given for decimal string\n"); | |
21 return(-1); | |
22 } | |
23 if (!isdigit(*arg)) { | |
24 fprintf(stderr, | |
25 "error: decimal string argument begins with a non-digit\n"); | |
26 return(-1); | |
27 } | |
28 for (n = 0; isdigit(*arg); ) { | |
29 if (n >= maxdigits) { | |
30 toolong: fprintf(stderr, | |
31 "error: decimal string exceeds limit of %u digits\n", | |
32 maxdigits); | |
33 return(-1); | |
34 } | |
35 dest[n++] = *arg++ - '0'; | |
36 } | |
37 if (!*arg) { | |
38 if (n != maxdigits) { | |
39 fprintf(stderr, | |
40 "error: %u digits required, %u digits given\n", | |
41 maxdigits, n); | |
42 return(-1); | |
43 } | |
44 return(0); | |
45 } | |
46 if (*arg++ != '-') { | |
47 malformed: fprintf(stderr, | |
48 "error: malformed shorthand decimal string argument\n"); | |
49 return(-1); | |
50 } | |
51 ntail = strlen(arg); | |
52 if (n + ntail >= maxdigits) | |
53 goto toolong; | |
54 while (n < maxdigits - ntail) | |
55 dest[n++] = 0; | |
56 if (!isdigit(*arg)) | |
57 goto malformed; | |
58 while (*arg) { | |
59 if (!isdigit(*arg)) | |
60 goto malformed; | |
61 dest[n++] = *arg++ - '0'; | |
62 } | |
63 return(0); | |
64 } |