Optimization of ini files load.
[midnight-commander.git] / lib / skin / ini-file.c
blobc4b5efa7975247fe4bba27e8d4087c4610ca7659
1 /*
2 Skins engine.
3 Reading and parse ini-files
5 Copyright (C) 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2009.
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include <config.h>
28 #include <string.h>
30 #include "internal.h"
31 #include "lib/fileloc.h"
32 #include "lib/util.h" /* exist_file() */
34 /*** global variables ****************************************************************************/
36 /*** file scope macro definitions ****************************************************************/
38 /*** file scope type declarations ****************************************************************/
40 /*** file scope variables ************************************************************************/
42 /*** file scope functions ************************************************************************/
44 /* --------------------------------------------------------------------------------------------- */
46 static gboolean
47 mc_skin_ini_file_load_search_in_dir (mc_skin_t * mc_skin, const gchar * base_dir)
49 char *file_name, *file_name2;
51 file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, mc_skin->name, NULL);
52 if (exist_file (file_name))
54 mc_skin->config = mc_config_init (file_name, TRUE);
55 g_free (file_name);
56 return (mc_skin->config != NULL);
58 g_free (file_name);
60 file_name2 = g_strdup_printf ("%s.ini", mc_skin->name);
61 file_name = g_build_filename (base_dir, MC_SKINS_SUBDIR, file_name2, NULL);
62 g_free (file_name2);
64 if (exist_file (file_name))
66 mc_skin->config = mc_config_init (file_name, TRUE);
67 g_free (file_name);
68 return (mc_skin->config != NULL);
70 g_free (file_name);
71 return FALSE;
74 /* --------------------------------------------------------------------------------------------- */
75 /*** public functions ****************************************************************************/
76 /* --------------------------------------------------------------------------------------------- */
78 gboolean
79 mc_skin_ini_file_load (mc_skin_t * mc_skin)
81 char *file_name;
83 file_name = g_path_get_basename (mc_skin->name);
84 if (file_name == NULL)
85 return FALSE;
87 if (strcmp (file_name, mc_skin->name) != 0)
89 g_free (file_name);
90 if (!g_path_is_absolute (mc_skin->name))
91 return FALSE;
92 mc_skin->config = mc_config_init (mc_skin->name, TRUE);
93 return (mc_skin->config != NULL);
95 g_free (file_name);
97 /* ${XDG_DATA_HOME}/mc/skins/ */
98 if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_config_get_data_path ()))
99 return TRUE;
101 /* /etc/mc/skins/ */
102 if (mc_skin_ini_file_load_search_in_dir (mc_skin, mc_global.sysconfig_dir))
103 return TRUE;
105 /* /usr/share/mc/skins/ */
106 return mc_skin_ini_file_load_search_in_dir (mc_skin, mc_global.share_data_dir);
109 /* --------------------------------------------------------------------------------------------- */
111 gboolean
112 mc_skin_ini_file_parse (mc_skin_t * mc_skin)
114 mc_skin->description =
115 mc_config_get_string (mc_skin->config, "skin", "description", "- no description -");
116 if (!mc_skin_color_parse_ini_file (mc_skin))
117 return FALSE;
119 mc_skin_lines_parse_ini_file (mc_skin);
120 mc_skin->have_256_colors = mc_config_get_bool (mc_skin->config, "skin", "256colors", FALSE);
122 return TRUE;
125 /* --------------------------------------------------------------------------------------------- */
127 void
128 mc_skin_set_hardcoded_skin (mc_skin_t * mc_skin)
130 mc_skin->config = mc_config_init (NULL, TRUE);
132 mc_config_set_string (mc_skin->config, "skin", "description", "hardcoded skin");
134 mc_skin_hardcoded_ugly_lines (mc_skin);
135 mc_skin_hardcoded_blackwhite_colors (mc_skin);
138 /* --------------------------------------------------------------------------------------------- */