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