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)) {
132 val = strtod(str, NULL);
133 if (errno != 0 || val < 0) {
136 *value = (utime_t)(val * mult[i]);
142 * Edit a utime "duration" into ASCII
144 char *edit_utime(utime_t val, char *buf)
147 static int mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
148 static char *mod[] = {"year", "month", "day", "hour", "min"};
153 for (i=0; i<5; i++) {
154 times = val / mult[i];
156 val = val - (utime_t)times * mult[i];
157 sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
161 if (val == 0 && strlen(buf) == 0) {
162 strcat(buf, "0 secs");
163 } else if (val != 0) {
164 sprintf(mybuf, "%d sec%s", (uint32_t)val, val>1?"s":"");
171 * Convert a size size in bytes to uint64_t
172 * Returns 0: if error
173 1: if OK, and value stored in value
175 int size_to_uint64(char *str, int str_len, uint64_t *rtn_value)
179 int mod[] = {'*', 'k', 'm', 'g', 0}; /* first item * not used */
180 uint64_t mult[] = {1, /* byte */
182 1048576, /* megabyte */
183 1073741824}; /* gigabyte */
185 #ifdef we_have_a_compiler_that_works
186 int mod[] = {'*', 'k', 'm', 'g', 't', 0};
187 uint64_t mult[] = {1, /* byte */
189 1048576, /* megabyte */
190 1073741824, /* gigabyte */
191 1099511627776};/* terabyte */
194 Dmsg1(400, "Enter sized to uint64 str=%s\n", str);
196 /* Look for modifier */
197 ch = str[str_len - 1];
203 while (mod[++i] != 0) {
206 str[str_len] = 0; /* strip modifier */
211 if (mod[i] == 0 || !is_a_number(str)) {
214 Dmsg3(400, "size str=:%s: %lf i=%d\n", str, strtod(str, NULL), i);
217 value = strtod(str, NULL);
218 if (errno != 0 || value < 0) {
221 *rtn_value = (uint64_t)(value * mult[i]);
222 Dmsg2(400, "Full value = %lf %" lld "\n", value * mult[i],
223 (uint64_t)(value * mult[i]));
228 * Check if specified string is a number or not.
229 * Taken from SQLite, cool, thanks.
231 int is_a_number(const char *n)
233 bool digit_seen = false;
235 if( *n == '-' || *n == '+' ) {
238 while (B_ISDIGIT(*n)) {
242 if (digit_seen && *n == '.') {
244 while (B_ISDIGIT(*n)) { n++; }
246 if (digit_seen && (*n == 'e' || *n == 'E')
247 && (B_ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && B_ISDIGIT(n[2])))) {
248 n += 2; /* skip e- or e+ or e digit */
249 while (B_ISDIGIT(*n)) { n++; }
251 return digit_seen && *n==0;
255 * Check if the specified string is an integer
257 int is_an_integer(const char *n)
259 bool digit_seen = false;
260 while (B_ISDIGIT(*n)) {
264 return digit_seen && *n==0;
268 * Add commas to a string, which is presumably
271 char *add_commas(char *val, char *buf)
289 for (i=0; i < 3; i++) {