Welcome to mirror list, hosted at ThFree Co, Russian Federation.

utfconv.c « utfconv « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9aeca36e55a1b6abe3921da7d9e950863087abfe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2009 Blender Foundation.
 * All rights reserved.
 * 
 * Contributor(s): Alexandr Kuznetsov, Andrea Weikert
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#include "utfconv.h"

size_t count_utf_8_from_16(wchar_t * string16)
{
	int i;
	size_t count = 0;
	wchar_t u = 0;
	if(!string16) return 0;

	for(i=0;u = string16[i];i++)
	{
		if(u < 0x0080) count+=1; else
		if(u < 0x0800) count+=2; else
		if(u < 0xD800) count+=3; else
		if(u < 0xDC00) {
			i++;
			if((u = string16[i])==0) break;
			if(u >= 0xDC00 && u < 0xE000)count+=4;
		} else
		if(u < 0xE000) /*illigal*/; else			
		count+=3;
	}

	return ++count;
}


size_t count_utf_16_from_8(char * string8)
{
		size_t count = 0;
		char u;
		char type = 0;
		unsigned int u32 = 0;

		if(!string8) return 0;

		for(;(u = *string8);string8++)
		{
			if(type==0)
			{
				if((u&0x01<<7) == 0)	 {count++; u32 = 0; continue;} 		//1 utf-8 char
				if((u&0x07<<5) == 0xC0)  {type=1; u32 = u & 0x1F; continue;} 	//2 utf-8 char
				if((u&0x0F<<4) == 0xE0)  {type=2; u32 = u & 0x0F; continue;} 	//3 utf-8 char
				if((u&0x1F<<3) == 0xF0)  {type=3; u32 = u & 0x07; continue;} 	//4 utf-8 char
					continue;
			} else 
			{
				if((u & 0xC0) == 0x80) {u32=(u32<<6) | (u&0x3F); type--;} else
				{u32 = 0; type = 0;};
			}
			if(type==0)
			{
				if((0 < u32 && u32 < 0xD800) || (0xE000 <= u32 && u32 < 0x10000)) count++; else
				if(0x10000 <= u32 && u32 < 0x110000) count+=2;
				u32 = 0;
			}

		}

	return ++count;
}




int conv_utf_16_to_8(wchar_t * in16, char * out8, size_t size8)
{
	char * out8end = out8+size8;
	wchar_t u = 0;
	int err = 0;
	if(!size8 || !in16 || !out8) return UTF_ERROR_NULL_IN;
	out8end--;

	for(; out8 < out8end && (u=*in16); in16++, out8++)
	{
		if(u < 0x0080) *out8 = u; else
		if(u < 0x0800) {
			if(out8 + 1 >= out8end) break;
			*out8++=(0x3<<6) | (0x1F & (u>>6));
			*out8  =(0x1<<7) | (0x3F & (u));		
		}else
		if(u < 0xD800 || u >= 0xE000) {
			if(out8 + 2 >= out8end) break;
			*out8++=(0x7<<5) | (0xF & (u>>12));
			*out8++=(0x1<<7) | (0x3F & (u>>6));;
			*out8  =(0x1<<7) | (0x3F & (u));
		}else
		if(u < 0xDC00) {
			wchar_t u2 = *++in16;

			if(!u2) break;
			if(u2 >= 0xDC00 && u2 < 0xE000)
			{
				if(out8 + 3 >= out8end) break; else { 
				unsigned int uc = 0x10000 + (u2 - 0xDC00) + ((u - 0xD800)<<10);

				*out8++=(0xF<<4) | (0x7 & (uc>>18));
				*out8++=(0x1<<7) | (0x3F & (uc>>12));
				*out8++=(0x1<<7) | (0x3F & (uc>>6));
				*out8  =(0x1<<7) | (0x3F & (uc));
				}
			} else {out8--; err|=UTF_ERROR_ILLCHAR;};
		} else
		if(u < 0xE000) {out8--; err|=UTF_ERROR_ILLCHAR;}


	}

	*out8=*out8end=0;

	if(*in16) err|=UTF_ERROR_SMALL;

	return err;
}


int conv_utf_8_to_16(char * in8, wchar_t * out16, size_t size16)
{
	char u;
	char type = 0;
	wchar_t u32 = 0;
	wchar_t * out16end = out16+size16;
	int err = 0;
	if(!size16 || !in8 || !out16) return UTF_ERROR_NULL_IN;
	out16end--;

	for(;out16<out16end && (u = *in8);in8++)
	{
		if(type==0)
		{
			if((u&0x01<<7) == 0)	 {*out16=u; out16++; u32 = 0; continue;} 		//1 utf-8 char
			if((u&0x07<<5) == 0xC0)  {type=1; u32 = u & 0x1F; continue;} 	//2 utf-8 char
			if((u&0x0F<<4) == 0xE0)  {type=2; u32 = u & 0x0F; continue;} 	//3 utf-8 char
			if((u&0x1F<<3) == 0xF0)  {type=3; u32 = u & 0x07; continue;} 	//4 utf-8 char
			err|=UTF_ERROR_ILLCHAR; continue;
		} else 
		{
			if((u & 0xC0) == 0x80) {u32=(u32<<6) | (u&0x3F); type--;} else
			{u32 = 0; type = 0; err|=UTF_ERROR_ILLSEQ;};
		}
		if(type==0)
		{
			if((0 < u32 && u32 < 0xD800) || (0xE000 <= u32 && u32 < 0x10000)) {*out16=u32; out16++;}else
			if(0x10000 <= u32 && u32 < 0x110000) {
				if(out16 + 1 >= out16end) break;
				u32-=0x10000;
				*out16 = 0xD800 + (u32 >> 10);
				out16++;
				*out16 = 0xDC00 + (u32 & 0x3FF);
				out16++;
			};
			u32 = 0;
		}

	}

	*out16=*out16end=0;

	if(*in8) err|=UTF_ERROR_SMALL;

	return err;
}

int is_ascii(char * in8)
{
	for(in8; *in8; in8++) 
		if(0x80 & *in8) return 0;

	return 1;
}

void utf_8_cut_end(char * inout8, size_t maxcutpoint)
{
	const char * start = inout8;
	char * cur = inout8 + maxcutpoint;
	char cc;
	if(!inout8) return;

	cc = *cur;


	
	
}



char * alloc_utf_8_from_16(wchar_t * in16, size_t add)
{
	size_t bsize = count_utf_8_from_16(in16);
	char * out8 = NULL;
	if(!bsize) return NULL;
	out8 = (char*)malloc(sizeof(char) * (bsize + add));
	conv_utf_16_to_8(in16,out8, bsize);
	return out8;
}

wchar_t * alloc_utf16_from_8(char * in8, size_t add)
{
	size_t bsize = count_utf_16_from_8(in8);
	wchar_t * out16 = NULL;
	if(!bsize) return NULL;
	out16 =(wchar_t*) malloc(sizeof(wchar_t) * (bsize + add));
	conv_utf_8_to_16(in8,out16, bsize);
	return out16;
}