7
|
1 /* vi:set ts=8 sts=4 sw=4:
|
|
2 *
|
|
3 * VIM - Vi IMproved by Bram Moolenaar
|
|
4 *
|
|
5 * Do ":help uganda" in Vim to read copying and usage conditions.
|
|
6 * Do ":help credits" in Vim to see a list of people who contributed.
|
|
7 * See README.txt for an overview of the Vim source code.
|
|
8 */
|
|
9
|
|
10 /*
|
|
11 * os_macosx.c -- election of os_mac.c or os_unix.c
|
|
12 *
|
|
13 */
|
|
14
|
|
15 #ifdef MACOS_X_UNIX
|
|
16 # ifdef HAVE_CONFIG_H /* Using Makefile. */
|
|
17 # include "vim.h"
|
|
18 # else
|
|
19 # include "os_unix.c" /* Using Project Builder */
|
|
20 # endif
|
|
21 #else
|
|
22 # include "os_mac.c"
|
|
23 # include <TextEncodingConverter.h>
|
|
24 #endif
|
|
25
|
|
26 #ifdef _DEBUG
|
|
27 void
|
|
28 Trace(char* fmt, ...)
|
|
29 {
|
|
30 char buf[2048];
|
|
31 va_list args;
|
|
32
|
|
33 va_start(args, fmt);
|
|
34 /* vsnprintf(buf, sizeof(buf), fmt, args);*/
|
|
35 fprintf(stderr, "%s", buf);
|
|
36 va_end(args);
|
|
37 }
|
|
38 #endif
|
|
39
|
|
40 #ifdef MACOS_X_ICONVEMU
|
|
41 /*
|
|
42 * Libiconv emulation layer
|
|
43 */
|
|
44
|
|
45 struct _iconv_t
|
|
46 {
|
|
47 TECObjectRef tec;
|
|
48 TECObjectRef tecReverse;
|
|
49 TECSnifferObjectRef sniff;
|
|
50 TextEncoding from;
|
|
51 TextEncoding to;
|
|
52 };
|
|
53 /* typedef struct _iconv_t *iconv_t; */
|
|
54
|
|
55
|
|
56 static int last_errno = 0;
|
|
57
|
|
58 /*
|
|
59 * Get TextEncoding from iconv's encoding name
|
|
60 */
|
|
61 static TextEncoding
|
|
62 get_textencoding(const char* encodename)
|
|
63 {
|
|
64 static struct {
|
|
65 const char* name;
|
|
66 TextEncoding encode;
|
|
67 } encodetable[] = {
|
|
68 /* ISO-8859 encodings family */
|
|
69 {"latin1", kTextEncodingISOLatin1},
|
|
70 {"latin2", kTextEncodingISOLatin2},
|
|
71 {"latin3", kTextEncodingISOLatin3},
|
|
72 {"latin4", kTextEncodingISOLatin4},
|
|
73 {"latin5", kTextEncodingISOLatin5},
|
|
74 {"latin6", kTextEncodingISOLatin6},
|
|
75 {"latin7", kTextEncodingISOLatin7},
|
|
76 {"latin8", kTextEncodingISOLatin8},
|
|
77 {"latin9", kTextEncodingISOLatin9},
|
|
78 {"iso-8859-1", kTextEncodingISOLatin1},
|
|
79 {"iso-8859-2", kTextEncodingISOLatin2},
|
|
80 {"iso-8859-3", kTextEncodingISOLatin3},
|
|
81 {"iso-8859-4", kTextEncodingISOLatin4},
|
|
82 {"iso-8859-5", kTextEncodingISOLatinCyrillic},
|
|
83 {"iso-8859-6", kTextEncodingISOLatinArabic},
|
|
84 {"iso-8859-7", kTextEncodingISOLatinGreek},
|
|
85 {"iso-8859-8", kTextEncodingISOLatinHebrew},
|
|
86 {"iso-8859-9", kTextEncodingISOLatin5},
|
|
87 {"iso-8859-10", kTextEncodingISOLatin6},
|
|
88
|
|
89 /* Unicode encodings. */
|
|
90 /* TODO: Add other type of unicode */
|
|
91 {"ucs-2", kTextEncodingMacUnicode},
|
|
92
|
|
93 /* Japanese encoding aliases */
|
|
94 {"cp932", kTextEncodingShiftJIS},
|
|
95 {"shift-jis", kTextEncodingShiftJIS},
|
|
96 {"euc-jp", kTextEncodingEUC_JP},
|
|
97 {"iso-2022-jp", kTextEncodingISO_2022_JP},
|
|
98 {"iso-2022-jp-1", kTextEncodingISO_2022_JP_1},
|
|
99 {"iso-2022-jp-2", kTextEncodingISO_2022_JP_2},
|
|
100 {"iso-2022-jp-3", kTextEncodingISO_2022_JP_3},
|
|
101
|
|
102 /* Other aliases. These aliases in this block are just guessed. */
|
|
103 /* TODO: Must be verified. */
|
|
104 {"gb2312", kTextEncodingGB_2312_80},
|
|
105 {"cp936", kTextEncodingMacChineseSimp},
|
|
106 {"euc-cn", kTextEncodingEUC_CN},
|
|
107 {"cp950", kTextEncodingMacChineseTrad},
|
|
108 {"euc-tw", kTextEncodingEUC_TW},
|
|
109 {"cp949", kTextEncodingMacKorean},
|
|
110 {"euc-kr", kTextEncodingEUC_KR},
|
|
111
|
|
112 /*
|
|
113 * All encodings supported by Macintosh. You can find these values
|
|
114 * in a file:
|
|
115 * /System/Library/Frameworks/CoreServices.framework/Versions/A/
|
|
116 * Frameworks/CarbonCore.framework/Versions/A/Headers/TextCommon.h
|
|
117 */
|
|
118 {"MacRoman", kTextEncodingMacRoman},
|
|
119 {"MacJapanese", kTextEncodingMacJapanese},
|
|
120 {"MacChineseTrad", kTextEncodingMacChineseTrad},
|
|
121 {"MacKorean", kTextEncodingMacKorean},
|
|
122 {"MacArabic", kTextEncodingMacArabic},
|
|
123 {"MacHebrew", kTextEncodingMacHebrew},
|
|
124 {"MacGreek", kTextEncodingMacGreek},
|
|
125 {"MacCyrillic", kTextEncodingMacCyrillic},
|
|
126 {"MacDevanagari", kTextEncodingMacDevanagari},
|
|
127 {"MacGurmukhi", kTextEncodingMacGurmukhi},
|
|
128 {"MacGujarati", kTextEncodingMacGujarati},
|
|
129 {"MacOriya", kTextEncodingMacOriya},
|
|
130 {"MacBengali", kTextEncodingMacBengali},
|
|
131 {"MacTamil", kTextEncodingMacTamil},
|
|
132 {"MacTelugu", kTextEncodingMacTelugu},
|
|
133 {"MacKannada", kTextEncodingMacKannada},
|
|
134 {"MacMalayalam", kTextEncodingMacMalayalam},
|
|
135 {"MacSinhalese", kTextEncodingMacSinhalese},
|
|
136 {"MacBurmese", kTextEncodingMacBurmese},
|
|
137 {"MacKhmer", kTextEncodingMacKhmer},
|
|
138 {"MacThai", kTextEncodingMacThai},
|
|
139 {"MacLaotian", kTextEncodingMacLaotian},
|
|
140 {"MacGeorgian", kTextEncodingMacGeorgian},
|
|
141 {"MacArmenian", kTextEncodingMacArmenian},
|
|
142 {"MacChineseSimp", kTextEncodingMacChineseSimp},
|
|
143 {"MacTibetan", kTextEncodingMacTibetan},
|
|
144 {"MacMongolian", kTextEncodingMacMongolian},
|
|
145 {"MacEthiopic", kTextEncodingMacEthiopic},
|
|
146 {"MacCentralEurRoman", kTextEncodingMacCentralEurRoman},
|
|
147 {"MacVietnamese", kTextEncodingMacVietnamese},
|
|
148 {"MacExtArabic", kTextEncodingMacExtArabic},
|
|
149 {"MacSymbol", kTextEncodingMacSymbol},
|
|
150 {"MacDingbats", kTextEncodingMacDingbats},
|
|
151 {"MacTurkish", kTextEncodingMacTurkish},
|
|
152 {"MacCroatian", kTextEncodingMacCroatian},
|
|
153 {"MacIcelandic", kTextEncodingMacIcelandic},
|
|
154 {"MacRomanian", kTextEncodingMacRomanian},
|
|
155 {"MacCeltic", kTextEncodingMacCeltic},
|
|
156 {"MacGaelic", kTextEncodingMacGaelic},
|
|
157 {"MacKeyboardGlyphs", kTextEncodingMacKeyboardGlyphs},
|
|
158 {"MacTradChinese", kTextEncodingMacTradChinese},
|
|
159 {"MacRSymbol", kTextEncodingMacRSymbol},
|
|
160 {"MacSimpChinese", kTextEncodingMacSimpChinese},
|
|
161 {"MacGeez", kTextEncodingMacGeez},
|
|
162 {"MacEastEurRoman", kTextEncodingMacEastEurRoman},
|
|
163 {"MacUninterp", kTextEncodingMacUninterp},
|
|
164 {"MacUnicode", kTextEncodingMacUnicode},
|
|
165 {"MacFarsi", kTextEncodingMacFarsi},
|
|
166 {"MacUkrainian", kTextEncodingMacUkrainian},
|
|
167 {"MacInuit", kTextEncodingMacInuit},
|
|
168 {"MacVT100", kTextEncodingMacVT100},
|
|
169 {"MacHFS", kTextEncodingMacHFS},
|
|
170 {"UnicodeDefault", kTextEncodingUnicodeDefault},
|
|
171 {"UnicodeV1_1", kTextEncodingUnicodeV1_1},
|
|
172 {"ISO10646_1993", kTextEncodingISO10646_1993},
|
|
173 {"UnicodeV2_0", kTextEncodingUnicodeV2_0},
|
|
174 {"UnicodeV2_1", kTextEncodingUnicodeV2_1},
|
|
175 {"UnicodeV3_0", kTextEncodingUnicodeV3_0},
|
|
176 {"UnicodeV3_1", kTextEncodingUnicodeV3_1},
|
|
177 {"UnicodeV3_2", kTextEncodingUnicodeV3_2},
|
|
178 {"ISOLatin1", kTextEncodingISOLatin1},
|
|
179 {"ISOLatin2", kTextEncodingISOLatin2},
|
|
180 {"ISOLatin3", kTextEncodingISOLatin3},
|
|
181 {"ISOLatin4", kTextEncodingISOLatin4},
|
|
182 {"ISOLatinCyrillic", kTextEncodingISOLatinCyrillic},
|
|
183 {"ISOLatinArabic", kTextEncodingISOLatinArabic},
|
|
184 {"ISOLatinGreek", kTextEncodingISOLatinGreek},
|
|
185 {"ISOLatinHebrew", kTextEncodingISOLatinHebrew},
|
|
186 {"ISOLatin5", kTextEncodingISOLatin5},
|
|
187 {"ISOLatin6", kTextEncodingISOLatin6},
|
|
188 {"ISOLatin7", kTextEncodingISOLatin7},
|
|
189 {"ISOLatin8", kTextEncodingISOLatin8},
|
|
190 {"ISOLatin9", kTextEncodingISOLatin9},
|
|
191 {"DOSLatinUS", kTextEncodingDOSLatinUS},
|
|
192 {"DOSGreek", kTextEncodingDOSGreek},
|
|
193 {"DOSBalticRim", kTextEncodingDOSBalticRim},
|
|
194 {"DOSLatin1", kTextEncodingDOSLatin1},
|
|
195 {"DOSGreek1", kTextEncodingDOSGreek1},
|
|
196 {"DOSLatin2", kTextEncodingDOSLatin2},
|
|
197 {"DOSCyrillic", kTextEncodingDOSCyrillic},
|
|
198 {"DOSTurkish", kTextEncodingDOSTurkish},
|
|
199 {"DOSPortuguese", kTextEncodingDOSPortuguese},
|
|
200 {"DOSIcelandic", kTextEncodingDOSIcelandic},
|
|
201 {"DOSHebrew", kTextEncodingDOSHebrew},
|
|
202 {"DOSCanadianFrench", kTextEncodingDOSCanadianFrench},
|
|
203 {"DOSArabic", kTextEncodingDOSArabic},
|
|
204 {"DOSNordic", kTextEncodingDOSNordic},
|
|
205 {"DOSRussian", kTextEncodingDOSRussian},
|
|
206 {"DOSGreek2", kTextEncodingDOSGreek2},
|
|
207 {"DOSThai", kTextEncodingDOSThai},
|
|
208 {"DOSJapanese", kTextEncodingDOSJapanese},
|
|
209 {"DOSChineseSimplif", kTextEncodingDOSChineseSimplif},
|
|
210 {"DOSKorean", kTextEncodingDOSKorean},
|
|
211 {"DOSChineseTrad", kTextEncodingDOSChineseTrad},
|
|
212 {"WindowsLatin1", kTextEncodingWindowsLatin1},
|
|
213 {"WindowsANSI", kTextEncodingWindowsANSI},
|
|
214 {"WindowsLatin2", kTextEncodingWindowsLatin2},
|
|
215 {"WindowsCyrillic", kTextEncodingWindowsCyrillic},
|
|
216 {"WindowsGreek", kTextEncodingWindowsGreek},
|
|
217 {"WindowsLatin5", kTextEncodingWindowsLatin5},
|
|
218 {"WindowsHebrew", kTextEncodingWindowsHebrew},
|
|
219 {"WindowsArabic", kTextEncodingWindowsArabic},
|
|
220 {"WindowsBalticRim", kTextEncodingWindowsBalticRim},
|
|
221 {"WindowsVietnamese", kTextEncodingWindowsVietnamese},
|
|
222 {"WindowsKoreanJohab", kTextEncodingWindowsKoreanJohab},
|
|
223 {"US_ASCII", kTextEncodingUS_ASCII},
|
|
224 {"JIS_X0201_76", kTextEncodingJIS_X0201_76},
|
|
225 {"JIS_X0208_83", kTextEncodingJIS_X0208_83},
|
|
226 {"JIS_X0208_90", kTextEncodingJIS_X0208_90},
|
|
227 {"JIS_X0212_90", kTextEncodingJIS_X0212_90},
|
|
228 {"JIS_C6226_78", kTextEncodingJIS_C6226_78},
|
|
229 {"ShiftJIS_X0213_00", kTextEncodingShiftJIS_X0213_00},
|
|
230 {"GB_2312_80", kTextEncodingGB_2312_80},
|
|
231 {"GBK_95", kTextEncodingGBK_95},
|
|
232 {"GB_18030_2000", kTextEncodingGB_18030_2000},
|
|
233 {"KSC_5601_87", kTextEncodingKSC_5601_87},
|
|
234 {"KSC_5601_92_Johab", kTextEncodingKSC_5601_92_Johab},
|
|
235 {"CNS_11643_92_P1", kTextEncodingCNS_11643_92_P1},
|
|
236 {"CNS_11643_92_P2", kTextEncodingCNS_11643_92_P2},
|
|
237 {"CNS_11643_92_P3", kTextEncodingCNS_11643_92_P3},
|
|
238 {"ISO_2022_JP", kTextEncodingISO_2022_JP},
|
|
239 {"ISO_2022_JP_2", kTextEncodingISO_2022_JP_2},
|
|
240 {"ISO_2022_JP_1", kTextEncodingISO_2022_JP_1},
|
|
241 {"ISO_2022_JP_3", kTextEncodingISO_2022_JP_3},
|
|
242 {"ISO_2022_CN", kTextEncodingISO_2022_CN},
|
|
243 {"ISO_2022_CN_EXT", kTextEncodingISO_2022_CN_EXT},
|
|
244 {"ISO_2022_KR", kTextEncodingISO_2022_KR},
|
|
245 {"EUC_JP", kTextEncodingEUC_JP},
|
|
246 {"EUC_CN", kTextEncodingEUC_CN},
|
|
247 {"EUC_TW", kTextEncodingEUC_TW},
|
|
248 {"EUC_KR", kTextEncodingEUC_KR},
|
|
249 {"ShiftJIS", kTextEncodingShiftJIS},
|
|
250 {"KOI8_R", kTextEncodingKOI8_R},
|
|
251 {"Big5", kTextEncodingBig5},
|
|
252 {"MacRomanLatin1", kTextEncodingMacRomanLatin1},
|
|
253 {"HZ_GB_2312", kTextEncodingHZ_GB_2312},
|
|
254 {"Big5_HKSCS_1999", kTextEncodingBig5_HKSCS_1999},
|
|
255 {"NextStepLatin", kTextEncodingNextStepLatin},
|
|
256 {"EBCDIC_US", kTextEncodingEBCDIC_US},
|
|
257 {"EBCDIC_CP037", kTextEncodingEBCDIC_CP037},
|
|
258 {"MultiRun", kTextEncodingMultiRun},
|
|
259
|
|
260 /* Terminator */
|
|
261 {NULL, -1},
|
|
262 };
|
|
263 int i;
|
|
264
|
|
265 i = 0;
|
|
266 for (i = 0; encodetable[i].name != NULL; ++i)
|
|
267 {
|
|
268 if (STRICMP(encodename, encodetable[i].name) == 0)
|
|
269 break;
|
|
270 }
|
|
271 return encodetable[i].encode;
|
|
272 }
|
|
273
|
|
274 /*
|
|
275 * iconv interfaces
|
|
276 */
|
|
277
|
|
278 iconv_t
|
|
279 iconv_open(const char* tocode, const char* fromcode)
|
|
280 {
|
|
281 TextEncoding toEnc, fromEnc;
|
|
282 iconv_t cd = NULL;
|
|
283 OSStatus st;
|
|
284
|
|
285 /* Verify to/from encoding name */
|
|
286 toEnc = get_textencoding(tocode);
|
|
287 fromEnc = get_textencoding(fromcode);
|
|
288 if (toEnc < 0 || fromEnc < 0)
|
|
289 goto ICONV_OPEN_ERR;
|
|
290
|
|
291 /* Allocate memory to object */
|
|
292 cd = (iconv_t)alloc(sizeof(struct _iconv_t));
|
|
293 if (!cd)
|
|
294 goto ICONV_OPEN_ERR;
|
|
295 memset(cd, 0, sizeof(struct _iconv_t));
|
|
296
|
|
297 /* Create converter */
|
|
298 if (fromEnc != toEnc)
|
|
299 {
|
|
300 TRACE("*** fromEnc=%d toEnc=%d\n", (int)fromEnc, (int)toEnc);
|
|
301 st = TECCreateConverter(&cd->tec, fromEnc, toEnc);
|
|
302 if (st != 0)
|
|
303 {
|
|
304 TRACE("*** TECCreateConverter()=%d\n", (int)st);
|
|
305 goto ICONV_OPEN_ERR;
|
|
306 }
|
|
307 /* Create reverse converter */
|
|
308 st = TECCreateConverter(&cd->tecReverse, toEnc, fromEnc);
|
|
309 if (st != 0)
|
|
310 {
|
|
311 TRACE("*** TECCreateConverter()=%d (reverse)\n", (int)st);
|
|
312 goto ICONV_OPEN_ERR;
|
|
313 }
|
|
314 /* Create Sniffer */
|
|
315 st = TECCreateSniffer(&cd->sniff, &fromEnc, 1);
|
|
316 if (st != 0)
|
|
317 {
|
|
318 TRACE("*** TECCreateSniffer()=%d\n", (int)st);
|
|
319 goto ICONV_OPEN_ERR;
|
|
320 }
|
|
321 }
|
|
322
|
|
323 cd->from = fromEnc;
|
|
324 cd->to = toEnc;
|
|
325 last_errno = 0;
|
|
326 return cd;
|
|
327
|
|
328 ICONV_OPEN_ERR:
|
|
329 if (cd)
|
|
330 iconv_close(cd);
|
|
331 last_errno = EINVAL;
|
|
332 return (iconv_t)-1;
|
|
333 }
|
|
334
|
|
335 /*
|
|
336 * Used when there are same value in 'from encoding' and 'to encoding'.
|
|
337 * TEC doesn't support conversion between same encodings, and
|
|
338 * TECCreateConverter() failed.
|
|
339 */
|
|
340 static size_t
|
18
|
341 null_conv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
|
|
342 char **outbuf, size_t *outbytesleft)
|
7
|
343 {
|
|
344 const char* buf_in = inbuf && *inbuf ? *inbuf : NULL;
|
|
345 char* buf_out = outbuf && *outbuf ? *outbuf : NULL;
|
|
346
|
|
347 if (buf_in)
|
|
348 {
|
|
349 int in_len = inbytesleft ? *inbytesleft : 0;
|
|
350 int out_len = outbytesleft ? *outbytesleft : 0;
|
|
351
|
|
352 if (!buf_out || out_len <= 0)
|
|
353 {
|
|
354 last_errno = E2BIG;
|
|
355 return -1;
|
|
356 }
|
|
357 else if (in_len > 0)
|
|
358 {
|
|
359 int len = in_len < out_len ? in_len : out_len;
|
|
360
|
|
361 memcpy (buf_out, buf_in, len);
|
|
362 *inbuf += len;
|
|
363 *outbuf += len;
|
|
364 *inbytesleft -= len;
|
|
365 *outbytesleft -= len;
|
|
366 if (*outbytesleft <= 0)
|
|
367 {
|
|
368 last_errno = E2BIG;
|
|
369 return -1;
|
|
370 }
|
|
371 }
|
|
372 }
|
|
373 last_errno = 0;
|
|
374 return 0;
|
|
375 }
|
|
376
|
|
377 size_t
|
18
|
378 iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft,
|
|
379 char **outbuf, size_t *outbytesleft)
|
7
|
380 {
|
18
|
381 ConstTextPtr buf_in;
|
|
382 TextPtr buf_out;
|
|
383 ByteCount out_len, out_true;
|
|
384 ByteCount in_len, in_true;
|
|
385 OSStatus st;
|
7
|
386
|
|
387 if (!cd)
|
|
388 {
|
|
389 last_errno = ENOENT; /* TODO: Another error code should be set */
|
|
390 return -1;
|
|
391 }
|
|
392 if (cd->from == cd->to)
|
|
393 return null_conv(cd, inbuf, inbytesleft, outbuf, outbytesleft) ;
|
|
394
|
|
395 buf_in = (TextPtr) inbuf ;
|
|
396 buf_out = (TextPtr) outbuf ;
|
|
397 out_len = out_true = -1;
|
|
398 in_len = in_true = -1;
|
|
399
|
|
400 if (buf_in && buf_out)
|
|
401 {
|
|
402 ItemCount error, feature;
|
|
403
|
|
404 /* Normal convert mode */
|
|
405 if (!inbytesleft || !outbytesleft)
|
|
406 {
|
|
407 last_errno = EFAULT;
|
|
408 return -1;
|
|
409 }
|
|
410 in_len = *inbytesleft;
|
|
411 out_len = *outbytesleft;
|
|
412
|
|
413 /* Check stream is form in expected encoding or not */
|
|
414 st = TECSniffTextEncoding(cd->sniff, (TextPtr)buf_in, in_len,
|
|
415 &cd->from, 1, &error, 1, &feature, 1);
|
|
416 TRACE("TECSniffTextEncoding()=%d error=%d feature=%d\n",
|
|
417 (int)st, (int)error, (int)feature);
|
|
418 if ((error != 0 || feature == 0)
|
|
419 && !(error == 0xffffffff && feature == 0xffffffff))
|
|
420 /* Not expected encoding */
|
|
421 st = kTECUnmappableElementErr;
|
|
422 else
|
|
423 {
|
|
424 /* Do convert */
|
|
425 st = TECConvertText(cd->tec,
|
|
426 buf_in, in_len, &in_true,
|
|
427 buf_out, out_len, &out_true);
|
|
428 /* Verify converted text. Compare original text with reverse
|
|
429 * converted text. If not match, there is some problem on
|
|
430 * converting. */
|
|
431 if (st == 0 && in_true > 0)
|
|
432 {
|
|
433 ByteCount rev_in, rev_out;
|
|
434 TextPtr buf_rev = (TextPtr)alloc(in_true);
|
|
435
|
|
436 if (buf_rev)
|
|
437 {
|
|
438 st = TECConvertText(cd->tecReverse,
|
|
439 buf_out, out_true, &rev_in,
|
|
440 buf_rev, in_true, &rev_out);
|
|
441 if (st != 0 || rev_in != out_true || rev_out != in_true
|
|
442 || memcmp(buf_rev, buf_in, rev_out) != 0)
|
|
443 {
|
|
444 #ifdef ICONVOSX_DEBUG
|
|
445 fprintf(stderr, " reverse conversion failed.\n");
|
|
446 #endif
|
|
447 st = kTECUnmappableElementErr;
|
|
448 }
|
|
449 vim_free(buf_rev);
|
|
450 }
|
|
451 else
|
|
452 st = kTECUnmappableElementErr;
|
|
453 }
|
|
454 }
|
|
455 }
|
|
456 else if (!buf_in && buf_out)
|
|
457 {
|
|
458 /* Flush all buffered strings to buffer, and reset status */
|
|
459 if (!outbytesleft)
|
|
460 {
|
|
461 last_errno = EFAULT;
|
|
462 return -1;
|
|
463 }
|
|
464 out_len = *outbytesleft;
|
|
465 st = TECFlushText(cd->tec,
|
|
466 buf_out, out_len, &out_true);
|
|
467 }
|
|
468 else if (!buf_in && !buf_out)
|
|
469 {
|
|
470 /* Reset cd's status and cancel buffered strings */
|
|
471 unsigned char tmp_out[256];
|
|
472
|
|
473 buf_out = tmp_out;
|
|
474 out_len = sizeof(tmp_out);
|
|
475 st = TECFlushText(cd->tec,
|
|
476 buf_out, out_len, &out_true);
|
|
477 }
|
|
478 else
|
|
479 {
|
|
480 last_errno = EFAULT;
|
|
481 return -1;
|
|
482 }
|
|
483 TRACE("st=%d, buf_in=%p, in_len=%d, in_true=%d\n"
|
|
484 " buf_out=%p, out_len=%d, out_true=%d\n", (int)st,
|
|
485 buf_in, (int)in_len, (int)in_true,
|
|
486 buf_out, (int)out_len, (int)out_true);
|
|
487
|
|
488 switch (st)
|
|
489 {
|
|
490 case 0:
|
|
491 /* No error */
|
|
492 if (inbytesleft)
|
|
493 *inbytesleft -= in_true;
|
|
494 if (outbytesleft)
|
|
495 *outbytesleft -= out_true;
|
|
496 if (inbuf && *inbuf)
|
|
497 *inbuf += in_true;
|
|
498 if (outbuf && *outbuf)
|
|
499 *outbuf += out_true;
|
|
500 last_errno = 0;
|
|
501 return 0; /* No error */
|
|
502 case kTECUnmappableElementErr:
|
|
503 last_errno = EILSEQ;
|
|
504 case kTECIncompleteElementErr:
|
|
505 last_errno = EINVAL;
|
|
506 case kTECOutputBufferFullStatus:
|
|
507 last_errno = E2BIG;
|
|
508 return -1;
|
|
509 default:
|
|
510 TRACE("iconv(%p, %p, %p, %p, %p) failed. (%d)\n",
|
|
511 cd, inbuf, inbytesleft, outbuf, outbytesleft, (int)st);
|
|
512 last_errno = EFAULT;
|
|
513 return -1;
|
|
514 }
|
|
515 }
|
|
516
|
|
517 int
|
|
518 iconv_close(iconv_t cd)
|
|
519 {
|
|
520 if (cd)
|
|
521 {
|
|
522 /* Free all elements of iconv_t */
|
|
523 if (cd->tec)
|
|
524 TECDisposeConverter(cd->tec);
|
|
525 if (cd->tecReverse)
|
|
526 TECDisposeConverter(cd->tecReverse);
|
|
527 if (cd->sniff)
|
|
528 TECDisposeSniffer(cd->sniff);
|
|
529 vim_free(cd);
|
|
530 last_errno = 0;
|
|
531 return 0;
|
|
532 }
|
|
533 else
|
|
534 {
|
|
535 last_errno = EINVAL;
|
|
536 return -1;
|
|
537 }
|
|
538 }
|
|
539
|
18
|
540 int *
|
7
|
541 iconv_errno()
|
|
542 {
|
|
543 return &last_errno;
|
|
544 }
|
|
545 #endif /* MACOS_X_ICONVEMU */
|
|
546
|
|
547 #ifdef USE_MCH_GETTEXT
|
|
548
|
|
549 #define GETTEXT_BUFNUM 64
|
|
550 #define GETTEXT_BUFSIZE 256
|
|
551
|
18
|
552 char *
|
|
553 mch_gettext(const char *msgid)
|
7
|
554 {
|
|
555 static char buf[GETTEXT_BUFNUM][GETTEXT_BUFSIZE];
|
|
556 static int bufnum = 0;
|
|
557 const char *msg = NULL;
|
|
558 CFStringRef strkey = NULL, strmsg = NULL;
|
|
559 CFStringEncoding enc;
|
|
560
|
|
561 if (!msgid)
|
|
562 goto MCH_GETTEXT_FINISH;
|
|
563 enc = CFStringGetSystemEncoding();
|
|
564 TRACE("mch_gettext(%s)\n", msgid);
|
|
565
|
|
566 strkey = CFStringCreateWithCString(NULL, msgid, enc);
|
|
567 if (!strkey)
|
|
568 {
|
|
569 TRACE(" Can't create a CFString for msgid.\n");
|
|
570 goto MCH_GETTEXT_FINISH;
|
|
571 }
|
|
572
|
|
573 strmsg = CFCopyLocalizedString(strkey, NULL);
|
|
574 if (!strmsg)
|
|
575 {
|
|
576 TRACE(" No localized strings for msgid.\n");
|
|
577 goto MCH_GETTEXT_FINISH;
|
|
578 }
|
|
579
|
|
580 msg = CFStringGetCStringPtr(strmsg, enc);
|
|
581 if (!msg)
|
|
582 {
|
|
583 /* This is as backup when CFStringGetCStringPtr was failed */
|
|
584 CFStringGetCString(strmsg, buf[bufnum], GETTEXT_BUFSIZE, enc);
|
|
585 msg = buf[bufnum];
|
|
586 if (++bufnum >= GETTEXT_BUFNUM)
|
|
587 bufnum = 0;
|
|
588 }
|
|
589 TRACE(" Localized to: %s\n", msg);
|
|
590
|
|
591 MCH_GETTEXT_FINISH:
|
|
592 if (strkey)
|
|
593 CFRelease(strkey);
|
|
594 if (strmsg)
|
|
595 CFRelease(strmsg);
|
18
|
596 return (char *)(msg ? msg : msgid);
|
7
|
597 }
|
|
598
|
18
|
599 char *
|
|
600 mch_bindtextdomain(const char *domain, const char *dirname)
|
7
|
601 {
|
|
602 TRACE("mch_bindtextdomain(%s, %s)\n", domain, dirname);
|
|
603 return (char*)dirname;
|
|
604 }
|
|
605
|
18
|
606 char *
|
|
607 mch_textdomain(const char *domain)
|
7
|
608 {
|
|
609 TRACE("mch_textdomain(%s)\n", domain);
|
|
610 return (char*)domain;
|
|
611 }
|
|
612 #endif
|