1 /* common strings utilities
2 Copyright (C) 2007 Free Software Foundation, Inc.
7 The file_date routine is mostly from GNU's fileutils package,
8 written by Richard Stallman and David MacKenzie.
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
33 #include "lib/global.h"
34 #include "lib/strutil.h"
36 /*names, that are used for utf-8 */
37 static const char *str_utf8_encodings
[] = {
43 /* standard 8bit encodings, no wide or multibytes characters */
44 static const char *str_8bit_encodings
[] = {
63 /* terminal encoding */
64 static char *codeset
= NULL
;
65 /* function for encoding specific operations */
66 static struct str_class used_class
;
68 GIConv str_cnv_to_term
;
69 GIConv str_cnv_from_term
;
70 GIConv str_cnv_not_convert
;
72 /* if enc is same encoding like on terminal */
74 str_test_not_convert (const char *enc
)
76 return g_ascii_strcasecmp (enc
, codeset
) == 0;
80 str_crt_conv_to (const char *to_enc
)
82 return (!str_test_not_convert (to_enc
)) ? g_iconv_open (to_enc
, codeset
) : str_cnv_not_convert
;
86 str_crt_conv_from (const char *from_enc
)
88 return (!str_test_not_convert (from_enc
))
89 ? g_iconv_open (codeset
, from_enc
) : str_cnv_not_convert
;
93 str_close_conv (GIConv conv
)
95 if (conv
!= str_cnv_not_convert
)
100 _str_convert (GIConv coder
, const char *string
, int size
, GString
* buffer
)
102 estr_t state
= ESTR_SUCCESS
;
103 gchar
*tmp_buff
= NULL
;
105 gsize bytes_read
= 0;
106 gsize bytes_written
= 0;
107 GError
*error
= NULL
;
110 if (coder
== INVALID_CONV
)
113 if (string
== NULL
|| buffer
== NULL
)
117 if (! used_class.is_valid_string (string))
124 size
= strlen (string
);
128 left
= strlen (string
);
134 g_iconv (coder
, NULL
, NULL
, NULL
, NULL
);
138 tmp_buff
= g_convert_with_iconv ((const gchar
*) string
,
139 left
, coder
, &bytes_read
, &bytes_written
, &error
);
142 int code
= error
->code
;
144 g_error_free (error
);
149 case G_CONVERT_ERROR_NO_CONVERSION
:
150 /* Conversion between the requested character sets is not supported. */
151 tmp_buff
= g_strnfill (strlen (string
), '?');
152 g_string_append (buffer
, tmp_buff
);
156 case G_CONVERT_ERROR_ILLEGAL_SEQUENCE
:
157 /* Invalid byte sequence in conversion input. */
158 if ((tmp_buff
== NULL
) && (bytes_read
!= 0))
159 /* recode valid byte sequence */
160 tmp_buff
= g_convert_with_iconv ((const gchar
*) string
,
161 bytes_read
, coder
, NULL
, NULL
, NULL
);
163 if (tmp_buff
!= NULL
)
165 g_string_append (buffer
, tmp_buff
);
169 if ((int) bytes_read
< left
)
171 string
+= bytes_read
+ 1;
172 size
-= (bytes_read
+ 1);
173 left
-= (bytes_read
+ 1);
174 g_string_append_c (buffer
, *(string
- 1));
180 state
= ESTR_PROBLEM
;
183 case G_CONVERT_ERROR_PARTIAL_INPUT
:
184 /* Partial character sequence at end of input. */
185 g_string_append (buffer
, tmp_buff
);
187 if ((int) bytes_read
< left
)
189 left
= left
- bytes_read
;
190 tmp_buff
= g_strnfill (left
, '?');
191 g_string_append (buffer
, tmp_buff
);
196 case G_CONVERT_ERROR_BAD_URI
: /* Don't know how handle this error :( */
197 case G_CONVERT_ERROR_NOT_ABSOLUTE_PATH
: /* Don't know how handle this error :( */
198 case G_CONVERT_ERROR_FAILED
: /* Conversion failed for some reason. */
206 if (tmp_buff
!= NULL
)
210 g_string_append (buffer
, tmp_buff
);
212 string
+= bytes_read
;
218 g_string_append (buffer
, string
);
224 g_string_append (buffer
, string
);
233 str_convert (GIConv coder
, const char *string
, GString
* buffer
)
235 return _str_convert (coder
, string
, -1, buffer
);
239 str_nconvert (GIConv coder
, const char *string
, int size
, GString
* buffer
)
241 return _str_convert (coder
, string
, size
, buffer
);
245 str_conv_gerror_message (GError
* error
, const char *def_msg
)
247 return used_class
.conv_gerror_message (error
, def_msg
);
251 str_vfs_convert_from (GIConv coder
, const char *string
, GString
* buffer
)
255 if (coder
== str_cnv_not_convert
)
257 g_string_append (buffer
, string
!= NULL
? string
: "");
258 result
= ESTR_SUCCESS
;
261 result
= _str_convert (coder
, string
, -1, buffer
);
267 str_vfs_convert_to (GIConv coder
, const char *string
, int size
, GString
* buffer
)
269 return used_class
.vfs_convert_to (coder
, string
, size
, buffer
);
273 str_printf (GString
* buffer
, const char *format
, ...)
276 va_start (ap
, format
);
277 #if GLIB_CHECK_VERSION (2, 14, 0)
278 g_string_append_vprintf (buffer
, format
, ap
);
282 tmp
= g_strdup_vprintf (format
, ap
);
283 g_string_append (buffer
, tmp
);
291 str_insert_replace_char (GString
* buffer
)
293 used_class
.insert_replace_char (buffer
);
297 str_translate_char (GIConv conv
, const char *keys
, size_t ch_size
, char *output
, size_t out_size
)
302 g_iconv (conv
, NULL
, NULL
, NULL
, NULL
);
304 left
= (ch_size
== (size_t) (-1)) ? strlen (keys
) : ch_size
;
306 cnv
= g_iconv (conv
, (gchar
**) & keys
, &left
, &output
, &out_size
);
307 if (cnv
== (size_t) (-1))
309 return (errno
== EINVAL
) ? ESTR_PROBLEM
: ESTR_FAILURE
;
320 str_detect_termencoding (void)
322 return (nl_langinfo (CODESET
));
326 str_test_encoding_class (const char *encoding
, const char **table
)
330 if (encoding
== NULL
)
333 for (t
= 0; table
[t
] != NULL
; t
++)
335 result
+= (g_ascii_strncasecmp (encoding
, table
[t
], strlen (table
[t
])) == 0);
341 str_choose_str_functions (void)
343 if (str_test_encoding_class (codeset
, str_utf8_encodings
))
345 used_class
= str_utf8_init ();
347 else if (str_test_encoding_class (codeset
, str_8bit_encodings
))
349 used_class
= str_8bit_init ();
353 used_class
= str_ascii_init ();
358 str_isutf8 (const char *codeset_name
)
361 if (str_test_encoding_class (codeset_name
, str_utf8_encodings
))
369 str_init_strings (const char *termenc
)
371 codeset
= g_strdup ((termenc
!= NULL
) ? termenc
: str_detect_termencoding ());
373 str_cnv_not_convert
= g_iconv_open (codeset
, codeset
);
374 if (str_cnv_not_convert
== INVALID_CONV
)
379 codeset
= g_strdup (str_detect_termencoding ());
380 str_cnv_not_convert
= g_iconv_open (codeset
, codeset
);
383 if (str_cnv_not_convert
== INVALID_CONV
)
386 codeset
= g_strdup ("ascii");
387 str_cnv_not_convert
= g_iconv_open (codeset
, codeset
);
391 str_cnv_to_term
= str_cnv_not_convert
;
392 str_cnv_from_term
= str_cnv_not_convert
;
394 str_choose_str_functions ();
398 str_uninit_strings (void)
400 if (str_cnv_not_convert
!= INVALID_CONV
)
401 g_iconv_close (str_cnv_not_convert
);
406 str_term_form (const char *text
)
408 return used_class
.term_form (text
);
412 str_fit_to_term (const char *text
, int width
, align_crt_t just_mode
)
414 return used_class
.fit_to_term (text
, width
, just_mode
);
418 str_term_trim (const char *text
, int width
)
420 return used_class
.term_trim (text
, width
);
424 str_term_substring (const char *text
, int start
, int width
)
426 return used_class
.term_substring (text
, start
, width
);
430 str_get_next_char (char *text
)
433 used_class
.cnext_char ((const char **) &text
);
438 str_cget_next_char (const char *text
)
440 used_class
.cnext_char (&text
);
445 str_next_char (char **text
)
447 used_class
.cnext_char ((const char **) text
);
451 str_cnext_char (const char **text
)
453 used_class
.cnext_char (text
);
457 str_get_prev_char (char *text
)
459 used_class
.cprev_char ((const char **) &text
);
464 str_cget_prev_char (const char *text
)
466 used_class
.cprev_char (&text
);
471 str_prev_char (char **text
)
473 used_class
.cprev_char ((const char **) text
);
477 str_cprev_char (const char **text
)
479 used_class
.cprev_char (text
);
483 str_get_next_char_safe (char *text
)
485 used_class
.cnext_char_safe ((const char **) &text
);
490 str_cget_next_char_safe (const char *text
)
492 used_class
.cnext_char_safe (&text
);
497 str_next_char_safe (char **text
)
499 used_class
.cnext_char_safe ((const char **) text
);
503 str_cnext_char_safe (const char **text
)
505 used_class
.cnext_char_safe (text
);
509 str_get_prev_char_safe (char *text
)
511 used_class
.cprev_char_safe ((const char **) &text
);
516 str_cget_prev_char_safe (const char *text
)
518 used_class
.cprev_char_safe (&text
);
523 str_prev_char_safe (char **text
)
525 used_class
.cprev_char_safe ((const char **) text
);
529 str_cprev_char_safe (const char **text
)
531 used_class
.cprev_char_safe (text
);
535 str_next_noncomb_char (char **text
)
537 return used_class
.cnext_noncomb_char ((const char **) text
);
541 str_cnext_noncomb_char (const char **text
)
543 return used_class
.cnext_noncomb_char (text
);
547 str_prev_noncomb_char (char **text
, const char *begin
)
549 return used_class
.cprev_noncomb_char ((const char **) text
, begin
);
553 str_cprev_noncomb_char (const char **text
, const char *begin
)
555 return used_class
.cprev_noncomb_char (text
, begin
);
559 str_is_valid_char (const char *ch
, size_t size
)
561 return used_class
.is_valid_char (ch
, size
);
565 str_term_width1 (const char *text
)
567 return used_class
.term_width1 (text
);
571 str_term_width2 (const char *text
, size_t length
)
573 return used_class
.term_width2 (text
, length
);
577 str_term_char_width (const char *text
)
579 return used_class
.term_char_width (text
);
583 str_offset_to_pos (const char *text
, size_t length
)
585 return used_class
.offset_to_pos (text
, length
);
589 str_length (const char *text
)
591 return used_class
.length (text
);
595 str_length_char (const char *text
)
597 return str_cget_next_char_safe (text
) - text
;
601 str_length2 (const char *text
, int size
)
603 return used_class
.length2 (text
, size
);
607 str_length_noncomb (const char *text
)
609 return used_class
.length_noncomb (text
);
613 str_column_to_pos (const char *text
, size_t pos
)
615 return used_class
.column_to_pos (text
, pos
);
619 str_isspace (const char *ch
)
621 return used_class
.isspace (ch
);
625 str_ispunct (const char *ch
)
627 return used_class
.ispunct (ch
);
631 str_isalnum (const char *ch
)
633 return used_class
.isalnum (ch
);
637 str_isdigit (const char *ch
)
639 return used_class
.isdigit (ch
);
643 str_toupper (const char *ch
, char **out
, size_t * remain
)
645 return used_class
.toupper (ch
, out
, remain
);
649 str_tolower (const char *ch
, char **out
, size_t * remain
)
651 return used_class
.tolower (ch
, out
, remain
);
655 str_isprint (const char *ch
)
657 return used_class
.isprint (ch
);
661 str_iscombiningmark (const char *ch
)
663 return used_class
.iscombiningmark (ch
);
667 str_trunc (const char *text
, int width
)
669 return used_class
.trunc (text
, width
);
673 str_create_search_needle (const char *needle
, int case_sen
)
675 return used_class
.create_search_needle (needle
, case_sen
);
680 str_release_search_needle (char *needle
, int case_sen
)
682 used_class
.release_search_needle (needle
, case_sen
);
686 str_search_first (const char *text
, const char *search
, int case_sen
)
688 return used_class
.search_first (text
, search
, case_sen
);
692 str_search_last (const char *text
, const char *search
, int case_sen
)
694 return used_class
.search_last (text
, search
, case_sen
);
698 str_is_valid_string (const char *text
)
700 return used_class
.is_valid_string (text
);
704 str_compare (const char *t1
, const char *t2
)
706 return used_class
.compare (t1
, t2
);
710 str_ncompare (const char *t1
, const char *t2
)
712 return used_class
.ncompare (t1
, t2
);
716 str_casecmp (const char *t1
, const char *t2
)
718 return used_class
.casecmp (t1
, t2
);
722 str_ncasecmp (const char *t1
, const char *t2
)
724 return used_class
.ncasecmp (t1
, t2
);
728 str_prefix (const char *text
, const char *prefix
)
730 return used_class
.prefix (text
, prefix
);
734 str_caseprefix (const char *text
, const char *prefix
)
736 return used_class
.caseprefix (text
, prefix
);
740 str_fix_string (char *text
)
742 used_class
.fix_string (text
);
746 str_create_key (const char *text
, int case_sen
)
748 return used_class
.create_key (text
, case_sen
);
752 str_create_key_for_filename (const char *text
, int case_sen
)
754 return used_class
.create_key_for_filename (text
, case_sen
);
758 str_key_collate (const char *t1
, const char *t2
, int case_sen
)
760 return used_class
.key_collate (t1
, t2
, case_sen
);
764 str_release_key (char *key
, int case_sen
)
766 used_class
.release_key (key
, case_sen
);
770 str_msg_term_size (const char *text
, int *lines
, int *columns
)
780 tmp
= g_strdup (text
);
785 q
= strchr (p
, '\n');
792 width
= str_term_width1 (p
);
793 if (width
> *columns
)
807 /* --------------------------------------------------------------------------------------------- */
810 strrstr_skip_count (const char *haystack
, const char *needle
, size_t skip_count
)
815 len
= strlen (haystack
);
819 semi
= g_strrstr_len (haystack
, len
, needle
);
822 len
= semi
- haystack
- 1;
824 while (skip_count
-- != 0);
828 /* --------------------------------------------------------------------------------------------- */