3 Common share code for module.
5 Copyright (C) 2009, 2011
6 The Free Software Foundation, Inc.
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/>.
30 #include <sys/types.h>
32 #include "lib/global.h"
33 #include "lib/strutil.h"
34 #include "lib/search.h"
36 #include "lib/charsets.h"
41 /*** global variables ****************************************************************************/
43 const char *STR_E_NOTFOUND
= N_("Search string not found");
44 const char *STR_E_UNKNOWN_TYPE
= N_("Not implemented yet");
45 const char *STR_E_RPL_NOT_EQ_TO_FOUND
=
46 N_("Num of replace tokens not equal to num of found tokens");
47 const char *STR_E_RPL_INVALID_TOKEN
= N_("Invalid token number %d");
49 /*** file scope macro definitions ****************************************************************/
51 /*** file scope type declarations ****************************************************************/
53 /*** file scope variables ************************************************************************/
55 /*** file scope functions ************************************************************************/
57 /*** public functions ****************************************************************************/
60 mc_search__recode_str (const char *str
, gsize str_len
,
61 const char *charset_from
, const char *charset_to
, gsize
* bytes_written
)
67 if (charset_from
== NULL
|| charset_to
== NULL
|| !strcmp (charset_to
, charset_from
))
69 *bytes_written
= str_len
;
70 return g_strndup (str
, str_len
);
73 conv
= g_iconv_open (charset_to
, charset_from
);
74 if (conv
== INVALID_CONV
)
76 *bytes_written
= str_len
;
77 return g_strndup (str
, str_len
);
80 ret
= g_convert_with_iconv (str
, str_len
, conv
, &bytes_read
, bytes_written
, NULL
);
85 *bytes_written
= str_len
;
86 return g_strndup (str
, str_len
);
92 /* --------------------------------------------------------------------------------------------- */
95 mc_search__get_one_symbol (const char *charset
, const char *str
, gsize str_len
,
96 gboolean
* just_letters
)
98 gchar
*converted_str
, *next_char
;
102 gsize converted_str_len
;
103 gchar
*converted_str2
;
108 converted_str
= mc_search__recode_str (str
, str_len
, charset
, cp_display
, &converted_str_len
);
112 converted_str
= g_strndup (str
, str_len
);
115 next_char
= (char *) str_cget_next_char (converted_str
);
117 tmp_len
= next_char
- converted_str
;
119 converted_str
[tmp_len
] = '\0';
123 mc_search__recode_str (converted_str
, tmp_len
, cp_display
, charset
, &converted_str_len
);
127 if (str_isalnum (converted_str
) && !str_isdigit (converted_str
))
128 *just_letters
= TRUE
;
130 *just_letters
= FALSE
;
133 g_free (converted_str
);
134 return converted_str2
;
136 return converted_str
;
140 /* --------------------------------------------------------------------------------------------- */
143 mc_search__get_char (mc_search_t
* lc_mc_search
, const void *user_data
, gsize current_pos
,
148 if (lc_mc_search
->search_fn
!= NULL
)
149 return lc_mc_search
->search_fn (user_data
, current_pos
, current_char
);
151 data
= (unsigned char *) user_data
;
152 *current_char
= (int) data
[current_pos
];
153 return (*current_char
== 0) ? MC_SEARCH_CB_ABORT
: MC_SEARCH_CB_OK
;
156 /* --------------------------------------------------------------------------------------------- */
159 mc_search__tolower_case_str (const char *charset
, const char *str
, gsize str_len
)
163 gchar
*converted_str
, *tmp_str1
, *tmp_str2
, *tmp_str3
;
164 gsize converted_str_len
;
170 tmp_str2
= converted_str
=
171 mc_search__recode_str (str
, str_len
, charset
, cp_display
, &converted_str_len
);
173 tmp_len
= converted_str_len
+ 1;
175 tmp_str3
= tmp_str1
= g_strdup (converted_str
);
177 while (str_tolower (tmp_str1
, &tmp_str2
, &tmp_len
))
178 tmp_str1
+= str_length_char (tmp_str1
);
182 mc_search__recode_str (converted_str
, converted_str_len
, cp_display
, charset
, &tmp_len
);
183 g_free (converted_str
);
185 ret
= g_string_new_len (tmp_str2
, tmp_len
);
189 const gchar
*tmp_str1
= str
;
190 gchar
*converted_str
, *tmp_str2
;
191 gsize converted_str_len
= str_len
+ 1;
195 tmp_str2
= converted_str
= g_strndup (str
, str_len
);
197 while (str_tolower (tmp_str1
, &tmp_str2
, &converted_str_len
))
198 tmp_str1
+= str_length_char (tmp_str1
);
200 ret
= g_string_new_len (converted_str
, str_len
);
201 g_free (converted_str
);
206 /* --------------------------------------------------------------------------------------------- */
209 mc_search__toupper_case_str (const char *charset
, const char *str
, gsize str_len
)
213 gchar
*converted_str
, *tmp_str1
, *tmp_str2
, *tmp_str3
;
214 gsize converted_str_len
;
220 tmp_str2
= converted_str
=
221 mc_search__recode_str (str
, str_len
, charset
, cp_display
, &converted_str_len
);
223 tmp_len
= converted_str_len
+ 1;
225 tmp_str3
= tmp_str1
= g_strdup (converted_str
);
227 while (str_toupper (tmp_str1
, &tmp_str2
, &tmp_len
))
228 tmp_str1
+= str_length_char (tmp_str1
);
233 mc_search__recode_str (converted_str
, converted_str_len
, cp_display
, charset
, &tmp_len
);
234 g_free (converted_str
);
236 ret
= g_string_new_len (tmp_str2
, tmp_len
);
241 const gchar
*tmp_str1
= str
;
242 gchar
*converted_str
, *tmp_str2
;
243 gsize converted_str_len
= str_len
+ 1;
247 tmp_str2
= converted_str
= g_strndup (str
, str_len
);
249 while (str_toupper (tmp_str1
, &tmp_str2
, &converted_str_len
))
250 tmp_str1
+= str_length_char (tmp_str1
);
252 ret
= g_string_new_len (converted_str
, str_len
);
253 g_free (converted_str
);
258 /* --------------------------------------------------------------------------------------------- */
261 mc_search_get_types_strings_array (size_t * num
)
267 const mc_search_type_str_t
*type_str
;
268 const mc_search_type_str_t
*types_str
= mc_search_types_list_get (&n
);
270 ret
= g_try_new0 (char *, n
+ 1);
274 for (lc_index
= 0, type_str
= types_str
; type_str
->str
!= NULL
; type_str
++, lc_index
++)
275 ret
[lc_index
] = g_strdup (type_str
->str
);
277 /* don't count last NULL item */
279 *num
= (size_t) lc_index
;
284 /* --------------------------------------------------------------------------------------------- */