diff options
author | dol-sen <brian.dolbec@gmail.com> | 2011-10-05 13:50:03 -0700 |
---|---|---|
committer | dol-sen <brian.dolbec@gmail.com> | 2011-10-05 13:50:03 -0700 |
commit | 78eefa4b5a74690a537fc725b4de2224548a0826 (patch) | |
tree | 68787c852f3303a27f8c12f98e9f9f15bf8ab283 /c-layman/src/dict.c | |
parent | 2b3e8c634f819f1cbd3a700f6299c5a99b989ea5 (diff) | |
download | layman-78eefa4b5a74690a537fc725b4de2224548a0826.tar.gz layman-78eefa4b5a74690a537fc725b4de2224548a0826.tar.bz2 layman-78eefa4b5a74690a537fc725b4de2224548a0826.zip |
move everything to prep for a merge into layman.
Diffstat (limited to 'c-layman/src/dict.c')
-rw-r--r-- | c-layman/src/dict.c | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/c-layman/src/dict.c b/c-layman/src/dict.c new file mode 100644 index 0000000..29c1dea --- /dev/null +++ b/c-layman/src/dict.c @@ -0,0 +1,77 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "internal.h" +#include "dict.h" + +/* + * Dict + */ +typedef struct DictElem DictElem; +struct DictElem +{ + const char *key; + const char *val; + struct DictElem *next; +}; + +struct Dict +{ + DictElem *root; + int count; +}; + +Dict *dictCreate() +{ + Dict *ret = malloc(sizeof(Dict)); + ret->count = 0; + ret->root = 0; + return ret; +} + +void dictInsert(Dict* list, const char* key, const char* value) +{ + if (!list) + return; + DictElem *node = malloc(sizeof(DictElem)); + node->key = key; + node->val = value; + node->next = list->root; + list->root = node; + list->count++; +} + +unsigned int dictCount(Dict *list) +{ + return (list ? list->count : 0); +} + +void dictFree(Dict *list) +{ + if (!list) + return; + + DictElem *node = list->root; + while (node) + { + DictElem *tmp = node; + node = node->next; + free(tmp); + } + + free(list); +} + +PyObject *dictToPyDict(Dict *dict) +{ + PyObject *pydict = PyDict_New(); + DictElem *node = dict->root; + while (node) + { + PyDict_SetItem(pydict, PyBytes_FromString(node->key), PyBytes_FromString(node->val)); + node = node->next; + } + + return pydict; +} |