mirror of
git://sourceware.org/git/newlib-cygwin.git
synced 2025-01-15 19:09:58 +08:00
a7197550f3
(CharNextExA): Define. * environ.cc (codepage_init): Un-static. Set active_codepage to active codepage. Default to ansi regardless of buf pointer. * fhandler.h (dev_console::get_console_cp): New method. (dev_console::con_to_str): Change declaration according to new implementation. (dev_console::str_to_con): Ditto. * fhandler_console.cc (cp_convert): Remove. (dev_console::con_to_str): Redefine to take WCHAR as incoming console char. (dev_console::get_console_cp): Return correct codepage according to alternate_charset_active setting. (dev_console::str_to_con): Redefine to create WCHAR buffer for console output. (fhandler_console::read): Read console input as WCHARs. (base_chars): Fix typo in comment. (fhandler_console::char_command): Save and restore console output buffer using UNICODE functions. (fhandler_console::write_normal): Convert to write output in UNICODE. Use CharNextExA to recognize multibyte characters in input. Workaround problem with UTF-8 and MultiByteToWideChar. Simplify the loop for printing "normal" characters. * strfuncs.cc (active_codepage): New variable to store active codepage. (get_cp): Call codepage_init() if active_codepage is uninitialized. Just return active_codepage. (is_cp_multibyte): New function. * winsup.h (active_codepage): Declare. (codepage_init): Declare. (is_cp_multibyte): Declare.
149 lines
3.7 KiB
C++
149 lines
3.7 KiB
C++
/* strfuncs.cc: misc funcs that don't belong anywhere else
|
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
|
|
2005, 2006, 2007 Red Hat, Inc.
|
|
|
|
This file is part of Cygwin.
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
details. */
|
|
|
|
#include "winsup.h"
|
|
#include <stdlib.h>
|
|
#include <winbase.h>
|
|
#include <winnls.h>
|
|
#include <ntdll.h>
|
|
#include "cygerrno.h"
|
|
#include "security.h"
|
|
#include "path.h"
|
|
#include "fhandler.h"
|
|
#include "dtable.h"
|
|
#include "cygheap.h"
|
|
|
|
codepage_type current_codepage = ansi_cp;
|
|
UINT active_codepage = 0;
|
|
|
|
#ifdef __OUTSIDE_CYGWIN__
|
|
#define codepage_init(cp) (active_codepage = GetACP ())
|
|
#endif
|
|
|
|
UINT
|
|
get_cp ()
|
|
{
|
|
if (!active_codepage)
|
|
codepage_init ("ansi");
|
|
return active_codepage;
|
|
}
|
|
|
|
bool
|
|
is_cp_multibyte (UINT cp)
|
|
{
|
|
CPINFO cpi;
|
|
GetCPInfo (cp, &cpi);
|
|
return cpi.MaxCharSize > 1;
|
|
}
|
|
|
|
/* tlen is always treated as the maximum buffer size, including the '\0'
|
|
character. sys_wcstombs will always return a 0-terminated result, no
|
|
matter what. */
|
|
int __stdcall
|
|
sys_wcstombs (char *tgt, int tlen, const PWCHAR src, int slen)
|
|
{
|
|
int ret;
|
|
|
|
ret = WideCharToMultiByte (get_cp (), 0, src, slen, tgt, tlen, NULL, NULL);
|
|
if (ret && tgt)
|
|
{
|
|
ret = (ret < tlen) ? ret : tlen - 1;
|
|
tgt[ret] = '\0';
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/* Allocate a buffer big enough for the string, always including the
|
|
terminating '\0'. The buffer pointer is returned in *tgt_p, the return
|
|
value is the number of bytes written to the buffer, as usual.
|
|
The "type" argument determines where the resulting buffer is stored.
|
|
It's either one of the cygheap_types values, or it's "HEAP_NOTHEAP".
|
|
In the latter case the allocation uses simple calloc.
|
|
|
|
Note that this code is shared by cygserver (which requires it via
|
|
__small_vsprintf) and so when built there plain calloc is the
|
|
only choice. */
|
|
int __stdcall
|
|
sys_wcstombs_alloc (char **tgt_p, int type, const PWCHAR src, int slen)
|
|
{
|
|
int ret;
|
|
|
|
ret = WideCharToMultiByte (get_cp (), 0, src, slen, NULL, 0,NULL, NULL);
|
|
if (ret)
|
|
{
|
|
size_t tlen = (slen == -1 ? ret : ret + 1);
|
|
|
|
#ifndef __OUTSIDE_CYGWIN__
|
|
if (type == HEAP_NOTHEAP)
|
|
#endif
|
|
*tgt_p = (char *) calloc (tlen, sizeof (char));
|
|
#ifndef __OUTSIDE_CYGWIN__
|
|
else
|
|
*tgt_p = (char *) ccalloc ((cygheap_types) type, tlen, sizeof (char));
|
|
#endif
|
|
if (!*tgt_p)
|
|
return 0;
|
|
ret = sys_wcstombs (*tgt_p, tlen, src, slen);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
int __stdcall
|
|
sys_mbstowcs (PWCHAR tgt, const char *src, int len)
|
|
{
|
|
int res = MultiByteToWideChar (get_cp (), 0, src, -1, tgt, len);
|
|
return res;
|
|
}
|
|
|
|
/* Same as sys_wcstombs_alloc, just backwards. */
|
|
int __stdcall
|
|
sys_mbstowcs_alloc (PWCHAR *tgt_p, int type, const char *src)
|
|
{
|
|
int ret;
|
|
|
|
ret = MultiByteToWideChar (get_cp (), 0, src, -1, NULL, 0);
|
|
if (ret)
|
|
{
|
|
#ifndef __OUTSIDE_CYGWIN__
|
|
if (type == HEAP_NOTHEAP)
|
|
#endif
|
|
*tgt_p = (PWCHAR) calloc (ret, sizeof (WCHAR));
|
|
#ifndef __OUTSIDE_CYGWIN__
|
|
else
|
|
*tgt_p = (PWCHAR) ccalloc ((cygheap_types) type, ret, sizeof (WCHAR));
|
|
#endif
|
|
if (!*tgt_p)
|
|
return 0;
|
|
ret = sys_mbstowcs (*tgt_p, src, ret);
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
static WCHAR hex_wchars[] = L"0123456789abcdef";
|
|
|
|
NTSTATUS NTAPI
|
|
RtlInt64ToHexUnicodeString (ULONGLONG value, PUNICODE_STRING dest,
|
|
BOOLEAN append)
|
|
{
|
|
USHORT len = append ? dest->Length : 0;
|
|
if (dest->MaximumLength - len < 16 * (int) sizeof (WCHAR))
|
|
return STATUS_BUFFER_OVERFLOW;
|
|
PWCHAR end = (PWCHAR) ((PBYTE) dest->Buffer + len);
|
|
register PWCHAR p = end + 16;
|
|
while (p-- > end)
|
|
{
|
|
*p = hex_wchars[value & 0xf];
|
|
value >>= 4;
|
|
}
|
|
dest->Length += 16 * sizeof (WCHAR);
|
|
return STATUS_SUCCESS;
|
|
}
|