Browse Source

faux.conv: faux_conv_str2bool() converts string to boolean

Serj Kalichev 3 years ago
parent
commit
048b7c253c
2 changed files with 32 additions and 0 deletions
  1. 2 0
      faux/conv.h
  2. 30 0
      faux/conv/conv.c

+ 2 - 0
faux/conv.h

@@ -24,6 +24,8 @@ bool_t faux_conv_atous(const char *str, unsigned short *val, int base);
 bool_t faux_conv_atoc(const char *str, char *val, int base);
 bool_t faux_conv_atouc(const char *str, unsigned char *val, int base);
 
+bool_t faux_conv_str2bool(const char *str, bool_t *val);
+
 C_DECL_END
 
 #endif

+ 30 - 0
faux/conv/conv.c

@@ -7,6 +7,7 @@
 #include <limits.h>
 
 #include "faux/conv.h"
+#include "faux/str.h"
 
 
 /** @brief Converts string to long int
@@ -279,3 +280,32 @@ bool_t faux_conv_atouc(const char *str, unsigned char *val, int base)
 
 	return BOOL_TRUE;
 }
+
+
+/** @brief Converts string to bool_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_str2bool(const char *str, bool_t *val)
+{
+	if (!str)
+		return BOOL_FALSE;
+
+	if (faux_str_casecmp(str, "true") == 0) {
+		if (val)
+			*val = BOOL_TRUE;
+		return BOOL_TRUE;
+	}
+
+	if (faux_str_casecmp(str, "false") == 0) {
+		if (val)
+			*val = BOOL_FALSE;
+		return BOOL_TRUE;
+	}
+
+	return BOOL_FALSE;
+}