(mc_search__hex_translate_to_regex): rename variables.
[midnight-commander.git] / lib / search / lib.c
blob5d086f1f52281eca06e7f459cff57455f4716291
1 /*
2 Search text engine.
3 Common share code for module.
5 Copyright (C) 2009-2016
6 Free Software Foundation, Inc.
8 Written by:
9 Slava Zanko <slavazanko@gmail.com>, 2009, 2011
10 Andrew Borodin <aborodin@vmail.ru>, 2013
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <config.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
33 #include "lib/global.h"
34 #include "lib/strutil.h"
35 #include "lib/search.h"
36 #ifdef HAVE_CHARSET
37 #include "lib/charsets.h"
38 #endif
40 #include "internal.h"
42 /*** global variables ****************************************************************************/
44 const char *STR_E_NOTFOUND = N_("Search string not found");
45 const char *STR_E_UNKNOWN_TYPE = N_("Not implemented yet");
46 const char *STR_E_RPL_NOT_EQ_TO_FOUND =
47 N_("Num of replace tokens not equal to num of found tokens");
48 const char *STR_E_RPL_INVALID_TOKEN = N_("Invalid token number %d");
50 /*** file scope macro definitions ****************************************************************/
52 /*** file scope type declarations ****************************************************************/
54 /*** file scope variables ************************************************************************/
56 /*** file scope functions ************************************************************************/
58 /*** public functions ****************************************************************************/
60 gchar *
61 mc_search__recode_str (const char *str, gsize str_len,
62 const char *charset_from, const char *charset_to, gsize * bytes_written)
64 gchar *ret;
65 gsize bytes_read;
66 GIConv conv;
68 if (charset_from == NULL || charset_to == NULL
69 || g_ascii_strcasecmp (charset_to, charset_from) == 0)
71 *bytes_written = str_len;
72 return g_strndup (str, str_len);
75 conv = g_iconv_open (charset_to, charset_from);
76 if (conv == INVALID_CONV)
78 *bytes_written = str_len;
79 return g_strndup (str, str_len);
82 ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL);
83 g_iconv_close (conv);
85 if (ret == NULL)
87 *bytes_written = str_len;
88 return g_strndup (str, str_len);
91 return ret;
94 /* --------------------------------------------------------------------------------------------- */
96 gchar *
97 mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
98 gboolean * just_letters)
100 gchar *converted_str;
101 const gchar *next_char;
103 gsize tmp_len;
104 #ifdef HAVE_CHARSET
105 gsize converted_str_len;
106 gchar *converted_str2;
108 if (charset == NULL)
109 charset = cp_source;
111 converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
112 #else
113 (void) charset;
115 converted_str = g_strndup (str, str_len);
116 #endif
118 next_char = str_cget_next_char (converted_str);
120 tmp_len = next_char - converted_str;
122 converted_str[tmp_len] = '\0';
124 #ifdef HAVE_CHARSET
125 converted_str2 =
126 mc_search__recode_str (converted_str, tmp_len, cp_display, charset, &converted_str_len);
127 #endif
128 if (just_letters)
130 if (str_isalnum (converted_str) && !str_isdigit (converted_str))
131 *just_letters = TRUE;
132 else
133 *just_letters = FALSE;
135 #ifdef HAVE_CHARSET
136 g_free (converted_str);
137 return converted_str2;
138 #else
139 return converted_str;
140 #endif
143 /* --------------------------------------------------------------------------------------------- */
145 GString *
146 mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len)
148 GString *ret;
149 #ifdef HAVE_CHARSET
150 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
151 gsize converted_str_len;
152 gsize tmp_len;
154 if (charset == NULL)
155 charset = cp_source;
157 tmp_str2 = converted_str =
158 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
160 tmp_len = converted_str_len + 1;
162 tmp_str3 = tmp_str1 = g_strdup (converted_str);
164 while (str_tolower (tmp_str1, &tmp_str2, &tmp_len))
165 tmp_str1 += str_length_char (tmp_str1);
167 g_free (tmp_str3);
168 tmp_str2 =
169 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
170 g_free (converted_str);
172 ret = g_string_new_len (tmp_str2, tmp_len);
173 g_free (tmp_str2);
174 return ret;
175 #else
176 const gchar *tmp_str1 = str;
177 gchar *converted_str, *tmp_str2;
178 gsize converted_str_len = str_len + 1;
180 (void) charset;
182 tmp_str2 = converted_str = g_strndup (str, str_len);
184 while (str_tolower (tmp_str1, &tmp_str2, &converted_str_len))
185 tmp_str1 += str_length_char (tmp_str1);
187 ret = g_string_new_len (converted_str, str_len);
188 g_free (converted_str);
189 return ret;
190 #endif
193 /* --------------------------------------------------------------------------------------------- */
195 GString *
196 mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len)
198 GString *ret;
199 #ifdef HAVE_CHARSET
200 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
201 gsize converted_str_len;
202 gsize tmp_len;
204 if (charset == NULL)
205 charset = cp_source;
207 tmp_str2 = converted_str =
208 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
210 tmp_len = converted_str_len + 1;
212 tmp_str3 = tmp_str1 = g_strdup (converted_str);
214 while (str_toupper (tmp_str1, &tmp_str2, &tmp_len))
215 tmp_str1 += str_length_char (tmp_str1);
217 g_free (tmp_str3);
219 tmp_str2 =
220 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
221 g_free (converted_str);
223 ret = g_string_new_len (tmp_str2, tmp_len);
224 g_free (tmp_str2);
225 return ret;
226 #else
228 const gchar *tmp_str1 = str;
229 gchar *converted_str, *tmp_str2;
230 gsize converted_str_len = str_len + 1;
232 (void) charset;
234 tmp_str2 = converted_str = g_strndup (str, str_len);
236 while (str_toupper (tmp_str1, &tmp_str2, &converted_str_len))
237 tmp_str1 += str_length_char (tmp_str1);
239 ret = g_string_new_len (converted_str, str_len);
240 g_free (converted_str);
241 return ret;
242 #endif
245 /* --------------------------------------------------------------------------------------------- */
247 gchar **
248 mc_search_get_types_strings_array (size_t * num)
250 gchar **ret;
251 int lc_index;
252 size_t n;
254 const mc_search_type_str_t *type_str;
255 const mc_search_type_str_t *types_str = mc_search_types_list_get (&n);
257 ret = g_try_new0 (char *, n + 1);
258 if (ret == NULL)
259 return NULL;
261 for (lc_index = 0, type_str = types_str; type_str->str != NULL; type_str++, lc_index++)
262 ret[lc_index] = g_strdup (type_str->str);
264 /* don't count last NULL item */
265 if (num != NULL)
266 *num = (size_t) lc_index;
268 return ret;
271 /* --------------------------------------------------------------------------------------------- */