From d798373fdf859fc7fe62ce598164b832c2122132 Mon Sep 17 00:00:00 2001 From: zolliker Date: Mon, 8 Oct 2012 08:47:23 +0000 Subject: [PATCH] - bug fix in decodeWithPrefix --- logreader.c | 47 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/logreader.c b/logreader.c index cb45049e..f5cd6f03 100644 --- a/logreader.c +++ b/logreader.c @@ -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 */