1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
/*
Copyright(C) 2018 Brazil
Copyright(C) 2022 Sutou Kouhei <kou@clear-code.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include "grn.h"
#include "grn_ctx.h"
#include "grn_encoding.h"
#include "grn_windows.h"
#include <string.h>
#ifdef WIN32
static const char *
grn_encoding_convert(grn_ctx *ctx,
const char *context,
UINT from_code_page,
UINT to_code_page,
const char *from_string,
ssize_t from_string_size,
size_t *converted_string_size)
{
char *converted_string = NULL;
if (from_string_size < 0) {
from_string_size = strlen(from_string);
}
if (from_code_page == to_code_page) {
goto exit;
}
{
WCHAR *utf16_string;
DWORD n_utf16_chars;
size_t converted_string_size_;
n_utf16_chars = MultiByteToWideChar(from_code_page,
0,
from_string,
from_string_size,
NULL,
0);
if (n_utf16_chars == 0) {
SERR("%s failed to estimate the number of UTF-16 characters",
context);
goto exit;
}
utf16_string = GRN_MALLOCN(WCHAR, n_utf16_chars);
n_utf16_chars = MultiByteToWideChar(from_code_page,
0,
from_string,
from_string_size,
utf16_string,
n_utf16_chars);
if (n_utf16_chars == 0) {
SERR("%s failed to convert to UTF-16 characters",
context);
GRN_FREE(utf16_string);
goto exit;
}
converted_string_size_ = WideCharToMultiByte(to_code_page,
0,
utf16_string,
n_utf16_chars,
NULL,
0,
NULL,
NULL);
if (converted_string_size_ == 0) {
SERR("%s failed to estimate required buffer size for converted string",
context);
GRN_FREE(utf16_string);
goto exit;
}
converted_string = GRN_MALLOCN(char, converted_string_size_ + 1);
converted_string_size_ = WideCharToMultiByte(to_code_page,
0,
utf16_string,
n_utf16_chars,
converted_string,
converted_string_size_,
NULL,
NULL);
GRN_FREE(utf16_string);
if (converted_string_size_ == 0) {
SERR("%s failed to estimate required buffer size for converted string",
context);
GRN_FREE(converted_string);
converted_string = NULL;
goto exit;
}
converted_string[converted_string_size_] = '\0';
if (converted_string_size) {
*converted_string_size = converted_string_size_;
}
}
exit :
if (!converted_string) {
converted_string = GRN_MALLOCN(char, from_string_size + 1);
if (converted_string) {
grn_memcpy(converted_string, from_string, from_string_size);
converted_string[from_string_size] = '\0';
if (converted_string_size) {
*converted_string_size = from_string_size;
}
} else {
ERR(ctx->rc,
"%s failed to allocate a buffer for converted string",
context);
if (converted_string_size) {
*converted_string_size = 0;
}
}
}
return converted_string;
}
const char *
grn_encoding_convert_to_locale(grn_ctx *ctx,
const char *grn_encoding_string,
ssize_t grn_encoding_string_size,
size_t *converted_string_size)
{
return grn_encoding_convert(ctx,
"[encoding][convert][grn->locale]",
grn_windows_encoding_to_code_page(ctx->encoding),
CP_ACP,
grn_encoding_string,
grn_encoding_string_size,
converted_string_size);
}
const char *
grn_encoding_convert_to_utf8(grn_ctx *ctx,
const char *grn_encoding_string,
ssize_t grn_encoding_string_size,
size_t *converted_string_size)
{
return grn_encoding_convert(ctx,
"[encoding][convert][grn->utf8]",
grn_windows_encoding_to_code_page(ctx->encoding),
CP_UTF8,
grn_encoding_string,
grn_encoding_string_size,
converted_string_size);
}
const char *
grn_encoding_convert_from_locale(grn_ctx *ctx,
const char *locale_string,
ssize_t locale_string_size,
size_t *converted_string_size)
{
return grn_encoding_convert(ctx,
"[encoding][convert][locale->grn]",
CP_ACP,
grn_windows_encoding_to_code_page(ctx->encoding),
locale_string,
locale_string_size,
converted_string_size);
}
const char *
grn_encoding_convert_from_utf8(grn_ctx *ctx,
const char *utf8_string,
ssize_t utf8_string_size,
size_t *converted_string_size)
{
return grn_encoding_convert(ctx,
"[encoding][convert][utf8->grn]",
CP_UTF8,
grn_windows_encoding_to_code_page(ctx->encoding),
utf8_string,
utf8_string_size,
converted_string_size);
}
const char *
grn_encoding_convert_to_utf8_from_locale(grn_ctx *ctx,
const char *locale_string,
ssize_t locale_string_size,
size_t *converted_string_size)
{
return grn_encoding_convert(ctx,
"[encoding][convert][locale->utf8]",
CP_ACP,
CP_UTF8,
locale_string,
locale_string_size,
converted_string_size);
}
const char *
grn_encoding_convert_to_locale_from_utf8(grn_ctx *ctx,
const char *utf8_string,
ssize_t utf8_string_size,
size_t *converted_string_size)
{
return grn_encoding_convert(ctx,
"[encoding][convert][utf8->locale]",
CP_UTF8,
CP_ACP,
utf8_string,
utf8_string_size,
converted_string_size);
}
void
grn_encoding_converted_free(grn_ctx *ctx, const char *converted_string)
{
GRN_FREE((char *)converted_string);
}
#else /* WIN32 */
const char *
grn_encoding_convert_to_locale(grn_ctx *ctx,
const char *grn_encoding_string,
ssize_t grn_encoding_string_size,
size_t *converted_string_size)
{
if (converted_string_size) {
if (grn_encoding_string_size < 0) {
*converted_string_size = strlen(grn_encoding_string);
} else {
*converted_string_size = (size_t)grn_encoding_string_size;
}
}
return grn_encoding_string;
}
const char *
grn_encoding_convert_to_utf8(grn_ctx *ctx,
const char *grn_encoding_string,
ssize_t grn_encoding_string_size,
size_t *converted_string_size)
{
if (converted_string_size) {
if (grn_encoding_string_size < 0) {
*converted_string_size = strlen(grn_encoding_string);
} else {
*converted_string_size = (size_t)grn_encoding_string_size;
}
}
return grn_encoding_string;
}
const char *
grn_encoding_convert_from_locale(grn_ctx *ctx,
const char *locale_string,
ssize_t locale_string_size,
size_t *converted_string_size)
{
if (converted_string_size) {
if (locale_string_size < 0) {
*converted_string_size = strlen(locale_string);
} else {
*converted_string_size = (size_t)locale_string_size;
}
}
return locale_string;
}
const char *
grn_encoding_convert_from_utf8(grn_ctx *ctx,
const char *utf8_string,
ssize_t utf8_string_size,
size_t *converted_string_size)
{
if (converted_string_size) {
if (utf8_string_size < 0) {
*converted_string_size = strlen(utf8_string);
} else {
*converted_string_size = (size_t)utf8_string_size;
}
}
return utf8_string;
}
const char *
grn_encoding_convert_to_utf8_from_locale(grn_ctx *ctx,
const char *locale_string,
ssize_t locale_string_size,
size_t *converted_string_size)
{
if (converted_string_size) {
if (locale_string_size < 0) {
*converted_string_size = strlen(locale_string);
} else {
*converted_string_size = (size_t)locale_string_size;
}
}
return locale_string;
}
const char *
grn_encoding_convert_to_locale_from_utf8(grn_ctx *ctx,
const char *utf8_string,
ssize_t utf8_string_size,
size_t *converted_string_size)
{
if (converted_string_size) {
if (utf8_string_size < 0) {
*converted_string_size = strlen(utf8_string);
} else {
*converted_string_size = (size_t)utf8_string_size;
}
}
return utf8_string;
}
void
grn_encoding_converted_free(grn_ctx *ctx, const char *converted_string)
{
}
#endif /* WIN32 */
|