annotate src/os_mac_conv.c @ 206:87857ffdbf46

updated for version 7.0060
author vimboss
date Tue, 15 Mar 2005 22:46:30 +0000
parents 7fd70926e2e1
children a5fcf36ef512
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
1 /* vi:set ts=8 sts=4 sw=4:
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
2 *
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
4 *
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
8 */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
9 /*
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
10 * os_mac_conv.c: Code specifically for Mac string conversions.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
11 *
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
12 * This code has been put in a separate file to avoid the conflicts that are
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
13 * caused by including both the X11 and Carbon header files.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
14 */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
15
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
16 #define NO_X11_INCLUDES
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
17 #include "vim.h"
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
18
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 18
diff changeset
19 #ifdef FEAT_MBYTE
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
20 extern char_u *mac_string_convert __ARGS((char_u *ptr, int len, int *lenp, int fail_on_error, int from, int to, int *unconvlenp));
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
21 extern int macroman2enc __ARGS((char_u *ptr, long *sizep, long real_size));
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
22 extern int enc2macroman __ARGS((char_u *from, size_t fromlen, char_u *to, int *tolenp, int maxtolen, char_u *rest, int *restlenp));
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
23
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
24 extern void mac_conv_init __ARGS((void));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
25 extern void mac_conv_cleanup __ARGS((void));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
26 extern char_u *mac_utf16_to_enc __ARGS((UniChar *from, size_t fromLen, size_t *actualLen));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
27 extern UniChar *mac_enc_to_utf16 __ARGS((char_u *from, size_t fromLen, size_t *actualLen));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
28 extern CFStringRef mac_enc_to_cfstring __ARGS((char_u *from, size_t fromLen));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
29 extern char_u *mac_precompose_path __ARGS((char_u *decompPath, size_t decompLen, size_t *precompLen));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
30
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
31 static char_u *mac_utf16_to_utf8 __ARGS((UniChar *from, size_t fromLen, size_t *actualLen));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
32 static UniChar *mac_utf8_to_utf16 __ARGS((char_u *from, size_t fromLen, size_t *actualLen));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
33
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
34 /* Converter for composing decomposed HFS+ file paths */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
35 static TECObjectRef gPathConverter;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
36 /* Converter used by mac_utf16_to_utf8 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
37 static TECObjectRef gUTF16ToUTF8Converter;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
38
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
39 /*
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
40 * A Mac version of string_convert_ext() for special cases.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
41 */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
42 char_u *
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
43 mac_string_convert(ptr, len, lenp, fail_on_error, from_enc, to_enc, unconvlenp)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
44 char_u *ptr;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
45 int len;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
46 int *lenp;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
47 int fail_on_error;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
48 int from_enc;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
49 int to_enc;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
50 int *unconvlenp;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
51 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
52 char_u *retval, *d;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
53 CFStringRef cfstr;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
54 int buflen, in, out, l, i;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
55 CFStringEncoding from;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
56 CFStringEncoding to;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
57
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
58 switch (from_enc)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
59 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
60 case 'l': from = kCFStringEncodingISOLatin1; break;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
61 case 'm': from = kCFStringEncodingMacRoman; break;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
62 case 'u': from = kCFStringEncodingUTF8; break;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
63 default: return NULL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
64 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
65 switch (to_enc)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
66 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
67 case 'l': to = kCFStringEncodingISOLatin1; break;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
68 case 'm': to = kCFStringEncodingMacRoman; break;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
69 case 'u': to = kCFStringEncodingUTF8; break;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
70 default: return NULL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
71 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
72
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
73 if (unconvlenp != NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
74 *unconvlenp = 0;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
75 cfstr = CFStringCreateWithBytes(NULL, ptr, len, from, 0);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
76
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
77 if(cfstr == NULL)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
78 fprintf(stderr, "Encoding failed\n");
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
79 /* When conversion failed, try excluding bytes from the end, helps when
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
80 * there is an incomplete byte sequence. Only do up to 6 bytes to avoid
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
81 * looping a long time when there really is something unconvertable. */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
82 while (cfstr == NULL && unconvlenp != NULL && len > 1 && *unconvlenp < 6)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
83 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
84 --len;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
85 ++*unconvlenp;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
86 cfstr = CFStringCreateWithBytes(NULL, ptr, len, from, 0);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
87 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
88 if (cfstr == NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
89 return NULL;
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
90
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
91 if (to == kCFStringEncodingUTF8)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
92 buflen = len * 6 + 1;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
93 else
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
94 buflen = len + 1;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
95 retval = alloc(buflen);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
96 if (retval == NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
97 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
98 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
99 return NULL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
100 }
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
101
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
102 #if 0
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
103 CFRange convertRange = CFRangeMake(0, CFStringGetLength(cfstr));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
104 /* Determine output buffer size */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
105 CFStringGetBytes(cfstr, convertRange, to, NULL, FALSE, NULL, 0, (CFIndex *)&buflen);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
106 retval = (buflen > 0) ? alloc(buflen) : NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
107 if (retval == NULL) {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
108 CFRelease(cfstr);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
109 return NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
110 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
111
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
112 if (lenp)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
113 *lenp = buflen / sizeof(char_u);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
114
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
115 if (!CFStringGetBytes(cfstr, convertRange, to, NULL, FALSE, retval, buflen, NULL))
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
116 #endif
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
117 if (!CFStringGetCString(cfstr, retval, buflen, to))
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
118 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
119 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
120 if (fail_on_error)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
121 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
122 vim_free(retval);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
123 return NULL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
124 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
125
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
126 fprintf(stderr, "Trying char-by-char conversion...\n");
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
127 /* conversion failed for the whole string, but maybe it will work
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
128 * for each character */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
129 for (d = retval, in = 0, out = 0; in < len && out < buflen - 1;)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
130 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
131 if (from == kCFStringEncodingUTF8)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
132 l = utf_ptr2len_check(ptr + in);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
133 else
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
134 l = 1;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
135 cfstr = CFStringCreateWithBytes(NULL, ptr + in, l, from, 0);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
136 if (cfstr == NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
137 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
138 *d++ = '?';
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
139 out++;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
140 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
141 else
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
142 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
143 if (!CFStringGetCString(cfstr, d, buflen - out, to))
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
144 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
145 *d++ = '?';
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
146 out++;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
147 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
148 else
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
149 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
150 i = strlen(d);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
151 d += i;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
152 out += i;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
153 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
154 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
155 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
156 in += l;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
157 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
158 *d = NUL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
159 if (lenp != NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
160 *lenp = out;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
161 return retval;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
162 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
163 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
164 if (lenp != NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
165 *lenp = strlen(retval);
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
166
18
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
167 return retval;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
168 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
169
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
170 /*
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
171 * Conversion from Apple MacRoman char encoding to UTF-8 or latin1, using
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
172 * standard Carbon framework.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
173 * Input: "ptr[*sizep]".
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
174 * "real_size" is the size of the buffer that "ptr" points to.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
175 * output is in-place, "sizep" is adjusted.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
176 * Returns OK or FAIL.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
177 */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
178 int
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
179 macroman2enc(ptr, sizep, real_size)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
180 char_u *ptr;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
181 long *sizep;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
182 long real_size;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
183 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
184 CFStringRef cfstr;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
185 CFRange r;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
186 CFIndex len = *sizep;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
187
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
188 /* MacRoman is an 8-bit encoding, no need to move bytes to
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
189 * conv_rest[]. */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
190 cfstr = CFStringCreateWithBytes(NULL, ptr, len,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
191 kCFStringEncodingMacRoman, 0);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
192 /*
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
193 * If there is a conversion error, try using another
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
194 * conversion.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
195 */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
196 if (cfstr == NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
197 return FAIL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
198
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
199 r.location = 0;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
200 r.length = CFStringGetLength(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
201 if (r.length != CFStringGetBytes(cfstr, r,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
202 (enc_utf8) ? kCFStringEncodingUTF8 : kCFStringEncodingISOLatin1,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
203 0, /* no lossy conversion */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
204 0, /* not external representation */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
205 ptr + *sizep, real_size - *sizep, &len))
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
206 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
207 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
208 return FAIL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
209 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
210 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
211 mch_memmove(ptr, ptr + *sizep, len);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
212 *sizep = len;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
213
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
214 return OK;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
215 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
216
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
217 /*
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
218 * Conversion from UTF-8 or latin1 to MacRoman.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
219 * Input: "from[fromlen]"
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
220 * Output: "to[maxtolen]" length in "*tolenp"
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
221 * Unconverted rest in rest[*restlenp].
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
222 * Returns OK or FAIL.
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
223 */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
224 int
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
225 enc2macroman(from, fromlen, to, tolenp, maxtolen, rest, restlenp)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
226 char_u *from;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
227 size_t fromlen;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
228 char_u *to;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
229 int *tolenp;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
230 int maxtolen;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
231 char_u *rest;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
232 int *restlenp;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
233 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
234 CFStringRef cfstr;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
235 CFRange r;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
236 CFIndex l;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
237
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
238 *restlenp = 0;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
239 cfstr = CFStringCreateWithBytes(NULL, from, fromlen,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
240 (enc_utf8) ? kCFStringEncodingUTF8 : kCFStringEncodingISOLatin1,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
241 0);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
242 while (cfstr == NULL && *restlenp < 3 && fromlen > 1)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
243 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
244 rest[*restlenp++] = from[--fromlen];
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
245 cfstr = CFStringCreateWithBytes(NULL, from, fromlen,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
246 (enc_utf8) ? kCFStringEncodingUTF8 : kCFStringEncodingISOLatin1,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
247 0);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
248 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
249 if (cfstr == NULL)
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
250 return FAIL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
251
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
252 r.location = 0;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
253 r.length = CFStringGetLength(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
254 if (r.length != CFStringGetBytes(cfstr, r,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
255 kCFStringEncodingMacRoman,
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
256 0, /* no lossy conversion */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
257 0, /* not external representation (since vim
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
258 * handles this internally */
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
259 to, maxtolen, &l))
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
260 {
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
261 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
262 return FAIL;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
263 }
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
264 CFRelease(cfstr);
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
265 *tolenp = l;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
266 return OK;
293621502c4d updated for version 7.0010
vimboss
parents:
diff changeset
267 }
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 18
diff changeset
268
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
269 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
270 * Initializes text converters
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
271 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
272 void
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
273 mac_conv_init()
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
274 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
275 TextEncoding utf8_encoding;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
276 TextEncoding utf8_hfsplus_encoding;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
277 TextEncoding utf8_canon_encoding;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
278 TextEncoding utf16_encoding;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
279
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
280 utf8_encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
281 kTextEncodingDefaultVariant, kUnicodeUTF8Format);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
282 utf8_hfsplus_encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
283 kUnicodeHFSPlusCompVariant, kUnicodeUTF8Format);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
284 utf8_canon_encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
285 kUnicodeCanonicalCompVariant, kUnicodeUTF8Format);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
286 utf16_encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
287 kTextEncodingDefaultVariant, kUnicode16BitFormat);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
288
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
289 if (TECCreateConverter(&gPathConverter, utf8_encoding,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
290 utf8_hfsplus_encoding) != noErr)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
291 gPathConverter = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
292
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
293 if (TECCreateConverter(&gUTF16ToUTF8Converter, utf16_encoding,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
294 utf8_canon_encoding) != noErr)
179
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
295 {
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
296 /* On pre-10.3, Unicode normalization is not available so
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
297 * fall back to non-normalizing converter */
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
298 if (TECCreateConverter(&gUTF16ToUTF8Converter, utf16_encoding,
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
299 utf8_encoding) != noErr)
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
300 gUTF16ToUTF8Converter = NULL;
7fd70926e2e1 updated for version 7.0055
vimboss
parents: 168
diff changeset
301 }
168
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
302 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
303
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
304 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
305 * Destroys text converters
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
306 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
307 void
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
308 mac_conv_cleanup()
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
309 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
310 if (gUTF16ToUTF8Converter)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
311 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
312 TECDisposeConverter(gUTF16ToUTF8Converter);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
313 gUTF16ToUTF8Converter = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
314 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
315
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
316 if (gPathConverter)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
317 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
318 TECDisposeConverter(gPathConverter);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
319 gPathConverter = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
320 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
321 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
322
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
323 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
324 * Conversion from UTF-16 UniChars to 'encoding'
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
325 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
326 char_u *
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
327 mac_utf16_to_enc(from, fromLen, actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
328 UniChar *from;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
329 size_t fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
330 size_t *actualLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
331 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
332 /* Following code borrows somewhat from os_mswin.c */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
333 vimconv_T conv;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
334 size_t utf8_len;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
335 char_u *utf8_str;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
336 char_u *result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
337
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
338 /* Convert to utf-8 first, works better with iconv */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
339 utf8_len = 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
340 utf8_str = mac_utf16_to_utf8(from, fromLen, &utf8_len);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
341
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
342 if (utf8_str)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
343 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
344 /* We might be called before we have p_enc set up. */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
345 conv.vc_type = CONV_NONE;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
346
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
347 /* If encoding (p_enc) is any unicode, it is actually in utf-8 (vim
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
348 * internal unicode is always utf-8) so don't convert in such cases */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
349
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
350 if ((enc_canon_props(p_enc) & ENC_UNICODE) == 0)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
351 convert_setup(&conv, (char_u *)"utf-8",
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
352 p_enc? p_enc: (char_u *)"macroman");
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
353 if (conv.vc_type == CONV_NONE)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
354 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
355 /* p_enc is utf-8, so we're done. */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
356 result = utf8_str;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
357 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
358 else
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
359 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
360 result = string_convert(&conv, utf8_str, (int *)&utf8_len);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
361 vim_free(utf8_str);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
362 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
363
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
364 convert_setup(&conv, NULL, NULL);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
365
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
366 if (actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
367 *actualLen = utf8_len;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
368 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
369 else if (actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
370 *actualLen = 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
371
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
372 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
373 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
374
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
375 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
376 * Conversion from 'encoding' to UTF-16 UniChars
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
377 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
378 UniChar *
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
379 mac_enc_to_utf16(from, fromLen, actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
380 char_u *from;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
381 size_t fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
382 size_t *actualLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
383 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
384 /* Following code borrows somewhat from os_mswin.c */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
385 vimconv_T conv;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
386 size_t utf8_len;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
387 char_u *utf8_str;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
388 UniChar *result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
389 Boolean should_free_utf8 = FALSE;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
390
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
391 do
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
392 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
393 /* Use MacRoman by default, we might be called before we have p_enc
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
394 * set up. Convert to utf-8 first, works better with iconv(). Does
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
395 * nothing if 'encoding' is "utf-8". */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
396 conv.vc_type = CONV_NONE;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
397 if ((enc_canon_props(p_enc) & ENC_UNICODE) == 0 &&
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
398 convert_setup(&conv, p_enc ? p_enc : (char_u *)"macroman",
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
399 (char_u *)"utf-8") == FAIL)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
400 break;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
401
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
402 if (conv.vc_type != CONV_NONE)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
403 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
404 utf8_len = fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
405 utf8_str = string_convert(&conv, from, (int *)&utf8_len);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
406 should_free_utf8 = TRUE;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
407 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
408 else
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
409 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
410 utf8_str = from;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
411 utf8_len = fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
412 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
413
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
414 if (utf8_str == NULL)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
415 break;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
416
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
417 convert_setup(&conv, NULL, NULL);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
418
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
419 result = mac_utf8_to_utf16(utf8_str, utf8_len, actualLen);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
420
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
421 if (should_free_utf8)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
422 vim_free(utf8_str);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
423 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
424 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
425 while (0);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
426
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
427 if (actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
428 *actualLen = 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
429
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
430 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
431 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
432
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
433 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
434 * Converts from UTF-16 UniChars to CFString
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
435 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
436 CFStringRef
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
437 mac_enc_to_cfstring(from, fromLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
438 char_u *from;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
439 size_t fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
440 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
441 UniChar *utf16_str;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
442 size_t utf16_len;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
443 CFStringRef result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
444
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
445 utf16_str = mac_enc_to_utf16(from, fromLen, &utf16_len);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
446 if (utf16_str)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
447 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
448 result = CFStringCreateWithCharacters(NULL, utf16_str, utf16_len/sizeof(UniChar));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
449 vim_free(utf16_str);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
450 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
451
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
452 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
453 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
454
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
455 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
456 * Converts a decomposed HFS+ UTF-8 path to precomposed UTF-8
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
457 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
458 char_u *
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
459 mac_precompose_path(decompPath, decompLen, precompLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
460 char_u *decompPath;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
461 size_t decompLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
462 size_t *precompLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
463 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
464 char_u *result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
465 size_t actualLen = 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
466
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
467 if (gPathConverter)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
468 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
469 result = alloc(decompLen);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
470 if (result)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
471 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
472 if (TECConvertText(gPathConverter, decompPath,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
473 decompLen, &decompLen, result,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
474 decompLen, &actualLen) != noErr)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
475 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
476 vim_free(result);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
477 result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
478 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
479 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
480 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
481
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
482 if (precompLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
483 *precompLen = actualLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
484
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
485 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
486 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
487
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
488 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
489 * Converts from UTF-16 UniChars to precomposed UTF-8
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
490 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
491 char_u *
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
492 mac_utf16_to_utf8(from, fromLen, actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
493 UniChar *from;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
494 size_t fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
495 size_t *actualLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
496 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
497 ByteCount utf8_len;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
498 ByteCount inputRead;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
499 char_u *result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
500
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
501 if (gUTF16ToUTF8Converter)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
502 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
503 result = alloc(fromLen * 6 + 1);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
504 if (result && TECConvertText(gUTF16ToUTF8Converter, (ConstTextPtr)from,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
505 fromLen, &inputRead, result,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
506 (fromLen*6+1)*sizeof(char_u), &utf8_len) == noErr)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
507 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
508 TECFlushText(gUTF16ToUTF8Converter, result, (fromLen*6+1)*sizeof(char_u), &inputRead);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
509 utf8_len += inputRead;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
510 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
511 else
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
512 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
513 vim_free(result);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
514 result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
515 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
516 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
517 else
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
518 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
519 result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
520 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
521
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
522 if (actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
523 *actualLen = result ? utf8_len : 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
524
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
525 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
526 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
527
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
528 /*
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
529 * Converts from UTF-8 to UTF-16 UniChars
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
530 */
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
531 UniChar *
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
532 mac_utf8_to_utf16(from, fromLen, actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
533 char_u *from;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
534 size_t fromLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
535 size_t *actualLen;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
536 {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
537 CFStringRef utf8_str;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
538 CFRange convertRange;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
539 UniChar *result = NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
540
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
541 utf8_str = CFStringCreateWithBytes(NULL, from, fromLen,
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
542 kCFStringEncodingUTF8, FALSE);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
543
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
544 if (utf8_str == NULL) {
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
545 if (actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
546 *actualLen = 0;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
547 return NULL;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
548 }
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
549
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
550 convertRange = CFRangeMake(0, CFStringGetLength(utf8_str));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
551 result = (UniChar *)alloc(convertRange.length * sizeof(UniChar));
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
552
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
553 CFStringGetCharacters(utf8_str, convertRange, result);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
554
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
555 CFRelease(utf8_str);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
556
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
557 if (actualLen)
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
558 *actualLen = convertRange.length * sizeof(UniChar);
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
559
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
560 return result;
4d9eabb1396e updated for version 7.0051
vimboss
parents: 20
diff changeset
561 }
20
4ac1dce8dd5e updated for version 7.0012
vimboss
parents: 18
diff changeset
562 #endif /* FEAT_MBYTE */