bool WideToChar(const wchar *Src,char *Dest,size_t DestSize) { bool RetCode=true; *Dest=0; // Set 'Dest' to zero just in case the conversion will fail. #ifdef _WIN_ALL if (WideCharToMultiByte(CP_ACP,0,Src,-1,Dest,(int)DestSize,NULL,NULL)==0) RetCode=false; //#elif defined(_APPLE) // WideToUtf(Src,Dest,DestSize); #elif defined(MBFUNCTIONS) size_t ResultingSize=wcstombs(Dest,Src,DestSize); if (ResultingSize==(size_t)-1) RetCode=false; if (ResultingSize==0 && *Src!=0) RetCode=false; if ((!RetCode || *Dest==0 && *Src!=0) && DestSize>NM && wcslen(Src)<NM) { /* Workaround for strange Linux Unicode functions bug. Some of wcstombs and mbstowcs implementations in some situations (we are yet to find out what it depends on) can return an empty string and success code if buffer size value is too large. */ return(WideToChar(Src,Dest,NM)); } #else if (UnicodeEnabled()) { #if defined(_EMX) && !defined(_DJGPP) int len=Min(wcslen(Src)+1,DestSize-1); if (uni_fromucs((UniChar*)Src,len,Dest,(size_t*)&DestSize)==-1 || DestSize>len*2) RetCode=false; Dest[DestSize]=0; #endif } else for (int I=0;I<DestSize;I++) { Dest[I]=(char)Src[I]; if (Src[I]==0) break; } #endif // We tried to return the empty string if conversion is failed, // but it does not work well. WideCharToMultiByte returns 'failed' code // and partially converted string even if we wanted to convert only a part // of string and passed DestSize smaller than required for fully converted // string. Such call is the valid behavior in RAR code and we do not expect // the empty string in this case. return(RetCode); }
bool WideToChar(const wchar *Src,char *Dest,size_t DestSize) { bool RetCode=true; #ifdef _WIN_32 if (WideCharToMultiByte(CP_ACP,0,Src,-1,Dest,(int)DestSize,NULL,NULL)==0) RetCode=false; #else #ifdef _APPLE WideToUtf(Src,Dest,DestSize); #else #ifdef MBFUNCTIONS size_t ResultingSize=wcstombs(Dest,Src,DestSize); if (ResultingSize==(size_t)-1) RetCode=false; if (ResultingSize==0 && *Src!=0) RetCode=false; if ((!RetCode || *Dest==0 && *Src!=0) && DestSize>NM && strlenw(Src)<NM) { /* Workaround for strange Linux Unicode functions bug. Some of wcstombs and mbstowcs implementations in some situations (we are yet to find out what it depends on) can return an empty string and success code if buffer size value is too large. */ return(WideToChar(Src,Dest,NM)); } #else if (UnicodeEnabled()) { #if defined(_EMX) && !defined(_DJGPP) int len=Min(strlenw(Src)+1,DestSize-1); if (uni_fromucs((UniChar*)Src,len,Dest,(size_t*)&DestSize)==-1 || DestSize>len*2) RetCode=false; Dest[DestSize]=0; #endif } else for (int I=0;I<DestSize;I++) { Dest[I]=(char)Src[I]; if (Src[I]==0) break; } #endif #endif #endif return(RetCode); }
bool WideToChar(const wchar *Src,char *Dest,int DestSize) { bool RetCode=true; #ifdef _WIN_32 if (WideCharToMultiByte(CP_ACP,0,Src,-1,Dest,DestSize,NULL,NULL)==0) RetCode=false; #else #ifdef _APPLE WideToUtf(Src,Dest,DestSize); #else #ifdef MBFUNCTIONS if (wcstombs(Dest,Src,DestSize)==-1) RetCode=false; #else if (UnicodeEnabled()) { #if defined(_EMX) && !defined(_DJGPP) int len=Min(strlenw(Src)+1,DestSize-1); if (uni_fromucs((UniChar*)Src,len,Dest,(size_t*)&DestSize)==-1 || DestSize>len*2) RetCode=false; Dest[DestSize]=0; #endif } else for (int I=0;I<DestSize;I++) { Dest[I]=(char)Src[I]; if (Src[I]==0) break; } #endif #endif #endif return(RetCode); }