Subversion Repositories Kolibri OS

Rev

Rev 7085 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7085 Rev 9307
Line 1... Line 1...
1
#include 
1
typedef unsigned int size_t;
2
#include 
2
#define NULL ((void*)0)
3
#include 
-
 
Line 4... Line 3...
4
 
3
 
5
typedef int conv_t;
4
typedef int conv_t;
6
typedef unsigned int ucs4_t;
5
typedef unsigned int ucs4_t;
Line 23... Line 22...
23
#define CP1251 1
22
#define CP1251 1
24
#define CP1252 2
23
#define CP1252 2
25
#define KOI8_RU 3
24
#define KOI8_RU 3
26
#define ISO8859_5 4
25
#define ISO8859_5 4
27
#define UTF_8 5
26
#define UTF_8 5
28
#define KOI8_R 6
-
 
-
 
27
 
29
#define ISO8859_1 7
28
int strcmp (const char* a, const char* b) {
-
 
29
    return (*a && *b && (*a == *b)) ? ((*(a+1) || *(b+1)) ? (strcmp(a+1, b+1)) : (0)) : ((*a > *b) ? (1) : (-1));
-
 
30
}
Line 30... Line 31...
30
 
31
 
31
#include "cp866.h"
32
#include "cp866.h"
32
#include "cp1251.h"
33
#include "cp1251.h"
33
#include "cp1252.h"
-
 
34
#include "koi8_r.h"
34
#include "cp1252.h"
35
#include "koi8_ru.h"
-
 
36
#include "iso8859_1.h"
35
#include "koi8_ru.h"
37
#include "iso8859_5.h"
36
#include "iso8859_5.h"
Line 38... Line 37...
38
#include "utf8.h"
37
#include "utf8.h"
39
 
-
 
40
int encoding(const char *someencoding) {
-
 
41
 
-
 
42
	char *what = strdup(someencoding);
-
 
43
	/* Ignore //TRANSLIT or //IGNORE for now. */
-
 
44
	int i;
-
 
45
	for(i = 0; i < strlen(what); i++) {
-
 
46
	  if(what[i] == '/') {
-
 
47
		what[i] = '\0';
-
 
48
		break;
-
 
49
	  }
-
 
50
	}
38
 
51
 
39
int encoding(const char *what) {
52
	if (!strcasecmp(what,"CP866")) return CP866;
-
 
53
	if (!strcasecmp(what,"CP1251")) return CP1251;
40
    if (!strcmp(what,"CP866")) return CP866;
54
	if (!strcasecmp(what,"windows-1251")) return CP1251;
-
 
55
	if (!strcasecmp(what,"CP1252")) return CP1252;
-
 
56
	if (!strcasecmp(what,"windows-1252")) return CP1252;	
41
    if (!strcmp(what,"CP1251")) return CP1251;
57
	if (!strcasecmp(what,"KOI8-R")) return KOI8_R;	
-
 
58
	if (!strcasecmp(what,"KOI8-RU")) return KOI8_RU;
42
    if (!strcmp(what,"CP1252")) return CP1252;
59
	if (!strcasecmp(what,"ISO8859-1")) return ISO8859_1;	
43
    if (!strcmp(what,"KOI8-RU")) return KOI8_RU;
60
	if (!strcasecmp(what,"ISO8859-5")) return ISO8859_5;
44
    if (!strcmp(what,"ISO8859-5")) return ISO8859_5;
61
	if (!strcasecmp(what,"UTF-8")) return UTF_8;
45
    if (!strcmp(what,"UTF-8")) return UTF_8;
Line 62... Line 46...
62
	return -1;
46
    return -1;
63
}
47
}
64
 
-
 
65
iconv_t iconv_open(const char *tocode, const char *fromcode) {
48
 
66
	int to, from;
49
iconv_t iconv_open(const char *tocode, const char *fromcode) {
67
 
-
 
68
	if ((to=encoding(tocode))==-1) return -1;
50
    int to, from;
69
	if ((from=encoding(fromcode))==-1) return -1;
51
    if ((to=encoding(tocode))==-1) return -1;
70
 
-
 
71
	to=to<<16&0xFFFF0000;
52
    if ((from=encoding(fromcode))==-1) return -1;
72
	from=from&0xFFFF;
53
    to=to<<16&0xFFFF0000;
Line 73... Line -...
73
 
-
 
74
	return to+from;
-
 
75
}
-
 
76
 
-
 
77
int iconv_close(iconv_t icd)
-
 
78
{
54
    from=from&0xFFFF;
79
  return 0;
-
 
80
}
-
 
81
 
-
 
82
size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
55
    return to+from;
83
	char **outbuf, size_t *outbytesleft)
-
 
84
{
56
}
85
	int n, to, from;
57
 
86
	size_t count1,count2;
58
size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) {
Line -... Line 59...
-
 
59
    int n, to, from, count1,count2;
-
 
60
    int pwc, converted,written;
-
 
61
    int (*mbtowc)(conv_t, ucs4_t*, const unsigned char*, int);
87
	unsigned int pwc;
62
    int (*wctomb)(conv_t, unsigned char*, ucs4_t, int);
88
	int converted,written;
63
 
Line 89... Line 64...
89
	int (*mbtowc)(conv_t, ucs4_t *, const unsigned char *, int);
64
    char *str;
90
	int (*wctomb)(conv_t, unsigned char *, ucs4_t, int);
65
    str=*outbuf;
91
 
66
 
92
	to=cd>>16;
67
    from=cd>>16;
93
	from=cd&0xFFFF;
68
    to=cd&0xFFFF;
94
 
-
 
95
	switch (from)
69
 
96
	{
-
 
97
		case CP866: mbtowc=cp866_mbtowc; break;
70
    switch (from)
98
		case CP1251: mbtowc=cp1251_mbtowc; break;
71
    {
99
		case CP1252: mbtowc=cp1252_mbtowc; break;
72
        case CP866: mbtowc=cp866_mbtowc; break;
100
		case ISO8859_1: mbtowc=iso8859_1_mbtowc; break;		
73
        case CP1251: mbtowc=cp1251_mbtowc; break;
Line 101... Line 74...
101
		case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
74
        case CP1252: mbtowc=cp1252_mbtowc; break;
102
		case KOI8_R: mbtowc=koi8_r_mbtowc; break;		
75
        case ISO8859_5: mbtowc=iso8859_5_mbtowc; break;
103
		case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
76
        case KOI8_RU: mbtowc=koi8_ru_mbtowc; break;
104
		case UTF_8: mbtowc=utf8_mbtowc; break;
77
        case UTF_8: mbtowc=utf8_mbtowc; break;
105
		default: return (size_t)-1;
78
        default: return -2;
106
	}
-
 
107
 
79
    }
108
	switch (to)
-
 
109
	{
80
 
110
		case CP866: wctomb=cp866_wctomb; break;
81
    switch (to)
111
		case CP1251: wctomb=cp1251_wctomb; break;
82
    {
112
		case CP1252: wctomb=cp1252_wctomb; break;
83
        case CP866: wctomb=cp866_wctomb; break;
Line 113... Line 84...
113
		case ISO8859_1: wctomb=iso8859_1_wctomb; break;		
84
        case CP1251: wctomb=cp1251_wctomb; break;
114
		case ISO8859_5: wctomb=iso8859_5_wctomb; break;
85
        case CP1252: wctomb=cp1252_wctomb; break;
Line 115... Line -...
115
		case KOI8_R: wctomb=koi8_r_wctomb; break;		
-
 
116
		case KOI8_RU: wctomb=koi8_ru_wctomb; break;
-
 
117
		case UTF_8: wctomb=utf8_wctomb; break;
-
 
118
		default: return (size_t)-1;
86
        case ISO8859_5: wctomb=iso8859_5_wctomb; break;
119
	}
-
 
120
 
-
 
121
	count1=0;
-
 
122
	count2=0;
-
 
123
 
-
 
124
    /* Convert input multibyte char to wide character by using calls to mbtowc */
-
 
125
    /* Convert wide character to multibyte by calls to wctomb */
-
 
126
    /* Handle errors as we go on converting to be as standard compliant as possible */
-
 
127
    while(count1 < *inbytesleft) {
-
 
128
      unsigned char mbholder[] = { 0,0,0,0,0,0 };
-
 
129
 
-
 
130
      int numbytes = (mbtowc)(0, &pwc,((*inbuf)+count1), *inbytesleft - count1);
-
 
131
      if(numbytes < 0) {
-
 
132
        /* errno = EILSEQ if invalid multibyte sequence encountered in input */
-
 
133
        /* errno = EINVAL if input ends in the middle of a multibyte sequence */
-
 
134
 
87
        case KOI8_RU: wctomb=koi8_ru_wctomb; break;
135
        switch(numbytes) {
-
 
136
          case RET_TOOFEW(0):
-
 
137
            errno = EINVAL;
-
 
138
            break;
-
 
139
 
-
 
140
          case RET_ILSEQ:
-
 
141
            errno = EILSEQ;
88
        case UTF_8: wctomb=utf8_wctomb; break;
Line 142... Line -...
142
            break;
-
 
143
        }
-
 
144
 
-
 
145
        *inbytesleft -= count1;
-
 
146
        *outbytesleft -= count2;
-
 
147
        *inbuf += count1;
-
 
148
        *outbuf += count2;
-
 
149
        return (size_t) -1;
-
 
150
      }
-
 
151
 
89
        default: return -3;
152
      /* Convert from wide to multibyte storing result in mbholder and num converted in numbytes2 */
90
    }
153
      /* Pass the minimum amount of space we have, one from mbholder and one from remaining in outbuf */
91
 
154
      int minspace = sizeof(mbholder) <= (*outbytesleft - count2) ? sizeof(mbholder) : (*outbytesleft - count2);
92
    count1=0;
155
 
-
 
Line 156... Line -...
156
      int numbytes2 = (wctomb)(0, &mbholder[0], pwc, minspace);
-
 
157
      if(numbytes2 < 0) {
-
 
158
        switch(numbytes2) {
93
    count2=0;
159
          case RET_ILUNI:
94
 
Line 160... Line 95...
160
            errno = EILSEQ;
95
    while ( *inbytesleft>0 && *outbytesleft>1)
-
 
96
    {
-
 
97
        n=1;
161
            break;
98
 
Line 162... Line -...
162
          case RET_TOOSMALL:
-
 
163
            errno = E2BIG;
-
 
164
            break;
99
        do {
165
        }
-
 
Line -... Line 100...
-
 
100
        //converted= (utf8_mbtowc)(0,&pwc,((*inbuf)+count1),n);
-
 
101
        //    printf("%d\n",n);
166
 
102
        converted = (mbtowc)(0,&pwc,((*inbuf)+count1),n);
167
        *inbytesleft -= count1;
103
 
168
        *outbytesleft -= count2;
104
        n++;
-
 
105
        }    while (converted==RET_TOOFEW(0));
Line 169... Line -...
169
        *inbuf += count1;
-
 
170
        *outbuf += count2;
106
 
171
 
-
 
172
        return (size_t) -1;
-
 
173
      }
-
 
174
 
-
 
175
      int i;
107
        if (converted<0) return -10; 
176
      for(i = 0; i < numbytes2; i++) {
108
        //written=  (cp866_wctomb)(0,str+count2,pwc,1);
Line 177... Line -...
177
        *(*outbuf + count2 + i) = mbholder[i];
-
 
178
      }
-
 
179
 
-
 
180
      count1+=numbytes;
-
 
181
      count2+=numbytes2;
-
 
182
    }
-
 
183
 
-
 
184
    /* Successfully converted everything, update the variables and return number of bytes converted */
-
 
185
    *inbytesleft -= count1;
-
 
186
    *outbytesleft -= count2;
-
 
187
    *inbuf += count1;
-
 
188
    *outbuf += count2;
-
 
189
 
-
 
190
    return count1;
-
 
191
}
-
 
192
 
-
 
193
/* int main() */
-
 
194
/* { */
-
 
195
/* 	char *s;// ="вертолет"; */
-
 
196
/* 	char *z; */
-
 
197
/* 	//unsigned int pwc; */
-
 
198
/* 	iconv_t cd; */
-
 
199
/* 	size_t in, out; */
-
 
200
 
-
 
201
/*     FILE *infile; */
-
 
202
/*     char *fname = "file3.txt"; */
-
 
203
 
-
 
204
/*     size_t testmax = 100; */
-
 
205
/*     size_t test = 0; */
-
 
206
 
-
 
207
/*     infile = fopen(fname,"r"); */
-
 
208
 
-
 
209
/* 	fseek(infile, 0, SEEK_END); */
-
 
210
/* 	size_t file_size = ftell(infile); */
-
 
211
/* 	rewind(infile); */
-
 
212
 
-
 
213
/* 	char *buffer = (char*)malloc(file_size * sizeof(char)); */
-
 
214
/* 	if (buffer == NULL) */
-
 
215
/* 	{ */
-
 
216
/* 		fclose(infile); */
-
 
217
/* 		printf("Error allocating %d bytes.\n", file_size * sizeof(char)); */
-
 
218
/* 		return -1; */
-
 
219
/* 	} */
-
 
220
/* 	size_t bytes_read = fread(buffer, sizeof(char), file_size, infile); */
-
 
221
/* 	if (bytes_read != file_size) */
-
 
222
/* 	{ */
-
 
223
/* 		/\* printf("Have read only %d bytes of %d.\n", bytes_read, file_size); *\/ */
-
 
224
/* 		free(buffer); */
-
 
225
/* 		fclose(infile); */
-
 
226
/* 		return -1; */
-
 
227
/* 	} */
-
 
228
 
-
 
229
/* 	/\* in=strlen(buffer); *\/ */
-
 
230
/*     in = bytes_read; */
-
 
231
/* 	z=malloc(in+12000); */
-
 
232
 
-
 
233
/* 	out=in-1000; */
-
 
234
/* 	cd=iconv_open("UTF-8","UTF-8"); */
-
 
235
/* //	printf("%x\n",cd); */
109
        written = (wctomb)(0,str+count2,pwc,1);
236
/* 	int t; */
-
 
237
/*     char *zor = z; */
110
        if (written<0) written=0;//return -11; 
238
 
111
 
239
/*     /\* for(t = 0; t < 27400; t++) *\/ */
112
        //printf("Conv:%d Wri:%d In:%d Out:%d UTF:%x UCS:%x 866:%s\n",converted, written, *inbytesleft,*outbytesleft,*((*inbuf)+count1),pwc, str);
240
/*     /\*    printf("0x%x,", buffer[t]); *\/ */
-
 
241
 
-
 
242
/* 	t=iconv(cd, &buffer, &in, &z, &out); */
-
 
243
/* 	/\* printf("\nResult after iconv(): %d", t); *\/ */
-
 
244
 
-
 
245
/*     /\* for(t = 0; t < 24259; t++) *\/ */
113
 
246
/*     /\*   printf("%c", zor[t]); *\/ */
114
        (*inbytesleft)-=converted;
247
 
-
 
248
/*  	//for (;s
115
        (*outbytesleft)-=written;
249
/* } */
116
        count1+=converted;
250
 
117
        count2+=written;
251
/* typedef struct */
118
    }
252
/* { */
119
    *(str+count2)='\0';
253
/* 	char *name; */
120