return buf;
}
-/*
- * Convert a size in bytes to uint64_t
- * Returns false: if error
- true: if OK, and value stored in value
- */
-bool size_to_uint64(char *str, int str_len, uint64_t *value)
+static bool strunit_to_uint64(char *str, int str_len, uint64_t *value,
+ const char **mod)
{
int i, mod_len;
double val;
char mod_str[20];
char num_str[50];
- static const char *mod[] = {"*", "k", "kb", "m", "mb", "g", "gb", NULL}; /* first item * not used */
const int64_t mult[] = {1, /* byte */
1024, /* kilobyte */
1000, /* kb kilobyte */
return true;
}
+/*
+ * Convert a size in bytes to uint64_t
+ * Returns false: if error
+ true: if OK, and value stored in value
+ */
+bool size_to_uint64(char *str, int str_len, uint64_t *value)
+{
+ /* first item * not used */
+ static const char *mod[] = {"*", "k", "kb", "m", "mb", "g", "gb", NULL};
+ return strunit_to_uint64(str, str_len, value, mod);
+}
+
+/*
+ * Convert a speed in bytes/s to uint64_t
+ * Returns false: if error
+ true: if OK, and value stored in value
+ */
+bool speed_to_uint64(char *str, int str_len, uint64_t *value)
+{
+ /* first item * not used */
+ static const char *mod[] = {"*", "k/s", "kb/s", "m/s", "mb/s", NULL};
+ return strunit_to_uint64(str, str_len, value, mod);
+}
+
/*
* Check if specified string is a number or not.
* Taken from SQLite, cool, thanks.
*(int64_t *)(items[i].value) = items[i].default_value;
} else if (items[i].handler == store_size64) {
*(uint64_t *)(items[i].value) = (uint64_t)items[i].default_value;
+ } else if (items[i].handler == store_speed) {
+ *(uint64_t *)(items[i].value) = (uint64_t)items[i].default_value;
} else if (items[i].handler == store_time) {
*(utime_t *)(items[i].value) = (utime_t)items[i].default_value;
} else if (pass == 1 && items[i].handler == store_addresses) {
set_bit(index, res_all.hdr.item_present);
}
+enum store_unit_type {
+ STORE_SIZE,
+ STORE_SPEED
+} ;
+
/* Store a size in bytes */
-static void store_size(LEX *lc, RES_ITEM *item, int index, int pass, bool size32)
+static void store_int_unit(LEX *lc, RES_ITEM *item, int index, int pass,
+ bool size32, enum store_unit_type type)
{
int token;
uint64_t uvalue;
char bsize[500];
- Dmsg0(900, "Enter store_size\n");
+ Dmsg0(900, "Enter store_unit\n");
token = lex_get_token(lc, T_SKIP_EOL);
errno = 0;
switch (token) {
break;
}
}
- if (!size_to_uint64(bsize, strlen(bsize), &uvalue)) {
- scan_err1(lc, _("expected a size number, got: %s"), lc->str);
- return;
+ if (type == STORE_SIZE) {
+ if (!size_to_uint64(bsize, strlen(bsize), &uvalue)) {
+ scan_err1(lc, _("expected a size number, got: %s"), lc->str);
+ return;
+ }
+ } else {
+ if (!speed_to_uint64(bsize, strlen(bsize), &uvalue)) {
+ scan_err1(lc, _("expected a speed number, got: %s"), lc->str);
+ return;
+ }
}
if (size32) {
*(uint32_t *)(item->value) = (uint32_t)uvalue;
}
break;
default:
- scan_err1(lc, _("expected a size, got: %s"), lc->str);
+ scan_err2(lc, _("expected a %s, got: %s"),
+ (type == STORE_SIZE)?_("size"):_("speed"), lc->str);
return;
}
if (token != T_EOL) {
scan_to_eol(lc);
}
set_bit(index, res_all.hdr.item_present);
- Dmsg0(900, "Leave store_size\n");
+ Dmsg0(900, "Leave store_unit\n");
}
/* Store a size in bytes */
void store_size32(LEX *lc, RES_ITEM *item, int index, int pass)
{
- store_size(lc, item, index, pass, true /* 32 bit */);
+ store_int_unit(lc, item, index, pass, true /* 32 bit */, STORE_SIZE);
}
/* Store a size in bytes */
void store_size64(LEX *lc, RES_ITEM *item, int index, int pass)
{
- store_size(lc, item, index, pass, false /* not 32 bit */);
+ store_int_unit(lc, item, index, pass, false /* not 32 bit */, STORE_SIZE);
}
+/* Store a speed in bytes/s */
+void store_speed(LEX *lc, RES_ITEM *item, int index, int pass)
+{
+ store_int_unit(lc, item, index, pass, false /* 64 bit */, STORE_SPEED);
+}
/* Store a time period in seconds */
void store_time(LEX *lc, RES_ITEM *item, int index, int pass)