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

=== modified file 'src/format/Format.cc'
--- src/format/Format.cc	2011-09-12 00:31:13 +0000
+++ src/format/Format.cc	2011-10-05 15:29:09 +0000
@@ -202,45 +202,45 @@
                     break;
 
                 case LOG_QUOTE_URL:
                     entry->append("#", 1);
                     break;
 
                 case LOG_QUOTE_RAW:
                     entry->append("'", 1);
                     break;
 
                 case LOG_QUOTE_NONE:
                     break;
                 }
 
                 if (t->left)
                     entry->append("-", 1);
 
                 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);
 
                 storeAppendPrintf(entry, "%s", t->label);
 
                 if (t->space)
                     entry->append(" ", 1);
             }
         }
 
         entry->append("\n", 1);
     }
 
 }
 
 static void
 log_quoted_string(const char *str, char *out)
 {
     char *p = out;
@@ -982,45 +982,45 @@
         case LFT_EXT_LOG:
             if (al->request)
                 out = al->request->extacl_log.termedBuf();
 
             quote = 1;
 
             break;
 
         case LFT_SEQUENCE_NUMBER:
             outoff = logSequenceNumber;
             dooff = 1;
             break;
 
         case LFT_PERCENT:
             out = "%";
 
             break;
         }
 
         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;
         }
 
         if (out && *out) {
             if (quote || fmt->quote != LOG_QUOTE_NONE) {
                 char *newout = NULL;
                 int newfree = 0;
 
                 switch (fmt->quote) {
 
                 case LOG_QUOTE_NONE:
                     newout = rfc1738_escape_unescaped(out);
                     break;
 
                 case LOG_QUOTE_QUOTES: {
                     size_t out_len = static_cast<size_t>(strlen(out)) * 2 + 1;
                     if (out_len >= sizeof(tmp)) {
                         newout = (char *)xmalloc(out_len);
                         newfree = 1;
                     } else
@@ -1036,46 +1036,46 @@
 
                 case LOG_QUOTE_URL:
                     newout = rfc1738_escape(out);
                     break;
 
                 case LOG_QUOTE_RAW:
                     break;
                 }
 
                 if (newout) {
                     if (dofree)
                         safe_free(out);
 
                     out = newout;
 
                     dofree = newfree;
                 }
             }
 
             // enforce width limits if configured
-            const bool haveMaxWidth = fmt->precision && !doint && !dooff;
-            if (haveMaxWidth || fmt->width) {
-                const int minWidth = fmt->width ?
-                                     static_cast<int>(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<int>(fmt->precision) : strlen(out);
+                                     fmt->widthMax : strlen(out);
 
                 if (fmt->left)
                     mb.Printf("%-*.*s", minWidth, maxWidth, out);
                 else
                     mb.Printf("%*.*s", minWidth, maxWidth, out);
             } else
                 mb.append(out, strlen(out));
         } else {
             mb.append("-", 1);
         }
 
         if (fmt->space)
             mb.append(" ", 1);
 
         sb.clean();
 
         if (dofree)
             safe_free(out);
     }
 }

=== modified file 'src/format/Tokens.cc'
--- src/format/Tokens.cc	2011-09-09 20:41:40 +0000
+++ src/format/Tokens.cc	2011-10-05 15:41:51 +0000
@@ -285,44 +285,44 @@
         quote = LOG_QUOTE_URL;
         cur++;
         break;
 
     default:
         quote = *quoting;
         break;
     }
 
     if (*cur == '-') {
         left = 1;
         cur++;
     }
 
     if (*cur == '0') {
         zero = 1;
         cur++;
     }
 
     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;
         cur++;
         l = strcspn(cur, "}");
         cp = (char *)xmalloc(l + 1);
         xstrncpy(cp, cur, l + 1);
         data.string = cp;
         cur += l;
 
         if (*cur == '}')
             cur++;
     }
 
     type = LFT_NONE;
 
     // Scan each token namespace
     if (strncmp(cur, "icap::", 6) == 0) {
 #if ICAP_CLIENT
         cur += 6;
@@ -462,45 +462,45 @@
                 break;
             case LFT_ICAP_REP_HEADER:
                 type = LFT_ICAP_REP_ALL_HEADERS;
                 break;
 #endif
             default:
                 break;
             }
             Config.onoff.log_mime_hdrs = 1;
         }
 
         break;
 
     case LFT_CLIENT_FQDN:
         Config.onoff.log_fqdn = 1;
         break;
 
     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)
                 divisor = 0;
         }
         break;
 
     case LFT_HTTP_SENT_STATUS_CODE_OLD_30:
         debugs(46, 0, "WARNING: The \"Hs\" formatting code is deprecated. Use the \">Hs\" instead.");
         type = LFT_HTTP_SENT_STATUS_CODE;
         break;
 
     case LFT_SERVER_LOCAL_IP_OLD_27:
         debugs(46, 0, "WARNING: The \"oa\" formatting code is deprecated. Use the \"<la\" instead.");
         type = LFT_SERVER_LOCAL_IP;
         break;
 
     case LFT_REQUEST_URLPATH_OLD_31:
         debugs(46, 0, "WARNING: The \"rp\" formatting code is deprecated. Use the \">rp\" instead.");
         type = LFT_CLIENT_REQ_URLPATH;

=== modified file 'src/format/Tokens.h'
--- src/format/Tokens.h	2011-09-12 00:31:13 +0000
+++ src/format/Tokens.h	2011-10-05 15:20:25 +0000
@@ -169,67 +169,67 @@
 enum Quoting {
     LOG_QUOTE_NONE = 0,
     LOG_QUOTE_QUOTES,
     LOG_QUOTE_MIMEBLOB,
     LOG_QUOTE_URL,
     LOG_QUOTE_RAW
 };
 
 struct TokenTableEntry {
     const char *config;
     ByteCode_t token_type;
     int options;
 };
 
 // XXX: inherit from linked list
 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),
             zero(0),
             divisor(0),
             next(NULL)
     { data.string = NULL; };
     ~Token();
 
     /** parses a single token. Returns the token length in characters,
      * and fills in this item with the token information.
      * def is for sure null-terminated.
      */
     int parse(char *def, enum Quoting *quote);
 
     ByteCode_t type;
     const char *label;
     union {
         char *string;
 
         struct {
             char *header;
             char *element;
             char separator;
         } 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;
     unsigned int zero:1;
     int divisor;
     Token *next;	/* todo: move from linked list to array */
 
 private:
     char *scanForToken(const struct TokenTableEntry *table, char *cur);
 };
 
 extern const char *log_tags[];
 
 } // namespace Format
 
 #endif /* _SQUID_FMT_TOKENS_H */


