Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
7045 ashmew2 1
#include 
5098 clevermous 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
#include "cp866.h"
34
#include "cp1251.h"
35
#include "cp1252.h"
36
#include "koi8_ru.h"
37
#include "iso8859_5.h"
38
#include "utf8.h"
39
 
7005 ashmew2 40
int encoding(char *what) {
6892 ashmew2 41
 
7005 ashmew2 42
	/* Ignore //TRANSLIT or //IGNORE for now. */
43
	int i;
44
	for(i = 0; i < strlen(what); i++) {
45
	  if(what[i] == '/') {
46
		what[i] = '\0';
47
		break;
48
	  }
49
	}
50
 
7045 ashmew2 51
	if (!strcasecmp(what,"CP866")) return CP866;
52
	if (!strcasecmp(what,"CP1251")) return CP1251;
53
	if (!strcasecmp(what,"windows-1252")) return CP1252;
54
	if (!strcasecmp(what,"CP1252")) return CP1252;
55
	if (!strcasecmp(what,"KOI8-RU")) return KOI8_RU;
56
	if (!strcasecmp(what,"ISO8859-5")) return ISO8859_5;
57
	if (!strcasecmp(what,"UTF-8")) return UTF_8;
3479 leency 58
	return -1;
59
}
60
 
61
 
62
iconv_t iconv_open(const char *tocode, const char *fromcode) {
63
	int to, from;
7044 ashmew2 64
 
3479 leency 65
	if ((to=encoding(tocode))==-1) return -1;
66
	if ((from=encoding(fromcode))==-1) return -1;
7044 ashmew2 67
 
3479 leency 68
	to=to<<16&0xFFFF0000;
69
	from=from&0xFFFF;
70
	return to+from;
71
}
72
 
6882 ashmew2 73
int iconv_close(iconv_t icd)
74
{
75
  return 0;
76
}
77
 
3479 leency 78
size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
79
	char **outbuf, size_t *outbytesleft)
80
{
81
	int n, to, from, count1,count2;
82
	int pwc, converted,written;
83
	int (*mbtowc)(conv_t, ucs4_t *, const unsigned char *, int);
84
	int (*wctomb)(conv_t, ucs4_t *, const unsigned char *, int);
85
 
86
	char *str;
87
	str=*outbuf;
88
 
89
	from=cd>>16;
90
	to=cd&0xFFFF;
91
 
92
	switch (from)
93
	{
94
		case CP866: mbtowc=cp866_mbtowc; break;
95
		case CP1251: mbtowc=cp1251_mbtowc; break;
96
		case CP1252: mbtowc=cp1252_mbtowc; break;
97
		case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
98
		case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
99
		case UTF_8: mbtowc=utf8_mbtowc; break;
100
		default: return -2;
101
	}
102
 
103
	switch (to)
104
	{
105
		case CP866: wctomb=cp866_wctomb; break;
106
		case CP1251: wctomb=cp1251_wctomb; break;
107
		case CP1252: wctomb=cp1252_wctomb; break;
108
		case ISO8859_5: wctomb=iso8859_5_wctomb; break;
109
		case KOI8_RU: wctomb=koi8_ru_wctomb; break;
110
		case UTF_8: wctomb=utf8_wctomb; break;
111
		default: return -3;
112
	}
6894 ashmew2 113
 
114
    if(from == to) {
115
      int oc=0,ic=0;
116
 
117
      while(*inbytesleft > 0 && *outbytesleft > 0) {
118
        str[oc]=(*inbuf)[ic];
119
        ++ic;
120
        ++oc;
121
        (*inbytesleft)--;
122
        (*outbytesleft)--;
7007 ashmew2 123
        (*outbuf)++;
6894 ashmew2 124
      }
125
 
126
      return 0;
127
    }
3479 leency 128
 
129
	count1=0;
130
	count2=0;
131
 
132
	while ( *inbytesleft>0 && *outbytesleft>1)
133
	{
134
		n=1;
135
 
136
		do {
137
		//converted= (utf8_mbtowc)(0,&pwc,((*inbuf)+count1),n);
138
		//	printf("%d\n",n);
139
		converted= (mbtowc)(0,&pwc,((*inbuf)+count1),n);
140
 
141
		n++;
142
		}	while (converted==RET_TOOFEW(0));
143
 
144
		if (converted<0) return -10;
145
		//written=  (cp866_wctomb)(0,str+count2,pwc,1);
146
		written=  (wctomb)(0,str+count2,pwc,1);
147
		if (written<0) written=0;//return -11;
148
 
149
		//printf("Conv:%d Wri:%d In:%d Out:%d UTF:%x UCS:%x 866:%s\n",converted, written, *inbytesleft,*outbytesleft,*((*inbuf)+count1),pwc, str);
150
 
151
		(*inbytesleft)-=converted;
152
		(*outbytesleft)-=written;
7044 ashmew2 153
		(*outbuf)+=written;
3479 leency 154
		count1+=converted;
155
		count2+=written;
156
	}
157
	*(str+count2)='\0';
158
 
159
	if (*inbytesleft>0 && *outbytesleft==0) return -12;
160
	return 0;
161
}
162
 
163
 
164
/*
165
int main()
166
{
167
	char *s;// ="вертолет";
168
	char *z;
169
	//unsigned int pwc;
170
	iconv_t cd;
171
	int in, out;
172
 
173
		FILE *infile;
174
		char *fname = "file.txt";
175
 
176
		infile = fopen(fname,"r");
177
 
178
	fseek(infile, 0, SEEK_END);
179
	size_t file_size = ftell(infile);
180
	rewind(infile);
181
 
182
	//printf ("LOL\n");
183
 
184
	char *buffer = (char*)malloc(file_size * sizeof(char));
185
	if (buffer == NULL)
186
	{
187
		fclose(infile);
188
		printf("Error allocating %d bytes.\n", file_size * sizeof(char));
189
		return -1;
190
	}
191
	size_t bytes_read = fread(buffer, sizeof(char), file_size, infile);
192
	if (bytes_read != file_size)
193
	{
194
		printf("Have read only %d bytes of %d.\n", bytes_read, file_size);
195
		free(buffer);
196
		fclose(infile);
197
		return -1;
198
	}
199
 
200
	in=strlen(buffer);
201
	z=malloc(in+1);
202
 
203
	out=in+1;
204
	cd=iconv_open("CP1251","CP866");
205
//	printf("%x\n",cd);
206
	int t;
207
	t=iconv(cd, &buffer, &in, &z, &out);
208
	printf("\nResult: %d", t);
209
	puts(z);
210
	//for (;s
211
}
212
*/
213
 
214
 
215
typedef struct
216
{
217
	char *name;
218
	void *f;
219
} export_t;
220
 
221
char szStart[]           = "START";
222
char szVersion[]         = "version";
223
char sziconv_open[]    = "iconv_open";
224
char sziconv[]   = "iconv";
225
 
226
export_t EXPORTS[] __asm__("EXPORTS") =
227
{
228
	{ szStart,       (void*)0x0 },
229
	{ szVersion,     (void*)0x00010001 },
230
	{ sziconv_open,  iconv_open    },
231
	{ sziconv,       iconv   },
232
	{ NULL,          NULL },
233
};