diff options
author | Detlev Casanova <detlev.casanova@gmail.com> | 2010-07-09 15:31:23 +0200 |
---|---|---|
committer | Detlev Casanova <detlev.casanova@gmail.com> | 2010-07-09 15:31:23 +0200 |
commit | 650c79cb75b4b09257ce0e489c5e20707d8758e0 (patch) | |
tree | ac82519281043d6bbbf843aee9e99d81ee9b037d /src/dict.c | |
parent | 593766766d7d892b469acda29bdb8d848a68dd3b (diff) | |
download | layman-650c79cb75b4b09257ce0e489c5e20707d8758e0.tar.gz layman-650c79cb75b4b09257ce0e489c5e20707d8758e0.tar.bz2 layman-650c79cb75b4b09257ce0e489c5e20707d8758e0.zip |
Add a C <-> Python Dict class and fix DbBase to take a Dict fo it's
config argument
Diffstat (limited to 'src/dict.c')
-rw-r--r-- | src/dict.c | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/dict.c b/src/dict.c new file mode 100644 index 0000000..a818a69 --- /dev/null +++ b/src/dict.c @@ -0,0 +1,80 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.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, int deref) +{ + if (!list) + return; + + DictElem *node = list->root; + while (node) + { + DictElem *tmp = node; + node = node->next; + /*if (deref) + { + Py_DECREF(tmp->object); + }*/ + 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; +} |