Browse Source

tri_t: Convert functions for tri_t type

Serj Kalichev 2 years ago
parent
commit
c9fe4ed541
3 changed files with 56 additions and 0 deletions
  1. 3 0
      faux/conv.h
  2. 51 0
      faux/conv/conv.c
  3. 2 0
      faux/faux.map

+ 3 - 0
faux/conv.h

@@ -27,6 +27,9 @@ bool_t faux_conv_atouc(const char *str, unsigned char *val, int base);
 bool_t faux_conv_str2bool(const char *str, bool_t *val);
 const char *faux_conv_bool2str(bool_t val);
 
+bool_t faux_conv_str2tri(const char *str, tri_t *val);
+const char *faux_conv_tri2str(tri_t val);
+
 C_DECL_END
 
 #endif

+ 51 - 0
faux/conv/conv.c

@@ -323,3 +323,54 @@ const char *faux_conv_bool2str(bool_t val)
 
 	return "false";
 }
+
+
+/** @brief Converts string to tri_t
+ *
+ * Case insensitive.
+ *
+ * @param [in] str Input string to convert.
+ * @param [out] val Pointer to result value.
+ * @return BOOL_TRUE - success, BOOL_FALSE - error
+ */
+bool_t faux_conv_str2tri(const char *str, tri_t *val)
+{
+	if (!str)
+		return BOOL_FALSE;
+
+	if (faux_str_casecmp(str, "true") == 0) {
+		if (val)
+			*val = TRI_TRUE;
+		return BOOL_TRUE;
+	}
+
+	if (faux_str_casecmp(str, "false") == 0) {
+		if (val)
+			*val = TRI_FALSE;
+		return BOOL_TRUE;
+	}
+
+	if (faux_str_casecmp(str, "undefined") == 0) {
+		if (val)
+			*val = TRI_UNDEFINED;
+		return BOOL_TRUE;
+	}
+
+	return BOOL_FALSE;
+}
+
+
+/** @brief Converts tri_t to string
+ *
+ * @param [in] val tri_t value.
+ * @return "true"/"false"/"undefined" strings
+ */
+const char *faux_conv_tri2str(tri_t val)
+{
+	if (TRI_TRUE == val)
+		return "true";
+	if (TRI_FALSE == val)
+		return "false";
+
+	return "undefined";
+}

+ 2 - 0
faux/faux.map

@@ -40,6 +40,8 @@ FAUX_2.0 {
 		faux_conv_atouc;
 		faux_conv_str2bool;
 		faux_conv_bool2str;
+		faux_conv_str2tri;
+		faux_conv_tri2str;
 
 		faux_ctype_isdigit;
 		faux_ctype_isspace;