]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
Added virConfNew() and virConfSetValue() apis to virConf object
authorDaniel P. Berrange <berrange@redhat.com>
Wed, 15 Nov 2006 19:46:23 +0000 (19:46 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Wed, 15 Nov 2006 19:46:23 +0000 (19:46 +0000)
ChangeLog
src/conf.c
src/conf.h

index 7c17c5d6b43858b1bd49a91ae25994952dc88e39..cd186694b2e84eaac4d49c1c8789250a9f16f2c5 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Wed Nov 15 15:52:01 EST 2006 Daniel Berrange <berrange@redhat.com>
+
+       * 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 <berrange@redhat.com>
 
        * python/libvir.c, python/libvirt_wrap.h, python/types.h: Ensure
index 7d81f2dcbe11c27fe6a2fea631c7de4e2463bf1e..18fa71907221765eae2efe1701fa29f5cdc753f2 100644 (file)
@@ -144,16 +144,7 @@ virConfFreeValue(virConfValuePtr val)
     free(val);
 }
 
-/**
- * virConfCreate:
- * @filename: the name to report errors
- *
- * Create a configuration internal structure
- *
- * Returns a pointer or NULL in case of error.
- */
-static virConfPtr
-virConfCreate(const char *filename)
+virConfPtr virConfNew(void)
 {
     virConfPtr ret;
 
@@ -164,8 +155,25 @@ virConfCreate(const char *filename)
     }
     memset(ret, 0, sizeof(virConf));
 
-    ret->filename = filename;
+    ret->filename = NULL;
+
+    return(ret);
+}
 
+/**
+ * virConfCreate:
+ * @filename: the name to report errors
+ *
+ * Create a configuration internal structure
+ *
+ * Returns a pointer or NULL in case of error.
+ */
+static virConfPtr
+virConfCreate(const char *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:
+ */
index fb2ebab38c5d3f5b8b3a9da44d62cdae6518b650..3b83ba62c53b0abd12861a4524e70144a510228d 100644 (file)
@@ -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,