3 // yuvalt: wcstombs is not the way to go since it does not convert
4 // from utf8 to utf16. Luickly, there's a UTF8/UTF16 conversion
5 // functions in here which are used if _APPLE is defined.
6 // Therefore, define _APPLE in this case to do proper conversion
11 bool WideToChar(const wchar *Src,char *Dest,int DestSize)
15 if (WideCharToMultiByte(CP_ACP,0,Src,-1,Dest,DestSize,NULL,NULL)==0)
19 WideToUtf(Src,Dest,DestSize);
22 if (wcstombs(Dest,Src,DestSize)==(uint)-1)
27 for (int I=0;I<DestSize;I++)
40 bool CharToWide(const char *Src,wchar *Dest,int DestSize)
44 if (MultiByteToWideChar(CP_ACP,0,Src,-1,Dest,DestSize)==0)
48 UtfToWide(Src,Dest,DestSize);
51 mbstowcs(Dest,Src,DestSize);
55 for (int I=0;I<DestSize;I++)
57 Dest[I]=(wchar_t)Src[I];
68 byte* WideToRaw(const wchar *Src,byte *Dest,int DestSize)
70 for (int I=0;I<DestSize;I++,Src++)
73 Dest[I*2+1]=(byte)(*Src>>8);
81 wchar* RawToWide(const byte *Src,wchar *Dest,int DestSize)
83 for (int I=0;I<DestSize;I++)
84 if ((Dest[I]=Src[I*2]+(Src[I*2+1]<<8))==0)
91 void WideToUtf(const wchar *Src,char *Dest,int DestSize)
94 while (*Src!=0 && --DestSize>=0)
100 if (c<0x800 && --DestSize>=0)
102 *(Dest++)=(0xc0|(c>>6));
103 *(Dest++)=(0x80|(c&0x3f));
106 if (c<0x10000 && (DestSize-=2)>=0)
108 *(Dest++)=(0xe0|(c>>12));
109 *(Dest++)=(0x80|((c>>6)&0x3f));
110 *(Dest++)=(0x80|(c&0x3f));
119 void UtfToWide(const char *Src,wchar *Dest,int DestSize)
124 uint c=(byte)*(Src++),d;
130 if ((*Src&0xc0)!=0x80)
132 d=((c&0x1f)<<6)|(*Src&0x3f);
138 if ((Src[0]&0xc0)!=0x80 || (Src[1]&0xc0)!=0x80)
140 d=((c&0xf)<<12)|((Src[0]&0x3f)<<6)|(Src[1]&0x3f);
154 bool UnicodeEnabled()
156 #ifdef UNICODE_SUPPORTED
168 int strlenw(const wchar *str)
177 wchar* strcpyw(wchar *dest,const wchar *src)
181 } while (*(src++)!=0);
186 wchar* strncpyw(wchar *dest,const wchar *src,int n)
190 } while (*(src++)!=0 && --n > 0);
195 wchar* strcatw(wchar *dest,const wchar *src)
197 return(strcpyw(dest+strlenw(dest),src));
202 wchar* strncatw(wchar *dest,const wchar *src,int n)
212 if ((*(dest++)=*(src++))==0)
219 int strcmpw(const wchar *s1,const wchar *s2)
228 return(*s1<*s2 ? -1:1);
232 int strncmpw(const wchar *s1,const wchar *s2,int n)
250 int stricmpw(const wchar *s1,const wchar *s2)
252 char Ansi1[NM*sizeof(wchar)],Ansi2[NM*sizeof(wchar)];
253 WideToChar(s1,Ansi1,sizeof(Ansi1));
254 WideToChar(s2,Ansi2,sizeof(Ansi2));
255 return(stricomp(Ansi1,Ansi2));
260 #if !defined(SFX_MODULE) && !defined(_WIN_CE)
261 inline int strnicmpw_w2c(const wchar *s1,const wchar *s2,int n)
263 wchar Wide1[NM*2],Wide2[NM*2];
264 strncpyw(Wide1,s1,sizeof(Wide1)/sizeof(Wide1[0])-1);
265 strncpyw(Wide2,s2,sizeof(Wide2)/sizeof(Wide2[0])-1);
266 Wide1[Min((int)(sizeof(Wide1)/sizeof(Wide1[0])-1),n)]=0;
267 Wide2[Min((int)(sizeof(Wide2)/sizeof(Wide2[0])-1),n)]=0;
268 char Ansi1[NM*2],Ansi2[NM*2];
269 WideToChar(Wide1,Ansi1,sizeof(Ansi1));
270 WideToChar(Wide2,Ansi2,sizeof(Ansi2));
271 return(stricomp(Ansi1,Ansi2));
277 int strnicmpw(const wchar *s1,const wchar *s2,int n)
279 return(strnicmpw_w2c(s1,s2,n));
284 wchar* strchrw(const wchar *s,int c)
296 wchar* strrchrw(const wchar *s,int c)
298 for (int I=strlenw(s)-1;I>=0;I--)
300 return((wchar *)(s+I));
305 wchar* strpbrkw(const wchar *s1,const wchar *s2)
309 if (strchrw(s2,*s1)!=NULL)
318 wchar* strlowerw(wchar *Str)
320 for (wchar *ChPtr=Str;*ChPtr;ChPtr++)
322 *ChPtr=loctolower((char)*ChPtr);
329 wchar* strupperw(wchar *Str)
331 for (wchar *ChPtr=Str;*ChPtr;ChPtr++)
333 *ChPtr=loctoupper((char)*ChPtr);
342 return((ch<128) ? loctoupper(ch):ch);
347 int atoiw(const wchar *s)
350 while (*s>='0' && *s<='9')
359 #ifdef DBCS_SUPPORTED
362 SupportDBCS::SupportDBCS()
368 void SupportDBCS::Init()
370 #if defined(TARGET_POSIX)
372 for (int I=0;I<sizeof(IsLeadByte)/sizeof(IsLeadByte[0]);I++)
376 GetCPInfo(CP_ACP,&CPInfo);
377 DBCSMode=CPInfo.MaxCharSize > 1;
378 for (int I=0;I<sizeof(IsLeadByte)/sizeof(IsLeadByte[0]);I++)
379 IsLeadByte[I]=IsDBCSLeadByte(I);
384 char* SupportDBCS::charnext(const char *s)
386 return (char *)(IsLeadByte[*s] ? s+2:s+1);
390 uint SupportDBCS::strlend(const char *s)
405 char* SupportDBCS::strchrd(const char *s, int c)
419 void SupportDBCS::copychrd(char *dest,const char *src)
422 if (IsLeadByte[src[0]])
427 char* SupportDBCS::strrchrd(const char *s, int c)
429 const char *found=NULL;
439 return((char *)found);