From 1625b619deb8d8a82ea3d21443afb0b4f5442782 Mon Sep 17 00:00:00 2001
From: mh17 <mh17>
Date: Mon, 14 Dec 2009 12:46:54 +0000
Subject: [PATCH] really trying to get this file accross

---
 src/zmapConfig/zmapConfigIni.c | 1315 +++++---------------------------
 1 file changed, 204 insertions(+), 1111 deletions(-)

diff --git a/src/zmapConfig/zmapConfigIni.c b/src/zmapConfig/zmapConfigIni.c
index 42947a77f..3db4d01a2 100755
--- a/src/zmapConfig/zmapConfigIni.c
+++ b/src/zmapConfig/zmapConfigIni.c
@@ -1,5 +1,5 @@
 /*  File: zmapConfigIni.c
- *  Author: Roy Storey (rds@sanger.ac.uk)
+ *  Author: Malcolm Hinsley (mh17@sanger.ac.uk) after Roy Storey (rds@sanger.ac.uk)
  *  Copyright (c) 2008: Genome Research Ltd.
  *-------------------------------------------------------------------
  * ZMap is free software; you can redistribute it and/or
@@ -29,707 +29,38 @@
  * HISTORY:
  * Last edited: May 26 15:30 2009 (edgrif)
  * Created: Wed Aug 27 16:21:40 2008 (rds)
- * CVS info:   $Id: zmapConfigIni.c,v 1.10 2009-12-03 14:58:29 mh17 Exp $
+ * CVS info:   $Id: zmapConfigIni.c,v 1.11 2009-12-14 12:46:54 mh17 Exp $
  *-------------------------------------------------------------------
  */
 
 #include <string.h>		/* memset */
 #include <glib.h>
+#include <zmapConfigIni_P.h>
 #include <ZMap/zmapConfigDir.h>
 #include <ZMap/zmapUtils.h>
 #include <ZMap/zmapConfigIni.h>
 
-#define FILE_COUNT 5
-#define IMPORTANT_COUNT 2
 
 #undef WITH_LOGGING
 
-typedef struct _ZMapConfigIniStruct
-{
-  //  ZMapMagic magic;
-  GKeyFile *buffer_key_file;
-  GKeyFile *extra_key_file;
-  GKeyFile *user_key_file;
-  GKeyFile *zmap_key_file;
-  GKeyFile *sys_key_file;
-
-  GError *buffer_key_error;
-  GError *extra_key_error;
-  GError *user_key_error;
-  GError *zmap_key_error;
-  GError *sys_key_error;
-
-  unsigned int unsaved_alterations : 1;
-
-}ZMapConfigIniStruct;
-
-
-static gboolean zmapConfigIniGetValueFull(ZMapConfigIni config, 
-					  char * stanza_name, 
-					  char * key_name, 
-					  GValue *value, 
-					  GType type,
-					  gboolean merge_files);
-static gboolean get_merged_key_value(ZMapConfigIni config, 
-				     char * stanza_name, 
-				     char * key_name, 
-				     GValue *value, 
-				     GType type);
-static gboolean get_value(GKeyFile *key_file, 
-			  char *stanza_name, 
-			  char *key_name, 
-			  GValue *value, 
-			  GType type,
-			  gboolean allow_overwrite,
-			  gboolean clear_if_not_exist);
-static gboolean has_system_file(ZMapConfigIni config);
-static gboolean system_file_loaded(ZMapConfigIni config);
-static gboolean has_system_zmap_file(ZMapConfigIni config);
-static gboolean system_zmap_file_loaded(ZMapConfigIni config);
-static GKeyFile *read_file(char *file_name, GError **error_in_out);
-
-
-
-ZMapConfigIni zMapConfigIniNew(void)
-{
-  ZMapConfigIni config = NULL;
-
-  if(!(config = g_new0(ZMapConfigIniStruct, 1)))
-    {
-      zMapAssertNotReached();
-    }
-  else
-    {
-      g_type_init();
-      //config->magic = config_magic_G;
-    }
-  
-  return config;
-}
-
-gboolean zMapConfigIniReadAll(ZMapConfigIni config)
-{
-  gboolean red = FALSE;
-  zMapAssert(config);
 
-  if(!system_file_loaded(config) && has_system_file(config))
-    {
-      config->sys_key_file = read_file(zMapConfigDirGetSysFile(), &(config->sys_key_error));
-    }
 
-  if(!system_zmap_file_loaded(config) && has_system_zmap_file(config))
-    {
-      config->zmap_key_file = read_file(zMapConfigDirGetZmapHomeFile(), &(config->zmap_key_error));
-    }
-
-  red = zMapConfigIniReadUser(config);
-
-  return red;
-}
-
-gboolean zMapConfigIniReadUser(ZMapConfigIni config)
-{
-  gboolean red = FALSE;
-  zMapAssert(config);
-
-  if(!config->unsaved_alterations)
-    config->user_key_file = read_file(zMapConfigDirGetFile(), &(config->user_key_error));
-
-  if(config->user_key_file != NULL)
-    red = TRUE;
-
-  return red;
-}
-
-gboolean zMapConfigIniReadBuffer(ZMapConfigIni config, char *buffer)
-{
-  gboolean red = FALSE;
-  zMapAssert(config);
-
-  if(buffer != NULL)
-    {
-      config->buffer_key_file = g_key_file_new();
-      
-      if(!(g_key_file_load_from_data(config->buffer_key_file, 
-				     buffer, strlen(buffer),
-				     G_KEY_FILE_KEEP_COMMENTS, 
-				     &(config->buffer_key_error))))
-	{
-	  /* Do something with the error... */
-	  g_key_file_free(config->buffer_key_file);
-	  config->buffer_key_file = NULL;
-	}
-      else
-	red = TRUE;
-    }
-
-  return red;
-}
-
-gboolean zMapConfigIniReadFile(ZMapConfigIni config, char *file)
-{
-  gboolean read = FALSE;
-
-  zMapAssert(config);
-
-
-  if ((g_path_is_absolute(file)
-       || (file = zMapConfigDirFindFile(file)))
-      && (config->extra_key_file = read_file(file, &(config->extra_key_error))))
-    {
-      read = TRUE;
-    }
-
-  return read ;
-}
 
 void zMapConfigIniGetStanza(ZMapConfigIni config, char *stanza_name)
 {
-  
-  
-  return ;
-}
-
-void zMapConfigIniGetAllStanzas(ZMapConfigIni config)
-{
-  return ;
-}
-
-gboolean zMapConfigIniGetUserValue(ZMapConfigIni config,
-				   char * stanza_name,
-				   char * key_name,
-				   GValue **value_out,
-				   GType type)
-{
-  gboolean success = FALSE;
-  GValue *value = NULL;
-
-  /* Get the value from _only_ the user config file.  Do not merge any
-   * other files that might be present. */
-  if(value_out && (value   = g_new0(GValue, 1)))
-    {
-      value   = g_value_init(value, type);
-      success = zmapConfigIniGetValueFull(config, stanza_name, key_name, 
-					  value, type, FALSE);
-      if(success)
-	*value_out = value;
-      else
-	{
-	  g_value_unset(value);
-	  g_free(value);
-	}
-    }
-
-  return success;
-}
-
-gboolean zMapConfigIniGetValue(ZMapConfigIni config,
-			       char * stanza_name,
-			       char * key_name,
-			       GValue **value_out,
-			       GType type)
-{
-  gboolean success = FALSE;
-  GValue *value = NULL;
-
-  /* Get the value from all files that exist, merging as appropriate. */
-  if(value_out && (type != 0) && (value = g_new0(GValue, 1)))
-    {
-      value   = g_value_init(value, type);
-      success = zmapConfigIniGetValueFull(config, stanza_name, key_name, 
-					  value, type, TRUE);
-      if(success)
-	*value_out = value;
-      else
-	{
-	  g_value_unset(value);
-	  g_free(value);
-	}
-    }
-
-  return success;
-}
-
-gboolean zMapConfigIniHasStanza(ZMapConfigIni config,
-				char *stanza_name)
-{
-  GKeyFile *files[FILE_COUNT];
-  gboolean result = FALSE;
-  int i;
-
-  files[0] = config->sys_key_file;
-  files[1] = config->zmap_key_file;
-  files[2] = config->user_key_file;
-  files[3] = config->extra_key_file;
-  files[4] = config->buffer_key_file;
-  
-  for (i = 0; result == FALSE && i < FILE_COUNT; i++)
-    {
-      if(files[i])
-	result = g_key_file_has_group(files[i], stanza_name);
-    }
-  
-  return result;
-}
-
-void zMapConfigIniSetValue(ZMapConfigIni config, 
-			   char *stanza_name, 
-			   char *key_name, 
-			   GValue *value)
-{
-  GKeyFile *user_key_file = NULL;
-
-  if((user_key_file = config->user_key_file))
-    {
-      GType type = 0;
-      type = G_VALUE_TYPE(value);
-
-      switch(type)
-	{
-	case G_TYPE_STRING:
-	  {
-	    char *string_value = NULL;
-	    string_value = (char *)g_value_get_string(value);
-	    g_key_file_set_string(user_key_file, stanza_name, key_name, string_value);
-	  }
-	  break;
-	case G_TYPE_INT:
-	  {
-	    int int_value = 0;
-	    int_value = g_value_get_int(value);
-	    g_key_file_set_integer(user_key_file, stanza_name, key_name, int_value);
-	  }
-	  break;
-	case G_TYPE_BOOLEAN:
-	  {
-	    gboolean bool_value = FALSE;
-	    bool_value = g_value_get_boolean(value);
-	    g_key_file_set_boolean(user_key_file, stanza_name, key_name, bool_value);
-	  }
-	  break;
-	case G_TYPE_DOUBLE:
-	  {
-	    double double_value = FALSE;
-	    double_value = g_value_get_double(value);
-	    g_key_file_set_double(user_key_file, stanza_name, key_name, double_value);
-	  }
-	  break;
-	default:
-	  break;
-	}
-    }
-
-  return ;
-}
-
-gboolean zMapConfigIniSaveUser(ZMapConfigIni config)
-{
-  gboolean saved = FALSE;
-
-  if(config->user_key_file)
-    {
-      char *file_contents = NULL;
-      gsize file_size = 0;
-      GError *key_error = NULL;
-
-      file_contents = g_key_file_to_data(config->user_key_file,
-					 &file_size,
-					 &key_error);
-
-      if((!file_contents) || key_error)
-	{
-	  /* There was an error */
-	}
-      else
-	{
-	  /* Ok we can write the file contents to disk */
-	  char *filename = NULL;
-
-	  if((filename = zMapConfigDirGetFile()))
-	    {
-	      GIOChannel *output = NULL;
-	      GError *error = NULL;
-
-	      if((output = g_io_channel_new_file(filename, "w", &error)) && (!error))
-		{
-		  GIOStatus status;
-		  gsize bytes_written = 0;
-
-		  status = g_io_channel_write_chars(output, file_contents, file_size,
-						    &bytes_written, &error);
-
-		  if((status == G_IO_STATUS_NORMAL) && (!error) && bytes_written == file_size)
-		    {
-		      /* Everything was ok */
-		      /* need to flush file... */
-		    }
-		  else if(error)
-		    {
-		      /* Cry about the error */
-		      zMapLogCritical("%s", error->message);
-		      g_error_free(error);
-		      error = NULL;
-		    }
-		  else
-		    zMapLogCritical("g_io_channel_write_chars returned error status '%d', but no error.", status);
-
-		  status = g_io_channel_shutdown(output, TRUE, &error);
-
-		  if(status != G_IO_STATUS_NORMAL && error)
-		    {
-		      /* Cry about another error... */
-		      zMapLogCritical("%s", error->message);
-		      g_error_free(error);
-		      error = NULL;
-		    }
-		}
-
-	      if(filename != zMapConfigDirGetFile())
-		{
-		  g_free(filename);
-		  filename = NULL;
-		}
-	    }
-	  else
-	    {
-	      /* problem with your file */
-	    }
-	}
-
-      if(file_contents)
-	{
-	  g_free(file_contents);
-	  file_contents = NULL;
-	}
-    }
-  
-
-  return saved;
-}
-
-void zMapConfigIniDestroy(ZMapConfigIni config, gboolean save_user)
-{
-  if(save_user)
-    zMapConfigIniSaveUser(config);
-
-  /* Free the key files */
-  if(config->sys_key_file)
-    g_key_file_free(config->sys_key_file);
-  if(config->zmap_key_file)
-    g_key_file_free(config->zmap_key_file);
-  if(config->user_key_file)
-    g_key_file_free(config->user_key_file);
-  if(config->extra_key_file)
-    g_key_file_free(config->extra_key_file);
-
-  /* And any errors left hanging around */
-  if(config->sys_key_error)
-    g_error_free(config->sys_key_error);
-  if(config->zmap_key_error)
-    g_error_free(config->zmap_key_error);
-  if(config->user_key_error)
-    g_error_free(config->user_key_error);
-  if(config->extra_key_error)
-    g_error_free(config->extra_key_error);
-
-  /* zero the memory */
-  memset(config, 0, sizeof(ZMapConfigIniStruct));
-
   return ;
 }
 
 
-
-
-/* internal calls */
-
-
-static gboolean zmapConfigIniGetValueFull(ZMapConfigIni config, 
-					  char * stanza_name, 
-					  char * key_name, 
-					  GValue *value, 
-					  GType type,
-					  gboolean merge_files)
-{
-  gboolean success = FALSE;
-
-  g_return_val_if_fail(value, FALSE);
-
-  if((!merge_files) && (config->user_key_file != NULL))
-    {
-      success = get_value(config->user_key_file, stanza_name, key_name, value, type, TRUE, TRUE);
-    }
-  else
-    {
-      success = get_merged_key_value(config, stanza_name, key_name, value, type);
-    }
-
-  return success;
-}
-
-
-/* dynamic merge, versus static (initial) merge??? */
-
-/* dynamic is going to take a lot more time, but might be more
-   accurate and easy to trace which setting came from where, at the
-   expense of having multiple GKeyFile instances around */
-
-static gboolean get_merged_key_value(ZMapConfigIni config, 
-				     char * stanza_name, 
-				     char * key_name, 
-				     GValue *value, 
-				     GType type)
-{
-  GKeyFile *files[FILE_COUNT], *important_files[IMPORTANT_COUNT];
-  gboolean key_found = FALSE;
-  gboolean honour_important = TRUE;
-  char *important_key = NULL;
-  int i;
-
-  if(honour_important)
-    {
-      important_key = g_strdup_printf("!%s", key_name);
-
-      /* get the system first, then the zmap one */
-      important_files[1] = config->sys_key_file;
-      important_files[0] = config->zmap_key_file;
-      
-      for(i = 0; key_found == FALSE && i < IMPORTANT_COUNT; i++)
-	{
-	  if(important_files[i])
-	    {
-	      if(get_value(important_files[i], stanza_name, key_name, value, type, TRUE, FALSE))
-		key_found = TRUE;
-	    }
-	}
-      
-      if(important_key)
-	g_free(important_key);
-    }
-
-  if(key_found == FALSE)
-    {
-      files[0] = config->buffer_key_file;
-      files[1] = config->extra_key_file;
-      files[2] = config->user_key_file;
-      files[3] = config->zmap_key_file;
-      files[4] = config->sys_key_file;
-
-      for(i = 0; key_found == FALSE && i < FILE_COUNT; i++)
-	{
-	  if(files[i])
-	    {
-	      if(get_value(files[i], stanza_name, key_name, value, type, TRUE, FALSE))
-		key_found = TRUE;
-	    }
-	}
-    }
-
-  return key_found;
-}
-
-static gboolean get_value(GKeyFile *key_file, 
-			  char *stanza_name, 
-			  char *key_name, 
-			  GValue *value, 
-			  GType type,
-			  gboolean allow_overwrite,
-			  gboolean clear_if_not_exist)
-{
-  char *tmp_string = NULL, **tmp_vector = NULL;
-  int tmp_integer;
-  double tmp_double;
-  GError *error = NULL;
-  gboolean value_set = FALSE;
-  gboolean tmp_bool;
-
-  g_return_val_if_fail(value != NULL, FALSE);
-  g_return_val_if_fail(G_VALUE_TYPE(value) == type, FALSE);
-
-  if(key_file)
-    {
-      switch(type)
-	{
-	case G_TYPE_INT:
-	  tmp_integer = g_key_file_get_integer (key_file,
-						stanza_name,
-						key_name,
-						&error);
-	  break;
-	case G_TYPE_DOUBLE:
-	  tmp_double = g_key_file_get_double (key_file,
-					      stanza_name,
-					      key_name,
-					      &error);
-	  break;
-	case G_TYPE_BOOLEAN:
-	  tmp_bool = g_key_file_get_boolean(key_file,
-					    stanza_name,
-					    key_name,
-					    &error);
-	  break;
-	case G_TYPE_POINTER:
-	  {
-	    unsigned int vector_length;
-	    tmp_vector = g_key_file_get_string_list(key_file,
-						    stanza_name,
-						    key_name,
-						    &vector_length,
-						    &error);
-	  }
-	  break;
-	case G_TYPE_STRING:
-	default:
-	  tmp_string = g_key_file_get_value (key_file,
-					     stanza_name,
-					     key_name,
-					     &error);
-	  break;
-	}
-    }
-
-  if(error)
-    {
-      /* handle error correctly */
-      switch(error->code)
-	{
-	case G_KEY_FILE_ERROR_KEY_NOT_FOUND: /* a requested key was not found  */
-	case G_KEY_FILE_ERROR_GROUP_NOT_FOUND: /* a requested group was not found  */
-	  {
-	    /* Is this really an error for us? */
-	    if(clear_if_not_exist)
-	      {
-		g_value_reset(value);
-	      }
-	  }
-	  break;
-	case G_KEY_FILE_ERROR_INVALID_VALUE: /* a value could not be parsed  */
-	  {
-	    char *try_again = NULL;
-	    /* This is reasonably serious, type didn't match in 
-	     * the get_strig/integer/double */
-	    if(type != G_TYPE_STRING)
-	      try_again = g_key_file_get_value(key_file, stanza_name, key_name, NULL);
-
-	    /* It would be nice to have line numbers here,
-	     * but group and key should be enough I guess. */
-#ifdef WITH_LOGGING
-	    zMapLogWarning("Failed reading/converting value '%s' for key '%s' in stanza '%s'. Expected type <>", 
-			   (try_again == NULL ? tmp_string : try_again),
-			   key_name, stanza_name);
-#endif /* WITH_LOGGING */
-	    if(try_again)
-	      g_free(try_again);
-	  }
-	  break;
-	case G_KEY_FILE_ERROR_UNKNOWN_ENCODING:	/* the text being parsed was in an unknown encoding  */
-	case G_KEY_FILE_ERROR_PARSE: /* document was ill-formed  */
-	case G_KEY_FILE_ERROR_NOT_FOUND: /* the file was not found  */
-	  /* We really shouldn't be getting these here.  */
-	  break;
-	default:
-	  break;
-	}
-    }
-  else if(allow_overwrite)
-    {
-      g_value_reset(value);
-
-      switch(type)
-	{
-	case G_TYPE_INT:
-	  g_value_set_int(value, tmp_integer);
-	  break;
-	case G_TYPE_DOUBLE:
-	  g_value_set_double(value, tmp_double);
-	  break;
-	case G_TYPE_BOOLEAN:
-	  g_value_set_boolean(value, tmp_bool);
-	  break;
-	case G_TYPE_POINTER:
-	  g_value_set_pointer(value, tmp_vector);
-	  break;
-	case G_TYPE_STRING:
-	default:
-	  g_value_set_string(value, tmp_string);
-	  break;
-	}
-
-      value_set = TRUE;
-    }
-
-  /* Just free tmp_string... */
-  if(tmp_string)
-    g_free(tmp_string);
-
-  return value_set;
-}
-
-static gboolean has_system_file(ZMapConfigIni config)
-{
-  gboolean exists = FALSE;
-  char *file = NULL;
-
-  if((file = zMapConfigDirGetSysFile()))
-    exists = TRUE;
-
-  return exists;
-}
-
-static gboolean system_file_loaded(ZMapConfigIni config)
-{
-  gboolean exists = FALSE;
-
-  if(config->sys_key_file)
-    exists = TRUE;
-
-  return exists;
-}
-
-static gboolean has_system_zmap_file(ZMapConfigIni config)
-{
-  gboolean exists = FALSE;
-  char *file = NULL;
-
-  if((file = zMapConfigDirGetZmapHomeFile()))
-    exists = TRUE;
-
-  return exists;
-}
-
-static gboolean system_zmap_file_loaded(ZMapConfigIni config)
+// all stanzas froma file, assumed to be of the same type
+gchar **zMapConfigIniContextGetAllStanzaNames(ZMapConfigIniContext context)
 {
-  gboolean exists = FALSE;
+  gchar **names = NULL;
 
-  if(config->zmap_key_file)
-    exists = TRUE;
-
-  return exists;
+  names = g_key_file_get_groups(context->config->extra_key_file,NULL);
+  return names ;
 }
 
-static GKeyFile *read_file(char *file_name, GError **error_in_out)
-{
-  GKeyFile *key_file = NULL;
-  GError *error = NULL;
-
-  g_return_val_if_fail((error_in_out && *error_in_out == NULL), NULL);
-
-  key_file = g_key_file_new();
-
-  if(!(g_key_file_load_from_file(key_file, file_name, G_KEY_FILE_KEEP_COMMENTS, &error)))
-    {
-      /* Do something with the error... */
-      g_key_file_free(key_file);
-      key_file = NULL;
-
-      *error_in_out = error;
-    }
-
-
-  return key_file;
-}
-
-
 
 typedef struct
 {
@@ -738,16 +69,6 @@ typedef struct
   GList *keys;
 }ZMapConfigIniContextStanzaEntryStruct, *ZMapConfigIniContextStanzaEntry;
 
-typedef struct _ZMapConfigIniContextStruct
-{
-  ZMapConfigIni config;
-
-  gboolean config_read;
-
-  char *error_message;
-
-  GList *groups;
-}ZMapConfigIniContextStruct;
 
 typedef struct
 {
@@ -756,17 +77,18 @@ typedef struct
   gboolean result;
 } CheckRequiredKeysStruct, *CheckRequiredKeys;
 
-static gint match_name_type(gconstpointer list_data, gconstpointer user_data);
-static GType get_stanza_key_type(ZMapConfigIniContext context,
-				 char *stanza_name,
-				 char *stanza_type,
-				 char *key_name);
-static ZMapConfigIniContextStanzaEntry get_stanza_with_type(ZMapConfigIniContext context,
-							    char *stanza_type);
+
+
 static GList *copy_keys(ZMapConfigIniContextKeyEntryStruct *keys);
 static void check_required(gpointer list_data, gpointer user_data);
 static gboolean check_required_keys(ZMapConfigIniContext context, 
 				    ZMapConfigIniContextStanzaEntry stanza);
+static GType get_stanza_key_type(ZMapConfigIniContext context,
+                         char *stanza_name,
+                         char *stanza_type,
+                         char *key_name);
+static gint match_name_type(gconstpointer list_data, gconstpointer user_data);
+
 
 
 ZMapConfigIniContext zMapConfigIniContextCreate(void)
@@ -805,9 +127,9 @@ gboolean zMapConfigIniContextIncludeFile(ZMapConfigIniContext context, char *fil
 }
 
 
-char *zMapConfigIniContextErrorMessage(ZMapConfigIniContext context)
+gchar *zMapConfigIniContextErrorMessage(ZMapConfigIniContext context)
 {
-  char *e = NULL;
+  gchar *e = NULL;
   e = context->error_message;
   return e;
 }
@@ -852,11 +174,82 @@ gboolean zMapConfigIniContextAddGroup(ZMapConfigIniContext context,
 }
 
 
+static void check_required(gpointer list_data, gpointer user_data)
+{
+  CheckRequiredKeys checking_data = (CheckRequiredKeys)user_data;
+  ZMapConfigIniContextKeyEntry key = (ZMapConfigIniContextKeyEntry)list_data;
+  ZMapConfigIniContextStanzaEntry stanza;
+  ZMapConfigIniContext context;
+  
+  context = checking_data->context;
+  stanza  = checking_data->stanza;
+
+  if(checking_data->result && !context->error_message && key->required)
+    {
+      GValue *value = NULL;
+      
+      if(zMapConfigIniGetValue(context->config, 
+                         stanza->stanza_name,
+                         key->key,
+                         &value, key->type))
+      {
+        checking_data->result = TRUE;
+        g_value_unset(value);
+        g_free(value);
+      }
+      else
+      {
+        checking_data->result = FALSE;
+        context->error_message = g_strdup_printf("Failed to get required key '%s' in stanza %s",
+                                       key->key, stanza->stanza_name);
+      }
+    }
+
+  return ;
+}
+
+static gboolean check_required_keys(ZMapConfigIniContext context, ZMapConfigIniContextStanzaEntry stanza)
+{
+  CheckRequiredKeysStruct checking_data = {};
+
+  checking_data.context = context;
+  checking_data.stanza  = stanza;
+  checking_data.result  = TRUE;
+
+  g_list_foreach(stanza->keys, check_required, &checking_data);
+
+  return checking_data.result;
+}
+
+
+gboolean zMapConfigIniContextSave(ZMapConfigIniContext context)
+{
+  gboolean saved = TRUE;
+
+  saved = zMapConfigIniSaveUser(context->config);
+
+  return saved;
+}
+
+ZMapConfigIniContext zMapConfigIniContextDestroy(ZMapConfigIniContext context)
+{
+  if(context->error_message)
+    g_free(context->error_message);
+
+  memset(context, 0, sizeof(ZMapConfigIniContextStruct));
+
+  g_free(context);
+
+  return NULL;
+}
+
+
+
 gboolean zMapConfigIniContextGetValue(ZMapConfigIniContext context, 
-				      char *stanza_name,
-				      char *stanza_type,
-				      char *key_name,
-				      GValue **value_out)
+                              char *stanza_name,
+                              char *stanza_type,
+                              char *key_name,
+                              GValue **value_out)
 {
   gboolean obtained = FALSE;
   GValue *value = NULL;
@@ -867,24 +260,24 @@ gboolean zMapConfigIniContextGetValue(ZMapConfigIniContext context,
       type  = get_stanza_key_type(context, stanza_name, stanza_type, key_name);
       
       if((type != 0))
-	{
-	  if((obtained = zMapConfigIniGetValue(context->config, 
-					       stanza_name, key_name, 
-					       &value, type)))
-	    *value_out = value;
-	  else
-	    {
-	      context->error_message = g_strdup_printf("failed to get value for %s, %s",
-						       stanza_name, key_name);
-	      *value_out = NULL;
-	    }
-	}
+      {
+        if((obtained = zMapConfigIniGetValue(context->config, 
+                                     stanza_name, key_name, 
+                                     &value, type)))
+          *value_out = value;
+        else
+          {
+            context->error_message = g_strdup_printf("failed to get value for %s, %s",
+                                           stanza_name, key_name);
+            *value_out = NULL;
+          }
+      }
       else
-	{
-	  context->error_message = g_strdup_printf("failed to get type for %s, %s", 
-						   stanza_name, key_name);
-	  *value_out = NULL;
-	}
+      {
+        context->error_message = g_strdup_printf("failed to get type for %s, %s", 
+                                       stanza_name, key_name);
+        *value_out = NULL;
+      }
     }
 
   return obtained;
@@ -892,60 +285,60 @@ gboolean zMapConfigIniContextGetValue(ZMapConfigIniContext context,
 
 
 gboolean zMapConfigIniContextGetBoolean(ZMapConfigIniContext context,
-					 char *stanza_name,
-					 char *stanza_type,
-					 char *key_name,
-					 gboolean *value)
+                               char *stanza_name,
+                               char *stanza_type,
+                               char *key_name,
+                               gboolean *value)
 {
   GValue *value_out = NULL;
   gboolean success = FALSE;
 
   if(zMapConfigIniContextGetValue(context, 
-				  stanza_name, stanza_type, 
-				  key_name,    &value_out))
+                          stanza_name, stanza_type, 
+                          key_name,    &value_out))
     {
       if(value)
-	{
-	  if(G_VALUE_HOLDS_BOOLEAN(value_out))
-	    {
-	      *value  = g_value_get_boolean(value_out);
-	      success = TRUE;
-	    }
-
-	  g_value_unset(value_out);
-	  g_free(value_out);
-	  value_out = NULL;
-	}
+      {
+        if(G_VALUE_HOLDS_BOOLEAN(value_out))
+          {
+            *value  = g_value_get_boolean(value_out);
+            success = TRUE;
+          }
+
+        g_value_unset(value_out);
+        g_free(value_out);
+        value_out = NULL;
+      }
     }
 
   return success;
 }
 
 gboolean zMapConfigIniContextGetString(ZMapConfigIniContext context,
-					char *stanza_name,
-					char *stanza_type,
-					char *key_name,
-					char **value)
+                              char *stanza_name,
+                              char *stanza_type,
+                              char *key_name,
+                              char **value)
 {
   GValue *value_out = NULL;
   gboolean success = FALSE;
 
   if(zMapConfigIniContextGetValue(context, 
-				  stanza_name, stanza_type, 
-				  key_name,    &value_out))
+                          stanza_name, stanza_type, 
+                          key_name,    &value_out))
     {
       if(value)
-	{
-	  if(G_VALUE_HOLDS_STRING(value_out))
-	    {
-	      *value  = g_value_dup_string(value_out);
-	      success = TRUE;
-	    }
-
-	  g_value_unset(value_out);
-	  g_free(value_out);
-	  value_out = NULL;
-	}
+      {
+        if(G_VALUE_HOLDS_STRING(value_out))
+          {
+            *value  = g_value_dup_string(value_out);
+            success = TRUE;
+          }
+
+        g_value_unset(value_out);
+        g_free(value_out);
+        value_out = NULL;
+      }
     }
 
   return success;
@@ -953,40 +346,40 @@ gboolean zMapConfigIniContextGetString(ZMapConfigIniContext context,
 
 
 gboolean zMapConfigIniContextGetInt(ZMapConfigIniContext context,
-				     char *stanza_name,
-				     char *stanza_type,
-				     char *key_name,
-				     int  *value)
+                             char *stanza_name,
+                             char *stanza_type,
+                             char *key_name,
+                             int  *value)
 {
   GValue *value_out = NULL;
   gboolean success = FALSE;
 
   if(zMapConfigIniContextGetValue(context, 
-				  stanza_name, stanza_type, 
-				  key_name,    &value_out))
+                          stanza_name, stanza_type, 
+                          key_name,    &value_out))
     {
       if(value)
-	{
-	  if(G_VALUE_HOLDS_INT(value_out))
-	    {
-	      *value  = g_value_get_int(value_out);
-	      success = TRUE;
-	    }
-
-	  g_value_unset(value_out);
-	  g_free(value_out);
-	  value_out = NULL;
-	}
+      {
+        if(G_VALUE_HOLDS_INT(value_out))
+          {
+            *value  = g_value_get_int(value_out);
+            success = TRUE;
+          }
+
+        g_value_unset(value_out);
+        g_free(value_out);
+        value_out = NULL;
+      }
     }
 
   return success;
 }
 
 gboolean zMapConfigIniContextSetString(ZMapConfigIniContext context,
-				       char *stanza_name,
-				       char *stanza_type,
-				       char *key_name,
-				       char *value_str)
+                               char *stanza_name,
+                               char *stanza_type,
+                               char *key_name,
+                               char *value_str)
 {
   GType type = 0;
   gboolean set = TRUE;
@@ -1002,17 +395,17 @@ gboolean zMapConfigIniContextSetString(ZMapConfigIniContext context,
       g_value_set_static_string(&value, value_str);
 
       set = zMapConfigIniContextSetValue(context, stanza_name,
-					 key_name, &value);
+                               key_name, &value);
     }
   
   return set;
 }
 
 gboolean zMapConfigIniContextSetInt(ZMapConfigIniContext context,
-				    char *stanza_name,
-				    char *stanza_type,
-				    char *key_name,
-				    int   value_int)
+                            char *stanza_name,
+                            char *stanza_type,
+                            char *key_name,
+                            int   value_int)
 {
   GType type = 0;
   gboolean set = TRUE;
@@ -1028,7 +421,7 @@ gboolean zMapConfigIniContextSetInt(ZMapConfigIniContext context,
       g_value_set_int(&value, value_int);
 
       set = zMapConfigIniContextSetValue(context, stanza_name,
-					 key_name, &value);
+                               key_name, &value);
     }
   
   return set;
@@ -1036,10 +429,10 @@ gboolean zMapConfigIniContextSetInt(ZMapConfigIniContext context,
 
 
 gboolean zMapConfigIniContextSetBoolean(ZMapConfigIniContext context,
-					char *stanza_name,
-					char *stanza_type,
-					char *key_name,
-					gboolean value_bool)
+                              char *stanza_name,
+                              char *stanza_type,
+                              char *key_name,
+                              gboolean value_bool)
 {
   GType type = 0;
   gboolean set = TRUE;
@@ -1055,16 +448,16 @@ gboolean zMapConfigIniContextSetBoolean(ZMapConfigIniContext context,
       g_value_set_boolean(&value, value_bool);
 
       set = zMapConfigIniContextSetValue(context, stanza_name,
-					 key_name, &value);
+                               key_name, &value);
     }
   
   return set;
 }
 
 gboolean zMapConfigIniContextSetValue(ZMapConfigIniContext context,
-				      char *stanza_name,
-				      char *key_name,
-				      GValue *value)
+                              char *stanza_name,
+                              char *key_name,
+                              GValue *value)
 {
   gboolean set = TRUE;
 
@@ -1073,36 +466,6 @@ gboolean zMapConfigIniContextSetValue(ZMapConfigIniContext context,
   return set;
 }
 
-gboolean zMapConfigIniContextSave(ZMapConfigIniContext context)
-{
-  gboolean saved = TRUE;
-
-  saved = zMapConfigIniSaveUser(context->config);
-
-  return saved;
-}
-
-ZMapConfigIniContext zMapConfigIniContextDestroy(ZMapConfigIniContext context)
-{
-  if(context->error_message)
-    g_free(context->error_message);
-
-  memset(context, 0, sizeof(ZMapConfigIniContextStruct));
-
-  g_free(context);
-
-  return NULL;
-}
-
-
-
-
-
-/* 
- *                    Context internals
- */
-
-
 
 /* here we want to prefer exact matches of name and type */
 /* then we'll only match types with a name == * already in the list */
@@ -1117,14 +480,14 @@ static gint match_name_type(gconstpointer list_data, gconstpointer user_data)
     {
       /* Checking that the types match.  Necessary? */
       if(g_ascii_strcasecmp( query->stanza_type, list_entry->stanza_type ) == 0)
-	{
-	  query->keys = list_entry->keys;
-	  result = 0;		/* zero == found */
-	}
+      {
+        query->keys = list_entry->keys;
+        result = 0;           /* zero == found */
+      }
       else
-	result = 1;
+      result = 1;
     }
-  /* not and exact name match, do the types match? */
+  /* not an exact name match, do the types match? */
   else if(g_ascii_strcasecmp( query->stanza_type, list_entry->stanza_type ) == 0)
     {
       /* make sure the stanza_name == * in the list the context
@@ -1132,12 +495,12 @@ static gint match_name_type(gconstpointer list_data, gconstpointer user_data)
        * same basic type. e.g. the same list of keys/info/G_TYPE data
        * for keys... */
       if(g_ascii_strcasecmp(list_entry->stanza_name, "*") == 0)
-	{
-	  query->keys = list_entry->keys;
-	  result = 0;		/* zero == found */
-	}
+      {
+        query->keys = list_entry->keys;
+        result = 0;           /* zero == found */
+      }
       else
-	result = 1;
+      result = 1;
     }
   else
     result = -1;
@@ -1145,6 +508,8 @@ static gint match_name_type(gconstpointer list_data, gconstpointer user_data)
   return result;
 }
 
+
+
 static gint match_key(gconstpointer list_data, gconstpointer user_data)
 {
   ZMapConfigIniContextKeyEntry query = (ZMapConfigIniContextKeyEntry)user_data;
@@ -1160,10 +525,11 @@ static gint match_key(gconstpointer list_data, gconstpointer user_data)
   return result ;
 }
 
+
 static GType get_stanza_key_type(ZMapConfigIniContext context,
-				 char *stanza_name,
-				 char *stanza_type,
-				 char *key_name)
+                         char *stanza_name,
+                         char *stanza_type,
+                         char *key_name)
 {
   ZMapConfigIniContextStanzaEntryStruct user_request = {NULL};
   GList *entry_found = NULL;
@@ -1180,31 +546,12 @@ static GType get_stanza_key_type(ZMapConfigIniContext context,
       key_request.key = key_name;
 
       if((key_found = g_list_find_custom(user_request.keys, &key_request, match_key)))
-	type = key_request.type;
-      
+      type = key_request.type;
     }
 
   return type;
 }
 
-static ZMapConfigIniContextStanzaEntry get_stanza_with_type(ZMapConfigIniContext context,
-							    char *stanza_type)
-{
-  ZMapConfigIniContextStanzaEntryStruct user_request = {NULL};
-  ZMapConfigIniContextStanzaEntry stanza = NULL;
-  GList *entry_found;
-
-  user_request.stanza_name = "*";
-  user_request.stanza_type = stanza_type;
-
-  if((entry_found = g_list_find_custom(context->groups, &user_request, match_name_type)))
-    {
-      stanza = (ZMapConfigIniContextStanzaEntry)(entry_found->data);
-    }
-
-  return stanza;
-}
-
 static GList *copy_keys(ZMapConfigIniContextKeyEntryStruct *keys)
 {
   GList *new_keys = NULL;
@@ -1214,13 +561,13 @@ static GList *copy_keys(ZMapConfigIniContextKeyEntryStruct *keys)
       ZMapConfigIniContextKeyEntry k = NULL;
 
       if((k = g_new0(ZMapConfigIniContextKeyEntryStruct, 1)))
-	{
-	  k->key      = keys->key;
-	  k->type     = keys->type;
-	  k->required = keys->required;
-	  k->set_property = keys->set_property;
-	  new_keys    = g_list_append(new_keys, k);
-	}
+      {
+        k->key      = keys->key;
+        k->type     = keys->type;
+        k->required = keys->required;
+        k->set_property = keys->set_property;
+        new_keys    = g_list_append(new_keys, k);
+      }
 
       keys++;
     }
@@ -1228,258 +575,4 @@ static GList *copy_keys(ZMapConfigIniContextKeyEntryStruct *keys)
   return new_keys;
 }
 
-static void check_required(gpointer list_data, gpointer user_data)
-{
-  CheckRequiredKeys checking_data = (CheckRequiredKeys)user_data;
-  ZMapConfigIniContextKeyEntry key = (ZMapConfigIniContextKeyEntry)list_data;
-  ZMapConfigIniContextStanzaEntry stanza;
-  ZMapConfigIniContext context;
-  
-  context = checking_data->context;
-  stanza  = checking_data->stanza;
-
-  if(checking_data->result && !context->error_message && key->required)
-    {
-      GValue *value = NULL;
-      
-      if(zMapConfigIniGetValue(context->config, 
-			       stanza->stanza_name,
-			       key->key,
-			       &value, key->type))
-	{
-	  checking_data->result = TRUE;
-	  g_value_unset(value);
-	  g_free(value);
-	}
-      else
-	{
-	  checking_data->result = FALSE;
-	  context->error_message = g_strdup_printf("Failed to get required key '%s' in stanza %s",
-						   key->key, stanza->stanza_name);
-	}
-    }
-
-  return ;
-}
-
-static gboolean check_required_keys(ZMapConfigIniContext context, ZMapConfigIniContextStanzaEntry stanza)
-{
-  CheckRequiredKeysStruct checking_data = {};
-
-  checking_data.context = context;
-  checking_data.stanza  = stanza;
-  checking_data.result  = TRUE;
-
-  g_list_foreach(stanza->keys, check_required, &checking_data);
-
-  return checking_data.result;
-}
-
-
-
-/* Context Config Loading */
-typedef struct
-{
-  ZMapConfigIniContext context;
-  ZMapConfigIniContextStanzaEntry stanza;
-  
-  char *current_stanza_name;
-
-  ZMapConfigIniUserDataCreateFunc object_create_func;
-  gpointer current_object;
-
-  GList *object_list_out;
-} FetchReferencedStanzasStruct, *FetchReferencedStanzas;
-
-static void fill_stanza_key_value(gpointer list_data, gpointer user_data)
-{
-  FetchReferencedStanzas full_data = (FetchReferencedStanzas)user_data;
-  ZMapConfigIniContextKeyEntry key = (ZMapConfigIniContextKeyEntry)list_data;
-  GValue *value = NULL;
-
-  if (zMapConfigIniGetValue(full_data->context->config,
-			    full_data->current_stanza_name,
-			    key->key,
-			    &value,
-			    key->type))
-    {
-      if (key->set_property)
-	(key->set_property)(full_data->current_stanza_name, key->key, key->type, full_data->current_object, value);
-      else
-	zMapLogWarning("No set_property function for key '%s'", key->key);
-
-      g_free(value);
-    }
-
-  return ;
-}
-
-static void fetch_referenced_stanzas(gpointer list_data, gpointer user_data)
-{
-  FetchReferencedStanzas full_data = (FetchReferencedStanzas)user_data;
-  char *stanza_name = (char *)list_data;
-
-  full_data->current_stanza_name = stanza_name;
-
-  if (zMapConfigIniHasStanza(full_data->context->config, stanza_name) && (full_data->object_create_func))
-    {
-      if ((full_data->current_object = (full_data->object_create_func)()))
-	{
-	  /* get stanza keys */
-	  g_list_foreach(full_data->stanza->keys, fill_stanza_key_value, user_data);
-	  
-	  full_data->object_list_out = g_list_append(full_data->object_list_out,
-						     full_data->current_object);
-	}
-      else
-	zMapLogWarning("Object Create Function for stanza '%s'"
-		       " failed to return anything", stanza_name);
-    }
-
-  return ;
-}
-
-GList *get_child_stanza_names_as_list(ZMapConfigIniContext context,
-				      char *parent_name,
-				      char *parent_key)
-{
-  GList *list = NULL;
-  GValue *value = NULL;
-
-  if (parent_name && parent_key &&  zMapConfigIniGetValue(context->config,
-							  parent_name, parent_key,
-							  &value, G_TYPE_POINTER))
-    {
-      char **strings_list = NULL;
-
-      if ((strings_list = g_value_get_pointer(value)))
-	{
-	  char **ptr = strings_list;
-
-	  while (ptr && *ptr)
-	    {
-	      *ptr = g_strstrip(*ptr) ;
-	      list = g_list_append(list, *ptr) ;
-	      ptr++ ;
-	    }
-
-	  g_free(strings_list);
-	}
-    }
-  
-  return list;
-}
-
-static void free_source_names(gpointer list_data, gpointer unused_user_data)
-{
-  g_free(list_data);
-  return ;
-}
-
-GList *zMapConfigIniContextGetStanza(ZMapConfigIniContext context,
-				     ZMapConfigIniUserDataCreateFunc create,
-				     char *stanza_name, char *stanza_type)
-{
-  GList *list = NULL;
-
-  if(strcmp(stanza_name, stanza_type) == 0)
-    list = zMapConfigIniContextGetReferencedStanzas(context, create, 
-						    NULL, NULL, 
-						    NULL, stanza_type);
-
-  return list;
-}
-
-GList *zMapConfigIniContextGetReferencedStanzas(ZMapConfigIniContext context,
-						ZMapConfigIniUserDataCreateFunc object_create_func,
-						char *parent_name, 
-						char *parent_type, 
-						char *parent_key,
-						char *child_type)
-{
-  FetchReferencedStanzasStruct data = {NULL};
-  GList *sources      = NULL;
-  GList *source_names = NULL;
-
-  data.context = context;
-  data.stanza  = get_stanza_with_type(context, child_type);
-  data.object_create_func = object_create_func;
-
-  source_names = get_child_stanza_names_as_list(context, 
-						parent_name, 
-						parent_key);
-
-  if(!source_names)
-    source_names = g_list_append(source_names, g_strdup(child_type));
-
-  g_list_foreach(source_names, fetch_referenced_stanzas, &data);
-
-  g_list_foreach(source_names, free_source_names, NULL);
-  g_list_free(source_names);
-
-  sources = data.object_list_out;
-
-  return sources;
-}
-
-
-
-// somehow !! 'styles = one ; two ; three' becomes a list of pointers to strings before we get here
-GList *get_names_as_list(char *styles)
-{
-  GList *list = NULL;
-
-  char **strings_list = NULL;
-
-  if ((strings_list = g_strsplit(styles,";",0)))
-  {
-    char **ptr = strings_list;
-
-    while (ptr && *ptr)
-      {
-       *ptr = g_strstrip(*ptr) ;
-       list = g_list_append(list, *ptr);
-       ptr++ ;
-      }
-
-    g_free(strings_list);
-  }
-  return list;
-}
-
-
-/* see zMapConfigIniContextGetNamed(ZMapConfigIniContext context, char *stanza_name)
- * in zmapConfigIni.c, which is a wrapper for GetReferencedStanzas() above
- * For a list of styles in a source stanza we can't get these easily as we don't know the name 
- * of the source stanza, and GetNamed hard codes 'source' as the name of the parent.
- * so we find each stanza bu name from the list and add to our return list.
- */
-  
-GList *zMapConfigIniContextGetListedStanzas(ZMapConfigIniContext context,
-                                    ZMapConfigIniUserDataCreateFunc object_create_func,
-                                    char *styles_list,char *child_type)
-{
-  FetchReferencedStanzasStruct data = {NULL};
-  GList *styles      = NULL;
-  GList *style_names = NULL;
-
-  data.context = context;
-  data.stanza  = get_stanza_with_type(context, child_type);
-  data.object_create_func = object_create_func;
-
-  style_names = get_names_as_list(styles_list);
-
-  zMapAssert(style_names) ;
-
-  g_list_foreach(style_names, fetch_referenced_stanzas, &data);
-
-//  g_list_foreach(style_names, free_source_names, NULL);
-  g_list_free(style_names);
-
-  styles = data.object_list_out;
-
-  return styles;
-}
-
-
 
-- 
GitLab