Plist.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // PlistCpp Property List (plist) serialization and parsing library.
  3. //
  4. // https://github.com/animetrics/PlistCpp
  5. //
  6. // Copyright (c) 2011 Animetrics Inc. (marc@animetrics.com)
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. #ifndef __PLIST_H__
  26. #define __PLIST_H__
  27. #include <boost/any.hpp>
  28. #include <boost/cstdint.hpp>
  29. #include <string>
  30. #include <vector>
  31. #include <map>
  32. #include <iostream>
  33. #include <fstream>
  34. #include <stdexcept>
  35. #include "PlistDate.hpp"
  36. namespace Plist
  37. {
  38. // Plist value types and their corresponding c++ types
  39. typedef std::string string_type;
  40. typedef int64_t integer_type;
  41. typedef double real_type;
  42. typedef std::map<std::string, boost::any> dictionary_type;
  43. typedef std::vector<boost::any> array_type;
  44. typedef Date date_type;
  45. typedef std::vector<char> data_type;
  46. typedef bool boolean_type;
  47. // Public read methods. Plist type (binary or xml) automatically detected.
  48. void readPlist(const char* byteArrayTemp, int64_t size, boost::any& message);
  49. void readPlist(std::istream& stream, boost::any& message);
  50. template<typename T>
  51. void readPlist(const char* byteArray, int64_t size, T& message);
  52. template<typename T>
  53. void readPlist(std::istream& stream, T& message);
  54. template<typename T>
  55. void readPlist(const char* filename, T& message);
  56. #if defined(_MSC_VER)
  57. template<typename T>
  58. void readPlist(const wchar_t* filename, T& message);
  59. #endif
  60. // Public binary write methods.
  61. void writePlistBinary(std::ostream& stream, const boost::any& message);
  62. void writePlistBinary(std::vector<char>& plist, const boost::any& message);
  63. void writePlistBinary(const char* filename, const boost::any& message);
  64. #if defined(_MSC_VER)
  65. void writePlistBinary(const wchar_t* filename, const boost::any& message);
  66. #endif
  67. // Public XML write methods.
  68. void writePlistXML(std::ostream& stream, const boost::any& message);
  69. void writePlistXML(std::vector<char>& plist, const boost::any& message);
  70. void writePlistXML(const char* filename, const boost::any& message);
  71. #if defined(_MSC_VER)
  72. void writePlistXML(const wchar_t* filename, const boost::any& message);
  73. #endif
  74. class Error: public std::runtime_error {
  75. public:
  76. #if __cplusplus >= 201103L
  77. using std::runtime_error::runtime_error;
  78. #else
  79. inline Error(const std::string& what)
  80. : runtime_error(what) { }
  81. #endif
  82. };
  83. };
  84. #if defined(_MSC_VER)
  85. template <typename T>
  86. void Plist::readPlist(const wchar_t* filename, T& message)
  87. {
  88. std::ifstream stream(filename, std::ios::binary);
  89. if(!stream)
  90. throw Error("Can't open file.");
  91. readPlist(stream, message);
  92. }
  93. #endif
  94. template <typename T>
  95. void Plist::readPlist(const char* filename, T& message)
  96. {
  97. std::ifstream stream(filename, std::ios::binary);
  98. if(!stream)
  99. throw Error("Can't open file.");
  100. readPlist(stream, message);
  101. }
  102. template <typename T>
  103. void Plist::readPlist(const char* byteArrayTemp, int64_t size, T& message)
  104. {
  105. boost::any tmp_message;
  106. readPlist(byteArrayTemp, size, tmp_message);
  107. message = boost::any_cast<T>(tmp_message);
  108. }
  109. template <typename T>
  110. void Plist::readPlist(std::istream& stream, T& message)
  111. {
  112. boost::any tmp_message;
  113. readPlist(stream, tmp_message);
  114. message = boost::any_cast<T&>(tmp_message);
  115. }
  116. #endif