[Openvas-commits] r6173 - in trunk/openvas-libraries: . base
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Thu Dec 17 14:58:51 CET 2009
Author: mwiegand
Date: 2009-12-17 14:58:46 +0100 (Thu, 17 Dec 2009)
New Revision: 6173
Added:
trunk/openvas-libraries/base/settings.c
trunk/openvas-libraries/base/settings.h
Modified:
trunk/openvas-libraries/ChangeLog
trunk/openvas-libraries/Makefile
trunk/openvas-libraries/base/CMakeLists.txt
Log:
Added initial support for accessing configuration files in the
keyfile format.
* base/settings.c: New.
* base/settings.h: New.
* base/CMakeLists.txt: Updated to include settings.c.
* Makefile: Update to ensure installation of settings.h.
Modified: trunk/openvas-libraries/ChangeLog
===================================================================
--- trunk/openvas-libraries/ChangeLog 2009-12-17 12:30:04 UTC (rev 6172)
+++ trunk/openvas-libraries/ChangeLog 2009-12-17 13:58:46 UTC (rev 6173)
@@ -1,3 +1,16 @@
+2009-12-17 Michael Wiegand <michael.wiegand at intevation.de>
+
+ Added initial support for accessing configuration files in the
+ keyfile format.
+
+ * base/settings.c: New.
+
+ * base/settings.h: New.
+
+ * base/CMakeLists.txt: Updated to include settings.c.
+
+ * Makefile: Update to ensure installation of settings.h.
+
2009-12-15 Matthew Mundell <matthew.mundell at intevation.de>
* base/pidfile.c: Add missing include.
Modified: trunk/openvas-libraries/Makefile
===================================================================
--- trunk/openvas-libraries/Makefile 2009-12-17 12:30:04 UTC (rev 6172)
+++ trunk/openvas-libraries/Makefile 2009-12-17 13:58:46 UTC (rev 6173)
@@ -86,6 +86,7 @@
$(INSTALL) -m 0444 base/hash_table_util.h $(DESTDIR)${includedir}/openvas/base
$(INSTALL) -m 0444 nasl/nasl.h $(DESTDIR)${includedir}/openvas/nasl
$(INSTALL) -m 0444 base/nvti.h $(DESTDIR)${includedir}/openvas/base
+ $(INSTALL) -m 0444 base/settings.h $(DESTDIR)${includedir}/openvas/base
$(INSTALL) -m 0444 base/openvas_certificate_file.h $(DESTDIR)${includedir}/openvas/base
$(INSTALL) -m 0444 base/openvas_string.h $(DESTDIR)${includedir}/openvas/base
$(INSTALL) -m 0444 base/pidfile.h $(DESTDIR)${includedir}/openvas/base
Modified: trunk/openvas-libraries/base/CMakeLists.txt
===================================================================
--- trunk/openvas-libraries/base/CMakeLists.txt 2009-12-17 12:30:04 UTC (rev 6172)
+++ trunk/openvas-libraries/base/CMakeLists.txt 2009-12-17 13:58:46 UTC (rev 6173)
@@ -136,7 +136,8 @@
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
set (FILES certificate.c hash_table_util.c nvti.c nvticache.c
-openvas_certificate_file.c openvas_string.c pidfile.c severity_filter.c)
+openvas_certificate_file.c openvas_string.c pidfile.c severity_filter.c
+settings.c)
add_library (openvas_base_static STATIC ${FILES})
set_target_properties (openvas_base_static PROPERTIES COMPILE_FLAGS "${GLIB_CFLAGS}")
Added: trunk/openvas-libraries/base/settings.c
===================================================================
--- trunk/openvas-libraries/base/settings.c 2009-12-17 12:30:04 UTC (rev 6172)
+++ trunk/openvas-libraries/base/settings.c 2009-12-17 13:58:46 UTC (rev 6173)
@@ -0,0 +1,102 @@
+/* openvas-libraries/base
+ * $Id$
+ * Description: Implementation of API to handle configuration file management
+ *
+ * Authors:
+ * Michael Wiegand <michael.wiegand at intevation.de>
+ *
+ * Copyright:
+ * Copyright (C) 2009 Greenbone Networks GmbH
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * or, at your option, any later version as published by the Free
+ * Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+/**
+ * @file settings.c
+ * @brief Implementation of API to handle configuration file management
+ *
+ */
+
+#include <string.h>
+#include <stdio.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+#include "settings.h"
+
+/**
+ * @brief Returns a HashTable of setting retrieved from a given group of
+ * an configuration file.
+ *
+ * @param filename The complete name of the configuration file.
+ * @param group The name of the group.
+ *
+ * @return A pointer to a GHashTable containing key/value pairs of all
+ * settings found in the group or NULL if the file contents could not be
+ * accessed. The HashTable should be freed with g_hash_table_destroy() when no
+ * longer needed.
+ */
+GHashTable *
+get_all_settings (const gchar * filename, const gchar * group)
+{
+ g_assert (filename);
+ g_assert (group);
+
+ GKeyFile* settingskeyfile = g_key_file_new ();
+ GError* error = NULL;
+ gchar** keys = NULL;
+ GHashTable* settings = NULL;
+ int i;
+
+ if (! g_key_file_load_from_file (settingskeyfile, filename,
+ G_KEY_FILE_NONE, &error))
+ {
+ g_warning ("Failed to load configuration from %s: %s", filename,
+ error->message);
+ g_key_file_free (settingskeyfile);
+ g_error_free (error);
+ return NULL;
+ }
+
+ keys = g_key_file_get_keys (settingskeyfile, group, NULL, &error);
+
+ if (keys == NULL)
+ {
+ if (error)
+ {
+ g_warning ("Failed to retrieve keys of group %s from %s: %s", group,
+ filename, error->message);
+ g_error_free (error);
+ }
+ g_key_file_free (settingskeyfile);
+ return NULL;
+ }
+
+ settings = g_hash_table_new (g_str_hash, g_str_equal);
+
+ for (i = 0; i < g_strv_length (keys); i++)
+ {
+ gchar* value = g_key_file_get_value (settingskeyfile, group,
+ keys[i], &error);
+ g_hash_table_insert (settings, g_strdup (keys[i]), g_strdup (value));
+ g_free (value);
+ }
+
+ g_strfreev (keys);
+ g_key_file_free (settingskeyfile);
+
+ return settings;
+}
+
Added: trunk/openvas-libraries/base/settings.h
===================================================================
--- trunk/openvas-libraries/base/settings.h 2009-12-17 12:30:04 UTC (rev 6172)
+++ trunk/openvas-libraries/base/settings.h 2009-12-17 13:58:46 UTC (rev 6173)
@@ -0,0 +1,41 @@
+/* openvas-libraries/base
+ * $Id$
+ * Description: API (structs and protos) for configuration file management
+ *
+ * Authors:
+ * Michael Wiegand <michael.wiegand at intevation.de>
+ *
+ * Copyright:
+ * Copyright (C) 2009 Greenbone Networks GmbH
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * or, at your option, any later version as published by the Free
+ * Software Foundation
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+/**
+ * @file settings.h
+ * @brief Protos and data structures for configuration file management
+ *
+ * This file contains the protos for \ref settings.c
+ */
+
+#ifndef _SETTINGS_H
+#define _SETTINGS_H
+
+#include <glib.h>
+
+GHashTable *
+get_all_settings (const gchar *, const gchar *);
+
+#endif /* not _SETTINGS_H */
More information about the Openvas-commits
mailing list