plist.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**************************************************************************/
  2. /* plist.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef PLIST_H
  31. #define PLIST_H
  32. // Property list file format (application/x-plist) parser, property list ASN-1 serialization.
  33. #include "core/io/file_access.h"
  34. class PListNode;
  35. class PList : public RefCounted {
  36. friend class PListNode;
  37. public:
  38. enum PLNodeType {
  39. PL_NODE_TYPE_NIL,
  40. PL_NODE_TYPE_STRING,
  41. PL_NODE_TYPE_ARRAY,
  42. PL_NODE_TYPE_DICT,
  43. PL_NODE_TYPE_BOOLEAN,
  44. PL_NODE_TYPE_INTEGER,
  45. PL_NODE_TYPE_REAL,
  46. PL_NODE_TYPE_DATA,
  47. PL_NODE_TYPE_DATE,
  48. };
  49. private:
  50. struct PListTrailer {
  51. uint8_t offset_size;
  52. uint8_t ref_size;
  53. uint64_t object_num;
  54. uint64_t root_offset_idx;
  55. uint64_t offset_table_start;
  56. };
  57. PListTrailer trailer;
  58. Ref<PListNode> root;
  59. uint64_t read_bplist_var_size_int(Ref<FileAccess> p_file, uint8_t p_size);
  60. Ref<PListNode> read_bplist_obj(Ref<FileAccess> p_file, uint64_t p_offset_idx);
  61. public:
  62. PList();
  63. PList(const String &p_string);
  64. bool load_file(const String &p_filename);
  65. bool load_string(const String &p_string, String &r_err_out);
  66. PackedByteArray save_asn1() const;
  67. String save_text() const;
  68. Ref<PListNode> get_root();
  69. };
  70. /*************************************************************************/
  71. class PListNode : public RefCounted {
  72. static int _asn1_size_len(uint8_t p_len_octets);
  73. public:
  74. PList::PLNodeType data_type = PList::PLNodeType::PL_NODE_TYPE_NIL;
  75. CharString data_string;
  76. Vector<Ref<PListNode>> data_array;
  77. HashMap<String, Ref<PListNode>> data_dict;
  78. union {
  79. int64_t data_int;
  80. bool data_bool;
  81. double data_real;
  82. };
  83. PList::PLNodeType get_type() const;
  84. Variant get_value() const;
  85. static Ref<PListNode> new_node(const Variant &p_value);
  86. static Ref<PListNode> new_array();
  87. static Ref<PListNode> new_dict();
  88. static Ref<PListNode> new_string(const String &p_string);
  89. static Ref<PListNode> new_data(const String &p_string);
  90. static Ref<PListNode> new_date(const String &p_string);
  91. static Ref<PListNode> new_bool(bool p_bool);
  92. static Ref<PListNode> new_int(int64_t p_int);
  93. static Ref<PListNode> new_real(double p_real);
  94. bool push_subnode(const Ref<PListNode> &p_node, const String &p_key = "");
  95. size_t get_asn1_size(uint8_t p_len_octets) const;
  96. void store_asn1_size(PackedByteArray &p_stream, uint8_t p_len_octets) const;
  97. bool store_asn1(PackedByteArray &p_stream, uint8_t p_len_octets) const;
  98. void store_text(String &p_stream, uint8_t p_indent) const;
  99. PListNode() {}
  100. ~PListNode() {}
  101. };
  102. #endif // PLIST_H