Browse Source

Add some utilities for tri_t

Ingo Albrecht 3 years ago
parent
commit
2d1f8d1314
5 changed files with 54 additions and 0 deletions
  1. 2 0
      lub/dump.h
  2. 2 0
      lub/module.am
  3. 18 0
      lub/types.h
  4. 2 0
      lub/types/module.am
  5. 30 0
      lub/types/tri.c

+ 2 - 0
lub/dump.h

@@ -17,6 +17,8 @@ easy.
 #define LUB_DUMP_NULL "(null)"
 #define LUB_DUMP_STR(str) ( str ? str : LUB_DUMP_NULL )
 #define LUB_DUMP_BOOL(val) ( val ? "true" : "false" )
+#define LUB_DUMP_TRI(val) ( val == TRI_UNDEFINED ? "undefined" : \
+			    (val == TRI_TRUE ? "true" : "false"))
 
 #include <stdarg.h>
 /*=====================================

+ 2 - 0
lub/module.am

@@ -31,6 +31,7 @@ EXTRA_DIST +=   \
     lub/ini/module.am \
     lub/log/module.am \
     lub/conv/module.am \
+    lub/types/module.am \
     lub/README
 
 include $(top_srcdir)/lub/argv/module.am
@@ -44,3 +45,4 @@ include $(top_srcdir)/lub/db/module.am
 include $(top_srcdir)/lub/ini/module.am
 include $(top_srcdir)/lub/log/module.am
 include $(top_srcdir)/lub/conv/module.am
+include $(top_srcdir)/lub/types/module.am

+ 18 - 0
lub/types.h

@@ -30,6 +30,24 @@ typedef enum {
        TRI_TRUE = 1
 } tri_t;
 
+/**
+ * Converts a string to a tri
+ *
+ * "true" yields TRI_TRUE.
+ * "false" yields TRI_FALSE.
+ * Anything else is TRI_FALSE.
+ */
+tri_t lub_tri_from_string(const char *s);
+
+/**
+ * Reduce a tri to bool using a default value
+ *
+ * Will return D if T == TRI_UNDEFINED.
+ *
+ * Returns boolean value of T otherwise.
+ */
+bool_t lub_tri_default(tri_t t, bool_t d);
+
 
 /** @} */
 #endif				/* _lub_types_h */

+ 2 - 0
lub/types/module.am

@@ -0,0 +1,2 @@
+liblub_la_SOURCES += \
+	lub/types/tri.c

+ 30 - 0
lub/types/tri.c

@@ -0,0 +1,30 @@
+/*
+ * tri.c
+ */
+
+#include "lub/types.h"
+
+#include "lub/string.h"
+
+tri_t lub_tri_from_string(const char *s) {
+	if(lub_string_nocasecmp(s, "true") == 0) {
+		return TRI_TRUE;
+	} else if(lub_string_nocasecmp(s, "false") == 0) {
+		return TRI_FALSE;
+	} else {
+		return TRI_UNDEFINED;
+	}
+}
+
+bool_t lub_tri_default(tri_t t, bool_t d)
+{
+	switch(t) {
+	case TRI_TRUE:
+		return BOOL_TRUE;
+	case TRI_FALSE:
+		return BOOL_FALSE;
+	case TRI_UNDEFINED:
+	default:
+		return d;
+	}
+}