1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
3 #include "kwsysPrivate.h"
4 #include KWSYS_HEADER(Encoding.h)
5
6 /* Work-around CMake dependency scanning limitation. This must
7 duplicate the above list of headers. */
8 #if 0
9 # include "Encoding.h.in"
10 #endif
11
12 #include <stdlib.h>
13
14 #ifdef _WIN32
15 # include <windows.h>
16 #endif
17
kwsysEncoding_mbstowcs(wchar_t * dest,const char * str,size_t n)18 size_t kwsysEncoding_mbstowcs(wchar_t* dest, const char* str, size_t n)
19 {
20 if (str == 0) {
21 return (size_t)-1;
22 }
23 #ifdef _WIN32
24 return MultiByteToWideChar(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str, -1, dest,
25 (int)n) -
26 1;
27 #else
28 return mbstowcs(dest, str, n);
29 #endif
30 }
31
kwsysEncoding_DupToWide(const char * str)32 wchar_t* kwsysEncoding_DupToWide(const char* str)
33 {
34 wchar_t* ret = NULL;
35 size_t length = kwsysEncoding_mbstowcs(NULL, str, 0) + 1;
36 if (length > 0) {
37 ret = (wchar_t*)malloc((length) * sizeof(wchar_t));
38 if (ret) {
39 ret[0] = 0;
40 kwsysEncoding_mbstowcs(ret, str, length);
41 }
42 }
43 return ret;
44 }
45
kwsysEncoding_wcstombs(char * dest,const wchar_t * str,size_t n)46 size_t kwsysEncoding_wcstombs(char* dest, const wchar_t* str, size_t n)
47 {
48 if (str == 0) {
49 return (size_t)-1;
50 }
51 #ifdef _WIN32
52 return WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str, -1, dest,
53 (int)n, NULL, NULL) -
54 1;
55 #else
56 return wcstombs(dest, str, n);
57 #endif
58 }
59
kwsysEncoding_DupToNarrow(const wchar_t * str)60 char* kwsysEncoding_DupToNarrow(const wchar_t* str)
61 {
62 char* ret = NULL;
63 size_t length = kwsysEncoding_wcstombs(NULL, str, 0) + 1;
64 if (length > 0) {
65 ret = (char*)malloc(length);
66 if (ret) {
67 ret[0] = 0;
68 kwsysEncoding_wcstombs(ret, str, length);
69 }
70 }
71 return ret;
72 }
73