Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 3 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 4 | */ |
| 5 | #ifndef UNICODE_H |
| 6 | #define UNICODE_H 1 |
| 7 | |
Denys Vlasenko | 19158a8 | 2010-03-26 14:06:56 +0100 | [diff] [blame] | 8 | #if ENABLE_UNICODE_USING_LOCALE |
Denys Vlasenko | 2f8a460 | 2010-02-19 08:47:17 +0100 | [diff] [blame] | 9 | # include <wchar.h> |
| 10 | # include <wctype.h> |
| 11 | #endif |
| 12 | |
Denys Vlasenko | bb1dcc9 | 2010-02-02 12:45:38 +0100 | [diff] [blame] | 13 | PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN |
| 14 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 15 | enum { |
| 16 | UNICODE_UNKNOWN = 0, |
| 17 | UNICODE_OFF = 1, |
| 18 | UNICODE_ON = 2, |
| 19 | }; |
| 20 | |
Tomas Heinrich | aa16755 | 2010-03-26 13:13:24 +0100 | [diff] [blame] | 21 | #define unicode_bidi_isrtl(wc) 0 |
| 22 | #define unicode_bidi_is_neutral_wchar(wc) (wc <= 126 && !isalpha(wc)) |
Tomas Heinrich | c5c006c | 2010-03-18 18:35:37 +0100 | [diff] [blame] | 23 | |
Denys Vlasenko | 19158a8 | 2010-03-26 14:06:56 +0100 | [diff] [blame] | 24 | #if !ENABLE_UNICODE_SUPPORT |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 25 | |
Tomas Heinrich | 11bcf4b | 2010-06-01 08:33:18 +0200 | [diff] [blame] | 26 | # define unicode_strlen(string) strlen(string) |
| 27 | # define unicode_strwidth(string) strlen(string) |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 28 | # define unicode_status UNICODE_OFF |
| 29 | # define init_unicode() ((void)0) |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 30 | # define reinit_unicode(LANG) ((void)0) |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 31 | |
| 32 | #else |
| 33 | |
Tomas Heinrich | c5c006c | 2010-03-18 18:35:37 +0100 | [diff] [blame] | 34 | # if CONFIG_LAST_SUPPORTED_WCHAR < 126 || CONFIG_LAST_SUPPORTED_WCHAR >= 0x30000 |
Denys Vlasenko | 26e2c1d | 2010-05-16 21:15:03 +0200 | [diff] [blame] | 35 | # undef CONFIG_LAST_SUPPORTED_WCHAR |
| 36 | # define CONFIG_LAST_SUPPORTED_WCHAR 0x2ffff |
Tomas Heinrich | c5c006c | 2010-03-18 18:35:37 +0100 | [diff] [blame] | 37 | # endif |
| 38 | |
Denys Vlasenko | 26e2c1d | 2010-05-16 21:15:03 +0200 | [diff] [blame] | 39 | # if CONFIG_LAST_SUPPORTED_WCHAR < 0x300 |
Tomas Heinrich | b8909c5 | 2010-05-16 20:46:53 +0200 | [diff] [blame] | 40 | # undef ENABLE_UNICODE_COMBINING_WCHARS |
| 41 | # define ENABLE_UNICODE_COMBINING_WCHARS 0 |
| 42 | # endif |
| 43 | |
Denys Vlasenko | 26e2c1d | 2010-05-16 21:15:03 +0200 | [diff] [blame] | 44 | # if CONFIG_LAST_SUPPORTED_WCHAR < 0x1100 |
Tomas Heinrich | b8909c5 | 2010-05-16 20:46:53 +0200 | [diff] [blame] | 45 | # undef ENABLE_UNICODE_WIDE_WCHARS |
| 46 | # define ENABLE_UNICODE_WIDE_WCHARS 0 |
| 47 | # endif |
| 48 | |
Denys Vlasenko | 26e2c1d | 2010-05-16 21:15:03 +0200 | [diff] [blame] | 49 | # if CONFIG_LAST_SUPPORTED_WCHAR < 0x590 |
Tomas Heinrich | c5c006c | 2010-03-18 18:35:37 +0100 | [diff] [blame] | 50 | # undef ENABLE_UNICODE_BIDI_SUPPORT |
| 51 | # define ENABLE_UNICODE_BIDI_SUPPORT 0 |
| 52 | # endif |
| 53 | |
Tomas Heinrich | 11bcf4b | 2010-06-01 08:33:18 +0200 | [diff] [blame] | 54 | /* Number of unicode chars. Falls back to strlen() on invalid unicode */ |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 55 | size_t FAST_FUNC unicode_strlen(const char *string); |
Tomas Heinrich | 11bcf4b | 2010-06-01 08:33:18 +0200 | [diff] [blame] | 56 | /* Width on terminal */ |
| 57 | size_t FAST_FUNC unicode_strwidth(const char *string); |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame] | 58 | enum { |
| 59 | UNI_FLAG_PAD = (1 << 0), |
| 60 | }; |
Denys Vlasenko | e17764c | 2010-01-30 23:16:21 +0100 | [diff] [blame] | 61 | //UNUSED: unsigned FAST_FUNC unicode_padding_to_width(unsigned width, const char *src); |
| 62 | //UNUSED: char* FAST_FUNC unicode_conv_to_printable2(uni_stat_t *stats, const char *src, unsigned width, int flags); |
| 63 | char* FAST_FUNC unicode_conv_to_printable(uni_stat_t *stats, const char *src); |
Denys Vlasenko | dc7e5c4 | 2011-01-11 13:08:28 +0100 | [diff] [blame] | 64 | //UNUSED: char* FAST_FUNC unicode_conv_to_printable_maxwidth(uni_stat_t *stats, const char *src, unsigned maxwidth); |
| 65 | char* FAST_FUNC unicode_conv_to_printable_fixedwidth(/*uni_stat_t *stats,*/ const char *src, unsigned width); |
Denys Vlasenko | fda8f57 | 2009-07-11 22:26:48 +0200 | [diff] [blame] | 66 | |
Denys Vlasenko | 19158a8 | 2010-03-26 14:06:56 +0100 | [diff] [blame] | 67 | # if ENABLE_UNICODE_USING_LOCALE |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 68 | |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 69 | extern uint8_t unicode_status; |
| 70 | void init_unicode(void) FAST_FUNC; |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 71 | void reinit_unicode(const char *LANG) FAST_FUNC; |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 72 | |
| 73 | # else |
| 74 | |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 75 | /* Homegrown Unicode support. It knows only C and Unicode locales. */ |
Denys Vlasenko | fda8f57 | 2009-07-11 22:26:48 +0200 | [diff] [blame] | 76 | |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 77 | # if !ENABLE_FEATURE_CHECK_UNICODE_IN_ENV |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 78 | # define unicode_status UNICODE_ON |
| 79 | # define init_unicode() ((void)0) |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 80 | # define reinit_unicode(LANG) ((void)0) |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 81 | # else |
Denys Vlasenko | 2805502 | 2010-01-04 20:49:58 +0100 | [diff] [blame] | 82 | extern uint8_t unicode_status; |
| 83 | void init_unicode(void) FAST_FUNC; |
Denys Vlasenko | 20704f0 | 2011-03-23 17:59:27 +0100 | [diff] [blame] | 84 | void reinit_unicode(const char *LANG) FAST_FUNC; |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 85 | # endif |
| 86 | |
| 87 | # undef MB_CUR_MAX |
| 88 | # define MB_CUR_MAX 6 |
| 89 | |
| 90 | /* Prevent name collisions */ |
| 91 | # define wint_t bb_wint_t |
| 92 | # define mbstate_t bb_mbstate_t |
| 93 | # define mbstowcs bb_mbstowcs |
| 94 | # define wcstombs bb_wcstombs |
| 95 | # define wcrtomb bb_wcrtomb |
| 96 | # define iswspace bb_iswspace |
| 97 | # define iswalnum bb_iswalnum |
| 98 | # define iswpunct bb_iswpunct |
Denys Vlasenko | 9f93d62 | 2010-01-24 07:44:03 +0100 | [diff] [blame] | 99 | # define wcwidth bb_wcwidth |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 100 | |
| 101 | typedef int32_t wint_t; |
| 102 | typedef struct { |
| 103 | char bogus; |
| 104 | } mbstate_t; |
| 105 | |
| 106 | size_t mbstowcs(wchar_t *dest, const char *src, size_t n) FAST_FUNC; |
| 107 | size_t wcstombs(char *dest, const wchar_t *src, size_t n) FAST_FUNC; |
| 108 | size_t wcrtomb(char *s, wchar_t wc, mbstate_t *ps) FAST_FUNC; |
| 109 | int iswspace(wint_t wc) FAST_FUNC; |
| 110 | int iswalnum(wint_t wc) FAST_FUNC; |
| 111 | int iswpunct(wint_t wc) FAST_FUNC; |
Tomas Heinrich | b8909c5 | 2010-05-16 20:46:53 +0200 | [diff] [blame] | 112 | int wcwidth(unsigned ucs) FAST_FUNC; |
Tomas Heinrich | c5c006c | 2010-03-18 18:35:37 +0100 | [diff] [blame] | 113 | # if ENABLE_UNICODE_BIDI_SUPPORT |
Tomas Heinrich | aa16755 | 2010-03-26 13:13:24 +0100 | [diff] [blame] | 114 | # undef unicode_bidi_isrtl |
| 115 | int unicode_bidi_isrtl(wint_t wc) FAST_FUNC; |
| 116 | # if ENABLE_UNICODE_NEUTRAL_TABLE |
| 117 | # undef unicode_bidi_is_neutral_wchar |
| 118 | int unicode_bidi_is_neutral_wchar(wint_t wc) FAST_FUNC; |
| 119 | # endif |
Tomas Heinrich | c5c006c | 2010-03-18 18:35:37 +0100 | [diff] [blame] | 120 | # endif |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 121 | |
Denys Vlasenko | 9c3b84a | 2010-01-18 01:55:00 +0100 | [diff] [blame] | 122 | |
Denys Vlasenko | 19158a8 | 2010-03-26 14:06:56 +0100 | [diff] [blame] | 123 | # endif /* !UNICODE_USING_LOCALE */ |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 124 | |
Denys Vlasenko | 19158a8 | 2010-03-26 14:06:56 +0100 | [diff] [blame] | 125 | #endif /* UNICODE_SUPPORT */ |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 126 | |
Denys Vlasenko | bb1dcc9 | 2010-02-02 12:45:38 +0100 | [diff] [blame] | 127 | POP_SAVED_FUNCTION_VISIBILITY |
| 128 | |
Denys Vlasenko | 42a8fd0 | 2009-07-11 21:36:13 +0200 | [diff] [blame] | 129 | #endif |