LCOV - code coverage report
Current view: top level - libcdpfgl - configuration.c (source / functions) Hit Total Coverage
Test: coverage-libcdpfgl.info Lines: 68 106 64.2 %
Date: 2016-02-03 22:31:46 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
       2             : /*
       3             :  *    configuration.c
       4             :  *    This file is part of "Sauvegarde" project.
       5             :  *
       6             :  *    (C) Copyright 2014 - 2016 Olivier Delhomme
       7             :  *     e-mail : olivier.delhomme@free.fr
       8             :  *
       9             :  *    "Sauvegarde" is free software: you can redistribute it and/or modify
      10             :  *    it under the terms of the GNU General Public License as published by
      11             :  *    the Free Software Foundation, either version 3 of the License, or
      12             :  *    (at your option) any later version.
      13             :  *
      14             :  *    "Sauvegarde" is distributed in the hope that it will be useful,
      15             :  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :  *    GNU General Public License for more details.
      18             :  *
      19             :  *    You should have received a copy of the GNU General Public License
      20             :  *    along with "Sauvegarde".  If not, see <http://www.gnu.org/licenses/>
      21             :  */
      22             : 
      23             : /**
      24             :  * @file configuration.c
      25             :  * This file contains the functions to deal with the configuration file
      26             :  * of the "Sauvegarde" programs.
      27             :  */
      28             : 
      29             : #include "libcdpfgl.h"
      30             : 
      31             : /**
      32             :  * Gets the probable filename for the configuration file of sauvegarde
      33             :  * project. This is needed when one wants to install the project in an
      34             :  * uncommon location such as a homedir for instance.
      35             :  * @param progname is the name of the program we want to search for in the
      36             :  *        user's path
      37             :  * @param default configuration file name that should be a const string.
      38             :  * @returns a gchar * which contain the filename of the configuration file
      39             :  *          relative to progname or NULL if something went wrong.
      40             :  */
      41          17 : gchar *get_probable_etc_path(gchar *progname, const gchar *configfile)
      42             : {
      43          17 :     gchar *abs_path = NULL;
      44          17 :     gchar *path = NULL;
      45          17 :     const gchar * const *system_dirs = NULL;
      46          17 :     gint i = 0;
      47          17 :     gboolean ok = FALSE;
      48             : 
      49          17 :     if (progname != NULL && configfile != NULL)
      50             :         {
      51             :             /* the first location of the program in the path */
      52          17 :             abs_path = g_find_program_in_path(progname);
      53          17 :             if (abs_path != NULL)
      54             :                 {
      55           5 :                     path =  g_build_path(G_DIR_SEPARATOR_S, g_path_get_dirname(abs_path), "..", "etc", "cdpfgl", configfile, NULL);
      56           5 :                     free_variable(abs_path);
      57             : 
      58           5 :                     if (file_exists(path) == FALSE)
      59             :                         {
      60           0 :                             free_variable(path);
      61           0 :                             system_dirs = g_get_system_config_dirs();
      62           0 :                             i = 0;
      63           0 :                             while (system_dirs[i] != NULL && ok == FALSE)
      64             :                                 {
      65           0 :                                     path =  g_build_path(G_DIR_SEPARATOR_S, system_dirs[i], "cdpfgl", configfile, NULL);
      66             : 
      67           0 :                                     if (file_exists(path) == FALSE)
      68             :                                         {
      69           0 :                                             free_variable(path);
      70             :                                         }
      71             :                                     else
      72             :                                         {
      73             :                                             ok = TRUE;
      74             :                                         }
      75           0 :                                     i++;
      76             :                                 }
      77             :                         }
      78             :                 }
      79             :         }
      80             : 
      81          17 :     return path;
      82             : }
      83             : 
      84             : 
      85             : /**
      86             :  * Reads a string from keyname key in the group grouname from keyfile file
      87             :  * and displays errormsg in case of an error
      88             :  * @param keyfile : the opened keyfile to read from
      89             :  * @param filename : the filename of the keyfile file
      90             :  * @param groupname : the groupname where to look for the key
      91             :  * @param keyname : the key to read the string from
      92             :  * @param errormsg : the error message to be displayed in case of an error
      93             :  * @returns the string read at the keyname in the groupname of keyfile
      94             :  *          file.
      95             :  */
      96          20 : gchar *read_string_from_file(GKeyFile *keyfile, gchar *filename, gchar *groupname, gchar *keyname, gchar *errormsg)
      97             : {
      98          20 :     gchar *a_string = NULL;   /** the string to be read */
      99          20 :     GError *error = NULL;     /** Glib error handling   */
     100             : 
     101          20 :      if (g_key_file_has_key(keyfile, groupname, keyname, &error) == TRUE)
     102             :         {
     103             : 
     104          20 :             a_string = g_key_file_get_string(keyfile, groupname, keyname, &error);
     105             : 
     106          20 :             if (error != NULL)
     107             :                 {
     108           0 :                     print_error(__FILE__, __LINE__,  "%s %s: %s", errormsg, filename, error->message);
     109           0 :                     error = free_error(error);
     110             :                 }
     111             :         }
     112           0 :     else if (error != NULL)
     113             :         {
     114           0 :             print_error(__FILE__, __LINE__, _("Error while looking for %s key in configuration file: %s\n"), keyname, error->message);
     115           0 :             error = free_error(error);
     116             :         }
     117             : 
     118          20 :     return a_string;
     119             : }
     120             : 
     121             : 
     122             : /**
     123             :  * Reads a gint64 from keyname key in the group grouname from keyfile file
     124             :  * and displays errormsg in case of an error
     125             :  * @param keyfile : the opened keyfile to read from
     126             :  * @param filename : the filename of the keyfile file
     127             :  * @param groupname : the groupname where to look for the key
     128             :  * @param keyname : the key to read the gint64 from
     129             :  * @param errormsg : the error message to be displayed in case of an error
     130             :  * @param def_value : the default value for the key.
     131             :  * @returns the gint64 read at the keyname in the groupname of keyfile
     132             :  *          file or 0;
     133             :  */
     134           1 : gint64 read_int64_from_file(GKeyFile *keyfile, gchar *filename, gchar *groupname, gchar *keyname, gchar *errormsg, gint64 def_value)
     135             : {
     136           1 :     gint64 num = def_value; /** Number to be read     */
     137           1 :     GError *error = NULL;   /** Glib error handling   */
     138             : 
     139           1 :      if (g_key_file_has_key(keyfile, groupname, keyname, &error) == TRUE)
     140             :         {
     141           0 :             num =  g_key_file_get_int64(keyfile, groupname, keyname, &error);
     142             : 
     143           0 :             if (error != NULL)
     144             :                 {
     145           0 :                     print_error(__FILE__, __LINE__, "%s %s: %s", errormsg, filename, error->message);
     146           0 :                     error = free_error(error);
     147             :                 }
     148             :         }
     149           1 :     else if (error != NULL)
     150             :         {
     151           0 :             print_error(__FILE__, __LINE__, _("Error while looking for %s key in configuration file: %s\n"), keyname, error->message);
     152           0 :             error = free_error(error);
     153             :         }
     154             :     else
     155             :         {
     156           1 :             fprintf(stdout, _("Key '%s' in group '%s' of configuration file '%s' not found.\n"), keyname, groupname, filename);
     157             :         }
     158             : 
     159           1 :     return num;
     160             : }
     161             : 
     162             : 
     163             : /**
     164             :  * Reads an integer from keyname key in the group grouname from keyfile file
     165             :  * and displays errormsg in case of an error
     166             :  * @param keyfile : the opened keyfile to read from
     167             :  * @param filename : the filename of the keyfile file
     168             :  * @param groupname : the groupname where to look for the key
     169             :  * @param keyname : the key to read the gint from
     170             :  * @param errormsg : the error message to be displayed in case of an error
     171             :  * @param def_value : the default value for the key.
     172             :  * @returns the gint read at the keyname in the groupname of keyfile
     173             :  *          file or 0;
     174             :  */
     175          21 : gint read_int_from_file(GKeyFile *keyfile, gchar *filename, gchar *groupname, gchar *keyname, gchar *errormsg, gint def_value)
     176             : {
     177          21 :     gint num = def_value;  /** Number to be read     */
     178          21 :     GError *error = NULL;  /** Glib error handling   */
     179             : 
     180          21 :     if (g_key_file_has_key(keyfile, groupname, keyname, &error) == TRUE)
     181             :         {
     182          21 :             num =  g_key_file_get_integer(keyfile, groupname, keyname, &error);
     183             : 
     184          21 :             if (error != NULL)
     185             :                 {
     186           0 :                     print_error(__FILE__, __LINE__, "%s %s: %s", errormsg, filename, error->message);
     187           0 :                     error = free_error(error);
     188             :                 }
     189             :         }
     190           0 :     else if (error != NULL)
     191             :         {
     192           0 :             print_error(__FILE__, __LINE__,  _("Error while looking for %s key in configuration file: %s\n"), keyname, error->message);
     193           0 :             error = free_error(error);
     194             :         }
     195             :     else
     196             :         {
     197           0 :             fprintf(stdout, _("Key '%s' in group '%s' of configuration file '%s' not found.\n"), keyname, groupname, filename);
     198             :         }
     199             : 
     200          21 :     return num;
     201             : }
     202             : 
     203             : 
     204             : /**
     205             :  * Reads an integer from keyname key in the group grouname from keyfile file
     206             :  * and displays errormsg in case of an error
     207             :  * @param keyfile : the opened keyfile to read from
     208             :  * @param filename : the filename of the keyfile file
     209             :  * @param groupname : the groupname where to look for the key
     210             :  * @param keyname : the key to read the gboolean from
     211             :  * @param errormsg : the error message to be displayed in case of an error
     212             :  * @returns the boolean read at the keyname in the groupname of keyfile
     213             :  *          file or FALSE;
     214             :  */
     215          20 : gboolean read_boolean_from_file(GKeyFile *keyfile, gchar *filename, gchar *groupname, gchar *keyname, gchar *errormsg)
     216             : {
     217          20 :     gboolean bool = FALSE; /** Boolean to be read    */
     218          20 :     GError *error = NULL;  /** Glib error handling   */
     219             : 
     220          20 :     if (g_key_file_has_key(keyfile, groupname, keyname, &error) == TRUE)
     221             :         {
     222          20 :             bool =  g_key_file_get_boolean(keyfile, groupname, keyname, &error);
     223             : 
     224          20 :             if (error != NULL)
     225             :                 {
     226           0 :                     print_error(__FILE__, __LINE__, "%s %s: %s", errormsg, filename, error->message);
     227           0 :                     error = free_error(error);
     228             :                 }
     229             :         }
     230           0 :     else if (error != NULL)
     231             :         {
     232           0 :             print_error(__FILE__, __LINE__,  _("Error while looking for %s key in configuration file: %s\n"), keyname, error->message);
     233           0 :             error = free_error(error);
     234             :         }
     235             :     else
     236             :         {
     237           0 :             fprintf(stdout, _("Key '%s' in group '%s' of configuration file '%s' not found.\n"), keyname, groupname, filename);
     238             :         }
     239             : 
     240          20 :     return bool;
     241             : }
     242             : 
     243             : 
     244             : /**
     245             :  * This functions converts a gchar ** array of directories to a GSList of
     246             :  * gchar * paths.
     247             :  * The function appends to the list first_list (if it exists - it may be
     248             :  * NULL) each entry of the array so elements are in the same order in the
     249             :  * array and in the list.
     250             :  * @param array is a gchar * array.
     251             :  * @param first_list is a list that may already contain some elements and
     252             :  *        to which we will add all the elements of 'array' array.
     253             :  * @returns a newly allocated GSList that may be freed when no longer
     254             :  *          needed or NULL if array is NULL.
     255             :  */
     256           6 : GSList *convert_gchar_array_to_GSList(gchar **array, GSList *first_list)
     257             : {
     258           6 :     gchar *a_string = NULL;    /** gchar * that is read in the array      */
     259           6 :     GSList *list = first_list; /** The list to be returned (may be NULL)  */
     260           6 :     gint i = 0;
     261           6 :     gint num = 0;              /** Number of elements in the array if any */
     262             : 
     263           6 :     if (array != NULL)
     264             :         {
     265           2 :             num = g_strv_length(array);
     266             : 
     267          10 :             for (i = 0; i < num; i++)
     268             :                 {
     269           8 :                     a_string = normalize_directory(array[i]);
     270           8 :                     list = g_slist_append(list, a_string);
     271             :                 }
     272             :         }
     273             : 
     274           6 :     return list;
     275             : }
     276             : 
     277             : 
     278             : /**
     279             :  * Reads a list of gchar * from keyname key in the group grouname from
     280             :  * keyfile file and displays errormsg in case of an error
     281             :  * @param keyfile : the opened keyfile to read from
     282             :  * @param filename : the filename of the keyfile file
     283             :  * @param groupname : the groupname where to look for the key
     284             :  * @param keyname : the key to read the list of gchar * from
     285             :  * @param errormsg : the error message to be displayed in case of an error
     286             :  * @returns the list of gchar * read at the keyname in the groupname of
     287             :  *          keyfile file or NULL;
     288             :  */
     289           2 : GSList *read_list_from_file(GKeyFile *keyfile, gchar *filename, gchar *groupname, gchar *keyname, gchar *errormsg)
     290             : {
     291           2 :     GSList *a_list = NULL;         /** list to be returned                                */
     292           2 :     GError *error = NULL;          /** Glib error handling                                */
     293           2 :     gchar **dirname_array = NULL;  /** array of dirnames read into the configuration file */
     294             : 
     295             : 
     296           2 :     if (g_key_file_has_key(keyfile, groupname, keyname, &error) == TRUE)
     297             :         {
     298           2 :             dirname_array = g_key_file_get_string_list(keyfile, groupname, keyname, NULL, &error);
     299             : 
     300           2 :             if (dirname_array != NULL)
     301             :                 {
     302           2 :                     a_list = convert_gchar_array_to_GSList(dirname_array, a_list);
     303             :                     /* The array is no longer needed (everything has been copied with g_strdup) */
     304           2 :                     g_strfreev(dirname_array);
     305             :                 }
     306           0 :             else if (error != NULL)
     307             :                 {
     308           0 :                     print_error(__FILE__, __LINE__, _("%s %s: %s\n"), errormsg, filename, error->message);
     309           0 :                     error = free_error(error);
     310             :                 }
     311             :         }
     312           0 :     else if (error != NULL)
     313             :         {
     314           0 :             print_error(__FILE__, __LINE__, _("Error while looking for %s key in configuration file: %s\n"), keyname, error->message);
     315           0 :             error = free_error(error);
     316             :         }
     317             :     else
     318             :         {
     319           0 :             fprintf(stdout, _("Key '%s' in group '%s' of configuration file '%s' not found.\n"), keyname, groupname, filename);
     320             :         }
     321             : 
     322           2 :     return a_list;
     323             : }
     324             : 
     325             : 
     326             : /**
     327             :  * Reads debug mode in keyfile
     328             :  * @param keyfile is the GKeyFile structure that is used by glib to read
     329             :  *        groups and keys from.
     330             :  * @param filename : the filename of the configuration file to read from
     331             :  */
     332          19 : void read_debug_mode_from_file(GKeyFile *keyfile, gchar *filename)
     333             : {
     334          19 :     gboolean debug = FALSE;
     335             : 
     336          19 :     if (keyfile != NULL && filename != NULL && g_key_file_has_group(keyfile, GN_ALL) == TRUE)
     337             :         {
     338             :              /* Reading in section [All] the debug mode */
     339          19 :             debug = read_boolean_from_file(keyfile, filename, GN_ALL, KN_DEBUG_MODE, _("Could not load debug mode configuration from file."));
     340             : 
     341          19 :             set_debug_mode(debug);
     342             :         }
     343          19 : }
     344             : 
     345             : 
     346             : 
     347             : 

Generated by: LCOV version 1.11