Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | #include <ctype.h> |
| 30 | #include <errno.h> |
Elliott Hughes | 77e944f | 2014-04-04 17:34:51 -0700 | [diff] [blame] | 31 | #include <limits.h> |
Elliott Hughes | 3d7a0d9 | 2014-04-29 14:46:56 -0700 | [diff] [blame] | 32 | #include <stdint.h> |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | #include <wchar.h> |
| 36 | |
| 37 | /* stubs for wide-char functions */ |
| 38 | |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 39 | int iswalnum(wint_t wc) { return isalnum(wc); } |
| 40 | int iswalpha(wint_t wc) { return isalpha(wc); } |
Elliott Hughes | 40b0579 | 2014-04-15 12:04:05 -0700 | [diff] [blame] | 41 | int iswblank(wint_t wc) { return isblank(wc); } |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 42 | int iswcntrl(wint_t wc) { return iscntrl(wc); } |
| 43 | int iswdigit(wint_t wc) { return isdigit(wc); } |
| 44 | int iswgraph(wint_t wc) { return isgraph(wc); } |
| 45 | int iswlower(wint_t wc) { return islower(wc); } |
| 46 | int iswprint(wint_t wc) { return isprint(wc); } |
| 47 | int iswpunct(wint_t wc) { return ispunct(wc); } |
| 48 | int iswspace(wint_t wc) { return isspace(wc); } |
| 49 | int iswupper(wint_t wc) { return isupper(wc); } |
| 50 | int iswxdigit(wint_t wc) { return isxdigit(wc); } |
| 51 | |
| 52 | int iswctype(wint_t wc, wctype_t char_class) { |
| 53 | switch (char_class) { |
| 54 | case WC_TYPE_ALNUM: return isalnum(wc); |
| 55 | case WC_TYPE_ALPHA: return isalpha(wc); |
| 56 | case WC_TYPE_BLANK: return isblank(wc); |
| 57 | case WC_TYPE_CNTRL: return iscntrl(wc); |
| 58 | case WC_TYPE_DIGIT: return isdigit(wc); |
| 59 | case WC_TYPE_GRAPH: return isgraph(wc); |
| 60 | case WC_TYPE_LOWER: return islower(wc); |
| 61 | case WC_TYPE_PRINT: return isprint(wc); |
| 62 | case WC_TYPE_PUNCT: return ispunct(wc); |
| 63 | case WC_TYPE_SPACE: return isspace(wc); |
| 64 | case WC_TYPE_UPPER: return isupper(wc); |
| 65 | case WC_TYPE_XDIGIT: return isxdigit(wc); |
| 66 | default: return 0; |
| 67 | } |
| 68 | } |
| 69 | |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 70 | int mbsinit(const mbstate_t* /*ps*/) { |
| 71 | return 1; |
| 72 | } |
| 73 | |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 74 | size_t mbrtowc(wchar_t* pwc, const char* s, size_t n, mbstate_t* /*ps*/) { |
| 75 | if (s == NULL) { |
Elliott Hughes | 0a5e26d | 2014-04-28 17:51:13 -0700 | [diff] [blame] | 76 | return 0; |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 77 | } |
| 78 | if (n == 0) { |
Elliott Hughes | 0a5e26d | 2014-04-28 17:51:13 -0700 | [diff] [blame] | 79 | return 0; |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 80 | } |
Elliott Hughes | 0a5e26d | 2014-04-28 17:51:13 -0700 | [diff] [blame] | 81 | if (pwc != NULL) { |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 82 | *pwc = *s; |
| 83 | } |
| 84 | return (*s != 0); |
| 85 | } |
| 86 | |
Elliott Hughes | 3d7a0d9 | 2014-04-29 14:46:56 -0700 | [diff] [blame] | 87 | size_t mbsnrtowcs(wchar_t* dst, const char** src, size_t n, size_t dst_size, mbstate_t* /*ps*/) { |
| 88 | size_t i = 0; // Number of input bytes read. |
| 89 | size_t o = 0; // Number of output characters written. |
| 90 | for (; i < n && (*src)[i] != 0; ++i) { |
| 91 | // TODO: UTF-8 support. |
Elliott Hughes | 770491f | 2014-04-29 16:05:58 -0700 | [diff] [blame] | 92 | if (static_cast<uint8_t>((*src)[i]) > 0x7f) { |
Elliott Hughes | 3d7a0d9 | 2014-04-29 14:46:56 -0700 | [diff] [blame] | 93 | errno = EILSEQ; |
| 94 | if (dst != NULL) { |
| 95 | *src = &(*src)[i]; |
| 96 | } |
| 97 | return static_cast<size_t>(-1); |
| 98 | } |
| 99 | if (dst != NULL) { |
| 100 | if (o + 1 > dst_size) { |
| 101 | break; |
| 102 | } |
| 103 | dst[o++] = static_cast<wchar_t>((*src)[i]); |
| 104 | } else { |
| 105 | ++o; |
| 106 | } |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 107 | } |
Elliott Hughes | 3d7a0d9 | 2014-04-29 14:46:56 -0700 | [diff] [blame] | 108 | // If we consumed all the input, terminate the output. |
| 109 | if (dst != NULL && o < dst_size) { |
| 110 | dst[o] = 0; |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 111 | } |
Elliott Hughes | 3d7a0d9 | 2014-04-29 14:46:56 -0700 | [diff] [blame] | 112 | // If we were actually consuming input, record how far we got. |
| 113 | if (dst != NULL) { |
| 114 | if ((*src)[i] != 0) { |
| 115 | *src = &(*src)[i]; // This is where the next call should pick up. |
| 116 | } else { |
| 117 | *src = NULL; // We consumed everything. |
| 118 | } |
| 119 | } |
| 120 | return o; |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Elliott Hughes | 3d7a0d9 | 2014-04-29 14:46:56 -0700 | [diff] [blame] | 123 | size_t mbsrtowcs(wchar_t* dst, const char** src, size_t dst_size, mbstate_t* ps) { |
| 124 | return mbsnrtowcs(dst, src, SIZE_MAX, dst_size, ps); |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 127 | wint_t towlower(wint_t wc) { |
| 128 | return tolower(wc); |
| 129 | } |
| 130 | |
| 131 | wint_t towupper(wint_t wc) { |
| 132 | return toupper(wc); |
| 133 | } |
| 134 | |
Elliott Hughes | 77e944f | 2014-04-04 17:34:51 -0700 | [diff] [blame] | 135 | int wctomb(char* s, wchar_t wc) { |
| 136 | if (s == NULL) { |
| 137 | return 0; |
| 138 | } |
| 139 | if (wc <= 0xff) { |
| 140 | *s = static_cast<char>(wc); |
| 141 | } else { |
| 142 | *s = '?'; |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 143 | } |
| 144 | return 1; |
| 145 | } |
| 146 | |
Elliott Hughes | 77e944f | 2014-04-04 17:34:51 -0700 | [diff] [blame] | 147 | size_t wcrtomb(char* s, wchar_t wc, mbstate_t* /*ps*/) { |
| 148 | if (s == NULL) { |
| 149 | char buf[MB_LEN_MAX]; |
| 150 | return wctomb(buf, L'\0'); |
| 151 | } |
| 152 | return wctomb(s, wc); |
| 153 | } |
| 154 | |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 155 | size_t wcsftime(wchar_t* wcs, size_t maxsize, const wchar_t* format, const struct tm* timptr) { |
| 156 | return strftime(reinterpret_cast<char*>(wcs), maxsize, reinterpret_cast<const char*>(format), timptr); |
| 157 | } |
| 158 | |
Elliott Hughes | 3d7a0d9 | 2014-04-29 14:46:56 -0700 | [diff] [blame] | 159 | size_t wcsnrtombs(char* dst, const wchar_t** src, size_t n, size_t dst_size, mbstate_t* /*ps*/) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 160 | size_t i = 0; // Number of input characters read. |
| 161 | size_t o = 0; // Number of output bytes written. |
Elliott Hughes | 3d7a0d9 | 2014-04-29 14:46:56 -0700 | [diff] [blame] | 162 | for (; i < n && (*src)[i] != 0; ++i) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 163 | // TODO: UTF-8 support. |
| 164 | if ((*src)[i] > 0x7f) { |
| 165 | errno = EILSEQ; |
Elliott Hughes | 1b836ee | 2014-04-18 13:32:33 -0700 | [diff] [blame] | 166 | if (dst != NULL) { |
| 167 | *src = &(*src)[i]; |
| 168 | } |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 169 | return static_cast<size_t>(-1); |
| 170 | } |
| 171 | if (dst != NULL) { |
Elliott Hughes | 3d7a0d9 | 2014-04-29 14:46:56 -0700 | [diff] [blame] | 172 | if (o + 1 > dst_size) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | dst[o++] = static_cast<char>((*src)[i]); |
| 176 | } else { |
| 177 | ++o; |
| 178 | } |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 179 | } |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 180 | // If we consumed all the input, terminate the output. |
Elliott Hughes | 3d7a0d9 | 2014-04-29 14:46:56 -0700 | [diff] [blame] | 181 | if (dst != NULL && o < dst_size) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 182 | dst[o] = 0; |
| 183 | } |
| 184 | // If we were actually consuming input, record how far we got. |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 185 | if (dst != NULL) { |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 186 | if ((*src)[i] != 0) { |
| 187 | *src = &(*src)[i]; // This is where the next call should pick up. |
| 188 | } else { |
| 189 | *src = NULL; // We consumed everything. |
| 190 | } |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 191 | } |
Elliott Hughes | 0549371 | 2014-04-17 17:30:03 -0700 | [diff] [blame] | 192 | return o; |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Elliott Hughes | 3d7a0d9 | 2014-04-29 14:46:56 -0700 | [diff] [blame] | 195 | size_t wcsrtombs(char* dst, const wchar_t** src, size_t dst_size, mbstate_t* ps) { |
| 196 | return wcsnrtombs(dst, src, SIZE_MAX, dst_size, ps); |
Elliott Hughes | 29c7f0b | 2012-10-22 17:05:27 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | wctype_t wctype(const char* property) { |
| 200 | static const char* const properties[WC_TYPE_MAX] = { |
| 201 | "<invalid>", |
| 202 | "alnum", "alpha", "blank", "cntrl", "digit", "graph", |
| 203 | "lower", "print", "punct", "space", "upper", "xdigit" |
| 204 | }; |
| 205 | for (size_t i = 0; i < WC_TYPE_MAX; ++i) { |
| 206 | if (!strcmp(properties[i], property)) { |
| 207 | return static_cast<wctype_t>(i); |
| 208 | } |
| 209 | } |
| 210 | return static_cast<wctype_t>(0); |
| 211 | } |
| 212 | |
| 213 | int wcwidth(wchar_t wc) { |
| 214 | return (wc > 0); |
| 215 | } |