generalised parse_units' and
unparse_units' and added new functions
`parse_flags' and `unparse_flags' that use these git-svn-id: svn://svn.h5l.se/heimdal/trunk/heimdal@3237 ec53bebd-3082-4978-b11e-865c3cabbd6b
This commit is contained in:
@@ -52,13 +52,15 @@ RCSID("$Id$");
|
|||||||
* def_unit defines the default unit.
|
* def_unit defines the default unit.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int
|
static int
|
||||||
parse_units (const char *s, const struct units *units,
|
parse_something (const char *s, const struct units *units,
|
||||||
const char *def_unit)
|
const char *def_unit,
|
||||||
|
int (*func)(int res, int val, unsigned mult),
|
||||||
|
int init,
|
||||||
|
int accept_no_val_p)
|
||||||
{
|
{
|
||||||
const char *p;
|
const char *p;
|
||||||
int res = 0;
|
int res = init;
|
||||||
int val;
|
|
||||||
unsigned def_mult = 1;
|
unsigned def_mult = 1;
|
||||||
|
|
||||||
if (def_unit != NULL) {
|
if (def_unit != NULL) {
|
||||||
@@ -84,24 +86,36 @@ parse_units (const char *s, const struct units *units,
|
|||||||
|
|
||||||
val = strtod (p, &next); /* strtol(p, &next, 0); */
|
val = strtod (p, &next); /* strtol(p, &next, 0); */
|
||||||
if (val == 0 && p == next)
|
if (val == 0 && p == next)
|
||||||
return -1;
|
if(accept_no_val_p)
|
||||||
|
val = 1;
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
p = next;
|
p = next;
|
||||||
while (isspace(*p))
|
while (isspace(*p))
|
||||||
++p;
|
++p;
|
||||||
if (*p == '\0') {
|
if (*p == '\0') {
|
||||||
res += val * def_mult;
|
res = (*func)(res, val, def_mult);
|
||||||
|
if (res < 0)
|
||||||
|
return res;
|
||||||
break;
|
break;
|
||||||
|
} else if (*p == '+') {
|
||||||
|
++p;
|
||||||
|
} else if (*p == '-') {
|
||||||
|
++p;
|
||||||
|
val = -1;
|
||||||
}
|
}
|
||||||
u_len = strcspn (p, "0123456789 \t");
|
u_len = strcspn (p, "0123456789 \t");
|
||||||
partial = NULL;
|
|
||||||
partial = 0;
|
partial = 0;
|
||||||
|
partial_unit = NULL;
|
||||||
if (u_len > 1 && p[u_len - 1] == 's')
|
if (u_len > 1 && p[u_len - 1] == 's')
|
||||||
--u_len;
|
--u_len;
|
||||||
for (u = units; u->name; ++u) {
|
for (u = units; u->name; ++u) {
|
||||||
if (strncasecmp (p, u->name, u_len) == 0) {
|
if (strncasecmp (p, u->name, u_len) == 0) {
|
||||||
if (u_len == strlen (u->name)) {
|
if (u_len == strlen (u->name)) {
|
||||||
p += u_len;
|
p += u_len;
|
||||||
res += val * u->mult;
|
res = (*func)(res, val, u->mult);
|
||||||
|
if (res < 0)
|
||||||
|
return res;
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
++partial;
|
++partial;
|
||||||
@@ -112,7 +126,9 @@ parse_units (const char *s, const struct units *units,
|
|||||||
if (u->name == NULL)
|
if (u->name == NULL)
|
||||||
if (partial == 1) {
|
if (partial == 1) {
|
||||||
p += u_len;
|
p += u_len;
|
||||||
res += val * partial_unit->mult;
|
res = (*func)(res, val, partial_unit->mult);
|
||||||
|
if (res < 0)
|
||||||
|
return res;
|
||||||
} else {
|
} else {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -123,28 +139,72 @@ parse_units (const char *s, const struct units *units,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return a string representation according to `units' of `num' in `s'
|
* The string consists of a sequence of `n unit'
|
||||||
* with maximum length `len'.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
size_t
|
static int
|
||||||
unparse_units (int num, const struct units *units, char *s, size_t len)
|
acc_units(int res, int val, unsigned mult)
|
||||||
|
{
|
||||||
|
return res + val * mult;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
parse_units (const char *s, const struct units *units,
|
||||||
|
const char *def_unit)
|
||||||
|
{
|
||||||
|
return parse_something (s, units, def_unit, acc_units, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The string consists of a sequence of `[+-]flag'. `orig' consists
|
||||||
|
* the original set of flags, those are then modified and returned as
|
||||||
|
* the function value.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static int
|
||||||
|
acc_flags(int res, int val, unsigned mult)
|
||||||
|
{
|
||||||
|
if(val == 1)
|
||||||
|
return res | mult;
|
||||||
|
else if(val == -1)
|
||||||
|
return res & ~mult;
|
||||||
|
else
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
parse_flags (const char *s, const struct units *units,
|
||||||
|
int orig)
|
||||||
|
{
|
||||||
|
return parse_something (s, units, NULL, acc_flags, orig, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Return a string representation according to `units' of `num' in `s'
|
||||||
|
* with maximum length `len'. The actual length is the function value.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static size_t
|
||||||
|
unparse_something (int num, const struct units *units, char *s, size_t len,
|
||||||
|
int (*print) (char *s, size_t len, int div,
|
||||||
|
const char *name, int rem),
|
||||||
|
int (*update) (int in, unsigned mult),
|
||||||
|
const char *zero_string)
|
||||||
{
|
{
|
||||||
const struct units *u;
|
const struct units *u;
|
||||||
size_t ret = 0, tmp;
|
size_t ret = 0, tmp;
|
||||||
|
|
||||||
if (num == 0)
|
if (num == 0)
|
||||||
return snprintf (s, len, "%u", 0);
|
return snprintf (s, len, "%s", zero_string);
|
||||||
|
|
||||||
for (u = units; num > 0 && u->name; ++u) {
|
for (u = units; num > 0 && u->name; ++u) {
|
||||||
int div;
|
int div;
|
||||||
|
|
||||||
div = num / u->mult;
|
div = num / u->mult;
|
||||||
if (div) {
|
if (div) {
|
||||||
num %= u->mult;
|
num = (*update) (num, u->mult);
|
||||||
tmp = snprintf (s, len, "%u %s%s%s", div, u->name,
|
tmp = (*print) (s, len, div, u->name, num);
|
||||||
div == 1 ? "" : "s",
|
|
||||||
num > 0 ? " " : "");
|
|
||||||
len -= tmp;
|
len -= tmp;
|
||||||
s += tmp;
|
s += tmp;
|
||||||
ret += tmp;
|
ret += tmp;
|
||||||
@@ -152,3 +212,48 @@ unparse_units (int num, const struct units *units, char *s, size_t len)
|
|||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
print_unit (char *s, size_t len, int div, const char *name, int rem)
|
||||||
|
{
|
||||||
|
return snprintf (s, len, "%u %s%s%s",
|
||||||
|
div, name,
|
||||||
|
div == 1 ? "" : "s",
|
||||||
|
rem > 0 ? " " : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
update_unit (int in, unsigned mult)
|
||||||
|
{
|
||||||
|
return in % mult;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t
|
||||||
|
unparse_units (int num, const struct units *units, char *s, size_t len)
|
||||||
|
{
|
||||||
|
return unparse_something (num, units, s, len,
|
||||||
|
print_unit,
|
||||||
|
update_unit,
|
||||||
|
"0");
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
print_flag (char *s, size_t len, int div, const char *name, int rem)
|
||||||
|
{
|
||||||
|
return snprintf (s, len, "%s%s", name, rem > 0 ? ", " : "");
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
update_flag (int in, unsigned mult)
|
||||||
|
{
|
||||||
|
return in - mult;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t
|
||||||
|
unparse_flags (int num, const struct units *units, char *s, size_t len)
|
||||||
|
{
|
||||||
|
return unparse_something (num, units, s, len,
|
||||||
|
print_flag,
|
||||||
|
update_flag,
|
||||||
|
"");
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user