home *** CD-ROM | disk | FTP | other *** search
/ ftp.rarlab.com / 2014.05.ftp.rarlab.com.tar / ftp.rarlab.com / rar / unrar_vms_alpha-3.6.5.zip / unicode.cxx < prev    next >
C/C++ Source or Header  |  2006-06-27  |  8KB  |  479 lines

  1. #include "rar.hpp"
  2.  
  3. #if defined(_EMX) && !defined(_DJGPP)
  4. #include "unios2.cpp"
  5. #endif
  6.  
  7. bool WideToChar(const wchar *Src,char *Dest,int DestSize)
  8. {
  9.   bool RetCode=true;
  10. #ifdef _WIN_32
  11.   if (WideCharToMultiByte(CP_ACP,0,Src,-1,Dest,DestSize,NULL,NULL)==0)
  12.     RetCode=false;
  13. #else
  14. #ifdef _APPLE
  15.   WideToUtf(Src,Dest,DestSize);
  16. #else
  17. #ifdef MBFUNCTIONS
  18. #  ifdef __VMS
  19.       if (wcstombs(Dest,(const wchar_t *)Src,DestSize)== (size_t)-1)
  20. #   else
  21.       if (wcstombs(Dest,(const wchar_t *)Src,DestSize)==-1)
  22. #  endif
  23.     RetCode=false;
  24. #else
  25.   if (UnicodeEnabled())
  26.   {
  27. #if defined(_EMX) && !defined(_DJGPP)
  28.     int len=Min(strlenw(Src)+1,DestSize-1);
  29.     if (uni_fromucs((UniChar*)Src,len,Dest,(size_t*)&DestSize)==-1 ||
  30.         DestSize>len*2)
  31.       RetCode=false;
  32.     Dest[DestSize]=0;
  33. #endif
  34.   }
  35.   else
  36.     for (int I=0;I<DestSize;I++)
  37.     {
  38.       Dest[I]=(char)Src[I];
  39.       if (Src[I]==0)
  40.         break;
  41.     }
  42. #endif
  43. #endif
  44. #endif
  45.   return(RetCode);
  46. }
  47.  
  48.  
  49. bool CharToWide(const char *Src,wchar *Dest,int DestSize)
  50. {
  51.   bool RetCode=true;
  52. #ifdef _WIN_32
  53.   if (MultiByteToWideChar(CP_ACP,0,Src,-1,Dest,DestSize)==0)
  54.     RetCode=false;
  55. #else
  56. #ifdef _APPLE
  57.   UtfToWide(Src,Dest,DestSize);
  58. #else
  59. #ifdef MBFUNCTIONS
  60. #  ifdef __VMS
  61.       if (mbstowcs((wchar_t *)Dest,Src,DestSize)==(size_t)-1)
  62. #    else
  63.       if (mbstowcs((wchar_t *)Dest,Src,DestSize)==-1)
  64. #  endif  
  65.     RetCode=false;
  66. #else
  67.   if (UnicodeEnabled())
  68.   {
  69. #if defined(_EMX) && !defined(_DJGPP)
  70.     int len=Min(strlen(Src)+1,DestSize-1);
  71.     if (uni_toucs((char*)Src,len,(UniChar*)Dest,(size_t*)&DestSize)==-1 ||
  72.         DestSize>len)
  73.       DestSize=0;
  74.     RetCode=false;
  75. #endif
  76.   }
  77.   else
  78.     for (int I=0;I<DestSize;I++)
  79.     {
  80.       Dest[I]=(wchar_t)Src[I];
  81.       if (Src[I]==0)
  82.         break;
  83.     }
  84. #endif
  85. #endif
  86. #endif
  87.   return(RetCode);
  88. }
  89.  
  90.  
  91. byte* WideToRaw(const wchar *Src,byte *Dest,int DestSize)
  92. {
  93.   for (int I=0;I<DestSize;I++,Src++)
  94.   {
  95.     Dest[I*2]=(byte)*Src;
  96.     Dest[I*2+1]=(byte)(*Src>>8);
  97.     if (*Src==0)
  98.       break;
  99.   }
  100.   return(Dest);
  101. }
  102.  
  103.  
  104. wchar* RawToWide(const byte *Src,wchar *Dest,int DestSize)
  105. {
  106.   for (int I=0;I<DestSize;I++)
  107.     if ((Dest[I]=Src[I*2]+(Src[I*2+1]<<8))==0)
  108.       break;
  109.   return(Dest);
  110. }
  111.  
  112.  
  113. void WideToUtf(const wchar *Src,char *Dest,int DestSize)
  114. {
  115.   DestSize--;
  116.   while (*Src!=0 && --DestSize>=0)
  117.   {
  118.     uint c=*(Src++);
  119.     if (c<0x80)
  120.       *(Dest++)=c;
  121.     else
  122.       if (c<0x800 && --DestSize>=0)
  123.       {
  124.         *(Dest++)=(0xc0|(c>>6));
  125.         *(Dest++)=(0x80|(c&0x3f));
  126.       }
  127.       else
  128.         if (c<0x10000 && (DestSize-=2)>=0)
  129.         {
  130.           *(Dest++)=(0xe0|(c>>12));
  131.           *(Dest++)=(0x80|((c>>6)&0x3f));
  132.           *(Dest++)=(0x80|(c&0x3f));
  133.         }
  134.         else
  135.           if (c < 0x200000 && (DestSize-=3)>=0)
  136.           {
  137.             *(Dest++)=(0xf0|(c>>18));
  138.             *(Dest++)=(0x80|((c>>12)&0x3f));
  139.             *(Dest++)=(0x80|((c>>6)&0x3f));
  140.             *(Dest++)=(0x80|(c&0x3f));
  141.           }
  142.   }
  143.   *Dest=0;
  144. }
  145.  
  146.  
  147. void UtfToWide(const char *Src,wchar *Dest,int DestSize)
  148. {
  149.   DestSize--;
  150.   while (*Src!=0)
  151.   {
  152.     uint c=(byte)*(Src++),d;
  153.     if (c<0x80)
  154.       d=c;
  155.     else
  156.       if ((c>>5)==6)
  157.       {
  158.         if ((*Src&0xc0)!=0x80)
  159.           break;
  160.         d=((c&0x1f)<<6)|(*Src&0x3f);
  161.         Src++;
  162.       }
  163.       else
  164.         if ((c>>4)==14)
  165.         {
  166.           if ((Src[0]&0xc0)!=0x80 || (Src[1]&0xc0)!=0x80)
  167.             break;
  168.           d=((c&0xf)<<12)|((Src[0]&0x3f)<<6)|(Src[1]&0x3f);
  169.           Src+=2;
  170.         }
  171.         else
  172.           if ((c>>3)==30)
  173.           {
  174.             if ((Src[0]&0xc0)!=0x80 || (Src[1]&0xc0)!=0x80 || (Src[2]&0xc0)!=0x80)
  175.               break;
  176.             d=((c&7)<<18)|((Src[0]&0x3f)<<12)|((Src[1]&0x3f)<<6)|(Src[2]&0x3f);
  177.             Src+=3;
  178.           }
  179.           else
  180.             break;
  181.     if (--DestSize<0)
  182.       break;
  183.     if (d>0xffff)
  184.     {
  185.       if (--DestSize<0 || d>0x10ffff)
  186.         break;
  187.       *(Dest++)=((d-0x10000)>>10)+0xd800;
  188.       *(Dest++)=(d&0x3ff)+0xdc00;
  189.     }
  190.     else
  191.       *(Dest++)=d;
  192.   }
  193.   *Dest=0;
  194. }
  195.  
  196.  
  197. bool UnicodeEnabled()
  198. {
  199. #ifdef UNICODE_SUPPORTED
  200.   #ifdef _EMX
  201.     return(uni_ready);
  202.   #else
  203.     return(true);
  204.   #endif
  205. #else
  206.   return(false);
  207. #endif
  208. }
  209.  
  210.  
  211. int strlenw(const wchar *str)
  212. {
  213.   int length=0;
  214.   while (*(str++)!=0)
  215.     length++;
  216.   return(length);
  217. }
  218.  
  219.  
  220. wchar* strcpyw(wchar *dest,const wchar *src)
  221. {
  222.   do {
  223.     *(dest++)=*src;
  224.   } while (*(src++)!=0);
  225.   return(dest);
  226. }
  227.  
  228.  
  229. wchar* strncpyw(wchar *dest,const wchar *src,int n)
  230. {
  231.   do {
  232.     *(dest++)=*src;
  233.   } while (*(src++)!=0 && --n > 0);
  234.   return(dest);
  235. }
  236.  
  237.  
  238. wchar* strcatw(wchar *dest,const wchar *src)
  239. {
  240.   return(strcpyw(dest+strlenw(dest),src));
  241. }
  242.  
  243.  
  244. #ifndef SFX_MODULE
  245. wchar* strncatw(wchar *dest,const wchar *src,int n)
  246. {
  247.   dest+=strlenw(dest);
  248.   while (true)
  249.     if (--n<0)
  250.     {
  251.       *dest=0;
  252.       break;
  253.     }
  254.     else
  255.       if ((*(dest++)=*(src++))==0)
  256.         break;
  257.   return(dest);
  258. }
  259. #endif
  260.  
  261.  
  262. int strcmpw(const wchar *s1,const wchar *s2)
  263. {
  264.   while (*s1==*s2)
  265.   {
  266.     if (*s1==0)
  267.       return(0);
  268.     s1++;
  269.     s2++;
  270.   }
  271.   return(*s1<*s2 ? -1:1);
  272. }
  273.  
  274.  
  275. int strncmpw(const wchar *s1,const wchar *s2,int n)
  276. {
  277.   while (n-->0)
  278.   {
  279.     if (*s1<*s2)
  280.       return(-1);
  281.     if (*s1>*s2)
  282.       return(-1);
  283.     if (*s1==0)
  284.       break;
  285.     s1++;
  286.     s2++;
  287.   }
  288.   return(0);
  289. }
  290.  
  291.  
  292. #ifndef SFX_MODULE
  293. int stricmpw(const wchar *s1,const wchar *s2)
  294. {
  295.   char Ansi1[NM*sizeof(wchar)],Ansi2[NM*sizeof(wchar)];
  296.   WideToChar(s1,Ansi1,sizeof(Ansi1));
  297.   WideToChar(s2,Ansi2,sizeof(Ansi2));
  298.   return(stricomp(Ansi1,Ansi2));
  299. }
  300. #endif
  301.  
  302.  
  303. #if !defined(SFX_MODULE) && !defined(_WIN_CE)
  304. inline int strnicmpw_w2c(const wchar *s1,const wchar *s2,int n)
  305. {
  306.   wchar Wide1[NM*2],Wide2[NM*2];
  307.   strncpyw(Wide1,s1,sizeof(Wide1)/sizeof(Wide1[0])-1);
  308.   strncpyw(Wide2,s2,sizeof(Wide2)/sizeof(Wide2[0])-1);
  309.   Wide1[Min(sizeof(Wide1)/sizeof(Wide1[0])-1,n)]=0;
  310.   Wide2[Min(sizeof(Wide2)/sizeof(Wide2[0])-1,n)]=0;
  311.   char Ansi1[NM*2],Ansi2[NM*2];
  312.   WideToChar(Wide1,Ansi1,sizeof(Ansi1));
  313.   WideToChar(Wide2,Ansi2,sizeof(Ansi2));
  314.   return(stricomp(Ansi1,Ansi2));
  315. }
  316. #endif
  317.  
  318.  
  319. #ifndef SFX_MODULE
  320. int strnicmpw(const wchar *s1,const wchar *s2,int n)
  321. {
  322.   return(strnicmpw_w2c(s1,s2,n));
  323. }
  324. #endif
  325.  
  326.  
  327. wchar* strchrw(const wchar *s,int c)
  328. {
  329.   while (*s)
  330.   {
  331.     if (*s==c)
  332.       return((wchar *)s);
  333.     s++;
  334.   }
  335.   return(NULL);
  336. }
  337.  
  338.  
  339. wchar* strrchrw(const wchar *s,int c)
  340. {
  341.   for (int I=strlenw(s)-1;I>=0;I--)
  342.     if (s[I]==c)
  343.       return((wchar *)(s+I));
  344.   return(NULL);
  345. }
  346.  
  347.  
  348. wchar* strpbrkw(const wchar *s1,const wchar *s2)
  349. {
  350.   while (*s1)
  351.   {
  352.     if (strchrw(s2,*s1)!=NULL)
  353.       return((wchar *)s1);
  354.     s1++;
  355.   }
  356.   return(NULL);
  357. }
  358.  
  359.  
  360. #ifndef SFX_MODULE
  361. wchar* strlowerw(wchar *Str)
  362. {
  363.   for (wchar *ChPtr=Str;*ChPtr;ChPtr++)
  364.     if (*ChPtr<128)
  365.       *ChPtr=loctolower(*ChPtr);
  366.   return(Str);
  367. }
  368. #endif
  369.  
  370.  
  371. #ifndef SFX_MODULE
  372. wchar* strupperw(wchar *Str)
  373. {
  374.   for (wchar *ChPtr=Str;*ChPtr;ChPtr++)
  375.     if (*ChPtr<128)
  376.       *ChPtr=loctoupper(*ChPtr);
  377.   return(Str);
  378. }
  379. #endif
  380.  
  381.  
  382. #ifndef SFX_MODULE
  383. int toupperw(int ch)
  384. {
  385.   return((ch<128) ? loctoupper(ch):ch);
  386. }
  387. #endif
  388.  
  389.  
  390. int atoiw(const wchar *s)
  391. {
  392.   int n=0;
  393.   while (*s>='0' && *s<='9')
  394.   {
  395.     n=n*10+(*s-'0');
  396.     s++;
  397.   }
  398.   return(n);
  399. }
  400.  
  401.  
  402. #ifdef DBCS_SUPPORTED
  403. SupportDBCS gdbcs;
  404.  
  405. SupportDBCS::SupportDBCS()
  406. {
  407.   Init();
  408. }
  409.  
  410.  
  411. void SupportDBCS::Init()
  412. {
  413.   CPINFO CPInfo;
  414.   GetCPInfo(CP_ACP,&CPInfo);
  415.   DBCSMode=CPInfo.MaxCharSize > 1;
  416.   for (int I=0;I<sizeof(IsLeadByte)/sizeof(IsLeadByte[0]);I++)
  417.     IsLeadByte[I]=IsDBCSLeadByte(I);
  418. }
  419.  
  420.  
  421. char* SupportDBCS::charnext(const char *s)
  422. {
  423.   return (char *)(IsLeadByte[*s] ? s+2:s+1);
  424. }
  425.  
  426.  
  427. uint SupportDBCS::strlend(const char *s)
  428. {
  429.   uint Length=0;
  430.   while (*s!=0)
  431.   {
  432.     if (IsLeadByte[*s])
  433.       s+=2;
  434.     else
  435.       s++;
  436.     Length++;
  437.   }
  438.   return(Length);
  439. }
  440.  
  441.  
  442. char* SupportDBCS::strchrd(const char *s, int c)
  443. {
  444.   while (*s!=0)
  445.     if (IsLeadByte[*s])
  446.       s+=2;
  447.     else
  448.       if (*s==c)
  449.         return((char *)s);
  450.       else
  451.         s++;
  452.   return(NULL);
  453. }
  454.  
  455.  
  456. void SupportDBCS::copychrd(char *dest,const char *src)
  457. {
  458.   dest[0]=src[0];
  459.   if (IsLeadByte[src[0]])
  460.     dest[1]=src[1];
  461. }
  462.  
  463.  
  464. char* SupportDBCS::strrchrd(const char *s, int c)
  465. {
  466.   const char *found=NULL;
  467.   while (*s!=0)
  468.     if (IsLeadByte[*s])
  469.       s+=2;
  470.     else
  471.     {
  472.       if (*s==c)
  473.         found=s;
  474.       s++;
  475.     }
  476.   return((char *)found);
  477. }
  478. #endif
  479.