]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/edit.c
Fix counter bugs reported by Chris Allen
[bacula/bacula] / bacula / src / lib / edit.c
1 /*
2  *   edit.c  edit string to ascii, and ascii to internal 
3  * 
4  *    Kern Sibbald, December MMII
5  *
6  *   Version $Id$
7  */
8
9 /*
10    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11
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.
16
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.
21
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,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include <math.h>
31
32 /* We assume ASCII input and don't worry about overflow */
33 uint64_t str_to_uint64(char *str) 
34 {
35    register char *p = str;
36    register uint64_t value = 0;
37
38    while (B_ISSPACE(*p)) {
39       p++;
40    }
41    if (*p == '+') {
42       p++;
43    }
44    while (B_ISDIGIT(*p)) {
45       value = value * 10 + *p - '0';
46       p++;
47    }
48    return value;
49 }
50
51 int64_t str_to_int64(char *str) 
52 {
53    register char *p = str;
54    register int64_t value;
55    int negative = FALSE;
56
57    while (B_ISSPACE(*p)) {
58       p++;
59    }
60    if (*p == '+') {
61       p++;
62    } else if (*p == '-') {
63       negative = TRUE;
64       p++;
65    }
66    value = str_to_uint64(p);
67    if (negative) {
68       value = -value;
69    }
70    return value;
71 }
72
73
74
75 /*
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.
79  */
80 char *edit_uint64_with_commas(uint64_t val, char *buf)
81 {
82    sprintf(buf, "%" lld, val);
83    return add_commas(buf, buf);
84 }
85
86 /*
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.
90  */
91 char *edit_uint64(uint64_t val, char *buf)
92 {
93    sprintf(buf, "%" lld, val);
94    return buf;
95 }
96
97
98 /*
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
102  */
103 int duration_to_utime(char *str, utime_t *value)
104 {
105    int i, ch, len;
106    double val;
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};
111
112    /* Look for modifier */
113    len = strlen(str);
114    ch = str[len - 1];
115    i = 0;
116    if (B_ISALPHA(ch)) {
117       if (B_ISUPPER(ch)) {
118          ch = tolower(ch);
119       }
120       while (mod[++i] != 0) {
121          if (ch == mod[i]) {
122             len--;
123             str[len] = 0; /* strip modifier */
124             break;
125          }
126       }
127    }
128    if (mod[i] == 0 || !is_a_number(str)) {
129       return 0;
130    }
131    errno = 0;
132    val = strtod(str, NULL);
133    if (errno != 0 || val < 0) {
134       return 0;
135    }
136    *value = (utime_t)(val * mult[i]);
137    return 1;
138
139 }
140
141 /*
142  * Edit a utime "duration" into ASCII
143  */
144 char *edit_utime(utime_t val, char *buf)
145 {
146    char mybuf[30];
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"};
149    int i;
150    uint32_t times;
151
152    *buf = 0;
153    for (i=0; i<5; i++) {
154       times = val / mult[i];
155       if (times > 0) {
156          val = val - (utime_t)times * mult[i];
157          sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
158          strcat(buf, mybuf);
159       }
160    }
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":"");
165       strcat(buf, mybuf);
166    }
167    return buf;
168 }
169
170 /*
171  * Convert a size size in bytes to uint64_t
172  * Returns 0: if error
173            1: if OK, and value stored in value
174  */
175 int size_to_uint64(char *str, int str_len, uint64_t *rtn_value)
176 {
177    int i, ch;
178    double value;
179    int mod[]  = {'*', 'k', 'm', 'g', 0}; /* first item * not used */
180    uint64_t mult[] = {1,             /* byte */
181                       1024,          /* kilobyte */
182                       1048576,       /* megabyte */
183                       1073741824};   /* gigabyte */
184
185 #ifdef we_have_a_compiler_that_works
186    int mod[]  = {'*', 'k', 'm', 'g', 't', 0};
187    uint64_t mult[] = {1,             /* byte */
188                       1024,          /* kilobyte */
189                       1048576,       /* megabyte */
190                       1073741824,    /* gigabyte */
191                       1099511627776};/* terabyte */
192 #endif
193
194    Dmsg1(400, "Enter sized to uint64 str=%s\n", str);
195
196    /* Look for modifier */
197    ch = str[str_len - 1];
198    i = 0;
199    if (B_ISALPHA(ch)) {
200       if (B_ISUPPER(ch)) {
201          ch = tolower(ch);
202       }
203       while (mod[++i] != 0) {
204          if (ch == mod[i]) {
205             str_len--;
206             str[str_len] = 0; /* strip modifier */
207             break;
208          }
209       }
210    }
211    if (mod[i] == 0 || !is_a_number(str)) {
212       return 0;
213    }
214    Dmsg3(400, "size str=:%s: %lf i=%d\n", str, strtod(str, NULL), i);
215
216    errno = 0;
217    value = strtod(str, NULL);
218    if (errno != 0 || value < 0) {
219       return 0;
220    }
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]));
224    return 1;
225 }
226
227 /*
228  * Check if specified string is a number or not.
229  *  Taken from SQLite, cool, thanks.
230  */
231 int is_a_number(const char *n)
232 {
233    bool digit_seen = false;
234
235    if( *n == '-' || *n == '+' ) {
236       n++;
237    }
238    while (B_ISDIGIT(*n)) {
239       digit_seen = true;
240       n++;
241    }
242    if (digit_seen && *n == '.') {
243       n++;
244       while (B_ISDIGIT(*n)) { n++; }
245    }
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++; }
250    }
251    return digit_seen && *n==0;
252 }
253
254 /*
255  * Check if the specified string is an integer   
256  */
257 int is_an_integer(const char *n)
258 {
259    bool digit_seen = false;
260    while (B_ISDIGIT(*n)) {
261       digit_seen = true;
262       n++;
263    }
264    return digit_seen && *n==0;
265 }
266
267 /*
268  * Add commas to a string, which is presumably
269  * a number.  
270  */
271 char *add_commas(char *val, char *buf)
272 {
273    int len, nc;
274    char *p, *q;
275    int i;
276
277    if (val != buf) {
278       strcpy(buf, val);
279    }
280    len = strlen(buf);
281    if (len < 1) {
282       len = 1;
283    }
284    nc = (len - 1) / 3;
285    p = buf+len;
286    q = p + nc;
287    *q-- = *p--;
288    for ( ; nc; nc--) {
289       for (i=0; i < 3; i++) {
290           *q-- = *p--;
291       }
292       *q-- = ',';
293    }   
294    return buf;
295 }