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.
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*/
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
))
83 ? g_iconv_open (to_enc
, codeset
) : str_cnv_not_convert
;
87 str_crt_conv_from (const char *from_enc
)
89 return (!str_test_not_convert (from_enc
))
90 ? g_iconv_open (codeset
, from_enc
) : str_cnv_not_convert
;
94 str_close_conv (GIConv conv
)
96 if (conv
!= str_cnv_not_convert
)
101 _str_convert (GIConv coder
, const char *string
, int size
, GString
* buffer
)
103 estr_t state
= ESTR_SUCCESS
;
104 gchar
*tmp_buff
= NULL
;
106 gsize bytes_read
= 0;
107 gsize bytes_written
= 0;
108 GError
*error
= NULL
;
111 if (coder
== INVALID_CONV
)
114 if (string
== NULL
|| buffer
== NULL
)
118 if (! used_class.is_valid_string (string))
125 size
= strlen (string
);
129 left
= strlen (string
);
135 g_iconv (coder
, NULL
, NULL
, NULL
, NULL
);
139 tmp_buff
= g_convert_with_iconv ((const gchar
*) string
,
143 &bytes_written
, &error
);
146 int code
= error
->code
;
148 g_error_free (error
);
153 case G_CONVERT_ERROR_NO_CONVERSION
:
154 /* Conversion between the requested character sets is not supported. */
155 tmp_buff
= g_strnfill (strlen (string
), '?');
156 g_string_append (buffer
, tmp_buff
);
160 case G_CONVERT_ERROR_ILLEGAL_SEQUENCE
:
161 /* Invalid byte sequence in conversion input. */
162 if ((tmp_buff
== NULL
) && (bytes_read
!= 0))
163 /* recode valid byte sequence */
164 tmp_buff
= g_convert_with_iconv ((const gchar
*) string
,
166 coder
, NULL
, NULL
, NULL
);
168 if (tmp_buff
!= NULL
)
170 g_string_append (buffer
, tmp_buff
);
174 if ((int)bytes_read
< left
)
176 string
+= bytes_read
+ 1;
177 size
-= (bytes_read
+ 1);
178 left
-= (bytes_read
+ 1);
179 g_string_append_c (buffer
, *(string
-1));
185 state
= ESTR_PROBLEM
;
188 case G_CONVERT_ERROR_PARTIAL_INPUT
:
189 /* Partial character sequence at end of input. */
190 g_string_append (buffer
, tmp_buff
);
192 if ((int)bytes_read
< left
)
194 left
= left
- bytes_read
;
195 tmp_buff
= g_strnfill (left
, '?');
196 g_string_append (buffer
, tmp_buff
);
201 case G_CONVERT_ERROR_BAD_URI
: /* Don't know how handle this error :( */
202 case G_CONVERT_ERROR_NOT_ABSOLUTE_PATH
: /* Don't know how handle this error :( */
203 case G_CONVERT_ERROR_FAILED
: /* Conversion failed for some reason. */
211 if (tmp_buff
!= NULL
)
215 g_string_append (buffer
, tmp_buff
);
217 string
+= bytes_read
;
223 g_string_append (buffer
, string
);
229 g_string_append (buffer
, string
);
238 str_convert (GIConv coder
, const char *string
, GString
* buffer
)
240 return _str_convert (coder
, string
, -1, buffer
);
244 str_nconvert (GIConv coder
, const char *string
, int size
, GString
* buffer
)
246 return _str_convert (coder
, string
, size
, buffer
);
250 str_conv_gerror_message (GError
*error
, const char *def_msg
)
252 return used_class
.conv_gerror_message (error
, def_msg
);
256 str_vfs_convert_from (GIConv coder
, const char *string
, GString
* buffer
)
260 if (coder
== str_cnv_not_convert
)
262 g_string_append (buffer
, string
!= NULL
? string
: "");
263 result
= ESTR_SUCCESS
;
266 result
= _str_convert (coder
, string
, -1, buffer
);
272 str_vfs_convert_to (GIConv coder
, const char *string
, int size
,
275 return used_class
.vfs_convert_to (coder
, string
, size
, buffer
);
279 str_printf (GString
* buffer
, const char *format
, ...)
282 va_start (ap
, format
);
283 #if GLIB_CHECK_VERSION (2, 14, 0)
284 g_string_append_vprintf (buffer
, format
, ap
);
288 tmp
= g_strdup_vprintf (format
, ap
);
289 g_string_append (buffer
, tmp
);
297 str_insert_replace_char (GString
* buffer
)
299 used_class
.insert_replace_char (buffer
);
303 str_translate_char (GIConv conv
, const char *keys
, size_t ch_size
,
304 char *output
, size_t out_size
)
309 g_iconv (conv
, NULL
, NULL
, NULL
, NULL
);
311 left
= (ch_size
== (size_t) (-1)) ? strlen (keys
) : ch_size
;
313 cnv
= g_iconv (conv
, (gchar
**) &keys
, &left
, &output
, &out_size
);
314 if (cnv
== (size_t)(-1)) {
315 return (errno
== EINVAL
) ? ESTR_PROBLEM
: ESTR_FAILURE
;
324 str_detect_termencoding (void)
326 return (nl_langinfo (CODESET
));
330 str_test_encoding_class (const char *encoding
, const char **table
)
334 if ( encoding
== NULL
)
337 for (t
= 0; table
[t
] != NULL
; t
++)
339 result
+= (g_ascii_strncasecmp (encoding
, table
[t
],
340 strlen (table
[t
])) == 0);
346 str_choose_str_functions ()
348 if (str_test_encoding_class (codeset
, str_utf8_encodings
))
350 used_class
= str_utf8_init ();
352 else if (str_test_encoding_class (codeset
, str_8bit_encodings
))
354 used_class
= str_8bit_init ();
358 used_class
= str_ascii_init ();
363 str_isutf8 (const char *codeset_name
)
366 if (str_test_encoding_class (codeset_name
, str_utf8_encodings
))
374 str_init_strings (const char *termenc
)
376 codeset
= g_strdup ((termenc
!= NULL
)
378 : str_detect_termencoding ());
380 str_cnv_not_convert
= g_iconv_open (codeset
, codeset
);
381 if (str_cnv_not_convert
== INVALID_CONV
)
386 codeset
= g_strdup (str_detect_termencoding ());
387 str_cnv_not_convert
= g_iconv_open (codeset
, codeset
);
390 if (str_cnv_not_convert
== INVALID_CONV
)
393 codeset
= g_strdup ("ascii");
394 str_cnv_not_convert
= g_iconv_open (codeset
, codeset
);
398 str_cnv_to_term
= str_cnv_not_convert
;
399 str_cnv_from_term
= str_cnv_not_convert
;
401 str_choose_str_functions ();
405 str_uninit_strings ()
407 g_iconv_close (str_cnv_not_convert
);
411 str_term_form (const char *text
)
413 return used_class
.term_form (text
);
417 str_fit_to_term (const char *text
, int width
, align_crt_t just_mode
)
419 return used_class
.fit_to_term (text
, width
, just_mode
);
423 str_term_trim (const char *text
, int width
)
425 return used_class
.term_trim (text
, width
);
429 str_msg_term_size (const char *text
, int *lines
, int *columns
)
431 used_class
.msg_term_size (text
, lines
, columns
);
435 str_term_substring (const char *text
, int start
, int width
)
437 return used_class
.term_substring (text
, start
, width
);
441 str_get_next_char (char *text
)
444 used_class
.cnext_char ((const char **) &text
);
449 str_cget_next_char (const char *text
)
451 used_class
.cnext_char(&text
);
456 str_next_char (char **text
)
458 used_class
.cnext_char ((const char **) text
);
462 str_cnext_char (const char **text
)
464 used_class
.cnext_char (text
);
468 str_get_prev_char (char *text
)
470 used_class
.cprev_char ((const char **) &text
);
475 str_cget_prev_char (const char *text
)
477 used_class
.cprev_char (&text
);
482 str_prev_char (char **text
)
484 used_class
.cprev_char ((const char **) text
);
488 str_cprev_char (const char **text
)
490 used_class
.cprev_char (text
);
494 str_get_next_char_safe (char *text
)
496 used_class
.cnext_char_safe ((const char **) &text
);
501 str_cget_next_char_safe (const char *text
)
503 used_class
.cnext_char_safe (&text
);
508 str_next_char_safe (char **text
)
510 used_class
.cnext_char_safe ((const char **) text
);
514 str_cnext_char_safe (const char **text
)
516 used_class
.cnext_char_safe (text
);
520 str_get_prev_char_safe (char *text
)
522 used_class
.cprev_char_safe ((const char **) &text
);
527 str_cget_prev_char_safe (const char *text
)
529 used_class
.cprev_char_safe (&text
);
534 str_prev_char_safe (char **text
)
536 used_class
.cprev_char_safe ((const char **) text
);
540 str_cprev_char_safe (const char **text
)
542 used_class
.cprev_char_safe (text
);
546 str_next_noncomb_char (char **text
)
548 return used_class
.cnext_noncomb_char ((const char **) text
);
552 str_cnext_noncomb_char (const char **text
)
554 return used_class
.cnext_noncomb_char (text
);
558 str_prev_noncomb_char (char **text
, const char *begin
)
560 return used_class
.cprev_noncomb_char ((const char **) text
, begin
);
564 str_cprev_noncomb_char (const char **text
, const char *begin
)
566 return used_class
.cprev_noncomb_char (text
, begin
);
570 str_is_valid_char (const char *ch
, size_t size
)
572 return used_class
.is_valid_char (ch
, size
);
576 str_term_width1 (const char *text
)
578 return used_class
.term_width1 (text
);
582 str_term_width2 (const char *text
, size_t length
)
584 return used_class
.term_width2 (text
, length
);
588 str_term_char_width (const char *text
)
590 return used_class
.term_char_width (text
);
594 str_offset_to_pos (const char *text
, size_t length
)
596 return used_class
.offset_to_pos (text
, length
);
600 str_length (const char *text
)
602 return used_class
.length (text
);
606 str_length_char (const char *text
)
608 return str_cget_next_char_safe (text
)-text
;
612 str_length2 (const char *text
, int size
)
614 return used_class
.length2 (text
, size
);
618 str_length_noncomb (const char *text
)
620 return used_class
.length_noncomb (text
);
624 str_column_to_pos (const char *text
, size_t pos
)
626 return used_class
.column_to_pos (text
, pos
);
630 str_isspace (const char *ch
)
632 return used_class
.isspace (ch
);
636 str_ispunct (const char *ch
)
638 return used_class
.ispunct (ch
);
642 str_isalnum (const char *ch
)
644 return used_class
.isalnum (ch
);
648 str_isdigit (const char *ch
)
650 return used_class
.isdigit (ch
);
654 str_toupper (const char *ch
, char **out
, size_t * remain
)
656 return used_class
.toupper (ch
, out
, remain
);
660 str_tolower (const char *ch
, char **out
, size_t * remain
)
662 return used_class
.tolower (ch
, out
, remain
);
666 str_isprint (const char *ch
)
668 return used_class
.isprint (ch
);
672 str_iscombiningmark (const char *ch
)
674 return used_class
.iscombiningmark (ch
);
678 str_trunc (const char *text
, int width
)
680 return used_class
.trunc (text
, width
);
684 str_create_search_needle (const char *needle
, int case_sen
)
686 return used_class
.create_search_needle (needle
, case_sen
);
691 str_release_search_needle (char *needle
, int case_sen
)
693 used_class
.release_search_needle (needle
, case_sen
);
697 str_search_first (const char *text
, const char *search
, int case_sen
)
699 return used_class
.search_first (text
, search
, case_sen
);
703 str_search_last (const char *text
, const char *search
, int case_sen
)
705 return used_class
.search_last (text
, search
, case_sen
);
709 str_is_valid_string (const char *text
)
711 return used_class
.is_valid_string (text
);
715 str_compare (const char *t1
, const char *t2
)
717 return used_class
.compare (t1
, t2
);
721 str_ncompare (const char *t1
, const char *t2
)
723 return used_class
.ncompare (t1
, t2
);
727 str_casecmp (const char *t1
, const char *t2
)
729 return used_class
.casecmp (t1
, t2
);
733 str_ncasecmp (const char *t1
, const char *t2
)
735 return used_class
.ncasecmp (t1
, t2
);
739 str_prefix (const char *text
, const char *prefix
)
741 return used_class
.prefix (text
, prefix
);
745 str_caseprefix (const char *text
, const char *prefix
)
747 return used_class
.caseprefix (text
, prefix
);
751 str_fix_string (char *text
)
753 used_class
.fix_string (text
);
757 str_create_key (const char *text
, int case_sen
)
759 return used_class
.create_key (text
, case_sen
);
763 str_create_key_for_filename (const char *text
, int case_sen
)
765 return used_class
.create_key_for_filename (text
, case_sen
);
769 str_key_collate (const char *t1
, const char *t2
, int case_sen
)
771 return used_class
.key_collate (t1
, t2
, case_sen
);
775 str_release_key (char *key
, int case_sen
)
777 used_class
.release_key (key
, case_sen
);