Browse Source

Use list for view's namespaces instead the vector

Serj Kalichev 10 years ago
parent
commit
18bd71eb75
4 changed files with 43 additions and 61 deletions
  1. 0 3
      clish/view.h
  2. 2 2
      clish/view/private.h
  3. 34 53
      clish/view/view.c
  4. 7 3
      clish/view/view_dump.c

+ 0 - 3
clish/view.h

@@ -64,9 +64,6 @@ void clish_view_clean_proxy(clish_view_t * instance);
 const char *clish_view__get_name(const clish_view_t * instance);
 void clish_view__set_prompt(clish_view_t * instance, const char *prompt);
 char *clish_view__get_prompt(const clish_view_t *instance);
-unsigned int clish_view__get_nspace_count(const clish_view_t * instance);
-clish_nspace_t *clish_view__get_nspace(const clish_view_t * instance,
-				       unsigned index);
 void clish_view__set_depth(clish_view_t * instance, unsigned depth);
 unsigned clish_view__get_depth(const clish_view_t * instance);
 void clish_view__set_restore(clish_view_t * instance,

+ 2 - 2
clish/view/private.h

@@ -3,6 +3,7 @@
  */
 #include "clish/view.h"
 #include "lub/bintree.h"
+#include "lub/list.h"
 #include "clish/hotkey.h"
 
 /*---------------------------------------------------------
@@ -14,8 +15,7 @@ struct clish_view_s {
 	char *name;
 	char *prompt;
 	char *access;
-	unsigned int nspacec;
-	clish_nspace_t **nspacev;
+	lub_list_t *nspaces;
 	clish_hotkeyv_t *hotkeys;
 	unsigned int depth;
 	clish_view_restore_t restore;

+ 34 - 53
clish/view/view.c

@@ -7,6 +7,7 @@
 #include "lub/argv.h"
 #include "lub/string.h"
 #include "lub/ctype.h"
+#include "lub/list.h"
 
 #include <assert.h>
 #include <stdlib.h>
@@ -41,8 +42,6 @@ static void clish_view_init(clish_view_t * this, const char *name, const char *p
 	/* set up defaults */
 	this->name = lub_string_dup(name);
 	this->prompt = NULL;
-	this->nspacec = 0;
-	this->nspacev = NULL;
 	this->depth = 0;
 	this->restore = CLISH_RESTORE_NONE;
 	this->access = NULL;
@@ -55,6 +54,9 @@ static void clish_view_init(clish_view_t * this, const char *name, const char *p
 		clish_command_bt_offset(),
 		clish_command_bt_compare, clish_command_bt_getkey);
 
+	/* Initialise the list of namespaces */
+	this->nspaces = lub_list_new(NULL);
+
 	/* set up the defaults */
 	clish_view__set_prompt(this, prompt);
 
@@ -66,7 +68,7 @@ static void clish_view_init(clish_view_t * this, const char *name, const char *p
 static void clish_view_fini(clish_view_t * this)
 {
 	clish_command_t *cmd;
-	unsigned i;
+	lub_list_node_t *iter;
 
 	/* delete each command held by this view */
 	while ((cmd = lub_bintree_findfirst(&this->tree))) {
@@ -76,15 +78,14 @@ static void clish_view_fini(clish_view_t * this)
 		clish_command_delete(cmd);
 	}
 
-	/* finalize each of the namespace instances */
-	for (i = 0; i < this->nspacec; i++) {
-		clish_nspace_delete(this->nspacev[i]);
+	/* Free namespaces list */
+	while ((iter = lub_list__get_head(this->nspaces))) {
+		/* Remove the nspace from the list */
+		lub_list_del(this->nspaces, iter);
+		/* Free the instance */
+		clish_nspace_delete((clish_nspace_t *)lub_list_node__get_data(iter));
 	}
-
-	/* free the namespace vector */
-	free(this->nspacev);
-	this->nspacec = 0;
-	this->nspacev = NULL;
+	lub_list_free(this->nspaces);
 
 	/* Free hotkey structures */
 	clish_hotkeyv_delete(this->hotkeys);
@@ -213,10 +214,7 @@ clish_command_t *clish_view_resolve_command(clish_view_t *this,
 clish_command_t *clish_view_find_command(clish_view_t * this,
 	const char *name, bool_t inherit)
 {
-	clish_command_t *cmd, *result = NULL;
-	clish_nspace_t *nspace;
-	unsigned cnt = clish_view__get_nspace_count(this);
-	int i;
+	clish_command_t *result = NULL;
 
 	/* Search the current view */
 	result = lub_bintree_find(&this->tree, name);
@@ -224,10 +222,16 @@ clish_command_t *clish_view_find_command(clish_view_t * this,
 	result = clish_command_alias_to_link(result);
 
 	if (inherit) {
-		for (i = cnt - 1; i >= 0; i--) {
-			nspace = clish_view__get_nspace(this, i);
+		lub_list_node_t *iter;
+		clish_command_t *cmd;
+		clish_nspace_t *nspace;
+
+		/* Iterate elements */
+		for(iter = lub_list__get_head(this->nspaces);
+			iter; iter = lub_list_node__get_next(iter)) {
+			nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
 			cmd = clish_nspace_find_command(nspace, name);
-			/* choose the longest match */
+			/* Choose the longest match */
 			result = clish_command_choose_longest(result, cmd);
 		}
 	}
@@ -278,8 +282,7 @@ const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
 {
 	const clish_command_t *result, *cmd;
 	clish_nspace_t *nspace;
-	unsigned cnt = clish_view__get_nspace_count(this);
-	int i;
+	lub_list_node_t *iter;
 
 	/* ask local view for next command */
 	result = find_next_completion(this, iter_cmd, line);
@@ -288,8 +291,11 @@ const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
 		return result;
 
 	/* ask the imported namespaces for next command */
-	for (i = cnt - 1; i >= 0; i--) {
-		nspace = clish_view__get_nspace(this, i);
+
+	/* Iterate elements */
+	for(iter = lub_list__get_head(this->nspaces);
+		iter; iter = lub_list_node__get_next(iter)) {
+		nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
 		if (!clish_nspace__get_visibility(nspace, field))
 			continue;
 		cmd = clish_nspace_find_next_completion(nspace,
@@ -304,25 +310,18 @@ const clish_command_t *clish_view_find_next_completion(clish_view_t * this,
 /*--------------------------------------------------------- */
 void clish_view_insert_nspace(clish_view_t * this, clish_nspace_t * nspace)
 {
-	size_t new_size = ((this->nspacec + 1) * sizeof(clish_nspace_t *));
-	clish_nspace_t **tmp;
-
-	/* resize the namespace vector */
-	tmp = realloc(this->nspacev, new_size);
-	assert(tmp);
-	this->nspacev = tmp;
-	/* insert reference to the namespace */
-	this->nspacev[this->nspacec++] = nspace;
+	lub_list_add(this->nspaces, nspace);
 }
 
 /*--------------------------------------------------------- */
 void clish_view_clean_proxy(clish_view_t * this)
 {
-	unsigned int i;
+	lub_list_node_t *iter;
 
-	/* Iterate namespace instances */
-	for (i = 0; i < this->nspacec; i++) {
-		clish_nspace_clean_proxy(this->nspacev[i]);
+	/* Iterate elements */
+	for(iter = lub_list__get_head(this->nspaces);
+		iter; iter = lub_list_node__get_next(iter)) {
+		clish_nspace_clean_proxy((clish_nspace_t *)lub_list_node__get_data(iter));
 	}
 }
 
@@ -347,24 +346,6 @@ char *clish_view__get_prompt(const clish_view_t *this)
 	return this->prompt;
 }
 
-/*--------------------------------------------------------- */
-clish_nspace_t *clish_view__get_nspace(const clish_view_t * this,
-				       unsigned index)
-{
-	clish_nspace_t *result = NULL;
-
-	if (index < this->nspacec) {
-		result = this->nspacev[index];
-	}
-	return result;
-}
-
-/*--------------------------------------------------------- */
-unsigned int clish_view__get_nspace_count(const clish_view_t * this)
-{
-	return this->nspacec;
-}
-
 /*--------------------------------------------------------- */
 void clish_view__set_depth(clish_view_t * this, unsigned depth)
 {

+ 7 - 3
clish/view/view_dump.c

@@ -5,6 +5,7 @@
  */
 #include "private.h"
 #include "lub/dump.h"
+#include "lub/list.h"
 
 /*--------------------------------------------------------- */
 void clish_view_dump(clish_view_t * this)
@@ -12,6 +13,7 @@ void clish_view_dump(clish_view_t * this)
 	clish_command_t *c;
 	lub_bintree_iterator_t iter;
 	unsigned i;
+	clish_nspace_t *nspace;
 
 	lub_dump_printf("view(%p)\n", this);
 	lub_dump_indent();
@@ -21,11 +23,13 @@ void clish_view_dump(clish_view_t * this)
 	lub_dump_printf("access : %u\n", clish_view__get_access(this));
 
 	/* Get each namespace to dump their details */
-	for (i = 0; i < this->nspacec; i++) {
-		clish_nspace_dump(clish_view__get_nspace(this, i));
+	for(iter = lub_list__get_head(this->nspaces);
+		iter; iter = lub_list_node__get_next(iter)) {
+		nspace = (clish_nspace_t *)lub_list_node__get_data(iter);
+		clish_nspace_dump(nspace);
 	}
 
-	/* iterate the tree of commands */
+	/* Iterate the tree of commands */
 	c = lub_bintree_findfirst(&this->tree);
 	for (lub_bintree_iterator_init(&iter, &this->tree, c);
 		c; c = lub_bintree_iterator_next(&iter)) {