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