multibyte.c 2.42 KB
Newer Older
1
/*--------
2 3 4 5 6
 * Module :			multibyte.c
 *
 * Description:		Mlutibyte related additional function.
 *
 *					Create 2001-03-03 Eiji Tokuya
7
 *--------
8
 */
9

10 11 12
#include <string.h>
#include "multibyte.h"

13 14
int			multibyte_client_encoding;	/* Multibyte Client Encoding. */
int			multibyte_status;	/* Multibyte Odds and ends character. */
15

16

17 18
unsigned char *
multibyte_strchr(unsigned char *s, unsigned char c)
19
{
20 21 22
	int			mb_st = 0,
				i = 0;

Hiroshi Inoue's avatar
Hiroshi Inoue committed
23
	while (!(mb_st == 0 && (s[i] == c || s[i] == 0)))
24 25 26
	{
		if (s[i] == 0)
			return (0);
27
		switch (multibyte_client_encoding)
28 29
		{
			case SJIS:
30 31 32 33
				{
					if (mb_st < 2 && s[i] > 0x80 && !(s[i] > 0x9f && s[i] < 0xe0))
						mb_st = 2;
					else if (mb_st == 2)
34 35 36
						mb_st = 1;
					else
						mb_st = 0;
37 38
				}
				break;
39 40

/* Chinese Big5 Support. */
41 42 43
			case BIG5:
				{
					if (mb_st < 2 && s[i] > 0xA0)
44
						mb_st = 2;
45
					else if (mb_st == 2)
46 47 48
						mb_st = 1;
					else
						mb_st = 0;
49 50 51
				}
				break;
			default:
52
				mb_st = 0;
53 54 55 56
		}
		i++;
	}
#ifdef _DEBUG
57
	qlog("i = %d\n", i);
58 59 60 61
#endif
	return (s + i);
}

62

63 64
void
multibyte_init(void)
65 66 67 68
{
	multibyte_status = 0;
}

69

70 71
unsigned char *
check_client_encoding(unsigned char *str)
72
{
73
	if (strstr(str, "%27SJIS%27") || strstr(str, "'SJIS'") || strstr(str, "'sjis'"))
74 75 76 77
	{
		multibyte_client_encoding = SJIS;
		return ("SJIS");
	}
78
	if (strstr(str, "%27BIG5%27") || strstr(str, "'BIG5'") || strstr(str, "'big5'"))
79 80 81 82 83 84 85
	{
		multibyte_client_encoding = BIG5;
		return ("BIG5");
	}
	return ("OHTER");
}

86 87

/*--------
88 89 90 91 92
 * Multibyte Status Function.
 *	Input	char
 *	Output	0	: 1 Byte Character.
 *			1	: MultibyteCharacter Last Byte.
 *			N	: MultibyteCharacter Fast or Middle Byte.
93
 *--------
94
 */
95 96
int
multibyte_char_check(unsigned char s)
97
{
98
	switch (multibyte_client_encoding)
99
	{
100
			/* Japanese Shift-JIS(CP932) Support. */
101 102 103 104 105 106 107 108 109 110 111
			case SJIS:
			{
				if (multibyte_status < 2 && s > 0x80 && !(s > 0x9f && s < 0xE0))
					multibyte_status = 2;
				else if (multibyte_status == 2)
					multibyte_status = 1;
				else
					multibyte_status = 0;
			}
			break;

112 113 114 115 116 117 118 119 120 121 122 123 124
			/* Chinese Big5(CP950) Support. */
			case BIG5:
				{
					if (multibyte_status < 2 && s > 0xA0)
						multibyte_status = 2;
					else if (multibyte_status == 2)
						multibyte_status = 1;
					else
						multibyte_status = 0;
				}
				break;
			default:
				multibyte_status = 0;
125 126
	}
#ifdef _DEBUG
127
	qlog("multibyte_client_encoding = %d   s = 0x%02X   multibyte_stat = %d\n", multibyte_client_encoding, s, multibyte_status);
128
#endif
129
	return (multibyte_status);
130
}