annotate libutil/decimal_str.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 2557012666ea
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 /*
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module implements some functions for initial parsing of decimal
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * string arguments, intended for implementation of commands like
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * write-iccid and write-imsi.
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/types.h>
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <ctype.h>
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 parse_decimal_string_arg(arg, dest, maxdigits)
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 {
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 unsigned n, ndig;
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 }
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 for (n = 0; *arg; ) {
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 if (!isdigit(*arg)) {
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 fprintf(stderr,
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 "error: non-digit char in decimal string argument\n");
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 return(-1);
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 }
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (n >= maxdigits) {
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 fprintf(stderr,
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 }
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 ndig = n;
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 while (n < maxdigits)
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 dest[n++] = 0xF;
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 return ndig;
3ef90bd13fbe fc-simtool write-imsi command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }