summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/lib/file.c4
-rw-r--r--src/lib/keytab.c8
-rw-r--r--src/lib/mdb.h2
-rw-r--r--src/main.c13
4 files changed, 21 insertions, 6 deletions
diff --git a/src/lib/file.c b/src/lib/file.c
index c5eabb2..34086e0 100644
--- a/src/lib/file.c
+++ b/src/lib/file.c
@@ -6,6 +6,9 @@
static char *getpair(int *c, FILE *fp);
+// readdb - reads the given file into a key table list
+// if fp returns NULL it will return the empty list
+// if readdb fails it will return NULL
struct keytablist *readdb(char *filename)
{
int len = 2;
@@ -48,6 +51,7 @@ struct keytablist *readdb(char *filename)
return list;
}
+// writedb - writes a keytablist to a given file
void writedb(char *filename, struct keytablist *list)
{
FILE *fp = fopen(filename, "wb");
diff --git a/src/lib/keytab.c b/src/lib/keytab.c
index 5d0c7dd..74dc47f 100644
--- a/src/lib/keytab.c
+++ b/src/lib/keytab.c
@@ -5,10 +5,9 @@
#include "mdb.h"
-static const struct keytablist empty;
-
static int hash(char *key);
+// getkeys - gets every single key in a key table
int *getkeys(struct keytablist *list, int id)
{
int len = 2;
@@ -24,6 +23,8 @@ int *getkeys(struct keytablist *list, int id)
return indexes;
}
+// getkey - gets a single key from a keytable
+// if it can't find the key it will return an empty table index
struct keytab getkey(struct keytablist *list, int id, char *key)
{
int idx = hash(key);
@@ -37,6 +38,8 @@ struct keytab getkey(struct keytablist *list, int id, char *key)
return list[id].tab[idx];
}
+// setkey - adds the given pair to the given key table
+// if setkey fails it will return 1 otherwise 0
int setkey(struct keytablist **list, int id, char *pair)
{
if (id >= (*list)[0].len) {
@@ -87,6 +90,7 @@ int setkey(struct keytablist **list, int id, char *pair)
return 0;
}
+// delkey - removes the given key from the given key table
void delkey(struct keytablist *list, int id, char *key)
{
int idx = hash(key);
diff --git a/src/lib/mdb.h b/src/lib/mdb.h
index aafacf0..7408dc7 100644
--- a/src/lib/mdb.h
+++ b/src/lib/mdb.h
@@ -20,11 +20,13 @@ struct keytablist {
struct keytab tab[TABLEN];
};
+// Table operations
int *getkeys(struct keytablist *list, int id);
struct keytab getkey(struct keytablist *list, int id, char *key);
int setkey(struct keytablist **list, int id, char *pair);
void delkey(struct keytablist *list, int id, char *key);
+// file operations
struct keytablist *readdb(char *filename);
void writedb(char *filename, struct keytablist *list);
diff --git a/src/main.c b/src/main.c
index 168c54a..040f8e2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -143,8 +143,13 @@ void setkeys(struct keytablist **list, int id, char **pairs, int plen)
void delkeys(struct keytablist *list, int id, char **keys, int klen)
{
- if (keys == NULL)
- return;
- for (int i = 0; i < klen; ++i)
- delkey(list, id, keys[i]);
+ if (keys == NULL) {
+ int *indexes = getkeys(list, id);
+ for (int i = 0; indexes[i]; ++i)
+ delkey(list, id, list[id].tab[indexes[i]].key);
+ free(indexes);
+ } else {
+ for (int i = 0; i < klen; ++i)
+ delkey(list, id, keys[i]);
+ }
}