2 * edit.c edit string to ascii, and ascii to internal
4 * Kern Sibbald, December MMII
10 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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;
38 while (B_ISSPACE(*p)) {
44 while (B_ISDIGIT(*p)) {
45 value = value * 10 + *p - '0';
51 int64_t str_to_int64(char *str)
53 register char *p = str;
54 register int64_t value;
57 while (B_ISSPACE(*p)) {
62 } else if (*p == '-') {
66 value = str_to_uint64(p);
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.
80 char *edit_uint64_with_commas(uint64_t val, char *buf)
82 sprintf(buf, "%" lld, val);
83 return add_commas(buf, buf);
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.
91 char *edit_uint64(uint64_t val, char *buf)
93 sprintf(buf, "%" lld, val);
98 * Given a string "str", separate the integer part into
99 * str, and the modifier into mod.
101 static bool get_modifier(char *str, char *mod, int mod_len)
105 * Look for modifier by walking back looking for the first
108 strip_trailing_junk(str);
110 /* Strip trailing spaces */
111 for (i=len; i>0; i--) {
112 if (!B_ISSPACE(str[i-1])) {
117 /* Find beginning of the modifier */
119 if (!B_ISALPHA(str[i-1])) {
123 /* If not found, error */
124 if (i == 0 || i == len) {
125 Dmsg2(200, "error i=%d len=%d\n", i, len);
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");
134 Dmsg2(200, "in=%s mod=%s:\n", str, mod);
135 /* Backup over any spaces in front of modifier */
137 if (B_ISSPACE(str[i-1])) {
143 /* The remainder (beginning) should be our number */
144 if (!is_a_number(str)) {
145 Dmsg0(200, "input not a number\n");
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
156 int duration_to_utime(char *str, utime_t *value)
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
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};
171 if (!get_modifier(str, mod_str, sizeof(mod_str))) {
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) {
181 if (mod[i] == NULL) {
182 Dmsg0(200, "Modifier not found\n");
183 return 0; /* modifer not found */
185 Dmsg2(200, "str=%s: mult=%d\n", str, mult[i]);
187 val = strtod(str, NULL);
188 if (errno != 0 || val < 0) {
191 *value = (utime_t)(val * mult[i]);
196 * Edit a utime "duration" into ASCII
198 char *edit_utime(utime_t val, char *buf)
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"};
207 for (i=0; i<5; i++) {
208 times = (uint32_t)(val / mult[i]);
210 val = val - (utime_t)times * mult[i];
211 sprintf(mybuf, "%d %s%s ", times, mod[i], times>1?"s":"");
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":"");
225 * Convert a size size in bytes to uint64_t
226 * Returns 0: if error
227 1: if OK, and value stored in value
229 int size_to_uint64(char *str, int str_len, uint64_t *value)
234 static const char *mod[] = {"*", "k", "kb", "m", "mb", "g", "gb", NULL}; /* first item * not used */
235 const int64_t mult[] = {1, /* byte */
237 1000, /* kb kilobyte */
238 1048576, /* megabyte */
239 1000000, /* mb megabyte */
240 1073741824, /* gigabyte */
241 1000000000}; /* gb gigabyte */
243 if (!get_modifier(str, mod_str, sizeof(mod_str))) {
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) {
253 if (mod[i] == NULL) {
254 Dmsg0(200, "Modifier not found\n");
255 return 0; /* modifer not found */
257 Dmsg2(200, "str=%s: mult=%d\n", str, mult[i]);
259 val = strtod(str, NULL);
260 if (errno != 0 || val < 0) {
263 *value = (utime_t)(val * mult[i]);
268 * Check if specified string is a number or not.
269 * Taken from SQLite, cool, thanks.
271 int is_a_number(const char *n)
273 bool digit_seen = false;
275 if( *n == '-' || *n == '+' ) {
278 while (B_ISDIGIT(*n)) {
282 if (digit_seen && *n == '.') {
284 while (B_ISDIGIT(*n)) { n++; }
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++; }
291 return digit_seen && *n==0;
295 * Check if the specified string is an integer
297 int is_an_integer(const char *n)
299 bool digit_seen = false;
300 while (B_ISDIGIT(*n)) {
304 return digit_seen && *n==0;
308 * Check if Bacula Resoure Name is valid
311 * Check if the Volume name has legal characters
312 * If ua is non-NULL send the message
314 bool is_name_valid(char *name, POOLMEM **msg)
318 /* Special characters to accept */
319 const char *accept = ":.-_ ";
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))) {
327 Mmsg(msg, _("Illegal character \"%c\" in name.\n"), *p);
332 if (len >= MAX_NAME_LENGTH) {
334 Mmsg(msg, _("Name too long.\n"));
340 Mmsg(msg, _("Volume name must be at least one character long.\n"));
350 * Add commas to a string, which is presumably
353 char *add_commas(char *val, char *buf)
371 for (i=0; i < 3; i++) {
380 void d_msg(char*, int, int, char*, ...)
382 int main(int argc, char *argv[])
384 char *str[] = {"3", "3n", "3 hours", "3.5 day", "3 week", "3 m", "3 q", "3 years"};
389 for (int i=0; i<8; i++) {
391 if (!duration_to_utime(buf, &val)) {
392 printf("Error return from duration_to_utime for in=%s\n", str[i]);
395 edit_utime(val, outval);
396 printf("in=%s val=%lld outval=%s\n", str[i], val, outval);