diff options
| -rw-r--r-- | swig/plist.i | 304 |
1 files changed, 13 insertions, 291 deletions
diff --git a/swig/plist.i b/swig/plist.i index 2d5dfa7..5e3f75a 100644 --- a/swig/plist.i +++ b/swig/plist.i | |||
| @@ -3,24 +3,7 @@ | |||
| 3 | %feature("autodoc", "1"); | 3 | %feature("autodoc", "1"); |
| 4 | %{ | 4 | %{ |
| 5 | /* Includes the header in the wrapper code */ | 5 | /* Includes the header in the wrapper code */ |
| 6 | #include <plist/plist.h> | ||
| 7 | #include <plist/plist++.h> | 6 | #include <plist/plist++.h> |
| 8 | |||
| 9 | typedef struct { | ||
| 10 | plist_t node; | ||
| 11 | char should_keep_plist; | ||
| 12 | } PListNode; | ||
| 13 | |||
| 14 | PListNode *allocate_plist_wrapper(plist_t plist, char should_keep_plist) { | ||
| 15 | PListNode* wrapper = (PListNode*) malloc(sizeof(PListNode)); | ||
| 16 | if (wrapper) { | ||
| 17 | memset(wrapper, 0, sizeof(PListNode)); | ||
| 18 | wrapper->node = plist; | ||
| 19 | wrapper->should_keep_plist = should_keep_plist; | ||
| 20 | return wrapper; | ||
| 21 | } | ||
| 22 | return NULL; | ||
| 23 | } | ||
| 24 | %} | 7 | %} |
| 25 | 8 | ||
| 26 | %include "std_string.i" | 9 | %include "std_string.i" |
| @@ -178,6 +161,19 @@ namespace std { | |||
| 178 | %include <plist/Dictionary.h> | 161 | %include <plist/Dictionary.h> |
| 179 | %include <plist/Utils.h> | 162 | %include <plist/Utils.h> |
| 180 | 163 | ||
| 164 | typedef enum { | ||
| 165 | PLIST_BOOLEAN, | ||
| 166 | PLIST_UINT, | ||
| 167 | PLIST_REAL, | ||
| 168 | PLIST_STRING, | ||
| 169 | PLIST_ARRAY, | ||
| 170 | PLIST_DICT, | ||
| 171 | PLIST_DATE, | ||
| 172 | PLIST_DATA, | ||
| 173 | PLIST_KEY, | ||
| 174 | PLIST_NONE | ||
| 175 | } plist_type; | ||
| 176 | |||
| 181 | #if SWIGPYTHON | 177 | #if SWIGPYTHON |
| 182 | 178 | ||
| 183 | #if SWIG_VERSION <= 0x010336 | 179 | #if SWIG_VERSION <= 0x010336 |
| @@ -275,277 +271,3 @@ namespace std { | |||
| 275 | 271 | ||
| 276 | #undef SwigPyIterator | 272 | #undef SwigPyIterator |
| 277 | #endif | 273 | #endif |
| 278 | |||
| 279 | |||
| 280 | //deprecated wrapper below | ||
| 281 | |||
| 282 | %include "stdint.i" | ||
| 283 | %include "cstring.i" | ||
| 284 | |||
| 285 | /* Parse the header file to generate wrappers */ | ||
| 286 | typedef enum { | ||
| 287 | PLIST_BOOLEAN, | ||
| 288 | PLIST_UINT, | ||
| 289 | PLIST_REAL, | ||
| 290 | PLIST_STRING, | ||
| 291 | PLIST_ARRAY, | ||
| 292 | PLIST_DICT, | ||
| 293 | PLIST_DATE, | ||
| 294 | PLIST_DATA, | ||
| 295 | PLIST_KEY, | ||
| 296 | PLIST_NONE | ||
| 297 | } plist_type; | ||
| 298 | |||
| 299 | typedef struct { | ||
| 300 | } PListNode; | ||
| 301 | |||
| 302 | %extend PListNode { // Attach these functions to struct Vector | ||
| 303 | PListNode(plist_type t) { | ||
| 304 | PListNode* node = NULL; | ||
| 305 | switch (t) { | ||
| 306 | case PLIST_ARRAY : | ||
| 307 | node = allocate_plist_wrapper( plist_new_array(), 0 ); | ||
| 308 | break; | ||
| 309 | case PLIST_DICT : | ||
| 310 | node = allocate_plist_wrapper( plist_new_dict(), 0 ); | ||
| 311 | break; | ||
| 312 | default : | ||
| 313 | node = NULL; | ||
| 314 | break; | ||
| 315 | } | ||
| 316 | return node; | ||
| 317 | } | ||
| 318 | |||
| 319 | %cstring_input_binary(char *data, uint64_t len); | ||
| 320 | PListNode(char *data, uint64_t len) { | ||
| 321 | //first check input | ||
| 322 | if (len > 8) { | ||
| 323 | plist_t plist = NULL; | ||
| 324 | if (memcmp(data, "bplist00", 8) == 0) { | ||
| 325 | plist_from_bin(data, len, &plist); | ||
| 326 | } else { | ||
| 327 | plist_from_xml(data, len, &plist); | ||
| 328 | } | ||
| 329 | if (plist) | ||
| 330 | return allocate_plist_wrapper( plist, 0 ); | ||
| 331 | } | ||
| 332 | return NULL; | ||
| 333 | } | ||
| 334 | |||
| 335 | ~PListNode() { | ||
| 336 | if (!$self->should_keep_plist) { | ||
| 337 | plist_free($self->node); | ||
| 338 | } | ||
| 339 | $self->node = NULL; | ||
| 340 | $self->should_keep_plist = 0; | ||
| 341 | free($self); | ||
| 342 | $self = NULL; | ||
| 343 | } | ||
| 344 | |||
| 345 | void add_sub_node(PListNode* subnode) { | ||
| 346 | if (subnode) { | ||
| 347 | plist_add_sub_node($self->node, subnode->node); | ||
| 348 | //subnode is not root anymore. Do not delete tree | ||
| 349 | subnode->should_keep_plist = 1; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | void add_sub_key(char* k) { | ||
| 354 | plist_add_sub_key_el($self->node, k); | ||
| 355 | } | ||
| 356 | |||
| 357 | void add_sub_string(char* s) { | ||
| 358 | plist_add_sub_string_el($self->node, s); | ||
| 359 | } | ||
| 360 | |||
| 361 | void add_sub_bool(char b) { | ||
| 362 | plist_add_sub_bool_el($self->node, b); | ||
| 363 | } | ||
| 364 | |||
| 365 | void add_sub_uint(uint64_t i) { | ||
| 366 | plist_add_sub_uint_el($self->node, i); | ||
| 367 | } | ||
| 368 | |||
| 369 | void add_sub_real(double d) { | ||
| 370 | plist_add_sub_real_el($self->node, d); | ||
| 371 | } | ||
| 372 | |||
| 373 | %cstring_input_binary(char *data, uint64_t len); | ||
| 374 | void add_sub_data(char *data, uint64_t len) { | ||
| 375 | plist_add_sub_data_el($self->node, data, len); | ||
| 376 | } | ||
| 377 | |||
| 378 | void set_as_key(char* k) { | ||
| 379 | plist_set_key_val($self->node, k); | ||
| 380 | } | ||
| 381 | |||
| 382 | void set_as_string(char* s) { | ||
| 383 | plist_set_string_val($self->node, s); | ||
| 384 | } | ||
| 385 | |||
| 386 | void set_as_bool(char b) { | ||
| 387 | plist_set_bool_val($self->node, b); | ||
| 388 | } | ||
| 389 | |||
| 390 | void set_as_uint(uint64_t i) { | ||
| 391 | plist_set_uint_val($self->node, i); | ||
| 392 | } | ||
| 393 | |||
| 394 | void set_as_real(double d) { | ||
| 395 | plist_set_real_val($self->node, d); | ||
| 396 | } | ||
| 397 | |||
| 398 | %cstring_input_binary(char *data, uint64_t len); | ||
| 399 | void set_as_data(char *data, uint64_t len) { | ||
| 400 | plist_set_data_val($self->node, data, len); | ||
| 401 | } | ||
| 402 | |||
| 403 | PListNode* get_first_child() { | ||
| 404 | plist_t node = plist_get_first_child( $self->node ); | ||
| 405 | if (node) { | ||
| 406 | return allocate_plist_wrapper(node, 1); | ||
| 407 | } | ||
| 408 | return NULL; | ||
| 409 | } | ||
| 410 | |||
| 411 | PListNode* get_next_sibling() { | ||
| 412 | plist_t node = plist_get_next_sibling( $self->node ); | ||
| 413 | if (node) { | ||
| 414 | return allocate_plist_wrapper(node, 1); | ||
| 415 | } | ||
| 416 | return NULL; | ||
| 417 | } | ||
| 418 | |||
| 419 | PListNode* get_prev_sibling() { | ||
| 420 | plist_t node = plist_get_prev_sibling( $self->node ); | ||
| 421 | if (node) { | ||
| 422 | return allocate_plist_wrapper(node, 1); | ||
| 423 | } | ||
| 424 | return NULL; | ||
| 425 | } | ||
| 426 | |||
| 427 | PListNode* get_parent() { | ||
| 428 | plist_t node = plist_get_parent( $self->node ); | ||
| 429 | if (node) { | ||
| 430 | return allocate_plist_wrapper(node, 1); | ||
| 431 | } | ||
| 432 | return NULL; | ||
| 433 | } | ||
| 434 | |||
| 435 | %newobject as_key; | ||
| 436 | char* as_key() { | ||
| 437 | char* k = NULL; | ||
| 438 | plist_get_key_val($self->node, &k); | ||
| 439 | return k; | ||
| 440 | } | ||
| 441 | |||
| 442 | %newobject as_string; | ||
| 443 | char* as_string() { | ||
| 444 | char* s = NULL; | ||
| 445 | plist_get_string_val($self->node, &s); | ||
| 446 | return s; | ||
| 447 | } | ||
| 448 | |||
| 449 | char as_bool() { | ||
| 450 | uint8_t b; | ||
| 451 | plist_get_bool_val($self->node, &b); | ||
| 452 | return (char)b; | ||
| 453 | } | ||
| 454 | |||
| 455 | uint64_t as_uint() { | ||
| 456 | uint64_t i = 0; | ||
| 457 | plist_get_uint_val($self->node, &i); | ||
| 458 | return i; | ||
| 459 | } | ||
| 460 | |||
| 461 | double as_real() { | ||
| 462 | double d = 0; | ||
| 463 | plist_get_real_val($self->node, &d); | ||
| 464 | return d; | ||
| 465 | } | ||
| 466 | |||
| 467 | %cstring_output_allocate_size(char **STRING, uint64_t *LENGTH, free(*$1)); | ||
| 468 | void as_data(char **STRING, uint64_t *LENGTH) { | ||
| 469 | char* s = NULL; | ||
| 470 | uint64_t l; | ||
| 471 | plist_get_data_val($self->node, &s, &l); | ||
| 472 | *STRING = s; | ||
| 473 | *LENGTH = l; | ||
| 474 | return; | ||
| 475 | } | ||
| 476 | |||
| 477 | plist_type get_type() { | ||
| 478 | return plist_get_node_type($self->node); | ||
| 479 | } | ||
| 480 | |||
| 481 | PListNode* find_node_by_key(char *s) { | ||
| 482 | plist_t node = plist_find_node_by_key($self->node, s); | ||
| 483 | if (node) { | ||
| 484 | return allocate_plist_wrapper(node, 1); | ||
| 485 | } | ||
| 486 | return NULL; | ||
| 487 | } | ||
| 488 | |||
| 489 | PListNode* find_node_by_string(char* s) { | ||
| 490 | plist_t node = plist_find_node_by_string($self->node, s); | ||
| 491 | if (node) { | ||
| 492 | return allocate_plist_wrapper(node, 1); | ||
| 493 | } | ||
| 494 | return NULL; | ||
| 495 | } | ||
| 496 | |||
| 497 | PListNode* get_array_nth_el(unsigned int n) { | ||
| 498 | plist_t node = plist_get_array_nth_el($self->node, n); | ||
| 499 | if (node) { | ||
| 500 | return allocate_plist_wrapper(node, 1); | ||
| 501 | } | ||
| 502 | return NULL; | ||
| 503 | } | ||
| 504 | |||
| 505 | PListNode* get_dict_el_from_key(char *key) { | ||
| 506 | plist_t node = plist_get_dict_el_from_key($self->node, key); | ||
| 507 | if (node) { | ||
| 508 | return allocate_plist_wrapper(node, 1); | ||
| 509 | } | ||
| 510 | return NULL; | ||
| 511 | } | ||
| 512 | |||
| 513 | %newobject to_xml; | ||
| 514 | char* to_xml () { | ||
| 515 | char* s = NULL; | ||
| 516 | uint32_t l; | ||
| 517 | plist_to_xml($self->node, &s, &l); | ||
| 518 | return s; | ||
| 519 | } | ||
| 520 | |||
| 521 | %cstring_output_allocate_size(char **STRING, uint64_t *LENGTH, free(*$1)); | ||
| 522 | void to_bin(char **STRING, uint64_t *LENGTH) { | ||
| 523 | char* s = NULL; | ||
| 524 | uint32_t l; | ||
| 525 | plist_to_bin($self->node, &s, &l); | ||
| 526 | *STRING = s; | ||
| 527 | *LENGTH = l; | ||
| 528 | return; | ||
| 529 | } | ||
| 530 | |||
| 531 | %cstring_input_binary(char *data, uint64_t len); | ||
| 532 | void from_xml (char *data, uint64_t len) { | ||
| 533 | if (!$self->should_keep_plist) { | ||
| 534 | plist_free($self->node); | ||
| 535 | } | ||
| 536 | $self->node = NULL; | ||
| 537 | $self->should_keep_plist = 0; | ||
| 538 | plist_from_xml(data, len, &$self->node); | ||
| 539 | } | ||
| 540 | |||
| 541 | %cstring_input_binary(char *data, uint64_t len); | ||
| 542 | void from_bin (char* data, uint64_t len) { | ||
| 543 | if (!$self->should_keep_plist) { | ||
| 544 | plist_free($self->node); | ||
| 545 | } | ||
| 546 | $self->node = NULL; | ||
| 547 | $self->should_keep_plist = 0; | ||
| 548 | plist_from_bin(data, len, &$self->node); | ||
| 549 | } | ||
| 550 | }; | ||
| 551 | |||
