Cleanup: avoid warnings for mc-library on x86_64
[midnight-commander.git] / lib / search / lib.c
blob2218b014a08231d7596d91e6496f90453c003a89
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 "src/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 = N_(" Num of replace tokens not equal to num of found tokens ");
45 const char * STR_E_RPL_INVALID_TOKEN = N_(" Invalid token number %d ");
47 /*** file scope macro definitions ****************************************************************/
49 /*** file scope type declarations ****************************************************************/
51 /*** file scope variables ************************************************************************/
53 /*** file scope functions ************************************************************************/
55 /*** public functions ****************************************************************************/
57 gchar *
58 mc_search__recode_str (const char *str, gsize str_len,
59 const char *charset_from, const char *charset_to, gsize * bytes_written)
61 gchar *ret;
62 gsize bytes_read;
63 GIConv conv;
65 if (charset_from == NULL || charset_to == NULL || !strcmp (charset_to, charset_from)) {
66 *bytes_written = str_len;
67 return g_strndup (str, str_len);
70 conv = g_iconv_open (charset_to, charset_from);
71 if (conv == INVALID_CONV) {
72 *bytes_written = str_len;
73 return g_strndup (str, str_len);
76 ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL);
77 g_iconv_close (conv);
79 if (ret == NULL) {
80 *bytes_written = str_len;
81 return g_strndup (str, str_len);
84 return ret;
87 /* --------------------------------------------------------------------------------------------- */
89 gchar *
90 mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
91 gboolean * just_letters)
94 gchar *converted_str, *next_char;
96 gsize tmp_len;
97 #ifdef HAVE_CHARSET
98 gsize converted_str_len;
99 gchar *converted_str2;
101 if (charset == NULL)
102 charset = cp_source;
104 converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
105 #else
106 (void) charset;
108 converted_str = g_strndup (str, str_len);
109 #endif
111 next_char = (char *) str_cget_next_char (converted_str);
113 tmp_len = next_char - converted_str;
115 converted_str[tmp_len] = '\0';
117 #ifdef HAVE_CHARSET
118 converted_str2 =
119 mc_search__recode_str (converted_str, tmp_len, cp_display, charset, &converted_str_len);
120 #endif
121 if (just_letters) {
122 if (str_isalnum (converted_str) && !str_isdigit (converted_str))
123 *just_letters = TRUE;
124 else
125 *just_letters = FALSE;
127 #ifdef HAVE_CHARSET
128 g_free (converted_str);
129 return converted_str2;
130 #else
131 return converted_str;
132 #endif
135 /* --------------------------------------------------------------------------------------------- */
137 mc_search__get_char (mc_search_t * lc_mc_search, const void *user_data, gsize current_pos)
139 char *data;
140 if (lc_mc_search->search_fn)
141 return (lc_mc_search->search_fn) (user_data, current_pos);
143 data = (char *) user_data;
144 return (int) (unsigned char) data[current_pos];
147 /* --------------------------------------------------------------------------------------------- */
150 GString *
151 mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len)
153 GString *ret;
154 #ifdef HAVE_CHARSET
155 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
156 gsize converted_str_len;
157 gsize tmp_len;
159 if (charset == NULL)
160 charset = cp_source;
162 tmp_str2 = converted_str =
163 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
165 tmp_len = converted_str_len + 1;
167 tmp_str3 = tmp_str1 = g_strdup (converted_str);
169 while (str_tolower (tmp_str1, &tmp_str2, &tmp_len))
170 tmp_str1 += str_length_char (tmp_str1);
172 g_free (tmp_str3);
173 tmp_str2 =
174 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
175 g_free (converted_str);
177 ret = g_string_new_len (tmp_str2, tmp_len);
178 g_free (tmp_str2);
179 return ret;
180 #else
181 const gchar *tmp_str1 = str;
182 gchar *converted_str, *tmp_str2;
183 gsize converted_str_len = str_len + 1;
185 (void) charset;
187 tmp_str2 = converted_str = g_strndup (str, str_len);
189 while (str_tolower (tmp_str1, &tmp_str2, &converted_str_len))
190 tmp_str1 += str_length_char (tmp_str1);
192 ret = g_string_new_len (converted_str, str_len);
193 g_free (converted_str);
194 return ret;
195 #endif
198 /* --------------------------------------------------------------------------------------------- */
200 GString *
201 mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len)
203 GString *ret;
204 #ifdef HAVE_CHARSET
205 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
206 gsize converted_str_len;
207 gsize tmp_len;
209 if (charset == NULL)
210 charset = cp_source;
212 tmp_str2 = converted_str =
213 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
215 tmp_len = converted_str_len + 1;
217 tmp_str3 = tmp_str1 = g_strdup (converted_str);
219 while (str_toupper (tmp_str1, &tmp_str2, &tmp_len))
220 tmp_str1 += str_length_char (tmp_str1);
222 g_free (tmp_str3);
224 tmp_str2 =
225 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
226 g_free (converted_str);
228 ret = g_string_new_len (tmp_str2, tmp_len);
229 g_free (tmp_str2);
230 return ret;
231 #else
233 const gchar *tmp_str1 = str;
234 gchar *converted_str, *tmp_str2;
235 gsize converted_str_len = str_len + 1;
237 (void) charset;
239 tmp_str2 = converted_str = g_strndup (str, str_len);
241 while (str_toupper (tmp_str1, &tmp_str2, &converted_str_len))
242 tmp_str1 += str_length_char (tmp_str1);
244 ret = g_string_new_len (converted_str, str_len);
245 g_free (converted_str);
246 return ret;
247 #endif
250 /* --------------------------------------------------------------------------------------------- */
252 gchar **
253 mc_search_get_types_strings_array (size_t *num)
255 gchar **ret;
256 int lc_index;
257 size_t n;
259 const mc_search_type_str_t *type_str;
260 const mc_search_type_str_t *types_str = mc_search_types_list_get (&n);
262 ret = g_try_new0 (char *, n + 1);
263 if (ret == NULL)
264 return NULL;
266 for (lc_index = 0, type_str = types_str;
267 type_str->str != NULL;
268 type_str++, lc_index++)
269 ret[lc_index] = g_strdup (type_str->str);
271 /* don't count last NULL item */
272 if (num != NULL)
273 *num = (size_t) lc_index;
275 return ret;
278 /* --------------------------------------------------------------------------------------------- */