continue develop of skins.
[midnight-commander.git] / src / filehighlight / ini-file-read.c
blobbf35728411e0668eb31fa1b9ed214b23692a441c
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 "../src/global.h"
32 #include "../src/main.h"
33 #include "../src/strescape.h"
34 #include "../src/skin/skin.h"
35 #include "fhl.h"
36 #include "internal.h"
38 /*** global variables ****************************************************************************/
40 extern mc_skin_t mc_skin__default;
42 /*** file scope macro definitions ****************************************************************/
44 /*** file scope type declarations ****************************************************************/
46 /*** file scope variables ************************************************************************/
48 /*** file scope functions ************************************************************************/
49 /* --------------------------------------------------------------------------------------------- */
51 static void
52 mc_fhl_parse_fill_color_info (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, const gchar * group_name)
54 (void) fhl;
55 mc_filter->color_pair_index = mc_skin_color_get("filehighlight", group_name);
58 /* --------------------------------------------------------------------------------------------- */
60 static gboolean
61 mc_fhl_parse_get_file_type_id (mc_fhl_t * fhl, const gchar * group_name)
63 mc_fhl_filter_t *mc_filter;
65 const gchar *types[] = {
66 "FILE", "FILE_EXE",
67 "DIR", "LINK_DIR",
68 "LINK", "HARDLINK", "SYMLINK",
69 "STALE_LINK",
70 "DEVICE", "DEVICE_BLOCK", "DEVICE_CHAR",
71 "SPECIAL", "SPECIAL_SOCKET", "SPECIAL_FIFO", "SPECIAL_DOOR",
72 NULL
74 int i;
75 gchar *param_type = mc_config_get_string (fhl->config, group_name, "type", "");
77 if (*param_type == '\0') {
78 g_free (param_type);
79 return FALSE;
82 for (i = 0; types[i] != NULL; i++) {
83 if (strcmp (types[i], param_type) == 0)
84 break;
86 g_free (param_type);
87 if (types[i] == NULL)
88 return FALSE;
90 mc_filter = g_new0 (mc_fhl_filter_t, 1);
91 mc_filter->type = MC_FLHGH_T_FTYPE;
92 mc_filter->file_type = (mc_flhgh_ftype_type) i;
93 mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
95 g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
96 return TRUE;
99 /* --------------------------------------------------------------------------------------------- */
101 static gboolean
102 mc_fhl_parse_get_regexp (mc_fhl_t * fhl, const gchar * group_name)
104 mc_fhl_filter_t *mc_filter;
105 gchar *regexp = mc_config_get_string (fhl->config, group_name, "regexp", "");
107 if (*regexp == '\0') {
108 g_free (regexp);
109 return FALSE;
112 mc_filter = g_new0 (mc_fhl_filter_t, 1);
113 mc_filter->type = MC_FLHGH_T_FREGEXP;
114 mc_filter->search_condition = mc_search_new (regexp, -1);
115 mc_filter->search_condition->is_case_sentitive = TRUE;
116 mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
118 mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
119 g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
120 g_free (regexp);
121 return TRUE;
125 /* --------------------------------------------------------------------------------------------- */
127 static gboolean
128 mc_fhl_parse_get_extensions (mc_fhl_t * fhl, const gchar * group_name)
130 mc_fhl_filter_t *mc_filter;
131 gchar **exts, **exts_orig;
132 gchar *esc_ext;
133 gsize exts_size;
134 GString *buf = g_string_new ("");
136 exts_orig = exts =
137 mc_config_get_string_list (fhl->config, group_name, "extensions", &exts_size);
139 if (exts_orig == NULL)
140 return FALSE;
142 if (exts_orig[0] == NULL) {
143 g_strfreev (exts_orig);
144 return FALSE;
147 while (*exts != NULL) {
148 esc_ext = strutils_regex_escape (*exts);
149 if (buf->len != 0)
150 g_string_append_c (buf, '|');
151 g_string_append (buf, esc_ext);
152 g_free (esc_ext);
153 exts++;
155 g_strfreev (exts_orig);
156 esc_ext = g_string_free (buf, FALSE);
157 buf = g_string_new (".*\\.(");
158 g_string_append (buf, esc_ext);
159 g_string_append (buf, ")$");
160 g_free (esc_ext);
162 mc_filter = g_new0 (mc_fhl_filter_t, 1);
163 mc_filter->type = MC_FLHGH_T_FREGEXP;
164 mc_filter->search_condition = mc_search_new (buf->str, -1);
165 mc_filter->search_condition->is_case_sentitive = TRUE;
166 mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
168 mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
169 g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
170 g_string_free (buf, TRUE);
171 return TRUE;
174 /* --------------------------------------------------------------------------------------------- */
175 /*** public functions ****************************************************************************/
176 /* --------------------------------------------------------------------------------------------- */
179 gboolean
180 mc_fhl_read_ini_file (mc_fhl_t * fhl, const gchar * filename)
182 if (fhl == NULL || filename == NULL || !exist_file (filename))
183 return FALSE;
185 if (fhl->config) {
187 if (!mc_config_read_file (fhl->config, filename))
188 return FALSE;
190 return TRUE;
193 fhl->config = mc_config_init (filename);
195 if (fhl->config == NULL)
196 return FALSE;
198 return TRUE;
202 /* --------------------------------------------------------------------------------------------- */
204 gboolean
205 mc_fhl_init_from_standart_files (mc_fhl_t * fhl)
207 gchar *name;
208 gchar *user_mc_dir;
210 /* ${datadir}/mc/filehighlight.ini */
211 name = concat_dir_and_file (mc_home_alt, MC_FHL_INI_FILE);
212 if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) {
213 g_free (name);
214 return FALSE;
216 g_free (name);
218 /* ${sysconfdir}/mc/filehighlight.ini */
219 name = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
220 if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) {
221 g_free (name);
222 return FALSE;
224 g_free (name);
226 /* ~/.mc/filehighlight.ini */
227 user_mc_dir = concat_dir_and_file (home_dir, MC_BASE);
228 name = concat_dir_and_file (user_mc_dir, MC_FHL_INI_FILE);
229 g_free (user_mc_dir);
230 if (exist_file (name) && (!mc_fhl_read_ini_file (fhl, name))) {
231 g_free (name);
232 return FALSE;
234 g_free (name);
236 return TRUE;
239 /* --------------------------------------------------------------------------------------------- */
241 gboolean
242 mc_fhl_parse_ini_file (mc_fhl_t * fhl)
244 gchar **group_names, **orig_group_names;
245 gsize ftype_names_size;
247 mc_fhl_array_free (fhl);
248 fhl->filters = g_ptr_array_new ();
250 orig_group_names = group_names =
251 mc_config_get_groups (fhl->config, &ftype_names_size);
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);
261 } else if (mc_config_has_param (fhl->config, *group_names, "regexp")) {
262 /* parse regexp filter */
263 mc_fhl_parse_get_regexp (fhl, *group_names);
264 } else if (mc_config_has_param (fhl->config, *group_names, "extensions")) {
265 /* parse extensions filter */
266 mc_fhl_parse_get_extensions (fhl, *group_names);
268 group_names++;
271 g_strfreev (orig_group_names);
272 return TRUE;
275 /* --------------------------------------------------------------------------------------------- */