This repository has been archived by the owner on Sep 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
/
XBMCTinyXML.cpp
161 lines (142 loc) · 4.4 KB
/
XBMCTinyXML.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (C) 2005-2011 Team XBMC
* http://www.xbmc.org
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "XBMCTinyXML.h"
#include "filesystem/File.h"
#include "RegExp.h"
#define MAX_ENTITY_LENGTH 8 // size of largest entity "&#xNNNN;"
#define BUFFER_SIZE 4096
CXBMCTinyXML::CXBMCTinyXML()
: TiXmlDocument()
{
}
CXBMCTinyXML::CXBMCTinyXML(const char *documentName)
: TiXmlDocument()
{
LoadFile(documentName);
}
CXBMCTinyXML::CXBMCTinyXML(const CStdString &documentName)
: TiXmlDocument()
{
LoadFile(documentName);
}
bool CXBMCTinyXML::LoadFile(TiXmlEncoding encoding)
{
return TiXmlDocument::LoadFile(encoding);
}
bool CXBMCTinyXML::LoadFile(const char *_filename, TiXmlEncoding encoding)
{
CStdString filename(_filename);
return LoadFile(filename, encoding);
}
bool CXBMCTinyXML::LoadFile(const CStdString &_filename, TiXmlEncoding encoding)
{
// There was a really terrifying little bug here. The code:
// value = filename
// in the STL case, cause the assignment method of the std::string to
// be called. What is strange, is that the std::string had the same
// address as it's c_str() method, and so bad things happen. Looks
// like a bug in the Microsoft STL implementation.
// Add an extra string to avoid the crash.
CStdString filename(_filename);
value = filename;
XFILE::CFileStream file;
if (!file.Open(value))
{
SetError(TIXML_ERROR_OPENING_FILE, NULL, NULL, TIXML_ENCODING_UNKNOWN);
return false;
}
// Delete the existing data:
Clear();
location.Clear();
CStdString data;
data.reserve(8 * 1000);
StreamIn(&file, &data);
file.Close();
Parse(data, NULL, encoding);
if (Error())
return false;
return true;
}
bool CXBMCTinyXML::LoadFile(FILE *f, TiXmlEncoding encoding)
{
CStdString data("");
char buf[BUFFER_SIZE];
int result;
while ((result = fread(buf, 1, BUFFER_SIZE, f)) > 0)
data.append(buf, result);
return Parse(data, NULL, encoding) != NULL;
}
bool CXBMCTinyXML::SaveFile(const char *_filename) const
{
CStdString filename(_filename);
return SaveFile(filename);
}
bool CXBMCTinyXML::SaveFile(const CStdString &filename) const
{
XFILE::CFile file;
if (file.OpenForWrite(filename, true))
{
TiXmlPrinter printer;
Accept(&printer);
file.Write(printer.CStr(), printer.Size());
return true;
}
return false;
}
const char *CXBMCTinyXML::Parse(const char *_data, TiXmlParsingData *prevData, TiXmlEncoding encoding)
{
CStdString data(_data);
return Parse(data, prevData, encoding);
}
const char *CXBMCTinyXML::Parse(CStdString &data, TiXmlParsingData *prevData, TiXmlEncoding encoding)
{
// Preprocess string, replacing '&' with '& for invalid XML entities
size_t pos = 0;
CRegExp re(true);
re.RegComp("^&(amp|lt|gt|quot|apos|#x[a-fA-F0-9]{1,4}|#[0-9]{1,5});.*");
while ((pos = data.find("&", pos)) != CStdString::npos)
{
CStdString tmp = data.substr(pos, pos + MAX_ENTITY_LENGTH);
if (re.RegFind(tmp) < 0)
data.insert(pos + 1, "amp;");
pos++;
}
return TiXmlDocument::Parse(data.c_str(), prevData, encoding);
}
bool CXBMCTinyXML::Test()
{
// scraper results with unescaped &
CXBMCTinyXML doc;
CStdString data("<details><url function=\"ParseTMDBRating\" "
"cache=\"tmdb-en-12244.json\">"
"http://api.themoviedb.org/3/movie/12244"
"?api_key=57983e31fb435df4df77afb854740ea9"
"&language=en???</url></details>");
doc.Parse(data.c_str());
TiXmlNode *root = doc.RootElement();
if (root && root->ValueStr() == "details")
{
TiXmlElement *url = root->FirstChildElement("url");
if (url && url->FirstChild())
{
return (url->FirstChild()->ValueStr() == "http://api.themoviedb.org/3/movie/12244?api_key=57983e31fb435df4df77afb854740ea9&language=en???");
}
}
return false;
}