diff options
author | Detlev Casanova <detlev.casanova@gmail.com> | 2010-07-29 13:43:26 +0200 |
---|---|---|
committer | Detlev Casanova <detlev.casanova@gmail.com> | 2010-07-29 13:43:26 +0200 |
commit | e4edeb3446e6e6cbe38eee061d6e1cc60aa4b094 (patch) | |
tree | 566c386ca4de9c233acd19d1bb9609a09fbaa5c7 /src/stringlist.c | |
download | c-portage-e4edeb3446e6e6cbe38eee061d6e1cc60aa4b094.tar.gz c-portage-e4edeb3446e6e6cbe38eee061d6e1cc60aa4b094.tar.bz2 c-portage-e4edeb3446e6e6cbe38eee061d6e1cc60aa4b094.zip |
Initial library commit
Diffstat (limited to 'src/stringlist.c')
-rw-r--r-- | src/stringlist.c | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/src/stringlist.c b/src/stringlist.c new file mode 100644 index 0000000..915cca4 --- /dev/null +++ b/src/stringlist.c @@ -0,0 +1,134 @@ +#include <Python.h> +#include <stdlib.h> +#include "stringlist.h" + +struct StringList +{ + char **list; + unsigned int count; +}; + +// Creates a String list to use with the library. +// len is the number of strings in the list. +StringList* stringListCreate(size_t len) +{ + StringList *ret = malloc(sizeof(StringList)); + ret->count = len; + ret->list = malloc(sizeof(char*) * len); + + return ret; +} + +/* + * Inserts the string str in the list l at position pos. + * Return True if it succeeded, False if not. + */ +int stringListInsertAt(StringList *l, unsigned int pos, char *str) +{ + if(!l || !l->list || l->count < pos) + return 0; + + l->list[pos] = str; + + return 1; +} + +/* + * Returns the number of strings in the list + */ +unsigned int stringListCount(StringList *l) +{ + if (!l) + return 0; + return l->count; +} + +/* + * Returns the String at position pos + */ +char* stringListGetAt(StringList *l, unsigned int pos) +{ + if (!l || !l->list || pos >= l->count) + return NULL; + + return l->list[pos]; +} + +/* + * Converts a Python list object to a C String list + */ +StringList* listToCList(PyObject* list) +{ + if (!list || !PyList_Check(list)) + return NULL; + + unsigned int len = PyList_Size(list); + StringList *ret = malloc(sizeof(StringList)); + ret->count = len; + ret->list = malloc(sizeof(char*) * len); + + for (unsigned int i = 0; i < len; i++) + { + //Item are copied so that the PyObject can be deleted after the call without + //destroying the data in the returned list. + PyObject *elem = PyList_GetItem(list, i); + ret->list[i] = malloc(sizeof(char) * (PyBytes_Size(elem) + 1)); + strcpy(ret->list[i], PyBytes_AsString(elem)); + } + + return ret; +} + +/* + * Converts a C String list to a Python List object + */ +PyObject* cListToPyList(StringList* list) +{ + if (!list) + Py_RETURN_NONE; + + PyObject *ret = PyList_New(list->count); + for(unsigned int i = 0; i < list->count; i++) + { + PyList_SetItem(ret, i, PyBytes_FromString(list->list[i])); + } + + return ret; +} + +/* + * Prints a C String list. + */ +void stringListPrint(StringList* list) +{ + if (!list) + return; + + for(unsigned int i = 0; i < list->count; i++) + { + printf("\"%s\"", list->list[i]); + // No coma after the last item. + if (i < list->count - 1) + printf(", "); + } +} + +/* + * Frees a string list and it's data + */ +void stringListFree(StringList* list) +{ + if (list && list->list) + { + for(unsigned int i = 0; i < list->count; i++) + { + free(list->list[i]); + } + + free(list->list); + } + + if (list) + free(list); +} + |