Files
kichikuou_xsystem35-sdl2/src/utfsjis.c
T

182 lines
3.7 KiB
C

/*
* utfsjis.c -- utf-8/sjis related function
*
* Copyright (C) 1997 Yutaka OIWA <oiwa@is.s.u-tokyo.ac.jp>
*
* written for Satoshi KURAMOCHI's "eplaymidi"
* <satoshi@ueda.info.waseda.ac.jp>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/* $Id: eucsjis.c,v 1.2 2000/09/20 10:33:16 chikama Exp $ */
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "portab.h"
#include "utfsjis.h"
#include "s2utbl.h"
BYTE *sjis2utf(BYTE *src) {
BYTE* dst = malloc(strlen(src) * 3 + 1);
BYTE* dstp = dst;
while (*src) {
if (*src <= 0x7f) {
*dstp++ = *src++;
continue;
}
int c;
if (*src >= 0xa0 && *src <= 0xdf) {
c = 0xff60 + *src - 0xa0;
src++;
} else {
c = s2u[*src - 0x80][*(src+1) - 0x40];
src += 2;
}
if (c <= 0x7f) {
*dstp++ = c;
} else if (c <= 0x7ff) {
*dstp++ = 0xc0 | c >> 6;
*dstp++ = 0x80 | (c & 0x3f);
} else {
*dstp++ = 0xe0 | c >> 12;
*dstp++ = 0x80 | (c >> 6 & 0x3f);
*dstp++ = 0x80 | (c & 0x3f);
}
}
*dstp = '\0';
return dst;
}
static int unicode_to_sjis(int u) {
for (int b1 = 0x80; b1 <= 0xff; b1++) {
if (b1 >= 0xa0 && b1 <= 0xdf)
continue;
for (int b2 = 0x40; b2 <= 0xff; b2++) {
if (u == s2u[b1 - 0x80][b2 - 0x40])
return b1 << 8 | b2;
}
}
return 0;
}
BYTE *utf2sjis(BYTE *src) {
BYTE* dst = malloc(strlen(src) + 1);
BYTE* dstp = dst;
while (*src) {
if (*src <= 0x7f) {
*dstp++ = *src++;
continue;
}
int u;
if (*src <= 0xdf) {
u = (src[0] & 0x1f) << 6 | (src[1] & 0x3f);
src += 2;
} else if (*src <= 0xef) {
u = (src[0] & 0xf) << 12 | (src[1] & 0x3f) << 6 | (src[2] & 0x3f);
src += 3;
} else {
*dstp++ = '?';
do src++; while ((*src & 0xc0) == 0x80);
continue;
}
if (u > 0xff60 && u <= 0xff9f) {
*dstp++ = u - 0xff60 + 0xa0;
} else {
int c = unicode_to_sjis(u);
if (c) {
*dstp++ = c >> 8;
*dstp++ = c & 0xff;
} else {
*dstp++ = '?';
}
}
}
*dstp = '\0';
return dst;
}
/* src 内に半角カナもしくはASCII文字があるかどうか */
boolean sjis_has_hankaku(BYTE *src) {
while(*src) {
if (CHECKSJIS1BYTE(*src)) {
src++;
} else {
return TRUE;
}
src++;
}
return FALSE;
}
/* src 内に 全角文字があるかどうか */
boolean sjis_has_zenkaku(BYTE *src) {
while(*src) {
if (CHECKSJIS1BYTE(*src)) {
return TRUE;
}
src++;
}
return FALSE;
}
/* src 中の文字数を数える 全角文字も1文字 */
int sjis_count_char(BYTE *src) {
int c = 0;
while(*src) {
if (CHECKSJIS1BYTE(*src)) {
src++;
}
c++; src++;
}
return c;
}
/* SJIS(EUC) を含む文字列の ASCII を大文字化する */
void sjis_toupper(BYTE *src) {
while(*src) {
if (CHECKSJIS1BYTE(*src)) {
src++;
} else {
if (*src >= 0x60 && *src <= 0x7a) {
*src &= 0xdf;
}
}
src++;
}
}
/* SJIS を含む文字列の ASCII を大文字化する2 */
BYTE *sjis_toupper2(BYTE *src) {
BYTE *dst;
dst = malloc(strlen(src) +1);
if (dst == NULL) return NULL;
strcpy(dst, src);
sjis_toupper(dst);
return dst;
}