-
Notifications
You must be signed in to change notification settings - Fork 41
/
Plist.hpp
132 lines (116 loc) · 4.36 KB
/
Plist.hpp
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
//
// PlistCpp Property List (plist) serialization and parsing library.
//
// https://github.com/animetrics/PlistCpp
//
// Copyright (c) 2011 Animetrics Inc. (marc@animetrics.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef __PLIST_H__
#define __PLIST_H__
#include <boost/any.hpp>
#include <boost/cstdint.hpp>
#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include "PlistDate.hpp"
namespace Plist
{
// Plist value types and their corresponding c++ types
typedef std::string string_type;
typedef int64_t integer_type;
typedef double real_type;
typedef std::map<std::string, boost::any> dictionary_type;
typedef std::vector<boost::any> array_type;
typedef Date date_type;
typedef std::vector<char> data_type;
typedef bool boolean_type;
// Public read methods. Plist type (binary or xml) automatically detected.
void readPlist(const char* byteArrayTemp, int64_t size, boost::any& message);
void readPlist(std::istream& stream, boost::any& message);
template<typename T>
void readPlist(const char* byteArray, int64_t size, T& message);
template<typename T>
void readPlist(std::istream& stream, T& message);
template<typename T>
void readPlist(const char* filename, T& message);
#if defined(_MSC_VER)
template<typename T>
void readPlist(const wchar_t* filename, T& message);
#endif
// Public binary write methods.
void writePlistBinary(std::ostream& stream, const boost::any& message);
void writePlistBinary(std::vector<char>& plist, const boost::any& message);
void writePlistBinary(const char* filename, const boost::any& message);
#if defined(_MSC_VER)
void writePlistBinary(const wchar_t* filename, const boost::any& message);
#endif
// Public XML write methods.
void writePlistXML(std::ostream& stream, const boost::any& message);
void writePlistXML(std::vector<char>& plist, const boost::any& message);
void writePlistXML(const char* filename, const boost::any& message);
#if defined(_MSC_VER)
void writePlistXML(const wchar_t* filename, const boost::any& message);
#endif
class Error: public std::runtime_error {
public:
#if __cplusplus >= 201103L
using std::runtime_error::runtime_error;
#else
inline Error(const std::string& what)
: runtime_error(what) { }
#endif
};
};
#if defined(_MSC_VER)
template <typename T>
void Plist::readPlist(const wchar_t* filename, T& message)
{
std::ifstream stream(filename, std::ios::binary);
if(!stream)
throw Error("Can't open file.");
readPlist(stream, message);
}
#endif
template <typename T>
void Plist::readPlist(const char* filename, T& message)
{
std::ifstream stream(filename, std::ios::binary);
if(!stream)
throw Error("Can't open file.");
readPlist(stream, message);
}
template <typename T>
void Plist::readPlist(const char* byteArrayTemp, int64_t size, T& message)
{
boost::any tmp_message;
readPlist(byteArrayTemp, size, tmp_message);
message = boost::any_cast<T>(tmp_message);
}
template <typename T>
void Plist::readPlist(std::istream& stream, T& message)
{
boost::any tmp_message;
readPlist(stream, tmp_message);
message = boost::any_cast<T>(tmp_message);
}
#endif