Subversion Repositories Kolibri OS

Rev

Rev 3479 | Rev 6892 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5098 clevermous 1
//#include 
2
//#include 
3
typedef unsigned int size_t;
4
#define NULL ((void*)0)
3479 leency 5
 
6
typedef int conv_t;
7
typedef unsigned int ucs4_t;
8
 
9
typedef int iconv_t;
10
 
11
 
12
/* Return code if invalid input after a shift sequence of n bytes was read.
13
   (xxx_mbtowc) */
14
#define RET_SHIFT_ILSEQ(n)  (-1-2*(n))
15
/* Return code if invalid. (xxx_mbtowc) */
16
#define RET_ILSEQ           RET_SHIFT_ILSEQ(0)
17
/* Return code if only a shift sequence of n bytes was read. (xxx_mbtowc) */
18
#define RET_TOOFEW(n)       (-2-2*(n))
19
 
20
/* Return code if invalid. (xxx_wctomb) */
21
#define RET_ILUNI      -1
22
/* Return code if output buffer is too small. (xxx_wctomb, xxx_reset) */
23
#define RET_TOOSMALL   -2
24
 
25
 
26
#define CP866 0
27
#define CP1251 1
28
#define CP1252 2
29
#define KOI8_RU 3
30
#define ISO8859_5 4
31
#define UTF_8 5
32
 
33
 
34
int strcmp (const char* a, const char* b)
35
{
36
	return (*a && *b && (*a == *b)) ? ((*(a+1) || *(b+1)) ? (strcmp(a+1, b+1)) : (0)) : ((*a > *b) ? (1) : (-1));
37
}
38
 
39
 
40
 
41
 
42
#include "cp866.h"
43
#include "cp1251.h"
44
#include "cp1252.h"
45
#include "koi8_ru.h"
46
#include "iso8859_5.h"
47
#include "utf8.h"
48
 
49
 
50
int encoding(const char *what) {
51
 
52
	if (!strcmp(what,"CP866")) return CP866;
53
	if (!strcmp(what,"CP1251")) return CP1251;
54
	if (!strcmp(what,"CP1252")) return CP1252;
55
	if (!strcmp(what,"KOI8-RU")) return KOI8_RU;
56
	if (!strcmp(what,"ISO8859-5")) return ISO8859_5;
57
	if (!strcmp(what,"UTF-8")) return UTF_8;
58
	return -1;
59
 
60
}
61
 
62
 
63
iconv_t iconv_open(const char *tocode, const char *fromcode) {
64
	int to, from;
65
	if ((to=encoding(tocode))==-1) return -1;
66
	if ((from=encoding(fromcode))==-1) return -1;
67
	to=to<<16&0xFFFF0000;
68
	from=from&0xFFFF;
69
	return to+from;
70
 
71
}
72
 
73
size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
74
	char **outbuf, size_t *outbytesleft)
75
{
76
	int n, to, from, count1,count2;
77
	int pwc, converted,written;
78
	int (*mbtowc)(conv_t, ucs4_t *, const unsigned char *, int);
79
	int (*wctomb)(conv_t, ucs4_t *, const unsigned char *, int);
80
 
81
	char *str;
82
	str=*outbuf;
83
 
84
	from=cd>>16;
85
	to=cd&0xFFFF;
86
 
87
	switch (from)
88
	{
89
		case CP866: mbtowc=cp866_mbtowc; break;
90
		case CP1251: mbtowc=cp1251_mbtowc; break;
91
		case CP1252: mbtowc=cp1252_mbtowc; break;
92
		case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
93
		case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
94
		case UTF_8: mbtowc=utf8_mbtowc; break;
95
		default: return -2;
96
	}
97
 
98
	switch (to)
99
	{
100
		case CP866: wctomb=cp866_wctomb; break;
101
		case CP1251: wctomb=cp1251_wctomb; break;
102
		case CP1252: wctomb=cp1252_wctomb; break;
103
		case ISO8859_5: wctomb=iso8859_5_wctomb; break;
104
		case KOI8_RU: wctomb=koi8_ru_wctomb; break;
105
		case UTF_8: wctomb=utf8_wctomb; break;
106
		default: return -3;
107
	}
108
 
109
	count1=0;
110
	count2=0;
111
 
112
	while ( *inbytesleft>0 && *outbytesleft>1)
113
	{
114
		n=1;
115
 
116
		do {
117
		//converted= (utf8_mbtowc)(0,&pwc,((*inbuf)+count1),n);
118
		//	printf("%d\n",n);
119
		converted= (mbtowc)(0,&pwc,((*inbuf)+count1),n);
120
 
121
		n++;
122
		}	while (converted==RET_TOOFEW(0));
123
 
124
		if (converted<0) return -10;
125
		//written=  (cp866_wctomb)(0,str+count2,pwc,1);
126
		written=  (wctomb)(0,str+count2,pwc,1);
127
		if (written<0) written=0;//return -11;
128
 
129
		//printf("Conv:%d Wri:%d In:%d Out:%d UTF:%x UCS:%x 866:%s\n",converted, written, *inbytesleft,*outbytesleft,*((*inbuf)+count1),pwc, str);
130
 
131
		(*inbytesleft)-=converted;
132
		(*outbytesleft)-=written;
133
		count1+=converted;
134
		count2+=written;
135
	}
136
	*(str+count2)='\0';
137
 
138
	if (*inbytesleft>0 && *outbytesleft==0) return -12;
139
	return 0;
140
}
141
 
142
 
143
/*
144
int main()
145
{
146
	char *s;// ="вертолет";
147
	char *z;
148
	//unsigned int pwc;
149
	iconv_t cd;
150
	int in, out;
151
 
152
		FILE *infile;
153
		char *fname = "file.txt";
154
 
155
		infile = fopen(fname,"r");
156
 
157
	fseek(infile, 0, SEEK_END);
158
	size_t file_size = ftell(infile);
159
	rewind(infile);
160
 
161
	//printf ("LOL\n");
162
 
163
	char *buffer = (char*)malloc(file_size * sizeof(char));
164
	if (buffer == NULL)
165
	{
166
		fclose(infile);
167
		printf("Error allocating %d bytes.\n", file_size * sizeof(char));
168
		return -1;
169
	}
170
	size_t bytes_read = fread(buffer, sizeof(char), file_size, infile);
171
	if (bytes_read != file_size)
172
	{
173
		printf("Have read only %d bytes of %d.\n", bytes_read, file_size);
174
		free(buffer);
175
		fclose(infile);
176
		return -1;
177
	}
178
 
179
	in=strlen(buffer);
180
	z=malloc(in+1);
181
 
182
	out=in+1;
183
	cd=iconv_open("CP1251","CP866");
184
//	printf("%x\n",cd);
185
	int t;
186
	t=iconv(cd, &buffer, &in, &z, &out);
187
	printf("\nResult: %d", t);
188
	puts(z);
189
	//for (;s
190
}
191
*/
192
 
193
 
194
typedef struct
195
{
196
	char *name;
197
	void *f;
198
} export_t;
199
 
200
char szStart[]           = "START";
201
char szVersion[]         = "version";
202
char sziconv_open[]    = "iconv_open";
203
char sziconv[]   = "iconv";
204
 
205
export_t EXPORTS[] __asm__("EXPORTS") =
206
{
207
	{ szStart,       (void*)0x0 },
208
	{ szVersion,     (void*)0x00010001 },
209
	{ sziconv_open,  iconv_open    },
210
	{ sziconv,       iconv   },
211
	{ NULL,          NULL },
212
};