dect
/
asterisk
Archived
13
0
Fork 0

formatting cleanup (bug #4752)

git-svn-id: http://svn.digium.com/svn/asterisk/trunk@6170 f38db490-d61c-443f-a65b-d21fe96a405b
This commit is contained in:
kpfleming 2005-07-20 00:09:39 +00:00
parent ef7ebdb3e8
commit a246ce557a
1 changed files with 69 additions and 50 deletions

119
db.c
View File

@ -64,8 +64,7 @@ static inline int keymatch(const char *key, const char *prefix)
return 1;
if (!strcasecmp(key, prefix))
return 1;
if ((strlen(key) > preflen) &&
!strncasecmp(key, prefix, preflen)) {
if ((strlen(key) > preflen) && !strncasecmp(key, prefix, preflen)) {
if (key[preflen] == '/')
return 1;
}
@ -94,14 +93,16 @@ int ast_db_deltree(const char *family, const char *keytree)
int pass;
if (family) {
if (keytree)
if (keytree) {
snprintf(prefix, sizeof(prefix), "/%s/%s", family, keytree);
else
} else {
snprintf(prefix, sizeof(prefix), "/%s", family);
} else if (keytree)
}
} else if (keytree) {
return -1;
else
} else {
prefix[0] = '\0';
}
ast_mutex_lock(&dblock);
if (dbinit())
@ -110,12 +111,13 @@ int ast_db_deltree(const char *family, const char *keytree)
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
pass = 0;
while(!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
if (key.size) {
keys = key.data;
keys[key.size - 1] = '\0';
} else
} else {
keys = "<bad key>";
}
if (keymatch(keys, prefix)) {
astdb->del(astdb, &key, 0);
}
@ -154,7 +156,7 @@ int ast_db_put(const char *family, const char *keys, char *value)
int ast_db_get(const char *family, const char *keys, char *value, int valuelen)
{
char fullkey[256]="";
char fullkey[256] = "";
DBT key, data;
int res, fullkeylen;
@ -227,10 +229,11 @@ static int database_put(int fd, int argc, char *argv[])
if (argc != 5)
return RESULT_SHOWUSAGE;
res = ast_db_put(argv[2], argv[3], argv[4]);
if (res)
if (res) {
ast_cli(fd, "Failed to update entry\n");
else
} else {
ast_cli(fd, "Updated database successfully\n");
}
return RESULT_SUCCESS;
}
@ -241,10 +244,11 @@ static int database_get(int fd, int argc, char *argv[])
if (argc != 4)
return RESULT_SHOWUSAGE;
res = ast_db_get(argv[2], argv[3], tmp, sizeof(tmp));
if (res)
if (res) {
ast_cli(fd, "Database entry not found.\n");
else
} else {
ast_cli(fd, "Value: %s\n", tmp);
}
return RESULT_SUCCESS;
}
@ -254,10 +258,11 @@ static int database_del(int fd, int argc, char *argv[])
if (argc != 4)
return RESULT_SHOWUSAGE;
res = ast_db_del(argv[2], argv[3]);
if (res)
if (res) {
ast_cli(fd, "Database entry does not exist.\n");
else
} else {
ast_cli(fd, "Database entry removed.\n");
}
return RESULT_SUCCESS;
}
@ -266,14 +271,16 @@ static int database_deltree(int fd, int argc, char *argv[])
int res;
if ((argc < 3) || (argc > 4))
return RESULT_SHOWUSAGE;
if (argc == 4)
if (argc == 4) {
res = ast_db_deltree(argv[2], argv[3]);
else
} else {
res = ast_db_deltree(argv[2], NULL);
if (res)
}
if (res) {
ast_cli(fd, "Database entries do not exist.\n");
else
} else {
ast_cli(fd, "Database entries removed.\n");
}
return RESULT_SUCCESS;
}
@ -294,8 +301,9 @@ static int database_show(int fd, int argc, char *argv[])
} else if (argc == 2) {
/* Neither */
prefix[0] = '\0';
} else
} else {
return RESULT_SHOWUSAGE;
}
ast_mutex_lock(&dblock);
if (dbinit()) {
ast_mutex_unlock(&dblock);
@ -305,17 +313,19 @@ static int database_show(int fd, int argc, char *argv[])
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
pass = 0;
while(!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
if (key.size) {
keys = key.data;
keys[key.size - 1] = '\0';
} else
} else {
keys = "<bad key>";
}
if (data.size) {
values = data.data;
values[data.size - 1]='\0';
} else
} else {
values = "<bad value>";
}
if (keymatch(keys, prefix)) {
ast_cli(fd, "%-50s: %-25s\n", keys, values);
}
@ -335,8 +345,9 @@ static int database_showkey(int fd, int argc, char *argv[])
if (argc == 3) {
/* Key only */
snprintf(suffix, sizeof(suffix), "/%s", argv[2]);
} else
} else {
return RESULT_SHOWUSAGE;
}
ast_mutex_lock(&dblock);
if (dbinit()) {
ast_mutex_unlock(&dblock);
@ -346,17 +357,19 @@ static int database_showkey(int fd, int argc, char *argv[])
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
pass = 0;
while(!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
if (key.size) {
keys = key.data;
keys[key.size - 1] = '\0';
} else
} else {
keys = "<bad key>";
}
if (data.size) {
values = data.data;
values[data.size - 1]='\0';
} else
} else {
values = "<bad value>";
}
if (subkeymatch(keys, suffix)) {
ast_cli(fd, "%-50s: %-25s\n", keys, values);
}
@ -376,14 +389,16 @@ struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
struct ast_db_entry *cur, *ret=NULL;
if (family && !ast_strlen_zero(family)) {
if (keytree && !ast_strlen_zero(keytree))
if (keytree && !ast_strlen_zero(keytree)) {
/* Family and key tree */
snprintf(prefix, sizeof(prefix), "/%s/%s", family, prefix);
else
} else {
/* Family only */
snprintf(prefix, sizeof(prefix), "/%s", family);
} else
}
} else {
prefix[0] = '\0';
}
ast_mutex_lock(&dblock);
if (dbinit()) {
ast_mutex_unlock(&dblock);
@ -393,30 +408,33 @@ struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
pass = 0;
while(!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
while (!(res = astdb->seq(astdb, &key, &data, pass++ ? R_NEXT : R_FIRST))) {
if (key.size) {
keys = key.data;
keys[key.size - 1] = '\0';
} else
} else {
keys = "<bad key>";
}
if (data.size) {
values = data.data;
values[data.size - 1]='\0';
} else
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;
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_mutex_unlock(&dblock);
@ -426,7 +444,7 @@ struct ast_db_entry *ast_db_gettree(const char *family, const char *keytree)
void ast_db_freetree(struct ast_db_entry *dbe)
{
struct ast_db_entry *last;
while(dbe) {
while (dbe) {
last = dbe;
dbe = dbe->next;
free(last);
@ -501,10 +519,11 @@ static int manager_dbput(struct mansession *s, struct message *m)
}
res = ast_db_put(family, key, val);
if (res)
if (res) {
astman_send_error(s, m, "Failed to update entry");
else
} else {
astman_send_ack(s, m, "Updated database successfully");
}
return 0;
}
@ -525,9 +544,9 @@ static int manager_dbget(struct mansession *s, struct message *m)
}
res = ast_db_get(family, key, tmp, sizeof(tmp));
if (res)
if (res) {
astman_send_error(s, m, "Database entry not found");
else {
} else {
astman_send_ack(s, m, "Result will follow");
ast_cli(s->fd, "Event: DBGetResponse\r\n"
"Family: %s\r\n"