Massive moved some dirs from $(srcdir)/src into $(srcdir)/lib
[midnight-commander.git] / lib / search / lib.c
blobc38ef0cef01efe4cda364e8fdb2fe773939a7ace
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 "../src/global.h"
34 #include "../src/search/search.h"
35 #include "../src/search/internal.h"
36 #include "../src/strutil.h"
37 #include "../src/charsets.h"
39 /*** global variables ****************************************************************************/
41 const char * STR_E_NOTFOUND = N_(" Search string not found ");
42 const char * STR_E_UNKNOWN_TYPE = N_(" Not implemented yet ");
43 const char * STR_E_RPL_NOT_EQ_TO_FOUND = N_(" Num of replace tokens not equal to num of found tokens ");
44 const char * STR_E_RPL_INVALID_TOKEN = N_(" Invalid token number %d ");
46 /*** file scope macro definitions ****************************************************************/
48 /*** file scope type declarations ****************************************************************/
50 /*** file scope variables ************************************************************************/
52 /*** file scope functions ************************************************************************/
54 /*** public functions ****************************************************************************/
56 gchar *
57 mc_search__recode_str (const char *str, gsize str_len,
58 const char *charset_from, const char *charset_to, gsize * bytes_written)
60 gchar *ret;
61 gsize bytes_read;
62 GIConv conv;
64 if (charset_from == NULL || charset_to == NULL || !strcmp (charset_to, charset_from)) {
65 *bytes_written = str_len;
66 return g_strndup (str, str_len);
69 conv = g_iconv_open (charset_to, charset_from);
70 if (conv == INVALID_CONV) {
71 *bytes_written = str_len;
72 return g_strndup (str, str_len);
75 ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL);
76 g_iconv_close (conv);
78 if (ret == NULL) {
79 *bytes_written = str_len;
80 return g_strndup (str, str_len);
83 return ret;
86 /* --------------------------------------------------------------------------------------------- */
88 gchar *
89 mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
90 gboolean * just_letters)
93 gchar *converted_str, *next_char;
95 gsize tmp_len;
96 #ifdef HAVE_CHARSET
97 gsize converted_str_len;
98 gchar *converted_str2;
100 if (charset == NULL)
101 charset = cp_source;
103 converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
104 #else
105 (void) charset;
107 converted_str = g_strndup (str, str_len);
108 #endif
110 next_char = (char *) str_cget_next_char (converted_str);
112 tmp_len = next_char - converted_str;
114 converted_str[tmp_len] = '\0';
116 #ifdef HAVE_CHARSET
117 converted_str2 =
118 mc_search__recode_str (converted_str, tmp_len, cp_display, charset, &converted_str_len);
119 #endif
120 if (just_letters) {
121 if (str_isalnum (converted_str) && !str_isdigit (converted_str))
122 *just_letters = TRUE;
123 else
124 *just_letters = FALSE;
126 #ifdef HAVE_CHARSET
127 g_free (converted_str);
128 return converted_str2;
129 #else
130 return converted_str;
131 #endif
134 /* --------------------------------------------------------------------------------------------- */
136 mc_search__get_char (mc_search_t * lc_mc_search, const void *user_data, gsize current_pos)
138 char *data;
139 if (lc_mc_search->search_fn)
140 return (lc_mc_search->search_fn) (user_data, current_pos);
142 data = (char *) user_data;
143 return (int) (unsigned char) data[current_pos];
146 /* --------------------------------------------------------------------------------------------- */
149 GString *
150 mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len)
152 GString *ret;
153 #ifdef HAVE_CHARSET
154 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
155 gsize converted_str_len;
156 gsize tmp_len;
158 if (charset == NULL)
159 charset = cp_source;
161 tmp_str2 = converted_str =
162 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
164 tmp_len = converted_str_len + 1;
166 tmp_str3 = tmp_str1 = g_strdup (converted_str);
168 while (str_tolower (tmp_str1, &tmp_str2, &tmp_len))
169 tmp_str1 += str_length_char (tmp_str1);
171 g_free (tmp_str3);
172 tmp_str2 =
173 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
174 g_free (converted_str);
176 ret = g_string_new_len (tmp_str2, tmp_len);
177 g_free (tmp_str2);
178 return ret;
179 #else
180 const gchar *tmp_str1 = str;
181 gchar *converted_str, *tmp_str2;
182 gsize converted_str_len = str_len + 1;
184 (void) charset;
186 tmp_str2 = converted_str = g_strndup (str, str_len);
188 while (str_tolower (tmp_str1, &tmp_str2, &converted_str_len))
189 tmp_str1 += str_length_char (tmp_str1);
191 ret = g_string_new_len (converted_str, str_len);
192 g_free (converted_str);
193 return ret;
194 #endif
197 /* --------------------------------------------------------------------------------------------- */
199 GString *
200 mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len)
202 GString *ret;
203 #ifdef HAVE_CHARSET
204 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
205 gsize converted_str_len;
206 gsize tmp_len;
208 if (charset == NULL)
209 charset = cp_source;
211 tmp_str2 = converted_str =
212 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
214 tmp_len = converted_str_len + 1;
216 tmp_str3 = tmp_str1 = g_strdup (converted_str);
218 while (str_toupper (tmp_str1, &tmp_str2, &tmp_len))
219 tmp_str1 += str_length_char (tmp_str1);
221 g_free (tmp_str3);
223 tmp_str2 =
224 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
225 g_free (converted_str);
227 ret = g_string_new_len (tmp_str2, tmp_len);
228 g_free (tmp_str2);
229 return ret;
230 #else
232 const gchar *tmp_str1 = str;
233 gchar *converted_str, *tmp_str2;
234 gsize converted_str_len = str_len + 1;
236 (void) charset;
238 tmp_str2 = converted_str = g_strndup (str, str_len);
240 while (str_toupper (tmp_str1, &tmp_str2, &converted_str_len))
241 tmp_str1 += str_length_char (tmp_str1);
243 ret = g_string_new_len (converted_str, str_len);
244 g_free (converted_str);
245 return ret;
246 #endif
249 /* --------------------------------------------------------------------------------------------- */
251 gchar **
252 mc_search_get_types_strings_array (size_t *num)
254 gchar **ret;
255 int lc_index;
256 size_t n;
258 const mc_search_type_str_t *type_str;
259 const mc_search_type_str_t *types_str = mc_search_types_list_get (&n);
261 ret = g_try_new0 (char *, n + 1);
262 if (ret == NULL)
263 return NULL;
265 for (lc_index = 0, type_str = types_str;
266 type_str->str != NULL;
267 type_str++, lc_index++)
268 ret[lc_index] = g_strdup (type_str->str);
270 /* don't count last NULL item */
271 if (num != NULL)
272 *num = (size_t) lc_index;
274 return ret;
277 /* --------------------------------------------------------------------------------------------- */