-
Notifications
You must be signed in to change notification settings - Fork 11
/
NSHelpers.pas
421 lines (337 loc) · 14.1 KB
/
NSHelpers.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
unit NSHelpers;
{
Unit of handy routines for use with Foundation and Core Foundation.
Author: Phil Hess.
Copyright: Copyright 2011 Phil Hess.
License: Modified LGPL. This means you can link your code to this
compiled unit (statically in a standalone executable or
dynamically in a library) without releasing your code. Only
changes to this unit need to be made publicly available.
CFStrToAnsiStr was adapted from the Lazarus CarbonProc unit's
CFStringToStr function.
Note that objects returned by functions with "Create" or "Copy"
in the function name need to be released by the calling code.
For example, CFStringCreateWithCString is called in AnsiStrToCFStr,
meaning this applies to code that calls AnsiStrToCFStr as well.
FreeCFRef and FreeAndNilCFRef are convenience routines provided
for that purpose.
See Apple docs for more information on the so-called Create Rule
and Get Rule:
https://developer.apple.com/library/mac/#documentation/CoreFoundation/
Conceptual/CFMemoryMgmt/Concepts/Ownership.html
}
{$MODE Delphi}
{$modeswitch ObjectiveC1}
interface
uses
CFBase,
CFString,
CocoaAll;
{Routines to convert CFString to and from:
- AnsiString with specified encoding
- string with specified encoding
- UTF8-encoded AnsiString
- WideString
- UnicodeString}
function CFStrToAnsiStr(cfStr : CFStringRef;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1): AnsiString;
procedure AnsiStrToCFStr(const aStr : AnsiString;
out cfStr : CFStringRef;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1);
function CFStrToStr(cfStr : CFStringRef;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1): string;
procedure StrToCFStr(const aStr : string;
out cfStr : CFStringRef;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1);
function CFStrToUtf8Str(cfStr : CFStringRef): AnsiString;
procedure Utf8StrToCFStr(const aStr : AnsiString;
out cfStr : CFStringRef);
function CFStrToWideStr(cfStr : CFStringRef): WideString;
procedure WideStrToCFStr(const aStr : WideString;
out cfStr : CFStringRef);
function CFStrToUniStr(cfStr : CFStringRef): UnicodeString;
procedure UniStrToCFStr(const aStr : UnicodeString;
out cfStr : CFStringRef);
procedure FreeCFRef(var cfRef: CFTypeRef);
procedure FreeAndNilCFRef(var cfRef : CFTypeRef);
{Routines to convert NSString to and from:
- AnsiString with specified encoding
- string with specified encoding
- UTF8-encoded AnsiString
- WideString
- UnicodeString}
function NSStrToAnsiStr(aNSStr : NSString;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1): AnsiString;
procedure AnsiStrToNSStr(const aStr : AnsiString;
out aNSStr : NSString;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1); overload;
function AnsiStrToNSStr(const aStr : AnsiString;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1) : NSString; overload;
function NSStrToStr(aNSStr : NSString;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1): string;
procedure StrToNSStr(const aStr : string;
out aNSStr : NSString;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1); overload;
function StrToNSStr(const aStr : string;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1) : NSString; overload;
function NSStrToUtf8Str(aNSStr : NSString): AnsiString;
procedure Utf8StrToNSStr(const aStr : AnsiString;
out aNSStr : NSString); overload;
function Utf8StrToNSStr(const aStr : AnsiString) : NSString; overload;
function NSStrToWideStr(aNSStr : NSString): WideString;
procedure WideStrToNSStr(const aStr : WideString;
out aNSStr : NSString); overload;
function WideStrToNSStr(const aStr : WideString) : NSString; overload;
function NSStrToUniStr(aNSStr : NSString): UnicodeString;
procedure UniStrToNSStr(const aStr : UnicodeString;
out aNSStr : NSString); overload;
function UniStrToNSStr(const aStr : UnicodeString) : NSString; overload;
implementation
function CFStrToAnsiStr(cfStr : CFStringRef;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1): AnsiString;
{Convert CFString to AnsiString.
If encoding is not specified, encode using CP1252.}
var
StrPtr : Pointer;
StrRange : CFRange;
StrSize : CFIndex;
begin
if cfStr = nil then
begin
Result := '';
Exit;
end;
{First try the optimized function}
StrPtr := CFStringGetCStringPtr(cfStr, encoding);
if StrPtr <> nil then {Succeeded?}
Result := PChar(StrPtr)
else {Use slower approach - see comments in CFString.pas}
begin
StrRange.location := 0;
StrRange.length := CFStringGetLength(cfStr);
{Determine how long resulting string will be}
CFStringGetBytes(cfStr, StrRange, encoding, Ord('?'),
False, nil, 0, StrSize);
SetLength(Result, StrSize); {Expand string to needed length}
if StrSize > 0 then {Convert string?}
CFStringGetBytes(cfStr, StrRange, encoding, Ord('?'),
False, @Result[1], StrSize, StrSize);
end;
end; {CFStrToAnsiStr}
procedure AnsiStrToCFStr(const aStr : AnsiString;
out cfStr : CFStringRef;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1);
{Create CFString from AnsiString.
If encoding is not specified, assume encoded as CP1252.
Note: Calling code is responsible for calling CFRelease on
returned CFString. Presumably that's the reason why CarbonProc
unit's CreateCFString is a procedure, so you don't use it in
an expression and leave the CFString dangling.}
begin
cfStr := CFStringCreateWithCString(nil, Pointer(PChar(aStr)), encoding);
end;
function CFStrToStr(cfStr : CFStringRef;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1): string;
{Convert CFString to string.
If encoding is not specified, encode using CP1252.
This assumes string = AnsiString.}
begin
Result := CFStrToAnsiStr(cfStr, encoding);
end;
procedure StrToCFStr(const aStr : string;
out cfStr : CFStringRef;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1);
{Create CFString from string.
If encoding is not specified, assume encoded as CP1252.
This assumes string = AnsiString.
Note: Calling code is responsible for calling CFRelease on
returned CFString.}
begin
AnsiStrToCFStr(aStr, cfStr, encoding);
end;
function CFStrToUtf8Str(cfStr : CFStringRef): AnsiString;
{Convert CFString to UTF8-encoded AnsiString.}
begin
Result := CFStrToAnsiStr(cfStr, kCFStringEncodingUTF8);
end;
procedure Utf8StrToCFStr(const aStr : AnsiString;
out cfStr : CFStringRef);
{Create CFString from UTF8-encoded AnsiString.
Note: Calling code is responsible for calling CFRelease on
returned CFString.}
begin
AnsiStrToCFStr(aStr, cfStr, kCFStringEncodingUTF8);
end;
function CFStrToWideStr(cfStr : CFStringRef): WideString;
{Convert CFString to WideString.}
begin
Result := UTF8Decode(CFStrToAnsiStr(cfStr, kCFStringEncodingUTF8));
end;
procedure WideStrToCFStr(const aStr : WideString;
out cfStr : CFStringRef);
{Create CFString from WideString.
Note: Calling code is responsible for calling CFRelease on
returned CFString.}
begin
AnsiStrToCFStr(UTF8Encode(aStr), cfStr, kCFStringEncodingUTF8);
end;
function CFStrToUniStr(cfStr : CFStringRef): UnicodeString;
{Convert CFString to UnicodeString.}
begin
Result := UTF8Decode(CFStrToAnsiStr(cfStr, kCFStringEncodingUTF8));
end;
procedure UniStrToCFStr(const aStr : UnicodeString;
out cfStr : CFStringRef);
{Create CFString from UnicodeString.
Note: Calling code is responsible for calling CFRelease on
returned CFString.}
begin
AnsiStrToCFStr(UTF8Encode(aStr), cfStr, kCFStringEncodingUTF8);
end;
procedure FreeCFRef(var cfRef : CFTypeRef);
{Convenience routine to free a CF reference so you don't have
to check if it's nil.}
begin
if Assigned(cfRef) then
CFRelease(cfRef);
end;
procedure FreeAndNilCFRef(var cfRef : CFTypeRef);
{Convenience routine to free a CF reference and set it to nil.}
begin
FreeCFRef(cfRef);
cfRef := nil;
end;
{NSString routines}
function NSStrToAnsiStr(aNSStr : NSString;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1): AnsiString;
{Convert NSString to AnsiString.
If encoding is not specified, encode using CP1252.}
begin
{Note NSString and CFStringRef are interchangable}
Result := CFStrToAnsiStr(CFStringRef(aNSStr), encoding);
end;
procedure AnsiStrToNSStr(const aStr : AnsiString;
out aNSStr : NSString;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1);
{Create NSString from AnsiString.
If encoding is not specified, assume encoded as CP1252.
Note: Calling code is responsible for calling returned
NSString's release method.}
begin
{Note NSString and CFStringRef are interchangable}
AnsiStrToCFStr(aStr, CFStringRef(aNSStr), encoding);
end;
function AnsiStrToNSStr(const aStr : AnsiString;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1) : NSString;
{Function version of previous procedure that can be called
without releasing NSString result.
Tip: You can use the procedure version and call release
explictly to reduce use of autoreleased objects.}
begin
AnsiStrToNSStr(aStr, Result, encoding);
Result.autorelease;
end;
function NSStrToStr(aNSStr : NSString;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1): string;
{Convert NSString to string.
If encoding is not specified, encode using CP1252.
This assumes string = AnsiString.}
begin
{Note NSString and CFStringRef are interchangable}
Result := CFStrToAnsiStr(CFStringRef(aNSStr), encoding);
end;
procedure StrToNSStr(const aStr : string;
out aNSStr : NSString;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1);
{Create NSString from string.
If encoding is not specified, assume encoded as CP1252.
This assumes string = AnsiString.
Note: Calling code is responsible for calling returned
NSString's release method.}
begin
{Note NSString and CFStringRef are interchangable}
AnsiStrToCFStr(aStr, CFStringRef(aNSStr), encoding);
end;
function StrToNSStr(const aStr : string;
encoding : CFStringEncoding = kCFStringEncodingWindowsLatin1) : NSString;
{Function version of previous procedure that can be called
without releasing NSString result.
Tip: You can use the procedure version and call release
explictly to reduce use of autoreleased objects.}
begin
StrToNSStr(aStr, Result, encoding);
Result.autorelease;
end;
function NSStrToUtf8Str(aNSStr : NSString): AnsiString;
{Convert NSString to UTF8-encoded AnsiString.}
begin
{Note NSString and CFStringRef are interchangable}
Result := CFStrToAnsiStr(CFStringRef(aNSStr), kCFStringEncodingUTF8);
end;
procedure Utf8StrToNSStr(const aStr : AnsiString;
out aNSStr : NSString);
{Create NSString from UTF8-encoded AnsiString.
Note: Calling code is responsible for calling returned
NSString's release method.}
begin
{Note NSString and CFStringRef are interchangable}
AnsiStrToCFStr(aStr, CFStringRef(aNSStr), kCFStringEncodingUTF8);
end;
function Utf8StrToNSStr(const aStr : AnsiString) : NSString;
{Function version of previous procedure that can be called
without releasing NSString result.
Tip: You can use the procedure version and call release
explictly to reduce use of autoreleased objects.}
begin
Utf8StrToNSStr(aStr, Result);
Result.autorelease;
end;
function NSStrToWideStr(aNSStr : NSString): WideString;
{Convert NSString to WideString.}
begin
{Note NSString and CFStringRef are interchangable}
Result := CFStrToWideStr(CFStringRef(aNSStr));
end;
procedure WideStrToNSStr(const aStr : WideString;
out aNSStr : NSString);
{Create NSString from WideString.
Note: Calling code is responsible for calling returned
NSString's release method.}
begin
{Note NSString and CFStringRef are interchangable}
WideStrToCFStr(aStr, CFStringRef(aNSStr));
end;
function WideStrToNSStr(const aStr : WideString) : NSString;
{Function version of previous procedure that can be called
without releasing NSString result.
Tip: You can use the procedure version and call release
explictly to reduce use of autoreleased objects.}
begin
WideStrToNSStr(aStr, Result);
Result.autorelease;
end;
function NSStrToUniStr(aNSStr : NSString): UnicodeString;
{Convert NSString to UnicodeString.}
begin
{Note NSString and CFStringRef are interchangable}
Result := CFStrToUniStr(CFStringRef(aNSStr));
end;
procedure UniStrToNSStr(const aStr : UnicodeString;
out aNSStr : NSString);
{Create NSString from UnicodeString.
Note: Calling code is responsible for calling returned
NSString's release method.}
begin
{Note NSString and CFStringRef are interchangable}
UniStrToCFStr(aStr, CFStringRef(aNSStr));
end;
function UniStrToNSStr(const aStr : UnicodeString) : NSString;
{Function version of previous procedure that can be called
without releasing NSString result.
Tip: You can use the procedure version and call release
explictly to reduce use of autoreleased objects.}
begin
UniStrToNSStr(aStr, Result);
Result.autorelease;
end;
end.