3 Common share code for module.
5 Copyright (C) 2009 The Free Software Foundation, Inc.
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,
31 #include <sys/types.h>
33 #include "lib/global.h"
34 #include "lib/strutil.h"
35 #include "lib/search.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 ****************************************************************************/
58 mc_search__recode_str (const char *str
, gsize str_len
,
59 const char *charset_from
, const char *charset_to
, gsize
* bytes_written
)
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
);
80 *bytes_written
= str_len
;
81 return g_strndup (str
, str_len
);
87 /* --------------------------------------------------------------------------------------------- */
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
;
98 gsize converted_str_len
;
99 gchar
*converted_str2
;
104 converted_str
= mc_search__recode_str (str
, str_len
, charset
, cp_display
, &converted_str_len
);
108 converted_str
= g_strndup (str
, str_len
);
111 next_char
= (char *) str_cget_next_char (converted_str
);
113 tmp_len
= next_char
- converted_str
;
115 converted_str
[tmp_len
] = '\0';
119 mc_search__recode_str (converted_str
, tmp_len
, cp_display
, charset
, &converted_str_len
);
122 if (str_isalnum (converted_str
) && !str_isdigit (converted_str
))
123 *just_letters
= TRUE
;
125 *just_letters
= FALSE
;
128 g_free (converted_str
);
129 return converted_str2
;
131 return converted_str
;
135 /* --------------------------------------------------------------------------------------------- */
137 mc_search__get_char (mc_search_t
* lc_mc_search
, const void *user_data
, gsize current_pos
)
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 /* --------------------------------------------------------------------------------------------- */
151 mc_search__tolower_case_str (const char *charset
, const char *str
, gsize str_len
)
155 gchar
*converted_str
, *tmp_str1
, *tmp_str2
, *tmp_str3
;
156 gsize converted_str_len
;
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
);
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
);
181 const gchar
*tmp_str1
= str
;
182 gchar
*converted_str
, *tmp_str2
;
183 gsize converted_str_len
= str_len
+ 1;
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
);
198 /* --------------------------------------------------------------------------------------------- */
201 mc_search__toupper_case_str (const char *charset
, const char *str
, gsize str_len
)
205 gchar
*converted_str
, *tmp_str1
, *tmp_str2
, *tmp_str3
;
206 gsize converted_str_len
;
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
);
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
);
233 const gchar
*tmp_str1
= str
;
234 gchar
*converted_str
, *tmp_str2
;
235 gsize converted_str_len
= str_len
+ 1;
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
);
250 /* --------------------------------------------------------------------------------------------- */
253 mc_search_get_types_strings_array (size_t *num
)
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);
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 */
273 *num
= (size_t) lc_index
;
278 /* --------------------------------------------------------------------------------------------- */