]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] provide easy-to-use limit_r and LIM2A* macros
authorWilly Tarreau <w@1wt.eu>
Thu, 25 Oct 2007 14:58:42 +0000 (16:58 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 25 Oct 2007 14:59:40 +0000 (16:59 +0200)
This is in fact the same as ultoa() except that it's possible to
pass the string to be returned in case the value is NULL. This is
useful to report limits in printf calls.

include/common/standard.h
src/standard.c

index 4808a228590420181729d55679a9180f85d409c8..90ed618a96fa586097e7f005e0ae85022fc7b69b 100644 (file)
@@ -88,6 +88,30 @@ static inline const char *ultoa(unsigned long n)
 #define U2A8(n) ({ ultoa_r((n), itoa_str[8], sizeof(itoa_str[8])); })
 #define U2A9(n) ({ ultoa_r((n), itoa_str[9], sizeof(itoa_str[9])); })
 
+/*
+ * This function simply returns a locally allocated string containing the ascii
+ * representation for number 'n' in decimal, unless n is 0 in which case it
+ * returns the alternate string (or an empty string if the alternate string is
+ * NULL). It use is intended for limits reported in reports, where it's
+ * desirable not to display anything if there is no limit. Warning! it shares
+ * the same vector as ultoa_r().
+ */
+extern const char *limit_r(unsigned long n, char *buffer, int size, const char *alt);
+
+/* Fast macros to convert up to 10 different parameters inside a same call of
+ * expression. Warning! they share the same vectors as U2A*!
+ */
+#define LIM2A0(n, alt) ({ limit_r((n), itoa_str[0], sizeof(itoa_str[0]), (alt)); })
+#define LIM2A1(n, alt) ({ limit_r((n), itoa_str[1], sizeof(itoa_str[1]), (alt)); })
+#define LIM2A2(n, alt) ({ limit_r((n), itoa_str[2], sizeof(itoa_str[2]), (alt)); })
+#define LIM2A3(n, alt) ({ limit_r((n), itoa_str[3], sizeof(itoa_str[3]), (alt)); })
+#define LIM2A4(n, alt) ({ limit_r((n), itoa_str[4], sizeof(itoa_str[4]), (alt)); })
+#define LIM2A5(n, alt) ({ limit_r((n), itoa_str[5], sizeof(itoa_str[5]), (alt)); })
+#define LIM2A6(n, alt) ({ limit_r((n), itoa_str[6], sizeof(itoa_str[6]), (alt)); })
+#define LIM2A7(n, alt) ({ limit_r((n), itoa_str[7], sizeof(itoa_str[7]), (alt)); })
+#define LIM2A8(n, alt) ({ limit_r((n), itoa_str[8], sizeof(itoa_str[8]), (alt)); })
+#define LIM2A9(n, alt) ({ limit_r((n), itoa_str[9], sizeof(itoa_str[9]), (alt)); })
+
 /*
  * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
  */
index fff7168ca66777cb0fb5b55c17d5c7788da7991f..1e631301c587a45aa77f378ad211cd042424311c 100644 (file)
@@ -63,6 +63,19 @@ const char *ultoa_r(unsigned long n, char *buffer, int size)
        return pos + 1;
 }
 
+/*
+ * This function simply returns a locally allocated string containing the ascii
+ * representation for number 'n' in decimal, unless n is 0 in which case it
+ * returns the alternate string (or an empty string if the alternate string is
+ * NULL). It use is intended for limits reported in reports, where it's
+ * desirable not to display anything if there is no limit. Warning! it shares
+ * the same vector as ultoa_r().
+ */
+const char *limit_r(unsigned long n, char *buffer, int size, const char *alt)
+{
+       return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : "");
+}
+
 
 /*
  * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.