Discussion:
[PATCH 1/3] format_print.h: avoid code duplication
Johannes Weißl
2011-03-10 11:47:54 UTC
Permalink
Better if something has to be changed (i.e. add float/double).
---
format_print.h | 15 +++++++--------
1 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/format_print.h b/format_print.h
index de35472..08db08b 100644
--- a/format_print.h
+++ b/format_print.h
@@ -18,17 +18,16 @@ struct format_option {
/* gcc < 4.6 and icc < 12.0 can't properly initialize anonymous unions */
#if (defined(__GNUC__) && defined(__GNUC_MINOR__) && (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 6))) || \
(defined(__INTEL_COMPILER) && __INTEL_COMPILER < 1200)
-#define DEF_FO_STR(c) { .type = FO_STR, .ch = c }
-#define DEF_FO_INT(c) { .type = FO_INT, .ch = c }
-#define DEF_FO_TIME(c) { .type = FO_TIME, .ch = c }
-#define DEF_FO_END { .type = 0, .ch = 0 }
+#define UNION_INIT(f, v) { .f = v }
#else
-#define DEF_FO_STR(c) { .fo_str = NULL, .type = FO_STR, .ch = c }
-#define DEF_FO_INT(c) { .fo_int = 0 , .type = FO_INT, .ch = c }
-#define DEF_FO_TIME(c) { .fo_time = 0 , .type = FO_TIME, .ch = c }
-#define DEF_FO_END { .fo_str = NULL, .type = 0, .ch = 0 }
+#define UNION_INIT(f, v) .f = v
#endif

+#define DEF_FO_STR(c) { UNION_INIT(fo_str, NULL), .type = FO_STR, .ch = c }
+#define DEF_FO_INT(c) { UNION_INIT(fo_int, 0), .type = FO_INT, .ch = c }
+#define DEF_FO_TIME(c) { UNION_INIT(fo_time, 0), .type = FO_TIME, .ch = c }
+#define DEF_FO_END { UNION_INIT(fo_str, NULL), .type = 0, .ch = 0 }
+
int format_print(char *str, int width, const char *format, const struct format_option *fopts);
int format_valid(const char *format);
--
1.7.4.1
Loading...