]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/edit.c
Misc
[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-2004 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    if (!p) {
39       return 0;
40    }
41    while (B_ISSPACE(*p)) {
42       p++;
43    }
44    if (*p == '+') {
45       p++;
46    }
47    while (B_ISDIGIT(*p)) {
48       value = value * 10 + *p - '0';
49       p++;
50    }
51    return value;
52 }
53
54 int64_t str_to_int64(char *str) 
55 {
56    register char *p = str;
57    register int64_t value;
58    bool negative = false;
59
60    if (!p) {
61       return 0;
62    }
63    while (B_ISSPACE(*p)) {
64       p++;
65    }
66    if (*p == '+') {
67       p++;
68    } else if (*p == '-') {
69       negative = true;
70       p++;
71    }
72    value = str_to_uint64(p);
73    if (negative) {
74       value = -value;
75    }
76    return value;
77 }
78
79
80 /*
81  * Edit an integer number with commas, the supplied buffer
82  * must be at least 27 bytes long.  The incoming number
83  * is always widened to 64 bits.
84  */
85 char *edit_uint64_with_commas(uint64_t val, char *buf)
86 {
87    /*  
88     * Replacement for sprintf(buf, "%" llu, val)
89     */
90    char mbuf[50];
91    mbuf[sizeof(mbuf)-1] = 0;
92    int i = sizeof(mbuf)-2;                 /* edit backward */
93    if (val == 0) {
94       mbuf[i--] = '0';
95    } else {
96       while (val != 0) {
97          mbuf[i--] = "0123456789"[val%10];
98          val /= 10;
99       }
100    }
101    strcpy(buf, &mbuf[i+1]);
102    return add_commas(buf, buf);
103 }
104
105 /*
106  * Edit an integer number, the supplied buffer
107  * must be at least 27 bytes long.  The incoming number
108  * is always widened to 64 bits.
109  */
110 char *edit_uint64(uint64_t val, char *buf)
111 {
112    /*  
113     * Replacement for sprintf(buf, "%" llu, val)
114     */
115    char mbuf[50];
116    mbuf[sizeof(mbuf)-1] = 0;
117    int i = sizeof(mbuf)-2;                 /* edit backward */
118    if (val == 0) {
119       mbuf[i--] = '0';
120    } else {
121       while (val != 0) {
122          mbuf[i--] = "0123456789"[val%10];
123          val /= 10;
124       }
125    }
126    strcpy(buf, &mbuf[i+1]);
127    return buf;
128 }
129
130 /*
131  * Given a string "str", separate the integer part into
132  *   str, and the modifier into mod.
133  */
134 static bool get_modifier(char *str, char *mod, int mod_len)
135 {
136    int i, len;
137    /*
138     * Look for modifier by walking back looking for the first
139     *   space or digit.
140     */
141    strip_trailing_junk(str);
142    len = strlen(str);
143
144    /* Find beginning of the modifier */
145    for (i=len; i > 0; i--) {
146       if (!B_ISALPHA(str[i-1])) {
147          break;
148       }
149    }
150
151    /* If nothing found, error */
152    if (i == 0) {
153       Dmsg2(900, "error i=%d len=%d\n", i, len);
154       return false;
155    }
156
157    /* Move modifier to its location */
158    bstrncpy(mod, &str[i], mod_len);
159    Dmsg2(900, "in=%s  mod=%s:\n", str, mod);
160
161    /* Backup over any spaces in front of modifier */
162    for ( ; i > 0; i--) {
163       if (B_ISSPACE(str[i-1])) {
164          continue;
165       }
166       str[i] = 0;
167       break;
168    }
169    /* The remainder (beginning) should be our number */
170    if (!is_a_number(str)) {
171       Dmsg0(900, "input not a number\n");
172       return false;
173    }
174    return true;
175 }
176
177 /*
178  * Convert a string duration to utime_t (64 bit seconds)
179  * Returns 0: if error
180            1: if OK, and value stored in value
181  */
182 int duration_to_utime(char *str, utime_t *value)
183 {
184    int i, mod_len;
185    double val;
186    char mod_str[20];
187    /*
188     * The "n" = mins and months appears before minutes so that m maps
189     *   to months. These "kludges" make it compatible with pre 1.31 
190     *   Baculas.
191     */
192    static const char *mod[] = {"n", "seconds", "months", "minutes", 
193                   "hours", "days", "weeks",   "quarters",   "years", NULL};
194    static const int32_t mult[] = {60,   1, 60*60*24*30, 60, 
195                   60*60, 60*60*24, 60*60*24*7, 60*60*24*91, 60*60*24*365};
196
197    if (!get_modifier(str, mod_str, sizeof(mod_str))) {
198       return 0;
199    }
200    /* Now find the multiplier corresponding to the modifier */
201    mod_len = strlen(mod_str);
202    for (i=0; mod[i]; i++) {
203       if (strncasecmp(mod_str, mod[i], mod_len) == 0) {
204          break;
205       }
206    }
207    if (mod[i] == NULL) {
208       i = 1;                          /* no modifier, assume 1 */
209    }
210    Dmsg2(900, "str=%s: mult=%d\n", str, mult[i]);
211    errno = 0;
212    val = strtod(str, NULL);
213    if (errno != 0 || val < 0) {
214       return 0;
215    }
216   *value = (utime_t)(val * mult[i]);
217    return 1;
218 }
219
220 /*
221  * Edit a utime "duration" into ASCII
222  */
223 char *edit_utime(utime_t val, char *buf)
224 {
225    char mybuf[30];
226    static const int32_t mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
227    static const char *mod[]  = {"year",  "month",  "day", "hour", "min"};
228    int i;
229    uint32_t times;
230
231    *buf = 0;
232    for (i=0; i<5; i++) {
233       times = (uint32_t)(val / mult[i]);
234       if (times > 0) {
235          val = val - (utime_t)times * mult[i];
236          sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
237          strcat(buf, mybuf);
238       }
239    }
240    if (val == 0 && strlen(buf) == 0) {     
241       strcat(buf, "0 secs");
242    } else if (val != 0) {
243       sprintf(mybuf, "%d sec%s", (uint32_t)val, val>1?"s":"");
244       strcat(buf, mybuf);
245    }
246    return buf;
247 }
248
249 /*
250  * Convert a size size in bytes to uint64_t
251  * Returns 0: if error
252            1: if OK, and value stored in value
253  */
254 int size_to_uint64(char *str, int str_len, uint64_t *value)
255 {
256    int i, mod_len;
257    double val;
258    char mod_str[20];
259    static const char *mod[]  = {"*", "k", "kb", "m", "mb",  "g", "gb",  NULL}; /* first item * not used */
260    const int64_t mult[] = {1,             /* byte */
261                            1024,          /* kilobyte */
262                            1000,          /* kb kilobyte */
263                            1048576,       /* megabyte */
264                            1000000,       /* mb megabyte */
265                            1073741824,    /* gigabyte */
266                            1000000000};   /* gb gigabyte */
267
268    if (!get_modifier(str, mod_str, sizeof(mod_str))) {
269       return 0;
270    }
271    /* Now find the multiplier corresponding to the modifier */
272    mod_len = strlen(mod_str);
273    for (i=0; mod[i]; i++) {
274       if (strncasecmp(mod_str, mod[i], mod_len) == 0) {
275          break;
276       }
277    }
278    if (mod[i] == NULL) {
279       i = 0;                          /* no modifier found, assume 1 */
280    }
281    Dmsg2(900, "str=%s: mult=%d\n", str, mult[i]);
282    errno = 0;
283    val = strtod(str, NULL);
284    if (errno != 0 || val < 0) {
285       return 0;
286    }
287   *value = (utime_t)(val * mult[i]);
288    return 1;
289 }
290
291 /*
292  * Check if specified string is a number or not.
293  *  Taken from SQLite, cool, thanks.
294  */
295 int is_a_number(const char *n)
296 {
297    bool digit_seen = false;
298
299    if( *n == '-' || *n == '+' ) {
300       n++;
301    }
302    while (B_ISDIGIT(*n)) {
303       digit_seen = true;
304       n++;
305    }
306    if (digit_seen && *n == '.') {
307       n++;
308       while (B_ISDIGIT(*n)) { n++; }
309    }
310    if (digit_seen && (*n == 'e' || *n == 'E')
311        && (B_ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && B_ISDIGIT(n[2])))) {
312       n += 2;                         /* skip e- or e+ or e digit */
313       while (B_ISDIGIT(*n)) { n++; }
314    }
315    return digit_seen && *n==0;
316 }
317
318 /*
319  * Check if the specified string is an integer   
320  */
321 int is_an_integer(const char *n)
322 {
323    bool digit_seen = false;
324    while (B_ISDIGIT(*n)) {
325       digit_seen = true;
326       n++;
327    }
328    return digit_seen && *n==0;
329 }
330
331 /*
332  * Check if Bacula Resoure Name is valid
333  */
334 /* 
335  * Check if the Volume name has legal characters
336  * If ua is non-NULL send the message
337  */
338 bool is_name_valid(char *name, POOLMEM **msg)
339 {
340    int len;
341    char *p;
342    /* Special characters to accept */
343    const char *accept = ":.-_ ";
344
345    /* Restrict the characters permitted in the Volume name */
346    for (p=name; *p; p++) {
347       if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
348          continue;
349       }
350       if (msg) {
351          Mmsg(msg, _("Illegal character \"%c\" in name.\n"), *p);
352       }
353       return false;
354    }
355    len = strlen(name);
356    if (len >= MAX_NAME_LENGTH) {
357       if (msg) {
358          Mmsg(msg, _("Name too long.\n"));
359       }
360       return false;
361    }
362    if (len == 0) {
363       if (msg) {
364          Mmsg(msg,  _("Volume name must be at least one character long.\n"));
365       }
366       return false;
367    }
368    return true;
369 }
370
371
372
373 /*
374  * Add commas to a string, which is presumably
375  * a number.  
376  */
377 char *add_commas(char *val, char *buf)
378 {
379    int len, nc;
380    char *p, *q;
381    int i;
382
383    if (val != buf) {
384       strcpy(buf, val);
385    }
386    len = strlen(buf);
387    if (len < 1) {
388       len = 1;
389    }
390    nc = (len - 1) / 3;
391    p = buf+len;
392    q = p + nc;
393    *q-- = *p--;
394    for ( ; nc; nc--) {
395       for (i=0; i < 3; i++) {
396           *q-- = *p--;
397       }
398       *q-- = ',';
399    }   
400    return buf;
401 }
402
403 #ifdef TEST_PROGRAM
404 void d_msg(const char*, int, int, const char*, ...)
405 {}
406 int main(int argc, char *argv[])
407 {
408    char *str[] = {"3", "3n", "3 hours", "3.5 day", "3 week", "3 m", "3 q", "3 years"};
409    utime_t val;
410    char buf[100];
411    char outval[100];
412
413    for (int i=0; i<8; i++) {
414       strcpy(buf, str[i]);
415       if (!duration_to_utime(buf, &val)) {
416          printf("Error return from duration_to_utime for in=%s\n", str[i]);
417          continue;
418       }
419       edit_utime(val, outval);
420       printf("in=%s val=%" lld " outval=%s\n", str[i], val, outval);
421    }
422 }
423 #endif