Debian Codegraph @

local/strutils.c

raw
1 /*
2 * strutils.c - various string routines shared by commands
3 * This file was copied from util-linux at fall 2011.
4 *
5 * Copyright (C) 2010 Karel Zak <[email protected]>
6 * Copyright (C) 2010 Davidlohr Bueso <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22
23 #include <stdlib.h>
24 #include <ctype.h>
25
26 #include "c.h"
27 #include "strutils.h"
28
29 /*
30 * same as strtol(3) but exit on failure instead of returning crap
31 */
32 long strtol_or_err(const char *str, const char *errmesg)
33 {
34 long num;
35 char *end = NULL;
36
37 if (str != NULL && *str != '\0') {
38 errno = 0;
39 num = strtol(str, &end, 10);
40 if (errno == 0 && str != end && end != NULL && *end == '\0')
41 return num;
42 }
43 error(EXIT_FAILURE, errno, "%s: '%s'", errmesg, str);
44 return 0;
45 }
46
47 /*
48 * same as strtod(3) but exit on failure instead of returning crap
49 */
50 double strtod_or_err(const char *str, const char *errmesg)
51 {
52 double num;
53 char *end = NULL;
54
55 if (str != NULL && *str != '\0') {
56 errno = 0;
57 num = strtod(str, &end);
58 if (errno == 0 && str != end && end != NULL && *end == '\0')
59 return num;
60 }
61 error(EXIT_FAILURE, errno, "%s: '%s'", errmesg, str);
62 return 0;
63 }
64
65 /*
66 * Covert a string into a double in a non-locale aware way.
67 * This means the decimal point can be either . or ,
68 * Also means you cannot use the other for thousands separator
69 *
70 * Exits on failure like its other _or_err cousins
71 */
72 double strtod_nol_or_err(char *str, const char *errmesg)
73 {
74 double num;
75 const char *cp, *radix;
76 double mult;
77 int negative = 0;
78
79 if (str != NULL && *str != '\0') {
80 num = 0.0;
81 cp = str;
82 /* strip leading spaces */
83 while (isspace(*cp))
84 cp++;
85
86 /* get sign */
87 if (*cp == '-') {
88 negative = 1;
89 cp++;
90 } else if (*cp == '+')
91 cp++;
92
93 /* find radix */
94 radix = cp;
95 mult=0.1;
96 while(isdigit(*radix)) {
97 radix++;
98 mult *= 10;
99 }
100 while(isdigit(*cp)) {
101 num += (*cp - '0') * mult;
102 mult /= 10;
103 cp++;
104 }
105 /* got the integers */
106 if (*cp == '\0')
107 return (negative?-num:num);
108 if (*cp != '.' && *cp != ',')
109 error(EXIT_FAILURE, EINVAL, "%s: '%s'", errmesg, str);
110
111 cp++;
112 mult = 0.1;
113 while(isdigit(*cp)) {
114 num += (*cp - '0') * mult;
115 mult /= 10;
116 cp++;
117 }
118 if (*cp == '\0')
119 return (negative?-num:num);
120 }
121 error(EXIT_FAILURE, errno, "%s: '%s'", errmesg, str);
122 return 0;
123 }