From 523389a72bea619a53caa0ca2105386c648d2661 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Wed, 15 Nov 2006 19:46:23 +0000 Subject: [PATCH] Added virConfNew() and virConfSetValue() apis to virConf object --- ChangeLog | 6 ++++ src/conf.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++------- src/conf.h | 4 +++ 3 files changed, 93 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7c17c5d6b4..cd186694b2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Wed Nov 15 15:52:01 EST 2006 Daniel Berrange + + * src/conf.c, src/conf.h: Add two new APIs virConfNew() and + virConfSetValue() for creating & populating new config objects + in memory instead of from a file + Wed Nov 15 15:42:01 EST 2006 Daniel Berrange * python/libvir.c, python/libvirt_wrap.h, python/types.h: Ensure diff --git a/src/conf.c b/src/conf.c index 7d81f2dcbe..18fa719072 100644 --- a/src/conf.c +++ b/src/conf.c @@ -144,6 +144,22 @@ virConfFreeValue(virConfValuePtr val) free(val); } +virConfPtr virConfNew(void) +{ + virConfPtr ret; + + ret = (virConfPtr) malloc(sizeof(virConf)); + if (ret == NULL) { + virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0); + return(NULL); + } + memset(ret, 0, sizeof(virConf)); + + ret->filename = NULL; + + return(ret); +} + /** * virConfCreate: * @filename: the name to report errors @@ -155,17 +171,9 @@ virConfFreeValue(virConfValuePtr val) static virConfPtr virConfCreate(const char *filename) { - virConfPtr ret; - - ret = (virConfPtr) malloc(sizeof(virConf)); - if (ret == NULL) { - virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0); - return(NULL); - } - memset(ret, 0, sizeof(virConf)); - - ret->filename = filename; - + virConfPtr ret = virConfNew(); + if (ret) + ret->filename = filename; return(ret); } @@ -784,6 +792,60 @@ virConfGetValue(virConfPtr conf, const char *setting) return(NULL); } +/** + * virConfGetValue: + * @conf: a configuration file handle + * @entry: the name of the entry + * @value: the new configuration value + * + * Set (or replace) the value associated to this entry in the configuration + * file. The passed in 'value' will be owned by the conf object upon return + * of this method, even in case of error. It should not be referenced again + * by the caller. + * + * Returns 0 on success, or -1 on failure. + */ +int virConfSetValue (virConfPtr conf, + const char *setting, + virConfValuePtr value) { + virConfEntryPtr cur, prev = NULL; + + cur = conf->entries; + while (cur != NULL) { + if ((cur->name != NULL) && (!strcmp(cur->name, setting))) { + break; + } + prev = cur; + cur = cur->next; + } + if (!cur) { + if (!(cur = malloc(sizeof(virConfEntry)))) { + virConfFreeValue(value); + return (-1); + } + cur->next = NULL; + cur->comment = NULL; + if (!(cur->name = strdup(setting))) { + virConfFreeValue(value); + free(cur); + return (-1); + } + cur->value = value; + if (prev) { + prev->next = cur; + } else { + conf->entries = cur; + } + } else { + if (cur->value) { + virConfFreeValue(cur->value); + } + cur->value = value; + } + return (0); +} + + /** * virConfWriteFile: * @filename: the path to the configuration file. @@ -878,3 +940,13 @@ error: virBufferFree(buf); return(ret); } + + +/* + * Local variables: + * indent-tabs-mode: nil + * c-indent-level: 4 + * c-basic-offset: 4 + * tab-width: 4 + * End: + */ diff --git a/src/conf.h b/src/conf.h index fb2ebab38c..3b83ba62c5 100644 --- a/src/conf.h +++ b/src/conf.h @@ -50,6 +50,7 @@ struct _virConfValue { typedef struct _virConf virConf; typedef virConf *virConfPtr; +virConfPtr virConfNew (void); virConfPtr virConfReadFile (const char *filename); virConfPtr virConfReadMem (const char *memory, int len); @@ -57,6 +58,9 @@ int virConfFree (virConfPtr conf); virConfValuePtr virConfGetValue (virConfPtr conf, const char *setting); +int virConfSetValue (virConfPtr conf, + const char *setting, + virConfValuePtr value); int virConfWriteFile (const char *filename, virConfPtr conf); int virConfWriteMem (char *memory,