summaryrefslogtreecommitdiffstats
path: root/src/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.c')
-rw-r--r--src/config.c36
1 files changed, 26 insertions, 10 deletions
diff --git a/src/config.c b/src/config.c
index 47d4437..12490f7 100644
--- a/src/config.c
+++ b/src/config.c
@@ -16,14 +16,21 @@ PyObject *_bareConfigObject(BareConfig *c)
Py_RETURN_NONE;
}
+/*
+ * Creates a bare config object with default values.
+ */
BareConfig *bareConfigCreate(Message *m, FILE* outFd, FILE* inFd, FILE* errFd)
{
- PyObject *pyout = PyFile_FromFile(((!outFd || fileno(outFd) <= 0) ? stdout : outFd),
- "", "w", 0);
- PyObject *pyin = PyFile_FromFile(((!inFd || fileno(inFd) <= 0) ? stdin : inFd),
- "", "r", 0);
- PyObject *pyerr = PyFile_FromFile(((!errFd || fileno(errFd) <= 0) ? stderr : errFd),
- "", "w", 0);
+ if (!outFd || fileno(outFd) <= 0)
+ outFd = stdout;
+ if (!inFd || fileno(inFd) <= 0)
+ inFd = stdin;
+ if (!errFd || fileno(errFd) <= 0)
+ errFd = stderr;
+
+ 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);
Py_DECREF(pyout);
@@ -50,16 +57,19 @@ void bareConfigFree(BareConfig* cfg)
free(cfg);
}
+/*
+ * Returns an option's default value
+ */
const char* bareConfigGetDefaultValue(BareConfig* cfg, const char* opt)
{
PyObject *obj = PyObject_CallMethod(cfg->object, "get_defaults", NULL);
if (!obj)
return NULL;
- if (PyDict_Contains(obj, PyBytes_FromString(opt)))
+ if (PyDict_Contains(obj, PyString_FromString(opt)))
{
- PyObject *pyopt = PyBytes_FromString(opt);
- char *tmp = PyBytes_AsString(PyDict_GetItem(obj, pyopt));
+ PyObject *pyopt = PyString_FromString(opt);
+ char *tmp = PyString_AsString(PyDict_GetItem(obj, pyopt));
Py_DECREF(pyopt);
char *ret = malloc(sizeof(char) * strlen(tmp));
@@ -72,10 +82,13 @@ const char* bareConfigGetDefaultValue(BareConfig* cfg, const char* opt)
return "";
}
+/*
+ * Get an option's current value.
+ */
const char* bareConfigGetOptionValue(BareConfig* cfg, const char* opt)
{
PyObject *obj = PyObject_CallMethod(cfg->object, "get_option", "(z)", opt);
- char *tmp = PyBytes_AsString(obj);
+ char *tmp = PyString_AsString(obj);
char *ret = malloc(sizeof(char) * (strlen(tmp) + 1));
strcpy(ret, tmp);
@@ -85,6 +98,9 @@ const char* bareConfigGetOptionValue(BareConfig* cfg, const char* opt)
return ret;
}
+/*
+ * Modifies an option's value
+ */
int bareConfigSetOptionValue(BareConfig* cfg, const char* opt, const char* val)
{
PyObject *obj = PyObject_CallMethod(cfg->object, "set_option", "(zz)", opt, val);