Updated Russian translation.
[midnight-commander.git] / lib / search / lib.c
blobc33e2592e5ab5305ebf10b2d9604735aa510641b
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>
30 #include <stdlib.h>
31 #include <sys/types.h>
33 #include "lib/global.h"
34 #include "lib/strutil.h"
35 #include "lib/search.h"
37 #include "internal.h"
38 #include "lib/charsets.h"
40 /*** global variables ****************************************************************************/
42 const char *STR_E_NOTFOUND = N_("Search string not found");
43 const char *STR_E_UNKNOWN_TYPE = N_("Not implemented yet");
44 const char *STR_E_RPL_NOT_EQ_TO_FOUND =
45 N_("Num of replace tokens not equal to num of found tokens");
46 const char *STR_E_RPL_INVALID_TOKEN = N_("Invalid token number %d");
48 /*** file scope macro definitions ****************************************************************/
50 /*** file scope type declarations ****************************************************************/
52 /*** file scope variables ************************************************************************/
54 /*** file scope functions ************************************************************************/
56 /*** public functions ****************************************************************************/
58 gchar *
59 mc_search__recode_str (const char *str, gsize str_len,
60 const char *charset_from, const char *charset_to, gsize * bytes_written)
62 gchar *ret;
63 gsize bytes_read;
64 GIConv conv;
66 if (charset_from == NULL || charset_to == NULL || !strcmp (charset_to, charset_from))
68 *bytes_written = str_len;
69 return g_strndup (str, str_len);
72 conv = g_iconv_open (charset_to, charset_from);
73 if (conv == INVALID_CONV)
75 *bytes_written = str_len;
76 return g_strndup (str, str_len);
79 ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL);
80 g_iconv_close (conv);
82 if (ret == NULL)
84 *bytes_written = str_len;
85 return g_strndup (str, str_len);
88 return ret;
91 /* --------------------------------------------------------------------------------------------- */
93 gchar *
94 mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
95 gboolean * just_letters)
97 gchar *converted_str, *next_char;
99 gsize tmp_len;
100 #ifdef HAVE_CHARSET
101 gsize converted_str_len;
102 gchar *converted_str2;
104 if (charset == NULL)
105 charset = cp_source;
107 converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
108 #else
109 (void) charset;
111 converted_str = g_strndup (str, str_len);
112 #endif
114 next_char = (char *) str_cget_next_char (converted_str);
116 tmp_len = next_char - converted_str;
118 converted_str[tmp_len] = '\0';
120 #ifdef HAVE_CHARSET
121 converted_str2 =
122 mc_search__recode_str (converted_str, tmp_len, cp_display, charset, &converted_str_len);
123 #endif
124 if (just_letters)
126 if (str_isalnum (converted_str) && !str_isdigit (converted_str))
127 *just_letters = TRUE;
128 else
129 *just_letters = FALSE;
131 #ifdef HAVE_CHARSET
132 g_free (converted_str);
133 return converted_str2;
134 #else
135 return converted_str;
136 #endif
139 /* --------------------------------------------------------------------------------------------- */
142 mc_search__get_char (mc_search_t * lc_mc_search, const void *user_data, gsize current_pos)
144 char *data;
145 if (lc_mc_search->search_fn)
146 return (lc_mc_search->search_fn) (user_data, current_pos);
148 data = (char *) user_data;
149 return (int) (unsigned char) data[current_pos];
152 /* --------------------------------------------------------------------------------------------- */
154 GString *
155 mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len)
157 GString *ret;
158 #ifdef HAVE_CHARSET
159 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
160 gsize converted_str_len;
161 gsize tmp_len;
163 if (charset == NULL)
164 charset = cp_source;
166 tmp_str2 = converted_str =
167 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
169 tmp_len = converted_str_len + 1;
171 tmp_str3 = tmp_str1 = g_strdup (converted_str);
173 while (str_tolower (tmp_str1, &tmp_str2, &tmp_len))
174 tmp_str1 += str_length_char (tmp_str1);
176 g_free (tmp_str3);
177 tmp_str2 =
178 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
179 g_free (converted_str);
181 ret = g_string_new_len (tmp_str2, tmp_len);
182 g_free (tmp_str2);
183 return ret;
184 #else
185 const gchar *tmp_str1 = str;
186 gchar *converted_str, *tmp_str2;
187 gsize converted_str_len = str_len + 1;
189 (void) charset;
191 tmp_str2 = converted_str = g_strndup (str, str_len);
193 while (str_tolower (tmp_str1, &tmp_str2, &converted_str_len))
194 tmp_str1 += str_length_char (tmp_str1);
196 ret = g_string_new_len (converted_str, str_len);
197 g_free (converted_str);
198 return ret;
199 #endif
202 /* --------------------------------------------------------------------------------------------- */
204 GString *
205 mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len)
207 GString *ret;
208 #ifdef HAVE_CHARSET
209 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
210 gsize converted_str_len;
211 gsize tmp_len;
213 if (charset == NULL)
214 charset = cp_source;
216 tmp_str2 = converted_str =
217 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
219 tmp_len = converted_str_len + 1;
221 tmp_str3 = tmp_str1 = g_strdup (converted_str);
223 while (str_toupper (tmp_str1, &tmp_str2, &tmp_len))
224 tmp_str1 += str_length_char (tmp_str1);
226 g_free (tmp_str3);
228 tmp_str2 =
229 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
230 g_free (converted_str);
232 ret = g_string_new_len (tmp_str2, tmp_len);
233 g_free (tmp_str2);
234 return ret;
235 #else
237 const gchar *tmp_str1 = str;
238 gchar *converted_str, *tmp_str2;
239 gsize converted_str_len = str_len + 1;
241 (void) charset;
243 tmp_str2 = converted_str = g_strndup (str, str_len);
245 while (str_toupper (tmp_str1, &tmp_str2, &converted_str_len))
246 tmp_str1 += str_length_char (tmp_str1);
248 ret = g_string_new_len (converted_str, str_len);
249 g_free (converted_str);
250 return ret;
251 #endif
254 /* --------------------------------------------------------------------------------------------- */
256 gchar **
257 mc_search_get_types_strings_array (size_t * num)
259 gchar **ret;
260 int lc_index;
261 size_t n;
263 const mc_search_type_str_t *type_str;
264 const mc_search_type_str_t *types_str = mc_search_types_list_get (&n);
266 ret = g_try_new0 (char *, n + 1);
267 if (ret == NULL)
268 return NULL;
270 for (lc_index = 0, type_str = types_str; type_str->str != NULL; type_str++, lc_index++)
271 ret[lc_index] = g_strdup (type_str->str);
273 /* don't count last NULL item */
274 if (num != NULL)
275 *num = (size_t) lc_index;
277 return ret;
280 /* --------------------------------------------------------------------------------------------- */