FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
livecode/libfoundation/src/foundation-cf.cpp at develop · livecode/livecode · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
livecode
/
livecode
Public archive
Notifications
You must be signed in to change notification settings
Fork
223
Star
517
Code
Pull requests
189
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
livecode
/
libfoundation
/
src
/
foundation-cf.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
525 lines (433 loc) · 15.2 KB
Breadcrumbs
livecode
/
libfoundation
/
src
/
foundation-cf.cpp
Copy path
File metadata and controls
525 lines (433 loc) · 15.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
/*
Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>.
*/
#
include
<
foundation.h
>
#
include
<
foundation-auto.h
>
#
include
"
foundation-private.h
"
#
define
integer_t
cf_integer_t
#
include
<
CoreFoundation/CoreFoundation.h
>
#
undef
integer_t
//
//////////////////////////////////////////////////////////////////////////////
template
<
typename
T>
static
inline
void
CFDeleter
(T type)
{
if
(type ==
nullptr
)
return
;
CFRelease
(type);
}
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCBooleanCreateWithCFBooleanRef
(CFBooleanRef p_cf_boolean, MCBooleanRef& r_boolean)
{
if
(p_cf_boolean ==
kCFBooleanTrue
)
{
r_boolean =
MCValueRetain
(
kMCTrue
);
}
else
{
r_boolean =
MCValueRetain
(
kMCFalse
);
}
return
true
;
}
MC_DLLEXPORT_DEF
bool
MCBooleanConvertToCFBooleanRef
(MCBooleanRef p_boolean, CFBooleanRef& r_boolean_ref)
{
if
(p_boolean ==
kMCTrue
)
{
r_boolean_ref = (CFBooleanRef)
CFRetain
(
kCFBooleanTrue
);
}
else
{
r_boolean_ref = (CFBooleanRef)
CFRetain
(
kCFBooleanFalse
);
}
return
true
;
}
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCNumberCreateWithCFNumberRef
(CFNumberRef p_cf_number, MCNumberRef& r_number)
{
if
(!
CFNumberIsFloatType
(p_cf_number))
{
int64_t
t_integer;
CFNumberGetValue
(p_cf_number,
kCFNumberSInt64Type
, &t_integer);
if
(t_integer >=
INTEGER_MIN
&& t_integer <=
INTEGER_MAX
)
{
return
MCNumberCreateWithInteger
((
integer_t
)t_integer, r_number);
}
else
if
(t_integer >=
UINTEGER_MIN
&& t_integer <=
UINTEGER_MAX
)
{
return
MCNumberCreateWithUnsignedInteger
((
uinteger_t
)t_integer, r_number);
}
}
double
t_real;
CFNumberGetValue
(p_cf_number,
kCFNumberDoubleType
, &t_real);
return
MCNumberCreateWithReal
(t_real, r_number);
}
MC_DLLEXPORT_DEF
bool
MCNumberConvertToCFNumberRef
(MCNumberRef p_number, CFNumberRef& r_number_ref)
{
CFNumberRef t_number_ref;
if
(
MCNumberIsInteger
(p_number))
{
t_number_ref =
CFNumberCreate
(
nullptr
,
kCFNumberSInt32Type
, &p_number->
integer
);
}
else
{
t_number_ref =
CFNumberCreate
(
nullptr
,
kCFNumberDoubleType
, &p_number->
real
);
}
if
(t_number_ref ==
nullptr
)
{
return
false
;
}
r_number_ref = t_number_ref;
return
true
;
}
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCStringCreateWithCFStringRef
(CFStringRef p_cf_string, MCStringRef& r_string)
{
bool
t_success;
t_success =
true
;
CFIndex t_string_length;
t_string_length =
CFStringGetLength
(p_cf_string);
CFIndex t_buffer_size;
t_buffer_size =
CFStringGetMaximumSizeForEncoding
(t_string_length,
kCFStringEncodingUnicode
) +
1
;
MCAutoPointer<
byte_t
> t_buffer;
t_success =
MCMemoryNewArray
(t_buffer_size, &t_buffer);
CFIndex t_used_size;
if
(t_success)
CFStringGetBytes
(p_cf_string,
CFRangeMake
(
0
, t_string_length),
kCFStringEncodingUnicode
,
'
?
'
,
false
, (UInt8*)*t_buffer, t_buffer_size, &t_used_size);
if
(t_success)
t_success =
MCStringCreateWithChars
((
unichar_t
*)*t_buffer, t_used_size /
2
, r_string);
return
t_success;
}
MC_DLLEXPORT_DEF
bool
MCStringConvertToCFStringRef
(MCStringRef p_string, CFStringRef& r_cfstring)
{
__MCAssertIsString
(p_string);
uindex_t
t_length;
unichar_t
* t_chars;
t_length =
MCStringGetLength
(p_string);
if
(!
MCMemoryNewArray
(t_length +
1
, t_chars))
return
false
;
MCStringGetChars
(p_string,
MCRangeMake
(
0
, t_length), t_chars);
r_cfstring =
CFStringCreateWithCharacters
(nil, t_chars, t_length);
MCMemoryDeleteArray
(t_chars);
return
r_cfstring != nil;
}
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCDataCreateWithCFDataRef
(CFDataRef p_cf_data, MCDataRef& r_data)
{
return
MCDataCreateWithBytes
(
CFDataGetBytePtr
(p_cf_data),
CFDataGetLength
(p_cf_data), r_data);
}
MC_DLLEXPORT_DEF
bool
MCDataConvertToCFDataRef
(MCDataRef p_data, CFDataRef& r_cf_data)
{
CFDataRef t_cf_data =
CFDataCreate
(nil,
MCDataGetBytePtr
(p_data),
MCDataGetLength
(p_data));
if
(t_cf_data ==
nullptr
)
{
return
false
;
}
r_cf_data = t_cf_data;
return
true
;
}
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCProperListCreateWithCFArrayRef
(CFArrayRef p_cf_array,
bool
p_use_lists, MCProperListRef& r_list)
{
MCAutoProperListRef t_list;
if
(!
MCProperListCreateMutable
(&t_list))
{
return
false
;
}
for
(CFIndex t_index =
0
; t_index <
CFArrayGetCount
(p_cf_array); t_index++)
{
MCAutoValueRef t_value;
if
(!
MCValueCreateWithCFTypeRef
(
CFArrayGetValueAtIndex
(p_cf_array, t_index), p_use_lists, &t_value) ||
!
MCProperListPushElementOntoBack
(*t_list, *t_value))
{
return
false
;
}
}
if
(!t_list.
MakeImmutable
())
{
return
false
;
}
r_list = t_list.
Take
();
return
true
;
}
MC_DLLEXPORT_DEF
bool
MCProperListConvertToCFArrayRef
(MCProperListRef p_list,
bool
p_use_lists, CFArrayRef& r_cf_array)
{
MCAutoCustomPointer<
const
void
, CFDeleter> t_cf_array =
CFArrayCreateMutable
(
nullptr
,
MCProperListGetLength
(p_list), &
kCFTypeArrayCallBacks
);
if
(!t_cf_array)
{
return
MCErrorThrowOutOfMemory
();
}
for
(
uindex_t
t_index =
0
; t_index <
MCProperListGetLength
(p_list); t_index++)
{
MCAutoCustomPointer<
const
void
, CFDeleter> t_cf_value;
if
(!
MCValueConvertToCFTypeRef
(
MCProperListFetchElementAtIndex
(p_list, t_index), p_use_lists, &t_cf_value))
{
return
false
;
}
CFArraySetValueAtIndex
((CFMutableArrayRef)*t_cf_array, t_index, *t_cf_value);
}
r_cf_array = (CFArrayRef)t_cf_array.
Release
();
return
true
;
}
MC_DLLEXPORT_DEF
bool
MCProperListCreateWithCFDictionaryRef
(CFDictionaryRef p_cf_dictionary,
bool
p_use_lists, MCProperListRef& r_proper_list)
{
MCAutoArrayRef t_array;
if
(!
MCArrayCreateWithCFDictionaryRef
(p_cf_dictionary, p_use_lists, &t_array))
{
return
false
;
}
return
MCArrayConvertToProperList
(*t_array, r_proper_list);
}
MC_DLLEXPORT_DEF
bool
MCProperListConvertToCFDictionaryRef
(MCProperListRef p_list,
bool
p_use_lists, CFDictionaryRef& r_cf_dictionary)
{
MCAutoArrayRef t_array;
if
(!
MCProperListConvertToArray
(p_list, &t_array))
{
return
false
;
}
return
MCArrayConvertToCFDictionaryRef
(*t_array, p_use_lists, r_cf_dictionary);
}
//
//////////////////////////////////////////////////////////////////////////////
struct
MCArrayCreateWithCFDictionaryRefContext
{
bool
use_lists;
MCArrayRef array;
bool
result;
};
static
void
MCArrayCreateWithCFDictionaryRefApplier
(
const
void
*p_key,
const
void
*p_value,
void
*p_context)
{
MCArrayCreateWithCFDictionaryRefContext *context =
static_cast
<MCArrayCreateWithCFDictionaryRefContext *>(p_context);
CFTypeRef t_cf_key = (CFTypeRef)p_key;
CFTypeRef t_cf_value = (CFTypeRef)p_value;
if
(p_value ==
nullptr
)
{
return
;
}
if
(
CFGetTypeID
(p_key) !=
CFStringGetTypeID
())
{
context->
result
=
false
;
return
;
}
MCAutoStringRef t_key_string;
MCNewAutoNameRef t_key;
MCAutoValueRef t_value;
if
(!
MCStringCreateWithCFStringRef
((CFStringRef)t_cf_key, &t_key_string) ||
!
MCNameCreate
(*t_key_string, &t_key) ||
!
MCValueCreateWithCFTypeRef
(t_cf_value, context->
use_lists
, &t_value) ||
!
MCArrayStoreValue
(context->
array
,
true
, *t_key, *t_value))
{
context->
result
=
false
;
return
;
}
}
MC_DLLEXPORT_DEF
bool
MCArrayCreateWithCFDictionaryRef
(CFDictionaryRef p_cf_dict,
bool
p_use_lists, MCArrayRef& r_array)
{
MCAutoArrayRef t_array;
if
(!
MCArrayCreateMutable
(&t_array))
{
return
false
;
}
MCArrayCreateWithCFDictionaryRefContext t_context;
t_context.
use_lists
= p_use_lists;
t_context.
array
= *t_array;
t_context.
result
=
true
;
CFDictionaryApplyFunction
(p_cf_dict, MCArrayCreateWithCFDictionaryRefApplier, &t_context);
if
(!t_context.
result
)
{
return
false
;
}
if
(!t_array.
MakeImmutable
())
{
return
false
;
}
r_array = t_array.
Take
();
return
true
;
}
MC_DLLEXPORT_DEF
bool
MCArrayConvertToCFDictionaryRef
(MCArrayRef p_array,
bool
p_use_lists, CFDictionaryRef& r_cf_dict)
{
MCAutoCustomPointer<
const
void
, CFDeleter> t_cf_dict =
CFDictionaryCreateMutable
(
nullptr
,
MCArrayGetCount
(p_array),
&
kCFTypeDictionaryKeyCallBacks
,
&
kCFTypeDictionaryValueCallBacks
);
uintptr_t
t_iterator =
0
;
MCNameRef t_key =
nullptr
;
MCValueRef t_value =
nullptr
;
while
(
MCArrayIterate
(p_array, t_iterator, t_key, t_value))
{
MCAutoCustomPointer<
const
void
, CFDeleter> t_cf_key;
if
(!
MCStringConvertToCFStringRef
(
MCNameGetString
(t_key), (CFStringRef&)t_cf_key))
{
return
false
;
}
MCAutoCustomPointer<
const
void
, CFDeleter> t_cf_value;
if
(!
MCValueConvertToCFTypeRef
(t_value, p_use_lists, &t_cf_value))
{
return
false
;
}
CFDictionarySetValue
((CFMutableDictionaryRef)*t_cf_dict, *t_cf_key, *t_cf_value);
}
r_cf_dict = (CFDictionaryRef)t_cf_dict.
Release
();
return
true
;
}
MC_DLLEXPORT_DEF
bool
MCArrayCreateWithCFArrayRef
(CFArrayRef p_cf_array,
bool
p_use_lists, MCArrayRef& r_array)
{
MCAutoProperListRef t_list;
if
(!
MCProperListCreateWithCFArrayRef
(p_cf_array, p_use_lists, &t_list))
{
return
false
;
}
return
MCProperListConvertToArray
(*t_list, r_array);
}
MC_DLLEXPORT_DEF
bool
MCArrayConvertToCFArrayRef
(MCArrayRef p_array,
bool
p_use_lists, CFArrayRef& r_cf_array)
{
MCAutoProperListRef t_list;
if
(!
MCArrayConvertToProperList
(p_array, &t_list))
{
return
false
;
}
if
(*t_list ==
nullptr
)
{
r_cf_array =
nullptr
;
return
true
;
}
return
MCProperListConvertToCFArrayRef
(*t_list, p_use_lists, r_cf_array);
}
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCValueCreateWithCFTypeRef
(CFTypeRef p_cf_type,
bool
p_use_lists, MCValueRef& r_value)
{
CFTypeID t_type_id =
CFGetTypeID
(p_cf_type);
if
((CFNullRef)p_cf_type ==
kCFNull
)
{
r_value =
MCValueRetain
(
kMCNull
);
return
true
;
}
else
if
((CFBooleanRef)p_cf_type ==
kCFBooleanTrue
)
{
r_value =
MCValueRetain
(
kMCTrue
);
return
true
;
}
else
if
((CFBooleanRef)p_cf_type ==
kCFBooleanFalse
)
{
r_value =
MCValueRetain
(
kMCFalse
);
return
true
;
}
else
if
(t_type_id ==
CFNumberGetTypeID
())
{
return
MCNumberCreateWithCFNumberRef
((CFNumberRef)p_cf_type, (MCNumberRef&)r_value);
}
else
if
(t_type_id ==
CFDataGetTypeID
())
{
return
MCDataCreateWithCFDataRef
((CFDataRef)p_cf_type, (MCDataRef&)r_value);
}
else
if
(t_type_id ==
CFStringGetTypeID
())
{
return
MCStringCreateWithCFStringRef
((CFStringRef)p_cf_type, (MCStringRef&)r_value);
}
else
if
(t_type_id ==
CFArrayGetTypeID
())
{
if
(p_use_lists)
{
return
MCProperListCreateWithCFArrayRef
((CFArrayRef)p_cf_type, p_use_lists, (MCProperListRef&)r_value);
}
return
MCArrayCreateWithCFArrayRef
((CFArrayRef)p_cf_type, p_use_lists, (MCArrayRef&)r_value);
}
else
if
(t_type_id ==
CFDictionaryGetTypeID
())
{
MCAutoArrayRef t_array;
if
(!
MCArrayCreateWithCFDictionaryRef
((CFDictionaryRef)p_cf_type, p_use_lists, &t_array))
{
return
false
;
}
MCValueRef t_value;
if
(p_use_lists &&
MCArrayFetchValueAtIndex
(*t_array,
1
, t_value))
{
MCAutoProperListRef t_maybe_list;
if
(!
MCArrayConvertToProperList
(*t_array, &t_maybe_list))
{
return
false
;
}
if
(*t_maybe_list !=
nullptr
)
{
r_value = t_maybe_list.
Take
();
return
true
;
}
}
r_value = t_array.
Take
();
return
true
;
}
return
MCObjcObjectCreateWithId
((
void
*)p_cf_type, (MCObjcObjectRef&)r_value);
}
MC_DLLEXPORT_DEF
bool
MCValueConvertToCFTypeRef
(MCValueRef p_value,
bool
p_use_lists, CFTypeRef& r_cf_type)
{
switch
(
MCValueGetTypeCode
(p_value))
{
case
kMCValueTypeCodeNull
:
r_cf_type =
CFRetain
(
kCFNull
);
return
true
;
case
kMCValueTypeCodeBoolean
:
return
MCBooleanConvertToCFBooleanRef
((MCBooleanRef)p_value, (CFBooleanRef&)r_cf_type);
case
kMCValueTypeCodeNumber
:
return
MCNumberConvertToCFNumberRef
((MCNumberRef)p_value, (CFNumberRef&)r_cf_type);
case
kMCValueTypeCodeData
:
return
MCDataConvertToCFDataRef
((MCDataRef)p_value, (CFDataRef&)r_cf_type);
case
kMCValueTypeCodeString
:
return
MCStringConvertToCFStringRef
((MCStringRef)p_value, (CFStringRef&)r_cf_type);
case
kMCValueTypeCodeName
:
return
MCStringConvertToCFStringRef
(
MCNameGetString
((MCNameRef)p_value), (CFStringRef&)r_cf_type);
case
kMCValueTypeCodeArray
:
MCValueRef t_value;
if
(p_use_lists &&
MCArrayFetchValueAtIndex
((MCArrayRef)p_value,
1
, t_value))
{
CFTypeRef t_cf_type;
if
(!
MCArrayConvertToCFArrayRef
((MCArrayRef)p_value, p_use_lists, (CFArrayRef&)t_cf_type))
{
return
false
;
}
if
(t_cf_type !=
nullptr
)
{
r_cf_type = t_cf_type;
return
true
;
}
}
return
MCArrayConvertToCFDictionaryRef
((MCArrayRef)p_value, p_use_lists, (CFDictionaryRef&)r_cf_type);
case
kMCValueTypeCodeProperList
:
return
MCProperListConvertToCFArrayRef
((MCProperListRef)p_value, p_use_lists, (CFArrayRef&)r_cf_type);
default
:
if
(
MCValueGetTypeInfo
(p_value) ==
kMCObjcObjectTypeInfo
)
{
r_cf_type =
CFRetain
(
MCObjcObjectGetId
((MCObjcObjectRef)p_value));
return
true
;
}
break
;
}
return
false
;
}
//
//////////////////////////////////////////////////////////////////////////////
Back
|
FazBrowse Home
|
New Git URL