Ticket 1551: Update GPL version from 2 to 3
[midnight-commander.git] / lib / search / lib.c
blob05cfef77b11a189d4933f307654bd9dd2da1089c
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"
36 #include "internal.h"
37 #include "lib/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 =
44 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))
67 *bytes_written = str_len;
68 return g_strndup (str, str_len);
71 conv = g_iconv_open (charset_to, charset_from);
72 if (conv == INVALID_CONV)
74 *bytes_written = str_len;
75 return g_strndup (str, str_len);
78 ret = g_convert_with_iconv (str, str_len, conv, &bytes_read, bytes_written, NULL);
79 g_iconv_close (conv);
81 if (ret == NULL)
83 *bytes_written = str_len;
84 return g_strndup (str, str_len);
87 return ret;
90 /* --------------------------------------------------------------------------------------------- */
92 gchar *
93 mc_search__get_one_symbol (const char *charset, const char *str, gsize str_len,
94 gboolean * just_letters)
96 gchar *converted_str, *next_char;
98 gsize tmp_len;
99 #ifdef HAVE_CHARSET
100 gsize converted_str_len;
101 gchar *converted_str2;
103 if (charset == NULL)
104 charset = cp_source;
106 converted_str = mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
107 #else
108 (void) charset;
110 converted_str = g_strndup (str, str_len);
111 #endif
113 next_char = (char *) str_cget_next_char (converted_str);
115 tmp_len = next_char - converted_str;
117 converted_str[tmp_len] = '\0';
119 #ifdef HAVE_CHARSET
120 converted_str2 =
121 mc_search__recode_str (converted_str, tmp_len, cp_display, charset, &converted_str_len);
122 #endif
123 if (just_letters)
125 if (str_isalnum (converted_str) && !str_isdigit (converted_str))
126 *just_letters = TRUE;
127 else
128 *just_letters = FALSE;
130 #ifdef HAVE_CHARSET
131 g_free (converted_str);
132 return converted_str2;
133 #else
134 return converted_str;
135 #endif
138 /* --------------------------------------------------------------------------------------------- */
141 mc_search__get_char (mc_search_t * lc_mc_search, const void *user_data, gsize current_pos)
143 char *data;
144 if (lc_mc_search->search_fn)
145 return (lc_mc_search->search_fn) (user_data, current_pos);
147 data = (char *) user_data;
148 return (int) (unsigned char) data[current_pos];
151 /* --------------------------------------------------------------------------------------------- */
153 GString *
154 mc_search__tolower_case_str (const char *charset, const char *str, gsize str_len)
156 GString *ret;
157 #ifdef HAVE_CHARSET
158 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
159 gsize converted_str_len;
160 gsize tmp_len;
162 if (charset == NULL)
163 charset = cp_source;
165 tmp_str2 = converted_str =
166 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
168 tmp_len = converted_str_len + 1;
170 tmp_str3 = tmp_str1 = g_strdup (converted_str);
172 while (str_tolower (tmp_str1, &tmp_str2, &tmp_len))
173 tmp_str1 += str_length_char (tmp_str1);
175 g_free (tmp_str3);
176 tmp_str2 =
177 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
178 g_free (converted_str);
180 ret = g_string_new_len (tmp_str2, tmp_len);
181 g_free (tmp_str2);
182 return ret;
183 #else
184 const gchar *tmp_str1 = str;
185 gchar *converted_str, *tmp_str2;
186 gsize converted_str_len = str_len + 1;
188 (void) charset;
190 tmp_str2 = converted_str = g_strndup (str, str_len);
192 while (str_tolower (tmp_str1, &tmp_str2, &converted_str_len))
193 tmp_str1 += str_length_char (tmp_str1);
195 ret = g_string_new_len (converted_str, str_len);
196 g_free (converted_str);
197 return ret;
198 #endif
201 /* --------------------------------------------------------------------------------------------- */
203 GString *
204 mc_search__toupper_case_str (const char *charset, const char *str, gsize str_len)
206 GString *ret;
207 #ifdef HAVE_CHARSET
208 gchar *converted_str, *tmp_str1, *tmp_str2, *tmp_str3;
209 gsize converted_str_len;
210 gsize tmp_len;
212 if (charset == NULL)
213 charset = cp_source;
215 tmp_str2 = converted_str =
216 mc_search__recode_str (str, str_len, charset, cp_display, &converted_str_len);
218 tmp_len = converted_str_len + 1;
220 tmp_str3 = tmp_str1 = g_strdup (converted_str);
222 while (str_toupper (tmp_str1, &tmp_str2, &tmp_len))
223 tmp_str1 += str_length_char (tmp_str1);
225 g_free (tmp_str3);
227 tmp_str2 =
228 mc_search__recode_str (converted_str, converted_str_len, cp_display, charset, &tmp_len);
229 g_free (converted_str);
231 ret = g_string_new_len (tmp_str2, tmp_len);
232 g_free (tmp_str2);
233 return ret;
234 #else
236 const gchar *tmp_str1 = str;
237 gchar *converted_str, *tmp_str2;
238 gsize converted_str_len = str_len + 1;
240 (void) charset;
242 tmp_str2 = converted_str = g_strndup (str, str_len);
244 while (str_toupper (tmp_str1, &tmp_str2, &converted_str_len))
245 tmp_str1 += str_length_char (tmp_str1);
247 ret = g_string_new_len (converted_str, str_len);
248 g_free (converted_str);
249 return ret;
250 #endif
253 /* --------------------------------------------------------------------------------------------- */
255 gchar **
256 mc_search_get_types_strings_array (size_t * num)
258 gchar **ret;
259 int lc_index;
260 size_t n;
262 const mc_search_type_str_t *type_str;
263 const mc_search_type_str_t *types_str = mc_search_types_list_get (&n);
265 ret = g_try_new0 (char *, n + 1);
266 if (ret == NULL)
267 return NULL;
269 for (lc_index = 0, type_str = types_str; type_str->str != NULL; type_str++, lc_index++)
270 ret[lc_index] = g_strdup (type_str->str);
272 /* don't count last NULL item */
273 if (num != NULL)
274 *num = (size_t) lc_index;
276 return ret;
279 /* --------------------------------------------------------------------------------------------- */