summaryrefslogtreecommitdiff
path: root/src/pv/number.c
blob: 0495b131a4b42d709c4fdc7cfbea2573454ef415 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 * Functions for converting strings to numbers.
 *
 * Copyright 2012 Andrew Wood, distributed under the Artistic License 2.0.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif


/*
 * This function is used instead of the macro from <ctype.h> because
 * including <ctype.h> causes weird versioned glibc dependencies on certain
 * Red Hat systems, complicating package management.
 */
static int pv__isdigit(char c)
{
	return ((c >= '0') && (c <= '9'));
}


/*
 * Return the numeric value of "str", as a long long.
 */
long long pv_getnum_ll(char *str)
{
	long long n = 0;
	long long decimal = 0;
	int decdivisor = 1;
	int shift = 0;

	while (str[0] != 0 && (!pv__isdigit(str[0])))
		str++;

	for (; pv__isdigit(str[0]); str++) {
		n = n * 10;
		n += (str[0] - '0');
	}

	/*
	 * If a decimal value was given, skip the decimal part.
	 */
	if ((str[0] == '.') || (str[0] == ',')) {
		str++;
		for (; pv__isdigit(str[0]); str++) {
			if (decdivisor < 10000) {
				decimal = decimal * 10;
				decimal += (str[0] - '0');
				decdivisor = decdivisor * 10;
			}
		}
	}

	/*
	 * Parse any units given (K=KiB=*1024, M=MiB=1024KiB, G=GiB=1024MiB,
	 * T=TiB=1024GiB).
	 */
	if (str[0]) {
		while ((str[0] == ' ') || (str[0] == '\t'))
			str++;
		switch (str[0]) {
		case 'k':
		case 'K':
			shift = 10;
			break;
		case 'm':
		case 'M':
			shift = 20;
			break;
		case 'g':
		case 'G':
			shift = 30;
			break;
		case 't':
		case 'T':
			shift = 40;
			break;
		default:
			break;
		}
	}

	/*
	 * Binary left-shift the supplied number by "shift" times, i.e.
	 * apply the given units (KiB, MiB, etc) to it, but never shift left
	 * more than 30 at a time to avoid overflows.
	 */
	while (shift > 0) {
		int shiftby;

		shiftby = shift;
		if (shiftby > 30)
			shiftby = 30;

		n = n << shiftby;
		decimal = decimal << shiftby;
		shift -= shiftby;
	}

	/*
	 * Add any decimal component.
	 */
	decimal = decimal / decdivisor;
	n += decimal;

	return n;
}


/*
 * Return the numeric value of "str", as a double.
 */
double pv_getnum_d(char *str)
{
	double n = 0.0;
	double step = 1;

	while (str[0] != 0 && (!pv__isdigit(str[0])))
		str++;

	for (; pv__isdigit(str[0]); str++) {
		n = n * 10;
		n += (str[0] - '0');
	}

	if ((str[0] != '.') && (str[0] != ','))
		return n;

	str++;

	for (; pv__isdigit(str[0]) && step < 1000000; str++) {
		step = step * 10;
		n += (str[0] - '0') / step;
	}

	return n;
}


/*
 * Return the numeric value of "str", as an int.
 */
int pv_getnum_i(char *str)
{
	return (int) pv_getnum_ll(str);
}


/*
 * Return nonzero if the given string is not a valid integer (type=0) or
 * double (type=1).
 */
int pv_getnum_check(char *str, int type)
{
	if (!str)
		return 1;

	while ((str[0] == ' ') || (str[0] == '\t'))
		str++;

	if (!pv__isdigit(str[0]))
		return 1;

	for (; pv__isdigit(str[0]); str++);

	if (str[0] == '.') {
		if (type == 0)
			return 1;
		str++;
		for (; pv__isdigit(str[0]); str++);
	}

	if (str[0] == 0)
		return 0;

	/*
	 * Suffixes are not allowed for doubles, only for integers.
	 */
	if (type == 1)
		return 1;

	while ((str[0] == ' ') || (str[0] == '\t'))
		str++;
	switch (str[0]) {
	case 'k':
	case 'K':
	case 'm':
	case 'M':
	case 'g':
	case 'G':
	case 't':
	case 'T':
		str++;
		break;
	default:
		return 1;
	}

	if (str[0])
		return 1;

	return 0;
}

/* EOF */