[Openvas-commits] r5539 - in trunk/openvas-libraries: . misc
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Wed Oct 14 13:55:59 CEST 2009
Author: felix
Date: 2009-10-14 13:55:58 +0200 (Wed, 14 Oct 2009)
New Revision: 5539
Modified:
trunk/openvas-libraries/ChangeLog
trunk/openvas-libraries/misc/hash_table_file.c
trunk/openvas-libraries/misc/hash_table_file.h
Log:
Refactored hash_table_file to be able to parse text directly (in
contrast to reading it from a file).
* misc/hash_table_file.c (hash_table_from_gkeyfile): New. Extracted
from hash_table_read.
(hash_table_read_text): New. Parse some textbuffer as GKeyFile.
(hash_table_read): Call extracted method.
misc/hash_table_file.h: Adjusted protos.
Modified: trunk/openvas-libraries/ChangeLog
===================================================================
--- trunk/openvas-libraries/ChangeLog 2009-10-14 10:21:19 UTC (rev 5538)
+++ trunk/openvas-libraries/ChangeLog 2009-10-14 11:55:58 UTC (rev 5539)
@@ -1,3 +1,14 @@
+2009-10-14 Felix Wolfsteller <felix.wolfsteller at intevation.de>
+
+ Refactored hash_table_file to be able to parse text directly (in
+ contrast to reading it from a file).
+
+ * misc/hash_table_file.c (hash_table_from_gkeyfile): New. Extracted
+ from hash_table_read.
+ (hash_table_read_text): New. Parse some textbuffer as GKeyFile.
+ (hash_table_read): Call extracted method.
+ misc/hash_table_file.h: Adjusted protos.
+
2009-10-14 Michael Wiegand <michael.wiegand at intevation.de>
* nasl/nasl.c (main): Made command line options more
Modified: trunk/openvas-libraries/misc/hash_table_file.c
===================================================================
--- trunk/openvas-libraries/misc/hash_table_file.c 2009-10-14 10:21:19 UTC (rev 5538)
+++ trunk/openvas-libraries/misc/hash_table_file.c 2009-10-14 11:55:58 UTC (rev 5539)
@@ -81,7 +81,7 @@
* @see GKeyFile
*/
gboolean
-hash_table_file_write (GHashTable* ghashtable, char* filename)
+hash_table_file_write (GHashTable* ghashtable, const char* filename)
{
int fd;
gchar* keyfile_data;
@@ -124,49 +124,93 @@
}
/**
- * @brief Reads key/value pairs (strings) from a file back into a GHashtable.
- *
- * The file has to follow freedesktop.org specifications.
- *
- * @param filename The filename to read from.
+ * @brief Reads key/value pairs (strings) from a GKeyFile into a GHashtable.
+ *
+ * Will free the GKeyFile.
+ *
+ * @param gkeyfile GKeyFile to use, will be freed.
+ *
* @return A GHashTable, mirroring the file or NULL in case of an error.
- *
- * @see hash_table_file_write
- * @see GKeyFile
*/
-GHashTable*
-hash_table_file_read (char* filename)
+static GHashTable*
+hash_table_from_gkeyfile (GKeyFile* gkeyfile)
{
- GKeyFile* file = NULL;
gchar** keys;
gchar** keys_it;
gsize length;
GHashTable* returntable = NULL;
-
- // Load key file into mem
- file = g_key_file_new ();
- g_key_file_load_from_file (file, filename, G_KEY_FILE_NONE, NULL);
- if (file == NULL)
- {
- return NULL;
- }
-
+
+ if (!gkeyfile)
+ return NULL;
+
returntable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
- keys = g_key_file_get_keys (file, GROUP_NONE, &length, NULL);
+ keys = g_key_file_get_keys (gkeyfile, GROUP_NONE, &length, NULL);
keys_it = keys;
-
+
// Add each key / value pair from file
while ( keys_it != NULL && (*keys_it) != NULL)
{
- char* value = g_key_file_get_value (file, GROUP_NONE, (*keys_it), NULL);
+ char* value = g_key_file_get_value (gkeyfile, GROUP_NONE, (*keys_it), NULL);
g_hash_table_insert (returntable, estrdup(*keys_it), value);
++keys_it;
}
-
+
if (keys != NULL)
g_strfreev (keys);
- g_key_file_free(file);
+ g_key_file_free (gkeyfile);
+
return returntable;
}
+
+/**
+ * @brief Reads key/value pairs (strings) from a text into a GHashtable.
+ *
+ * The text has to follow freedesktop.org specifications (e.g. be the text
+ * of a ini- file).
+ *
+ * @param text The text to use.
+ * @param length Lenght of \ref text.
+ *
+ * @return A GHashTable, mirroring the text or NULL in case of an error.
+ *
+ * @see hash_table_file_read
+ * @see hash_table_file_write
+ * @see GKeyFile
+ */
+GHashTable*
+hash_table_file_read_text (const char* text, gsize length)
+{
+ GKeyFile* file = NULL;
+
+ // Load key file from mem
+ file = g_key_file_new ();
+ g_key_file_load_from_data (file, text, length, G_KEY_FILE_NONE, NULL);
+
+ return hash_table_from_gkeyfile (file);
+}
+
+
+/**
+ * @brief Reads key/value pairs (strings) from a file back into a GHashtable.
+ *
+ * The file has to follow freedesktop.org specifications.
+ *
+ * @param filename The filename to read from.
+ * @return A GHashTable, mirroring the file or NULL in case of an error.
+ *
+ * @see hash_table_file_write
+ * @see GKeyFile
+ */
+GHashTable*
+hash_table_file_read (const char* filename)
+{
+ GKeyFile* file = NULL;
+
+ // Load key file into mem
+ file = g_key_file_new ();
+ g_key_file_load_from_file (file, filename, G_KEY_FILE_NONE, NULL);
+
+ return hash_table_from_gkeyfile (file);
+}
Modified: trunk/openvas-libraries/misc/hash_table_file.h
===================================================================
--- trunk/openvas-libraries/misc/hash_table_file.h 2009-10-14 10:21:19 UTC (rev 5538)
+++ trunk/openvas-libraries/misc/hash_table_file.h 2009-10-14 11:55:58 UTC (rev 5539)
@@ -33,7 +33,8 @@
#include <glib.h>
-gboolean hash_table_file_write(GHashTable* ghashtable, char* filename);
-GHashTable* hash_table_file_read(char* filename);
+gboolean hash_table_file_write (GHashTable* ghashtable, const char* filename);
+GHashTable* hash_table_file_read (const char* filename);
+GHashTable* hash_table_file_read_text (const char* text, gsize length);
#endif
More information about the Openvas-commits
mailing list