From 8846a2b47415f92c15b9ab3727b6ebcd31233329 Mon Sep 17 00:00:00 2001 From: Christos Tsantilas Date: Thu, 13 Oct 2011 20:05:25 +0300 Subject: [PATCH] Log field width polishing This patch: - converts type of the Token::[width|precision] members from "unsigned int" to "int" - renames the Token::[width|precision] members to Token::[widthMin/widthMax] - removes unneeded typecastings This is a Measurement Factory project --- src/format/Format.cc | 22 +++++++++++----------- src/format/Tokens.cc | 10 +++++----- src/format/Tokens.h | 8 ++++---- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/format/Format.cc b/src/format/Format.cc index 1f5b2676c0..62063bb9bf 100644 --- a/src/format/Format.cc +++ b/src/format/Format.cc @@ -219,11 +219,11 @@ Format::Format::dump(StoreEntry * entry, const char *name) if (t->zero) entry->append("0", 1); - if (t->width) - storeAppendPrintf(entry, "%d", (int) t->width); + if (t->widthMin >= 0) + storeAppendPrintf(entry, "%d", t->widthMin); - if (t->precision) - storeAppendPrintf(entry, ".%d", (int) t->precision); + if (t->widthMax >= 0) + storeAppendPrintf(entry, ".%d", t->widthMax); if (arg) storeAppendPrintf(entry, "{%s}", arg); @@ -999,11 +999,11 @@ Format::Format::assemble(MemBuf &mb, AccessLogEntry *al, int logSequenceNumber) } if (dooff) { - snprintf(tmp, sizeof(tmp), "%0*" PRId64, fmt->zero ? (int) fmt->width : 0, outoff); + snprintf(tmp, sizeof(tmp), "%0*" PRId64, fmt->zero && fmt->widthMin >= 0 ? fmt->widthMin : 0, outoff); out = tmp; } else if (doint) { - snprintf(tmp, sizeof(tmp), "%0*ld", fmt->zero ? (int) fmt->width : 0, outint); + snprintf(tmp, sizeof(tmp), "%0*ld", fmt->zero && fmt->widthMin >= 0 ? fmt->widthMin : 0, outint); out = tmp; } @@ -1053,12 +1053,12 @@ Format::Format::assemble(MemBuf &mb, AccessLogEntry *al, int logSequenceNumber) } // enforce width limits if configured - const bool haveMaxWidth = fmt->precision && !doint && !dooff; - if (haveMaxWidth || fmt->width) { - const int minWidth = fmt->width ? - static_cast(fmt->width) : 0; + const bool haveMaxWidth = fmt->widthMax >=0 && !doint && !dooff; + if (haveMaxWidth || fmt->widthMin) { + const int minWidth = fmt->widthMin >= 0 ? + fmt->widthMin : 0; const int maxWidth = haveMaxWidth ? - static_cast(fmt->precision) : strlen(out); + fmt->widthMax : strlen(out); if (fmt->left) mb.Printf("%-*.*s", minWidth, maxWidth, out); diff --git a/src/format/Tokens.cc b/src/format/Tokens.cc index 0a4f1b0aeb..f27203e9e5 100644 --- a/src/format/Tokens.cc +++ b/src/format/Tokens.cc @@ -302,10 +302,10 @@ Format::Token::parse(char *def, Quoting *quoting) } if (xisdigit(*cur)) - width = strtol(cur, &cur, 10); + widthMin = strtol(cur, &cur, 10); - if (*cur == '.') - precision = strtol(cur + 1, &cur, 10); + if (*cur == '.' && xisdigit(*(++cur))) + widthMax = strtol(cur, &cur, 10); if (*cur == '{') { char *cp; @@ -479,11 +479,11 @@ done: case LFT_TIME_SUBSECOND: divisor = 1000; - if (precision) { + if (widthMax > 0) { int i; divisor = 1000000; - for (i = precision; i > 1; i--) + for (i = widthMax; i > 1; i--) divisor /= 10; if (!divisor) diff --git a/src/format/Tokens.h b/src/format/Tokens.h index fefe2e059c..507badffe4 100644 --- a/src/format/Tokens.h +++ b/src/format/Tokens.h @@ -186,8 +186,8 @@ class Token public: Token() : type(LFT_NONE), label(NULL), - width(0), - precision(0), + widthMin(-1), + widthMax(-1), quote(LOG_QUOTE_NONE), left(0), space(0), @@ -215,8 +215,8 @@ public: } header; char *timespec; } data; - unsigned int width; - unsigned int precision; + int widthMin; ///< minimum field width + int widthMax; ///< maximum field width enum Quoting quote; unsigned int left:1; unsigned int space:1; -- 2.47.3