blob: 5e3ef598209b58f02285a29b04ddbc8f4b24487f (
plain)
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
|
/* NSDictionary+HBAdditions.m $
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
#import "NSDictionary+HBAdditions.h"
@implementation NSDictionary (HBAddtions)
- (instancetype)initWithHBDict:(const hb_dict_t *)dict
{
self = [self init];
if (self)
{
self = convertDictToObjcType(dict);
}
return self;
}
- (hb_dict_t *)hb_dictValue
{
return convertObjcDictToHBValue(self);
}
#pragma mark - hb_dict_t to NSDictionary
static id valueToObjcValue(const hb_value_t *val)
{
hb_value_type_t val_type = hb_value_type(val);
id result = nil;
switch (val_type)
{
case HB_VALUE_TYPE_INT:
result = @(hb_value_get_int(val));
break;
case HB_VALUE_TYPE_DOUBLE:
result = @(hb_value_get_double(val));
break;
case HB_VALUE_TYPE_BOOL:
result = @((BOOL)hb_value_get_bool(val));
break;
case HB_VALUE_TYPE_STRING:
result = @(hb_value_get_string(val));
break;
case HB_VALUE_TYPE_DICT:
result = convertDictToObjcType(val);
break;
case HB_VALUE_TYPE_ARRAY:
result = convertArrayToObjcType(val);
break;
case HB_VALUE_TYPE_NULL:
result = [NSNull null];
break;
default:
break;
}
return result;
}
static NSDictionary * convertDictToObjcType(const hb_dict_t *dict)
{
NSMutableDictionary *result = [NSMutableDictionary dictionary];
hb_dict_iter_t iter;
for (iter = hb_dict_iter_init(dict);
iter != HB_DICT_ITER_DONE;
iter = hb_dict_iter_next(dict, iter))
{
const NSString *key = @(hb_dict_iter_key(iter));
const hb_value_t *val = hb_dict_iter_value(iter);
id objcType = valueToObjcValue(val);
if (objcType)
{
result[key] = objcType;
}
}
return [result copy];
}
static NSArray * convertArrayToObjcType(const hb_value_array_t *array)
{
size_t count = hb_value_array_len(array);
NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];
for (int ii = 0; ii < count; ii++)
{
const hb_value_t *val = hb_value_array_get(array, ii);
id objcType = valueToObjcValue(val);
if (objcType)
{
[result addObject:objcType];
}
}
return [result copy];
}
#pragma mark - NSDictionary to hb_dict_t
static hb_value_t * objcValueToHBValue(id val)
{
hb_value_t *result = NULL;
if ([val isKindOfClass:[NSNumber class]])
{
NSNumber *number = val;
const char *objCType = number.objCType;
if((strcmp(objCType, @encode(float))) == 0 ||
(strcmp(objCType, @encode(double))) == 0)
{
result = hb_value_double(number.doubleValue);
}
else if((strcmp(objCType, @encode(BOOL))) == 0)
{
result = hb_value_bool(number.boolValue);
}
else
{
result = hb_value_int(number.integerValue);
}
}
else if ([val isKindOfClass:[NSString class]])
{
NSString *string = val;
result = hb_value_string(string.UTF8String);
}
else if ([val isKindOfClass:[NSDictionary class]])
{
result = convertObjcDictToHBValue(val);
}
else if ([val isKindOfClass:[NSArray class]])
{
result = convertObjcArrayToHBValue(val);
}
return result;
}
static hb_dict_t * convertObjcDictToHBValue(NSDictionary *dict)
{
hb_dict_t *result = hb_dict_init();
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop)
{
if ([key isKindOfClass:[NSString class]])
{
hb_value_t *val = objcValueToHBValue(obj);
if (val)
{
hb_dict_set(result, [key UTF8String], objcValueToHBValue(obj));
}
}
}];
return result;
}
static hb_value_array_t * convertObjcArrayToHBValue(NSArray *array)
{
hb_value_array_t *result = hb_value_array_init();
for (id obj in array)
{
hb_value_t *val = objcValueToHBValue(obj);
if (val)
{
hb_value_array_append(result, objcValueToHBValue(obj));
}
}
return result;
}
@end
|