File indexing completed on 2024-05-12 04:39:27

0001 /*============================================================================
0002   KWSys - Kitware System Library
0003   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
0004 
0005   Distributed under the OSI-approved BSD License (the "License");
0006   see accompanying file Copyright.txt for details.
0007 
0008   This software is distributed WITHOUT ANY WARRANTY; without even the
0009   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
0010   See the License for more information.
0011 ============================================================================*/
0012 /*#include "kwsysPrivate.h"*/
0013 /*#include KWSYS_HEADER(Encoding.h)*/
0014 
0015 /* Work-around CMake dependency scanning limitation.  This must
0016    duplicate the above list of headers.  */
0017 #if 0
0018 # include "Encoding.h.in"
0019 #endif
0020 
0021 #include <stdlib.h>
0022 
0023 #ifdef _WIN32
0024 #include <windows.h>
0025 #endif
0026 
0027 size_t kwsysEncoding_mbstowcs(wchar_t* dest, const char* str, size_t n)
0028 {
0029   if(str == 0)
0030     {
0031     return (size_t)-1;
0032     }
0033 #ifdef _WIN32
0034   return MultiByteToWideChar(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0,
0035                              str, -1, dest, (int)n) - 1;
0036 #else
0037   return mbstowcs(dest, str, n);
0038 #endif
0039 }
0040 
0041 wchar_t* kwsysEncoding_DupToWide(const char* str)
0042 {
0043   wchar_t* ret = NULL;
0044   size_t length = kwsysEncoding_mbstowcs(NULL, str, 0) + 1;
0045   if(length > 0)
0046     {
0047     ret = (wchar_t*)malloc((length)*sizeof(wchar_t));
0048     if(ret)
0049       {
0050       ret[0] = 0;
0051       kwsysEncoding_mbstowcs(ret, str, length);
0052       }
0053     }
0054   return ret;
0055 }
0056 
0057 size_t kwsysEncoding_wcstombs(char* dest, const wchar_t* str, size_t n)
0058 {
0059   if(str == 0)
0060     {
0061     return (size_t)-1;
0062     }
0063 #ifdef _WIN32
0064   return WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str, -1,
0065                              dest, (int)n, NULL, NULL) - 1;
0066 #else
0067   return wcstombs(dest, str, n);
0068 #endif
0069 }
0070 
0071 char* kwsysEncoding_DupToNarrow(const wchar_t* str)
0072 {
0073   char* ret = NULL;
0074   size_t length = kwsysEncoding_wcstombs(0, str, 0) + 1;
0075   if(length > 0)
0076     {
0077     ret = (char*)malloc(length);
0078     if(ret)
0079       {
0080       ret[0] = 0;
0081       kwsysEncoding_wcstombs(ret, str, length);
0082       }
0083     }
0084   return ret;
0085 }