]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/edit.c
Implement state file and save/restore last_jobs list
[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 const char *mod[] = {"n", "seconds", "months", "minutes", 
113                   "hours", "days", "weeks",   "quarters",   "years", NULL};
114    static const 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 #if defined(HAVE_WIN32)
266    /* work around microsofts non handling of uint64 to double cvt*/
267    *rtn_value = (uint64_t)(value * (__int64)mult[i]);
268    Dmsg2(400, "Full value = %lf %" lld "\n", value * (__int64)mult[i],  
269          (uint64_t)(value * (__int64)mult[i]));
270 #else
271    *rtn_value = (uint64_t)(value * mult[i]);
272    Dmsg2(400, "Full value = %lf %" lld "\n", value * mult[i],  
273       (uint64_t)(value * mult[i]));
274 #endif
275    return 1;
276 }
277
278 /*
279  * Check if specified string is a number or not.
280  *  Taken from SQLite, cool, thanks.
281  */
282 int is_a_number(const char *n)
283 {
284    bool digit_seen = false;
285
286    if( *n == '-' || *n == '+' ) {
287       n++;
288    }
289    while (B_ISDIGIT(*n)) {
290       digit_seen = true;
291       n++;
292    }
293    if (digit_seen && *n == '.') {
294       n++;
295       while (B_ISDIGIT(*n)) { n++; }
296    }
297    if (digit_seen && (*n == 'e' || *n == 'E')
298        && (B_ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && B_ISDIGIT(n[2])))) {
299       n += 2;                         /* skip e- or e+ or e digit */
300       while (B_ISDIGIT(*n)) { n++; }
301    }
302    return digit_seen && *n==0;
303 }
304
305 /*
306  * Check if the specified string is an integer   
307  */
308 int is_an_integer(const char *n)
309 {
310    bool digit_seen = false;
311    while (B_ISDIGIT(*n)) {
312       digit_seen = true;
313       n++;
314    }
315    return digit_seen && *n==0;
316 }
317
318 /*
319  * Check if Bacula Resoure Name is valid
320  */
321 /* 
322  * Check if the Volume name has legal characters
323  * If ua is non-NULL send the message
324  */
325 bool is_name_valid(char *name, POOLMEM **msg)
326 {
327    int len;
328    char *p;
329    /* Special characters to accept */
330    const char *accept = ":.-_ ";
331
332    /* Restrict the characters permitted in the Volume name */
333    for (p=name; *p; p++) {
334       if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
335          continue;
336       }
337       if (msg) {
338          Mmsg(msg, _("Illegal character \"%c\" in name.\n"), *p);
339       }
340       return false;
341    }
342    len = strlen(name);
343    if (len >= MAX_NAME_LENGTH) {
344       if (msg) {
345          Mmsg(msg, _("Name too long.\n"));
346       }
347       return false;
348    }
349    if (len == 0) {
350       if (msg) {
351          Mmsg(msg,  _("Volume name must be at least one character long.\n"));
352       }
353       return false;
354    }
355    return true;
356 }
357
358
359
360 /*
361  * Add commas to a string, which is presumably
362  * a number.  
363  */
364 char *add_commas(char *val, char *buf)
365 {
366    int len, nc;
367    char *p, *q;
368    int i;
369
370    if (val != buf) {
371       strcpy(buf, val);
372    }
373    len = strlen(buf);
374    if (len < 1) {
375       len = 1;
376    }
377    nc = (len - 1) / 3;
378    p = buf+len;
379    q = p + nc;
380    *q-- = *p--;
381    for ( ; nc; nc--) {
382       for (i=0; i < 3; i++) {
383           *q-- = *p--;
384       }
385       *q-- = ',';
386    }   
387    return buf;
388 }
389
390 #ifdef TEST_PROGRAM
391 void d_msg(char*, int, int, char*, ...)
392 {}
393 int main(int argc, char *argv[])
394 {
395    char *str[] = {"3", "3n", "3 hours", "3.5 day", "3 week", "3 m", "3 q", "3 years"};
396    utime_t val;
397    char buf[100];
398    char outval[100];
399
400    for (int i=0; i<8; i++) {
401       strcpy(buf, str[i]);
402       if (!duration_to_utime(buf, &val)) {
403          printf("Error return from duration_to_utime for in=%s\n", str[i]);
404          continue;
405       }
406       edit_utime(val, outval);
407       printf("in=%s val=%lld outval=%s\n", str[i], val, outval);
408    }
409 }
410 #endif