summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/include/keytab.h13
-rw-r--r--src/keytab.c44
-rw-r--r--src/main.c50
3 files changed, 67 insertions, 40 deletions
diff --git a/src/include/keytab.h b/src/include/keytab.h
index f3b8910..40db10f 100644
--- a/src/include/keytab.h
+++ b/src/include/keytab.h
@@ -15,9 +15,14 @@ struct keytab {
union value v;
};
-int *getkeys(struct keytab *tab);
-struct keytab getkey(struct keytab *tab, char *key);
-void setkey(struct keytab *tab, char *pair);
-void delkey(struct keytab *tab, char *key);
+struct keytablist {
+ struct keytab tab[TABLEN];
+};
+
+int *getkeys(struct keytablist *list, int id);
+struct keytab getkey(struct keytablist *list, int id, char *key);
+// TODO: rewrite setkey to take len as an argument
+void setkey(struct keytablist *list, int id, char *pair);
+void delkey(struct keytablist *list, int id, char *key);
#endif
diff --git a/src/keytab.c b/src/keytab.c
index 6186c9d..160e50a 100644
--- a/src/keytab.c
+++ b/src/keytab.c
@@ -7,7 +7,7 @@
static int hash(char *key);
-int *getkeys(struct keytab *tab)
+int *getkeys(struct keytablist *list, int id)
{
int len = 2;
int *indexes = calloc(len, sizeof(int));
@@ -16,26 +16,26 @@ int *getkeys(struct keytab *tab)
indexes = realloc(indexes, ++len * sizeof(int));
indexes[len - 1] = 0;
}
- if (tab[i].key)
+ if (list[id].tab[i].key)
indexes[j++] = i;
}
return indexes;
}
-struct keytab getkey(struct keytab *tab, char *key)
+struct keytab getkey(struct keytablist *list, int id, char *key)
{
int idx = hash(key);
- if (tab[idx].key == NULL)
- return (struct keytab) { .key = NULL, .v = 0 };
+ if (list[id].tab[idx].key == NULL)
+ return (struct keytab) { .key = NULL, .flag = 0, .v = 0 };
- while (strcmp(tab[idx].key, key) && idx < TABLEN)
+ while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN)
idx++;
if (idx >= TABLEN)
- return (struct keytab) { .key = NULL, .v = 0 };
- return tab[idx];
+ return (struct keytab) { .key = NULL, .flag = 0, .v = 0 };
+ return list[id].tab[idx];
}
-void setkey(struct keytab *tab, char *pair)
+void setkey(struct keytablist *list, int id, char *pair)
{
char *tok = strtok(pair, ":");
char *key = calloc(strlen(tok) + 1, sizeof(char));
@@ -61,36 +61,36 @@ void setkey(struct keytab *tab, char *pair)
}
int idx = hash(key);
- while (tab[idx].key != NULL && strcmp(tab[idx].key, key) && idx < TABLEN)
+ while (list[id].tab[idx].key != NULL && strcmp(list[id].tab[idx].key, key) && idx < TABLEN)
idx++;
if (idx >= TABLEN) {
fprintf(stderr, "No more room in table\n");
return;
}
- if (!tab[idx].key)
- tab[idx].key = key;
+ if (!list[id].tab[idx].key)
+ list[id].tab[idx].key = key;
else
free(key);
- tab[idx].v = v;
- tab[idx].flag = flag;
+ list[id].tab[idx].v = v;
+ list[id].tab[idx].flag = flag;
}
-void delkey(struct keytab *tab, char *key)
+void delkey(struct keytablist *list, int id, char *key)
{
int idx = hash(key);
- while (strcmp(tab[idx].key, key) && idx < TABLEN)
+ while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN)
idx++;
if (idx >= TABLEN) {
fprintf(stderr, "Invalid key: %s\n", key);
return;
}
- free(tab[idx].key);
- tab[idx].key = NULL;
- if (tab[idx].flag == 3) {
- free(tab[idx].v.str);
- tab[idx].v.str = NULL;
+ free(list[id].tab[idx].key);
+ list[id].tab[idx].key = NULL;
+ if (list[id].tab[idx].flag == 3) {
+ free(list[id].tab[idx].v.str);
+ list[id].tab[idx].v.str = NULL;
}
- tab[idx].flag = 0;
+ list[id].tab[idx].flag = 0;
}
static int hash(char *key)
diff --git a/src/main.c b/src/main.c
index 1aaaaa7..e8a3a08 100644
--- a/src/main.c
+++ b/src/main.c
@@ -8,7 +8,9 @@
int main(int argc, char **argv)
{
- struct keytab tab[TABLEN] = {{ NULL, 0, { 0 } }};
+ int open = 0;
+ int len = 2;
+ struct keytablist *list = calloc(len, sizeof(struct keytablist));
if (argc != 2)
exit(1);
char *filename = argv[1];
@@ -19,14 +21,26 @@ int main(int argc, char **argv)
struct byte *bytes = parse(buf);
free(buf);
- for (int i = 0; i < blen; ++i) {
+ for (int i = 0, j = 0; i < blen; ++i) {
switch (bytes[i].type) {
case BEGIN:
+ if (j >= len) {
+ len *= 2;
+ list = realloc(list, len * sizeof(struct keytablist));
+ }
+ if (open == 1) {
+ fprintf(stderr, "missing close!\n");
+ free(bytes);
+ exit(1);
+ }
+ open = 1;
break;
case END:
+ open = 0;
+ j++;
break;
case PAIR:
- setkey(tab, bytes[i].value);
+ setkey(list, j, bytes[i].value);
free(bytes[i].value);
break;
case ERROR:
@@ -35,19 +49,27 @@ int main(int argc, char **argv)
}
}
- int *indexes = getkeys(tab);
- for (int i = 0; indexes[i] != 0; ++i) {
- printf("%s: ", tab[indexes[i]].key);
- if (tab[indexes[i]].flag == 1) {
- printf("%.2lf\n", tab[indexes[i]].v.num);
- } else if (tab[indexes[i]].flag == 2) {
- printf("%d\n", tab[indexes[i]].v.b);
- } else if (tab[indexes[i]].flag == 3) {
- printf("%s\n", tab[indexes[i]].v.str);
+ for (int i = 0; i < len; ++i) {
+ int *indexes = getkeys(list, i);
+ for (int j = 0; indexes[j]; ++j) {
+ printf("%s: ", list[i].tab[indexes[j]].key);
+ switch (list[i].tab[indexes[j]].flag) {
+ case 1:
+ printf("%.2lf\n", list[i].tab[indexes[j]].v.num);
+ break;
+ case 2:
+ printf("%d\n", list[i].tab[indexes[j]].v.b);
+ break;
+ case 3:
+ printf("%s\n", list[i].tab[indexes[j]].v.str);
+ break;
+ }
+ delkey(list, i, list[i].tab[indexes[j]].key);
}
- delkey(tab, tab[indexes[i]].key);
+ free(indexes);
}
- free(indexes);
+
+ free(list);
free(bytes);
exit(0);
}