Editor: sync with new global config location (user menu and syntax files).
[midnight-commander.git] / src / search / lib.c
blob819e5a0578393508dd1fad90b08b197f43fc679e
1 /*
2 Search text engine.
3 Common share code for module.
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>
31 #include "../src/global.h"
32 #include "../src/search/search.h"
33 #include "../src/search/internal.h"
34 #include "../src/strutil.h"
35 #include "../src/charsets.h"
37 /*** global variables ****************************************************************************/
39 char STR_E_NOTFOUND[] = N_(" Search string not found ");
40 char STR_E_UNKNOWN_TYPE[] = N_(" Not implemented yet ");
41 char STR_E_RPL_NOT_EQ_TO_FOUND[] = N_(" Num of replace tokens not equal to num of found tokens ");
42 char STR_E_RPL_INVALID_TOKEN[] = N_(" Invalid token number %d ");
44 /*** file scope macro definitions ****************************************************************/
46 /*** file scope type declarations ****************************************************************/
48 /*** file scope variables ************************************************************************/
50 /*** file scope functions ************************************************************************/
52 /*** public functions ****************************************************************************/
54 gchar *
55 mc_search__recode_str (const char *str, gsize str_len,
56 const char *charset_from, const char *charset_to, gsize * bytes_written)
58 gchar *ret;
59 gsize bytes_read;
60 GIConv conv;
63 if (!strcmp (charset_to, charset_from)) {
64 *bytes_written = str_len;
65 return g_strndup (str, str_len);
68 conv = g_iconv_open (charset_to, charset_from);
69 if (conv == INVALID_CONV)
70 return NULL;
72 ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL);
73 g_iconv_close (conv);
74 return ret;
77 /* --------------------------------------------------------------------------------------------- */
79 gchar *
80 mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
81 gboolean * just_letters)
84 gchar *converted_str, *next_char;
86 gsize tmp_len;
87 #ifdef HAVE_CHARSET
88 gsize converted_str_len;
89 gchar *converted_str2;
91 converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
92 #else
93 converted_str = g_strndup(str, str_len);
94 #endif
96 next_char = (char *) str_cget_next_char (converted_str);
98 tmp_len = next_char - converted_str;
100 converted_str[tmp_len] = '\0';
102 #ifdef HAVE_CHARSET
103 converted_str2 =
104 mc_search__recode_str (converted_str, tmp_len, cp_display, charset, &converted_str_len);
105 #endif
107 if (str_isalnum (converted_str) && !str_isdigit (converted_str))
108 *just_letters = TRUE;
109 else
110 *just_letters = FALSE;
113 #ifdef HAVE_CHARSET
114 g_free (converted_str);
115 return converted_str2;
116 #else
117 return converted_str;
118 #endif
121 /* --------------------------------------------------------------------------------------------- */
123 mc_search__get_char (mc_search_t * mc_search, const void *user_data, gsize current_pos)
125 char *data;
126 if (mc_search->search_fn)
127 return (mc_search->search_fn) (user_data, current_pos);
129 data = (char *) user_data;
130 return (int) (unsigned char) data[current_pos];
133 /* --------------------------------------------------------------------------------------------- */
136 GString *
137 mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len)
139 GString *ret;
140 #ifdef HAVE_CHARSET
141 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
142 gsize converted_str_len;
143 gsize tmp_len;
145 tmp_str2 = converted_str =
146 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
147 if (converted_str == NULL)
148 return NULL;
150 tmp_len = converted_str_len + 1;
152 tmp_str3 = tmp_str1 = g_strdup (converted_str);
154 while (str_tolower (tmp_str1, &tmp_str2, &tmp_len))
155 tmp_str1 += str_length_char (tmp_str1);
157 g_free (tmp_str3);
158 tmp_str2 =
159 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
160 g_free (converted_str);
161 if (tmp_str2 == NULL)
162 return NULL;
164 ret = g_string_new_len (tmp_str2, tmp_len);
165 g_free(tmp_str2);
166 return ret;
167 #else
168 const gchar *tmp_str1 = str;
169 gchar *converted_str, *tmp_str2;
170 gsize converted_str_len = str_len+1;
172 tmp_str2 = converted_str = g_strndup(str, str_len);
174 while (str_tolower (tmp_str1, &tmp_str2, &converted_str_len))
175 tmp_str1 += str_length_char (tmp_str1);
177 ret = g_string_new_len (converted_str, str_len);
178 g_free(converted_str);
179 return ret;
180 #endif
183 /* --------------------------------------------------------------------------------------------- */
185 GString *
186 mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len)
188 GString *ret;
189 #ifdef HAVE_CHARSET
190 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
191 gsize converted_str_len;
192 gsize tmp_len;
194 tmp_str2 = converted_str =
195 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
196 if (converted_str == NULL)
197 return NULL;
199 tmp_len = converted_str_len + 1;
201 tmp_str3 = tmp_str1 = g_strdup (converted_str);
203 while (str_toupper (tmp_str1, &tmp_str2, &tmp_len))
204 tmp_str1 += str_length_char (tmp_str1);
206 g_free (tmp_str3);
208 tmp_str2 =
209 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
210 g_free (converted_str);
211 if (tmp_str2 == NULL)
212 return NULL;
214 ret = g_string_new_len (tmp_str2, tmp_len);
215 g_free(tmp_str2);
216 return ret;
217 #else
218 const gchar *tmp_str1 = str;
219 gchar *converted_str, *tmp_str2;
220 gsize converted_str_len = str_len+1;
222 tmp_str2 = converted_str = g_strndup(str, str_len);
224 while (str_toupper (tmp_str1, &tmp_str2, &converted_str_len))
225 tmp_str1 += str_length_char (tmp_str1);
227 ret = g_string_new_len (converted_str, str_len);
228 g_free(converted_str);
229 return ret;
230 #endif
233 /* --------------------------------------------------------------------------------------------- */
235 gboolean
236 mc_search__regex_is_char_escaped (const char *start, const char *current)
238 int num_esc = 0;
239 while (*current == '\\' && current >= start) {
240 num_esc++;
241 current--;
243 return (gboolean) num_esc % 2;
246 /* --------------------------------------------------------------------------------------------- */
248 gchar **
249 mc_search_get_types_strings_array (void)
251 GString *tmp;
252 gchar **ret;
253 const mc_search_type_str_t *type_str;
254 const mc_search_type_str_t *types_str = mc_search_types_list_get ();
256 tmp = g_string_new ("");
257 type_str = types_str;
258 while (type_str->str) {
259 if (tmp->len)
260 g_string_append (tmp, "__||__");
261 g_string_append (tmp, type_str->str);
262 type_str++;
264 ret = g_strsplit (tmp->str, "__||__", -1);
265 g_string_free (tmp, TRUE);
266 return ret;