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