Ticket #1897: Build breaks on ignored return values
[free-mc.git] / lib / filehighlight / ini-file-read.c
blob4edc7b4b139c7a41b9c5aed8e35909c2bf30a43c
1 /*
2 File highlight plugin.
3 Reading and parse rules from ini-files
5 Copyright (C) 2009 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2009.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software; you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of the
15 License, or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be
18 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
19 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 MA 02110-1301, USA.
28 #include <config.h>
29 #include <string.h>
31 #include "lib/global.h"
32 #include "lib/fileloc.h"
33 #include "lib/strescape.h"
34 #include "lib/skin.h"
35 #include "lib/filehighlight.h"
37 #include "src/main.h"
39 #include "internal.h"
41 /*** global variables ****************************************************************************/
43 extern mc_skin_t mc_skin__default;
45 /*** file scope macro definitions ****************************************************************/
47 /*** file scope type declarations ****************************************************************/
49 /*** file scope variables ************************************************************************/
51 /*** file scope functions ************************************************************************/
52 /* --------------------------------------------------------------------------------------------- */
54 static void
55 mc_fhl_parse_fill_color_info (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, const gchar * group_name)
57 (void) fhl;
58 mc_filter->color_pair_index = mc_skin_color_get ("filehighlight", group_name);
61 /* --------------------------------------------------------------------------------------------- */
63 static gboolean
64 mc_fhl_parse_get_file_type_id (mc_fhl_t * fhl, const gchar * group_name)
66 mc_fhl_filter_t *mc_filter;
68 const gchar *types[] = {
69 "FILE", "FILE_EXE",
70 "DIR", "LINK_DIR",
71 "LINK", "HARDLINK", "SYMLINK",
72 "STALE_LINK",
73 "DEVICE", "DEVICE_BLOCK", "DEVICE_CHAR",
74 "SPECIAL", "SPECIAL_SOCKET", "SPECIAL_FIFO", "SPECIAL_DOOR",
75 NULL
77 int i;
78 gchar *param_type = mc_config_get_string (fhl->config, group_name, "type", "");
80 if (*param_type == '\0') {
81 g_free (param_type);
82 return FALSE;
85 for (i = 0; types[i] != NULL; i++) {
86 if (strcmp (types[i], param_type) == 0)
87 break;
89 g_free (param_type);
90 if (types[i] == NULL)
91 return FALSE;
93 mc_filter = g_new0 (mc_fhl_filter_t, 1);
94 mc_filter->type = MC_FLHGH_T_FTYPE;
95 mc_filter->file_type = (mc_flhgh_ftype_type) i;
96 mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
98 g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
99 return TRUE;
102 /* --------------------------------------------------------------------------------------------- */
104 static gboolean
105 mc_fhl_parse_get_regexp (mc_fhl_t * fhl, const gchar * group_name)
107 mc_fhl_filter_t *mc_filter;
108 gchar *regexp = mc_config_get_string (fhl->config, group_name, "regexp", "");
110 if (*regexp == '\0') {
111 g_free (regexp);
112 return FALSE;
115 mc_filter = g_new0 (mc_fhl_filter_t, 1);
116 mc_filter->type = MC_FLHGH_T_FREGEXP;
117 mc_filter->search_condition = mc_search_new (regexp, -1);
118 mc_filter->search_condition->is_case_sentitive = TRUE;
119 mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
121 mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
122 g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
123 g_free (regexp);
124 return TRUE;
128 /* --------------------------------------------------------------------------------------------- */
130 static gboolean
131 mc_fhl_parse_get_extensions (mc_fhl_t * fhl, const gchar * group_name)
133 mc_fhl_filter_t *mc_filter;
134 gchar **exts, **exts_orig;
135 gchar *esc_ext;
136 gsize exts_size;
137 GString *buf = g_string_new ("");
139 exts_orig = exts =
140 mc_config_get_string_list (fhl->config, group_name, "extensions", &exts_size);
142 if (exts_orig == NULL)
143 return FALSE;
145 if (exts_orig[0] == NULL) {
146 g_strfreev (exts_orig);
147 return FALSE;
150 while (*exts != NULL) {
151 esc_ext = strutils_regex_escape (*exts);
152 if (buf->len != 0)
153 g_string_append_c (buf, '|');
154 g_string_append (buf, esc_ext);
155 g_free (esc_ext);
156 exts++;
158 g_strfreev (exts_orig);
159 esc_ext = g_string_free (buf, FALSE);
160 buf = g_string_new (".*\\.(");
161 g_string_append (buf, esc_ext);
162 g_string_append (buf, ")$");
163 g_free (esc_ext);
165 mc_filter = g_new0 (mc_fhl_filter_t, 1);
166 mc_filter->type = MC_FLHGH_T_FREGEXP;
167 mc_filter->search_condition = mc_search_new (buf->str, -1);
168 mc_filter->search_condition->is_case_sentitive =
169 mc_config_get_bool (fhl->config, group_name, "extensions_case", TRUE);
170 mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
172 mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
173 g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
174 g_string_free (buf, TRUE);
175 return TRUE;
178 /* --------------------------------------------------------------------------------------------- */
179 /*** public functions ****************************************************************************/
180 /* --------------------------------------------------------------------------------------------- */
183 gboolean
184 mc_fhl_read_ini_file (mc_fhl_t * fhl, const gchar * filename)
186 if (fhl == NULL || filename == NULL || !exist_file (filename))
187 return FALSE;
189 if (fhl->config) {
191 if (!mc_config_read_file (fhl->config, filename))
192 return FALSE;
194 return TRUE;
197 fhl->config = mc_config_init (filename);
199 if (fhl->config == NULL)
200 return FALSE;
202 return TRUE;
206 /* --------------------------------------------------------------------------------------------- */
208 gboolean
209 mc_fhl_init_from_standart_files (mc_fhl_t * fhl)
211 gchar *name;
213 /* ${datadir}/mc/filehighlight.ini */
214 name = concat_dir_and_file (mc_home_alt, MC_FHL_INI_FILE);
215 if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) {
216 g_free (name);
217 return FALSE;
219 g_free (name);
221 /* ${sysconfdir}/mc/filehighlight.ini */
222 name = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
223 if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) {
224 g_free (name);
225 return FALSE;
227 g_free (name);
229 /* ~/.mc/filehighlight.ini */
230 name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FHL_INI_FILE, NULL);
232 if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) {
233 g_free (name);
234 return FALSE;
236 g_free (name);
238 return TRUE;
241 /* --------------------------------------------------------------------------------------------- */
243 gboolean
244 mc_fhl_parse_ini_file (mc_fhl_t * fhl)
246 gchar **group_names, **orig_group_names;
248 mc_fhl_array_free (fhl);
249 fhl->filters = g_ptr_array_new ();
251 orig_group_names = group_names = mc_config_get_groups (fhl->config, NULL);
253 if (group_names == NULL)
254 return FALSE;
256 while (*group_names) {
258 if (mc_config_has_param (fhl->config, *group_names, "type")) {
259 /* parse filetype filter */
260 mc_fhl_parse_get_file_type_id (fhl, *group_names);
262 if (mc_config_has_param (fhl->config, *group_names, "regexp")) {
263 /* parse regexp filter */
264 mc_fhl_parse_get_regexp (fhl, *group_names);
266 if (mc_config_has_param (fhl->config, *group_names, "extensions")) {
267 /* parse extensions filter */
268 mc_fhl_parse_get_extensions (fhl, *group_names);
270 group_names++;
273 g_strfreev (orig_group_names);
274 return TRUE;
277 /* --------------------------------------------------------------------------------------------- */