sj4/lib/sj4rkcv/wc16_str.c

372 lines
6.8 KiB
C
Raw Normal View History

2008-01-11 11:57:08 +00:00
/*
* Copyright (c) 1994 Sony Corporation
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL SONY CORPORATION BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2026-06-19 01:49:53 +09:00
*
2008-01-11 11:57:08 +00:00
* Except as contained in this notice, the name of Sony Corporation
* shall not be used in advertising or otherwise to promote the sale, use
* or other dealings in this Software without prior written authorization
* from Sony Corporation.
*
*/
#include <stdio.h>
#include <locale.h>
#include "wchar16.h"
2026-06-20 02:12:04 +09:00
#include "sj4rkcv.h"
2008-01-11 11:57:08 +00:00
#define MSB 0x80
#ifndef TRUE
#define TRUE (1)
#define FALSE (0)
#endif
2026-06-20 02:12:04 +09:00
#define sjis2euc sj4_sjis2euc
#define euc2sjis sj4_euc2sjis
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
#define issjis1(c) (((0x81 <= (c)) && ((c) <= 0x9f)) || \
((0xe0 <= (c)) && ((c) <= 0xfc)))
#define iseuc(c) ((0xa1 <= (c)) && ((c) <= 0xfe))
#define iskana(c) ((0xa1 <= (c)) && ((c) <= 0xdf))
2008-01-11 11:57:08 +00:00
extern int current_locale;
2026-06-20 02:12:04 +09:00
int sj4_iswcntrl16(wchar16_t wc) {
2026-06-19 01:49:53 +09:00
if((wc < 0x20) || (wc == 0x7f)) return TRUE;
2008-01-11 11:57:08 +00:00
return FALSE;
}
2026-06-20 02:12:04 +09:00
int sj4_iswupper16(wchar16_t wc) {
2026-06-19 01:49:53 +09:00
if(((0x40 < wc) && (wc < 0x5b)) ||
((0xa3c0 < wc) && (wc < 0xa3db))) return TRUE;
2008-01-11 11:57:08 +00:00
return FALSE;
}
2026-06-20 02:12:04 +09:00
int sj4_iswdigit16(wchar16_t wc) {
2026-06-19 01:49:53 +09:00
if(((0x29 < wc) && (wc < 0x3a)) ||
((0xa3af < wc) && (wc < 0xa3ba))) return TRUE;
2008-01-11 11:57:08 +00:00
return FALSE;
}
2026-06-20 02:12:04 +09:00
int sj4_iswxdigit16(wchar16_t wc) {
2026-06-19 01:49:53 +09:00
if(((0x29 < wc) && (wc < 0x3a)) ||
((0xa3af < wc) && (wc < 0xa3ba))) return TRUE;
if(((0x60 < wc) && (wc < 0x67)) ||
((0x40 < wc) && (wc < 0x47))) return TRUE;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(((0xa3c0 < wc) && (wc < 0xa3c7)) ||
((0xa3e0 < wc) && (wc < 0xa3e6))) return TRUE;
2008-01-11 11:57:08 +00:00
return FALSE;
}
2026-06-20 02:12:04 +09:00
int sj4_wslen16(wchar16_t* ws) {
2026-06-19 01:49:53 +09:00
int i = 0;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(!ws) return 0;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
while(*ws++) i++;
2008-01-11 11:57:08 +00:00
return i;
}
2026-06-20 02:12:04 +09:00
int sj4_wscmp16(wchar16_t* ws1, wchar16_t* ws2) {
2026-06-19 01:49:53 +09:00
while((*ws1 && *ws2) && (*ws1 == *ws2)) {
2008-01-11 11:57:08 +00:00
ws1++;
ws2++;
}
2026-06-19 01:49:53 +09:00
if(!*ws1 && !*ws2) return 0;
if(!*ws1) return -1;
if(!*ws2) return 1;
if(*ws1 < *ws2) return -1;
return 1;
2008-01-11 11:57:08 +00:00
}
2026-06-20 02:12:04 +09:00
int sj4_wsncmp16(wchar16_t* ws1, wchar16_t* ws2, int n) {
2026-06-19 01:49:53 +09:00
int i = 0;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
while((*ws1 && *ws2) && (*ws1 == *ws2) && (i < n)) {
2008-01-11 11:57:08 +00:00
ws1++;
ws2++;
i++;
}
2026-06-19 01:49:53 +09:00
if(!*ws1 && !*ws2) return 0;
if(i == n) return 0;
if(!*ws1) return -1;
if(!*ws2) return 1;
if(*ws1 < *ws2) return -1;
2008-01-11 11:57:08 +00:00
return 1;
}
2026-06-19 01:49:53 +09:00
wchar16_t*
2026-06-20 02:12:04 +09:00
sj4_wscpy16(wchar16_t* ws1, wchar16_t* ws2) {
2026-06-19 01:49:53 +09:00
wchar16_t* ws;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(!ws2) return NULL;
2008-01-11 11:57:08 +00:00
ws = ws1;
2026-06-19 01:49:53 +09:00
while(*ws2) {
2008-01-11 11:57:08 +00:00
*ws++ = *ws2++;
}
*ws = 0;
return ws1;
}
2026-06-19 01:49:53 +09:00
wchar16_t*
2026-06-20 02:12:04 +09:00
sj4_wsncpy16(wchar16_t* ws1, wchar16_t* ws2, int n) {
2026-06-19 01:49:53 +09:00
wchar16_t* ws;
int i = 0;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(!ws2) return NULL;
2008-01-11 11:57:08 +00:00
ws = ws1;
2026-06-19 01:49:53 +09:00
while(*ws2 && (i < n)) {
2008-01-11 11:57:08 +00:00
*ws++ = *ws2++;
i++;
}
2026-06-19 01:49:53 +09:00
if(i < n) *ws = 0;
2008-01-11 11:57:08 +00:00
return ws1;
}
2026-06-19 01:49:53 +09:00
wchar16_t*
2026-06-20 02:12:04 +09:00
sj4_wscat16(wchar16_t* ws1, wchar16_t* ws2) {
2026-06-19 01:49:53 +09:00
wchar16_t* ws;
2008-01-11 11:57:08 +00:00
2026-06-19 01:49:53 +09:00
if(!ws1) return NULL;
if(!ws2) return NULL;
2008-01-11 11:57:08 +00:00
ws = ws1;
2026-06-19 01:49:53 +09:00
while(*ws) ws++;
while(*ws2) {
2008-01-11 11:57:08 +00:00
*ws++ = *ws2++;
}
*ws = 0;
return ws1;
}
wchar16_t
2026-06-20 02:12:04 +09:00
sj4_euc2wc16(unsigned int code) {
2008-01-11 11:57:08 +00:00
wchar16_t wc = 0;
2026-06-19 01:49:53 +09:00
if(((code >> 16) & 0xff) == SS3) {
2008-01-11 11:57:08 +00:00
wc = code & 0xff7f;
2026-06-19 01:49:53 +09:00
} else if(((code >> 8) & 0xff) == SS2) {
2008-01-11 11:57:08 +00:00
wc = code & 0xff;
} else {
wc = code;
2026-06-19 01:49:53 +09:00
}
2008-01-11 11:57:08 +00:00
return wc;
}
wchar16_t
2026-06-20 02:12:04 +09:00
sj4_sjis2wc16(unsigned int code) {
2008-01-11 11:57:08 +00:00
unsigned short ch;
2026-06-19 01:49:53 +09:00
if((code >> 8) & 0xff) {
2008-01-11 11:57:08 +00:00
ch = sjis2euc(code);
2026-06-19 01:49:53 +09:00
} else if(code & MSB) {
2008-01-11 11:57:08 +00:00
ch = (SS2 << 8) + (code & 0xff);
} else {
return code;
}
2026-06-20 02:12:04 +09:00
return (sj4_euc2wc16(ch));
2008-01-11 11:57:08 +00:00
}
unsigned int
2026-06-20 02:12:04 +09:00
sj4_wc2euc16(wchar16_t wc) {
2026-06-19 01:49:53 +09:00
unsigned int code = 0;
2008-01-11 11:57:08 +00:00
unsigned char tmp;
tmp = (wc >> 8) & 0xff;
2026-06-19 01:49:53 +09:00
if(tmp) {
if(wc & MSB) {
2008-01-11 11:57:08 +00:00
code = wc;
} else {
code = (SS3 << 16) + wc + MSB;
}
} else {
2026-06-19 01:49:53 +09:00
if(wc & MSB) {
2008-01-11 11:57:08 +00:00
code = (SS2 << 8) + wc;
} else {
code = wc;
}
}
return code;
}
unsigned int
2026-06-20 02:12:04 +09:00
sj4_wc2sjis16(wchar16_t wc) {
2008-01-11 11:57:08 +00:00
unsigned int ch;
2026-06-20 02:12:04 +09:00
ch = sj4_wc2euc16(wc);
2026-06-19 01:49:53 +09:00
if((ch >> 16) == SS3) {
2008-01-11 11:57:08 +00:00
return 0;
2026-06-19 01:49:53 +09:00
} else if((ch >> 8) == SS2) {
2008-01-11 11:57:08 +00:00
return (ch & 0xff);
2026-06-19 01:49:53 +09:00
} else if((ch >> 8) & 0xff) {
2008-01-11 11:57:08 +00:00
return euc2sjis(ch);
} else {
return wc;
}
}
2026-06-20 02:12:04 +09:00
int sj4_wcs2eucs16(unsigned char* mb, wchar16_t* ws, int n) {
2026-06-19 01:49:53 +09:00
int i = 0;
unsigned int code;
2008-01-11 11:57:08 +00:00
unsigned char c;
2026-06-19 01:49:53 +09:00
while(*ws && (i < n)) {
2026-06-20 02:12:04 +09:00
code = sj4_wc2euc16(*ws);
2026-06-19 01:49:53 +09:00
if(c = ((code >> 16) & 0xff)) {
2008-01-11 11:57:08 +00:00
*mb++ = c;
i++;
2026-06-19 01:49:53 +09:00
if(i == n) break;
2008-01-11 11:57:08 +00:00
*mb++ = ((code >> 8) & 0xff);
i++;
2026-06-19 01:49:53 +09:00
if(i == n) break;
2008-01-11 11:57:08 +00:00
*mb++ = code & 0xff;
2026-06-19 01:49:53 +09:00
i++;
if(i == n) break;
} else if(c = ((code >> 8) & 0xff)) {
2008-01-11 11:57:08 +00:00
*mb++ = c;
i++;
2026-06-19 01:49:53 +09:00
if(i == n) break;
2008-01-11 11:57:08 +00:00
*mb++ = code & 0xff;
2026-06-19 01:49:53 +09:00
i++;
if(i == n) break;
} else if(c = (code & 0xff)) {
2008-01-11 11:57:08 +00:00
*mb++ = c;
i++;
} else {
return -1;
}
ws++;
}
2026-06-19 01:49:53 +09:00
if(!*ws) {
2008-01-11 11:57:08 +00:00
*mb = '\0';
}
return i;
}
2026-06-20 02:12:04 +09:00
int sj4_wcs2sjiss16(unsigned char* mb, wchar16_t* ws, int n) {
2026-06-19 01:49:53 +09:00
int i = 0;
unsigned int code;
2008-01-11 11:57:08 +00:00
unsigned char c;
2026-06-19 01:49:53 +09:00
while(*ws && (i < n)) {
2026-06-20 02:12:04 +09:00
code = sj4_wc2sjis16(*ws);
2026-06-19 01:49:53 +09:00
if(c = ((code >> 8) & 0xff)) {
2008-01-11 11:57:08 +00:00
*mb++ = c;
i++;
2026-06-19 01:49:53 +09:00
if(i == n) break;
2008-01-11 11:57:08 +00:00
*mb++ = code & 0xff;
i++;
2026-06-19 01:49:53 +09:00
if(i == n) break;
} else if(c = (code & 0xff)) {
2008-01-11 11:57:08 +00:00
*mb++ = c;
i++;
} else {
return -1;
}
ws++;
}
2026-06-19 01:49:53 +09:00
if(!*ws) {
2008-01-11 11:57:08 +00:00
*mb = '\0';
}
return i;
}
2026-06-20 02:12:04 +09:00
int sj4_eucs2wcs16(wchar16_t* ws, unsigned char* mb, int n) {
2026-06-19 01:49:53 +09:00
int i = 0;
2008-01-11 11:57:08 +00:00
unsigned int code;
2026-06-19 01:49:53 +09:00
while(*mb && (i < n)) {
if(*mb == SS3) {
2008-01-11 11:57:08 +00:00
code = *mb++ << 16;
code += *mb++ << 8;
code += *mb++;
2026-06-19 01:49:53 +09:00
} else if((*mb == SS2) || (*mb & 0x80)) {
2008-01-11 11:57:08 +00:00
code = *mb++ << 8;
code += *mb++;
} else {
code = *mb++;
}
2026-06-20 02:12:04 +09:00
*ws = sj4_euc2wc16(code);
2026-06-19 01:49:53 +09:00
if(!*ws) return -1;
2008-01-11 11:57:08 +00:00
ws++;
i++;
}
2026-06-19 01:49:53 +09:00
if(!*mb) {
2008-01-11 11:57:08 +00:00
*ws = 0;
}
return i;
}
2026-06-20 02:12:04 +09:00
int sj4_sjiss2wcs16(wchar16_t* ws, unsigned char* mb, int n) {
2026-06-19 01:49:53 +09:00
int i = 0;
2008-01-11 11:57:08 +00:00
unsigned int code;
2026-06-19 01:49:53 +09:00
while(*mb && (i < n)) {
if(issjis1(*mb)) {
2008-01-11 11:57:08 +00:00
code = *mb++ << 8;
code += *mb++;
} else {
code = *mb++;
}
2026-06-20 02:12:04 +09:00
*ws = sj4_sjis2wc16(code);
2026-06-19 01:49:53 +09:00
if(!*ws) return -1;
2008-01-11 11:57:08 +00:00
ws++;
i++;
}
2026-06-19 01:49:53 +09:00
if(!*mb) {
2008-01-11 11:57:08 +00:00
*ws = 0;
}
return i;
}
2026-06-20 02:12:04 +09:00
int sj4_mbstowcs16(wchar16_t* ws, unsigned char* mb, int n) {
2026-06-19 01:49:53 +09:00
if(current_locale == LC_CTYPE_EUC)
2026-06-20 02:12:04 +09:00
return sj4_eucs2wcs16(ws, mb, n);
2026-06-19 01:49:53 +09:00
else
2026-06-20 02:12:04 +09:00
return sj4_sjiss2wcs16(ws, mb, n);
2008-01-11 11:57:08 +00:00
}
2026-06-20 21:32:18 +09:00
int sj4_wcstombs16(unsigned char* mb, wchar16_t* ws, int n) {
2026-06-19 01:49:53 +09:00
if(current_locale == LC_CTYPE_EUC)
2026-06-20 02:12:04 +09:00
return sj4_wcs2eucs16(mb, ws, n);
2008-01-11 11:57:08 +00:00
else
2026-06-20 02:12:04 +09:00
return sj4_wcs2sjiss16(mb, ws, n);
2008-01-11 11:57:08 +00:00
}