2 * edit.c edit string to ascii, and ascii to internal
4 * Kern Sibbald, December MMII
10 Copyright (C) 2000-2005 Kern Sibbald
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.
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.
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,
32 /* We assume ASCII input and don't worry about overflow */
33 uint64_t str_to_uint64(char *str)
35 register char *p = str;
36 register uint64_t value = 0;
41 while (B_ISSPACE(*p)) {
47 while (B_ISDIGIT(*p)) {
48 value = value * 10 + *p - '0';
54 int64_t str_to_int64(char *str)
56 register char *p = str;
57 register int64_t value;
58 bool negative = false;
63 while (B_ISSPACE(*p)) {
68 } else if (*p == '-') {
72 value = str_to_uint64(p);
81 * Edit an integer number with commas, the supplied buffer
82 * must be at least 27 bytes long. The incoming number
83 * is always widened to 64 bits.
85 char *edit_uint64_with_commas(uint64_t val, char *buf)
88 * Replacement for sprintf(buf, "%" llu, val)
91 mbuf[sizeof(mbuf)-1] = 0;
92 int i = sizeof(mbuf)-2; /* edit backward */
97 mbuf[i--] = "0123456789"[val%10];
101 strcpy(buf, &mbuf[i+1]);
102 return add_commas(buf, buf);
106 * Edit an integer number, the supplied buffer
107 * must be at least 27 bytes long. The incoming number
108 * is always widened to 64 bits.
110 char *edit_uint64(uint64_t val, char *buf)
113 * Replacement for sprintf(buf, "%" llu, val)
116 mbuf[sizeof(mbuf)-1] = 0;
117 int i = sizeof(mbuf)-2; /* edit backward */
122 mbuf[i--] = "0123456789"[val%10];
126 strcpy(buf, &mbuf[i+1]);
130 char *edit_int64(int64_t val, char *buf)
133 * Replacement for sprintf(buf, "%" llu, val)
136 bool negative = false;
137 mbuf[sizeof(mbuf)-1] = 0;
138 int i = sizeof(mbuf)-2; /* edit backward */
147 mbuf[i--] = "0123456789"[val%10];
154 strcpy(buf, &mbuf[i+1]);
160 * Given a string "str", separate the integer part into
161 * str, and the modifier into mod.
163 static bool get_modifier(char *str, char *num, int num_len, char *mod, int mod_len)
165 int i, len, num_begin, num_end, mod_begin, mod_end;
168 * Look for modifier by walking back looking for the first
171 strip_trailing_junk(str);
174 for (i=0; i<len; i++) {
175 if (!B_ISSPACE(str[i])) {
181 /* Walk through integer part */
182 for ( ; i<len; i++) {
183 if (!B_ISDIGIT(str[i])) {
188 if (num_len > (num_end - num_begin + 1)) {
189 num_len = num_end - num_begin + 1;
194 for ( ; i<len; i++) {
195 if (!B_ISSPACE(str[i])) {
200 for ( ; i<len; i++) {
201 if (!B_ISALPHA(str[i])) {
206 if (mod_len > (mod_end - mod_begin + 1)) {
207 mod_len = mod_end - mod_begin + 1;
209 Dmsg5(900, "str=%s: num_beg=%d num_end=%d mod_beg=%d mod_end=%d\n",
210 str, num_begin, num_end, mod_begin, mod_end);
211 bstrncpy(num, &str[num_begin], num_len);
212 bstrncpy(mod, &str[mod_begin], mod_len);
213 if (!is_a_number(num)) {
216 bstrncpy(str, &str[mod_end], len);
222 * Convert a string duration to utime_t (64 bit seconds)
223 * Returns 0: if error
224 1: if OK, and value stored in value
226 int duration_to_utime(char *str, utime_t *value)
229 double val, total = 0.0;
233 * The "n" = mins and months appears before minutes so that m maps
234 * to months. These "kludges" make it compatible with pre 1.31
237 static const char *mod[] = {"n", "seconds", "months", "minutes",
238 "hours", "days", "weeks", "quarters", "years", NULL};
239 static const int32_t mult[] = {60, 1, 60*60*24*30, 60,
240 60*60, 60*60*24, 60*60*24*7, 60*60*24*91, 60*60*24*365};
243 if (!get_modifier(str, num_str, sizeof(num_str), mod_str, sizeof(mod_str))) {
246 /* Now find the multiplier corresponding to the modifier */
247 mod_len = strlen(mod_str);
249 i = 1; /* assume seconds */
251 for (i=0; mod[i]; i++) {
252 if (strncasecmp(mod_str, mod[i], mod_len) == 0) {
256 if (mod[i] == NULL) {
257 i = 1; /* no modifier, assume secs */
260 Dmsg2(900, "str=%s: mult=%d\n", num_str, mult[i]);
262 val = strtod(num_str, NULL);
263 if (errno != 0 || val < 0) {
266 total += val * mult[i];
268 *value = (utime_t)total;
273 * Edit a utime "duration" into ASCII
275 char *edit_utime(utime_t val, char *buf, int buf_len)
278 static const int32_t mult[] = {60*60*24*365, 60*60*24*30, 60*60*24, 60*60, 60};
279 static const char *mod[] = {"year", "month", "day", "hour", "min"};
284 for (i=0; i<5; i++) {
285 times = (uint32_t)(val / mult[i]);
287 val = val - (utime_t)times * mult[i];
288 bsnprintf(mybuf, sizeof(mybuf), "%d %s%s ", times, mod[i], times>1?"s":"");
289 bstrncat(buf, mybuf, buf_len);
292 if (val == 0 && strlen(buf) == 0) {
293 bstrncat(buf, "0 secs", buf_len);
294 } else if (val != 0) {
295 bsnprintf(mybuf, sizeof(mybuf), "%d sec%s", (uint32_t)val, val>1?"s":"");
296 bstrncat(buf, mybuf, buf_len);
302 * Convert a size in bytes to uint64_t
303 * Returns 0: if error
304 1: if OK, and value stored in value
306 int size_to_uint64(char *str, int str_len, uint64_t *value)
312 static const char *mod[] = {"*", "k", "kb", "m", "mb", "g", "gb", NULL}; /* first item * not used */
313 const int64_t mult[] = {1, /* byte */
315 1000, /* kb kilobyte */
316 1048576, /* megabyte */
317 1000000, /* mb megabyte */
318 1073741824, /* gigabyte */
319 1000000000}; /* gb gigabyte */
321 if (!get_modifier(str, num_str, sizeof(num_str), mod_str, sizeof(mod_str))) {
324 /* Now find the multiplier corresponding to the modifier */
325 mod_len = strlen(mod_str);
326 for (i=0; mod[i]; i++) {
327 if (strncasecmp(mod_str, mod[i], mod_len) == 0) {
331 if (mod[i] == NULL) {
332 i = 0; /* no modifier found, assume 1 */
334 Dmsg2(900, "str=%s: mult=%d\n", str, mult[i]);
336 val = strtod(num_str, NULL);
337 if (errno != 0 || val < 0) {
340 *value = (utime_t)(val * mult[i]);
345 * Check if specified string is a number or not.
346 * Taken from SQLite, cool, thanks.
348 bool is_a_number(const char *n)
350 bool digit_seen = false;
352 if( *n == '-' || *n == '+' ) {
355 while (B_ISDIGIT(*n)) {
359 if (digit_seen && *n == '.') {
361 while (B_ISDIGIT(*n)) { n++; }
363 if (digit_seen && (*n == 'e' || *n == 'E')
364 && (B_ISDIGIT(n[1]) || ((n[1]=='-' || n[1] == '+') && B_ISDIGIT(n[2])))) {
365 n += 2; /* skip e- or e+ or e digit */
366 while (B_ISDIGIT(*n)) { n++; }
368 return digit_seen && *n==0;
372 * Check if the specified string is an integer
374 bool is_an_integer(const char *n)
376 bool digit_seen = false;
377 while (B_ISDIGIT(*n)) {
381 return digit_seen && *n==0;
385 * Check if Bacula Resoure Name is valid
388 * Check if the Volume name has legal characters
389 * If ua is non-NULL send the message
391 bool is_name_valid(char *name, POOLMEM **msg)
395 /* Special characters to accept */
396 const char *accept = ":.-_ ";
398 /* Restrict the characters permitted in the Volume name */
399 for (p=name; *p; p++) {
400 if (B_ISALPHA(*p) || B_ISDIGIT(*p) || strchr(accept, (int)(*p))) {
404 Mmsg(msg, _("Illegal character \"%c\" in name.\n"), *p);
409 if (len >= MAX_NAME_LENGTH) {
411 Mmsg(msg, _("Name too long.\n"));
417 Mmsg(msg, _("Volume name must be at least one character long.\n"));
427 * Add commas to a string, which is presumably
430 char *add_commas(char *val, char *buf)
448 for (i=0; i < 3; i++) {
457 void d_msg(const char*, int, int, const char*, ...)
459 int main(int argc, char *argv[])
461 char *str[] = {"3", "3n", "3 hours", "3.5 day", "3 week", "3 m", "3 q", "3 years"};
466 for (int i=0; i<8; i++) {
468 if (!duration_to_utime(buf, &val)) {
469 printf("Error return from duration_to_utime for in=%s\n", str[i]);
472 edit_utime(val, outval);
473 printf("in=%s val=%" lld " outval=%s\n", str[i], val, outval);