Add ast_db_freetree and ast_db_gettree
authorMark Spencer <markster@digium.com>
Sun, 13 Apr 2003 04:17:45 +0000 (04:17 +0000)
committerMark Spencer <markster@digium.com>
Sun, 13 Apr 2003 04:17:45 +0000 (04:17 +0000)
git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@843 65c4cc65-6c06-0410-ace0-fbb531ad65f3

db.c
include/asterisk/astdb.h

diff --git a/db.c b/db.c
index cbecc4d..943d925 100755 (executable)
--- a/db.c
+++ b/db.c
@@ -294,7 +294,7 @@ static int database_show(int fd, int argc, char *argv[])
                        values = data.data;
                        values[data.size - 1]='\0';
                } else
-                       values = "<vad value>";
+                       values = "<bad value>";
                if (keymatch(keys, prefix)) {
                                ast_cli(fd, "%-50s: %-25s\n", keys, values);
                }
@@ -303,6 +303,74 @@ static int database_show(int fd, int argc, char *argv[])
        return RESULT_SUCCESS;  
 }
 
+struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
+{
+       char prefix[256];
+       DBT key, data;
+       char *keys, *values;
+       int res;
+       int pass;
+       struct ast_db_entry *last = NULL;
+       struct ast_db_entry *cur, *ret=NULL;
+
+       if (family && strlen(family)) {
+               if (keytree && strlen(keytree))
+                       /* Family and key tree */
+                       snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix);
+               else
+                       /* Family only */
+                       snprintf(prefix, sizeof(prefix), "/%s", family);
+       } else
+               strcpy(prefix, "");
+       ast_pthread_mutex_lock(&dblock);
+       if (dbinit()) {
+               ast_pthread_mutex_unlock(&dblock);
+               ast_log(LOG_WARNING, "Database unavailable\n");
+               return NULL;    
+       }
+       memset(&key, 0, sizeof(key));
+       memset(&data, 0, sizeof(data));
+       pass = 0;
+       while(!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
+               if (key.size) {
+                       keys = key.data;
+                       keys[key.size - 1] = '\0';
+               } else
+                       keys = "<bad key>";
+               if (data.size) {
+                       values = data.data;
+                       values[data.size - 1]='\0';
+               } else
+                       values = "<bad value>";
+               if (keymatch(keys, prefix)) {
+                               cur = malloc(sizeof(struct ast_db_entry) + strlen(keys) + strlen(values) + 2);
+                               if (cur) {
+                                       cur->next = NULL;
+                                       cur->key = cur->data + strlen(values) + 1;
+                                       strcpy(cur->data, values);
+                                       strcpy(cur->key, keys);
+                                       if (last)
+                                               last->next = cur;
+                                       else
+                                               ret = cur;
+                                       last = cur;
+                               }
+               }
+       }
+       ast_pthread_mutex_unlock(&dblock);
+       return ret;     
+}
+
+void ast_db_freetree(struct ast_db_entry *dbe)
+{
+       struct ast_db_entry *last;
+       while(dbe) {
+               last = dbe;
+               dbe = dbe->next;
+               free(last);
+       }
+}
+
 static char database_show_usage[] =
 "Usage: database show [family [keytree]]\n"
 "       Shows Asterisk database contents, optionally restricted\n"
index f40acf2..547ffed 100755 (executable)
 extern "C" {
 #endif
 
+struct ast_db_entry {
+       struct ast_db_entry *next;
+       char *key;
+       char data[0];
+};
+
 int ast_db_get(const char *family, const char *key, char *out, int outlen);
 
 int ast_db_put(const char *family, const char *key, char *value);
@@ -26,6 +32,10 @@ int ast_db_del(const char *family, const char *key);
 
 int ast_db_deltree(const char *family, const char *keytree);
 
+struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree);
+
+void ast_db_freetree(struct ast_db_entry *entry);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif