Browse Source

bfg_strtobool helper function

Luke Dashjr 11 years ago
parent
commit
0e204e3a91
4 changed files with 67 additions and 19 deletions
  1. 5 14
      driver-knc.c
  2. 6 3
      driver-opencl.c
  3. 54 2
      util.c
  4. 2 0
      util.h

+ 5 - 14
driver-knc.c

@@ -31,6 +31,7 @@
 #include "logging.h"
 #include "lowl-spi.h"
 #include "miner.h"
+#include "util.h"
 
 #define KNC_POLL_INTERVAL_US 10000
 #define KNC_SPI_SPEED 3000000
@@ -825,21 +826,11 @@ const char *knc_set_use_dcdc(struct cgpu_info *proc, const char * const optname,
 {
 	int core_index_on_die = proc->proc_id % KNC_CORES_PER_DIE;
 	bool nv;
+	char *end;
 	
-	if (!(strcasecmp(newvalue, "no") && strcasecmp(newvalue, "false") && strcasecmp(newvalue, "0") && strcasecmp(newvalue, "off") && strcasecmp(newvalue, "disable")))
-		nv = false;
-	else
-	if (!(strcasecmp(newvalue, "yes") && strcasecmp(newvalue, "true") && strcasecmp(newvalue, "on") && strcasecmp(newvalue, "enable")))
-		nv = true;
-	else
-	{
-		char *p;
-		strtol(newvalue, &p, 0);
-		if (newvalue[0] && !p[0])
-			nv = true;
-		else
-			return "Usage: use_dcdc=yes/no";
-	}
+	nv = bfg_strtobool(newvalue, &end, 0);
+	if (!(newvalue[0] && !end[0]))
+		return "Usage: use_dcdc=yes/no";
 	
 	if (core_index_on_die)
 	{

+ 6 - 3
driver-opencl.c

@@ -432,9 +432,12 @@ static
 const char *opencl_init_binary(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const out_success)
 {
 	struct opencl_device_data * const data = proc->device_data;
+	char *end;
+	bool nv;
 	
-	if (!(strcasecmp(newvalue, "no") && strcasecmp(newvalue, "never") && strcasecmp(newvalue, "none")))
-		data->opt_opencl_binaries = OBU_NONE;
+	nv = bfg_strtobool(newvalue, &end, 0);
+	if (newvalue[0] && !end[0])
+		data->opt_opencl_binaries = nv ? OBU_LOADSAVE : OBU_NONE;
 	else
 	if (!(strcasecmp(newvalue, "load") && strcasecmp(newvalue, "read")))
 		data->opt_opencl_binaries = OBU_LOAD;
@@ -442,7 +445,7 @@ const char *opencl_init_binary(struct cgpu_info * const proc, const char * const
 	if (!(strcasecmp(newvalue, "save") && strcasecmp(newvalue, "write")))
 		data->opt_opencl_binaries = OBU_SAVE;
 	else
-	if (!(strcasecmp(newvalue, "yes") && strcasecmp(newvalue, "always") && strcasecmp(newvalue, "both")))
+	if (!(strcasecmp(newvalue, "both")))
 		data->opt_opencl_binaries = OBU_LOADSAVE;
 	else
 	if (!(strcasecmp(newvalue, "default")))

+ 54 - 2
util.c

@@ -2058,6 +2058,53 @@ void test_domain_funcs()
 	_test_get_regd_domain("2001:db8::1", "2001:db8::1");
 }
 
+struct bfg_strtobool_keyword {
+	bool val;
+	const char *keyword;
+};
+
+bool bfg_strtobool(const char * const s, char ** const endptr, __maybe_unused const int opts)
+{
+	struct bfg_strtobool_keyword keywords[] = {
+		{false, "false"},
+		{false, "never"},
+		{false, "none"},
+		{false, "off"},
+		{false, "no"},
+		{false, "0"},
+		
+		{true , "always"},
+		{true , "true"},
+		{true , "yes"},
+		{true , "on"},
+	};
+	
+	const int total_keywords = sizeof(keywords) / sizeof(*keywords);
+	for (int i = 0; i < total_keywords; ++i)
+	{
+		const size_t kwlen = strlen(keywords[i].keyword);
+		if (!strncasecmp(keywords[i].keyword, s, kwlen))
+		{
+			if (endptr)
+				*endptr = (char*)&s[kwlen];
+			return keywords[i].val;
+		}
+	}
+	
+	char *lend;
+	strtol(s, &lend, 0);
+	if (lend > s)
+	{
+		if (endptr)
+			*endptr = lend;
+		// Any number other than "0" is intentionally considered true, including 0x0
+		return true;
+	}
+	
+	*endptr = (char*)s;
+	return false;
+}
+
 bool uri_get_param_bool(const char * const uri, const char * const param, const bool defval)
 {
 	const char *start = strchr(uri, '#');
@@ -2081,8 +2128,10 @@ nextmatch:
 	if (q[0] == '=')
 	{
 		++q;
-		if (q[0] == '0' && !isCalpha(q[1]))
-			foundval = false;
+		char *end;
+		bool v = bfg_strtobool(q, &end, 0);
+		if (end > q && !isCalpha(end[0]))
+			foundval = v;
 	}
 	if (invert)
 		foundval = !foundval;
@@ -2114,6 +2163,9 @@ void test_uri_get_param()
 	_test_uri_get_param("stratum+tcp://footest/#redirect=1,foo=0", "redirect", false, true);
 	_test_uri_get_param("stratum+tcp://footest/#foo=1,noredirect=0,foo=1", "redirect", false, true);
 	_test_uri_get_param("stratum+tcp://footest/#bar=0,noredirect=1,foo=0", "redirect", true, false);
+	_test_uri_get_param("stratum+tcp://footest/#redirect=false", "redirect", true, false);
+	_test_uri_get_param("stratum+tcp://footest/#redirect=no", "redirect", true, false);
+	_test_uri_get_param("stratum+tcp://footest/#redirect=yes", "redirect", false, true);
 }
 
 void stratum_probe_transparency(struct pool *pool)

+ 2 - 0
util.h

@@ -119,6 +119,8 @@ extern const char *extract_domain(size_t *out_len, const char *uri, size_t urile
 extern bool match_domains(const char *a, size_t alen, const char *b, size_t blen);
 extern void test_domain_funcs();
 
+extern bool bfg_strtobool(const char *, char **endptr, int opts);
+
 extern bool uri_get_param_bool(const char *uri, const char *param, bool defval);
 extern void test_uri_get_param();