]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/edit.c
Use rentrant mysql lib, eliminate race in sql_list, Win32 streams, misc see kes-1.31
[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    /* Default to 1 day if no modifier given */
107    static int  mod[] = {'*',       's', 'n', 'h', 'd',      'w',    'm', 'q', 'y', 0};
108    static int mult[] = {60*60*24,   1,  60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30, 
109                   60*60*24*91, 60*60*24*365};
110
111    /* Look for modifier */
112    len = strlen(str);
113    ch = str[len - 1];
114    i = 0;
115    if (B_ISALPHA(ch)) {
116       if (B_ISUPPER(ch)) {
117          ch = tolower(ch);
118       }
119       while (mod[++i] != 0) {
120          if (ch == mod[i]) {
121             len--;
122             str[len] = 0; /* strip modifier */
123             break;
124          }
125       }
126    }
127    if (mod[i] == 0 || !is_a_number(str)) {
128       return 0;
129    }
130    val = strtod(str, NULL);
131    if (errno != 0 || val < 0) {
132       return 0;
133    }
134    *value = (utime_t)(val * mult[i]);
135    return 1;
136
137 }
138
139 /*
140  * Edit a utime "duration" into ASCII
141  */
142 char *edit_utime(utime_t val, char *buf)
143 {
144    char mybuf[30];
145    static int mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
146    static char *mod[]  = {"year",  "month",  "day", "hour", "min"};
147    int i;
148    uint32_t times;
149
150    *buf = 0;
151    for (i=0; i<5; i++) {
152       times = val / mult[i];
153       if (times > 0) {
154          val = val - (utime_t)times * mult[i];
155          sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
156          strcat(buf, mybuf);
157       }
158    }
159    if (val == 0 && strlen(buf) == 0) {     
160       strcat(buf, "0 secs");
161    } else if (val != 0) {
162       sprintf(mybuf, "%d sec%s", (uint32_t)val, val>1?"s":"");
163       strcat(buf, mybuf);
164    }
165    return buf;
166 }
167
168 /*
169  * Convert a size size in bytes to uint64_t
170  * Returns 0: if error
171            1: if OK, and value stored in value
172  */
173 int size_to_uint64(char *str, int str_len, uint64_t *rtn_value)
174 {
175    int i, ch;
176    double value;
177    int mod[]  = {'*', 'k', 'm', 'g', 0}; /* first item * not used */
178    uint64_t mult[] = {1,             /* byte */
179                       1024,          /* kilobyte */
180                       1048576,       /* megabyte */
181                       1073741824};   /* gigabyte */
182
183 #ifdef we_have_a_compiler_that_works
184    int mod[]  = {'*', 'k', 'm', 'g', 't', 0};
185    uint64_t mult[] = {1,             /* byte */
186                       1024,          /* kilobyte */
187                       1048576,       /* megabyte */
188                       1073741824,    /* gigabyte */
189                       1099511627776};/* terabyte */
190 #endif
191
192    Dmsg0(400, "Enter sized to uint64\n");
193
194    /* Look for modifier */
195    ch = str[str_len - 1];
196    i = 0;
197    if (B_ISALPHA(ch)) {
198       if (B_ISUPPER(ch)) {
199          ch = tolower(ch);
200       }
201       while (mod[++i] != 0) {
202          if (ch == mod[i]) {
203             str_len--;
204             str[str_len] = 0; /* strip modifier */
205             break;
206          }
207       }
208    }
209    if (mod[i] == 0 || !is_a_number(str)) {
210       return 0;
211    }
212    Dmsg3(400, "size str=:%s: %f i=%d\n", str, strtod(str, NULL), i);
213
214    value = (uint64_t)strtod(str, NULL);
215    Dmsg1(400, "Int value = %d\n", (int)value);
216    if (errno != 0 || value < 0) {
217       return 0;
218    }
219    *rtn_value = (uint64_t)(value * mult[i]);
220    Dmsg2(400, "Full value = %f %" lld "\n", strtod(str, NULL) * mult[i],
221        value *mult[i]);
222    return 1;
223 }
224
225 /*
226  * Check if specified string is a number or not.
227  *  Taken from SQLite, cool, thanks.
228  */
229 int is_a_number(const char *n)
230 {
231    int digit_seen = 0;
232
233    if( *n == '-' || *n == '+' ) {
234       n++;
235    }
236    while (B_ISDIGIT(*n)) {
237       digit_seen = 1;
238       n++;
239    }
240    if (digit_seen && *n == '.') {
241       n++;
242       while (B_ISDIGIT(*n)) { n++; }
243    }
244    if (digit_seen && (*n == 'e' || *n == 'E')
245        && (B_ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && B_ISDIGIT(n[2])))) {
246       n += 2;                         /* skip e- or e+ or e digit */
247       while (B_ISDIGIT(*n)) { n++; }
248    }
249    return digit_seen && *n==0;
250 }
251
252 /*
253  * Check if the specified string is an integer   
254  */
255 int is_an_integer(const char *n)
256 {
257    int digit_seen = 0;
258    while (B_ISDIGIT(*n)) {
259       digit_seen = 1;
260       n++;
261    }
262    return digit_seen && *n==0;
263 }
264
265 /*
266  * Add commas to a string, which is presumably
267  * a number.  
268  */
269 char *add_commas(char *val, char *buf)
270 {
271    int len, nc;
272    char *p, *q;
273    int i;
274
275    if (val != buf) {
276       strcpy(buf, val);
277    }
278    len = strlen(buf);
279    if (len < 1) {
280       len = 1;
281    }
282    nc = (len - 1) / 3;
283    p = buf+len;
284    q = p + nc;
285    *q-- = *p--;
286    for ( ; nc; nc--) {
287       for (i=0; i < 3; i++) {
288           *q-- = *p--;
289       }
290       *q-- = ',';
291    }   
292    return buf;
293 }