diff options
author | dol-sen <brian.dolbec@gmail.com> | 2011-10-08 13:24:50 -0700 |
---|---|---|
committer | dol-sen <brian.dolbec@gmail.com> | 2011-10-08 13:24:50 -0700 |
commit | cb8ca469d526aef973ae853a42263c89230e4f4b (patch) | |
tree | 78d949bb171b574121b79d3f7c6b780d72d69533 | |
parent | Merge remote-tracking branch 'c-layman/master' (diff) | |
download | layman-cb8ca469d526aef973ae853a42263c89230e4f4b.tar.gz layman-cb8ca469d526aef973ae853a42263c89230e4f4b.tar.bz2 layman-cb8ca469d526aef973ae853a42263c89230e4f4b.zip |
begin massive update and re-organization of the code.
-rw-r--r-- | c-layman/Makefile.am | 4 | ||||
-rw-r--r-- | c-layman/src/basic.h | 5 | ||||
-rw-r--r-- | c-layman/src/config.c | 133 | ||||
-rw-r--r-- | c-layman/src/config.h | 18 | ||||
-rw-r--r-- | c-layman/src/dict.c | 19 | ||||
-rw-r--r-- | c-layman/src/dict.h | 5 | ||||
-rw-r--r-- | c-layman/src/internal.h | 42 | ||||
-rw-r--r-- | c-layman/src/interpreter.c | 309 | ||||
-rw-r--r-- | c-layman/src/interpreter.h | 6 | ||||
-rw-r--r-- | c-layman/src/layman.c | 29 | ||||
-rw-r--r-- | c-layman/src/layman.h | 4 | ||||
-rw-r--r-- | c-layman/src/laymanapi.c | 214 | ||||
-rw-r--r-- | c-layman/src/laymanapi.h | 64 | ||||
-rw-r--r-- | c-layman/src/message.c | 111 | ||||
-rw-r--r-- | c-layman/src/message.h | 7 | ||||
-rw-r--r-- | c-layman/src/stringlist.c | 50 | ||||
-rw-r--r-- | c-layman/src/stringlist.h | 8 | ||||
-rw-r--r-- | c-layman/src/tester.c | 62 |
18 files changed, 756 insertions, 334 deletions
diff --git a/c-layman/Makefile.am b/c-layman/Makefile.am index c12c925..55a1847 100644 --- a/c-layman/Makefile.am +++ b/c-layman/Makefile.am @@ -1,10 +1,10 @@ AUTOMAKE_OPTIONS = subdir-objects ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} lib_LTLIBRARIES = liblayman.la - + AM_CFLAGS = $(PYTHON_CFLAGS) --std=c99 -W -Wall -liblayman_la_SOURCES = src/config.c src/dict.c src/interpreter.c src/laymanapi.c src/message.c src/stringlist.c +liblayman_la_SOURCES = src/config.c src/dict.c src/interpreter.c src/laymanapi.c src/message.c src/stringlist.c src/layman.c #src/tester.c liblayman_la_LIBADD = $(PYTHON_LIBS) diff --git a/c-layman/src/basic.h b/c-layman/src/basic.h new file mode 100644 index 0000000..e6db912 --- /dev/null +++ b/c-layman/src/basic.h @@ -0,0 +1,5 @@ +#define TRUE 1 +#define True 1 +#define FALSE 0 +#define False 0 +
\ No newline at end of file diff --git a/c-layman/src/config.c b/c-layman/src/config.c index c578d90..774a954 100644 --- a/c-layman/src/config.c +++ b/c-layman/src/config.c @@ -1,17 +1,23 @@ #include <Python.h> +#include <stdio.h> #include "config.h" #include "internal.h" +#include "dict.h" +#include "basic.h" + /** * \internal */ + #define debug(x) printf(x) /** \defgroup config Config * \brief Layman library configuration module */ + /** \addtogroup config * @{ */ @@ -23,13 +29,14 @@ struct BareConfig { PyObject *object; -}; +} /** * \internal * Returns the internal Python object. */ -PyObject *_bareConfigObject(BareConfig *c) +PyObject * +_ConfigObject(BareConfig *c) { if (c && c->object) return c->object; @@ -46,8 +53,12 @@ PyObject *_bareConfigObject(BareConfig *c) * * \return a new instance of a BareConfig object. It must be freed with bareConfigFree() */ -BareConfig *bareConfigCreate(Message *m, FILE* outFd, FILE* inFd, FILE* errFd) +BareConfig * +bareConfigCreate(Message *m, FILE *outFd, FILE *inFd, FILE *errFd) { + if(!m) + return NULL; + if (!outFd || fileno(outFd) <= 0) outFd = stdout; if (!inFd || fileno(inFd) <= 0) @@ -55,18 +66,18 @@ BareConfig *bareConfigCreate(Message *m, FILE* outFd, FILE* inFd, FILE* errFd) if (!errFd || fileno(errFd) <= 0) errFd = stderr; - PyObject *pyout = PyFile_FromFile(outFd, "", "w", 0); + PyObject *pyout = PyFile_FromFile(outFd, "", "w", 0); PyObject *pyin = PyFile_FromFile(inFd, "", "r", 0); PyObject *pyerr = PyFile_FromFile(errFd, "", "w", 0); - PyObject *obj = executeFunction("layman.config", "BareConfig", "OOOO", _messageObject(m), pyout, pyin, pyerr); + PyObject *obj = executeFunction("layman.config", "BareConfig", "OOOO", _messagePyObject(m), pyout, pyin, pyerr); Py_DECREF(pyout); Py_DECREF(pyin); Py_DECREF(pyerr); if (!obj) { - debug("The execution failed, Are you sure app-portage/layman-8888 is properly installed ?\n"); + debug("The execution failed, Are you sure >=app-portage/layman-2.0* is properly installed ?\n"); return NULL; } @@ -76,10 +87,12 @@ BareConfig *bareConfigCreate(Message *m, FILE* outFd, FILE* inFd, FILE* errFd) return ret; } + /** * Frees a BareConfig object. */ -void bareConfigFree(BareConfig* cfg) +void +ConfigFree(BareConfig *cfg) { if (cfg && cfg->object) { @@ -87,9 +100,13 @@ void bareConfigFree(BareConfig* cfg) } if (cfg) + { free(cfg); + cfg = NULL; + } } + /** * Get an option's default value. * @@ -97,9 +114,10 @@ void bareConfigFree(BareConfig* cfg) * * \return the value or NULL on failure. */ -const char* bareConfigGetDefaultValue(BareConfig* cfg, const char* opt) +const char * +ConfigGetDefaultValue(BareConfig *cfg, const char *opt) { - PyObject *obj = PyObject_CallMethod(cfg->object, "get_defaults", NULL); + PyObject *obj = PyObject_CallMethod(_ConfigObject(cfg), "get_defaults", NULL); if (!obj) return NULL; @@ -119,14 +137,16 @@ const char* bareConfigGetDefaultValue(BareConfig* cfg, const char* opt) return ""; } -/* + +/** * Get an option's current value. * * \param opt the name of the option * * \return the value or NULL on failure */ -const char* bareConfigGetOptionValue(BareConfig* cfg, const char* opt) +const char * +ConfigGetOptionValue(BareConfig *cfg, const char *opt) { PyObject *obj = PyObject_CallMethod(cfg->object, "get_option", "(z)", opt); char *tmp = PyString_AsString(obj); @@ -139,7 +159,8 @@ const char* bareConfigGetOptionValue(BareConfig* cfg, const char* opt) return ret; } -/* + +/** * Modifies an option's value * * \param opt the name of the option @@ -147,18 +168,100 @@ const char* bareConfigGetOptionValue(BareConfig* cfg, const char* opt) * * \return True on success, 0 on failure */ -int bareConfigSetOptionValue(BareConfig* cfg, const char* opt, const char* val) +int +ConfigSetOptionValue(BareConfig *cfg, const char *opt, const char *val) { PyObject *obj = PyObject_CallMethod(cfg->object, "set_option", "(zz)", opt, val); int ret; if (obj) - ret = 1; + ret = True; else - ret = 0; + ret = False; Py_DECREF(obj); return ret; } + +/** + * Creates an option config object with new option and/or default values. + * + * + * \return a new instance of a OptionConfig object. It must be freed with ConfigFree() + */ +BareConfig * +optionConfigCreate( Dict *options, Dict *defaults) +{ + + PyObject *opts = dictToPyDict(options); + PyObject *dflts = dictToPyDict(defaults); + PyObject *obj = executeFunction("layman.config", "OptionConfig", "OOOO", opts, dflts); + + if (!obj) + { + debug("The execution failed, Are you sure >=app-portage/layman-2.0* is properly installed ?\n"); + return NULL; + } + + BareConfig *ret = malloc(sizeof(BareConfig)); + ret->object = obj; + + Py_DECREF(opts); + Py_DECREF(dflts); + + return ret; +} + + +/** + * Updates the options values + * + * \param opt Dict of the key/value pairs + * + * \return True on success, False on failure + */ +int +optionConfigUpdateOptions(BareConfig *cfg, Dict *opt) +{ + PyObject *opts = dictToPyDict(opt); + PyObject *obj = PyObject_CallMethod(cfg->object, "update", "(zz)", opts); + int ret; + if (obj) + ret = True; + else + ret = False; + + Py_DECREF(obj); + Py_DECREF(opt); + + return ret; +} + + +/** + * Updates the defaults values + * +* \param opt Dict of the default key/value pairs + * + * \return True on success, 0 on failure + */ +int +optionConfigUpdateDefaults(BareConfig *cfg, Dict *opt) +{ + PyObject *opts = dictToPyDict(opt); + PyObject *obj = PyObject_CallMethod(cfg->object, "update_defaults", "(zz)", opts); + int ret; + if (obj) + ret = True; + else + ret = False; + + Py_DECREF(obj); + Py_DECREF(opt); + + return ret; +} + + /** @} */ diff --git a/c-layman/src/config.h b/c-layman/src/config.h index cb3c726..11e4e12 100644 --- a/c-layman/src/config.h +++ b/c-layman/src/config.h @@ -1,20 +1,28 @@ #ifndef CONFIG_H #define CONFIG_H +#include <stdio.h> + #include "stringlist.h" #include "message.h" +#include "dict.h" + typedef struct BareConfig BareConfig; -BareConfig* bareConfigCreate(Message* m, FILE* outFd, FILE* inFd, FILE* errFd); +BareConfig *bareConfigCreate(Message *m, FILE *outFd, FILE *inFd, FILE *errFd); /* * FIXME : could the Python lib work the same way ? */ -const char* bareConfigGetDefaultValue(BareConfig* cfg, const char*); -const char* bareConfigGetOptionValue(BareConfig* cfg, const char* opt); -int bareConfigSetOptionValue(BareConfig* cfg, const char*, const char*); -void bareConfigFree(BareConfig*); +const char *ConfigGetDefaultValue(BareConfig *cfg, const char*); +const char *ConfigGetOptionValue(BareConfig *cfg, const char *opt); +int ConfigSetOptionValue(BareConfig *cfg, const char*, const char*); +void ConfigFree(BareConfig*); + +BareConfig *optionConfigCreate(Dict *options, Dict *defaults); +int optionConfigUpdateDefaults(BareConfig *cfg, Dict *opt); +int optionConfigUpdateOptions(BareConfig *cfg, Dict *opt); #endif diff --git a/c-layman/src/dict.c b/c-layman/src/dict.c index 29c1dea..f85951e 100644 --- a/c-layman/src/dict.c +++ b/c-layman/src/dict.c @@ -5,6 +5,7 @@ #include "internal.h" #include "dict.h" + /* * Dict */ @@ -16,12 +17,14 @@ struct DictElem struct DictElem *next; }; + struct Dict { DictElem *root; int count; }; + Dict *dictCreate() { Dict *ret = malloc(sizeof(Dict)); @@ -30,7 +33,9 @@ Dict *dictCreate() return ret; } -void dictInsert(Dict* list, const char* key, const char* value) + +void +dictInsert(Dict *list, const char *key, const char *value) { if (!list) return; @@ -42,12 +47,16 @@ void dictInsert(Dict* list, const char* key, const char* value) list->count++; } -unsigned int dictCount(Dict *list) + +unsigned int +dictCount(Dict *list) { return (list ? list->count : 0); } -void dictFree(Dict *list) + +void +dictFree(Dict *list) { if (!list) return; @@ -63,7 +72,9 @@ void dictFree(Dict *list) free(list); } -PyObject *dictToPyDict(Dict *dict) + +PyObject * +dictToPyDict(Dict *dict) { PyObject *pydict = PyDict_New(); DictElem *node = dict->root; diff --git a/c-layman/src/dict.h b/c-layman/src/dict.h index 86cbbb3..6046784 100644 --- a/c-layman/src/dict.h +++ b/c-layman/src/dict.h @@ -3,9 +3,10 @@ typedef struct Dict Dict; -Dict* dictCreate(); +Dict *dictCreate(); //char* tableFind(Dict *table, char* key); void dictFree(Dict *t); -void dictInsert(Dict* list, const char* key, const char* value); +void dictInsert(Dict *list, const char *key, const char *value); unsigned int dictCount(Dict *table); + #endif diff --git a/c-layman/src/internal.h b/c-layman/src/internal.h index 5d05d09..5a00acd 100644 --- a/c-layman/src/internal.h +++ b/c-layman/src/internal.h @@ -2,20 +2,50 @@ #define INTERNAL_H #include <Python.h> + #include "stringlist.h" #include "dict.h" #include "config.h" #include "message.h" -PyObject* executeFunction(const char *module, const char *funcName, const char* format, ...); + +PyObject *executeFunction(const char *module, const char *funcName, const char *format, ...); + +PyObject *_ConfigObject(BareConfig*); + +PyObject *_messagePyObject(Message *m); + +StringList *listToCList(PyObject *list); +PyObject *cListToPyList(StringList*); + +PyObject *dictToPyDict(Dict *dict); + +/** + * \internal + * Stores modules in a linked list so that they don't have to be reloaded every time. + */ +typedef struct PyObjectListElem +{ + PyObject *object; + struct PyObjectListElem *next; +} + -PyObject* _bareConfigObject(BareConfig*); +typedef struct PyObjectList +{ + PyObjectListElem root; + int count; +} -PyObject* _messageObject(Message* m); -StringList* listToCList(PyObject* list); -PyObject* cListToPyList(StringList*); +/** + * \internal + * A Python interpreter object keeps the context like the loaded modules. + */ +typedef struct Interpreter +{ + PyObjectList modules; +} -PyObject* dictToPyDict(Dict *dict); #endif diff --git a/c-layman/src/interpreter.c b/c-layman/src/interpreter.c index 95d40d9..72eedf7 100644 --- a/c-layman/src/interpreter.c +++ b/c-layman/src/interpreter.c @@ -1,189 +1,196 @@ #include <Python.h> #include <stdio.h> #include <string.h> + #include "interpreter.h" +#include "internal.h" -/** - * \internal - * Stores modules in a linked list so that they don't have to be reloaded every time. - */ -typedef struct PyObjectListElem -{ - PyObject *object; - struct PyObjectListElem *next; -} PyObjectListElem; + -typedef struct PyObjectList +/** High level interactive python session structure */ +typedef struct PythonSessionStruct { - PyObjectListElem *root; - int count; -} PyObjectList; + private: -PyObjectList *createObjectList() -{ - PyObjectList *ret = malloc(sizeof(PyObjectList)); - ret->count = 0; - ret->root = 0; - return ret; -} + PyObjectList + createObjectList() + { + PyObjectList *ret = malloc(sizeof(PyObjectList)); + ret->count = 0; + ret->root = 0; + return ret; + } -void insert(PyObjectList* list, PyObject *object) -{ - if (!list || !object) - return; - PyObjectListElem *node = malloc(sizeof(PyObjectListElem)); - node->object = object; - node->next = list->root; - list->root = node; - list->count++; -} - -PyObject *moduleNamed(const char *name, PyObjectList *list) -{ - PyObjectListElem *node = list->root; - while (node) + + void + insert(PyObjectList list, PyObject *object) { - if (strcmp(PyModule_GetName(node->object), name) == 0) - return node->object; - node = node->next; + if (!list || !object) + return; + PyObjectListElem node = malloc(sizeof(PyObjectListElem)); + node->object = object; + node->next = list->root; + list->root = node; + list->count++; } - - return NULL; -} -int listCount(PyObjectList *list) -{ - return (list ? list->count : 0); -} + PyObject * + moduleNamed(const char *name, PyObjectList list) + { + PyObjectListElem *node = list->root; + while (node) + { + if (strcmp(PyModule_GetName(node->object), name) == 0) + return node->object; + node = node->next; + } + + return NULL; + } -void freeList(PyObjectList *list, int deref) -{ - if (!list) - return; + int + listCount(PyObjectList list) + { + return (list ? list->count : 0); + } - PyObjectListElem *node = list->root; - while (node) + void + freeList(PyObjectList list, int deref) { - PyObjectListElem *tmp = node; - node = node->next; - if (deref) + if (!list) + return; + + PyObjectListElem *node = list->root; + while (node) { - Py_DECREF(tmp->object); + PyObjectListElem *tmp = node; + node = node->next; + if (deref) + { + Py_DECREF(tmp->object); + } + free(tmp); } - free(tmp); + + free(list); } - free(list); -} + public: -/** - * \internal - * A Python interpreter object keeps the context like the loaded modules. - */ -struct Interpreter -{ - PyObjectList *modules; -} *in = 0; - - -/** \defgroup layman_base Layman base - * - * \brief Layman Base functions - * - * These functions are used when you want to begin or end using the library. - */ - -/** \addtogroup layman_base - * @{ - */ - -/** - * This is the first function that must be called before using the library. - * It initializes the Python interpreter. - */ -void laymanInit() -{ - if (in) - return; + + Interpreter initialized = 0; - if (!Py_IsInitialized()) - Py_Initialize(); - in = malloc(sizeof(struct Interpreter)); - in->modules = createObjectList(); -} + /** \defgroup base Layman base + * + * \brief Base functions + * + * These functions are used when you want to begin or end using the library. + */ -/** - * Call this function when you're done using the library. - * It will free memory. - */ -void laymanFinalize() -{ - if (!in) - return; - freeList(in->modules, 1); - free(in); - - if (Py_IsInitialized()) - Py_Finalize(); -} - -/** @} */ - -/** - * \internal - * printf() like function that executes a python function - * - * \param module name of the python module in which the function is - * \param funcName the function name to call - * \param format printf() like list of arguments. See Python documentation - * \param ... arguments for the function - * - * \return the result of the execution. Can be NULL if the call failed. - */ -PyObject *executeFunction(const char *module, const char *funcName, const char* format, ...) -{ - assert(in); + /** \addtogroup base + * @{ + */ - // Make argument list - PyObject *args; - if (format == NULL || strcmp(format, "") == 0) - args = PyTuple_New(0); - else + + /** + * This is the first function that must be called before using the library. + * It initializes the Python interpreter. + */ + void + Initialize() { - va_list listArgs; - va_start(listArgs, format); + if (initialized) + return; + + if (!Py_IsInitialized()) + Py_Initialize(); - args = Py_VaBuildValue(format, listArgs); + initialized = malloc(sizeof(struct Interpreter)); + initialized->modules = createObjectList(); } - // Look for the module. - PyObject *mod = 0; - if (in->modules) + /** + * Call this function when you're done using the library. + * It will free memory. + */ + void + Finalize() { - mod = moduleNamed(module, in->modules); + if (!initialized) + return; + freeList(initialized->modules, 1); + free(initialized); + + if (Py_IsInitialized()) + Py_Finalize(); } - // If module is not loaded yet, do it. - if (!mod) + + /** @} */ + + /** + * \internal + * printf() like function that executes a python function + * + * \param module name of the python module in which the function is + * \param funcName the function name to call + * \param format printf() like list of arguments. See Python documentation + * \param ... arguments for the function + * + * \return the result of the execution. Can be NULL if the call failed. + */ + PyObject * + executeFunction(const char *module, const char *funcName, const char *format, ...) { - mod = PyImport_ImportModule(module); + assert(initialized); + + // Make argument list + PyObject *args; + if (format == NULL || strcmp(format, "") == 0) + args = PyTuple_New(0); + else + { + va_list listArgs; + va_start(listArgs, format); + + args = Py_VaBuildValue(format, listArgs); + } + + // Look for the module. + PyObject *mod = 0; + if (initialized->modules) + { + mod = moduleNamed(module, initialized->modules); + } + // If module is not loaded yet, do it. if (!mod) + { + mod = PyImport_ImportModule(module); + if (!mod) + return NULL; + insert(initialized->modules, mod); + } + + // Look for the function + PyObject *func = PyObject_GetAttrString(mod, funcName); + if (!PyCallable_Check(func)) return NULL; - insert(in->modules, mod); - } - // Look for the function - PyObject *func = PyObject_GetAttrString(mod, funcName); - if (!PyCallable_Check(func)) - return NULL; + // Execute it + PyObject *val = PyObject_CallObject(func, args); - // Execute it - PyObject *val = PyObject_CallObject(func, args); + is Py_XDECREF(args); - if (args != NULL) - { - Py_DECREF(args); + return val; } +}; - return val; -} + +/** Opens an interactive python session */ +PythonSessionStruct +PySession() +{ + PythonSessionStruct interpreter; + interpreter.Initialize(); + return interpreter; +}
\ No newline at end of file diff --git a/c-layman/src/interpreter.h b/c-layman/src/interpreter.h index b280ad2..c3cb819 100644 --- a/c-layman/src/interpreter.h +++ b/c-layman/src/interpreter.h @@ -1,7 +1,9 @@ #ifndef INTERPRETER_H #define INTERPRETER_H -void laymanInit(); -void laymanFinalize(); + +typedef struct PythonSessionStruct PythonSessionStruct; + +PythonSessionStruct PySession(); #endif diff --git a/c-layman/src/layman.c b/c-layman/src/layman.c new file mode 100644 index 0000000..f5b8c31 --- /dev/null +++ b/c-layman/src/layman.c @@ -0,0 +1,29 @@ +#include <Python.h> + +#include "config.h" +#include "internal.h" +#include "dict.h" +#include "basic.h" +#include "laymanapi.h" +#include "message.h" +#include "stringlist.h" +#include "interpreter.h" + + +/** High level object structure to hold a complete layman instance */ +typedef struct LaymanObject +{ + // Python + bool initialized = False; + PythonSessionStruct *pysession; + initialized = assert(pysession.initialized); + + // Layman + BareConfig *config = NULL; + Message *output = NULL; + LaymanAPI *api = NULL; + + +}; + + diff --git a/c-layman/src/layman.h b/c-layman/src/layman.h index 9811ba5..3baf5e2 100644 --- a/c-layman/src/layman.h +++ b/c-layman/src/layman.h @@ -1,6 +1,7 @@ #ifndef LAYMAN_H #define LAYMAN_H + #include "laymanapi.h" #include "message.h" #include "stringlist.h" @@ -8,4 +9,7 @@ #include "dict.h" #include "interpreter.h" + + typedef struct LaymanObject LaymanObject; + #endif diff --git a/c-layman/src/laymanapi.c b/c-layman/src/laymanapi.c index d733cfd..85dca1b 100644 --- a/c-layman/src/laymanapi.c +++ b/c-layman/src/laymanapi.c @@ -1,7 +1,10 @@ #include <Python.h> + #include "internal.h" #include "laymanapi.h" +#include "basic.h" + /** \defgroup layman_api Layman API * \brief Main API functions */ @@ -10,7 +13,8 @@ * @{ */ -static int _laymanAPIGetAllInfos(LaymanAPI* l, StringList* overlays, OverlayInfo *results, const char *overlay); +static int _laymanAPIGetAllInfos(LaymanAPI *l, StringList *overlays, OverlayInfo *results, const char *overlay); + /** * Layman structure that is used in all functions @@ -33,13 +37,14 @@ struct LaymanAPI * \param output ? * \return a new instance of the LaymanAPI class, to be freed with laymanAPIFree() */ -LaymanAPI* laymanAPICreate(BareConfig* config, int report_error, int output) +LaymanAPI * +laymanAPICreate(BareConfig *config, int report_error, int output) { PyObject *cfg; if (!config) cfg = Py_None; else - cfg = _bareConfigObject(config); + cfg = _ConfigObject(config); PyObject *obj = executeFunction("layman.api", "LaymanAPI", "Oii", cfg, report_error, output); if (!obj) @@ -51,6 +56,7 @@ LaymanAPI* laymanAPICreate(BareConfig* config, int report_error, int output) return ret; } + /** * Check if the given string is a valid repository * @@ -59,14 +65,15 @@ LaymanAPI* laymanAPICreate(BareConfig* config, int report_error, int output) * * \return True if the repository is valid, False if not */ -int laymanAPIIsRepo(LaymanAPI *l, const char* repo) +int +laymanAPIIsRepo(LaymanAPI *l, const char *repo) { if (!l || !l->object) - return 0; + return False; PyObject *obj = PyObject_CallMethod(l->object, "is_repo", "(s)", repo); if (!obj) - return 0; + return False; int ret = PyObject_IsTrue(obj); // ret must be 1 or 0 @@ -77,6 +84,7 @@ int laymanAPIIsRepo(LaymanAPI *l, const char* repo) return ret; } + /** * Check if the given string is a valid and installed repository * @@ -85,14 +93,15 @@ int laymanAPIIsRepo(LaymanAPI *l, const char* repo) * * \return True if the repository is valid and installed, False if not */ -int laymanAPIIsInstalled(LaymanAPI *l, const char* repo) +int +laymanAPIIsInstalled(LaymanAPI *l, const char *repo) { if (!l || !l->object) - return 0; + return False; PyObject *obj = PyObject_CallMethod(l->object, "is_installed", "(s)", repo); if (!obj) - return 0; + return False; int ret = PyObject_IsTrue(obj); // ret must be 1 or 0 @@ -103,6 +112,7 @@ int laymanAPIIsInstalled(LaymanAPI *l, const char* repo) return ret; } + /** * Returns a list of the available overlays. * @@ -111,7 +121,8 @@ int laymanAPIIsInstalled(LaymanAPI *l, const char* repo) * * \return the list of available overlays */ -StringList* laymanAPIGetAvailable(LaymanAPI* l, int reload) +StringList * +laymanAPIGetAvailable(LaymanAPI *l, int reload) { if (!l || !l->object) return NULL; @@ -127,6 +138,7 @@ StringList* laymanAPIGetAvailable(LaymanAPI* l, int reload) return ret; } + /** * Returns a list of the installed overlays. * @@ -135,7 +147,8 @@ StringList* laymanAPIGetAvailable(LaymanAPI* l, int reload) * * \return the list of installed overlays */ -StringList* laymanAPIGetInstalled(LaymanAPI* l, int reload) +StringList * +laymanAPIGetInstalled(LaymanAPI *l, int reload) { if (!l || !l->object) return NULL; @@ -144,12 +157,13 @@ StringList* laymanAPIGetInstalled(LaymanAPI* l, int reload) if (!obj) return NULL; - StringList* ret = listToCList(obj); + StringList *ret = listToCList(obj); Py_DECREF(obj); return ret; } + /** * Syncs an overlay. * @@ -158,14 +172,15 @@ StringList* laymanAPIGetInstalled(LaymanAPI* l, int reload) * * \return True if it succeeded, False if not. */ -int laymanAPISync(LaymanAPI* l, const char* overlay, int verbose) +int +laymanAPISync(LaymanAPI *l, const char *overlay, int verbose) { if (!l || !l->object) - return 0; + return False; PyObject *obj = PyObject_CallMethod(l->object, "sync", "(si)", overlay, verbose); if (!obj) - return 0; + return False; int ret = PyObject_IsTrue(obj); @@ -177,19 +192,21 @@ int laymanAPISync(LaymanAPI* l, const char* overlay, int verbose) return ret; } + /** * Updates the local overlay list. * * \return True if it succeeded, False if not. */ -int laymanAPIFetchRemoteList(LaymanAPI* l) +int +laymanAPIFetchRemoteList(LaymanAPI *l) { if (!l || !l->object) - return 0; + return False; PyObject *obj = PyObject_CallMethod(l->object, "fetch_remote_list", NULL); if (!obj) - return 0; + return False; int ret = PyObject_IsTrue(obj); assert(-1 != ret); @@ -199,6 +216,7 @@ int laymanAPIFetchRemoteList(LaymanAPI* l) return ret; } + /** * Gets the information from the overlays given in the StringList overlays. * The results are stored in the results table which must be initialized with N structures, @@ -210,11 +228,12 @@ int laymanAPIFetchRemoteList(LaymanAPI* l) * * \return the number of results structures that have been filled */ -int laymanAPIGetInfoStrList(LaymanAPI* l, StringList* overlays, OverlayInfo* results) +int +laymanAPIGetInfoStrList(LaymanAPI *l, StringList *overlays, OverlayInfo *results) { // Check input data. if (!l || !l->object || !overlays || !results) - return 0; + return False; // Convert the StringList to a Python list object. PyObject *list = cListToPyList(overlays); @@ -226,11 +245,8 @@ int laymanAPIGetInfoStrList(LaymanAPI* l, StringList* overlays, OverlayInfo* res // Check if the returned value is a dict as expected. if (!obj || !PyDict_Check(obj)) { - if (obj) - { - Py_DECREF(obj); - } - return 0; + is Py_XDECREF(obj); + return False; } PyObject *name, *tuple; @@ -258,7 +274,7 @@ int laymanAPIGetInfoStrList(LaymanAPI* l, StringList* overlays, OverlayInfo* res continue; // Copy values in the kth structure of the results. - char* tmp = PyString_AsString(name); + char *tmp = PyString_AsString(name); assert(NULL != tmp); results[k].name = strdup(tmp); @@ -280,6 +296,7 @@ int laymanAPIGetInfoStrList(LaymanAPI* l, StringList* overlays, OverlayInfo* res return k; } + /** * Provided for convenience, this function get the information for only 1 overlay. * @@ -287,7 +304,8 @@ int laymanAPIGetInfoStrList(LaymanAPI* l, StringList* overlays, OverlayInfo* res * * \return NULL if it fails, an OverlayInfo struct if not. */ -OverlayInfo *laymanAPIGetInfoStr(LaymanAPI* l, const char* overlay) +OverlayInfo * +laymanAPIGetInfoStr(LaymanAPI *l, char *overlay) { // Check input data. if (!l || !l->object || !overlay) @@ -306,6 +324,7 @@ OverlayInfo *laymanAPIGetInfoStr(LaymanAPI* l, const char* overlay) return oi; } + /** * Get all information from an overlay. * This function fills every fields but the text field of the OverlayInfo structure. @@ -314,7 +333,8 @@ OverlayInfo *laymanAPIGetInfoStr(LaymanAPI* l, const char* overlay) * * \return NULL if it fails, an OverlayInfo struct if not. */ -OverlayInfo *laymanAPIGetAllInfo(LaymanAPI* l, const char* overlay) +OverlayInfo * +laymanAPIGetAllInfo(LaymanAPI *l, const char *overlay) { // Check input data. if (!l || !l->object || !overlay) @@ -324,7 +344,7 @@ OverlayInfo *laymanAPIGetAllInfo(LaymanAPI* l, const char* overlay) OverlayInfo *ret = calloc(1, sizeof(OverlayInfo)); // Fill it in. - if (0 == _laymanAPIGetAllInfos(l, NULL, ret, overlay)) + if (False == _laymanAPIGetAllInfos(l, NULL, ret, overlay)) { free(ret); return NULL; @@ -334,6 +354,7 @@ OverlayInfo *laymanAPIGetAllInfo(LaymanAPI* l, const char* overlay) return ret; } + /** * Gives a list of OverlayInfo's from the overaly names found in the overlays StringList. * results must be allocated and initialized with zeroes. @@ -348,11 +369,13 @@ OverlayInfo *laymanAPIGetAllInfo(LaymanAPI* l, const char* overlay) * * @return the number of OverlayInfo structures filled. */ -int laymanAPIGetAllInfoList(LaymanAPI* l, StringList* overlays, OverlayInfo *results) +int +laymanAPIGetAllInfoList(LaymanAPI *l, StringList *overlays, OverlayInfo *results) { return _laymanAPIGetAllInfos(l, overlays, results, NULL); } + /** * \internal * \brief Gives a list of OverlayInfo's from the overaly names found in the overlays StringList if it's not NULL @@ -369,11 +392,12 @@ int laymanAPIGetAllInfoList(LaymanAPI* l, StringList* overlays, OverlayInfo *res * * \return the number of OverlayInfo structures filled. */ -int _laymanAPIGetAllInfos(LaymanAPI* l, StringList* overlays, OverlayInfo *results, const char *overlay) +int +_laymanAPIGetAllInfos(LaymanAPI *l, StringList *overlays, OverlayInfo *results, const char *overlay) { // Check input data. if (!l || !l->object || !results || (!overlays && !overlay)) - return 0; + return False; PyObject *obj = NULL; @@ -400,7 +424,7 @@ int _laymanAPIGetAllInfos(LaymanAPI* l, StringList* overlays, OverlayInfo *resul { Py_DECREF(obj); } - return 0; + return False; } PyObject *name, *dict; @@ -426,10 +450,10 @@ int _laymanAPIGetAllInfos(LaymanAPI* l, StringList* overlays, OverlayInfo *resul PyObject *srcType = PyDict_GetItemString(dict, "src_type"); PyObject *priority = PyDict_GetItemString(dict, "priority"); PyObject *quality = PyDict_GetItemString(dict, "quality"); -//'status':?? TODO + //'status':?? TODO // Copy values in the kth structure of the results. - char* tmp = PyString_AsString(name); + char *tmp = PyString_AsString(name); assert(NULL != tmp); //name must not be NULL results[k].name = strdup(tmp); @@ -477,6 +501,7 @@ int _laymanAPIGetAllInfos(LaymanAPI* l, StringList* overlays, OverlayInfo *resul return k; } + /** * Adds an overlay to layman * @@ -484,10 +509,11 @@ int _laymanAPIGetAllInfos(LaymanAPI* l, StringList* overlays, OverlayInfo *resul * * \return True if it succeeded, False if not */ -int laymanAPIAddRepo(LaymanAPI* l, const char *repo) +int +laymanAPIAddRepo(LaymanAPI *l, const char *repo) { if (!l || !l->object || !repo) - return 0; + return False; // Call the method PyObject *obj = PyObject_CallMethod(l->object, "delete_repos", "(s)", repo); @@ -495,15 +521,16 @@ int laymanAPIAddRepo(LaymanAPI* l, const char *repo) // If the call returned NULL, it failed. int ret; if (!obj) - ret = 0; + ret = False; else - ret = 1; + ret = True; Py_DECREF(obj); return ret; } + /** * Adds a list of overlays to layman * @@ -511,10 +538,11 @@ int laymanAPIAddRepo(LaymanAPI* l, const char *repo) * * Return True if it succeeded, False if not */ -int laymanAPIAddRepoList(LaymanAPI* l, StringList *repos) +int +laymanAPIAddRepoList(LaymanAPI *l, StringList *repos) { if (!l || !l->object || !repos) - return 0; + return False; // Converting the C list to a python list PyObject *pyrepos = cListToPyList(repos); @@ -526,15 +554,16 @@ int laymanAPIAddRepoList(LaymanAPI* l, StringList *repos) // If the call returned NULL, it failed. int ret; if (!obj) - ret = 0; + ret = False; else - ret = 1; + ret = True; Py_DECREF(obj); return ret; } + /** * Deletes an overlay from layman * @@ -542,10 +571,11 @@ int laymanAPIAddRepoList(LaymanAPI* l, StringList *repos) * * \return True if it succeeded, False if not */ -int laymanAPIDeleteRepo(LaymanAPI* l, const char *repo) +int +laymanAPIDeleteRepo(LaymanAPI *l, const char *repo) { if (!l || !l->object || !repo) - return 0; + return False; // Call the method PyObject *obj = PyObject_CallMethod(l->object, "delete_repos", "(s)", repo); @@ -553,15 +583,16 @@ int laymanAPIDeleteRepo(LaymanAPI* l, const char *repo) // If the call returned NULL, it failed. int ret; if (!obj) - ret = 0; + ret = False; else - ret = 1; + ret = True; Py_DECREF(obj); return ret; } + /** * Deletes a list of overlays from layman * @@ -569,10 +600,11 @@ int laymanAPIDeleteRepo(LaymanAPI* l, const char *repo) * * \return True if it succeeded, False if not */ -int laymanAPIDeleteRepoList(LaymanAPI* l, StringList *repos) +int +laymanAPIDeleteRepoList(LaymanAPI *l, StringList *repos) { if (!l || !l->object || !repos) - return 0; + return False; // Converting the C list to a python list PyObject *pyrepos = cListToPyList(repos); @@ -584,19 +616,54 @@ int laymanAPIDeleteRepoList(LaymanAPI* l, StringList *repos) // If the call returned NULL, it failed. int ret; if (!obj) - ret = 0; + ret = False; else - ret = 1; + ret = True; Py_DECREF(obj); return ret; } + +/** + * Retrieves any errors that were recorded by the LaymanAPI + */ +StringList * +laymanAPIGetErrors(LaymanAPI *l) +{ + if (!l) + return NULL; + + // Call the method + PyObject *obj = PyObject_CallMethod(l->object, "get_errors", "(O)", NULL); + if (!obj) + { + StringList *ret = stringListCreate(1); + char *msg = "*****laymanAPIGetErrors(): Failed to get a PyObject from get_errors call.\n"; + int ok = stringListInsertAt(ret, 1, msg); + if(ok == False) + { + printf("laymanAPIGetErrors(): Failed to insert failure message in StringList\n"); + printf("%s", msg); + stringListFree(ret); + return NULL; + } + return ret; + } + //listToCList() will return Type_NONE if the python list is not valid. + StringList *ret = listToCList(obj); + Py_DECREF(obj); + + return ret; +} + + /** * Frees a LaymanAPI object from memory */ -void laymanAPIFree(LaymanAPI* l) +void +laymanAPIFree(LaymanAPI* l) { if (l && l->object) { @@ -611,7 +678,8 @@ void laymanAPIFree(LaymanAPI* l) /* * Function that properly frees an OverlayInfo structure's data */ -void overlayInfoFree(OverlayInfo oi) +void +overlayInfoFree(OverlayInfo oi) { if (oi.name) free(oi.name); @@ -633,6 +701,46 @@ void overlayInfoFree(OverlayInfo oi) stringListFree(oi.srcUris); } + +/** + * Creates a high level LaymanAPI object that must be used for all functions. + * + * \param config a BareConfig object that contains all configuration options. If NULL, the default configuration will be used. + * \param report_error if True, errors reporting on stdout will be activated. + * \param output ? + * \return a new instance of the LaymanAPI class, to be freed with laymanAPIFree() + */ +LaymanAPI *laymanCreate( + FILE *outfd, FILE *infd, FILE *errfd, + BareConfig *cfg, int *read_configfile, int *quiet, int *quietness, + int *verbose, int *nocolor, int *width) +{ + PyObject *cfg; + if (!config) + cfg = Py_None; + else + cfg = _ConfigObject(config); + + PyObject *obj = executeFunction("layman", "Layman", "Oii", + outfd, infd, errfd, + _ConfigObject(cfg), + PyBool_FromLong(read_configfile), + PyBool_FromLong(quiet), + PyBool_FromLong(quietness), + PyBool_FromLong(verbose), + PyBool_FromLong(nocolor), + PyBool_FromLong(width) + ); + if (!obj) + return NULL; + + LaymanAPI *ret = malloc(sizeof(LaymanAPI)); + ret->object = obj; + + return ret; +} + + /** * @} */ diff --git a/c-layman/src/laymanapi.h b/c-layman/src/laymanapi.h index 60e20e0..93fd7ec 100644 --- a/c-layman/src/laymanapi.h +++ b/c-layman/src/laymanapi.h @@ -1,30 +1,34 @@ #ifndef LAYMAN_API_H #define LAYMAN_API_H + #include "config.h" #include "stringlist.h" + typedef struct LaymanAPI LaymanAPI; + /** * Contains all information for an overlay */ typedef struct OverlayInfo { - char* name; - char* text; - char* ownerEmail; - char* ownerName; - char* homepage; - char* description; - char* srcType; - char* quality; - int priority; - StringList* srcUris; - int official; - int supported; + char *name; + char *text; + char *ownerEmail; + char *ownerName; + char *homepage; + char *description; + char *srcType; + char *quality; + int priority; + StringList *srcUris; + int official; + int supported; } OverlayInfo; + /** * Creates a LaymanAPI object that must be used in all function in this file. * @@ -33,24 +37,30 @@ typedef struct OverlayInfo * \param output ? * \return a new instance of the LaymanAPI class, to be freed with laymanAPIFree() */ -LaymanAPI* laymanAPICreate(BareConfig* config, int report_error, int output); +LaymanAPI *laymanAPICreate(BareConfig *config, int report_error, int output); -int laymanAPIIsRepo(LaymanAPI *l, const char* repo); -int laymanAPIIsInstalled(LaymanAPI *l, const char* repo); -StringList* laymanAPIGetAvailable(LaymanAPI*, int reload); -StringList* laymanAPIGetInstalled(LaymanAPI*, int reload); -int laymanAPISync(LaymanAPI* l, const char* overlay, int verbose); +int laymanAPIIsRepo(LaymanAPI *l, const char *repo); +int laymanAPIIsInstalled(LaymanAPI *l, const char *repo); +StringList *laymanAPIGetAvailable(LaymanAPI*, int reload); +StringList *laymanAPIGetInstalled(LaymanAPI*, int reload); +int laymanAPISync(LaymanAPI* l, const char *overlay, int verbose); int laymanAPIFetchRemoteList(LaymanAPI*); -int laymanAPIGetInfoStrList(LaymanAPI* l, StringList* overlays, OverlayInfo* results); -OverlayInfo* laymanAPIGetInfoStr(LaymanAPI* l, const char* overlay); -int laymanAPIGetAllInfoList(LaymanAPI* l, StringList*, OverlayInfo*); -OverlayInfo* laymanAPIGetAllInfo(LaymanAPI* l, const char*); -int laymanAPIAddRepo(LaymanAPI* l, const char *repo); -int laymanAPIAddRepoList(LaymanAPI* l, StringList *repos); -int laymanAPIDeleteRepo(LaymanAPI* l, const char *repo); -int laymanAPIDeleteRepoList(LaymanAPI* l, StringList *repos); -OverlayInfo* laymanAPIGetInfo(LaymanAPI* l, const char* overlay); +int laymanAPIGetInfoStrList(LaymanAPI *l, StringList *overlays, OverlayInfo *results); +OverlayInfo *laymanAPIGetInfoStr(LaymanAPI *l, char *overlay); +int laymanAPIGetAllInfoList(LaymanAPI *l, StringList*, OverlayInfo*); +OverlayInfo *laymanAPIGetAllInfo(LaymanAPI *l, const char*); +int laymanAPIAddRepo(LaymanAPI *l, const char *repo); +int laymanAPIAddRepoList(LaymanAPI *l, StringList *repos); +int laymanAPIDeleteRepo(LaymanAPI *l, const char *repo); +int laymanAPIDeleteRepoList(LaymanAPI *l, StringList *repos); +OverlayInfo *laymanAPIGetInfo(LaymanAPI *l, const char *overlay); +StringList *laymanAPIGetErrors(LaymanAPI *l); void laymanAPIFree(LaymanAPI*); void overlayInfoFree(OverlayInfo oi); +LaymanAPI *laymanCreate( + FILE *outfd, FILE *infd, FILE *errfd, + BareConfig *cfg, int *read_configfile, int *quiet, int *quietness, + int *verbose, int *nocolor, int *width); + #endif diff --git a/c-layman/src/message.c b/c-layman/src/message.c index 4d35bb6..3d4e3cd 100644 --- a/c-layman/src/message.c +++ b/c-layman/src/message.c @@ -1,6 +1,9 @@ #include <Python.h> + #include "message.h" #include "internal.h" +#include "basic.h" + /** \defgroup message Message * \brief Debug message management @@ -9,10 +12,12 @@ * debug levels. */ + /** \addtogroup message * @{ */ + /** * Message structure that is used in all functions */ @@ -24,6 +29,7 @@ struct Message PyObject *object; }; + /** * Creates a Message instance with default values. * To modify those values, use the corresponding functions. @@ -35,34 +41,33 @@ struct Message * * \return a new instance of a Message object. It must be freed with messageFree() */ -Message *messageCreate(const char* module, - FILE* out, - FILE* err, - FILE* dbg) +Message * +messageCreate( + FILE *out, FILE *err, + int infolevel, int warnlevel, + int col) { - PyObject *pyout, *pyerr, *pydbg; + PyObject *pyout, *pyerr; if (!out || fileno(out) <= 0) out = stdout; if (!err || fileno(err) <= 0) err = stderr; - if (!dbg || fileno(dbg) <= 0) - dbg = stderr; pyout = PyFile_FromFile(out, "", "w", 0); pyerr = PyFile_FromFile(err, "", "w", 0); - pydbg = PyFile_FromFile(dbg, "", "w", 0); - PyObject *object = executeFunction("layman.debug", "Message", + PyObject *object = executeFunction("layman.output", "Message", "(sOOO)", - module, pyout, pyerr, - pydbg); + PyInt_FromLong(infolevel), + PyInt_FromLong(warnlevel), + PyBool_FromLong(col) + ); Py_DECREF(pyout); Py_DECREF(pyerr); - Py_DECREF(pydbg); if (!object) return NULL; @@ -73,6 +78,7 @@ Message *messageCreate(const char* module, return ret; } + /** * Set the debug level. * @@ -80,24 +86,26 @@ Message *messageCreate(const char* module, * * \return True on success, False on failure. */ -int messageSetDebugLevel(Message *m, int debug_level) +int +messageSetDebugLevel(Message *m, int debug_level) { if (!m || !m->object) - return 0; + return False; PyObject *obj = PyObject_CallMethod(m->object, "set_debug_level", "(I)", debug_level); int ret; if (obj) - ret = 1; + ret = True; else - ret = 0; + ret = False; Py_DECREF(obj); return ret; } + /** * Set the debug verbosity. * @@ -105,7 +113,7 @@ int messageSetDebugLevel(Message *m, int debug_level) * * \return True on success, False on failure. */ -int messageSetDebugVerbosity(Message *m, int debug_verbosity) +/*int messageSetDebugVerbosity(Message *m, int debug_verbosity) { if (!m || !m->object) return 0; @@ -122,6 +130,8 @@ int messageSetDebugVerbosity(Message *m, int debug_verbosity) return ret; } +*/ + /** * Set the info level. @@ -130,18 +140,20 @@ int messageSetDebugVerbosity(Message *m, int debug_verbosity) * * \return True on success, False on failure. */ -int messageSetInfoLevel(Message *m, int info_level) +int +messageSetInfoLevel(Message *m, int info_level) { if (!m || !m->object) - return 0; + return False; - PyObject *obj = PyObject_CallMethod(m->object, "set_info_level", "(I)", info_level); + PyObject *obj = PyObject_CallMethod(m->object, "set_info_level", "(I)", + PyInt_FromLong(info_level)); int ret; if (obj) - ret = 1; + ret = True; else - ret = 0; + ret = False; Py_DECREF(obj); @@ -155,18 +167,20 @@ int messageSetInfoLevel(Message *m, int info_level) * * \return True on success, False on failure. */ -int messageSetWarnLevel(Message *m, int warn_level) +int +messageSetWarnLevel(Message *m, int warn_level) { if (!m || !m->object) - return 0; + return False; - PyObject *obj = PyObject_CallMethod(m->object, "set_warn_level", "(I)", warn_level); + PyObject *obj = PyObject_CallMethod(m->object, "set_warn_level", "(I)", + PyInt_FromLong(warn_level)); int ret; if (obj) - ret = 1; + ret = True; else - ret = 0; + ret = False; Py_DECREF(obj); @@ -178,18 +192,19 @@ int messageSetWarnLevel(Message *m, int warn_level) * * \return 1 on success, 0 on failure */ -int messageSetColorsOn(Message *m) +int +messageSetColorsOn(Message *m) { if (!m || !m->object) - return 0; + return False; - PyObject *obj = PyObject_CallMethod(m->object, "color_on", NULL); + PyObject *obj = PyObject_CallMethod(m->object, "set_colorize", Py_True); int ret; if (obj) - ret = 1; + ret = True; else - ret = 0; + ret = False; Py_DECREF(obj); @@ -201,18 +216,19 @@ int messageSetColorsOn(Message *m) * * \return 1 on success, 0 on failure */ -int messageSetColorsOff(Message *m) +int +messageSetColorsOff(Message *m) { if (!m || !m->object) - return 0; + return False; - PyObject *obj = PyObject_CallMethod(m->object, "color_off", NULL); + PyObject *obj = PyObject_CallMethod(m->object, "set_colorize", Py_False); int ret; if (obj) - ret = 1; + ret = True; else - ret = 0; + ret = False; Py_DECREF(obj); @@ -226,7 +242,7 @@ int messageSetColorsOff(Message *m) * * \return 1 on success, 0 on failure */ -int messageSetDebugMethods(Message *m, const char* mth) +/*int messageSetDebugMethods(Message *m, const char* mth) { if (!m || !m->object) return 0; @@ -243,6 +259,8 @@ int messageSetDebugMethods(Message *m, const char* mth) return ret; } +*/ + /** * Sets the classes to be debugged. @@ -251,7 +269,7 @@ int messageSetDebugMethods(Message *m, const char* mth) * * \return 1 on success, 0 on failure */ -int messageSetDebugClasses(Message *m, const char* cla) +/*int messageSetDebugClasses(Message *m, const char* cla) { if (!m || !m->object) return 0; @@ -268,6 +286,8 @@ int messageSetDebugClasses(Message *m, const char* cla) return ret; } +*/ + /** * Sets the variables to be debugged. @@ -276,7 +296,7 @@ int messageSetDebugClasses(Message *m, const char* cla) * * \return 1 on success, 0 on failure */ -int messageSetDebugVariables(Message *m, const char* var) +/*int messageSetDebugVariables(Message *m, const char* var) { if (!m || !m->object) return 0; @@ -293,25 +313,32 @@ int messageSetDebugVariables(Message *m, const char* var) return ret; } +*/ + /** * Frees a message structure. */ -void messageFree(Message *m) +void +messageFree(Message *m) { if (m && m->object) { Py_DECREF(m->object); } if (m) + { free(m); + m = NULL; + } } /** * \internal * Returns the internal Python object */ -PyObject *_messageObject(Message* m) +PyObject * +_messagePyObject(Message* m) { if (m && m->object) return m->object; diff --git a/c-layman/src/message.h b/c-layman/src/message.h index 7fe8668..0c13765 100644 --- a/c-layman/src/message.h +++ b/c-layman/src/message.h @@ -2,11 +2,16 @@ #define MESSAGE_H #include <stdio.h> + #include "stringlist.h" typedef struct Message Message; -Message* messageCreate(const char* module, FILE* out, FILE* err, FILE* dbg); +Message *messageCreate(FILE *out, FILE *err, int infolevel, int warnlevel, int col); void messageFree(Message *m); +int messageSetDebugLevel(Message *m, int debug_level); +int messageSetInfoLevel(Message *m, int info_level); +int messageSetWarnLevel(Message *m, int warn_level); + #endif diff --git a/c-layman/src/stringlist.c b/c-layman/src/stringlist.c index fed7b8b..1d82d4d 100644 --- a/c-layman/src/stringlist.c +++ b/c-layman/src/stringlist.c @@ -1,25 +1,32 @@ #include <Python.h> #include <stdlib.h> + #include "stringlist.h" + /** \defgroup string_list StringList * \brief String list management class */ + /** \addtogroup string_list * @{ */ + + struct StringList { char **list; unsigned int count; }; + /** * Creates a String list to use with the library. * \param len the number of strings in the list. */ -StringList* stringListCreate(size_t len) +StringList * +stringListCreate(size_t len) { StringList *ret = malloc(sizeof(StringList)); ret->count = len; @@ -27,12 +34,14 @@ StringList* stringListCreate(size_t 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) +int +stringListInsertAt(StringList *l, unsigned int pos, char *str) { if(!l || !l->list || l->count < pos) return 0; @@ -42,23 +51,27 @@ int stringListInsertAt(StringList *l, unsigned int pos, char *str) return 1; } + /** * Get the number of strings in the list. * * \return the number of strings in the list */ -unsigned int stringListCount(StringList *l) +unsigned int +stringListCount(StringList *l) { if (!l) return 0; return l->count; } + /** * Get the String at position pos * \return the String at position pos */ -char* stringListGetAt(StringList *l, unsigned int pos) +char * +stringListGetAt(StringList *l, unsigned int pos) { if (!l || !l->list || pos >= l->count) return NULL; @@ -66,24 +79,28 @@ char* stringListGetAt(StringList *l, unsigned int pos) return l->list[pos]; } + /** * \section internal * @{ * \internal */ + /** * Converts a Python list object to a C String list */ -StringList* listToCList(PyObject* list) +StringList * +listToCList(PyObject *list) { if (!list || !PyList_Check(list)) return NULL; unsigned int len = PyList_Size(list); + //printf("listToCList(): len = %d\n", len); StringList *ret = malloc(sizeof(StringList)); ret->count = len; - ret->list = malloc(sizeof(char*) * len); + ret->list = malloc(sizeof(char*) *len); for (unsigned int i = 0; i < len; i++) { @@ -97,10 +114,12 @@ StringList* listToCList(PyObject* list) return ret; } + /** * Converts a C String list to a Python List object */ -PyObject* cListToPyList(StringList* list) +PyObject * +cListToPyList(StringList *list) { if (!list) Py_RETURN_NONE; @@ -114,29 +133,36 @@ PyObject* cListToPyList(StringList* list) return ret; } + /** @} */ + /** * Prints a C String list. */ -void stringListPrint(StringList* list) +void +stringListPrint(StringList *list) { if (!list) return; + + //printf("ListPrint: count = %d\n", list->count); for(unsigned int i = 0; i < list->count; i++) { - printf("\"%s\"", list->list[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) +void +stringListFree(StringList *list) { if (list && list->list) { @@ -149,7 +175,11 @@ void stringListFree(StringList* list) } if (list) + { free(list); + list = NULL; + } } + /** @} */ diff --git a/c-layman/src/stringlist.h b/c-layman/src/stringlist.h index 60800f0..b90cfd2 100644 --- a/c-layman/src/stringlist.h +++ b/c-layman/src/stringlist.h @@ -5,11 +5,11 @@ typedef struct StringList StringList; -StringList* stringListCreate(size_t); +StringList *stringListCreate(size_t); unsigned int stringListCount(StringList*); -int stringListInsertAt(StringList*, unsigned int, char*); -char* stringListGetAt(StringList*, unsigned int); +int stringListInsertAt(StringList*, unsigned int, char*); +char *stringListGetAt(StringList*, unsigned int); void stringListPrint(StringList*); void stringListFree(StringList*); - + #endif diff --git a/c-layman/src/tester.c b/c-layman/src/tester.c index de0b6c7..bed4108 100644 --- a/c-layman/src/tester.c +++ b/c-layman/src/tester.c @@ -1,19 +1,63 @@ #include <stdlib.h> + #include "interpreter.h" #include "config.h" #include "laymanapi.h" #include "message.h" +#include "basic.h" +#include "stringlist.h" + -int main(int argc, char *argv[]) +void +output_errors(LaymanAPI *l) +{ + StringList *errors = laymanAPIGetErrors(l); + if(errors == NULL) + printf("Failed to get any errors\n"); + else + { + printf("Recorded errors were:\n"); + printf("Error count = %d\n", stringListCount(errors)); + stringListPrint(errors); + } +} + + +int +test_fetch_remote(LaymanAPI *l) +/**/ +{ + int ret; + if (False == laymanAPIFetchRemoteList(l)) + { + printf("Unable to fetch the remote list.\n"); + output_errors(l); + ret = False; + } + else + ret = True; + return ret; +} + + +int +main(int argc, char *argv[]) { argc = argc; argv = argv; int ret = 0; laymanInit(); - Message *msg = messageCreate("layman", 0, 0, 0); + Message *msg = messageCreate( 0, 0, 4, 4, True); + if(msg == NULL) + printf("Failed to create a Message object\n"); + BareConfig *cfg = bareConfigCreate(msg, 0, 0, 0); - /*if (!bareConfigSetOptionValue(cfg, "local_list", "/home/detlev/src/gsoc2010/layman/layman/tests/testfiles/global-overlays.xml")) + if(cfg == NULL) + printf("Failed to create BareConfig object\n"); + + /*if (!bareConfigSetOptionValue(cfg, "local_list", + "/home/detlev/src/gsoc2010/layman/layman/tests/testfiles/global-overlays.xml")) printf("Error setting config option.\n"); if (!bareConfigSetOptionValue(cfg, "storage", "/home/detlev/gsoc2010/layman-test")) printf("Error setting config option.\n"); @@ -22,11 +66,9 @@ int main(int argc, char *argv[]) printf("local_list: %s\n", bareConfigGetOptionValue(cfg, "local_list"));*/ LaymanAPI *l = laymanAPICreate(cfg, 0, 0); - if (0 == laymanAPIFetchRemoteList(l)) - { - printf("Unable to fetch the remote list.\n"); - ret = -1; - } + + if(!test_fetch_remote(l)) + printf("\nFetch test failed.\n"); StringList *strs = laymanAPIGetAvailable(l, 0); printf("list:\n"); @@ -34,7 +76,7 @@ int main(int argc, char *argv[]) printf("\n"); - unsigned int len = stringListCount(strs); + //unsigned int len = stringListCount(strs); //OverlayInfo *infos = calloc(len, sizeof(OverlayInfo)); //int count = laymanAPIGetAllInfos(l, strs, infos); @@ -63,7 +105,7 @@ int main(int argc, char *argv[]) //free(infos); stringListFree(strs); - bareConfigFree(cfg); + ConfigFree(cfg); laymanAPIFree(l); laymanFinalize(); |