Optimization of ini files load.
[midnight-commander.git] / lib / filehighlight / ini-file-read.c
blob44faca42fd7e260418e874e19855f0edf0879425
1 /*
2 File highlight plugin.
3 Reading and parse rules from 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 "lib/global.h"
31 #include "lib/fileloc.h"
32 #include "lib/strescape.h"
33 #include "lib/skin.h"
34 #include "lib/util.h" /* exist_file() */
35 #include "lib/filehighlight.h"
37 #include "internal.h"
39 /*** global variables ****************************************************************************/
41 extern mc_skin_t mc_skin__default;
43 /*** file scope macro definitions ****************************************************************/
45 /*** file scope type declarations ****************************************************************/
47 /*** file scope variables ************************************************************************/
49 /*** file scope functions ************************************************************************/
50 /* --------------------------------------------------------------------------------------------- */
52 static void
53 mc_fhl_parse_fill_color_info (mc_fhl_filter_t * mc_filter, mc_fhl_t * fhl, const gchar * group_name)
55 (void) fhl;
56 mc_filter->color_pair_index = mc_skin_color_get ("filehighlight", group_name);
59 /* --------------------------------------------------------------------------------------------- */
61 static gboolean
62 mc_fhl_parse_get_file_type_id (mc_fhl_t * fhl, const gchar * group_name)
64 mc_fhl_filter_t *mc_filter;
66 const gchar *types[] = {
67 "FILE", "FILE_EXE",
68 "DIR", "LINK_DIR",
69 "LINK", "HARDLINK", "SYMLINK",
70 "STALE_LINK",
71 "DEVICE", "DEVICE_BLOCK", "DEVICE_CHAR",
72 "SPECIAL", "SPECIAL_SOCKET", "SPECIAL_FIFO", "SPECIAL_DOOR",
73 NULL
75 int i;
76 gchar *param_type = mc_config_get_string (fhl->config, group_name, "type", "");
78 if (*param_type == '\0')
80 g_free (param_type);
81 return FALSE;
84 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')
112 g_free (regexp);
113 return FALSE;
116 mc_filter = g_new0 (mc_fhl_filter_t, 1);
117 mc_filter->type = MC_FLHGH_T_FREGEXP;
118 mc_filter->search_condition = mc_search_new (regexp, -1);
119 mc_filter->search_condition->is_case_sensitive = TRUE;
120 mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
122 mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
123 g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
124 g_free (regexp);
125 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 gsize exts_size;
136 GString *buf;
138 exts_orig = exts =
139 mc_config_get_string_list (fhl->config, group_name, "extensions", &exts_size);
141 if (exts_orig == NULL || exts_orig[0] == NULL)
143 g_strfreev (exts_orig);
144 return FALSE;
147 buf = g_string_sized_new (64);
148 for (exts = exts_orig; *exts != NULL; exts++)
150 char *esc_ext;
152 esc_ext = strutils_regex_escape (*exts);
153 if (buf->len != 0)
154 g_string_append_c (buf, '|');
155 g_string_append (buf, esc_ext);
156 g_free (esc_ext);
158 g_strfreev (exts_orig);
160 g_string_prepend (buf, ".*\\.(");
161 g_string_append (buf, ")$");
163 mc_filter = g_new0 (mc_fhl_filter_t, 1);
164 mc_filter->type = MC_FLHGH_T_FREGEXP;
165 mc_filter->search_condition = mc_search_new (buf->str, buf->len);
166 mc_filter->search_condition->is_case_sensitive =
167 mc_config_get_bool (fhl->config, group_name, "extensions_case", TRUE);
168 mc_filter->search_condition->search_type = MC_SEARCH_T_REGEX;
170 mc_fhl_parse_fill_color_info (mc_filter, fhl, group_name);
171 g_ptr_array_add (fhl->filters, (gpointer) mc_filter);
172 g_string_free (buf, TRUE);
173 return TRUE;
176 /* --------------------------------------------------------------------------------------------- */
177 /*** public functions ****************************************************************************/
178 /* --------------------------------------------------------------------------------------------- */
180 gboolean
181 mc_fhl_read_ini_file (mc_fhl_t * fhl, const gchar * filename)
183 if (fhl == NULL || filename == NULL || !exist_file (filename))
184 return FALSE;
186 if (fhl->config != NULL)
187 return mc_config_read_file (fhl->config, filename, TRUE, FALSE);
189 fhl->config = mc_config_init (filename, TRUE);
190 return (fhl->config != NULL);
193 /* --------------------------------------------------------------------------------------------- */
195 gboolean
196 mc_fhl_init_from_standard_files (mc_fhl_t * fhl)
198 gchar *name;
199 gboolean ok;
201 /* ${XDG_CONFIG_HOME}/mc/filehighlight.ini */
202 name = mc_config_get_full_path (MC_FHL_INI_FILE);
203 ok = mc_fhl_read_ini_file (fhl, name);
204 g_free (name);
205 if (ok)
206 return TRUE;
208 /* ${sysconfdir}/mc/filehighlight.ini */
209 name = g_build_filename (mc_global.sysconfig_dir, MC_FHL_INI_FILE, (char *) NULL);
210 ok = mc_fhl_read_ini_file (fhl, name);
211 g_free (name);
212 if (ok)
213 return TRUE;
215 /* ${datadir}/mc/filehighlight.ini */
216 name = g_build_filename (mc_global.share_data_dir, MC_FHL_INI_FILE, (char *) NULL);
217 ok = mc_fhl_read_ini_file (fhl, name);
218 g_free (name);
219 return ok;
222 /* --------------------------------------------------------------------------------------------- */
224 gboolean
225 mc_fhl_parse_ini_file (mc_fhl_t * fhl)
227 gchar **group_names, **orig_group_names;
229 mc_fhl_array_free (fhl);
230 fhl->filters = g_ptr_array_new ();
232 orig_group_names = group_names = mc_config_get_groups (fhl->config, NULL);
234 if (group_names == NULL)
235 return FALSE;
237 while (*group_names)
240 if (mc_config_has_param (fhl->config, *group_names, "type"))
242 /* parse filetype filter */
243 mc_fhl_parse_get_file_type_id (fhl, *group_names);
245 if (mc_config_has_param (fhl->config, *group_names, "regexp"))
247 /* parse regexp filter */
248 mc_fhl_parse_get_regexp (fhl, *group_names);
250 if (mc_config_has_param (fhl->config, *group_names, "extensions"))
252 /* parse extensions filter */
253 mc_fhl_parse_get_extensions (fhl, *group_names);
255 group_names++;
258 g_strfreev (orig_group_names);
259 return TRUE;
262 /* --------------------------------------------------------------------------------------------- */