[PATCH] libosmocore[master]: Use value_string for ctrl_type

Max gerrit-no-reply at lists.osmocom.org
Fri Feb 24 14:04:26 UTC 2017


Review at  https://gerrit.osmocom.org/1895

Use value_string for ctrl_type

Use value_string for enum ctrl_type instead of custom code.

Change-Id: Icd4e96dd9f00876cb70b43cfcf42ab4f10311b28
---
M include/osmocom/ctrl/control_cmd.h
M src/ctrl/control_cmd.c
2 files changed, 16 insertions(+), 33 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/95/1895/1

diff --git a/include/osmocom/ctrl/control_cmd.h b/include/osmocom/ctrl/control_cmd.h
index a63557d..d9092f3 100644
--- a/include/osmocom/ctrl/control_cmd.h
+++ b/include/osmocom/ctrl/control_cmd.h
@@ -4,7 +4,7 @@
 #include <osmocom/core/talloc.h>
 #include <osmocom/core/write_queue.h>
 #include <osmocom/core/logging.h>
-
+#include <osmocom/core/utils.h>
 #include <osmocom/vty/vector.h>
 
 #define CTRL_CMD_ERROR		-1
@@ -31,6 +31,8 @@
 	CTRL_TYPE_ERROR
 };
 
+extern const struct value_string ctrl_type_vals[];
+
 struct ctrl_connection {
 	struct llist_head list_entry;
 
diff --git a/src/ctrl/control_cmd.c b/src/ctrl/control_cmd.c
index 2cf66cf..1cc8247 100644
--- a/src/ctrl/control_cmd.c
+++ b/src/ctrl/control_cmd.c
@@ -33,41 +33,21 @@
 
 #include <osmocom/core/msgb.h>
 #include <osmocom/core/talloc.h>
-
+#include <osmocom/core/utils.h>
 #include <osmocom/vty/command.h>
 #include <osmocom/vty/vector.h>
 
 extern vector ctrl_node_vec;
 
-static struct ctrl_cmd_map ccm[] = {
-	{"GET", CTRL_TYPE_GET},
-	{"SET", CTRL_TYPE_SET},
-	{"GET_REPLY", CTRL_TYPE_GET_REPLY},
-	{"SET_REPLY", CTRL_TYPE_SET_REPLY},
-	{"TRAP", CTRL_TYPE_TRAP},
-	{"ERROR", CTRL_TYPE_ERROR},
-	{NULL}
+const struct value_string ctrl_type_vals[] = {
+	{ CTRL_TYPE_GET,	"GET" },
+	{ CTRL_TYPE_SET,	"SET" },
+	{ CTRL_TYPE_GET_REPLY,	"GET_REPLY" },
+	{ CTRL_TYPE_SET_REPLY,	"SET_REPLY" },
+	{ CTRL_TYPE_TRAP,	"TRAP" },
+	{ CTRL_TYPE_ERROR,	"ERROR" },
+	{ CTRL_TYPE_UNKNOWN,	NULL }
 };
-
-static int ctrl_cmd_str2type(char *s)
-{
-	int i;
-	for (i=0; ccm[i].cmd != NULL; i++) {
-		if (strcasecmp(s, ccm[i].cmd) == 0)
-			return ccm[i].type;
-	}
-	return CTRL_TYPE_UNKNOWN;
-}
-
-static char *ctrl_cmd_type2str(int type)
-{
-	int i;
-	for (i=0; ccm[i].cmd != NULL; i++) {
-		if (ccm[i].type == type)
-			return ccm[i].cmd;
-	}
-	return NULL;
-}
 
 /* Functions from libosmocom */
 extern vector cmd_make_descvec(const char *string, const char *descstr);
@@ -308,7 +288,7 @@
 		goto err;
 	}
 
-	cmd->type = ctrl_cmd_str2type(tmp);
+	cmd->type = get_string_value(ctrl_type_vals, tmp);
 	if (cmd->type == CTRL_TYPE_UNKNOWN) {
 		cmd->type = CTRL_TYPE_ERROR;
 		cmd->id = "err";
@@ -403,7 +383,8 @@
 struct msgb *ctrl_cmd_make(struct ctrl_cmd *cmd)
 {
 	struct msgb *msg;
-	char *type, *tmp;
+	const char *type;
+	char *tmp;
 
 	if (!cmd->id)
 		return NULL;
@@ -412,7 +393,7 @@
 	if (!msg)
 		return NULL;
 
-	type = ctrl_cmd_type2str(cmd->type);
+	type = get_value_string(ctrl_type_vals, cmd->type);
 
 	switch (cmd->type) {
 	case CTRL_TYPE_GET:

-- 
To view, visit https://gerrit.osmocom.org/1895
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Icd4e96dd9f00876cb70b43cfcf42ab4f10311b28
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Max <msuraev at sysmocom.de>


More information about the gerrit-log mailing list