Check perl, python and ruby programs and substitute them in various files.
[midnight-commander.git] / lib / search / lib.c
blob0ecbff93edd82142b7c8eacc7decc92c8f110195
1 /*
2 Search text engine.
3 Common share code for module.
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>
29 #include <stdlib.h>
30 #include <sys/types.h>
32 #include "lib/global.h"
33 #include "lib/strutil.h"
34 #include "lib/search.h"
35 #ifdef HAVE_CHARSET
36 #include "lib/charsets.h"
37 #endif
39 #include "internal.h"
41 /*** global variables ****************************************************************************/
43 const char *STR_E_NOTFOUND = N_("Search string not found");
44 const char *STR_E_UNKNOWN_TYPE = N_("Not implemented yet");
45 const char *STR_E_RPL_NOT_EQ_TO_FOUND =
46 N_("Num of replace tokens not equal to num of found tokens");
47 const char *STR_E_RPL_INVALID_TOKEN = N_("Invalid token number %d");
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** file scope variables ************************************************************************/
55 /*** file scope functions ************************************************************************/
57 /*** public functions ****************************************************************************/
59 gchar *
60 mc_search__recode_str (const char *str, gsize str_len,
61 const char *charset_from, const char *charset_to, gsize * bytes_written)
63 gchar *ret;
64 gsize bytes_read;
65 GIConv conv;
67 if (charset_from == NULL || charset_to == NULL || !strcmp (charset_to, charset_from))
69 *bytes_written = str_len;
70 return g_strndup (str, str_len);
73 conv = g_iconv_open (charset_to, charset_from);
74 if (conv == INVALID_CONV)
76 *bytes_written = str_len;
77 return g_strndup (str, str_len);
80 ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL);
81 g_iconv_close (conv);
83 if (ret == NULL)
85 *bytes_written = str_len;
86 return g_strndup (str, str_len);
89 return ret;
92 /* --------------------------------------------------------------------------------------------- */
94 gchar *
95 mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
96 gboolean * just_letters)
98 gchar *converted_str, *next_char;
100 gsize tmp_len;
101 #ifdef HAVE_CHARSET
102 gsize converted_str_len;
103 gchar *converted_str2;
105 if (charset == NULL)
106 charset = cp_source;
108 converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
109 #else
110 (void) charset;
112 converted_str = g_strndup (str, str_len);
113 #endif
115 next_char = (char *) str_cget_next_char (converted_str);
117 tmp_len = next_char - converted_str;
119 converted_str[tmp_len] = '\0';
121 #ifdef HAVE_CHARSET
122 converted_str2 =
123 mc_search__recode_str (converted_str, tmp_len, cp_display, charset, &converted_str_len);
124 #endif
125 if (just_letters)
127 if (str_isalnum (converted_str) && !str_isdigit (converted_str))
128 *just_letters = TRUE;
129 else
130 *just_letters = FALSE;
132 #ifdef HAVE_CHARSET
133 g_free (converted_str);
134 return converted_str2;
135 #else
136 return converted_str;
137 #endif
140 /* --------------------------------------------------------------------------------------------- */
142 mc_search_cbret_t
143 mc_search__get_char (mc_search_t * lc_mc_search, const void *user_data, gsize current_pos,
144 int *current_char)
146 unsigned char *data;
148 if (lc_mc_search->search_fn != NULL)
149 return lc_mc_search->search_fn (user_data, current_pos, current_char);
151 data = (unsigned char *) user_data;
152 *current_char = (int) data[current_pos];
153 return (*current_char == 0) ? MC_SEARCH_CB_ABORT : MC_SEARCH_CB_OK;
156 /* --------------------------------------------------------------------------------------------- */
158 GString *
159 mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len)
161 GString *ret;
162 #ifdef HAVE_CHARSET
163 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
164 gsize converted_str_len;
165 gsize tmp_len;
167 if (charset == NULL)
168 charset = cp_source;
170 tmp_str2 = converted_str =
171 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
173 tmp_len = converted_str_len + 1;
175 tmp_str3 = tmp_str1 = g_strdup (converted_str);
177 while (str_tolower (tmp_str1, &tmp_str2, &tmp_len))
178 tmp_str1 += str_length_char (tmp_str1);
180 g_free (tmp_str3);
181 tmp_str2 =
182 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
183 g_free (converted_str);
185 ret = g_string_new_len (tmp_str2, tmp_len);
186 g_free (tmp_str2);
187 return ret;
188 #else
189 const gchar *tmp_str1 = str;
190 gchar *converted_str, *tmp_str2;
191 gsize converted_str_len = str_len + 1;
193 (void) charset;
195 tmp_str2 = converted_str = g_strndup (str, str_len);
197 while (str_tolower (tmp_str1, &tmp_str2, &converted_str_len))
198 tmp_str1 += str_length_char (tmp_str1);
200 ret = g_string_new_len (converted_str, str_len);
201 g_free (converted_str);
202 return ret;
203 #endif
206 /* --------------------------------------------------------------------------------------------- */
208 GString *
209 mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len)
211 GString *ret;
212 #ifdef HAVE_CHARSET
213 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
214 gsize converted_str_len;
215 gsize tmp_len;
217 if (charset == NULL)
218 charset = cp_source;
220 tmp_str2 = converted_str =
221 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
223 tmp_len = converted_str_len + 1;
225 tmp_str3 = tmp_str1 = g_strdup (converted_str);
227 while (str_toupper (tmp_str1, &tmp_str2, &tmp_len))
228 tmp_str1 += str_length_char (tmp_str1);
230 g_free (tmp_str3);
232 tmp_str2 =
233 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
234 g_free (converted_str);
236 ret = g_string_new_len (tmp_str2, tmp_len);
237 g_free (tmp_str2);
238 return ret;
239 #else
241 const gchar *tmp_str1 = str;
242 gchar *converted_str, *tmp_str2;
243 gsize converted_str_len = str_len + 1;
245 (void) charset;
247 tmp_str2 = converted_str = g_strndup (str, str_len);
249 while (str_toupper (tmp_str1, &tmp_str2, &converted_str_len))
250 tmp_str1 += str_length_char (tmp_str1);
252 ret = g_string_new_len (converted_str, str_len);
253 g_free (converted_str);
254 return ret;
255 #endif
258 /* --------------------------------------------------------------------------------------------- */
260 gchar **
261 mc_search_get_types_strings_array (size_t * num)
263 gchar **ret;
264 int lc_index;
265 size_t n;
267 const mc_search_type_str_t *type_str;
268 const mc_search_type_str_t *types_str = mc_search_types_list_get (&n);
270 ret = g_try_new0 (char *, n + 1);
271 if (ret == NULL)
272 return NULL;
274 for (lc_index = 0, type_str = types_str; type_str->str != NULL; type_str++, lc_index++)
275 ret[lc_index] = g_strdup (type_str->str);
277 /* don't count last NULL item */
278 if (num != NULL)
279 *num = (size_t) lc_index;
281 return ret;
284 /* --------------------------------------------------------------------------------------------- */