diff options
Diffstat (limited to 'src/include')
-rw-r--r-- | src/include/engine/delkeys.c | 174 | ||||
-rw-r--r-- | src/include/engine/engine.h | 23 | ||||
-rw-r--r-- | src/include/engine/file.c | 273 | ||||
-rw-r--r-- | src/include/engine/getkeys.c | 162 | ||||
-rw-r--r-- | src/include/engine/setkeys.c | 232 | ||||
-rw-r--r-- | src/include/engine/utils.c | 111 | ||||
-rw-r--r-- | src/include/engine/utils.h | 2 | ||||
-rw-r--r-- | src/include/xml/decode.c | 44 | ||||
-rw-r--r-- | src/include/xml/encode.c | 79 | ||||
-rw-r--r-- | src/include/xml/free.c | 8 | ||||
-rw-r--r-- | src/include/xml/xml.h | 19 |
11 files changed, 553 insertions, 574 deletions
diff --git a/src/include/engine/delkeys.c b/src/include/engine/delkeys.c index ad9c228..70e668c 100644 --- a/src/include/engine/delkeys.c +++ b/src/include/engine/delkeys.c @@ -1,111 +1,109 @@ -#include <threads.h> #include <stdlib.h> #include <string.h> +#include <threads.h> #include "engine.h" #include "utils.h" static int delkey_helper(void *thr_data); static int delkey(tablist_t *, int, char *); -static struct params *pass(mtx_t *mtx, tablist_t *list, char **keys, int len, int id); +static struct params *pass(mtx_t *mtx, tablist_t *list, char **keys, int len, + int id); struct params { - mtx_t *mtx; - tablist_t *copy; - char **keys; - int len; - int id; + mtx_t *mtx; + tablist_t *copy; + char **keys; + int len; + int id; }; +int delkeys(tablist_t *list, int id, char **keys, int len) { + mtx_t mtx; + if (id >= list[0].len || id < -1 || len < 0) + return -1; + if (mtx_init(&mtx, mtx_plain) != thrd_success) + return -2; + int rc = 0; + tablist_t *copy = calloc(list[0].len, sizeof(tablist_t)); + copytab(copy, list); -int delkeys(tablist_t *list, int id, char **keys, int len) -{ - mtx_t mtx; - if (id >= list[0].len || id < -1 || len < 0) - return -1; - if (mtx_init(&mtx, mtx_plain) != thrd_success) - return -2; - int rc = 0; - tablist_t *copy = calloc(list[0].len, sizeof(tablist_t)); - copytab(copy, list); - - if (id == -1) { - thrd_t *thrds = calloc(list[0].len, sizeof(thrd_t)); - for (int i = 0; i < copy[0].len; ++i) - thrd_create(&thrds[i], delkey_helper, pass(&mtx, copy, keys, len, i)); - for (int i = 0; i < copy[0].len; ++i) { - if (rc) - thrd_join(thrds[i], NULL); - else - thrd_join(thrds[i], &rc); - } - free(thrds); - } else - rc = delkey_helper(pass(&mtx, copy, keys, len, id)); + if (id == -1) { + thrd_t *thrds = calloc(list[0].len, sizeof(thrd_t)); + for (int i = 0; i < copy[0].len; ++i) + thrd_create(&thrds[i], delkey_helper, + pass(&mtx, copy, keys, len, i)); + for (int i = 0; i < copy[0].len; ++i) { + if (rc) + thrd_join(thrds[i], NULL); + else + thrd_join(thrds[i], &rc); + } + free(thrds); + } else + rc = delkey_helper(pass(&mtx, copy, keys, len, id)); - if (!rc) { - dellist(list); - memmove(list, copy, copy[0].len * sizeof(tablist_t)); - } else - dellist(copy); - mtx_destroy(&mtx); - free(copy); - return rc; + if (!rc) { + dellist(list); + memmove(list, copy, copy[0].len * sizeof(tablist_t)); + } else + dellist(copy); + mtx_destroy(&mtx); + free(copy); + return rc; } -static int delkey_helper(void *thr_data) -{ - int rc = 0; - struct params *p = (struct params *) thr_data; +static int delkey_helper(void *thr_data) { + int rc = 0; + struct params *p = (struct params *)thr_data; - mtx_lock(p->mtx); - if (p->len > 0 && p->keys != NULL) - for (int i = 0; i < p->len; ++i) { - if (p->keys[i] == NULL) { - mtx_unlock(p->mtx); - free(p); - return -3; - } - rc = delkey(p->copy, p->id, p->keys[i]); - } - else { - tablist_t *indexes = getkeys(p->copy, p->id, NULL, 0); - for (int i = 0; indexes[0].tab[i].flag; ++i) - rc = delkey(p->copy, p->id, indexes[0].tab[i].key); - free(indexes); - } - mtx_unlock(p->mtx); + mtx_lock(p->mtx); + if (p->len > 0 && p->keys != NULL) + for (int i = 0; i < p->len; ++i) { + if (p->keys[i] == NULL) { + mtx_unlock(p->mtx); + free(p); + return -3; + } + rc = delkey(p->copy, p->id, p->keys[i]); + } + else { + tablist_t *indexes = getkeys(p->copy, p->id, NULL, 0); + for (int i = 0; indexes[0].tab[i].flag; ++i) + rc = delkey(p->copy, p->id, indexes[0].tab[i].key); + free(indexes); + } + mtx_unlock(p->mtx); - free(p); - return rc; + free(p); + return rc; } -static int delkey(tablist_t *list, int id, char *key) -{ - int idx = hash(key); - if (list[id].tab[idx].key == NULL) - return 1; - while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) - idx++; - if (idx >= TABLEN) - return 2; - free(list[id].tab[idx].key); - list[id].tab[idx].key = NULL; - if (list[id].tab[idx].flag == 3) { - free(list[id].tab[idx].value.str); - list[id].tab[idx].value.str = NULL; - } - list[id].tab[idx].flag = 0; - return 0; +static int delkey(tablist_t *list, int id, char *key) { + int idx = hash(key); + if (list[id].tab[idx].key == NULL) + return 1; + while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) + idx++; + if (idx >= TABLEN) + return 2; + free(list[id].tab[idx].key); + list[id].tab[idx].key = NULL; + if (list[id].tab[idx].flag == 3) { + free(list[id].tab[idx].value.str); + list[id].tab[idx].value.str = NULL; + } + list[id].tab[idx].flag = 0; + return 0; } -static struct params *pass(mtx_t *mtx, tablist_t *list, char **keys, int len, int id) -{ - struct params *p = calloc(1, sizeof(struct params)); - p->mtx = mtx; - p->copy = list; - p->keys = keys; - p->len = len; - p->id = id; - return p; +static struct params *pass(mtx_t *mtx, tablist_t *list, char **keys, int len, + int id) { + struct params *p = calloc(1, sizeof(struct params)); + p->mtx = mtx; + p->copy = list; + p->keys = keys; + p->len = len; + p->id = id; + return p; } diff --git a/src/include/engine/engine.h b/src/include/engine/engine.h index d233bdd..5d9a3ea 100644 --- a/src/include/engine/engine.h +++ b/src/include/engine/engine.h @@ -4,18 +4,18 @@ #define TABLEN 1024 typedef struct { - char *key; - int flag; - union { - char *str; - double num; - unsigned int boolean : 1; - } value; + char *key; + int flag; + union { + char *str; + double num; + unsigned int boolean : 1; + } value; } tabidx_t; typedef struct { - int len; - tabidx_t tab[TABLEN]; + int len; + tabidx_t tab[TABLEN]; } tablist_t; /* getkeys: gets the provided keys from the provided id, @@ -32,6 +32,11 @@ int setkeys(tablist_t **list, int id, char **pairs, int len); * if keys is NULL it, will delete every key. */ int delkeys(tablist_t *list, int id, char **keys, int len); +// TODO: make readdb and writedb rely on helpers +// to make them both more private + +// TODO: reimplement binary formatting, again + /* readdb: reads the provided db file, * if the filename is NULL, it will return an empty table list, * if the file format is invalid, it will return NULL. */ diff --git a/src/include/engine/file.c b/src/include/engine/file.c index 13d6d3e..ab77010 100644 --- a/src/include/engine/file.c +++ b/src/include/engine/file.c @@ -11,166 +11,157 @@ static long getsz(FILE *fp); static int checkmark(char *buf, int pos); static char *readidx(char *buf, int *i, char ***known, int *len, int *pos); -tablist_t *readdb(char *filename) -{ - FILE *fp; - int len = 2; - tablist_t *list = calloc(len, sizeof(tablist_t)); - list[0].len = len; - if (filename == NULL || (fp = fopen(filename, "rb")) == NULL) - return list; - long sz = getsz(fp); - char *buf = calloc(sz, sizeof(char)); - fread(buf, sizeof(char), sz, fp); - fclose(fp); +tablist_t *readdb(char *filename) { + FILE *fp; + int len = 2; + tablist_t *list = calloc(len, sizeof(tablist_t)); + list[0].len = len; + if (filename == NULL || (fp = fopen(filename, "rb")) == NULL) + return list; + long sz = getsz(fp); + char *buf = calloc(sz, sizeof(char)); + fread(buf, sizeof(char), sz, fp); + fclose(fp); - int tlen, vlen, t = 0, v = 0, l = -1; - char **ktags = calloc((tlen = 2), sizeof(char *)); - char **kvalues = calloc((vlen = 2), sizeof (char *)); - char *key, *value; - for (int i = 0; i < sz; ++i) { - if (buf[i] == ':') { - ++i; - value = readidx(buf, &i, &kvalues, &vlen, &v); - char *pair = calloc(strlen(key) + strlen(value) + 2, sizeof(char)); - sprintf(pair, "%s:%s", key, value); - setkeys(&list, l, &pair, 1); - free(pair); - key = value = NULL; - } - else { - key = readidx(buf, &i, &ktags, &tlen, &t); - if (!strcmp(key, "document")) - ++l; - } - } - freeknown(&ktags, t); - freeknown(&kvalues, v); - free(buf); - return list; + int tlen, vlen, t = 0, v = 0, l = -1; + char **ktags = calloc((tlen = 2), sizeof(char *)); + char **kvalues = calloc((vlen = 2), sizeof(char *)); + char *key, *value; + for (int i = 0; i < sz; ++i) { + if (buf[i] == ':') { + ++i; + value = readidx(buf, &i, &kvalues, &vlen, &v); + char *pair = calloc(strlen(key) + strlen(value) + 2, sizeof(char)); + sprintf(pair, "%s:%s", key, value); + setkeys(&list, l, &pair, 1); + free(pair); + key = value = NULL; + } else { + key = readidx(buf, &i, &ktags, &tlen, &t); + if (!strcmp(key, "document")) + ++l; + } + } + freeknown(&ktags, t); + freeknown(&kvalues, v); + free(buf); + return list; } -static char *readidx(char *buf, int *i, char ***known, int *len, int *pos) -{ - char *r = NULL; - if (checkmark(buf, *i)) { - if (*len >= *pos) { - *len *= 2; - *known = realloc(*known, *len * sizeof(char *)); - } - *i += 4; - int nlen; - memcpy(&nlen, buf + *i, sizeof(int)); - *i += sizeof(int); - (*known)[*pos] = calloc(nlen + 1, sizeof(char)); - strncpy((*known)[(*pos)++], buf + *i, nlen); - r = (*known)[(*pos) - 1]; - *i += nlen - 1; - } else { - int idx; - memcpy(&idx, buf + *i, sizeof(int)); - *i += sizeof(int) - 1; - if (idx > *pos) - return NULL; - r = (*known)[idx]; - } - return r; +static char *readidx(char *buf, int *i, char ***known, int *len, int *pos) { + char *r = NULL; + if (checkmark(buf, *i)) { + if (*len >= *pos) { + *len *= 2; + *known = realloc(*known, *len * sizeof(char *)); + } + *i += 4; + int nlen; + memcpy(&nlen, buf + *i, sizeof(int)); + *i += sizeof(int); + (*known)[*pos] = calloc(nlen + 1, sizeof(char)); + strncpy((*known)[(*pos)++], buf + *i, nlen); + r = (*known)[(*pos) - 1]; + *i += nlen - 1; + } else { + int idx; + memcpy(&idx, buf + *i, sizeof(int)); + *i += sizeof(int) - 1; + if (idx > *pos) + return NULL; + r = (*known)[idx]; + } + return r; } -static int checkmark(char *buf, int pos) -{ - int marker = 0xffffffff; - return !memcmp(buf + pos, &marker, sizeof(int)); +static int checkmark(char *buf, int pos) { + int marker = 0xffffffff; + return !memcmp(buf + pos, &marker, sizeof(int)); } -static long getsz(FILE *fp) -{ - fseek(fp, 0, SEEK_END); - long sz = ftell(fp); - fseek(fp, 0, SEEK_SET); - return sz; +static long getsz(FILE *fp) { + fseek(fp, 0, SEEK_END); + long sz = ftell(fp); + fseek(fp, 0, SEEK_SET); + return sz; } static void newtag(char *tag, FILE *fp); static void writeidx(char ***known, int *len, int *pos, char *str, FILE *fp); -void writedb(char *filename, tablist_t *list) -{ - FILE *fp = fopen(filename, "wb"); - if (fp == NULL) - return; - int tlen, vlen, t = 0, v = 0; - char **ktags = calloc((tlen = 2), sizeof(char *)); - writeidx(&ktags, &tlen, &t, "documents", fp); - writeidx(&ktags, &tlen, &t, "document", fp); +void writedb(char *filename, tablist_t *list) { + FILE *fp = fopen(filename, "wb"); + if (fp == NULL) + return; + int tlen, vlen, t = 0, v = 0; + char **ktags = calloc((tlen = 2), sizeof(char *)); + writeidx(&ktags, &tlen, &t, "documents", fp); + writeidx(&ktags, &tlen, &t, "document", fp); - char buf[64]; - char **kvalues = calloc((vlen = 2), sizeof(char *)); - tablist_t *indexes = getkeys(list, -1, NULL, 0); - for (int i = 0; i < indexes[0].len; ++i) { - if (i > 0) - writeidx(&ktags, &tlen, &t, "document", fp); - for (int j = 0; indexes[i].tab[j].flag; ++j) { - writeidx(&ktags, &tlen, &t, indexes[i].tab[j].key, fp); - fputc(':', fp); - switch (indexes[i].tab[j].flag) { - case 1: - snprintf(buf, 64, "%g", indexes[i].tab[j].value.num); - writeidx(&kvalues, &vlen, &v, buf, fp); - break; - case 2: - writeidx(&kvalues, &vlen, &v, - indexes[i].tab[j].value.boolean ? "true" : "false", fp); - break; - case 3: - writeidx(&kvalues, &vlen, &v, indexes[i].tab[j].value.str, fp); - break; - } - } - } - freeknown(&ktags, t); - freeknown(&kvalues, v); - free(indexes); - fclose(fp); + char buf[64]; + char **kvalues = calloc((vlen = 2), sizeof(char *)); + tablist_t *indexes = getkeys(list, -1, NULL, 0); + for (int i = 0; i < indexes[0].len; ++i) { + if (i > 0) + writeidx(&ktags, &tlen, &t, "document", fp); + for (int j = 0; indexes[i].tab[j].flag; ++j) { + writeidx(&ktags, &tlen, &t, indexes[i].tab[j].key, fp); + fputc(':', fp); + switch (indexes[i].tab[j].flag) { + case 1: + snprintf(buf, 64, "%g", indexes[i].tab[j].value.num); + writeidx(&kvalues, &vlen, &v, buf, fp); + break; + case 2: + writeidx(&kvalues, &vlen, &v, + indexes[i].tab[j].value.boolean ? "true" : "false", + fp); + break; + case 3: + writeidx(&kvalues, &vlen, &v, indexes[i].tab[j].value.str, fp); + break; + } + } + } + freeknown(&ktags, t); + freeknown(&kvalues, v); + free(indexes); + fclose(fp); } -static void newtag(char *tag, FILE *fp) -{ - int len = strlen(tag); - int marker = 0xffffffff; - fwrite(&marker, sizeof(int), 1, fp); - fwrite(&len, sizeof(int), 1, fp); - fwrite(tag, sizeof(char), len, fp); +static void newtag(char *tag, FILE *fp) { + int len = strlen(tag); + int marker = 0xffffffff; + fwrite(&marker, sizeof(int), 1, fp); + fwrite(&len, sizeof(int), 1, fp); + fwrite(tag, sizeof(char), len, fp); } -static void writeidx(char ***known, int *len, int *pos, char *str, FILE *fp) -{ - int n; - if ((n = check(*known, *pos, str)) >= 0) - fwrite(&n, sizeof(int), 1, fp); - else { - if (*pos >= *len) { - *len *= 2; - *known = realloc(*known, *len * sizeof(char *)); - } - (*known)[*pos] = calloc(strlen(str) + 1, sizeof(char)); - strcpy((*known)[*pos], str); - newtag((*known)[(*pos)++], fp); - } +static void writeidx(char ***known, int *len, int *pos, char *str, FILE *fp) { + int n; + if ((n = check(*known, *pos, str)) >= 0) + fwrite(&n, sizeof(int), 1, fp); + else { + if (*pos >= *len) { + *len *= 2; + *known = realloc(*known, *len * sizeof(char *)); + } + (*known)[*pos] = calloc(strlen(str) + 1, sizeof(char)); + strcpy((*known)[*pos], str); + newtag((*known)[(*pos)++], fp); + } } -static void freeknown(char ***known, int pos) -{ - for (int i = 0; i < pos; ++i) - free((*known)[i]); - free(*known); +static void freeknown(char ***known, int pos) { + for (int i = 0; i < pos; ++i) + free((*known)[i]); + free(*known); } -static int check(char **known, int len, char *str) -{ - for (int i = 0; i < len; ++i) { - if (!strcmp(str, known[i])) - return i; - } - return -1; +static int check(char **known, int len, char *str) { + for (int i = 0; i < len; ++i) { + if (!strcmp(str, known[i])) + return i; + } + return -1; } diff --git a/src/include/engine/getkeys.c b/src/include/engine/getkeys.c index ae2eb73..3193e6b 100644 --- a/src/include/engine/getkeys.c +++ b/src/include/engine/getkeys.c @@ -1,6 +1,6 @@ +#include <ctype.h> #include <stdlib.h> #include <string.h> -#include <ctype.h> #include <threads.h> #include "engine.h" @@ -11,96 +11,96 @@ static tabidx_t getkey(tablist_t *list, int id, char *key); static int getkeys_helper(void *data); struct params { - mtx_t *mtx; - tablist_t *list; - tablist_t *ret; - char **keys; - int len; - int lid; - int pid; + mtx_t *mtx; + tablist_t *list; + tablist_t *ret; + char **keys; + int len; + int lid; + int pid; }; -static struct params *pass(mtx_t *mtx, tablist_t *list, tablist_t *ret, char **keys, int len, int lid, int pid); +static struct params *pass(mtx_t *mtx, tablist_t *list, tablist_t *ret, + char **keys, int len, int lid, int pid); -tablist_t *getkeys(tablist_t *list, int id, char **keys, int klen) -{ - mtx_t mtx; - if (id >= list[0].len || id < -1 || mtx_init(&mtx, mtx_plain) != thrd_success) - return NULL; - int rc = 0; - int len = id == -1 ? list[0].len : 1; - tablist_t *indexes = calloc(len, sizeof(tablist_t)); - indexes[0].len = len; - if (id >= 0) - rc = getkeys_helper(pass(&mtx, list, indexes, keys, klen, id, 0)); - else { - thrd_t *thrds = calloc(list[0].len, sizeof(thrd_t)); - for (int i = 0; i < list[0].len; ++i) - thrd_create(&thrds[i], getkeys_helper, pass(&mtx, list, indexes, keys, klen, i, i)); - for (int i = 0; i < list[0].len; ++i) { - if (rc) - thrd_join(thrds[i], NULL); - else - thrd_join(thrds[i], &rc); - } - free(thrds); - } +tablist_t *getkeys(tablist_t *list, int id, char **keys, int klen) { + mtx_t mtx; + if (id >= list[0].len || id < -1 || + mtx_init(&mtx, mtx_plain) != thrd_success) + return NULL; + int rc = 0; + int len = id == -1 ? list[0].len : 1; + tablist_t *indexes = calloc(len, sizeof(tablist_t)); + indexes[0].len = len; + if (id >= 0) + rc = getkeys_helper(pass(&mtx, list, indexes, keys, klen, id, 0)); + else { + thrd_t *thrds = calloc(list[0].len, sizeof(thrd_t)); + for (int i = 0; i < list[0].len; ++i) + thrd_create(&thrds[i], getkeys_helper, + pass(&mtx, list, indexes, keys, klen, i, i)); + for (int i = 0; i < list[0].len; ++i) { + if (rc) + thrd_join(thrds[i], NULL); + else + thrd_join(thrds[i], &rc); + } + free(thrds); + } - mtx_destroy(&mtx); - if (!rc) - return indexes; - else { - free(indexes); - return NULL; - } + mtx_destroy(&mtx); + if (!rc) + return indexes; + else { + free(indexes); + return NULL; + } } -static int getkeys_helper(void *data) -{ - struct params *p = (struct params *) data; - int rc = 0; +static int getkeys_helper(void *data) { + struct params *p = (struct params *)data; + int rc = 0; - mtx_lock(p->mtx); - if (p->keys == NULL) { - for (int i = 0, j = 0; i < TABLEN; ++i) - if (p->list[p->lid].tab[i].flag) - p->ret[p->pid].tab[j++] = p->list[p->lid].tab[i]; - } else { - for (int i = 0, j = 0; i < p->len; ++i) { - tabidx_t idx = getkey(p->list, p->lid, p->keys[i]); - if (idx.flag == 0) - rc = 1; - else - p->ret[p->pid].tab[j++] = idx; - } - } - mtx_unlock(p->mtx); - free(p); - return rc; + mtx_lock(p->mtx); + if (p->keys == NULL) { + for (int i = 0, j = 0; i < TABLEN; ++i) + if (p->list[p->lid].tab[i].flag) + p->ret[p->pid].tab[j++] = p->list[p->lid].tab[i]; + } else { + for (int i = 0, j = 0; i < p->len; ++i) { + tabidx_t idx = getkey(p->list, p->lid, p->keys[i]); + if (idx.flag == 0) + rc = 1; + else + p->ret[p->pid].tab[j++] = idx; + } + } + mtx_unlock(p->mtx); + free(p); + return rc; } -static struct params *pass(mtx_t *mtx, tablist_t *list, tablist_t *ret, char **keys, int len, int lid, int pid) -{ - struct params *p = calloc(1, sizeof(struct params)); - p->mtx = mtx; - p->list = list; - p->ret = ret; - p->keys = keys; - p->len = len; - p->lid = lid; - p->pid = pid; - return p; +static struct params *pass(mtx_t *mtx, tablist_t *list, tablist_t *ret, + char **keys, int len, int lid, int pid) { + struct params *p = calloc(1, sizeof(struct params)); + p->mtx = mtx; + p->list = list; + p->ret = ret; + p->keys = keys; + p->len = len; + p->lid = lid; + p->pid = pid; + return p; } -static tabidx_t getkey(tablist_t *list, int id, char *key) -{ - int idx = hash(key); - if (list[id].tab[idx].key == NULL) - return (tabidx_t) { .key = NULL, .flag = 0, .value = { 0 } }; +static tabidx_t getkey(tablist_t *list, int id, char *key) { + int idx = hash(key); + if (list[id].tab[idx].key == NULL) + return (tabidx_t){.key = NULL, .flag = 0, .value = {0}}; - while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) - idx++; - if (idx >= TABLEN) - return (tabidx_t) { .key = NULL, .flag = 0, .value = { 0 } }; - return list[id].tab[idx]; + while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) + idx++; + if (idx >= TABLEN) + return (tabidx_t){.key = NULL, .flag = 0, .value = {0}}; + return list[id].tab[idx]; } diff --git a/src/include/engine/setkeys.c b/src/include/engine/setkeys.c index 34ef3d2..f8797e6 100644 --- a/src/include/engine/setkeys.c +++ b/src/include/engine/setkeys.c @@ -1,6 +1,6 @@ #include <stdio.h> -#include <string.h> #include <stdlib.h> +#include <string.h> #include <threads.h> #include "engine.h" @@ -11,138 +11,134 @@ static int setkey(tablist_t **, int, char *); static char **getkv(char *pair); struct params { - mtx_t *mtx; - tablist_t **copy; - char *pair; - int id; + mtx_t *mtx; + tablist_t **copy; + char *pair; + int id; }; static struct params *pass(mtx_t *mtx, tablist_t **copy, char *pair, int id); -int setkeys(tablist_t **list, int id, char **pairs, int len) -{ - mtx_t mtx; - if (id < -1 || pairs == NULL || len <= 0) - return -1; - if (mtx_init(&mtx, mtx_plain) != thrd_success) - return -2; - int rc = 0; - tablist_t *copy = calloc((*list)[0].len, sizeof(tablist_t)); - copytab(copy, *list); +int setkeys(tablist_t **list, int id, char **pairs, int len) { + mtx_t mtx; + if (id < -1 || pairs == NULL || len <= 0) + return -1; + if (mtx_init(&mtx, mtx_plain) != thrd_success) + return -2; + int rc = 0; + tablist_t *copy = calloc((*list)[0].len, sizeof(tablist_t)); + copytab(copy, *list); - for (int i = 0; i < len; ++i) { - char *pair = pairs[i]; - if (id == -1) { - thrd_t *thrds = calloc((*list)[0].len, sizeof(thrd_t)); - for (int i = 0; i < copy[0].len; ++i) - thrd_create(&thrds[i], setkey_helper, pass(&mtx, ©, pair, i)); - for (int i = 0; i < copy[0].len; ++i) { - if (rc) - thrd_join(thrds[i], NULL); - else - thrd_join(thrds[i], &rc); - } - free(thrds); - } - else - rc = setkey_helper(pass(&mtx, ©, pair, id)); - } + for (int i = 0; i < len; ++i) { + char *pair = pairs[i]; + if (id == -1) { + thrd_t *thrds = calloc((*list)[0].len, sizeof(thrd_t)); + for (int i = 0; i < copy[0].len; ++i) + thrd_create(&thrds[i], setkey_helper, + pass(&mtx, ©, pair, i)); + for (int i = 0; i < copy[0].len; ++i) { + if (rc) + thrd_join(thrds[i], NULL); + else + thrd_join(thrds[i], &rc); + } + free(thrds); + } else + rc = setkey_helper(pass(&mtx, ©, pair, id)); + } - if (!rc) { - if (copy[0].len > (*list)[0].len) - *list = realloc(*list, copy[0].len * sizeof(tablist_t)); - dellist(*list); - copytab(*list, copy); - } - mtx_destroy(&mtx); - dellist(copy); - free(copy); - return rc; + if (!rc) { + if (copy[0].len > (*list)[0].len) + *list = realloc(*list, copy[0].len * sizeof(tablist_t)); + dellist(*list); + copytab(*list, copy); + } + mtx_destroy(&mtx); + dellist(copy); + free(copy); + return rc; } -static int setkey_helper(void *thr_data) -{ - int rc; - struct params *p = (struct params *) thr_data; +static int setkey_helper(void *thr_data) { + int rc; + struct params *p = (struct params *)thr_data; - mtx_lock(p->mtx); - rc = setkey(p->copy, p->id, p->pair); - mtx_unlock(p->mtx); + mtx_lock(p->mtx); + rc = setkey(p->copy, p->id, p->pair); + mtx_unlock(p->mtx); - free(p); - return rc; + free(p); + return rc; } -static int setkey(tablist_t **list, int id, char *pair) -{ - if (pair == NULL) - return 1; - if (id >= (*list)[0].len) { - *list = realloc(*list, (id + 1) * sizeof(tablist_t)); - for (int i = (*list)[0].len; i <= id; ++i) { - for (int j = 0; j < TABLEN; ++j) - (*list)[i].tab[j] = (tabidx_t) { NULL, 0, { 0 } }; - } - (*list)[0].len = id + 1; - } - char **kv = getkv(pair); - if (kv == NULL) - return 2; +static int setkey(tablist_t **list, int id, char *pair) { + if (pair == NULL) + return 1; + if (id >= (*list)[0].len) { + *list = realloc(*list, (id + 1) * sizeof(tablist_t)); + for (int i = (*list)[0].len; i <= id; ++i) { + for (int j = 0; j < TABLEN; ++j) + (*list)[i].tab[j] = (tabidx_t){NULL, 0, {0}}; + } + (*list)[0].len = id + 1; + } + char **kv = getkv(pair); + if (kv == NULL) + return 2; - int idx = hash(kv[0]); - while ((*list)[id].tab[idx].key != NULL && - strcmp((*list)[id].tab[idx].key, kv[0]) && - idx < TABLEN) idx++; - if (idx >= TABLEN) - return 2; - if (!(*list)[id].tab[idx].key) - (*list)[id].tab[idx].key = kv[0]; - else { - free(kv[0]); - if ((*list)[id].tab[idx].flag == 3) - free((*list)[id].tab[idx].value.str); - } - char *end; - double num = strtod(kv[1], &end); - if (*end == '\0') { - (*list)[id].tab[idx].flag = 1; - (*list)[id].tab[idx].value.num = num; - } else if (!strcmp(kv[1], "true") || !strcmp(kv[1], "false")) { - (*list)[id].tab[idx].flag = 2; - (*list)[id].tab[idx].value.boolean = !strcmp(kv[1], "true"); - } else { - (*list)[id].tab[idx].flag = 3; - (*list)[id].tab[idx].value.str = calloc(strlen(kv[1]) + 1, sizeof(char)); - strcpy((*list)[id].tab[idx].value.str, kv[1]); - } - free(kv[1]); - free(kv); - return 0; + int idx = hash(kv[0]); + while ((*list)[id].tab[idx].key != NULL && + strcmp((*list)[id].tab[idx].key, kv[0]) && idx < TABLEN) + idx++; + if (idx >= TABLEN) + return 2; + if (!(*list)[id].tab[idx].key) + (*list)[id].tab[idx].key = kv[0]; + else { + free(kv[0]); + if ((*list)[id].tab[idx].flag == 3) + free((*list)[id].tab[idx].value.str); + } + char *end; + double num = strtod(kv[1], &end); + if (*end == '\0') { + (*list)[id].tab[idx].flag = 1; + (*list)[id].tab[idx].value.num = num; + } else if (!strcmp(kv[1], "true") || !strcmp(kv[1], "false")) { + (*list)[id].tab[idx].flag = 2; + (*list)[id].tab[idx].value.boolean = !strcmp(kv[1], "true"); + } else { + (*list)[id].tab[idx].flag = 3; + (*list)[id].tab[idx].value.str = + calloc(strlen(kv[1]) + 1, sizeof(char)); + strcpy((*list)[id].tab[idx].value.str, kv[1]); + } + free(kv[1]); + free(kv); + return 0; } -static char **getkv(char *pair) -{ - char **kv = calloc(2, sizeof(char *)); - int i = 0; - while (pair[i] != ':' && i < strlen(pair)) - i++; - if (i >= strlen(pair)) { - free(kv); - return NULL; - } - kv[0] = calloc(i + 1, sizeof(char)); - strncpy(kv[0], pair, i); - kv[1] = calloc(strlen(pair) - i, sizeof(char)); - strcpy(kv[1], pair + i + 1); - return kv; +static char **getkv(char *pair) { + char **kv = calloc(2, sizeof(char *)); + int i = 0; + while (pair[i] != ':' && i < strlen(pair)) + i++; + if (i >= strlen(pair)) { + free(kv); + return NULL; + } + kv[0] = calloc(i + 1, sizeof(char)); + strncpy(kv[0], pair, i); + kv[1] = calloc(strlen(pair) - i, sizeof(char)); + strcpy(kv[1], pair + i + 1); + return kv; } -static struct params *pass(mtx_t *mtx, tablist_t **copy, char *pair, int id) -{ - struct params *p = calloc(1, sizeof(struct params)); - p->mtx = mtx; - p->copy = copy; - p->pair = pair; - p->id = id; - return p; +static struct params *pass(mtx_t *mtx, tablist_t **copy, char *pair, int id) { + struct params *p = calloc(1, sizeof(struct params)); + p->mtx = mtx; + p->copy = copy; + p->pair = pair; + p->id = id; + return p; } diff --git a/src/include/engine/utils.c b/src/include/engine/utils.c index 909c123..f8ef7f0 100644 --- a/src/include/engine/utils.c +++ b/src/include/engine/utils.c @@ -5,67 +5,64 @@ static int delkey(tablist_t *, int, char *); -void dellist(tablist_t *list) -{ - for (int i = 0; i < list[0].len; ++i) { - tablist_t *indexes = getkeys(list, i, NULL, 0); - for (int j = 0; indexes[0].tab[j].flag; ++j) - delkey(list, i, indexes[0].tab[j].key); - free(indexes); - } +void dellist(tablist_t *list) { + for (int i = 0; i < list[0].len; ++i) { + tablist_t *indexes = getkeys(list, i, NULL, 0); + for (int j = 0; indexes[0].tab[j].flag; ++j) + delkey(list, i, indexes[0].tab[j].key); + free(indexes); + } } -int hash(char *key) -{ - unsigned long h = 5381; - for (int i = 0; i < strlen(key); ++i) - h = ((h << 5) + h) + key[i]; - return h % TABLEN; +int hash(char *key) { + unsigned long h = 5381; + for (int i = 0; i < strlen(key); ++i) + h = ((h << 5) + h) + key[i]; + return h % TABLEN; } -tablist_t *copytab(tablist_t *dst, tablist_t *src) -{ - if (dst == NULL) - return NULL; - dst[0].len = src[0].len; - for (int i = 0; i < src[0].len; ++i) { - for (int j = 0; j < TABLEN; ++j) { - dst[i].tab[j] = (tabidx_t) { NULL, 0, { 0 } }; - if (src[i].tab[j].flag) { - switch (src[i].tab[j].flag) { - case 3: - dst[i].tab[j].value.str = - calloc(strlen(src[i].tab[j].value.str) + 1, sizeof(char)); - strcpy(dst[i].tab[j].value.str, src[i].tab[j].value.str); - break; - default: - dst[i].tab[j].value = src[i].tab[j].value; - break; - } - dst[i].tab[j].flag = src[i].tab[j].flag; - dst[i].tab[j].key = calloc(strlen(src[i].tab[j].key) + 1, sizeof(char)); - strcpy(dst[i].tab[j].key, src[i].tab[j].key); - } - } - } - return dst; +tablist_t *copytab(tablist_t *dst, tablist_t *src) { + if (dst == NULL) + return NULL; + dst[0].len = src[0].len; + for (int i = 0; i < src[0].len; ++i) { + for (int j = 0; j < TABLEN; ++j) { + dst[i].tab[j] = (tabidx_t){NULL, 0, {0}}; + if (src[i].tab[j].flag) { + switch (src[i].tab[j].flag) { + case 3: + dst[i].tab[j].value.str = calloc( + strlen(src[i].tab[j].value.str) + 1, sizeof(char)); + strcpy(dst[i].tab[j].value.str, src[i].tab[j].value.str); + break; + default: + dst[i].tab[j].value = src[i].tab[j].value; + break; + } + dst[i].tab[j].flag = src[i].tab[j].flag; + dst[i].tab[j].key = + calloc(strlen(src[i].tab[j].key) + 1, sizeof(char)); + strcpy(dst[i].tab[j].key, src[i].tab[j].key); + } + } + } + return dst; } -static int delkey(tablist_t *list, int id, char *key) -{ - int idx = hash(key); - if (list[id].tab[idx].key == NULL) - return 1; - while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) - idx++; - if (idx >= TABLEN) - return 2; - free(list[id].tab[idx].key); - list[id].tab[idx].key = NULL; - if (list[id].tab[idx].flag == 3) { - free(list[id].tab[idx].value.str); - list[id].tab[idx].value.str = NULL; - } - list[id].tab[idx].flag = 0; - return 0; +static int delkey(tablist_t *list, int id, char *key) { + int idx = hash(key); + if (list[id].tab[idx].key == NULL) + return 1; + while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) + idx++; + if (idx >= TABLEN) + return 2; + free(list[id].tab[idx].key); + list[id].tab[idx].key = NULL; + if (list[id].tab[idx].flag == 3) { + free(list[id].tab[idx].value.str); + list[id].tab[idx].value.str = NULL; + } + list[id].tab[idx].flag = 0; + return 0; } diff --git a/src/include/engine/utils.h b/src/include/engine/utils.h index 54bfc17..fe6022f 100644 --- a/src/include/engine/utils.h +++ b/src/include/engine/utils.h @@ -6,7 +6,7 @@ // dellist: deletes the provided table void dellist(tablist_t *list); -/* hash: calculates the DJB2 hash of a string, +/* hash: calculates the DJB2 hash of a string, * and returns that mod TABLEN */ int hash(char *str); diff --git a/src/include/xml/decode.c b/src/include/xml/decode.c index 6a163bb..14af551 100644 --- a/src/include/xml/decode.c +++ b/src/include/xml/decode.c @@ -10,10 +10,11 @@ static int check_closing_tag(char *xml, int *pos, char *tag, int *err); static void set_tag(char *xml, map_t **map, int *closed, int *pos); static void set_value(char *xml, int *pos, map_t **map); -map_t *decode(char *xml, int *pos, int *len) -{ - if (*len) ++(*len); - else *len = 1; +map_t *decode(char *xml, int *pos, int *len) { + if (*len) + ++(*len); + else + *len = 1; map_t *decoded = calloc(1, sizeof(map_t)); int err = 0, closed = 1; @@ -28,10 +29,11 @@ map_t *decode(char *xml, int *pos, int *len) map_t *ndec = decode(xml, &i, &(decoded->n)); decoded->size = sizeof(map_t); if (decoded->n > 1) - decoded->payload = realloc(decoded->payload, decoded->n * sizeof(map_t)); + decoded->payload = + realloc(decoded->payload, decoded->n * sizeof(map_t)); else decoded->payload = calloc(1, sizeof(map_t)); - ((map_t *) decoded->payload)[decoded->n - 1] = *ndec; + ((map_t *)decoded->payload)[decoded->n - 1] = *ndec; free(ndec); } else set_value(xml, &i, &decoded); @@ -43,8 +45,7 @@ map_t *decode(char *xml, int *pos, int *len) return decoded; } -static char *get_tag(char *xml, int *pos) -{ +static char *get_tag(char *xml, int *pos) { int len, i; for (i = *pos, len = 0; xml[i] != '>' && i < strlen(xml); ++i, ++len) ; @@ -56,8 +57,7 @@ static char *get_tag(char *xml, int *pos) return title; } -static void set_value(char *xml, int *pos, map_t **map) -{ +static void set_value(char *xml, int *pos, map_t **map) { int len, i; for (i = *pos, len = 0; xml[i] != '<' && i < strlen(xml); ++i, ++len) ; @@ -71,8 +71,7 @@ static void set_value(char *xml, int *pos, map_t **map) *pos += len - 1; } -static attr_t *get_attrs(char *parsed, char **tag, int *n_attrs) -{ +static attr_t *get_attrs(char *parsed, char **tag, int *n_attrs) { attr_t *attrs; char *iter = strtok(parsed, " "); *tag = calloc(strlen(iter) + 1, sizeof(char)); @@ -84,7 +83,7 @@ static attr_t *get_attrs(char *parsed, char **tag, int *n_attrs) free(parsed); return NULL; } - + int pos = 0; do { if (!iter) @@ -92,14 +91,13 @@ static attr_t *get_attrs(char *parsed, char **tag, int *n_attrs) int i, len = strlen(iter); for (i = 0; i < len && iter[i] != '='; ++i) ; - if (i >= len || - (iter[i] == '=' && i >= len - 1) || + if (i >= len || (iter[i] == '=' && i >= len - 1) || (iter[i + 1] != '\'' && iter[i + 1] != '"')) { free(parsed); free(attrs); return NULL; } - + char *name = calloc(i + 1, sizeof(char)); char *value = calloc(len - i, sizeof(char)); strncpy(name, iter, i); @@ -110,23 +108,21 @@ static attr_t *get_attrs(char *parsed, char **tag, int *n_attrs) attrs = realloc(attrs, (*n_attrs *= 2) * sizeof(attr_t)); attrs[pos].id = name; attrs[pos++].value = value; - } while((iter = strtok(NULL, " "))); - + } while ((iter = strtok(NULL, " "))); + *n_attrs = pos; free(parsed); return attrs; } -static void set_tag(char *xml, map_t **map, int *closed, int *pos) -{ +static void set_tag(char *xml, map_t **map, int *closed, int *pos) { (*pos)++; - (*map)->attrs = get_attrs(get_tag(xml, pos), - &((*map)->tag), &((*map)->n_attrs)); + (*map)->attrs = + get_attrs(get_tag(xml, pos), &((*map)->tag), &((*map)->n_attrs)); *closed = 0; } -static int check_closing_tag(char *xml, int *pos, char *tag, int *err) -{ +static int check_closing_tag(char *xml, int *pos, char *tag, int *err) { if (!strncmp(xml + *pos, "</", 2)) { *pos += 2; char *tmp = get_tag(xml, pos); diff --git a/src/include/xml/encode.c b/src/include/xml/encode.c index 9e76c69..60e418d 100644 --- a/src/include/xml/encode.c +++ b/src/include/xml/encode.c @@ -1,52 +1,49 @@ #include <stdio.h> -#include <string.h> #include <stdlib.h> +#include <string.h> #include "xml.h" static void check(char **str, int *len, int *used, int n); -char *encode(map_t *map, int len) -{ - int slen = 128, used = 0; - char *str = calloc(slen, sizeof(char)); +char *encode(map_t *map, int len) { + int slen = 128, used = 0; + char *str = calloc(slen, sizeof(char)); - char buf[64]; - for (int i = 0; i < len; ++i) { - check(&str, &slen, &used, snprintf(buf, 64, "<%s", map[i].tag)); - strcat(str, buf); - if (map[i].attrs != NULL) { - for (int j = 0; j < map[i].n_attrs; ++j) { - check(&str, &slen, &used, - snprintf(buf, 64, " %s='%s'", - map[i].attrs[j].id, map[i].attrs[j].value)); - strcat(str, buf); - } - } - check(&str, &slen, &used, 1); - strcat(str, ">"); - if (map[i].size == sizeof(map_t)) { - char *rt = encode((map_t *) map[i].payload, map[i].n); - if (rt == NULL) - return NULL; - check(&str, &slen, &used, strlen(rt)); - strcat(str, rt); - free(rt); - } else if (map[i].size == sizeof(char)) { - check(&str, &slen, &used, map[i].n); - strcat(str, map[i].payload); - } - else - return NULL; - check(&str, &slen, &used, snprintf(buf, 64, "</%s>", map[i].tag)); - strcat(str, buf); - } - return str; + char buf[64]; + for (int i = 0; i < len; ++i) { + check(&str, &slen, &used, snprintf(buf, 64, "<%s", map[i].tag)); + strcat(str, buf); + if (map[i].attrs != NULL) { + for (int j = 0; j < map[i].n_attrs; ++j) { + check(&str, &slen, &used, + snprintf(buf, 64, " %s='%s'", map[i].attrs[j].id, + map[i].attrs[j].value)); + strcat(str, buf); + } + } + check(&str, &slen, &used, 1); + strcat(str, ">"); + if (map[i].size == sizeof(map_t)) { + char *rt = encode((map_t *)map[i].payload, map[i].n); + if (rt == NULL) + return NULL; + check(&str, &slen, &used, strlen(rt)); + strcat(str, rt); + free(rt); + } else if (map[i].size == sizeof(char)) { + check(&str, &slen, &used, map[i].n); + strcat(str, map[i].payload); + } else + return NULL; + check(&str, &slen, &used, snprintf(buf, 64, "</%s>", map[i].tag)); + strcat(str, buf); + } + return str; } -static void check(char **str, int *len, int *used, int n) -{ - if (*used + n >= *len) - *str = realloc(*str, (*len *= 2)); - *used += n; +static void check(char **str, int *len, int *used, int n) { + if (*used + n >= *len) + *str = realloc(*str, (*len *= 2)); + *used += n; } diff --git a/src/include/xml/free.c b/src/include/xml/free.c index bbf3369..b23b379 100644 --- a/src/include/xml/free.c +++ b/src/include/xml/free.c @@ -2,8 +2,7 @@ #include "xml.h" -void freemap(map_t *map) -{ +void freemap(map_t *map) { if (map->n_attrs) { for (int i = 0; i < map->n_attrs; ++i) { free(map->attrs[i].id); @@ -11,7 +10,7 @@ void freemap(map_t *map) } free(map->attrs); } - if(map->size == sizeof(map_t)) { + if (map->size == sizeof(map_t)) { for (int i = 0; i < map->n; ++i) { map_t *pl = (map_t *)map->payload; if (pl[i].n_attrs) { @@ -28,8 +27,7 @@ void freemap(map_t *map) free(pl[i].tag); } free(map->payload); - } - else + } else free(map->payload); free(map->tag); free(map); diff --git a/src/include/xml/xml.h b/src/include/xml/xml.h index ff64796..c812005 100644 --- a/src/include/xml/xml.h +++ b/src/include/xml/xml.h @@ -3,21 +3,22 @@ #include <stdio.h> - typedef struct { - char *id; - char *value; + char *id; + char *value; } attr_t; typedef struct { - char *tag; - void *payload; - size_t size; - int n; - attr_t *attrs; - int n_attrs; + char *tag; + void *payload; + size_t size; + int n; + attr_t *attrs; + int n_attrs; } map_t; +// TODO: make encode more reliant on helpers +// to make function more private /* decode: decodes the provided xml statement into a map_t */ map_t *decode(char *xml, int *pos, int *len); |