forked from JuulLabs-OSS/cbgo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.m
200 lines (163 loc) · 3.93 KB
/
util.m
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
#import "bt.h"
struct byte_arr
nsdata_to_byte_arr(const NSData *nsdata)
{
return (struct byte_arr) {
.data = [nsdata bytes],
.length = [nsdata length],
};
}
NSData *
byte_arr_to_nsdata(const struct byte_arr *ba)
{
if (ba->length == 0) {
return nil;
} else {
return [NSData dataWithBytes: ba->data length: ba->length];
}
}
struct obj_arr
nsarray_to_obj_arr(const NSArray *arr)
{
struct obj_arr oa = {0};
if (arr != nil && [arr count] > 0) {
oa.count = [arr count],
oa.objs = malloc(oa.count * sizeof (void *)),
assert(oa.objs != NULL);
for (int i = 0; i < oa.count; i++) {
oa.objs[i] = [arr objectAtIndex:i];
}
}
return oa;
}
NSArray *
obj_arr_to_nsarray(const struct obj_arr *oa)
{
NSMutableArray *nsa = [[NSMutableArray alloc] init];
[nsa autorelease];
for (int i = 0; i < oa->count; i++) {
[nsa addObject:oa->objs[i]];
}
return nsa;
}
NSString *
str_to_nsstring(const char *s)
{
return [[NSString alloc] initWithCString:s encoding:NSUTF8StringEncoding];
}
struct bt_error
nserror_to_bt_error(const NSError *err)
{
if (err == NULL) {
return (struct bt_error) {0};
} else {
return (struct bt_error) {
.msg = [err.localizedDescription UTF8String],
.code = err.code,
};
}
}
NSUUID *
str_to_nsuuid(const char *s)
{
NSString *nss = str_to_nsstring(s);
return [[NSUUID alloc] initWithUUIDString:nss];
}
CBUUID *
str_to_cbuuid(const char *s)
{
NSString *nss = str_to_nsstring(s);
return [CBUUID UUIDWithString:nss];
}
NSArray *
strs_to_nsuuids(const struct string_arr *sa)
{
if (sa == nil || sa->count == 0) {
return nil;
};
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr autorelease];
for (int i = 0; i < sa->count; i++) {
[arr addObject:str_to_nsuuid(sa->strings[i])];
}
return arr;
}
NSArray *
strs_to_cbuuids(const struct string_arr *sa)
{
if (sa == nil || sa->count == 0) {
return nil;
};
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr autorelease];
for (int i = 0; i < sa->count; i++) {
[arr addObject:str_to_cbuuid(sa->strings[i])];
}
return arr;
}
NSArray *
strs_to_nsstrings(const struct string_arr *sa)
{
if (sa == nil || sa->count == 0) {
return nil;
};
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr autorelease];
for (int i = 0; i < sa->count; i++) {
[arr addObject:str_to_nsstring(sa->strings[i])];
}
return arr;
}
struct string_arr
cbuuids_to_strs(const NSArray *cbuuids)
{
struct string_arr sa = {0};
if (cbuuids != nil && [cbuuids count] > 0) {
sa.count = [cbuuids count];
sa.strings = malloc(sa.count * sizeof (char *));
assert(sa.strings != NULL);
for (int i = 0; i < sa.count; i++) {
sa.strings[i] = [[[cbuuids objectAtIndex:i] UUIDString] UTF8String];
}
}
return sa;
}
int
dict_int(NSDictionary *dict, NSString *key, int dflt)
{
NSNumber *nsn = [dict objectForKey:key];
if (nsn == nil) {
return dflt;
} else {
return [nsn intValue];
}
}
const char *
dict_string(NSDictionary *dict, NSString *key)
{
return [[dict objectForKey:key] UTF8String];
}
const void *
dict_data(NSDictionary *dict, NSString *key, int *out_len)
{
NSData *data;
data = [dict objectForKey:key];
*out_len = [data length];
return [data bytes];
}
const struct byte_arr
dict_bytes(NSDictionary *dict, NSString *key)
{
const NSData *nsdata = [dict objectForKey:key];
return nsdata_to_byte_arr(nsdata);
}
void
dict_set_bool(NSMutableDictionary *dict, NSString *key, bool val)
{
[dict setObject:[NSNumber numberWithBool:val] forKey:key];
}
void
dict_set_int(NSMutableDictionary *dict, NSString *key, int val)
{
[dict setObject:[NSNumber numberWithInt:val] forKey:key];
}