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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
|
/* swig.i */
%module(package="libplist") PList
%feature("autodoc", "1");
%{
/* Includes the header in the wrapper code */
#include <plist/plist.h>
#include <plist/plist++.h>
typedef struct {
plist_t node;
char should_keep_plist;
} PListNode;
PListNode *allocate_plist_wrapper(plist_t plist, char should_keep_plist) {
PListNode* wrapper = (PListNode*) malloc(sizeof(PListNode));
if (wrapper) {
memset(wrapper, 0, sizeof(PListNode));
wrapper->node = plist;
wrapper->should_keep_plist = should_keep_plist;
return wrapper;
}
return NULL;
}
%}
%include "std_string.i"
%typemap(out) std::vector<char> {
$result = SWIG_FromCharPtrAndSize((const char*)&($1[0]),(int)($1.size()));
}
%typemap(in) (const std::vector<char>& v)
{
char* buffer = NULL;
int length = 0;
SWIG_AsCharPtrAndSize($input, &buffer, &length, NULL);
$1 = std::vector<char>(buffer, buffer + length);
}
#if SWIGPYTHON
//for datetime in python
%{
#include <ctime>
#include <datetime.h>
%}
%typemap(typecheck,precedence=SWIG_TYPECHECK_POINTER) timeval {
PyDateTime_IMPORT;
$1 = PyDateTime_Check($input) ? 1 : 0;
}
%typemap(out) timeval {
struct tm* t = gmtime ( &$1.tv_sec );
if (t)
{
PyDateTime_IMPORT;
$result = PyDateTime_FromDateAndTime(t->tm_year+1900, t->tm_mon+1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, $1.tv_usec);
}
}
%typemap(in) (timeval t)
{
PyDateTime_IMPORT;
if (!PyDateTime_Check($input)) {
PyErr_SetString(PyExc_ValueError,"Expected a datetime");
return NULL;
}
struct tm t = {
PyDateTime_DATE_GET_SECOND($input),
PyDateTime_DATE_GET_MINUTE($input),
PyDateTime_DATE_GET_HOUR($input),
PyDateTime_GET_DAY($input),
PyDateTime_GET_MONTH($input)-1,
PyDateTime_GET_YEAR($input)-1900,
0,0,0
};
timeval ret = {(int)mktime(&t), PyDateTime_DATE_GET_MICROSECOND($input)};
$1 = ret;
}
#endif
%apply SWIGTYPE *DYNAMIC { PList::Node* };
%apply SWIGTYPE *DYNAMIC { PList::Structure* };
%{
static swig_type_info *Node_dynamic(void **ptr)
{
PList::Node* node = dynamic_cast<PList::Node *>((PList::Node *) *ptr);
if (node)
{
plist_type type = node->GetType();
switch(type)
{
case PLIST_DICT:
*ptr = dynamic_cast<PList::Dictionary *>(node);
return SWIGTYPE_p_PList__Dictionary;
case PLIST_ARRAY:
*ptr = dynamic_cast<PList::Array *>(node);
return SWIGTYPE_p_PList__Array;
case PLIST_BOOLEAN:
*ptr = dynamic_cast<PList::Boolean *>(node);
return SWIGTYPE_p_PList__Boolean;
case PLIST_UINT:
*ptr = dynamic_cast<PList::Integer *>(node);
return SWIGTYPE_p_PList__Integer;
case PLIST_REAL:
*ptr = dynamic_cast<PList::Real *>(node);
return SWIGTYPE_p_PList__Real;
case PLIST_STRING:
*ptr = dynamic_cast<PList::String *>(node);
return SWIGTYPE_p_PList__String;
case PLIST_DATE:
*ptr = dynamic_cast<PList::Date *>(node);
return SWIGTYPE_p_PList__Date;
case PLIST_DATA:
*ptr = dynamic_cast<PList::Data *>(node);
return SWIGTYPE_p_PList__Data;
default:
break;
}
}
return 0;
}
%}
// Register the above casting function
DYNAMIC_CAST(SWIGTYPE_p_PList__Node, Node_dynamic);
DYNAMIC_CAST(SWIGTYPE_p_PList__Structure, Node_dynamic);
%include "std_map.i"
// Instantiate templates used by example
namespace std {
%template(PairStringNodePtr) std::pair<string, PList::Node*>;
%template(MapStringNodePtr) map<string,PList::Node*>;
}
%rename(__assign__) *::operator=;
%rename(__getitem__) *::operator[];
%rename(__delitem__) *::Remove;
%rename(__setitem__) PList::Dictionary::Insert;
%rename(__deepcopy__) *::Clone;
%rename(__len__) *::GetSize;
%rename(get_type) *::GetType;
%rename(set_value) *::SetValue;
%rename(get_value) *::GetValue;
%rename(to_xml) *::ToXml;
%rename(to_bin) *::ToBin;
%rename(from_xml) *::FromXml;
%rename(from_bin) *::FromBin;
%rename(append) *::Append;
%rename(insert) PList::Array::Insert;
%ignore GetPlist();
%ignore Boolean(plist_t);
%ignore Integer(plist_t);
%ignore Real(plist_t);
%ignore String(plist_t);
%ignore Data(plist_t);
%ignore Date(plist_t);
%ignore Array(plist_t);
%ignore Dictionary(plist_t);
%ignore Begin();
%ignore End();
%ignore Find();
%include <plist/Node.h>
%include <plist/Boolean.h>
%include <plist/Integer.h>
%include <plist/Real.h>
%include <plist/String.h>
%include <plist/Data.h>
%include <plist/Date.h>
%include <plist/Structure.h>
%include <plist/Array.h>
%include <plist/Dictionary.h>
%include <plist/Utils.h>
%extend PList::Dictionary {
%newobject key_iterator(PyObject **PYTHON_SELF);
swig::PySwigIterator* key_iterator(PyObject **PYTHON_SELF) {
return swig::make_output_key_iterator(self->Begin(), self->Begin(), self->End(), *PYTHON_SELF);
}
%newobject value_iterator(PyObject **PYTHON_SELF);
swig::PySwigIterator* value_iterator(PyObject **PYTHON_SELF) {
return swig::make_output_value_iterator(self->Begin(), self->Begin(), self->End(), *PYTHON_SELF);
}
iterator iteritems()
{
return self->Begin();
}
bool has_key(const std::string& key) const {
PList::Dictionary* dict = const_cast<PList::Dictionary*>(self);
PList::Dictionary::iterator i = dict->Find(key);
return i != dict->End();
}
PyObject* keys() {
uint32_t size = self->GetSize();
int pysize = (size <= (uint32_t) INT_MAX) ? (int) size : -1;
if (pysize < 0) {
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
PyErr_SetString(PyExc_OverflowError,
"map size not valid in python");
SWIG_PYTHON_THREAD_END_BLOCK;
return NULL;
}
PyObject* keyList = PyList_New(pysize);
PList::Dictionary::iterator i = self->Begin();
for (int j = 0; j < pysize; ++i, ++j) {
PyList_SET_ITEM(keyList, j, swig::from(i->first));
}
return keyList;
}
PyObject* values() {
uint32_t size = self->GetSize();
int pysize = (size <= (uint32_t) INT_MAX) ? (int) size : -1;
if (pysize < 0) {
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
PyErr_SetString(PyExc_OverflowError,
"map size not valid in python");
SWIG_PYTHON_THREAD_END_BLOCK;
return NULL;
}
PyObject* valList = PyList_New(pysize);
PList::Dictionary::iterator i = self->Begin();
for (int j = 0; j < pysize; ++i, ++j) {
PList::Node *second = i->second;
PyObject *down = SWIG_NewPointerObj(SWIG_as_voidptr(second), SWIG_TypeDynamicCast(SWIGTYPE_p_PList__Node, SWIG_as_voidptrptr(&second)), 0 | 0 );
PyList_SET_ITEM(valList, j, down);
}
return valList;
}
PyObject* items() {
uint32_t size = self->GetSize();
int pysize = (size <= (uint32_t) INT_MAX) ? (int) size : -1;
if (pysize < 0) {
SWIG_PYTHON_THREAD_BEGIN_BLOCK;
PyErr_SetString(PyExc_OverflowError,
"map size not valid in python");
SWIG_PYTHON_THREAD_END_BLOCK;
return NULL;
}
PyObject* itemList = PyList_New(pysize);
PList::Dictionary::iterator i = self->Begin();
for (int j = 0; j < pysize; ++i, ++j) {
PyObject *item = PyTuple_New(2);
PList::Node *second = i->second;
PyObject *down = SWIG_NewPointerObj(SWIG_as_voidptr(second), SWIG_TypeDynamicCast(SWIGTYPE_p_PList__Node, SWIG_as_voidptrptr(&second)), 0 | 0 );
PyTuple_SetItem(item, 0, swig::from(i->first));
PyTuple_SetItem(item, 1, down);
PyList_SET_ITEM(itemList, j, item);
}
return itemList;
}
%pythoncode {def __iter__(self): return self.key_iterator()}
%pythoncode {def iterkeys(self): return self.key_iterator()}
%pythoncode {def itervalues(self): return self.value_iterator()}
}
//deprecated wrapper below
%include "stdint.i"
%include "cstring.i"
/* Parse the header file to generate wrappers */
typedef enum {
PLIST_BOOLEAN,
PLIST_UINT,
PLIST_REAL,
PLIST_STRING,
PLIST_ARRAY,
PLIST_DICT,
PLIST_DATE,
PLIST_DATA,
PLIST_KEY,
PLIST_NONE
} plist_type;
typedef struct {
} PListNode;
%extend PListNode { // Attach these functions to struct Vector
PListNode(plist_type t) {
PListNode* node = NULL;
switch (t) {
case PLIST_ARRAY :
node = allocate_plist_wrapper( plist_new_array(), 0 );
break;
case PLIST_DICT :
node = allocate_plist_wrapper( plist_new_dict(), 0 );
break;
default :
node = NULL;
break;
}
return node;
}
%cstring_input_binary(char *data, uint64_t len);
PListNode(char *data, uint64_t len) {
//first check input
if (len > 8) {
plist_t plist = NULL;
if (memcmp(data, "bplist00", 8) == 0) {
plist_from_bin(data, len, &plist);
} else {
plist_from_xml(data, len, &plist);
}
if (plist)
return allocate_plist_wrapper( plist, 0 );
}
return NULL;
}
~PListNode() {
if (!$self->should_keep_plist) {
plist_free($self->node);
}
$self->node = NULL;
$self->should_keep_plist = 0;
free($self);
$self = NULL;
}
void add_sub_node(PListNode* subnode) {
if (subnode) {
plist_add_sub_node($self->node, subnode->node);
//subnode is not root anymore. Do not delete tree
subnode->should_keep_plist = 1;
}
}
void add_sub_key(char* k) {
plist_add_sub_key_el($self->node, k);
}
void add_sub_string(char* s) {
plist_add_sub_string_el($self->node, s);
}
void add_sub_bool(char b) {
plist_add_sub_bool_el($self->node, b);
}
void add_sub_uint(uint64_t i) {
plist_add_sub_uint_el($self->node, i);
}
void add_sub_real(double d) {
plist_add_sub_real_el($self->node, d);
}
%cstring_input_binary(char *data, uint64_t len);
void add_sub_data(char *data, uint64_t len) {
plist_add_sub_data_el($self->node, data, len);
}
void set_as_key(char* k) {
plist_set_key_val($self->node, k);
}
void set_as_string(char* s) {
plist_set_string_val($self->node, s);
}
void set_as_bool(char b) {
plist_set_bool_val($self->node, b);
}
void set_as_uint(uint64_t i) {
plist_set_uint_val($self->node, i);
}
void set_as_real(double d) {
plist_set_real_val($self->node, d);
}
%cstring_input_binary(char *data, uint64_t len);
void set_as_data(char *data, uint64_t len) {
plist_set_data_val($self->node, data, len);
}
PListNode* get_first_child() {
plist_t node = plist_get_first_child( $self->node );
if (node) {
return allocate_plist_wrapper(node, 1);
}
return NULL;
}
PListNode* get_next_sibling() {
plist_t node = plist_get_next_sibling( $self->node );
if (node) {
return allocate_plist_wrapper(node, 1);
}
return NULL;
}
PListNode* get_prev_sibling() {
plist_t node = plist_get_prev_sibling( $self->node );
if (node) {
return allocate_plist_wrapper(node, 1);
}
return NULL;
}
PListNode* get_parent() {
plist_t node = plist_get_parent( $self->node );
if (node) {
return allocate_plist_wrapper(node, 1);
}
return NULL;
}
%newobject as_key;
char* as_key() {
char* k = NULL;
plist_get_key_val($self->node, &k);
return k;
}
%newobject as_string;
char* as_string() {
char* s = NULL;
plist_get_string_val($self->node, &s);
return s;
}
char as_bool() {
uint8_t b;
plist_get_bool_val($self->node, &b);
return (char)b;
}
uint64_t as_uint() {
uint64_t i = 0;
plist_get_uint_val($self->node, &i);
return i;
}
double as_real() {
double d = 0;
plist_get_real_val($self->node, &d);
return d;
}
%cstring_output_allocate_size(char **STRING, uint64_t *LENGTH, free(*$1));
void as_data(char **STRING, uint64_t *LENGTH) {
char* s = NULL;
uint64_t l;
plist_get_data_val($self->node, &s, &l);
*STRING = s;
*LENGTH = l;
return;
}
plist_type get_type() {
return plist_get_node_type($self->node);
}
PListNode* find_node_by_key(char *s) {
plist_t node = plist_find_node_by_key($self->node, s);
if (node) {
return allocate_plist_wrapper(node, 1);
}
return NULL;
}
PListNode* find_node_by_string(char* s) {
plist_t node = plist_find_node_by_string($self->node, s);
if (node) {
return allocate_plist_wrapper(node, 1);
}
return NULL;
}
PListNode* get_array_nth_el(unsigned int n) {
plist_t node = plist_get_array_nth_el($self->node, n);
if (node) {
return allocate_plist_wrapper(node, 1);
}
return NULL;
}
PListNode* get_dict_el_from_key(char *key) {
plist_t node = plist_get_dict_el_from_key($self->node, key);
if (node) {
return allocate_plist_wrapper(node, 1);
}
return NULL;
}
%newobject to_xml;
char* to_xml () {
char* s = NULL;
uint32_t l;
plist_to_xml($self->node, &s, &l);
return s;
}
%cstring_output_allocate_size(char **STRING, uint64_t *LENGTH, free(*$1));
void to_bin(char **STRING, uint64_t *LENGTH) {
char* s = NULL;
uint32_t l;
plist_to_bin($self->node, &s, &l);
*STRING = s;
*LENGTH = l;
return;
}
%cstring_input_binary(char *data, uint64_t len);
void from_xml (char *data, uint64_t len) {
if (!$self->should_keep_plist) {
plist_free($self->node);
}
$self->node = NULL;
$self->should_keep_plist = 0;
plist_from_xml(data, len, &$self->node);
}
%cstring_input_binary(char *data, uint64_t len);
void from_bin (char* data, uint64_t len) {
if (!$self->should_keep_plist) {
plist_free($self->node);
}
$self->node = NULL;
$self->should_keep_plist = 0;
plist_from_bin(data, len, &$self->node);
}
};
|