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