Commit ae0d629c authored by Alessandro Rubini's avatar Alessandro Rubini

shell: fromdec/fromhex: parse sign

While we also have atoi, our custom fromdec/fromhex are more similar
to strtol, and thus useful to parse "number.number" or such stuff.

And since I need to pass negative hex numbers in later commits, here
it is.
Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 882ba996
......@@ -42,16 +42,14 @@ static int cmd_faketemp(const char *args[])
}
for (i = 0; i < 3 && args[i]; i++) {
int sign = 1, val;
int val;
/* accept negative, and at most one decimal */
if (args[i][0] == '-')
sign = -1, args[i]++;
/* accept at most one decimal */
dot = fromdec(args[i], &val);
val <<= 16;
if (dot[0] == '.' && dot[1] >= '0' && dot[1] <= '9')
val += 0x10000 / 10 * (dot[1] - '0');
temp_fake_data[i].t = val * sign;
temp_fake_data[i].t = val;
}
return 0;
}
......
......@@ -214,8 +214,12 @@ int shell_interactive()
const char *fromhex(const char *hex, int *v)
{
int o = 0;
int o = 0, sign = 1;
if (hex && *hex == '-') {
sign = -1;
hex++;
}
for (; hex && *hex; ++hex) {
if (*hex >= '0' && *hex <= '9') {
o = (o << 4) + (*hex - '0');
......@@ -228,14 +232,18 @@ const char *fromhex(const char *hex, int *v)
}
}
*v = o;
*v = o * sign;
return hex;
}
const char *fromdec(const char *dec, int *v)
{
int o = 0;
int o = 0, sign = 1;
if (dec && *dec == '-') {
sign = -1;
dec++;
}
for (; dec && *dec; ++dec) {
if (*dec >= '0' && *dec <= '9') {
o = (o * 10) + (*dec - '0');
......@@ -244,7 +252,7 @@ const char *fromdec(const char *dec, int *v)
}
}
*v = o;
*v = o * sign;
return dec;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment