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"
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 ****************************************************************************/
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
))
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
);
83 *bytes_written
= str_len
;
84 return g_strndup (str
, str_len
);
90 /* --------------------------------------------------------------------------------------------- */
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
;
100 gsize converted_str_len
;
101 gchar
*converted_str2
;
106 converted_str
= mc_search__recode_str (str
, str_len
, charset
, cp_display
, &converted_str_len
);
110 converted_str
= g_strndup (str
, str_len
);
113 next_char
= (char *) str_cget_next_char (converted_str
);
115 tmp_len
= next_char
- converted_str
;
117 converted_str
[tmp_len
] = '\0';
121 mc_search__recode_str (converted_str
, tmp_len
, cp_display
, charset
, &converted_str_len
);
125 if (str_isalnum (converted_str
) && !str_isdigit (converted_str
))
126 *just_letters
= TRUE
;
128 *just_letters
= FALSE
;
131 g_free (converted_str
);
132 return converted_str2
;
134 return converted_str
;
138 /* --------------------------------------------------------------------------------------------- */
141 mc_search__get_char (mc_search_t
* lc_mc_search
, const void *user_data
, gsize current_pos
)
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 /* --------------------------------------------------------------------------------------------- */
154 mc_search__tolower_case_str (const char *charset
, const char *str
, gsize str_len
)
158 gchar
*converted_str
, *tmp_str1
, *tmp_str2
, *tmp_str3
;
159 gsize converted_str_len
;
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
);
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
);
184 const gchar
*tmp_str1
= str
;
185 gchar
*converted_str
, *tmp_str2
;
186 gsize converted_str_len
= str_len
+ 1;
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
);
201 /* --------------------------------------------------------------------------------------------- */
204 mc_search__toupper_case_str (const char *charset
, const char *str
, gsize str_len
)
208 gchar
*converted_str
, *tmp_str1
, *tmp_str2
, *tmp_str3
;
209 gsize converted_str_len
;
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
);
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
);
236 const gchar
*tmp_str1
= str
;
237 gchar
*converted_str
, *tmp_str2
;
238 gsize converted_str_len
= str_len
+ 1;
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
);
253 /* --------------------------------------------------------------------------------------------- */
256 mc_search_get_types_strings_array (size_t * num
)
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);
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 */
274 *num
= (size_t) lc_index
;
279 /* --------------------------------------------------------------------------------------------- */