]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/edit.c
28ea49bb4a3359fa2c52fb08b99f8237dfd96ab7
[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
31 /* We assume ASCII input and don't worry about overflow */
32 uint64_t str_to_uint64(char *str) 
33 {
34    register char *p = str;
35    register uint64_t value = 0;
36
37    while (B_ISSPACE(*p)) {
38       p++;
39    }
40    if (*p == '+') {
41       p++;
42    }
43    while (B_ISDIGIT(*p)) {
44       value = value * 10 + *p - '0';
45       p++;
46    }
47    return value;
48 }
49
50 int64_t str_to_int64(char *str) 
51 {
52    register char *p = str;
53    register int64_t value;
54    int negative = FALSE;
55
56    while (B_ISSPACE(*p)) {
57       p++;
58    }
59    if (*p == '+') {
60       p++;
61    } else if (*p == '-') {
62       negative = TRUE;
63       p++;
64    }
65    value = str_to_uint64(p);
66    if (negative) {
67       value = -value;
68    }
69    return value;
70 }
71
72
73
74 /*
75  * Edit an integer number with commas, the supplied buffer
76  * must be at least 27 bytes long.  The incoming number
77  * is always widened to 64 bits.
78  */
79 char *edit_uint64_with_commas(uint64_t val, char *buf)
80 {
81    sprintf(buf, "%" lld, val);
82    return add_commas(buf, buf);
83 }
84
85 /*
86  * Edit an integer number, the supplied buffer
87  * must be at least 27 bytes long.  The incoming number
88  * is always widened to 64 bits.
89  */
90 char *edit_uint64(uint64_t val, char *buf)
91 {
92    sprintf(buf, "%" lld, val);
93    return buf;
94 }
95
96
97 /*
98  * Convert a string duration to utime_t (64 bit seconds)
99  * Returns 0: if error
100            1: if OK, and value stored in value
101  */
102 int duration_to_utime(char *str, utime_t *value)
103 {
104    int i, ch, len;
105    double val;
106    static int  mod[] = {'*', 's', 'n', 'h', 'd', 'w', 'm', 'q', 'y', 0};
107    static int mult[] = {1,    1,  60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30, 
108                   60*60*24*91, 60*60*24*365};
109
110    /* Look for modifier */
111    len = strlen(str);
112    ch = str[len - 1];
113    i = 0;
114    if (B_ISALPHA(ch)) {
115       if (B_ISUPPER(ch)) {
116          ch = tolower(ch);
117       }
118       while (mod[++i] != 0) {
119          if (ch == mod[i]) {
120             len--;
121             str[len] = 0; /* strip modifier */
122             break;
123          }
124       }
125    }
126    if (mod[i] == 0 || !is_a_number(str)) {
127       return 0;
128    }
129    val = strtod(str, NULL);
130    if (errno != 0 || val < 0) {
131       return 0;
132    }
133    *value = (utime_t)(val * mult[i]);
134    return 1;
135
136 }
137
138 /*
139  * Edit a utime "duration" into ASCII
140  */
141 char *edit_utime(utime_t val, char *buf)
142 {
143    char mybuf[30];
144    static int mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
145    static char *mod[]  = {"year",  "month",  "day", "hour", "min"};
146    int i;
147    uint32_t times;
148
149    *buf = 0;
150    for (i=0; i<5; i++) {
151       times = val / mult[i];
152       if (times > 0) {
153          val = val - (utime_t)times * mult[i];
154          sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
155          strcat(buf, mybuf);
156       }
157    }
158    if (val == 0 && strlen(buf) == 0) {     
159       strcat(buf, "0 secs");
160    } else if (val != 0) {
161       sprintf(mybuf, "%d sec%s", (uint32_t)val, val>1?"s":"");
162       strcat(buf, mybuf);
163    }
164    return buf;
165 }
166
167 /*
168  * Convert a size size in bytes to uint64_t
169  * Returns 0: if error
170            1: if OK, and value stored in value
171  */
172 int size_to_uint64(char *str, int str_len, uint64_t *rtn_value)
173 {
174    int i, ch;
175    double value;
176    int mod[]  = {'*', 'k', 'm', 'g', 0}; /* first item * not used */
177    uint64_t mult[] = {1,             /* byte */
178                       1024,          /* kilobyte */
179                       1048576,       /* megabyte */
180                       1073741824};   /* gigabyte */
181
182 #ifdef we_have_a_compiler_that_works
183    int mod[]  = {'*', 'k', 'm', 'g', 't', 0};
184    uint64_t mult[] = {1,             /* byte */
185                       1024,          /* kilobyte */
186                       1048576,       /* megabyte */
187                       1073741824,    /* gigabyte */
188                       1099511627776};/* terabyte */
189 #endif
190
191    Dmsg0(400, "Enter sized to uint64\n");
192
193    /* Look for modifier */
194    ch = str[str_len - 1];
195    i = 0;
196    if (B_ISALPHA(ch)) {
197       if (B_ISUPPER(ch)) {
198          ch = tolower(ch);
199       }
200       while (mod[++i] != 0) {
201          if (ch == mod[i]) {
202             str_len--;
203             str[str_len] = 0; /* strip modifier */
204             break;
205          }
206       }
207    }
208    if (mod[i] == 0 || !is_a_number(str)) {
209       return 0;
210    }
211    Dmsg3(400, "size str=:%s: %f i=%d\n", str, strtod(str, NULL), i);
212
213    value = (uint64_t)strtod(str, NULL);
214    Dmsg1(400, "Int value = %d\n", (int)value);
215    if (errno != 0 || value < 0) {
216       return 0;
217    }
218    *rtn_value = (uint64_t)(value * mult[i]);
219    Dmsg2(400, "Full value = %f %" lld "\n", strtod(str, NULL) * mult[i],
220        value *mult[i]);
221    return 1;
222 }
223
224 /*
225  * Check if specified string is a number or not.
226  *  Taken from SQLite, cool, thanks.
227  */
228 int is_a_number(const char *n)
229 {
230    int digit_seen = 0;
231
232    if( *n == '-' || *n == '+' ) {
233       n++;
234    }
235    while (B_ISDIGIT(*n)) {
236       digit_seen = 1;
237       n++;
238    }
239    if (digit_seen && *n == '.') {
240       n++;
241       while (B_ISDIGIT(*n)) { n++; }
242    }
243    if (digit_seen && (*n == 'e' || *n == 'E')
244        && (B_ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && B_ISDIGIT(n[2])))) {
245       n += 2;                         /* skip e- or e+ or e digit */
246       while (B_ISDIGIT(*n)) { n++; }
247    }
248    return digit_seen && *n==0;
249 }
250
251 /*
252  * Check if the specified string is an integer   
253  */
254 int is_an_integer(const char *n)
255 {
256    int digit_seen = 0;
257    while (B_ISDIGIT(*n)) {
258       digit_seen = 1;
259       n++;
260    }
261    return digit_seen && *n==0;
262 }
263
264 /*
265  * Add commas to a string, which is presumably
266  * a number.  
267  */
268 char *add_commas(char *val, char *buf)
269 {
270    int len, nc;
271    char *p, *q;
272    int i;
273
274    if (val != buf) {
275       strcpy(buf, val);
276    }
277    len = strlen(buf);
278    if (len < 1) {
279       len = 1;
280    }
281    nc = (len - 1) / 3;
282    p = buf+len;
283    q = p + nc;
284    *q-- = *p--;
285    for ( ; nc; nc--) {
286       for (i=0; i < 3; i++) {
287           *q-- = *p--;
288       }
289       *q-- = ',';
290    }   
291    return buf;
292 }