Selaa lähdekoodia

Generalized the clish_view_restore_e enum

Wanted for better bindings.
Ingo Albrecht 8 vuotta sitten
vanhempi
commit
90b2ec1323
3 muutettua tiedostoa jossa 46 lisäystä ja 9 poistoa
  1. 6 9
      clish/shell/shell_xml.c
  2. 5 0
      clish/view.h
  3. 35 0
      clish/view/view.c

+ 6 - 9
clish/shell/shell_xml.c

@@ -321,12 +321,13 @@ static int process_view(clish_shell_t *shell, clish_xmlnode_t *element,
 	void *parent)
 {
 	clish_view_t *view;
+	clish_view_restore_e restore;
 	int res = -1;
 
 	char *name = clish_xmlnode_fetch_attr(element, "name");
 	char *prompt = clish_xmlnode_fetch_attr(element, "prompt");
 	char *depth = clish_xmlnode_fetch_attr(element, "depth");
-	char *restore = clish_xmlnode_fetch_attr(element, "restore");
+	char *restore_name = clish_xmlnode_fetch_attr(element, "restore");
 	char *access = clish_xmlnode_fetch_attr(element, "access");
 
 	/* Check syntax */
@@ -347,13 +348,9 @@ static int process_view(clish_shell_t *shell, clish_xmlnode_t *element,
 		clish_view__set_depth(view, res);
 	}
 
-	if (restore) {
-		if (!lub_string_nocasecmp(restore, "depth"))
-			clish_view__set_restore(view, CLISH_RESTORE_DEPTH);
-		else if (!lub_string_nocasecmp(restore, "view"))
-			clish_view__set_restore(view, CLISH_RESTORE_VIEW);
-		else
-			clish_view__set_restore(view, CLISH_RESTORE_NONE);
+	if (restore_name) {
+		restore = clish_view_restore_resolve(restore_name);
+		clish_view__set_restore(view, restore);
 	}
 
 	if (access)
@@ -365,7 +362,7 @@ error:
 	clish_xml_release(name);
 	clish_xml_release(prompt);
 	clish_xml_release(depth);
-	clish_xml_release(restore);
+	clish_xml_release(restore_name);
 	clish_xml_release(access);
 
 	parent = parent; /* Happy compiler */

+ 5 - 0
clish/view.h

@@ -62,5 +62,10 @@ _CLISH_GET(view, clish_view_restore_e, restore);
 
 lub_bintree_t * clish_view__get_tree(clish_view_t *instance);
 
+const char *clish_view_restore__get_name(clish_view_restore_e
+	restore);
+clish_view_restore_e clish_view_restore_resolve(const char
+	*restore_name);
+
 #endif				/* _clish_view_h */
 /** @} clish_view */

+ 35 - 0
clish/view/view.c

@@ -322,3 +322,38 @@ lub_bintree_t * clish_view__get_tree(clish_view_t *inst)
 	assert(inst);
 	return &inst->tree;
 }
+/*--------------------------------------------------------- */
+static const char *restore_names[] = {
+	"none",
+	"depth",
+	"view",
+};
+
+/*--------------------------------------------------------- */
+const char *clish_view_restore__get_name(clish_view_restore_e restore)
+{
+	unsigned int max_restore = sizeof(restore_names) / sizeof(char *);
+
+	if (restore >= max_restore)
+		return NULL;
+	return restore_names[restore];
+}
+
+/*--------------------------------------------------------- */
+clish_view_restore_e clish_view_restore_resolve(const char *name)
+{
+	clish_view_restore_e result = CLISH_RESTORE_NONE;
+	if (NULL != name) {
+		unsigned i;
+		for (i = 0; i < CLISH_RESTORE_VIEW + 1; i++) {
+			if (0 == strcmp(name, restore_names[i])) {
+				result = (clish_view_restore_e) i;
+				break;
+			}
+		}
+		/* error for incorrect type spec */
+		assert(i <= CLISH_RESTORE_VIEW);
+	}
+	return result;
+}
+