3 Common share code for module.
5 Copyright (C) 2009-2024
6 Free Software Foundation, Inc.
9 Slava Zanko <slavazanko@gmail.com>, 2009, 2011
10 Andrew Borodin <aborodin@vmail.ru>, 2013
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include <sys/types.h>
33 #include "lib/global.h"
34 #include "lib/strutil.h"
35 #include "lib/search.h"
37 #include "lib/charsets.h"
42 /*** global variables ****************************************************************************/
45 const char *STR_E_NOTFOUND
= N_("Search string not found");
46 const char *STR_E_UNKNOWN_TYPE
= N_("Not implemented yet");
47 const char *STR_E_RPL_NOT_EQ_TO_FOUND
=
48 N_("Num of replace tokens not equal to num of found tokens");
49 const char *STR_E_RPL_INVALID_TOKEN
= N_("Invalid token number %d");
52 /*** file scope macro definitions ****************************************************************/
54 /*** file scope type declarations ****************************************************************/
56 typedef gboolean (*case_conv_fn
) (const char *ch
, char **out
, size_t *remain
);
58 /*** forward declarations (file scope functions) *************************************************/
60 /*** file scope variables ************************************************************************/
62 /* --------------------------------------------------------------------------------------------- */
63 /*** file scope functions ************************************************************************/
64 /* --------------------------------------------------------------------------------------------- */
67 mc_search__change_case_str (const char *charset
, const GString
*str
, case_conv_fn case_conv
)
75 GString
*converted_str
;
80 converted_str
= mc_search__recode_str (str
->str
, str
->len
, charset
, cp_display
);
82 dst_len
= converted_str
->len
+ 1; /* +1 is required for str_toupper/str_tolower */
83 dst_str
= g_malloc (dst_len
);
85 for (src_ptr
= converted_str
->str
, dst_ptr
= dst_str
;
86 case_conv (src_ptr
, &dst_ptr
, &dst_len
); src_ptr
+= str_length_char (src_ptr
))
90 dst_len
= converted_str
->len
;
91 g_string_free (converted_str
, TRUE
);
93 ret
= mc_search__recode_str (dst_str
, dst_len
, cp_display
, charset
);
98 dst_len
= str
->len
+ 1; /* +1 is required for str_toupper/str_tolower */
99 dst_str
= g_malloc (dst_len
);
101 for (src_ptr
= str
->str
, dst_ptr
= dst_str
;
102 case_conv (src_ptr
, &dst_ptr
, &dst_len
); src_ptr
+= str_length_char (src_ptr
))
106 ret
= g_string_new_len (dst_str
, dst_len
);
112 /* --------------------------------------------------------------------------------------------- */
113 /*** public functions ****************************************************************************/
114 /* --------------------------------------------------------------------------------------------- */
117 mc_search__recode_str (const char *str
, gsize str_len
, const char *charset_from
,
118 const char *charset_to
)
122 if (charset_from
!= NULL
&& charset_to
!= NULL
123 && g_ascii_strcasecmp (charset_to
, charset_from
) != 0)
127 conv
= g_iconv_open (charset_to
, charset_from
);
128 if (conv
!= INVALID_CONV
)
131 gsize bytes_read
= 0;
132 gsize bytes_written
= 0;
134 val
= g_convert_with_iconv (str
, str_len
, conv
, &bytes_read
, &bytes_written
, NULL
);
136 g_iconv_close (conv
);
140 ret
= g_string_new_len (val
, bytes_written
);
147 ret
= g_string_new_len (str
, str_len
);
152 /* --------------------------------------------------------------------------------------------- */
155 mc_search__get_one_symbol (const char *charset
, const char *str
, gsize str_len
,
156 gboolean
*just_letters
)
158 GString
*converted_str
;
159 const gchar
*next_char
;
162 GString
*converted_str2
;
167 converted_str
= mc_search__recode_str (str
, str_len
, charset
, cp_display
);
171 converted_str
= g_string_new_len (str
, str_len
);
174 next_char
= str_cget_next_char (converted_str
->str
);
175 g_string_set_size (converted_str
, (gsize
) (next_char
- converted_str
->str
));
179 mc_search__recode_str (converted_str
->str
, converted_str
->len
, cp_display
, charset
);
181 if (just_letters
!= NULL
)
182 *just_letters
= str_isalnum (converted_str
->str
) && !str_isdigit (converted_str
->str
);
184 g_string_free (converted_str
, TRUE
);
185 return converted_str2
;
187 return converted_str
;
191 /* --------------------------------------------------------------------------------------------- */
194 mc_search__tolower_case_str (const char *charset
, const GString
*str
)
196 return mc_search__change_case_str (charset
, str
, str_tolower
);
199 /* --------------------------------------------------------------------------------------------- */
202 mc_search__toupper_case_str (const char *charset
, const GString
*str
)
204 return mc_search__change_case_str (charset
, str
, str_toupper
);
207 /* --------------------------------------------------------------------------------------------- */
210 mc_search_get_types_strings_array (size_t *num
)
216 const mc_search_type_str_t
*type_str
;
217 const mc_search_type_str_t
*types_str
= mc_search_types_list_get (&n
);
219 ret
= g_try_new0 (char *, n
+ 1);
223 for (lc_index
= 0, type_str
= types_str
; type_str
->str
!= NULL
; type_str
++, lc_index
++)
224 ret
[lc_index
] = g_strdup (type_str
->str
);
226 /* don't count last NULL item */
228 *num
= (size_t) lc_index
;
233 /* --------------------------------------------------------------------------------------------- */