- bug fix in decodeWithPrefix

This commit is contained in:
zolliker
2012-10-08 08:47:23 +00:00
parent e7531270fb
commit d798373fdf

View File

@ -41,6 +41,38 @@ typedef struct {
static char *dirs[MAX_DIRS] = { NULL };
static int ndirs = 0;
static float decodeWithPrefix(char *text, int *ok) {
static char *prefixes = "yzafpnumkMGTPEZY";
static float multiplier[] = {
1e-24, 1e-21, 1e-18, 1e-15, 1e-12, 1e-9, 1e-6, 1e-3,
1e3, 1e6, 1e9, 1e12, 1e15, 1e18, 1e21, 1e24};
char *pos, *unit;
float result;
float eps;
result = strtod(text, &unit);
if (unit == text) {
if (ok) {
*ok = 0;
}
return 0.0;
}
while (*unit == ' ') {
unit++;
}
if (unit[0] != '\0' && unit[1] > ' ') {
/* do not allow prefixes without unit (like "m" - might be meter instead of milli) */
pos = strchr(prefixes, *unit);
if (pos != NULL) {
result *= multiplier[pos-prefixes];
}
}
if (ok) {
*ok = 1;
}
return result;
}
static void InitCompressor(Compressor * c, SConnection * pCon, time_t step)
{
c->pCon = pCon;
@ -90,15 +122,18 @@ static void PutValue(Compressor * c, time_t t, char *value)
{
char *p;
Point new;
char unitPrefix;
int ok;
WriteHeader(c);
if (c->type == NUMERIC) {
new.y = strtod(value, &p);
if (p == value) {
new.y = LOGGER_NAN;
} else {
if (new.y == LOGGER_NAN)
new.y = decodeWithPrefix(value, &ok);
if (ok) {
if (new.y == LOGGER_NAN) {
new.y *= 1.0000002;
}
} else {
new.y = LOGGER_NAN;
}
new.t = t;
if (t >= c->tlim) { /* a new interval starts */