2 * edit.c edit string to ascii, and ascii to internal
4 * Kern Sibbald, December MMII
10 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
32 /* We assume ASCII input and don't worry about overflow */
33 uint64_t str_to_uint64(char *str)
35 register char *p = str;
36 register uint64_t value = 0;
38 while (B_ISSPACE(*p)) {
44 while (B_ISDIGIT(*p)) {
45 value = value * 10 + *p - '0';
51 int64_t str_to_int64(char *str)
53 register char *p = str;
54 register int64_t value;
57 while (B_ISSPACE(*p)) {
62 } else if (*p == '-') {
66 value = str_to_uint64(p);
76 * Edit an integer number with commas, the supplied buffer
77 * must be at least 27 bytes long. The incoming number
78 * is always widened to 64 bits.
80 char *edit_uint64_with_commas(uint64_t val, char *buf)
82 sprintf(buf, "%" lld, val);
83 return add_commas(buf, buf);
87 * Edit an integer number, the supplied buffer
88 * must be at least 27 bytes long. The incoming number
89 * is always widened to 64 bits.
91 char *edit_uint64(uint64_t val, char *buf)
93 sprintf(buf, "%" lld, val);
99 * Convert a string duration to utime_t (64 bit seconds)
100 * Returns 0: if error
101 1: if OK, and value stored in value
103 int duration_to_utime(char *str, utime_t *value)
107 /* Default to 1 day if no modifier given */
108 static int mod[] = {'*', 's', 'n', 'h', 'd', 'w', 'm', 'q', 'y', 0};
109 static int mult[] = {60*60*24, 1, 60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30,
110 60*60*24*91, 60*60*24*365};
112 /* Look for modifier */
120 while (mod[++i] != 0) {
123 str[len] = 0; /* strip modifier */
128 if (mod[i] == 0 || !is_a_number(str)) {
131 val = strtod(str, NULL);
132 if (errno != 0 || val < 0) {
135 *value = (utime_t)(val * mult[i]);
141 * Edit a utime "duration" into ASCII
143 char *edit_utime(utime_t val, char *buf)
146 static int mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
147 static char *mod[] = {"year", "month", "day", "hour", "min"};
152 for (i=0; i<5; i++) {
153 times = val / mult[i];
155 val = val - (utime_t)times * mult[i];
156 sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
160 if (val == 0 && strlen(buf) == 0) {
161 strcat(buf, "0 secs");
162 } else if (val != 0) {
163 sprintf(mybuf, "%d sec%s", (uint32_t)val, val>1?"s":"");
170 * Convert a size size in bytes to uint64_t
171 * Returns 0: if error
172 1: if OK, and value stored in value
174 int size_to_uint64(char *str, int str_len, uint64_t *rtn_value)
178 int mod[] = {'*', 'k', 'm', 'g', 0}; /* first item * not used */
179 uint64_t mult[] = {1, /* byte */
181 1048576, /* megabyte */
182 1073741824}; /* gigabyte */
184 #ifdef we_have_a_compiler_that_works
185 int mod[] = {'*', 'k', 'm', 'g', 't', 0};
186 uint64_t mult[] = {1, /* byte */
188 1048576, /* megabyte */
189 1073741824, /* gigabyte */
190 1099511627776};/* terabyte */
193 Dmsg1(400, "Enter sized to uint64 str=%s\n", str);
195 /* Look for modifier */
196 ch = str[str_len - 1];
202 while (mod[++i] != 0) {
205 str[str_len] = 0; /* strip modifier */
210 if (mod[i] == 0 || !is_a_number(str)) {
213 Dmsg3(400, "size str=:%s: %lf i=%d\n", str, strtod(str, NULL), i);
215 value = strtod(str, NULL);
216 if (errno != 0 || value < 0) {
219 *rtn_value = (uint64_t)(value * mult[i]);
220 Dmsg2(400, "Full value = %lf %" lld "\n", value * mult[i],
221 (uint64_t)(value * mult[i]));
226 * Check if specified string is a number or not.
227 * Taken from SQLite, cool, thanks.
229 int is_a_number(const char *n)
231 bool digit_seen = false;
233 if( *n == '-' || *n == '+' ) {
236 while (B_ISDIGIT(*n)) {
240 if (digit_seen && *n == '.') {
242 while (B_ISDIGIT(*n)) { n++; }
244 if (digit_seen && (*n == 'e' || *n == 'E')
245 && (B_ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && B_ISDIGIT(n[2])))) {
246 n += 2; /* skip e- or e+ or e digit */
247 while (B_ISDIGIT(*n)) { n++; }
249 return digit_seen && *n==0;
253 * Check if the specified string is an integer
255 int is_an_integer(const char *n)
257 bool digit_seen = false;
258 while (B_ISDIGIT(*n)) {
262 return digit_seen && *n==0;
266 * Add commas to a string, which is presumably
269 char *add_commas(char *val, char *buf)
287 for (i=0; i < 3; i++) {