diff options
| author | 2023-01-16 04:25:52 +0100 | |
|---|---|---|
| committer | 2023-01-16 04:25:52 +0100 | |
| commit | d886885b0ec2506fa2caf0986a3d0e496fea91c2 (patch) | |
| tree | 58bc4bcd1963ea885abd60a65bf87a2685526714 /include/plist | |
| parent | 47a7fbe438ee7350a2b151e007f07043ef596775 (diff) | |
| download | libplist-d886885b0ec2506fa2caf0986a3d0e496fea91c2.tar.gz libplist-d886885b0ec2506fa2caf0986a3d0e496fea91c2.tar.bz2 | |
Rename PLIST_UINT to PLIST_INT and add plist_new_int() and plist_get_int_val()
This properly supports getting and setting signed or unsigned integer values.
Also, a new helper function plist_int_val_is_negative() was added to determine if
a given #PLIST_INT node has a negative value or not.
The old type PLIST_UINT is defined as a macro with the value of PLIST_INT for
backwards compatibility.
This commit also adds int vs. uint support to the C++ interface, and the python
bindings in a hopefully useful way.
Diffstat (limited to 'include/plist')
| -rw-r--r-- | include/plist/Integer.h | 8 | ||||
| -rw-r--r-- | include/plist/plist.h | 79 |
2 files changed, 74 insertions, 13 deletions
diff --git a/include/plist/Integer.h b/include/plist/Integer.h index bdabc6f..1a4d980 100644 --- a/include/plist/Integer.h +++ b/include/plist/Integer.h | |||
| @@ -35,12 +35,18 @@ public : | |||
| 35 | Integer(const Integer& i); | 35 | Integer(const Integer& i); |
| 36 | Integer& operator=(const Integer& i); | 36 | Integer& operator=(const Integer& i); |
| 37 | Integer(uint64_t i); | 37 | Integer(uint64_t i); |
| 38 | Integer(int64_t i); | ||
| 38 | virtual ~Integer(); | 39 | virtual ~Integer(); |
| 39 | 40 | ||
| 40 | Node* Clone() const; | 41 | Node* Clone() const; |
| 41 | 42 | ||
| 43 | void SetValue(int64_t i); | ||
| 42 | void SetValue(uint64_t i); | 44 | void SetValue(uint64_t i); |
| 43 | uint64_t GetValue() const; | 45 | void SetUnsignedValue(uint64_t i); |
| 46 | int64_t GetValue() const; | ||
| 47 | uint64_t GetUnsignedValue() const; | ||
| 48 | |||
| 49 | bool isNegative() const; | ||
| 44 | }; | 50 | }; |
| 45 | 51 | ||
| 46 | }; | 52 | }; |
diff --git a/include/plist/plist.h b/include/plist/plist.h index 0ae8889..2bb947f 100644 --- a/include/plist/plist.h +++ b/include/plist/plist.h | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | * @brief Main include of libplist | 3 | * @brief Main include of libplist |
| 4 | * \internal | 4 | * \internal |
| 5 | * | 5 | * |
| 6 | * Copyright (c) 2012-2019 Nikias Bassen, All Rights Reserved. | 6 | * Copyright (c) 2012-2023 Nikias Bassen, All Rights Reserved. |
| 7 | * Copyright (c) 2008-2009 Jonathan Beck, All Rights Reserved. | 7 | * Copyright (c) 2008-2009 Jonathan Beck, All Rights Reserved. |
| 8 | * | 8 | * |
| 9 | * This library is free software; you can redistribute it and/or | 9 | * This library is free software; you can redistribute it and/or |
| @@ -104,7 +104,7 @@ extern "C" | |||
| 104 | typedef enum | 104 | typedef enum |
| 105 | { | 105 | { |
| 106 | PLIST_BOOLEAN, /**< Boolean, scalar type */ | 106 | PLIST_BOOLEAN, /**< Boolean, scalar type */ |
| 107 | PLIST_UINT, /**< Unsigned integer, scalar type */ | 107 | PLIST_INT, /**< Integer, scalar type */ |
| 108 | PLIST_REAL, /**< Real, scalar type */ | 108 | PLIST_REAL, /**< Real, scalar type */ |
| 109 | PLIST_STRING, /**< ASCII string, scalar type */ | 109 | PLIST_STRING, /**< ASCII string, scalar type */ |
| 110 | PLIST_ARRAY, /**< Ordered array, structured type */ | 110 | PLIST_ARRAY, /**< Ordered array, structured type */ |
| @@ -117,6 +117,9 @@ extern "C" | |||
| 117 | PLIST_NONE /**< No type */ | 117 | PLIST_NONE /**< No type */ |
| 118 | } plist_type; | 118 | } plist_type; |
| 119 | 119 | ||
| 120 | /* for backwards compatibility */ | ||
| 121 | #define PLIST_UINT PLIST_INT | ||
| 122 | |||
| 120 | /** | 123 | /** |
| 121 | * libplist error values | 124 | * libplist error values |
| 122 | */ | 125 | */ |
| @@ -171,15 +174,28 @@ extern "C" | |||
| 171 | plist_t plist_new_bool(uint8_t val); | 174 | plist_t plist_new_bool(uint8_t val); |
| 172 | 175 | ||
| 173 | /** | 176 | /** |
| 174 | * Create a new plist_t type #PLIST_UINT | 177 | * Create a new plist_t type #PLIST_INT with an unsigned integer value |
| 175 | * | 178 | * |
| 176 | * @param val the unsigned integer value | 179 | * @param val the unsigned integer value |
| 177 | * @return the created item | 180 | * @return the created item |
| 178 | * @sa #plist_type | 181 | * @sa #plist_type |
| 182 | * @note The value is always stored as uint64_t internally. | ||
| 183 | * Use #plist_get_uint_val or #plist_get_int_val to get the unsigned or signed value. | ||
| 179 | */ | 184 | */ |
| 180 | plist_t plist_new_uint(uint64_t val); | 185 | plist_t plist_new_uint(uint64_t val); |
| 181 | 186 | ||
| 182 | /** | 187 | /** |
| 188 | * Create a new plist_t type #PLIST_INT with a signed integer value | ||
| 189 | * | ||
| 190 | * @param val the signed integer value | ||
| 191 | * @return the created item | ||
| 192 | * @sa #plist_type | ||
| 193 | * @note The value is always stored as uint64_t internally. | ||
| 194 | * Use #plist_get_uint_val or #plist_get_int_val to get the unsigned or signed value. | ||
| 195 | */ | ||
| 196 | plist_t plist_new_int(int64_t val); | ||
| 197 | |||
| 198 | /** | ||
| 183 | * Create a new plist_t type #PLIST_REAL | 199 | * Create a new plist_t type #PLIST_REAL |
| 184 | * | 200 | * |
| 185 | * @param val the real value | 201 | * @param val the real value |
| @@ -509,8 +525,8 @@ extern "C" | |||
| 509 | void plist_get_bool_val(plist_t node, uint8_t * val); | 525 | void plist_get_bool_val(plist_t node, uint8_t * val); |
| 510 | 526 | ||
| 511 | /** | 527 | /** |
| 512 | * Get the value of a #PLIST_UINT node. | 528 | * Get the unsigned integer value of a #PLIST_INT node. |
| 513 | * This function does nothing if node is not of type #PLIST_UINT | 529 | * This function does nothing if node is not of type #PLIST_INT |
| 514 | * | 530 | * |
| 515 | * @param node the node | 531 | * @param node the node |
| 516 | * @param val a pointer to a uint64_t variable. | 532 | * @param val a pointer to a uint64_t variable. |
| @@ -518,6 +534,15 @@ extern "C" | |||
| 518 | void plist_get_uint_val(plist_t node, uint64_t * val); | 534 | void plist_get_uint_val(plist_t node, uint64_t * val); |
| 519 | 535 | ||
| 520 | /** | 536 | /** |
| 537 | * Get the signed integer value of a #PLIST_INT node. | ||
| 538 | * This function does nothing if node is not of type #PLIST_INT | ||
| 539 | * | ||
| 540 | * @param node the node | ||
| 541 | * @param val a pointer to a int64_t variable. | ||
| 542 | */ | ||
| 543 | void plist_get_int_val(plist_t node, int64_t * val); | ||
| 544 | |||
| 545 | /** | ||
| 521 | * Get the value of a #PLIST_REAL node. | 546 | * Get the value of a #PLIST_REAL node. |
| 522 | * This function does nothing if node is not of type #PLIST_REAL | 547 | * This function does nothing if node is not of type #PLIST_REAL |
| 523 | * | 548 | * |
| @@ -607,7 +632,7 @@ extern "C" | |||
| 607 | 632 | ||
| 608 | /** | 633 | /** |
| 609 | * Set the value of a node. | 634 | * Set the value of a node. |
| 610 | * Forces type of node to #PLIST_UINT | 635 | * Forces type of node to #PLIST_INT |
| 611 | * | 636 | * |
| 612 | * @param node the node | 637 | * @param node the node |
| 613 | * @param val the unsigned integer value | 638 | * @param val the unsigned integer value |
| @@ -616,6 +641,15 @@ extern "C" | |||
| 616 | 641 | ||
| 617 | /** | 642 | /** |
| 618 | * Set the value of a node. | 643 | * Set the value of a node. |
| 644 | * Forces type of node to #PLIST_INT | ||
| 645 | * | ||
| 646 | * @param node the node | ||
| 647 | * @param val the signed integer value | ||
| 648 | */ | ||
| 649 | void plist_set_int_val(plist_t node, int64_t val); | ||
| 650 | |||
| 651 | /** | ||
| 652 | * Set the value of a node. | ||
| 619 | * Forces type of node to #PLIST_REAL | 653 | * Forces type of node to #PLIST_REAL |
| 620 | * | 654 | * |
| 621 | * @param node the node | 655 | * @param node the node |
| @@ -823,7 +857,7 @@ extern "C" | |||
| 823 | 857 | ||
| 824 | /* Helper macros for the different plist types */ | 858 | /* Helper macros for the different plist types */ |
| 825 | #define PLIST_IS_BOOLEAN(__plist) _PLIST_IS_TYPE(__plist, BOOLEAN) | 859 | #define PLIST_IS_BOOLEAN(__plist) _PLIST_IS_TYPE(__plist, BOOLEAN) |
| 826 | #define PLIST_IS_UINT(__plist) _PLIST_IS_TYPE(__plist, UINT) | 860 | #define PLIST_IS_INT(__plist) _PLIST_IS_TYPE(__plist, INT) |
| 827 | #define PLIST_IS_REAL(__plist) _PLIST_IS_TYPE(__plist, REAL) | 861 | #define PLIST_IS_REAL(__plist) _PLIST_IS_TYPE(__plist, REAL) |
| 828 | #define PLIST_IS_STRING(__plist) _PLIST_IS_TYPE(__plist, STRING) | 862 | #define PLIST_IS_STRING(__plist) _PLIST_IS_TYPE(__plist, STRING) |
| 829 | #define PLIST_IS_ARRAY(__plist) _PLIST_IS_TYPE(__plist, ARRAY) | 863 | #define PLIST_IS_ARRAY(__plist) _PLIST_IS_TYPE(__plist, ARRAY) |
| @@ -832,21 +866,42 @@ extern "C" | |||
| 832 | #define PLIST_IS_DATA(__plist) _PLIST_IS_TYPE(__plist, DATA) | 866 | #define PLIST_IS_DATA(__plist) _PLIST_IS_TYPE(__plist, DATA) |
| 833 | #define PLIST_IS_KEY(__plist) _PLIST_IS_TYPE(__plist, KEY) | 867 | #define PLIST_IS_KEY(__plist) _PLIST_IS_TYPE(__plist, KEY) |
| 834 | #define PLIST_IS_UID(__plist) _PLIST_IS_TYPE(__plist, UID) | 868 | #define PLIST_IS_UID(__plist) _PLIST_IS_TYPE(__plist, UID) |
| 869 | /* for backwards compatibility */ | ||
| 870 | #define PLIST_IS_UINT PLIST_IS_INT | ||
| 835 | 871 | ||
| 836 | /** | 872 | /** |
| 837 | * Helper function to check the value of a PLIST_BOOL node. | 873 | * Helper function to check the value of a PLIST_BOOL node. |
| 838 | * | 874 | * |
| 839 | * @param boolnode node of type PLIST_BOOL | 875 | * @param boolnode node of type PLIST_BOOL |
| 840 | * @return 1 if the boolean node has a value of TRUE, 0 if FALSE, | 876 | * @return 1 if the boolean node has a value of TRUE or 0 if FALSE. |
| 841 | * or -1 if the node is not of type PLIST_BOOL | ||
| 842 | */ | 877 | */ |
| 843 | int plist_bool_val_is_true(plist_t boolnode); | 878 | int plist_bool_val_is_true(plist_t boolnode); |
| 844 | 879 | ||
| 845 | /** | 880 | /** |
| 846 | * Helper function to compare the value of a PLIST_UINT node against | 881 | * Helper function to test if a given #PLIST_INT node's value is negative |
| 847 | * a given value. | 882 | * |
| 883 | * @param intnode node of type PLIST_INT | ||
| 884 | * @return 1 if the node's value is negative, or 0 if positive. | ||
| 885 | */ | ||
| 886 | int plist_int_val_is_negative(plist_t intnode); | ||
| 887 | |||
| 888 | /** | ||
| 889 | * Helper function to compare the value of a PLIST_INT node against | ||
| 890 | * a given signed integer value. | ||
| 891 | * | ||
| 892 | * @param uintnode node of type PLIST_INT | ||
| 893 | * @param cmpval value to compare against | ||
| 894 | * @return 0 if the node's value and cmpval are equal, | ||
| 895 | * 1 if the node's value is greater than cmpval, | ||
| 896 | * or -1 if the node's value is less than cmpval. | ||
| 897 | */ | ||
| 898 | int plist_int_val_compare(plist_t uintnode, int64_t cmpval); | ||
| 899 | |||
| 900 | /** | ||
| 901 | * Helper function to compare the value of a PLIST_INT node against | ||
| 902 | * a given unsigned integer value. | ||
| 848 | * | 903 | * |
| 849 | * @param uintnode node of type PLIST_UINT | 904 | * @param uintnode node of type PLIST_INT |
| 850 | * @param cmpval value to compare against | 905 | * @param cmpval value to compare against |
| 851 | * @return 0 if the node's value and cmpval are equal, | 906 | * @return 0 if the node's value and cmpval are equal, |
| 852 | * 1 if the node's value is greater than cmpval, | 907 | * 1 if the node's value is greater than cmpval, |
