asyc-ii: Rephrase "exponent" logic when parsing packets

Replace a C library strcspn(3) call with the more portable glib
g_strstr_len(3) routine. This is possible since a single separator
is searched for, no actual "set of characters" is involved.

As a byproduct, this eliminates a "late" reference to 'cnt' at the
bottom of the routine, after the value was assigned in a rather distant
location at the top of the routine. The cost of strlen() should be
acceptable for a buffer with a single digit total length.
This commit is contained in:
Gerhard Sittig 2016-12-31 14:25:07 +01:00 committed by Uwe Hermann
parent 43528280d9
commit 661aa24aa6
1 changed files with 7 additions and 5 deletions

View File

@ -60,7 +60,8 @@ static int parse_value(const char *buf, struct asycii_info *info,
{
char valstr[7 + 1];
const char *valp;
int i, cnt, is_ol, dot_pos;
int i, cnt, is_ol;
const char *dot_pos;
/*
* Strip all spaces from bytes 0-6. By copying all
@ -102,12 +103,13 @@ static int parse_value(const char *buf, struct asycii_info *info,
sr_spew("%s(), cannot convert number", __func__);
return SR_ERR_DATA;
}
dot_pos = strcspn(valstr, ".");
if (dot_pos < cnt)
*exponent = -(cnt - dot_pos - 1);
dot_pos = g_strstr_len(valstr, -1, ".");
if (dot_pos)
*exponent = -(valstr + strlen(valstr) - dot_pos - 1);
else
*exponent = 0;
sr_spew("%s(), display value is %f", __func__, *result);
sr_spew("%s(), display value is %f, exponent %d",
__func__, *result, *exponent);
return SR_OK;
}