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