]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/edit.c
Final changes
[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, len;
106    double val;
107    /*
108     * The "n" = mins and months appears before minutes so that m maps
109     *   to months. These "kludges" make it compatible with pre 1.31 
110     *   Baculas.
111     */
112    static char *mod[] = {"n", "seconds", "months", "minutes", 
113                  "hours", "days", "weeks",   "quarters",   "years", NULL};
114    static int32_t mult[] = {60,   1, 60*60*24*30, 60, 
115                   60*60, 60*60*24, 60*60*24*7, 60*60*24*91, 60*60*24*365};
116    char mod_str[20];
117    int mod_len;
118
119    /*
120     * Look for modifier by walking back looking for the first
121     *   space or digit.
122     */
123    strip_trailing_junk(str);
124    len = strlen(str);
125    /* Strip trailing spaces */
126    for (i=len; i>0; i--) {
127       if (!B_ISSPACE(str[i-1])) {
128          break;
129       }
130       str[i-1] = 0;
131    }
132    /* Find beginning of the modifier */
133    for ( ; i>0; i--) {
134       if (!B_ISALPHA(str[i-1])) {
135          break;
136       }
137    }
138    /* If not found, error */
139    if (i == 0 || i == len) {
140       Dmsg2(200, "error i=%d len=%d\n", i, len);
141       return 0;
142    }
143    /* Move modifier to mod_str */
144    bstrncpy(mod_str, &str[i], sizeof(mod_str));
145    mod_len = strlen(mod_str);
146    if (mod_len == 0) {                /* Make sure we have a modifier */
147       Dmsg0(200, "No modifier found\n");
148       return 0;
149    }
150    Dmsg2(200, "in=%s  mod=%s:\n", str, mod_str);
151    /* Backup over any spaces in front of modifier */
152    for ( ; i>0; i--) {
153       if (B_ISSPACE(str[i-1])) {
154          continue;
155       }
156       str[i] = 0;
157       break;
158    }
159    /* The remainder (beginning) should be our number */
160    if (!is_a_number(str)) {
161       Dmsg0(200, "input not a number\n");
162       return 0;
163    }
164    /* Now find the multiplier corresponding to the modifier */
165    for (i=0; mod[i]; i++) {
166       if (strncasecmp(mod_str, mod[i], mod_len) == 0) {
167          break;
168       }
169    }
170    if (mod[i] == NULL) {
171       Dmsg0(200, "Modifier not found\n");
172       return 0;                       /* modifer not found */
173    }
174    Dmsg2(200, "str=%s: mult=%d\n", str, mult[i]);
175    errno = 0;
176    val = strtod(str, NULL);
177    if (errno != 0 || val < 0) {
178       return 0;
179    }
180   *value = (utime_t)(val * mult[i]);
181    return 1;
182
183 }
184
185 /*
186  * Edit a utime "duration" into ASCII
187  */
188 char *edit_utime(utime_t val, char *buf)
189 {
190    char mybuf[30];
191    static int mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
192    static char *mod[]  = {"year",  "month",  "day", "hour", "min"};
193    int i;
194    uint32_t times;
195
196    *buf = 0;
197    for (i=0; i<5; i++) {
198       times = val / mult[i];
199       if (times > 0) {
200          val = val - (utime_t)times * mult[i];
201          sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
202          strcat(buf, mybuf);
203       }
204    }
205    if (val == 0 && strlen(buf) == 0) {     
206       strcat(buf, "0 secs");
207    } else if (val != 0) {
208       sprintf(mybuf, "%d sec%s", (uint32_t)val, val>1?"s":"");
209       strcat(buf, mybuf);
210    }
211    return buf;
212 }
213
214 /*
215  * Convert a size size in bytes to uint64_t
216  * Returns 0: if error
217            1: if OK, and value stored in value
218  */
219 int size_to_uint64(char *str, int str_len, uint64_t *rtn_value)
220 {
221    int i, ch;
222    double value;
223    int mod[]  = {'*', 'k', 'm', 'g', 0}; /* first item * not used */
224    uint64_t mult[] = {1,             /* byte */
225                       1024,          /* kilobyte */
226                       1048576,       /* megabyte */
227                       1073741824};   /* gigabyte */
228
229 #ifdef we_have_a_compiler_that_works
230    int mod[]  = {'*', 'k', 'm', 'g', 't', 0};
231    uint64_t mult[] = {1,             /* byte */
232                       1024,          /* kilobyte */
233                       1048576,       /* megabyte */
234                       1073741824,    /* gigabyte */
235                       1099511627776};/* terabyte */
236 #endif
237
238    Dmsg1(400, "Enter sized to uint64 str=%s\n", str);
239
240    /* Look for modifier */
241    ch = str[str_len - 1];
242    i = 0;
243    if (B_ISALPHA(ch)) {
244       if (B_ISUPPER(ch)) {
245          ch = tolower(ch);
246       }
247       while (mod[++i] != 0) {
248          if (ch == mod[i]) {
249             str_len--;
250             str[str_len] = 0; /* strip modifier */
251             break;
252          }
253       }
254    }
255    if (mod[i] == 0 || !is_a_number(str)) {
256       return 0;
257    }
258    Dmsg3(400, "size str=:%s: %lf i=%d\n", str, strtod(str, NULL), i);
259
260    errno = 0;
261    value = strtod(str, NULL);
262    if (errno != 0 || value < 0) {
263       return 0;
264    }
265    *rtn_value = (uint64_t)(value * mult[i]);
266    Dmsg2(400, "Full value = %lf %" lld "\n", value * mult[i],  
267       (uint64_t)(value * mult[i]));
268    return 1;
269 }
270
271 /*
272  * Check if specified string is a number or not.
273  *  Taken from SQLite, cool, thanks.
274  */
275 int is_a_number(const char *n)
276 {
277    bool digit_seen = false;
278
279    if( *n == '-' || *n == '+' ) {
280       n++;
281    }
282    while (B_ISDIGIT(*n)) {
283       digit_seen = true;
284       n++;
285    }
286    if (digit_seen && *n == '.') {
287       n++;
288       while (B_ISDIGIT(*n)) { n++; }
289    }
290    if (digit_seen && (*n == 'e' || *n == 'E')
291        && (B_ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && B_ISDIGIT(n[2])))) {
292       n += 2;                         /* skip e- or e+ or e digit */
293       while (B_ISDIGIT(*n)) { n++; }
294    }
295    return digit_seen && *n==0;
296 }
297
298 /*
299  * Check if the specified string is an integer   
300  */
301 int is_an_integer(const char *n)
302 {
303    bool digit_seen = false;
304    while (B_ISDIGIT(*n)) {
305       digit_seen = true;
306       n++;
307    }
308    return digit_seen && *n==0;
309 }
310
311 /*
312  * Add commas to a string, which is presumably
313  * a number.  
314  */
315 char *add_commas(char *val, char *buf)
316 {
317    int len, nc;
318    char *p, *q;
319    int i;
320
321    if (val != buf) {
322       strcpy(buf, val);
323    }
324    len = strlen(buf);
325    if (len < 1) {
326       len = 1;
327    }
328    nc = (len - 1) / 3;
329    p = buf+len;
330    q = p + nc;
331    *q-- = *p--;
332    for ( ; nc; nc--) {
333       for (i=0; i < 3; i++) {
334           *q-- = *p--;
335       }
336       *q-- = ',';
337    }   
338    return buf;
339 }
340
341 #ifdef TEST_PROGRAM
342 void d_msg(char*, int, int, char*, ...)
343 {}
344 int main(int argc, char *argv[])
345 {
346    char *str[] = {"3", "3n", "3 hours", "3.5 day", "3 week", "3 m", "3 q", "3 years"};
347    utime_t val;
348    char buf[100];
349    char outval[100];
350
351    for (int i=0; i<8; i++) {
352       strcpy(buf, str[i]);
353       if (!duration_to_utime(buf, &val)) {
354          printf("Error return from duration_to_utime for in=%s\n", str[i]);
355          continue;
356       }
357       edit_utime(val, outval);
358       printf("in=%s val=%lld outval=%s\n", str[i], val, outval);
359    }
360 }
361 #endif