annotate libutil/shorthand.c @ 229:ed8cb3c0d312 default tip

new README, indicating repository move
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 18:26:13 +0000
parents b86b3f8890ba
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
66
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
160
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
2 * This module implements the function for parsing shorthand decimal strings.
66
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <ctype.h>
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <stdio.h>
160
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
8 #include <string.h>
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
9 #include <strings.h>
66
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10
160
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
11 parse_decimal_shorthand(arg, dest, maxdigits)
66
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 char *arg;
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 u_char *dest;
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 unsigned maxdigits;
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 {
160
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
16 unsigned n, ntail;
66
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 if (!*arg) {
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 fprintf(stderr,
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 "error: empty argument given for decimal string\n");
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 return(-1);
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 }
160
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
23 if (!isdigit(*arg)) {
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
24 fprintf(stderr,
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
25 "error: decimal string argument begins with a non-digit\n");
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
26 return(-1);
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
27 }
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
28 for (n = 0; isdigit(*arg); ) {
66
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (n >= maxdigits) {
160
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
30 toolong: fprintf(stderr,
66
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 "error: decimal string exceeds limit of %u digits\n",
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 maxdigits);
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 return(-1);
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 }
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 dest[n++] = *arg++ - '0';
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 }
160
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
37 if (!*arg) {
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
38 if (n != maxdigits) {
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
39 fprintf(stderr,
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
40 "error: %u digits required, %u digits given\n",
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
41 maxdigits, n);
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
42 return(-1);
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
43 }
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
44 return(0);
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
45 }
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
46 if (*arg++ != '-') {
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
47 malformed: fprintf(stderr,
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
48 "error: malformed shorthand decimal string argument\n");
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
49 return(-1);
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
50 }
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
51 ntail = strlen(arg);
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
52 if (n + ntail >= maxdigits)
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
53 goto toolong;
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
54 while (n < maxdigits - ntail)
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
55 dest[n++] = 0;
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
56 if (!isdigit(*arg))
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
57 goto malformed;
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
58 while (*arg) {
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
59 if (!isdigit(*arg))
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
60 goto malformed;
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
61 dest[n++] = *arg++ - '0';
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
62 }
b86b3f8890ba libutil: shorthand decimal string parsing implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 159
diff changeset
63 return(0);
66
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 }