"Fossies" - the Fresh Open Source Software Archive

Member "gvm-libs-11.0.1/base/settings.c" (12 May 2020, 4998 Bytes) of package /linux/misc/openvas/gvm-libs-11.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "settings.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.0-beta2_vs_1.0.0.

    1 /* Copyright (C) 2010-2019 Greenbone Networks GmbH
    2  *
    3  * SPDX-License-Identifier: GPL-2.0-or-later
    4  *
    5  * This program is free software; you can redistribute it and/or
    6  * modify it under the terms of the GNU General Public License
    7  * as published by the Free Software Foundation; either version 2
    8  * of the License, or (at your option) any later version.
    9  *
   10  * This program is distributed in the hope that it will be useful,
   11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  * GNU General Public License for more details.
   14  *
   15  * You should have received a copy of the GNU General Public License
   16  * along with this program; if not, write to the Free Software
   17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
   18  */
   19 
   20 /**
   21  * @file settings.c
   22  * @brief Implementation of API to handle configuration file management
   23  */
   24 
   25 #include "settings.h"
   26 
   27 #include <stdio.h>
   28 #include <string.h> /* for strlen */
   29 
   30 /**
   31  * @brief Initialise a settings struct from a file.
   32  *
   33  * @param[in]  settings  Settings.
   34  * @param[in]  filename  Complete name of the configuration file.
   35  * @param[in]  group     Name of the group in the file.
   36  *
   37  * @return 0 success, -1 error.
   38  */
   39 int
   40 settings_init_from_file (settings_t *settings, const gchar *filename,
   41                          const gchar *group)
   42 {
   43   GError *error = NULL;
   44 
   45   if (filename == NULL || group == NULL)
   46     return -1;
   47 
   48   gchar *contents = NULL;
   49 
   50   if (!g_file_get_contents (filename, &contents, NULL, &error))
   51     {
   52       g_error_free (error);
   53       return -1;
   54     }
   55 
   56   if (contents != NULL)
   57     {
   58       gchar *contents_with_group = g_strjoin ("\n", "[Misc]", contents, NULL);
   59       settings->key_file = g_key_file_new ();
   60 
   61       if (!g_key_file_load_from_data (
   62             settings->key_file, contents_with_group,
   63             strlen (contents_with_group),
   64             G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error))
   65         {
   66           g_warning ("Failed to load configuration from %s: %s", filename,
   67                      error->message);
   68           g_error_free (error);
   69           g_free (contents_with_group);
   70           g_free (contents);
   71           return -1;
   72         }
   73       g_free (contents_with_group);
   74       g_free (contents);
   75     }
   76 
   77   settings->group_name = g_strdup (group);
   78   settings->file_name = g_strdup (filename);
   79 
   80   return 0;
   81 }
   82 
   83 /**
   84  * @brief Cleanup a settings structure.
   85  *
   86  * @param[in]  settings  Settings structure.
   87  */
   88 void
   89 settings_cleanup (settings_t *settings)
   90 {
   91   g_free (settings->group_name);
   92   g_free (settings->file_name);
   93   g_key_file_free (settings->key_file);
   94 }
   95 
   96 /**
   97  * @brief Initialise a settings iterator from a file.
   98  *
   99  * @param[in]  iterator  Settings iterator.
  100  * @param[in]  filename  Complete name of the configuration file.
  101  * @param[in]  group     Name of the group in the file.
  102  *
  103  * @return 0 success, -1 error.
  104  */
  105 int
  106 init_settings_iterator_from_file (settings_iterator_t *iterator,
  107                                   const gchar *filename, const gchar *group)
  108 {
  109   int ret;
  110   gsize keys_length;
  111   GError *error = NULL;
  112 
  113   ret = settings_init_from_file (&iterator->settings, filename, group);
  114   if (ret)
  115     return ret;
  116 
  117   iterator->keys = g_key_file_get_keys (iterator->settings.key_file, group,
  118                                         &keys_length, &error);
  119 
  120   if (iterator->keys == NULL)
  121     {
  122       if (error)
  123         {
  124           g_warning ("Failed to retrieve keys of group %s from %s: %s", group,
  125                      filename, error->message);
  126           g_error_free (error);
  127         }
  128       g_key_file_free (iterator->settings.key_file);
  129       return -1;
  130     }
  131 
  132   iterator->current_key = iterator->keys - 1;
  133   iterator->last_key = iterator->keys + keys_length - 1;
  134 
  135   return 0;
  136 }
  137 
  138 /**
  139  * @brief Cleanup a settings iterator.
  140  *
  141  * @param[in]  iterator  Settings iterator.
  142  */
  143 void
  144 cleanup_settings_iterator (settings_iterator_t *iterator)
  145 {
  146   g_strfreev (iterator->keys);
  147   settings_cleanup (&iterator->settings);
  148 }
  149 
  150 /**
  151  * @brief Increment an iterator.
  152  *
  153  * @param[in]  iterator  Settings iterator.
  154  *
  155  * @return TRUE if there was a next item, else FALSE.
  156  */
  157 gboolean
  158 settings_iterator_next (settings_iterator_t *iterator)
  159 {
  160   if (iterator->current_key == iterator->last_key)
  161     return FALSE;
  162   iterator->current_key++;
  163   return TRUE;
  164 }
  165 
  166 /**
  167  * @brief Get the name from a settings iterator.
  168  *
  169  * @param[in]  iterator  Settings iterator.
  170  *
  171  * @return Name of current key.
  172  */
  173 const gchar *
  174 settings_iterator_name (settings_iterator_t *iterator)
  175 {
  176   return *iterator->current_key;
  177 }
  178 
  179 /**
  180  * @brief Get the value from a settings iterator.
  181  *
  182  * @param[in]  iterator  Settings iterator.
  183  *
  184  * @return Value of current key.
  185  */
  186 const gchar *
  187 settings_iterator_value (settings_iterator_t *iterator)
  188 {
  189   return g_key_file_get_value (iterator->settings.key_file,
  190                                iterator->settings.group_name,
  191                                *iterator->current_key, NULL);
  192 }