📄 wstring.c
字号:
/*
+-----------------------------------------------------------------------+
| Restricted Rights Legend |
| Copyright(c) 1999-2000 by Mobile Innovation, Inc. |
| This file is the property of the Mobile Innovation Inc. and it is |
| protected by P.R.China Copyright laws, Internationals laws and |
| treaties. It is there by forbidden for any unauthorized use, |
| duplication, distribution or disclosure by any commercial industry |
| (public or private), private individual, or by any Government agency |
| without a explicit written consent of release from Synchronization. |
|.................................................................... |
| PROJECT: Lithium $Workfile:: wstring.c $|
| $Author: root $$Revision: 1.1.1.1 $|
| CREATED: 15 MAR 2001 $Modtime:: Mar 15 2001 14:12:34 $|
+-----------------------------------------------------------------------+
PURPOSE : For Unicode Extension
TO DO : Unicode string process
*/
#include <string.h>
#include "wstring.h"
static unsigned short sms2uni[] = {
'@', 0xa3, '$', 0xa5, 0xe8, 0xe9, 0xf9, 0xec, 0xf2, 0xc7, 0x0a, 0xd8, 0xf8, 0x0d, 0xc5, 0xe5,
0x394,'_',0x3a6,0x393,0x39b,0x3a9,0x3a0,0x3a8,0x3a3,0x398,0x39e,' ',0xc6,0xe6,0xdf,0xc9,
' ', '!', '\"','#',0xa4, '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
0xa1, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 0xc4,0xd6,0xd1,0xdc,0xa7,
0xbf,'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 0xe4, 0xf6, 0xf1, 0xfc, 0xe0,
'@'
};
typedef struct
{
unsigned short uni,sms;
} _uni_sms_pair_;
static _uni_sms_pair_ uni2sms[] = {
{0x000a,0x0a}, {0x000d,0x0d}, {0x0020,0x20}, {0x0021,0x21}, {0x0022,0x22}, {0x0023,0x23}, {0x0024,0x02}, {0x0025,0x25},
{0x0026,0x26}, {0x0027,0x27}, {0x0028,0x28}, {0x0029,0x29}, {0x002a,0x2a}, {0x002b,0x2b}, {0x002c,0x2c}, {0x002d,0x2d},
{0x002e,0x2e}, {0x002f,0x2f}, {0x0030,0x30}, {0x0031,0x31}, {0x0032,0x32}, {0x0033,0x33}, {0x0034,0x34}, {0x0035,0x35},
{0x0036,0x36}, {0x0037,0x37}, {0x0038,0x38}, {0x0039,0x39}, {0x003a,0x3a}, {0x003b,0x3b}, {0x003c,0x3c}, {0x003d,0x3d},
{0x003e,0x3e}, {0x003f,0x3f}, {0x0040,0x00}, {0x0041,0x41}, {0x0042,0x42}, {0x0043,0x43}, {0x0044,0x44}, {0x0045,0x45},
{0x0046,0x46}, {0x0047,0x47}, {0x0048,0x48}, {0x0049,0x49}, {0x004a,0x4a}, {0x004b,0x4b}, {0x004c,0x4c}, {0x004d,0x4d},
{0x004e,0x4e}, {0x004f,0x4f}, {0x0050,0x50}, {0x0051,0x51}, {0x0052,0x52}, {0x0053,0x53}, {0x0054,0x54}, {0x0055,0x55},
{0x0056,0x56}, {0x0057,0x57}, {0x0058,0x58}, {0x0059,0x59}, {0x005a,0x5a}, {0x005f,0x11}, {0x0061,0x61}, {0x0062,0x62},
{0x0063,0x63}, {0x0064,0x64}, {0x0065,0x65}, {0x0066,0x66}, {0x0067,0x67}, {0x0068,0x68}, {0x0069,0x69}, {0x006a,0x6a},
{0x006b,0x6b}, {0x006c,0x6c}, {0x006d,0x6d}, {0x006e,0x6e}, {0x006f,0x6f}, {0x0070,0x70}, {0x0071,0x71}, {0x0072,0x72},
{0x0073,0x73}, {0x0074,0x74}, {0x0075,0x75}, {0x0076,0x76}, {0x0077,0x77}, {0x0078,0x78}, {0x0079,0x79}, {0x007a,0x7a},
{0x00a1,0x40}, {0x00a3,0x01}, {0x00a4,0x24}, {0x00a5,0x03}, {0x00a7,0x5f}, {0x00bf,0x60}, {0x00c4,0x5b}, {0x00c5,0x0e},
{0x00c6,0x1c}, {0x00c7,0x09}, {0x00c9,0x1f}, {0x00d1,0x5d}, {0x00d6,0x5c}, {0x00d8,0x0b}, {0x00dc,0x5e}, {0x00df,0x1e},
{0x00e0,0x7f}, {0x00e4,0x7b}, {0x00e5,0x0f}, {0x00e6,0x1d}, {0x00e8,0x04}, {0x00e9,0x05}, {0x00ec,0x07}, {0x00f1,0x7d},
{0x00f2,0x08}, {0x00f6,0x7c}, {0x00f8,0x0c}, {0x00f9,0x06}, {0x00fc,0x7e}, {0x0393,0x13}, {0x0394,0x10}, {0x0398,0x19},
{0x039b,0x14}, {0x039e,0x1a}, {0x03a0,0x16}, {0x03a3,0x18}, {0x03a6,0x12}, {0x03a8,0x17}, {0x03a9,0x15}
};
#if 1
extern void AscEx_To_Unicode(char* ascEx_txt, char* uni_txt);
/*
* clrobert commented
* Return the string length in bytes, even in unicode case, because at present
* the fontinfo.font_w is 1/2 of the real font width, it may should be modified
* to get a simple process for unicode display
*
*/
int wstrlen(char * txt)
{
int lenth = 0;
unsigned char * ss = (unsigned char*)txt;
#ifdef _INNOVATION_EMULATOR_
if (txt == NULL)
return 0;
#endif
if (*ss == 0x80)//if unicode
{
ss++;
lenth++;
while ((*ss<<8)+*(ss+1)!=0)
{
lenth += 2;
ss += 2;
}
} else//if ascii
{
while (*ss++ != 0)
lenth++;
}
return lenth;
}
/*
* clrobert comment
* Return the string length in bytes, in unicode case the leading flag byte is
* not counted, it should be modified to return the 1/2 of the length of the
* real char string in unicode case
*
*/
int wstrBitlen(char * txt)
{
int lenth=0;
unsigned char * ss=(unsigned char*)txt;
#ifdef _INNOVATION_EMULATOR_
if (txt == NULL)
return 0;
#endif
if (*ss == (unsigned char)0x80)//if unicode
{
ss++;
//lenth++; xsf disabled it 2003.01.06
while ((*ss<<8) + *(ss+1) != 0)
{
if (*ss == 0x00)//if not chinese
lenth++;
else
lenth += 2;
ss += 2;
}
} else//if ascii
{
while (*ss++ != 0)
lenth++;
}
return lenth;
}
/*
*
*
*/
void wstrcpy(char *outtxt, char *intxt)
{
int lenth = wstrlen(intxt);
#ifdef _INNOVATION_EMULATOR_
if (outtxt == NULL || intxt == NULL)
return;
#endif
if ((unsigned char)*intxt == 0x80)
{
memcpy(outtxt, intxt, lenth +2);
} else
{
strcpy(outtxt, intxt);
outtxt[lenth] = 0;
}
}
void wstrncpy(char * ws1, char * ws2, int size)
{
int i=0;
int tmplen=size;
int lenth=wstrlen((char *)ws2);
if ((unsigned char)*ws2==0x80)
tmplen+=1;
if (lenth>=tmplen)
lenth=tmplen;
memcpy(ws1,ws2,lenth);
ws1[lenth]=0;
ws1[lenth+1]=0;
ws1[lenth+2]=0;//to avoid crash
return;
}
void wstrcat(char * ws1, char * ws2)
{
char *tmpstr;
unsigned char *ss1=(unsigned char*)ws1;
unsigned char *ss2=(unsigned char*)ws2;
int len1=wstrlen((char*)ss1);
int len2=wstrlen((char*)ss2);
if (*ss1!=0x80&&*ss2!=0x80)
{//the both strings are all ascii
memcpy(&ws1[len1],ss2,len2);
ws1[len1+len2]=0x00;
} else if (*ss1==0x80&&*ss2==0x80)
{//if both ws1 and ws2 are unicode;
memcpy(&ws1[len1],&ss2[1],len2-1);
ws1[len1+len2-1]=0;
ws1[len1+len2]=0;
ws1[len1+len2+1]=0;//to avoid crash
} else if (*ss1==0x80&&*ss2!=0x80)
{//if ws1 is unicode and ws2 is ascii;
tmpstr=(char*)malloc(2*len2+3);
AscEx_To_Unicode((char *)ss2, tmpstr);
memcpy(&ws1[len1],&tmpstr[1],2*len2);
ws1[len1+2*len2]=0;
ws1[len1+2*len2+1]=0;
ws1[len1+2*len2+2]=0;//to avoid crash
free(tmpstr);
} else if (*ss1!=0x80&&*ss2==0x80)
{//if ws2 is unicode and ws1 is ascii;
tmpstr=(char*)malloc(2*len1+3);
AscEx_To_Unicode((char *)ss1, tmpstr);
memcpy(ws1,tmpstr,2*len1+1);
memcpy(&ws1[2*len1+1],&ss2[1],len2-1);
ws1[2*len1+len2]=0;
ws1[2*len1+len2+1]=0;
ws1[2*len1+len2+2]=0;//to avoid crash
free(tmpstr);
}
return;
}
void wstrncat(char * ws1, char * ws2,int size)
{
int i=0;
int lenth=wstrlen(ws2);
int tmplen=size;
char *ss=(char*)malloc(lenth+3);
if ((unsigned char)*ws2==0x80)
tmplen+=1;
if (lenth>=tmplen)
lenth=tmplen;
memcpy(ss,ws2,lenth);
ss[lenth]=0;
ss[lenth+1]=0;
wstrcat(ws1,ss);
free(ss);
return;
}
#endif
#if 1
/****************************************************************************/
/* wstrchr */
/****************************************************************************/
unsigned short *wstrchr(const unsigned short *ws, int c)
{
do
if (*ws == c) return(unsigned short *)ws;
while (*ws++);
return NULL;
}
/****************************************************************************/
/* str2wstr */
/****************************************************************************/
unsigned short *str2wstr(unsigned short *ws, const char *s)
{
unsigned short *p = ws;
while (*p++ = *s++);
return(ws);
}
/****************************************************************************/
/* str2nwstr */
/****************************************************************************/
unsigned short *str2nwstr(unsigned short *ws, const char *s, size_t n)
{
unsigned short *p = ws;
while (n--) *p++ = *s++;
return(ws);
}
/****************************************************************************/
/* wstr2str */
/****************************************************************************/
char *wstr2str(char *s, const unsigned short *ws)
{
char *p = s;
while (*ws < 0x80 && *ws) *p++ = (char)*ws++;
*p = 0;
return(s);
}
/****************************************************************************/
/* wstr2nstr */
/****************************************************************************/
char *wstr2nstr(char *s, const unsigned short *ws, size_t n)
{
char *p = s;
while (*ws < 0x80 && n--) *p++ = (char)*ws++;
return(s);
}
/****************************************************************************/
/* strrchr */
/****************************************************************************/
unsigned short *wstrrchr(const unsigned short *ws, int c)
{
unsigned short *result = NULL;
do
if (*ws == c) result = (unsigned short *)ws;
while (*ws++);
return(result);
}
/****************************************************************************/
/* wmemchr */
/****************************************************************************/
unsigned short *wmemchr(const unsigned short *s, int c, size_t n)
{
const unsigned short *st = s;
unsigned short ch = c;
while (n-- != 0)
if (*st++ == ch)
{
return(unsigned short *)--st;
}
return NULL;
}
/****************************************************************************/
/* wmemset */
/****************************************************************************/
unsigned short *wmemset(unsigned short *s, int c, size_t n)
{
unsigned short *st = s;
unsigned short ch = c;
while (n-- > 0) *st++ = ch;
return s;
}
/****************************************************************************/
/* wstrcmp */
/****************************************************************************/
int wstrcmp(const unsigned short *ws1, const unsigned short *ws2)
{
unsigned short *st1 = (unsigned short *)ws1;
unsigned short *st2 = (unsigned short *)ws2;
unsigned int ch1, diff;
while (!(diff = (ch1 = *st1++) - *st2++) && ch1 != 0) ;
return diff;
}
/****************************************************************************/
/* wstrncmp */
/****************************************************************************/
int wstrncmp(const unsigned short *ws1, const unsigned short *ws2, size_t n)
{
unsigned short *st1 = (unsigned short *)ws1;
unsigned short *st2 = (unsigned short *)ws2;
unsigned int ch1, diff;
if (n == 0) return 0;
while (n-- && !(diff = (ch1 = *st1++) - *st2++) && ch1 != 0) ;
return diff;
}
/****************************************************************************/
/* is_sms_alphabet */
/****************************************************************************/
int is_sms_alphabet(unsigned short c)
{
int b,e,l;
if ((c>=' '&&c<='#')||(c>='%'&&c<='?')||((c|0x20)>='a'&&(c|0x20)<='z'))
return 1;
if (c > 0x3a9)// primary u is used and error occur
return 0;
b = 0;
e = sizeof(uni2sms)/sizeof(_uni_sms_pair_);
while (b+1 < e)
{
l = (b+e)/2;
if (uni2sms[l].uni == c)
return 1;
if (uni2sms[l].uni < c)
b = l;
else
e = l;
}
if (uni2sms[b].uni == c||uni2sms[e].uni == c)
return 1;
return 0;
}
/****************************************************************************/
/* sms2unicode */
/****************************************************************************/
unsigned short sms2unicode(unsigned short s)
{
if (s > 0x80)
return 0x20;
return sms2uni[s];
}
/****************************************************************************/
/* unicode2sms */
/****************************************************************************/
unsigned short unicode2sms(unsigned short u)
{
int b,e,l;
if ((u>=' '&&u<='#')||(u>='%'&&u<='?')||((u|0x20)>='a'&&(u|0x20)<='z'))
return u;
if (u > 0x3a9)
return 0x20;
b = 0;
e = sizeof(uni2sms)/sizeof(_uni_sms_pair_);
while (b+1 < e)
{
l = (b+e)/2;
if (uni2sms[l].uni == u)
return uni2sms[l].sms;
if (uni2sms[l].uni < u)
b = l;
else
e = l;
}
if (uni2sms[b].uni == u)
return uni2sms[b].sms;
if (uni2sms[e].uni == u)
return uni2sms[e].sms;
return 0x20;
}
/*--------------------------------------------------------------------
function: expand2wstr(unsinged short *ws, const char *s)
description: copy char *s to short *ws, convert char string to unicode string
---------------------------------------------------------------------*/
unsigned short *expand2wstr(unsigned short *ws, const char *s)
{
unsigned short *p=ws;
while (*p++ = *s++);
return(ws);
}
/*--------------------------------------------------------------------
function: expand2nwstr(unsinged short *ws, const char *s)
description: copy number of n char *s to short *ws, convert char string to unicode string
---------------------------------------------------------------------*/
unsigned short *expand2nwstr(unsigned short *ws, const char *s, size_t n)
{
unsigned short *p = ws;
while (n--)
{
*p++ = *s++;
}
return(ws);
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -