Merge lp:~qqworini/ubuntu-rssreader-app/add-non-google-support into lp:ubuntu-rssreader-app
- add-non-google-support
- Merge into reboot
Proposed by
Joey Chan
Status: | Merged |
---|---|
Approved by: | Roman Shchekin |
Approved revision: | 417 |
Merged at revision: | 415 |
Proposed branch: | lp:~qqworini/ubuntu-rssreader-app/add-non-google-support |
Merge into: | lp:ubuntu-rssreader-app |
Diff against target: |
14900 lines (+14343/-44) 52 files modified
shorts.apparmor (+3/-1) shorts/CMakeLists.txt (+1/-0) shorts/main.cpp (+5/-1) shorts/po/com.ubuntu.shorts.pot (+74/-34) shorts/qml/CMakeLists.txt (+1/-0) shorts/qml/components/NetworkManager.qml (+144/-2) shorts/qml/components/OptionsKeeper.qml (+19/-1) shorts/qml/nongoogle/AppendNGFeedPage.qml (+254/-0) shorts/qml/nongoogle/CMakeLists.txt (+6/-0) shorts/qml/nongoogle/Positioner.qml (+114/-0) shorts/qml/nongoogle/XmlNetwork.qml (+84/-0) shorts/qml/pages/PageSettings.qml (+62/-0) shorts/qml/shorts-app.qml (+56/-2) shorts/shorts.pro (+42/-3) shorts/shorts.qrc (+4/-0) shorts/xml2json/rapidjson/allocators.h (+245/-0) shorts/xml2json/rapidjson/document.h (+1969/-0) shorts/xml2json/rapidjson/encodedstream.h (+290/-0) shorts/xml2json/rapidjson/encodings.h (+630/-0) shorts/xml2json/rapidjson/error/en.h (+71/-0) shorts/xml2json/rapidjson/error/error.h (+150/-0) shorts/xml2json/rapidjson/filereadstream.h (+94/-0) shorts/xml2json/rapidjson/filestream.h (+73/-0) shorts/xml2json/rapidjson/filewritestream.h (+97/-0) shorts/xml2json/rapidjson/internal/biginteger.h (+294/-0) shorts/xml2json/rapidjson/internal/diyfp.h (+268/-0) shorts/xml2json/rapidjson/internal/dtoa.h (+225/-0) shorts/xml2json/rapidjson/internal/ieee754.h (+90/-0) shorts/xml2json/rapidjson/internal/itoa.h (+306/-0) shorts/xml2json/rapidjson/internal/meta.h (+189/-0) shorts/xml2json/rapidjson/internal/pow10.h (+59/-0) shorts/xml2json/rapidjson/internal/stack.h (+183/-0) shorts/xml2json/rapidjson/internal/strfunc.h (+43/-0) shorts/xml2json/rapidjson/internal/strtod.h (+285/-0) shorts/xml2json/rapidjson/memorybuffer.h (+76/-0) shorts/xml2json/rapidjson/memorystream.h (+67/-0) shorts/xml2json/rapidjson/msinttypes/inttypes.h (+312/-0) shorts/xml2json/rapidjson/msinttypes/stdint.h (+296/-0) shorts/xml2json/rapidjson/prettywriter.h (+211/-0) shorts/xml2json/rapidjson/rapidjson.h (+629/-0) shorts/xml2json/rapidjson/reader.h (+1444/-0) shorts/xml2json/rapidjson/stringbuffer.h (+99/-0) shorts/xml2json/rapidjson/writer.h (+401/-0) shorts/xml2json/rapidxml/license.txt (+52/-0) shorts/xml2json/rapidxml/manual.html (+406/-0) shorts/xml2json/rapidxml/rapidxml.hpp (+2924/-0) shorts/xml2json/rapidxml/rapidxml_iterators.hpp (+174/-0) shorts/xml2json/rapidxml/rapidxml_print.hpp (+425/-0) shorts/xml2json/rapidxml/rapidxml_utils.hpp (+122/-0) shorts/xml2json/utilities.cpp (+46/-0) shorts/xml2json/utilities.h (+24/-0) shorts/xml2json/xml2json.hpp (+205/-0) |
To merge this branch: | bzr merge lp:~qqworini/ubuntu-rssreader-app/add-non-google-support |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Roman Shchekin | Needs Fixing | ||
Jenkins Bot | continuous-integration | Approve | |
Review via email: mp+280614@code.launchpad.net |
Commit message
remove useless commented code
Description of the change
remove useless commented code
To post a comment you must log in.
Revision history for this message
Jenkins Bot (ubuntu-core-apps-jenkins-bot) wrote : | # |
review:
Approve
(continuous-integration)
Revision history for this message
Roman Shchekin (mrqtros) wrote : | # |
See inline comments.
review:
Needs Fixing
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'shorts.apparmor' | |||
2 | --- shorts.apparmor 2015-10-24 07:06:24 +0000 | |||
3 | +++ shorts.apparmor 2015-12-15 16:05:21 +0000 | |||
4 | @@ -1,7 +1,9 @@ | |||
5 | 1 | { | 1 | { |
6 | 2 | "policy_groups": [ | 2 | "policy_groups": [ |
7 | 3 | "networking", | 3 | "networking", |
9 | 4 | "content_exchange_source" | 4 | "content_exchange_source", |
10 | 5 | "content_exchange", | ||
11 | 6 | "location" | ||
12 | 5 | ], | 7 | ], |
13 | 6 | "policy_version": 1.3 | 8 | "policy_version": 1.3 |
14 | 7 | } | 9 | } |
15 | 8 | 10 | ||
16 | === modified file 'shorts/CMakeLists.txt' | |||
17 | --- shorts/CMakeLists.txt 2015-08-11 12:07:22 +0000 | |||
18 | +++ shorts/CMakeLists.txt 2015-12-15 16:05:21 +0000 | |||
19 | @@ -5,6 +5,7 @@ | |||
20 | 5 | main.cpp | 5 | main.cpp |
21 | 6 | CachingNetworkManagerFactory.cpp | 6 | CachingNetworkManagerFactory.cpp |
22 | 7 | shorts.qrc | 7 | shorts.qrc |
23 | 8 | xml2json/utilities.cpp | ||
24 | 8 | 9 | ||
25 | 9 | ) | 10 | ) |
26 | 10 | 11 | ||
27 | 11 | 12 | ||
28 | === modified file 'shorts/main.cpp' | |||
29 | --- shorts/main.cpp 2015-07-19 14:29:20 +0000 | |||
30 | +++ shorts/main.cpp 2015-12-15 16:05:21 +0000 | |||
31 | @@ -1,8 +1,9 @@ | |||
32 | 1 | #include <QGuiApplication> | 1 | #include <QGuiApplication> |
33 | 2 | #include <QQmlApplicationEngine> | 2 | #include <QQmlApplicationEngine> |
35 | 3 | #include <QQuickView> | 3 | #include <QtQuick> |
36 | 4 | 4 | ||
37 | 5 | #include "CachingNetworkManagerFactory.h" | 5 | #include "CachingNetworkManagerFactory.h" |
38 | 6 | #include "xml2json/utilities.h" | ||
39 | 6 | 7 | ||
40 | 7 | int main(int argc, char *argv[]) | 8 | int main(int argc, char *argv[]) |
41 | 8 | { | 9 | { |
42 | @@ -16,6 +17,9 @@ | |||
43 | 16 | CachingNetworkManagerFactory *managerFactory = new CachingNetworkManagerFactory(); | 17 | CachingNetworkManagerFactory *managerFactory = new CachingNetworkManagerFactory(); |
44 | 17 | view.engine()->setNetworkAccessManagerFactory(managerFactory); | 18 | view.engine()->setNetworkAccessManagerFactory(managerFactory); |
45 | 18 | 19 | ||
46 | 20 | Utilities *utilities = new Utilities(); | ||
47 | 21 | view.engine()->rootContext()->setContextProperty("utilities", utilities); | ||
48 | 22 | |||
49 | 19 | view.setSource(QUrl(QStringLiteral("qrc:///qml/shorts-app.qml"))); | 23 | view.setSource(QUrl(QStringLiteral("qrc:///qml/shorts-app.qml"))); |
50 | 20 | // view.setSource(QUrl("./share/qml/shorts-app.qml")); | 24 | // view.setSource(QUrl("./share/qml/shorts-app.qml")); |
51 | 21 | view.setResizeMode(QQuickView::SizeRootObjectToView); | 25 | view.setResizeMode(QQuickView::SizeRootObjectToView); |
52 | 22 | 26 | ||
53 | === modified file 'shorts/po/com.ubuntu.shorts.pot' | |||
54 | --- shorts/po/com.ubuntu.shorts.pot 2015-12-01 17:03:50 +0000 | |||
55 | +++ shorts/po/com.ubuntu.shorts.pot 2015-12-15 16:05:21 +0000 | |||
56 | @@ -8,7 +8,7 @@ | |||
57 | 8 | msgstr "" | 8 | msgstr "" |
58 | 9 | "Project-Id-Version: \n" | 9 | "Project-Id-Version: \n" |
59 | 10 | "Report-Msgid-Bugs-To: \n" | 10 | "Report-Msgid-Bugs-To: \n" |
61 | 11 | "POT-Creation-Date: 2015-12-01 17:27+0800\n" | 11 | "POT-Creation-Date: 2015-12-03 17:59+0800\n" |
62 | 12 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" | 12 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" |
63 | 13 | "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" | 13 | "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" |
64 | 14 | "Language-Team: LANGUAGE <LL@li.org>\n" | 14 | "Language-Team: LANGUAGE <LL@li.org>\n" |
65 | @@ -42,11 +42,41 @@ | |||
66 | 42 | msgid "Large" | 42 | msgid "Large" |
67 | 43 | msgstr "" | 43 | msgstr "" |
68 | 44 | 44 | ||
71 | 45 | #: ../qml/pages/AppendFeedPage.qml:31 ../qml/shorts-app.qml:243 | 45 | #: ../qml/nongoogle/AppendNGFeedPage.qml:30 ../qml/pages/AppendFeedPage.qml:31 |
72 | 46 | #: ../qml/shorts-app.qml:378 | 46 | #: ../qml/shorts-app.qml:255 ../qml/shorts-app.qml:390 |
73 | 47 | #: ../qml/shorts-app.qml:398 | ||
74 | 47 | msgid "Add feeds" | 48 | msgid "Add feeds" |
75 | 48 | msgstr "" | 49 | msgstr "" |
76 | 49 | 50 | ||
77 | 51 | #: ../qml/nongoogle/AppendNGFeedPage.qml:105 | ||
78 | 52 | #: ../qml/pages/AppendFeedPage.qml:137 | ||
79 | 53 | msgid "Type a keyword or URL" | ||
80 | 54 | msgstr "" | ||
81 | 55 | |||
82 | 56 | #: ../qml/nongoogle/AppendNGFeedPage.qml:162 | ||
83 | 57 | msgid "Feed Title:" | ||
84 | 58 | msgstr "" | ||
85 | 59 | |||
86 | 60 | #: ../qml/nongoogle/AppendNGFeedPage.qml:170 | ||
87 | 61 | #: ../qml/nongoogle/AppendNGFeedPage.qml:185 | ||
88 | 62 | msgid "No data" | ||
89 | 63 | msgstr "" | ||
90 | 64 | |||
91 | 65 | #: ../qml/nongoogle/AppendNGFeedPage.qml:177 | ||
92 | 66 | msgid "Feed Description:" | ||
93 | 67 | msgstr "" | ||
94 | 68 | |||
95 | 69 | #: ../qml/nongoogle/AppendNGFeedPage.qml:210 | ||
96 | 70 | #: ../qml/pages/AppendFeedPage.qml:243 ../qml/pages/CreateTopicPage.qml:38 | ||
97 | 71 | #: ../qml/pages/TopicManagement.qml:239 ../qml/shorts-app.qml:481 | ||
98 | 72 | msgid "Cancel" | ||
99 | 73 | msgstr "" | ||
100 | 74 | |||
101 | 75 | #: ../qml/nongoogle/AppendNGFeedPage.qml:243 | ||
102 | 76 | #: ../qml/pages/AppendFeedPage.qml:276 | ||
103 | 77 | msgid "Next" | ||
104 | 78 | msgstr "" | ||
105 | 79 | |||
106 | 50 | #: ../qml/pages/AppendFeedPage.qml:69 | 80 | #: ../qml/pages/AppendFeedPage.qml:69 |
107 | 51 | msgid "Failed to perform a feed search by keyword" | 81 | msgid "Failed to perform a feed search by keyword" |
108 | 52 | msgstr "" | 82 | msgstr "" |
109 | @@ -59,23 +89,10 @@ | |||
110 | 59 | msgid "Failed to perform a feed search by URL" | 89 | msgid "Failed to perform a feed search by URL" |
111 | 60 | msgstr "" | 90 | msgstr "" |
112 | 61 | 91 | ||
113 | 62 | #: ../qml/pages/AppendFeedPage.qml:137 | ||
114 | 63 | msgid "Type a keyword or URL" | ||
115 | 64 | msgstr "" | ||
116 | 65 | |||
117 | 66 | #: ../qml/pages/AppendFeedPage.qml:185 | 92 | #: ../qml/pages/AppendFeedPage.qml:185 |
118 | 67 | msgid "Search results" | 93 | msgid "Search results" |
119 | 68 | msgstr "" | 94 | msgstr "" |
120 | 69 | 95 | ||
121 | 70 | #: ../qml/pages/AppendFeedPage.qml:243 ../qml/pages/CreateTopicPage.qml:38 | ||
122 | 71 | #: ../qml/pages/TopicManagement.qml:239 ../qml/shorts-app.qml:456 | ||
123 | 72 | msgid "Cancel" | ||
124 | 73 | msgstr "" | ||
125 | 74 | |||
126 | 75 | #: ../qml/pages/AppendFeedPage.qml:276 | ||
127 | 76 | msgid "Next" | ||
128 | 77 | msgstr "" | ||
129 | 78 | |||
130 | 79 | #: ../qml/pages/AppendFeedPage.qml:305 ../qml/pages/CreateTopicPage.qml:181 | 96 | #: ../qml/pages/AppendFeedPage.qml:305 ../qml/pages/CreateTopicPage.qml:181 |
131 | 80 | msgid "No feeds" | 97 | msgid "No feeds" |
132 | 81 | msgstr "" | 98 | msgstr "" |
133 | @@ -118,6 +135,7 @@ | |||
134 | 118 | 135 | ||
135 | 119 | #: ../qml/pages/ChooseTopicPage.qml:109 ../qml/pages/ChooseTopicPage.qml:122 | 136 | #: ../qml/pages/ChooseTopicPage.qml:109 ../qml/pages/ChooseTopicPage.qml:122 |
136 | 120 | #: ../qml/pages/CreateTopicPage.qml:66 ../qml/pages/CreateTopicPage.qml:78 | 137 | #: ../qml/pages/CreateTopicPage.qml:66 ../qml/pages/CreateTopicPage.qml:78 |
137 | 138 | #: ../qml/shorts-app.qml:568 | ||
138 | 121 | msgid "Warning" | 139 | msgid "Warning" |
139 | 122 | msgstr "" | 140 | msgstr "" |
140 | 123 | 141 | ||
141 | @@ -161,7 +179,15 @@ | |||
142 | 161 | msgid "Topic: " | 179 | msgid "Topic: " |
143 | 162 | msgstr "" | 180 | msgstr "" |
144 | 163 | 181 | ||
146 | 164 | #: ../qml/pages/TopicManagement.qml:13 ../qml/shorts-app.qml:217 | 182 | #: ../qml/pages/PageSettings.qml:9 ../qml/shorts-app.qml:239 |
147 | 183 | msgid "Settings" | ||
148 | 184 | msgstr "" | ||
149 | 185 | |||
150 | 186 | #: ../qml/pages/PageSettings.qml:21 | ||
151 | 187 | msgid "Use Google Search: " | ||
152 | 188 | msgstr "" | ||
153 | 189 | |||
154 | 190 | #: ../qml/pages/TopicManagement.qml:13 ../qml/shorts-app.qml:219 | ||
155 | 165 | msgid "Edit topics" | 191 | msgid "Edit topics" |
156 | 166 | msgstr "" | 192 | msgstr "" |
157 | 167 | 193 | ||
158 | @@ -173,74 +199,88 @@ | |||
159 | 173 | msgid "Add Feed" | 199 | msgid "Add Feed" |
160 | 174 | msgstr "" | 200 | msgstr "" |
161 | 175 | 201 | ||
163 | 176 | #: ../qml/shorts-app.qml:184 | 202 | #: ../qml/shorts-app.qml:186 |
164 | 177 | msgid "Refresh" | 203 | msgid "Refresh" |
165 | 178 | msgstr "" | 204 | msgstr "" |
166 | 179 | 205 | ||
168 | 180 | #: ../qml/shorts-app.qml:191 | 206 | #: ../qml/shorts-app.qml:193 |
169 | 181 | msgid "Grid View" | 207 | msgid "Grid View" |
170 | 182 | msgstr "" | 208 | msgstr "" |
171 | 183 | 209 | ||
173 | 184 | #: ../qml/shorts-app.qml:191 | 210 | #: ../qml/shorts-app.qml:193 |
174 | 185 | msgid "List view" | 211 | msgid "List view" |
175 | 186 | msgstr "" | 212 | msgstr "" |
176 | 187 | 213 | ||
178 | 188 | #: ../qml/shorts-app.qml:227 | 214 | #: ../qml/shorts-app.qml:229 |
179 | 189 | msgid "Disable night mode" | 215 | msgid "Disable night mode" |
180 | 190 | msgstr "" | 216 | msgstr "" |
181 | 191 | 217 | ||
183 | 192 | #: ../qml/shorts-app.qml:227 | 218 | #: ../qml/shorts-app.qml:229 |
184 | 193 | msgid "Enable night mode" | 219 | msgid "Enable night mode" |
185 | 194 | msgstr "" | 220 | msgstr "" |
186 | 195 | 221 | ||
188 | 196 | #: ../qml/shorts-app.qml:309 | 222 | #: ../qml/shorts-app.qml:321 |
189 | 197 | msgid "Saved" | 223 | msgid "Saved" |
190 | 198 | msgstr "" | 224 | msgstr "" |
191 | 199 | 225 | ||
193 | 200 | #: ../qml/shorts-app.qml:317 shorts.desktop.in.in.h:1 | 226 | #: ../qml/shorts-app.qml:329 shorts.desktop.in.in.h:1 |
194 | 201 | msgid "Shorts" | 227 | msgid "Shorts" |
195 | 202 | msgstr "" | 228 | msgstr "" |
196 | 203 | 229 | ||
198 | 204 | #: ../qml/shorts-app.qml:448 | 230 | #: ../qml/shorts-app.qml:473 |
199 | 205 | msgid "Checking for new articles" | 231 | msgid "Checking for new articles" |
200 | 206 | msgstr "" | 232 | msgstr "" |
201 | 207 | 233 | ||
203 | 208 | #: ../qml/shorts-app.qml:472 | 234 | #: ../qml/shorts-app.qml:497 |
204 | 209 | msgid "Perhaps some of the channels have not been updated." | 235 | msgid "Perhaps some of the channels have not been updated." |
205 | 210 | msgstr "" | 236 | msgstr "" |
206 | 211 | 237 | ||
208 | 212 | #: ../qml/shorts-app.qml:473 | 238 | #: ../qml/shorts-app.qml:498 |
209 | 213 | msgid "Errors occurred during the update" | 239 | msgid "Errors occurred during the update" |
210 | 214 | msgstr "" | 240 | msgstr "" |
211 | 215 | 241 | ||
213 | 216 | #: ../qml/shorts-app.qml:491 | 242 | #: ../qml/shorts-app.qml:516 |
214 | 217 | msgid "+ Add feeds" | 243 | msgid "+ Add feeds" |
215 | 218 | msgstr "" | 244 | msgstr "" |
216 | 219 | 245 | ||
218 | 220 | #: ../qml/shorts-app.qml:503 | 246 | #: ../qml/shorts-app.qml:528 |
219 | 221 | msgid "Add online accounts" | 247 | msgid "Add online accounts" |
220 | 222 | msgstr "" | 248 | msgstr "" |
221 | 223 | 249 | ||
223 | 224 | #: ../qml/shorts-app.qml:506 | 250 | #: ../qml/shorts-app.qml:531 |
224 | 225 | msgid "Online accounts are not available for now" | 251 | msgid "Online accounts are not available for now" |
225 | 226 | msgstr "" | 252 | msgstr "" |
226 | 227 | 253 | ||
228 | 228 | #: ../qml/shorts-app.qml:507 ../qml/shorts-app.qml:516 | 254 | #: ../qml/shorts-app.qml:532 ../qml/shorts-app.qml:541 |
229 | 229 | msgid "We are sorry" | 255 | msgid "We are sorry" |
230 | 230 | msgstr "" | 256 | msgstr "" |
231 | 231 | 257 | ||
233 | 232 | #: ../qml/shorts-app.qml:512 | 258 | #: ../qml/shorts-app.qml:537 |
234 | 233 | msgid "Import subscriptions" | 259 | msgid "Import subscriptions" |
235 | 234 | msgstr "" | 260 | msgstr "" |
236 | 235 | 261 | ||
238 | 236 | #: ../qml/shorts-app.qml:515 | 262 | #: ../qml/shorts-app.qml:540 |
239 | 237 | msgid "Importing subscriptions is not available for now" | 263 | msgid "Importing subscriptions is not available for now" |
240 | 238 | msgstr "" | 264 | msgstr "" |
241 | 239 | 265 | ||
243 | 240 | #: ../qml/shorts-app.qml:530 | 266 | #: ../qml/shorts-app.qml:555 |
244 | 241 | msgid "Ok" | 267 | msgid "Ok" |
245 | 242 | msgstr "" | 268 | msgstr "" |
246 | 243 | 269 | ||
247 | 270 | #: ../qml/shorts-app.qml:569 | ||
248 | 271 | msgid "" | ||
249 | 272 | "Shorts detects that you're located in an area which blocks Google's IP." | ||
250 | 273 | "<br><br>" | ||
251 | 274 | msgstr "" | ||
252 | 275 | |||
253 | 276 | #: ../qml/shorts-app.qml:574 | ||
254 | 277 | msgid "Yes, please." | ||
255 | 278 | msgstr "" | ||
256 | 279 | |||
257 | 280 | #: ../qml/shorts-app.qml:584 | ||
258 | 281 | msgid "No, thanks." | ||
259 | 282 | msgstr "" | ||
260 | 283 | |||
261 | 244 | #: ../qml/tabs/BaseTab.qml:153 | 284 | #: ../qml/tabs/BaseTab.qml:153 |
262 | 245 | msgid "There are no articles to show" | 285 | msgid "There are no articles to show" |
263 | 246 | msgstr "" | 286 | msgstr "" |
264 | 247 | 287 | ||
265 | === modified file 'shorts/qml/CMakeLists.txt' | |||
266 | --- shorts/qml/CMakeLists.txt 2015-07-17 14:42:42 +0000 | |||
267 | +++ shorts/qml/CMakeLists.txt 2015-12-15 16:05:21 +0000 | |||
268 | @@ -19,3 +19,4 @@ | |||
269 | 19 | add_subdirectory(pages) | 19 | add_subdirectory(pages) |
270 | 20 | add_subdirectory(tabs) | 20 | add_subdirectory(tabs) |
271 | 21 | add_subdirectory(utils) | 21 | add_subdirectory(utils) |
272 | 22 | add_subdirectory(nongoogle) | ||
273 | 22 | 23 | ||
274 | === modified file 'shorts/qml/components/NetworkManager.qml' | |||
275 | --- shorts/qml/components/NetworkManager.qml 2015-10-24 07:06:24 +0000 | |||
276 | +++ shorts/qml/components/NetworkManager.qml 2015-12-15 16:05:21 +0000 | |||
277 | @@ -9,6 +9,9 @@ | |||
278 | 9 | import "../utils/imgSeparator.js" as ImageUtils | 9 | import "../utils/imgSeparator.js" as ImageUtils |
279 | 10 | import "../utils/dateutils.js" as DateUtils | 10 | import "../utils/dateutils.js" as DateUtils |
280 | 11 | 11 | ||
281 | 12 | // below import for non-google usage | ||
282 | 13 | import "../nongoogle" | ||
283 | 14 | |||
284 | 12 | Item { | 15 | Item { |
285 | 13 | id: networkManagerRoot | 16 | id: networkManagerRoot |
286 | 14 | 17 | ||
287 | @@ -18,11 +21,21 @@ | |||
288 | 18 | property string operationStatus: "success" | 21 | property string operationStatus: "success" |
289 | 19 | 22 | ||
290 | 20 | function updateFeeds(feedsArray, topicId) { | 23 | function updateFeeds(feedsArray, topicId) { |
292 | 21 | d.updateFeeds(feedsArray, topicId) | 24 | if (optionsKeeper.useGoogleSearch()) { |
293 | 25 | d.updateFeeds(feedsArray, topicId) | ||
294 | 26 | } | ||
295 | 27 | else { | ||
296 | 28 | dNG.updateFeeds(feedsArray, topicId) | ||
297 | 29 | } | ||
298 | 22 | } | 30 | } |
299 | 23 | 31 | ||
300 | 24 | function cancelDownload() { | 32 | function cancelDownload() { |
302 | 25 | d.cancelDownload() | 33 | if (optionsKeeper.useGoogleSearch()) { |
303 | 34 | d.cancelDownload() | ||
304 | 35 | } | ||
305 | 36 | else { | ||
306 | 37 | dNG.cancelDownload() | ||
307 | 38 | } | ||
308 | 26 | } | 39 | } |
309 | 27 | 40 | ||
310 | 28 | /* All private method are inside QtObject. | 41 | /* All private method are inside QtObject. |
311 | @@ -129,4 +142,133 @@ | |||
312 | 129 | } | 142 | } |
313 | 130 | } // GFA | 143 | } // GFA |
314 | 131 | } // QtObject | 144 | } // QtObject |
315 | 145 | |||
316 | 146 | ////////////////////////////////////////// add a new object to refresh non-google feeds | ||
317 | 147 | QtObject { | ||
318 | 148 | id: dNG | ||
319 | 149 | |||
320 | 150 | property var feedList: [] // Feed list to update. | ||
321 | 151 | property var currentFeed // Current downloading feed. | ||
322 | 152 | property int tagId: 0 // Tag to update. | ||
323 | 153 | |||
324 | 154 | /* Method updates feeds one by another. | ||
325 | 155 | * Input: array of objects, each should include | ||
326 | 156 | * source, link and id (of feed in DB) properties. | ||
327 | 157 | */ | ||
328 | 158 | function updateFeeds(feedsArray, topicId) { | ||
329 | 159 | tagId = topicId || 0 | ||
330 | 160 | |||
331 | 161 | downloadStarted(tagId) | ||
332 | 162 | |||
333 | 163 | feedList = feedsArray | ||
334 | 164 | operationStatus = "success" | ||
335 | 165 | updateNextFeed() | ||
336 | 166 | } | ||
337 | 167 | |||
338 | 168 | // For inner usage only. | ||
339 | 169 | function updateNextFeed() { | ||
340 | 170 | if (feedList.length == 0) { | ||
341 | 171 | downloadFinished(tagId) | ||
342 | 172 | return | ||
343 | 173 | } | ||
344 | 174 | |||
345 | 175 | currentFeed = feedList.shift() | ||
346 | 176 | nonGoogleFeedApi.loadFeed(currentFeed.source) | ||
347 | 177 | } | ||
348 | 178 | |||
349 | 179 | function cancelDownload() { | ||
350 | 180 | feedList = [] | ||
351 | 181 | operationStatus = "abort" | ||
352 | 182 | nonGoogleFeedApi.abort() | ||
353 | 183 | } | ||
354 | 184 | |||
355 | 185 | function updateFeedInfo(feedId, feedLink, responseData) { | ||
356 | 186 | var entries = responseData.item | ||
357 | 187 | var f = responseData | ||
358 | 188 | |||
359 | 189 | var fde = f.description == undefined ? "" : f.description["#text"] == undefined ? f.description : f.description["#text"] | ||
360 | 190 | var fti = f.title == undefined ? "" : f.title["#text"] == undefined ? f.title : f.title["#text"] | ||
361 | 191 | |||
362 | 192 | DB.updateFeedByXml(feedId, feedLink, fde, fti) | ||
363 | 193 | console.log(" -------- UPDATE INFO -------- ") | ||
364 | 194 | // console.log(f.title, f.link, f.feedUrl, f.description) | ||
365 | 195 | |||
366 | 196 | console.time("addArticlesEx") | ||
367 | 197 | |||
368 | 198 | var newArticles = [] | ||
369 | 199 | var maxLength = entries.length > 50 ? 50 : entries.length | ||
370 | 200 | for (var i = 0; i < maxLength; i++) { | ||
371 | 201 | var e = entries[i] | ||
372 | 202 | |||
373 | 203 | // print("article detail: ", JSON.stringify(e)) | ||
374 | 204 | // Grab image from for article. | ||
375 | 205 | // var articleImage = ImageUtils.grabArticleImage(e) | ||
376 | 206 | // e.content = clearFromBadTags(e.content) | ||
377 | 207 | |||
378 | 208 | var ti = e.title == undefined ? "" : e.title["#text"] == undefined ? e.title : e.title["#text"] | ||
379 | 209 | var li = e.link == undefined ? "" : e.link["#text"] == undefined ? e.link : e.link["#text"] | ||
380 | 210 | var au = e.author == undefined ? "" : e.author["#text"] == undefined ? e.author : e.author["#text"] | ||
381 | 211 | var creator = e.creator == undefined ? "" : e.creator["#text"] == undefined ? e.creator : e.creator["#text"] | ||
382 | 212 | var de = e.description == undefined ? "" : e.description["#text"] == undefined ? e.description : e.description["#text"] | ||
383 | 213 | var pu = e.pubDate == undefined ? "" : e.pubDate["#text"] == undefined ? e.pubDate : e.pubDate["#text"] | ||
384 | 214 | var co = e.content == undefined ? "" : e.content["#text"] == undefined ? e.content : e.content["#text"] | ||
385 | 215 | var articleImage = utilities.htmlGetImg(de) | ||
386 | 216 | if (articleImage.length == 0) { articleImage = utilities.htmlGetImg(co) } | ||
387 | 217 | else { | ||
388 | 218 | print("articleImage: ", articleImage[0]) | ||
389 | 219 | } | ||
390 | 220 | // print("date parse 0: ", pu, DateUtils.parseDate(pu)) | ||
391 | 221 | // print("date parse 1: ", DateUtils.formatRelativeTime(i18n, DateUtils.parseDate(pu))) | ||
392 | 222 | |||
393 | 223 | var temp = | ||
394 | 224 | { | ||
395 | 225 | "title": ti, | ||
396 | 226 | "content": co == "" ? de : co, | ||
397 | 227 | "link": li, | ||
398 | 228 | "author": creator == "" ? au : creator, | ||
399 | 229 | "description": de, | ||
400 | 230 | "pubDate": DateUtils.parseDate(pu), | ||
401 | 231 | "guid": Qt.md5(li + pu), | ||
402 | 232 | "image" : articleImage.length > 0 ? articleImage[0] : "", | ||
403 | 233 | "media_groups" : "" | ||
404 | 234 | } | ||
405 | 235 | |||
406 | 236 | newArticles.push(temp) | ||
407 | 237 | } | ||
408 | 238 | // print("new article length: ", newArticles.length) | ||
409 | 239 | |||
410 | 240 | // /* Add new articles to DB and restore 'read' status of some of them. | ||
411 | 241 | // */ | ||
412 | 242 | // DB.addArticles(articleModel, feedId, articleProperties); | ||
413 | 243 | |||
414 | 244 | /* Add new articles to DB and restore 'read' status of some of them. */ | ||
415 | 245 | try { | ||
416 | 246 | DB.addArticlesEx(newArticles, feedId) | ||
417 | 247 | } catch (e) { | ||
418 | 248 | console.log("Exception:", JSON.stringify(e)) | ||
419 | 249 | } | ||
420 | 250 | |||
421 | 251 | console.timeEnd("addArticlesEx") | ||
422 | 252 | } | ||
423 | 253 | |||
424 | 254 | function clearFromBadTags(content) { | ||
425 | 255 | /* Remove non empty too. Useless anyway. | ||
426 | 256 | */ | ||
427 | 257 | content = content.replace(/alt=".*?"/g, "") | ||
428 | 258 | content = content.replace(/title=".*?"/g, "") | ||
429 | 259 | return content | ||
430 | 260 | } | ||
431 | 261 | |||
432 | 262 | property var nonGoogleFeedApi: XmlNetwork { | ||
433 | 263 | onLoadResult: { | ||
434 | 264 | if (result.rss == undefined || result.rss == "") { | ||
435 | 265 | // console.log("XML NETWORK GFA:", JSON.stringify(result)) | ||
436 | 266 | if (operationStatus == "success") | ||
437 | 267 | operationStatus = "withErrors" | ||
438 | 268 | } else dNG.updateFeedInfo(dNG.currentFeed.id, dNG.currentFeed.link, result.rss.channel) | ||
439 | 269 | |||
440 | 270 | dNG.updateNextFeed() | ||
441 | 271 | } | ||
442 | 272 | } // GFA | ||
443 | 273 | } // QtObject | ||
444 | 132 | } | 274 | } |
445 | 133 | 275 | ||
446 | === modified file 'shorts/qml/components/OptionsKeeper.qml' | |||
447 | --- shorts/qml/components/OptionsKeeper.qml 2015-10-10 11:24:04 +0000 | |||
448 | +++ shorts/qml/components/OptionsKeeper.qml 2015-12-15 16:05:21 +0000 | |||
449 | @@ -14,6 +14,11 @@ | |||
450 | 14 | fontSize = getFontSize() | 14 | fontSize = getFontSize() |
451 | 15 | useDarkTheme = getUseDarkTheme() | 15 | useDarkTheme = getUseDarkTheme() |
452 | 16 | useListMode = getUseListMode() | 16 | useListMode = getUseListMode() |
453 | 17 | |||
454 | 18 | |||
455 | 19 | if (useGoogleSearch() == undefined ) { | ||
456 | 20 | setUseGoogleSearch(true) | ||
457 | 21 | } | ||
458 | 17 | } | 22 | } |
459 | 18 | 23 | ||
460 | 19 | onFontSizeChanged: setFontSize(fontSize) | 24 | onFontSizeChanged: setFontSize(fontSize) |
461 | @@ -64,6 +69,17 @@ | |||
462 | 64 | return settingsDocument.contents.dbLastUpdate | 69 | return settingsDocument.contents.dbLastUpdate |
463 | 65 | } | 70 | } |
464 | 66 | 71 | ||
465 | 72 | /////////////////////////////////////////////////////// below two functions are get/set "useGoogleSearch" value | ||
466 | 73 | function useGoogleSearch() { | ||
467 | 74 | return settingsDocument.contents.useGoogleSearch | ||
468 | 75 | } | ||
469 | 76 | |||
470 | 77 | function setUseGoogleSearch(value) { | ||
471 | 78 | var cont = settingsDocument.contents | ||
472 | 79 | cont.useGoogleSearch = value | ||
473 | 80 | settingsDocument.contents = cont | ||
474 | 81 | } | ||
475 | 82 | |||
476 | 67 | U1db.Database { | 83 | U1db.Database { |
477 | 68 | id: settingsDataBase | 84 | id: settingsDataBase |
478 | 69 | path: "ShortsOptions" | 85 | path: "ShortsOptions" |
479 | @@ -75,6 +91,8 @@ | |||
480 | 75 | docId: 'settingsDocument' | 91 | docId: 'settingsDocument' |
481 | 76 | create: true | 92 | create: true |
482 | 77 | defaults: { "useDarkTheme" : false, "fontSize" : 1, | 93 | defaults: { "useDarkTheme" : false, "fontSize" : 1, |
484 | 78 | "useListMode" : false, "dbVersion" : 1.2} | 94 | "useListMode" : false, "dbVersion" : 1.2, |
485 | 95 | "useGoogleSearch" : true | ||
486 | 96 | } | ||
487 | 79 | } | 97 | } |
488 | 80 | } // Item | 98 | } // Item |
489 | 81 | 99 | ||
490 | === added directory 'shorts/qml/nongoogle' | |||
491 | === added file 'shorts/qml/nongoogle/AppendNGFeedPage.qml' | |||
492 | --- shorts/qml/nongoogle/AppendNGFeedPage.qml 1970-01-01 00:00:00 +0000 | |||
493 | +++ shorts/qml/nongoogle/AppendNGFeedPage.qml 2015-12-15 16:05:21 +0000 | |||
494 | @@ -0,0 +1,254 @@ | |||
495 | 1 | /* | ||
496 | 2 | * Copyright (C) 2013, 2014 | ||
497 | 3 | * | ||
498 | 4 | * This program is free software; you can redistribute it and/or modify | ||
499 | 5 | * it under the terms of the GNU General Public License as published by | ||
500 | 6 | * the Free Software Foundation; version 3. | ||
501 | 7 | * | ||
502 | 8 | * This program is distributed in the hope that it will be useful, | ||
503 | 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
504 | 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
505 | 11 | * GNU General Public License for more details. | ||
506 | 12 | * | ||
507 | 13 | * You should have received a copy of the GNU General Public License | ||
508 | 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
509 | 15 | */ | ||
510 | 16 | |||
511 | 17 | import QtQuick 2.4 | ||
512 | 18 | import QtQuick.XmlListModel 2.0 | ||
513 | 19 | import Ubuntu.Components 1.3 | ||
514 | 20 | import Ubuntu.Components.ListItems 1.3 as ListItem | ||
515 | 21 | import Ubuntu.Components.Popups 1.3 | ||
516 | 22 | |||
517 | 23 | import "../utils/databasemodule_v2.js" as DB | ||
518 | 24 | import "../." | ||
519 | 25 | |||
520 | 26 | Page { | ||
521 | 27 | id: appendFeedPage | ||
522 | 28 | |||
523 | 29 | objectName: "appendfeedpage" | ||
524 | 30 | title: i18n.tr("Add feeds") | ||
525 | 31 | flickable: null | ||
526 | 32 | visible: false | ||
527 | 33 | |||
528 | 34 | property bool isDirty: false // Need to clear all page content. | ||
529 | 35 | property int selectedCount: 0 | ||
530 | 36 | property bool resultsReceived: false // Indicates that at least once results were received. | ||
531 | 37 | |||
532 | 38 | function reloadPageContent() { | ||
533 | 39 | } | ||
534 | 40 | |||
535 | 41 | /* Clear model and model's depend data. | ||
536 | 42 | * Currently only selectedCount. | ||
537 | 43 | */ | ||
538 | 44 | function clearModelDependData() { | ||
539 | 45 | // searchResultsModel.clear() | ||
540 | 46 | // selectedCount = 0 | ||
541 | 47 | } | ||
542 | 48 | |||
543 | 49 | // ------------------------------------- XmlNetwork | ||
544 | 50 | |||
545 | 51 | property string feedTitle: "" | ||
546 | 52 | property string feedDesc: "" | ||
547 | 53 | property string feedUrl: "" | ||
548 | 54 | property string feedLink: "" | ||
549 | 55 | property var feedObj | ||
550 | 56 | |||
551 | 57 | XmlNetwork { | ||
552 | 58 | id: xmlFeedApi | ||
553 | 59 | |||
554 | 60 | onLoadResult: { | ||
555 | 61 | if (result.rss == undefined || result.rss == "") { | ||
556 | 62 | // TODO alert that fail retriving feed data | ||
557 | 63 | print("onLoadResult failed") | ||
558 | 64 | } | ||
559 | 65 | else { | ||
560 | 66 | // d.updateFeedInfo(d.currentFeed.id, d.currentFeed.link, result.rss.channel) | ||
561 | 67 | var f = result.rss.channel | ||
562 | 68 | |||
563 | 69 | feedDesc = f.description == undefined ? "" : f.description["#text"] == undefined ? f.description : f.description["#text"] | ||
564 | 70 | feedTitle = f.title == undefined ? "" : f.title["#text"] == undefined ? f.title : f.title["#text"] | ||
565 | 71 | // feedUrl = l | ||
566 | 72 | feedLink = f.link == undefined ? "" : f.link["#text"] == undefined ? f.link : f.link["#text"] | ||
567 | 73 | feedObj = { | ||
568 | 74 | "url" : feedUrl, | ||
569 | 75 | "title" : feedTitle, | ||
570 | 76 | "description" : feedDesc, | ||
571 | 77 | "link" : feedLink | ||
572 | 78 | } | ||
573 | 79 | } | ||
574 | 80 | |||
575 | 81 | } | ||
576 | 82 | } | ||
577 | 83 | |||
578 | 84 | // ------------------------------------- XmlNetwork | ||
579 | 85 | |||
580 | 86 | Column { | ||
581 | 87 | id: appendFeedColumn | ||
582 | 88 | |||
583 | 89 | anchors.top: parent.top | ||
584 | 90 | anchors.topMargin: units.gu(2) | ||
585 | 91 | width: parent.width | ||
586 | 92 | spacing: units.gu(2) | ||
587 | 93 | |||
588 | 94 | TextField { | ||
589 | 95 | objectName: "tfFeedUrl" | ||
590 | 96 | id: tfFeedUrl | ||
591 | 97 | |||
592 | 98 | placeholderText: i18n.tr("Type a keyword or URL") | ||
593 | 99 | |||
594 | 100 | width: parent.width - units.gu(4) | ||
595 | 101 | // height:units.gu(5) | ||
596 | 102 | anchors { | ||
597 | 103 | horizontalCenter: parent.horizontalCenter | ||
598 | 104 | } | ||
599 | 105 | primaryItem: Image { | ||
600 | 106 | height: parent.height*0.5 | ||
601 | 107 | width: height | ||
602 | 108 | anchors.verticalCenter: parent.verticalCenter | ||
603 | 109 | // anchors.verticalCenterOffset: -units.gu(0.2) | ||
604 | 110 | source: Qt.resolvedUrl("../icons_tmp/find.svg") | ||
605 | 111 | smooth: true | ||
606 | 112 | |||
607 | 113 | MouseArea { | ||
608 | 114 | anchors.fill: parent | ||
609 | 115 | onClicked: { | ||
610 | 116 | if (Qt.inputMethod.visible) | ||
611 | 117 | tfFeedUrl.accapt() | ||
612 | 118 | } | ||
613 | 119 | } | ||
614 | 120 | } | ||
615 | 121 | |||
616 | 122 | onAccepted: { | ||
617 | 123 | accapt() | ||
618 | 124 | } | ||
619 | 125 | |||
620 | 126 | function accapt() { | ||
621 | 127 | Qt.inputMethod.hide() | ||
622 | 128 | var userInput = text | ||
623 | 129 | |||
624 | 130 | if (userInput == "") | ||
625 | 131 | return | ||
626 | 132 | |||
627 | 133 | // Very simple logic, URL if there are no spaces and contains dots. | ||
628 | 134 | // But simple not means that it is wrong. | ||
629 | 135 | var isUrlEntered = (userInput.indexOf(" ") === -1 && userInput.indexOf(".") !== -1) | ||
630 | 136 | |||
631 | 137 | if (isUrlEntered) { | ||
632 | 138 | if (userInput.indexOf("http://") !== 0) | ||
633 | 139 | {userInput = "http://" + userInput} | ||
634 | 140 | feedUrl = userInput | ||
635 | 141 | xmlFeedApi.loadFeed(userInput) | ||
636 | 142 | } | ||
637 | 143 | // else xmlFeedApi.findFeeds(text) | ||
638 | 144 | else { | ||
639 | 145 | // TODO alert that user input invalid | ||
640 | 146 | print("input invalid") | ||
641 | 147 | } | ||
642 | 148 | } | ||
643 | 149 | } | ||
644 | 150 | |||
645 | 151 | ListItem.Header { | ||
646 | 152 | |||
647 | 153 | ListItem.ThinDivider { } | ||
648 | 154 | |||
649 | 155 | text: i18n.tr("Feed Title:") | ||
650 | 156 | |||
651 | 157 | ListItem.ThinDivider { anchors.bottom: parent.bottom } | ||
652 | 158 | } | ||
653 | 159 | |||
654 | 160 | Label { | ||
655 | 161 | anchors { left: parent.left;right: parent.right; margins: units.gu(2) } | ||
656 | 162 | wrapMode: Text.WrapAtWordBoundaryOrAnywhere | ||
657 | 163 | text: feedTitle == "" ? i18n.tr("No data") : feedTitle | ||
658 | 164 | } | ||
659 | 165 | |||
660 | 166 | ListItem.Header { | ||
661 | 167 | |||
662 | 168 | ListItem.ThinDivider { } | ||
663 | 169 | |||
664 | 170 | text: i18n.tr("Feed Description:") | ||
665 | 171 | |||
666 | 172 | ListItem.ThinDivider { anchors.bottom: parent.bottom } | ||
667 | 173 | } | ||
668 | 174 | |||
669 | 175 | Label { | ||
670 | 176 | anchors { left: parent.left;right: parent.right; margins: units.gu(2) } | ||
671 | 177 | wrapMode: Text.WrapAtWordBoundaryOrAnywhere | ||
672 | 178 | text: feedDesc == "" ? i18n.tr("No data") : feedDesc | ||
673 | 179 | } | ||
674 | 180 | } // Column | ||
675 | 181 | |||
676 | 182 | Rectangle { | ||
677 | 183 | id: fakePanel | ||
678 | 184 | |||
679 | 185 | color: "#fafafa" | ||
680 | 186 | anchors { | ||
681 | 187 | left: parent.left | ||
682 | 188 | right: parent.right | ||
683 | 189 | bottom: parent.bottom | ||
684 | 190 | } | ||
685 | 191 | height: units.gu(6) | ||
686 | 192 | Item { | ||
687 | 193 | anchors.fill: parent | ||
688 | 194 | |||
689 | 195 | Button { | ||
690 | 196 | anchors { | ||
691 | 197 | verticalCenter: parent.verticalCenter | ||
692 | 198 | left: parent.left | ||
693 | 199 | leftMargin: units.gu(1) | ||
694 | 200 | } | ||
695 | 201 | gradient: UbuntuColors.greyGradient | ||
696 | 202 | action: Action { | ||
697 | 203 | text: i18n.tr("Cancel") | ||
698 | 204 | |||
699 | 205 | onTriggered: { | ||
700 | 206 | xmlFeedApi.abort() | ||
701 | 207 | pageStack.pop() | ||
702 | 208 | } | ||
703 | 209 | } | ||
704 | 210 | } | ||
705 | 211 | |||
706 | 212 | ActivityIndicator { | ||
707 | 213 | id: checkRunning | ||
708 | 214 | |||
709 | 215 | visible: xmlFeedApi.inProgress | ||
710 | 216 | running: xmlFeedApi.inProgress | ||
711 | 217 | |||
712 | 218 | anchors { | ||
713 | 219 | verticalCenter: parent.verticalCenter | ||
714 | 220 | right: nextBtn.left | ||
715 | 221 | rightMargin: units.gu(1) | ||
716 | 222 | } | ||
717 | 223 | } | ||
718 | 224 | |||
719 | 225 | Button { | ||
720 | 226 | id: nextBtn | ||
721 | 227 | objectName: "nextButton" | ||
722 | 228 | anchors { | ||
723 | 229 | verticalCenter: parent.verticalCenter | ||
724 | 230 | right: parent.right | ||
725 | 231 | rightMargin: units.gu(1) | ||
726 | 232 | } | ||
727 | 233 | |||
728 | 234 | enabled: !xmlFeedApi.inProgress && feedUrl != "" && feedTitle != "" | ||
729 | 235 | action: Action { | ||
730 | 236 | text: i18n.tr("Next") | ||
731 | 237 | |||
732 | 238 | onTriggered: { | ||
733 | 239 | if (!nextBtn.enabled) | ||
734 | 240 | return | ||
735 | 241 | |||
736 | 242 | var selectedFeeds = [] | ||
737 | 243 | |||
738 | 244 | selectedFeeds.push(feedObj) | ||
739 | 245 | |||
740 | 246 | pageStack.push(chooseTopicPage, {"feedsToAdd" : selectedFeeds}) | ||
741 | 247 | // pageStack.push(Qt.resolvedUrl("../pages/ChooseTopicPage.qml"), {"feedsToAdd" : selectedFeeds}) | ||
742 | 248 | } | ||
743 | 249 | } | ||
744 | 250 | } // Button | ||
745 | 251 | } | ||
746 | 252 | } // Rectangle fakePanel | ||
747 | 253 | |||
748 | 254 | } | ||
749 | 0 | 255 | ||
750 | === added file 'shorts/qml/nongoogle/CMakeLists.txt' | |||
751 | --- shorts/qml/nongoogle/CMakeLists.txt 1970-01-01 00:00:00 +0000 | |||
752 | +++ shorts/qml/nongoogle/CMakeLists.txt 2015-12-15 16:05:21 +0000 | |||
753 | @@ -0,0 +1,6 @@ | |||
754 | 1 | file(GLOB NONGOOGLE_QML_JS_FILES *.qml *.js) | ||
755 | 2 | |||
756 | 3 | # make the files visible in the qtcreator tree | ||
757 | 4 | add_custom_target(ubuntu-rssreader-app_nongoogle_QMlFiles ALL SOURCES ${NONGOOGLE_QML_JS_FILES}) | ||
758 | 5 | |||
759 | 6 | install(FILES ${NONGOOGLE_QML_JS_FILES} DESTINATION ${SHORTS_DIR}/nongoogle/) | ||
760 | 0 | \ No newline at end of file | 7 | \ No newline at end of file |
761 | 1 | 8 | ||
762 | === added file 'shorts/qml/nongoogle/Positioner.qml' | |||
763 | --- shorts/qml/nongoogle/Positioner.qml 1970-01-01 00:00:00 +0000 | |||
764 | +++ shorts/qml/nongoogle/Positioner.qml 2015-12-15 16:05:21 +0000 | |||
765 | @@ -0,0 +1,114 @@ | |||
766 | 1 | /* | ||
767 | 2 | * Copyright (C) 2014-2015 Canonical Ltd | ||
768 | 3 | * | ||
769 | 4 | * This file is part of Ubuntu Clock App | ||
770 | 5 | * | ||
771 | 6 | * Ubuntu Clock App is free software: you can redistribute it and/or modify | ||
772 | 7 | * it under the terms of the GNU General Public License version 3 as | ||
773 | 8 | * published by the Free Software Foundation. | ||
774 | 9 | * | ||
775 | 10 | * Ubuntu Clock App is distributed in the hope that it will be useful, | ||
776 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
777 | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
778 | 13 | * GNU General Public License for more details. | ||
779 | 14 | * | ||
780 | 15 | * You should have received a copy of the GNU General Public License | ||
781 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
782 | 17 | */ | ||
783 | 18 | |||
784 | 19 | import QtQuick 2.4 | ||
785 | 20 | import U1db 1.0 as U1db | ||
786 | 21 | import QtPositioning 5.2 | ||
787 | 22 | import Ubuntu.Components 1.3 | ||
788 | 23 | import Ubuntu.Components.Popups 1.3 | ||
789 | 24 | //import "../components" | ||
790 | 25 | |||
791 | 26 | Item { | ||
792 | 27 | id: positioner | ||
793 | 28 | objectName: "positioner" | ||
794 | 29 | |||
795 | 30 | anchors.fill: parent | ||
796 | 31 | |||
797 | 32 | // Property to keep track of app cold start status | ||
798 | 33 | property string countryCode: "" | ||
799 | 34 | |||
800 | 35 | Component.onCompleted: { | ||
801 | 36 | } | ||
802 | 37 | |||
803 | 38 | PositionSource { | ||
804 | 39 | id: geoposition | ||
805 | 40 | |||
806 | 41 | // Property to store the time of the last GPS location update | ||
807 | 42 | property var lastUpdate | ||
808 | 43 | |||
809 | 44 | readonly property real userLongitude: position.coordinate.longitude | ||
810 | 45 | |||
811 | 46 | readonly property real userLatitude: position.coordinate.latitude | ||
812 | 47 | |||
813 | 48 | active: true | ||
814 | 49 | updateInterval: 1000 | ||
815 | 50 | |||
816 | 51 | onSourceErrorChanged: { | ||
817 | 52 | // Stop querying user location if location service is not available | ||
818 | 53 | if (sourceError !== PositionSource.NoError) { | ||
819 | 54 | console.log("[Source Error]: Location Service Error") | ||
820 | 55 | geoposition.stop() | ||
821 | 56 | |||
822 | 57 | } | ||
823 | 58 | } | ||
824 | 59 | |||
825 | 60 | onPositionChanged: { | ||
826 | 61 | // Do not accept an invalid user location | ||
827 | 62 | if(!position.longitudeValid || !position.latitudeValid) { | ||
828 | 63 | return | ||
829 | 64 | } | ||
830 | 65 | |||
831 | 66 | if(position.longitudeValid || position.latitudeValid) { | ||
832 | 67 | print("current position: ", userLatitude, userLongitude) | ||
833 | 68 | geoposition.stop() | ||
834 | 69 | getCountryCode(userLatitude, userLongitude) | ||
835 | 70 | } | ||
836 | 71 | |||
837 | 72 | |||
838 | 73 | } | ||
839 | 74 | |||
840 | 75 | /* get country code from geonames.org | ||
841 | 76 | // http://api.geonames.org/countryCode?lat=23&lng=113&username=krnekhelesh | ||
842 | 77 | */ | ||
843 | 78 | function getCountryCode(lat, lng) { | ||
844 | 79 | |||
845 | 80 | var url = "http://api.geonames.org/countryCode?lat=" + lat + "&lng=" + lng + "&username=krnekhelesh&type=JSON" | ||
846 | 81 | |||
847 | 82 | var doc = new XMLHttpRequest() | ||
848 | 83 | |||
849 | 84 | doc.onreadystatechange = function() { | ||
850 | 85 | |||
851 | 86 | // print("positioner onreadystatechange: ", doc.readyState, doc.status, feedUrl) | ||
852 | 87 | if (doc.readyState === XMLHttpRequest.DONE) { | ||
853 | 88 | |||
854 | 89 | var resObj | ||
855 | 90 | if (doc.status == 200) { | ||
856 | 91 | resObj = JSON.parse(doc.responseText) | ||
857 | 92 | } else { // Error | ||
858 | 93 | resObj = {"responseDetails" : doc.statusText, | ||
859 | 94 | "responseStatus" : doc.status} | ||
860 | 95 | // resObj = "" | ||
861 | 96 | } | ||
862 | 97 | |||
863 | 98 | countryCode = resObj.countryName | ||
864 | 99 | print("countryCode", resObj) | ||
865 | 100 | |||
866 | 101 | if (countryCode == "China") { | ||
867 | 102 | if (optionsKeeper.useGoogleSearch()) { | ||
868 | 103 | PopupUtils.open(componentDialogNG, tabstabs) | ||
869 | 104 | } | ||
870 | 105 | } | ||
871 | 106 | } | ||
872 | 107 | } | ||
873 | 108 | |||
874 | 109 | doc.open("GET", url, true); | ||
875 | 110 | doc.send(); | ||
876 | 111 | } | ||
877 | 112 | } //PositionSource | ||
878 | 113 | |||
879 | 114 | } | ||
880 | 0 | 115 | ||
881 | === added file 'shorts/qml/nongoogle/XmlNetwork.qml' | |||
882 | --- shorts/qml/nongoogle/XmlNetwork.qml 1970-01-01 00:00:00 +0000 | |||
883 | +++ shorts/qml/nongoogle/XmlNetwork.qml 2015-12-15 16:05:21 +0000 | |||
884 | @@ -0,0 +1,84 @@ | |||
885 | 1 | import QtQuick 2.4 | ||
886 | 2 | |||
887 | 3 | QtObject { | ||
888 | 4 | id: rootObject | ||
889 | 5 | |||
890 | 6 | property bool inProgress: __doc != null | ||
891 | 7 | |||
892 | 8 | signal findResult(var result) | ||
893 | 9 | signal loadResult(var result) | ||
894 | 10 | |||
895 | 11 | property var __doc: null | ||
896 | 12 | |||
897 | 13 | /* Load feed by URL. | ||
898 | 14 | */ | ||
899 | 15 | function loadFeed(feedUrl, num) { | ||
900 | 16 | abort(true) | ||
901 | 17 | |||
902 | 18 | if (num) | ||
903 | 19 | num = Math.min(num, 100) | ||
904 | 20 | else num = 50 | ||
905 | 21 | |||
906 | 22 | __doc = new XMLHttpRequest() | ||
907 | 23 | var doc = __doc | ||
908 | 24 | |||
909 | 25 | doc.onreadystatechange = function() { | ||
910 | 26 | |||
911 | 27 | // print("xmlnetwork onreadystatechange: ", doc.readyState, doc.status, feedUrl) | ||
912 | 28 | if (doc.readyState === XMLHttpRequest.DONE) { | ||
913 | 29 | |||
914 | 30 | var resObj | ||
915 | 31 | if (doc.status == 200) { | ||
916 | 32 | // resObj = JSON.parse(doc.responseText) | ||
917 | 33 | resObj = utilities.xmlToJson(doc.responseText) | ||
918 | 34 | } else { // Error | ||
919 | 35 | resObj = {"responseDetails" : doc.statusText, | ||
920 | 36 | "responseStatus" : doc.status} | ||
921 | 37 | } | ||
922 | 38 | |||
923 | 39 | __doc = null | ||
924 | 40 | loadResult(resObj) | ||
925 | 41 | } | ||
926 | 42 | } | ||
927 | 43 | |||
928 | 44 | |||
929 | 45 | /* Number of articles to download. | ||
930 | 46 | */ | ||
931 | 47 | // finalRequest += "&num=" + num | ||
932 | 48 | |||
933 | 49 | /* Add some optional params. | ||
934 | 50 | * May be usable: | ||
935 | 51 | * hl - host language, for example "hl=ru", default en. | ||
936 | 52 | * num - number of entries, for example "num=50", default 4, maximum 100. | ||
937 | 53 | * output - format of output, for example "output=json", may be xml, json_xml, json. | ||
938 | 54 | */ | ||
939 | 55 | // doc.open("GET", finalRequest, true); | ||
940 | 56 | doc.open("GET", feedUrl, true); | ||
941 | 57 | doc.send(); | ||
942 | 58 | } | ||
943 | 59 | |||
944 | 60 | /* Param "isAbortOnly" used to preserve | ||
945 | 61 | * property "__doc" in not null state. | ||
946 | 62 | * inProgress binded to it so we can avoid | ||
947 | 63 | * additional recalculations. | ||
948 | 64 | */ | ||
949 | 65 | function abort(isAbortOnly) { | ||
950 | 66 | if (__doc != null) { | ||
951 | 67 | __doc.abort() | ||
952 | 68 | if (!isAbortOnly) | ||
953 | 69 | __doc = null | ||
954 | 70 | } | ||
955 | 71 | } | ||
956 | 72 | |||
957 | 73 | /* Return true if some kind of errors detected. | ||
958 | 74 | * TODO DEMO | ||
959 | 75 | */ | ||
960 | 76 | function checkForErrors(result) { | ||
961 | 77 | if (result.responseStatus == 200 || // HTTP OK | ||
962 | 78 | result.responseStatus == 0) { // ABORTED | ||
963 | 79 | return false | ||
964 | 80 | } | ||
965 | 81 | |||
966 | 82 | return true | ||
967 | 83 | } | ||
968 | 84 | } // QtObject | ||
969 | 0 | 85 | ||
970 | === added file 'shorts/qml/pages/PageSettings.qml' | |||
971 | --- shorts/qml/pages/PageSettings.qml 1970-01-01 00:00:00 +0000 | |||
972 | +++ shorts/qml/pages/PageSettings.qml 2015-12-15 16:05:21 +0000 | |||
973 | @@ -0,0 +1,62 @@ | |||
974 | 1 | import QtQuick 2.4 | ||
975 | 2 | import Ubuntu.Components 1.3 | ||
976 | 3 | import Ubuntu.Components.ListItems 1.3 as ListItem | ||
977 | 4 | import Ubuntu.Components.Popups 1.3 | ||
978 | 5 | import "../components" | ||
979 | 6 | |||
980 | 7 | Page { | ||
981 | 8 | id: pageSettings | ||
982 | 9 | title: i18n.tr("Settings") | ||
983 | 10 | flickable: null | ||
984 | 11 | |||
985 | 12 | Column { | ||
986 | 13 | anchors { | ||
987 | 14 | top: parent.top; topMargin: units.gu(1) | ||
988 | 15 | left: parent.left; leftMargin: units.gu(1) | ||
989 | 16 | right: parent.right; rightMargin: units.gu(1) | ||
990 | 17 | } | ||
991 | 18 | height: childrenRect.height | ||
992 | 19 | spacing: units.gu(0.8) | ||
993 | 20 | |||
994 | 21 | ///////////////////////////////////////////////////////////////////// Google RSS engine switch start here | ||
995 | 22 | Label { | ||
996 | 23 | anchors { left: parent.left; right: parent.right; margins: units.gu(2) } | ||
997 | 24 | horizontalAlignment: Text.AlignHCenter | ||
998 | 25 | wrapMode: Text.WrapAtWordBoundaryOrAnywhere | ||
999 | 26 | text: i18n.tr("For those who living in some special regions cannot access Google, the switch below can disable Google RSS engine, Shorts will directly get data from RSS sources.") | ||
1000 | 27 | } | ||
1001 | 28 | |||
1002 | 29 | Item { width: 10; height: 1 } // just a separator | ||
1003 | 30 | |||
1004 | 31 | Item { | ||
1005 | 32 | anchors { left: parent.left; right: parent.right; } | ||
1006 | 33 | height: childrenRect.height | ||
1007 | 34 | |||
1008 | 35 | Label { | ||
1009 | 36 | text: i18n.tr("Use Google Search: ") | ||
1010 | 37 | } | ||
1011 | 38 | |||
1012 | 39 | Switch { | ||
1013 | 40 | anchors.right: parent.right | ||
1014 | 41 | checked: optionsKeeper.useGoogleSearch() | ||
1015 | 42 | |||
1016 | 43 | Component.onCompleted: { | ||
1017 | 44 | if (optionsKeeper.useGoogleSearch() == undefined ) { | ||
1018 | 45 | optionsKeeper.setUseGoogleSearch(true) | ||
1019 | 46 | checked = true | ||
1020 | 47 | } | ||
1021 | 48 | } | ||
1022 | 49 | |||
1023 | 50 | onCheckedChanged: { | ||
1024 | 51 | optionsKeeper.setUseGoogleSearch(checked) | ||
1025 | 52 | } | ||
1026 | 53 | } | ||
1027 | 54 | } | ||
1028 | 55 | |||
1029 | 56 | ListItem.ThinDivider{} | ||
1030 | 57 | ///////////////////////////////////////////////////////////////////// Google RSS engine switch end here | ||
1031 | 58 | |||
1032 | 59 | |||
1033 | 60 | }// Column | ||
1034 | 61 | |||
1035 | 62 | } | ||
1036 | 0 | 63 | ||
1037 | === modified file 'shorts/qml/shorts-app.qml' | |||
1038 | --- shorts/qml/shorts-app.qml 2015-11-27 22:08:54 +0000 | |||
1039 | +++ shorts/qml/shorts-app.qml 2015-12-15 16:05:21 +0000 | |||
1040 | @@ -8,6 +8,8 @@ | |||
1041 | 8 | import "./components" | 8 | import "./components" |
1042 | 9 | import "./utils/databasemodule_v2.js" as DB | 9 | import "./utils/databasemodule_v2.js" as DB |
1043 | 10 | 10 | ||
1044 | 11 | import "./nongoogle" | ||
1045 | 12 | |||
1046 | 11 | MainView { | 13 | MainView { |
1047 | 12 | id: mainView | 14 | id: mainView |
1048 | 13 | 15 | ||
1049 | @@ -119,7 +121,7 @@ | |||
1050 | 119 | objectName: "pageStack" | 121 | objectName: "pageStack" |
1051 | 120 | 122 | ||
1052 | 121 | property bool isListView: optionsKeeper.useListMode | 123 | property bool isListView: optionsKeeper.useListMode |
1054 | 122 | property var commonHeadActions: [refreshAction, changeModeAction, editTopicsAction, nightModeAction] | 124 | property var commonHeadActions: [refreshAction, changeModeAction, editTopicsAction, nightModeAction, actionSetting] |
1055 | 123 | 125 | ||
1056 | 124 | anchors.fill: parent | 126 | anchors.fill: parent |
1057 | 125 | focus: true | 127 | focus: true |
1058 | @@ -231,6 +233,16 @@ | |||
1059 | 231 | } | 233 | } |
1060 | 232 | } | 234 | } |
1061 | 233 | 235 | ||
1062 | 236 | Action { | ||
1063 | 237 | id: actionSetting | ||
1064 | 238 | objectName:"actionSetting" | ||
1065 | 239 | text: i18n.tr("Settings") | ||
1066 | 240 | iconName: "settings" | ||
1067 | 241 | onTriggered: { | ||
1068 | 242 | pageStack.push(Qt.resolvedUrl("./pages/PageSettings.qml")) | ||
1069 | 243 | } | ||
1070 | 244 | } | ||
1071 | 245 | |||
1072 | 234 | /* -------------------------- Pages & Tabs ---------------------------- */ | 246 | /* -------------------------- Pages & Tabs ---------------------------- */ |
1073 | 235 | 247 | ||
1074 | 236 | BottomEdgeTabs { | 248 | BottomEdgeTabs { |
1075 | @@ -239,7 +251,7 @@ | |||
1076 | 239 | objectName: "tabstabs" | 251 | objectName: "tabstabs" |
1077 | 240 | visible: false | 252 | visible: false |
1078 | 241 | 253 | ||
1080 | 242 | bottomEdgePage: appendFeedPage | 254 | bottomEdgePage: optionsKeeper.useGoogleSearch() ? appendFeedPage : appendNGFeedPage |
1081 | 243 | bottomEdgeTitle: i18n.tr("Add feeds") | 255 | bottomEdgeTitle: i18n.tr("Add feeds") |
1082 | 244 | bottomEdgeBackgroundColor: "#F5F5F5" // "#875864" | 256 | bottomEdgeBackgroundColor: "#F5F5F5" // "#875864" |
1083 | 245 | bottomEdgeTipColor: "#5533b5e5"// "#E0E0E0" //"#9b616c" | 257 | bottomEdgeTipColor: "#5533b5e5"// "#E0E0E0" //"#9b616c" |
1084 | @@ -379,6 +391,14 @@ | |||
1085 | 379 | flickable: null | 391 | flickable: null |
1086 | 380 | visible: false | 392 | visible: false |
1087 | 381 | } | 393 | } |
1088 | 394 | |||
1089 | 395 | AppendNGFeedPage { | ||
1090 | 396 | id: appendNGFeedPage | ||
1091 | 397 | |||
1092 | 398 | title: i18n.tr("Add feeds") | ||
1093 | 399 | flickable: null | ||
1094 | 400 | visible: false | ||
1095 | 401 | } | ||
1096 | 382 | // ******************************** Choose Topic Page ***********************/////////////// | 402 | // ******************************** Choose Topic Page ***********************/////////////// |
1097 | 383 | 403 | ||
1098 | 384 | ChooseTopicPage { | 404 | ChooseTopicPage { |
1099 | @@ -436,6 +456,11 @@ | |||
1100 | 436 | id: optionsKeeper | 456 | id: optionsKeeper |
1101 | 437 | } | 457 | } |
1102 | 438 | 458 | ||
1103 | 459 | // Positioner to detect current position | ||
1104 | 460 | Positioner { | ||
1105 | 461 | id: positionDetector | ||
1106 | 462 | } | ||
1107 | 463 | |||
1108 | 439 | /* -------------------------- Components ---------------------------- */ | 464 | /* -------------------------- Components ---------------------------- */ |
1109 | 440 | 465 | ||
1110 | 441 | Component { | 466 | Component { |
1111 | @@ -534,6 +559,35 @@ | |||
1112 | 534 | } | 559 | } |
1113 | 535 | } // Component | 560 | } // Component |
1114 | 536 | 561 | ||
1115 | 562 | //////////////////////////////////////////////////////// a dialog to ask user if she/he wants to turn off the google search | ||
1116 | 563 | Component { | ||
1117 | 564 | id: componentDialogNG | ||
1118 | 565 | |||
1119 | 566 | Dialog { | ||
1120 | 567 | id: dialogNG | ||
1121 | 568 | title: i18n.tr("Warning") | ||
1122 | 569 | text: i18n.tr("Shorts detects that you're located in an area which blocks Google's IP.<br><br>" | ||
1123 | 570 | + "We strongly reconmend you to turn off the Google search funtion." | ||
1124 | 571 | + "Or you can do it in the settings page manually.") | ||
1125 | 572 | |||
1126 | 573 | Button { | ||
1127 | 574 | text: i18n.tr("Yes, please.") | ||
1128 | 575 | color: UbuntuColors.orange | ||
1129 | 576 | objectName: "dialogNGButtonYes" | ||
1130 | 577 | onClicked: { | ||
1131 | 578 | optionsKeeper.setUseGoogleSearch(false) | ||
1132 | 579 | PopupUtils.close(dialogNG) | ||
1133 | 580 | } | ||
1134 | 581 | } | ||
1135 | 582 | |||
1136 | 583 | Button { | ||
1137 | 584 | text: i18n.tr("No, thanks.") | ||
1138 | 585 | objectName: "dialogNGButtonNo" | ||
1139 | 586 | onClicked: PopupUtils.close(dialogNG) | ||
1140 | 587 | } | ||
1141 | 588 | } | ||
1142 | 589 | } // Component | ||
1143 | 590 | |||
1144 | 537 | /* -------------------------- Connections ---------------------------- */ | 591 | /* -------------------------- Connections ---------------------------- */ |
1145 | 538 | 592 | ||
1146 | 539 | Connections { | 593 | Connections { |
1147 | 540 | 594 | ||
1148 | === modified file 'shorts/shorts.pro' | |||
1149 | --- shorts/shorts.pro 2015-09-16 08:52:25 +0000 | |||
1150 | +++ shorts/shorts.pro 2015-12-15 16:05:21 +0000 | |||
1151 | @@ -6,7 +6,8 @@ | |||
1152 | 6 | QT += qml quick | 6 | QT += qml quick |
1153 | 7 | 7 | ||
1154 | 8 | SOURCES += main.cpp \ | 8 | SOURCES += main.cpp \ |
1156 | 9 | CachingNetworkManagerFactory.cpp | 9 | CachingNetworkManagerFactory.cpp \ |
1157 | 10 | xml2json/utilities.cpp | ||
1158 | 10 | 11 | ||
1159 | 11 | RESOURCES += shorts.qrc | 12 | RESOURCES += shorts.qrc |
1160 | 12 | 13 | ||
1161 | @@ -41,12 +42,16 @@ | |||
1162 | 41 | qml/pages/SwipeDelete.qml \ | 42 | qml/pages/SwipeDelete.qml \ |
1163 | 42 | qml/pages/TopicComponent.qml \ | 43 | qml/pages/TopicComponent.qml \ |
1164 | 43 | qml/pages/TopicManagement.qml \ | 44 | qml/pages/TopicManagement.qml \ |
1165 | 45 | qml/pages/PageSettings.qml \ | ||
1166 | 44 | qml/tabs/BaseTab.qml \ | 46 | qml/tabs/BaseTab.qml \ |
1167 | 45 | qml/tabs/SavedTab.qml \ | 47 | qml/tabs/SavedTab.qml \ |
1168 | 46 | qml/tabs/ShortsTab.qml \ | 48 | qml/tabs/ShortsTab.qml \ |
1169 | 47 | qml/tabs/TopicTab.qml \ | 49 | qml/tabs/TopicTab.qml \ |
1170 | 48 | qml/shorts-app.qml \ | 50 | qml/shorts-app.qml \ |
1172 | 49 | qml/content/SharePage.qml | 51 | qml/content/SharePage.qml \ |
1173 | 52 | qml/nongoogle/AppendNGFeedPage.qml \ | ||
1174 | 53 | qml/nongoogle/Positioner.qml \ | ||
1175 | 54 | qml/nongoogle/XmlNetwork.qml | ||
1176 | 50 | 55 | ||
1177 | 51 | #specify where the config files are installed to | 56 | #specify where the config files are installed to |
1178 | 52 | config_files.path = /. | 57 | config_files.path = /. |
1179 | @@ -59,5 +64,39 @@ | |||
1180 | 59 | INSTALLS+=target | 64 | INSTALLS+=target |
1181 | 60 | 65 | ||
1182 | 61 | HEADERS += \ | 66 | HEADERS += \ |
1184 | 62 | CachingNetworkManagerFactory.h | 67 | CachingNetworkManagerFactory.h \ |
1185 | 68 | xml2json/utilities.h \ | ||
1186 | 69 | xml2json/rapidjson/error/en.h \ | ||
1187 | 70 | xml2json/rapidjson/error/error.h \ | ||
1188 | 71 | xml2json/rapidjson/internal/biginteger.h \ | ||
1189 | 72 | xml2json/rapidjson/internal/diyfp.h \ | ||
1190 | 73 | xml2json/rapidjson/internal/dtoa.h \ | ||
1191 | 74 | xml2json/rapidjson/internal/ieee754.h \ | ||
1192 | 75 | xml2json/rapidjson/internal/itoa.h \ | ||
1193 | 76 | xml2json/rapidjson/internal/meta.h \ | ||
1194 | 77 | xml2json/rapidjson/internal/pow10.h \ | ||
1195 | 78 | xml2json/rapidjson/internal/stack.h \ | ||
1196 | 79 | xml2json/rapidjson/internal/strfunc.h \ | ||
1197 | 80 | xml2json/rapidjson/internal/strtod.h \ | ||
1198 | 81 | xml2json/rapidjson/msinttypes/inttypes.h \ | ||
1199 | 82 | xml2json/rapidjson/msinttypes/stdint.h \ | ||
1200 | 83 | xml2json/rapidjson/allocators.h \ | ||
1201 | 84 | xml2json/rapidjson/document.h \ | ||
1202 | 85 | xml2json/rapidjson/encodedstream.h \ | ||
1203 | 86 | xml2json/rapidjson/encodings.h \ | ||
1204 | 87 | xml2json/rapidjson/filereadstream.h \ | ||
1205 | 88 | xml2json/rapidjson/filestream.h \ | ||
1206 | 89 | xml2json/rapidjson/filewritestream.h \ | ||
1207 | 90 | xml2json/rapidjson/memorybuffer.h \ | ||
1208 | 91 | xml2json/rapidjson/memorystream.h \ | ||
1209 | 92 | xml2json/rapidjson/prettywriter.h \ | ||
1210 | 93 | xml2json/rapidjson/rapidjson.h \ | ||
1211 | 94 | xml2json/rapidjson/reader.h \ | ||
1212 | 95 | xml2json/rapidjson/stringbuffer.h \ | ||
1213 | 96 | xml2json/rapidjson/writer.h \ | ||
1214 | 97 | xml2json/rapidxml/rapidxml.hpp \ | ||
1215 | 98 | xml2json/rapidxml/rapidxml_iterators.hpp \ | ||
1216 | 99 | xml2json/rapidxml/rapidxml_print.hpp \ | ||
1217 | 100 | xml2json/rapidxml/rapidxml_utils.hpp \ | ||
1218 | 101 | xml2json/xml2json.hpp | ||
1219 | 63 | 102 | ||
1220 | 64 | 103 | ||
1221 | === modified file 'shorts/shorts.qrc' | |||
1222 | --- shorts/shorts.qrc 2015-09-16 08:52:25 +0000 | |||
1223 | +++ shorts/shorts.qrc 2015-12-15 16:05:21 +0000 | |||
1224 | @@ -32,6 +32,10 @@ | |||
1225 | 32 | <file>qml/shorts-app.qml</file> | 32 | <file>qml/shorts-app.qml</file> |
1226 | 33 | <file>qml/content/SharePage.qml</file> | 33 | <file>qml/content/SharePage.qml</file> |
1227 | 34 | <file>qml/components/DarkModeShader.qml</file> | 34 | <file>qml/components/DarkModeShader.qml</file> |
1228 | 35 | <file>qml/nongoogle/XmlNetwork.qml</file> | ||
1229 | 36 | <file>qml/nongoogle/AppendNGFeedPage.qml</file> | ||
1230 | 37 | <file>qml/pages/PageSettings.qml</file> | ||
1231 | 38 | <file>qml/nongoogle/Positioner.qml</file> | ||
1232 | 35 | </qresource> | 39 | </qresource> |
1233 | 36 | <qresource prefix="/img"> | 40 | <qresource prefix="/img"> |
1234 | 37 | <file>qml/icons/add.svg</file> | 41 | <file>qml/icons/add.svg</file> |
1235 | 38 | 42 | ||
1236 | === added directory 'shorts/xml2json' | |||
1237 | === added directory 'shorts/xml2json/rapidjson' | |||
1238 | === added file 'shorts/xml2json/rapidjson/allocators.h' | |||
1239 | --- shorts/xml2json/rapidjson/allocators.h 1970-01-01 00:00:00 +0000 | |||
1240 | +++ shorts/xml2json/rapidjson/allocators.h 2015-12-15 16:05:21 +0000 | |||
1241 | @@ -0,0 +1,245 @@ | |||
1242 | 1 | // Copyright (C) 2011 Milo Yip | ||
1243 | 2 | // | ||
1244 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
1245 | 4 | // of this software and associated documentation files (the "Software"), to deal | ||
1246 | 5 | // in the Software without restriction, including without limitation the rights | ||
1247 | 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
1248 | 7 | // copies of the Software, and to permit persons to whom the Software is | ||
1249 | 8 | // furnished to do so, subject to the following conditions: | ||
1250 | 9 | // | ||
1251 | 10 | // The above copyright notice and this permission notice shall be included in | ||
1252 | 11 | // all copies or substantial portions of the Software. | ||
1253 | 12 | // | ||
1254 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
1255 | 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
1256 | 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
1257 | 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
1258 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
1259 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
1260 | 19 | // THE SOFTWARE. | ||
1261 | 20 | |||
1262 | 21 | #ifndef RAPIDJSON_ALLOCATORS_H_ | ||
1263 | 22 | #define RAPIDJSON_ALLOCATORS_H_ | ||
1264 | 23 | |||
1265 | 24 | #include "rapidjson.h" | ||
1266 | 25 | |||
1267 | 26 | RAPIDJSON_NAMESPACE_BEGIN | ||
1268 | 27 | |||
1269 | 28 | /////////////////////////////////////////////////////////////////////////////// | ||
1270 | 29 | // Allocator | ||
1271 | 30 | |||
1272 | 31 | /*! \class rapidjson::Allocator | ||
1273 | 32 | \brief Concept for allocating, resizing and freeing memory block. | ||
1274 | 33 | |||
1275 | 34 | Note that Malloc() and Realloc() are non-static but Free() is static. | ||
1276 | 35 | |||
1277 | 36 | So if an allocator need to support Free(), it needs to put its pointer in | ||
1278 | 37 | the header of memory block. | ||
1279 | 38 | |||
1280 | 39 | \code | ||
1281 | 40 | concept Allocator { | ||
1282 | 41 | static const bool kNeedFree; //!< Whether this allocator needs to call Free(). | ||
1283 | 42 | |||
1284 | 43 | // Allocate a memory block. | ||
1285 | 44 | // \param size of the memory block in bytes. | ||
1286 | 45 | // \returns pointer to the memory block. | ||
1287 | 46 | void* Malloc(size_t size); | ||
1288 | 47 | |||
1289 | 48 | // Resize a memory block. | ||
1290 | 49 | // \param originalPtr The pointer to current memory block. Null pointer is permitted. | ||
1291 | 50 | // \param originalSize The current size in bytes. (Design issue: since some allocator may not book-keep this, explicitly pass to it can save memory.) | ||
1292 | 51 | // \param newSize the new size in bytes. | ||
1293 | 52 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize); | ||
1294 | 53 | |||
1295 | 54 | // Free a memory block. | ||
1296 | 55 | // \param pointer to the memory block. Null pointer is permitted. | ||
1297 | 56 | static void Free(void *ptr); | ||
1298 | 57 | }; | ||
1299 | 58 | \endcode | ||
1300 | 59 | */ | ||
1301 | 60 | |||
1302 | 61 | /////////////////////////////////////////////////////////////////////////////// | ||
1303 | 62 | // CrtAllocator | ||
1304 | 63 | |||
1305 | 64 | //! C-runtime library allocator. | ||
1306 | 65 | /*! This class is just wrapper for standard C library memory routines. | ||
1307 | 66 | \note implements Allocator concept | ||
1308 | 67 | */ | ||
1309 | 68 | class CrtAllocator { | ||
1310 | 69 | public: | ||
1311 | 70 | static const bool kNeedFree = true; | ||
1312 | 71 | void* Malloc(size_t size) { return std::malloc(size); } | ||
1313 | 72 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { (void)originalSize; return std::realloc(originalPtr, newSize); } | ||
1314 | 73 | static void Free(void *ptr) { std::free(ptr); } | ||
1315 | 74 | }; | ||
1316 | 75 | |||
1317 | 76 | /////////////////////////////////////////////////////////////////////////////// | ||
1318 | 77 | // MemoryPoolAllocator | ||
1319 | 78 | |||
1320 | 79 | //! Default memory allocator used by the parser and DOM. | ||
1321 | 80 | /*! This allocator allocate memory blocks from pre-allocated memory chunks. | ||
1322 | 81 | |||
1323 | 82 | It does not free memory blocks. And Realloc() only allocate new memory. | ||
1324 | 83 | |||
1325 | 84 | The memory chunks are allocated by BaseAllocator, which is CrtAllocator by default. | ||
1326 | 85 | |||
1327 | 86 | User may also supply a buffer as the first chunk. | ||
1328 | 87 | |||
1329 | 88 | If the user-buffer is full then additional chunks are allocated by BaseAllocator. | ||
1330 | 89 | |||
1331 | 90 | The user-buffer is not deallocated by this allocator. | ||
1332 | 91 | |||
1333 | 92 | \tparam BaseAllocator the allocator type for allocating memory chunks. Default is CrtAllocator. | ||
1334 | 93 | \note implements Allocator concept | ||
1335 | 94 | */ | ||
1336 | 95 | template <typename BaseAllocator = CrtAllocator> | ||
1337 | 96 | class MemoryPoolAllocator { | ||
1338 | 97 | public: | ||
1339 | 98 | static const bool kNeedFree = false; //!< Tell users that no need to call Free() with this allocator. (concept Allocator) | ||
1340 | 99 | |||
1341 | 100 | //! Constructor with chunkSize. | ||
1342 | 101 | /*! \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. | ||
1343 | 102 | \param baseAllocator The allocator for allocating memory chunks. | ||
1344 | 103 | */ | ||
1345 | 104 | MemoryPoolAllocator(size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : | ||
1346 | 105 | chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(0), baseAllocator_(baseAllocator), ownBaseAllocator_(0) | ||
1347 | 106 | { | ||
1348 | 107 | } | ||
1349 | 108 | |||
1350 | 109 | //! Constructor with user-supplied buffer. | ||
1351 | 110 | /*! The user buffer will be used firstly. When it is full, memory pool allocates new chunk with chunk size. | ||
1352 | 111 | |||
1353 | 112 | The user buffer will not be deallocated when this allocator is destructed. | ||
1354 | 113 | |||
1355 | 114 | \param buffer User supplied buffer. | ||
1356 | 115 | \param size Size of the buffer in bytes. It must at least larger than sizeof(ChunkHeader). | ||
1357 | 116 | \param chunkSize The size of memory chunk. The default is kDefaultChunkSize. | ||
1358 | 117 | \param baseAllocator The allocator for allocating memory chunks. | ||
1359 | 118 | */ | ||
1360 | 119 | MemoryPoolAllocator(void *buffer, size_t size, size_t chunkSize = kDefaultChunkCapacity, BaseAllocator* baseAllocator = 0) : | ||
1361 | 120 | chunkHead_(0), chunk_capacity_(chunkSize), userBuffer_(buffer), baseAllocator_(baseAllocator), ownBaseAllocator_(0) | ||
1362 | 121 | { | ||
1363 | 122 | RAPIDJSON_ASSERT(buffer != 0); | ||
1364 | 123 | RAPIDJSON_ASSERT(size > sizeof(ChunkHeader)); | ||
1365 | 124 | chunkHead_ = reinterpret_cast<ChunkHeader*>(buffer); | ||
1366 | 125 | chunkHead_->capacity = size - sizeof(ChunkHeader); | ||
1367 | 126 | chunkHead_->size = 0; | ||
1368 | 127 | chunkHead_->next = 0; | ||
1369 | 128 | } | ||
1370 | 129 | |||
1371 | 130 | //! Destructor. | ||
1372 | 131 | /*! This deallocates all memory chunks, excluding the user-supplied buffer. | ||
1373 | 132 | */ | ||
1374 | 133 | ~MemoryPoolAllocator() { | ||
1375 | 134 | Clear(); | ||
1376 | 135 | RAPIDJSON_DELETE(ownBaseAllocator_); | ||
1377 | 136 | } | ||
1378 | 137 | |||
1379 | 138 | //! Deallocates all memory chunks, excluding the user-supplied buffer. | ||
1380 | 139 | void Clear() { | ||
1381 | 140 | while(chunkHead_ != 0 && chunkHead_ != userBuffer_) { | ||
1382 | 141 | ChunkHeader* next = chunkHead_->next; | ||
1383 | 142 | baseAllocator_->Free(chunkHead_); | ||
1384 | 143 | chunkHead_ = next; | ||
1385 | 144 | } | ||
1386 | 145 | } | ||
1387 | 146 | |||
1388 | 147 | //! Computes the total capacity of allocated memory chunks. | ||
1389 | 148 | /*! \return total capacity in bytes. | ||
1390 | 149 | */ | ||
1391 | 150 | size_t Capacity() const { | ||
1392 | 151 | size_t capacity = 0; | ||
1393 | 152 | for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) | ||
1394 | 153 | capacity += c->capacity; | ||
1395 | 154 | return capacity; | ||
1396 | 155 | } | ||
1397 | 156 | |||
1398 | 157 | //! Computes the memory blocks allocated. | ||
1399 | 158 | /*! \return total used bytes. | ||
1400 | 159 | */ | ||
1401 | 160 | size_t Size() const { | ||
1402 | 161 | size_t size = 0; | ||
1403 | 162 | for (ChunkHeader* c = chunkHead_; c != 0; c = c->next) | ||
1404 | 163 | size += c->size; | ||
1405 | 164 | return size; | ||
1406 | 165 | } | ||
1407 | 166 | |||
1408 | 167 | //! Allocates a memory block. (concept Allocator) | ||
1409 | 168 | void* Malloc(size_t size) { | ||
1410 | 169 | size = RAPIDJSON_ALIGN(size); | ||
1411 | 170 | if (chunkHead_ == 0 || chunkHead_->size + size > chunkHead_->capacity) | ||
1412 | 171 | AddChunk(chunk_capacity_ > size ? chunk_capacity_ : size); | ||
1413 | 172 | |||
1414 | 173 | void *buffer = reinterpret_cast<char *>(chunkHead_ + 1) + chunkHead_->size; | ||
1415 | 174 | chunkHead_->size += size; | ||
1416 | 175 | return buffer; | ||
1417 | 176 | } | ||
1418 | 177 | |||
1419 | 178 | //! Resizes a memory block (concept Allocator) | ||
1420 | 179 | void* Realloc(void* originalPtr, size_t originalSize, size_t newSize) { | ||
1421 | 180 | if (originalPtr == 0) | ||
1422 | 181 | return Malloc(newSize); | ||
1423 | 182 | |||
1424 | 183 | // Do not shrink if new size is smaller than original | ||
1425 | 184 | if (originalSize >= newSize) | ||
1426 | 185 | return originalPtr; | ||
1427 | 186 | |||
1428 | 187 | // Simply expand it if it is the last allocation and there is sufficient space | ||
1429 | 188 | if (originalPtr == (char *)(chunkHead_ + 1) + chunkHead_->size - originalSize) { | ||
1430 | 189 | size_t increment = static_cast<size_t>(newSize - originalSize); | ||
1431 | 190 | increment = RAPIDJSON_ALIGN(increment); | ||
1432 | 191 | if (chunkHead_->size + increment <= chunkHead_->capacity) { | ||
1433 | 192 | chunkHead_->size += increment; | ||
1434 | 193 | return originalPtr; | ||
1435 | 194 | } | ||
1436 | 195 | } | ||
1437 | 196 | |||
1438 | 197 | // Realloc process: allocate and copy memory, do not free original buffer. | ||
1439 | 198 | void* newBuffer = Malloc(newSize); | ||
1440 | 199 | RAPIDJSON_ASSERT(newBuffer != 0); // Do not handle out-of-memory explicitly. | ||
1441 | 200 | return std::memcpy(newBuffer, originalPtr, originalSize); | ||
1442 | 201 | } | ||
1443 | 202 | |||
1444 | 203 | //! Frees a memory block (concept Allocator) | ||
1445 | 204 | static void Free(void *ptr) { (void)ptr; } // Do nothing | ||
1446 | 205 | |||
1447 | 206 | private: | ||
1448 | 207 | //! Copy constructor is not permitted. | ||
1449 | 208 | MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */; | ||
1450 | 209 | //! Copy assignment operator is not permitted. | ||
1451 | 210 | MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */; | ||
1452 | 211 | |||
1453 | 212 | //! Creates a new chunk. | ||
1454 | 213 | /*! \param capacity Capacity of the chunk in bytes. | ||
1455 | 214 | */ | ||
1456 | 215 | void AddChunk(size_t capacity) { | ||
1457 | 216 | if (!baseAllocator_) | ||
1458 | 217 | ownBaseAllocator_ = baseAllocator_ = RAPIDJSON_NEW(BaseAllocator()); | ||
1459 | 218 | ChunkHeader* chunk = reinterpret_cast<ChunkHeader*>(baseAllocator_->Malloc(sizeof(ChunkHeader) + capacity)); | ||
1460 | 219 | chunk->capacity = capacity; | ||
1461 | 220 | chunk->size = 0; | ||
1462 | 221 | chunk->next = chunkHead_; | ||
1463 | 222 | chunkHead_ = chunk; | ||
1464 | 223 | } | ||
1465 | 224 | |||
1466 | 225 | static const int kDefaultChunkCapacity = 64 * 1024; //!< Default chunk capacity. | ||
1467 | 226 | |||
1468 | 227 | //! Chunk header for perpending to each chunk. | ||
1469 | 228 | /*! Chunks are stored as a singly linked list. | ||
1470 | 229 | */ | ||
1471 | 230 | struct ChunkHeader { | ||
1472 | 231 | size_t capacity; //!< Capacity of the chunk in bytes (excluding the header itself). | ||
1473 | 232 | size_t size; //!< Current size of allocated memory in bytes. | ||
1474 | 233 | ChunkHeader *next; //!< Next chunk in the linked list. | ||
1475 | 234 | }; | ||
1476 | 235 | |||
1477 | 236 | ChunkHeader *chunkHead_; //!< Head of the chunk linked-list. Only the head chunk serves allocation. | ||
1478 | 237 | size_t chunk_capacity_; //!< The minimum capacity of chunk when they are allocated. | ||
1479 | 238 | void *userBuffer_; //!< User supplied buffer. | ||
1480 | 239 | BaseAllocator* baseAllocator_; //!< base allocator for allocating memory chunks. | ||
1481 | 240 | BaseAllocator* ownBaseAllocator_; //!< base allocator created by this object. | ||
1482 | 241 | }; | ||
1483 | 242 | |||
1484 | 243 | RAPIDJSON_NAMESPACE_END | ||
1485 | 244 | |||
1486 | 245 | #endif // RAPIDJSON_ENCODINGS_H_ | ||
1487 | 0 | 246 | ||
1488 | === added file 'shorts/xml2json/rapidjson/document.h' | |||
1489 | --- shorts/xml2json/rapidjson/document.h 1970-01-01 00:00:00 +0000 | |||
1490 | +++ shorts/xml2json/rapidjson/document.h 2015-12-15 16:05:21 +0000 | |||
1491 | @@ -0,0 +1,1969 @@ | |||
1492 | 1 | // Copyright (C) 2011 Milo Yip | ||
1493 | 2 | // | ||
1494 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
1495 | 4 | // of this software and associated documentation files (the "Software"), to deal | ||
1496 | 5 | // in the Software without restriction, including without limitation the rights | ||
1497 | 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
1498 | 7 | // copies of the Software, and to permit persons to whom the Software is | ||
1499 | 8 | // furnished to do so, subject to the following conditions: | ||
1500 | 9 | // | ||
1501 | 10 | // The above copyright notice and this permission notice shall be included in | ||
1502 | 11 | // all copies or substantial portions of the Software. | ||
1503 | 12 | // | ||
1504 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
1505 | 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
1506 | 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
1507 | 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
1508 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
1509 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
1510 | 19 | // THE SOFTWARE. | ||
1511 | 20 | |||
1512 | 21 | #ifndef RAPIDJSON_DOCUMENT_H_ | ||
1513 | 22 | #define RAPIDJSON_DOCUMENT_H_ | ||
1514 | 23 | |||
1515 | 24 | /*! \file document.h */ | ||
1516 | 25 | |||
1517 | 26 | #include "reader.h" | ||
1518 | 27 | #include "internal/meta.h" | ||
1519 | 28 | #include "internal/strfunc.h" | ||
1520 | 29 | #include <new> // placement new | ||
1521 | 30 | |||
1522 | 31 | #ifdef _MSC_VER | ||
1523 | 32 | RAPIDJSON_DIAG_PUSH | ||
1524 | 33 | RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant | ||
1525 | 34 | #elif defined(__GNUC__) | ||
1526 | 35 | RAPIDJSON_DIAG_PUSH | ||
1527 | 36 | RAPIDJSON_DIAG_OFF(effc++) | ||
1528 | 37 | #endif | ||
1529 | 38 | |||
1530 | 39 | /////////////////////////////////////////////////////////////////////////////// | ||
1531 | 40 | // RAPIDJSON_HAS_STDSTRING | ||
1532 | 41 | |||
1533 | 42 | #ifndef RAPIDJSON_HAS_STDSTRING | ||
1534 | 43 | #ifdef RAPIDJSON_DOXYGEN_RUNNING | ||
1535 | 44 | #define RAPIDJSON_HAS_STDSTRING 1 // force generation of documentation | ||
1536 | 45 | #else | ||
1537 | 46 | #define RAPIDJSON_HAS_STDSTRING 0 // no std::string support by default | ||
1538 | 47 | #endif | ||
1539 | 48 | /*! \def RAPIDJSON_HAS_STDSTRING | ||
1540 | 49 | \ingroup RAPIDJSON_CONFIG | ||
1541 | 50 | \brief Enable RapidJSON support for \c std::string | ||
1542 | 51 | |||
1543 | 52 | By defining this preprocessor symbol to \c 1, several convenience functions for using | ||
1544 | 53 | \ref rapidjson::GenericValue with \c std::string are enabled, especially | ||
1545 | 54 | for construction and comparison. | ||
1546 | 55 | |||
1547 | 56 | \hideinitializer | ||
1548 | 57 | */ | ||
1549 | 58 | #include <string> | ||
1550 | 59 | #endif // RAPIDJSON_HAS_STDSTRING | ||
1551 | 60 | |||
1552 | 61 | #ifndef RAPIDJSON_NOMEMBERITERATORCLASS | ||
1553 | 62 | #include <iterator> // std::iterator, std::random_access_iterator_tag | ||
1554 | 63 | #endif | ||
1555 | 64 | |||
1556 | 65 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS | ||
1557 | 66 | #include <utility> // std::move | ||
1558 | 67 | #endif | ||
1559 | 68 | |||
1560 | 69 | RAPIDJSON_NAMESPACE_BEGIN | ||
1561 | 70 | |||
1562 | 71 | // Forward declaration. | ||
1563 | 72 | template <typename Encoding, typename Allocator> | ||
1564 | 73 | class GenericValue; | ||
1565 | 74 | |||
1566 | 75 | //! Name-value pair in a JSON object value. | ||
1567 | 76 | /*! | ||
1568 | 77 | This class was internal to GenericValue. It used to be a inner struct. | ||
1569 | 78 | But a compiler (IBM XL C/C++ for AIX) have reported to have problem with that so it moved as a namespace scope struct. | ||
1570 | 79 | https://code.google.com/p/rapidjson/issues/detail?id=64 | ||
1571 | 80 | */ | ||
1572 | 81 | template <typename Encoding, typename Allocator> | ||
1573 | 82 | struct GenericMember { | ||
1574 | 83 | GenericValue<Encoding, Allocator> name; //!< name of member (must be a string) | ||
1575 | 84 | GenericValue<Encoding, Allocator> value; //!< value of member. | ||
1576 | 85 | }; | ||
1577 | 86 | |||
1578 | 87 | /////////////////////////////////////////////////////////////////////////////// | ||
1579 | 88 | // GenericMemberIterator | ||
1580 | 89 | |||
1581 | 90 | #ifndef RAPIDJSON_NOMEMBERITERATORCLASS | ||
1582 | 91 | |||
1583 | 92 | //! (Constant) member iterator for a JSON object value | ||
1584 | 93 | /*! | ||
1585 | 94 | \tparam Const Is this a constant iterator? | ||
1586 | 95 | \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document) | ||
1587 | 96 | \tparam Allocator Allocator type for allocating memory of object, array and string. | ||
1588 | 97 | |||
1589 | 98 | This class implements a Random Access Iterator for GenericMember elements | ||
1590 | 99 | of a GenericValue, see ISO/IEC 14882:2003(E) C++ standard, 24.1 [lib.iterator.requirements]. | ||
1591 | 100 | |||
1592 | 101 | \note This iterator implementation is mainly intended to avoid implicit | ||
1593 | 102 | conversions from iterator values to \c NULL, | ||
1594 | 103 | e.g. from GenericValue::FindMember. | ||
1595 | 104 | |||
1596 | 105 | \note Define \c RAPIDJSON_NOMEMBERITERATORCLASS to fall back to a | ||
1597 | 106 | pointer-based implementation, if your platform doesn't provide | ||
1598 | 107 | the C++ <iterator> header. | ||
1599 | 108 | |||
1600 | 109 | \see GenericMember, GenericValue::MemberIterator, GenericValue::ConstMemberIterator | ||
1601 | 110 | */ | ||
1602 | 111 | template <bool Const, typename Encoding, typename Allocator> | ||
1603 | 112 | class GenericMemberIterator | ||
1604 | 113 | : public std::iterator<std::random_access_iterator_tag | ||
1605 | 114 | , typename internal::MaybeAddConst<Const,GenericMember<Encoding,Allocator> >::Type> { | ||
1606 | 115 | |||
1607 | 116 | friend class GenericValue<Encoding,Allocator>; | ||
1608 | 117 | template <bool, typename, typename> friend class GenericMemberIterator; | ||
1609 | 118 | |||
1610 | 119 | typedef GenericMember<Encoding,Allocator> PlainType; | ||
1611 | 120 | typedef typename internal::MaybeAddConst<Const,PlainType>::Type ValueType; | ||
1612 | 121 | typedef std::iterator<std::random_access_iterator_tag,ValueType> BaseType; | ||
1613 | 122 | |||
1614 | 123 | public: | ||
1615 | 124 | //! Iterator type itself | ||
1616 | 125 | typedef GenericMemberIterator Iterator; | ||
1617 | 126 | //! Constant iterator type | ||
1618 | 127 | typedef GenericMemberIterator<true,Encoding,Allocator> ConstIterator; | ||
1619 | 128 | //! Non-constant iterator type | ||
1620 | 129 | typedef GenericMemberIterator<false,Encoding,Allocator> NonConstIterator; | ||
1621 | 130 | |||
1622 | 131 | //! Pointer to (const) GenericMember | ||
1623 | 132 | typedef typename BaseType::pointer Pointer; | ||
1624 | 133 | //! Reference to (const) GenericMember | ||
1625 | 134 | typedef typename BaseType::reference Reference; | ||
1626 | 135 | //! Signed integer type (e.g. \c ptrdiff_t) | ||
1627 | 136 | typedef typename BaseType::difference_type DifferenceType; | ||
1628 | 137 | |||
1629 | 138 | //! Default constructor (singular value) | ||
1630 | 139 | /*! Creates an iterator pointing to no element. | ||
1631 | 140 | \note All operations, except for comparisons, are undefined on such values. | ||
1632 | 141 | */ | ||
1633 | 142 | GenericMemberIterator() : ptr_() {} | ||
1634 | 143 | |||
1635 | 144 | //! Iterator conversions to more const | ||
1636 | 145 | /*! | ||
1637 | 146 | \param it (Non-const) iterator to copy from | ||
1638 | 147 | |||
1639 | 148 | Allows the creation of an iterator from another GenericMemberIterator | ||
1640 | 149 | that is "less const". Especially, creating a non-constant iterator | ||
1641 | 150 | from a constant iterator are disabled: | ||
1642 | 151 | \li const -> non-const (not ok) | ||
1643 | 152 | \li const -> const (ok) | ||
1644 | 153 | \li non-const -> const (ok) | ||
1645 | 154 | \li non-const -> non-const (ok) | ||
1646 | 155 | |||
1647 | 156 | \note If the \c Const template parameter is already \c false, this | ||
1648 | 157 | constructor effectively defines a regular copy-constructor. | ||
1649 | 158 | Otherwise, the copy constructor is implicitly defined. | ||
1650 | 159 | */ | ||
1651 | 160 | GenericMemberIterator(const NonConstIterator & it) : ptr_(it.ptr_) {} | ||
1652 | 161 | |||
1653 | 162 | //! @name stepping | ||
1654 | 163 | //@{ | ||
1655 | 164 | Iterator& operator++(){ ++ptr_; return *this; } | ||
1656 | 165 | Iterator& operator--(){ --ptr_; return *this; } | ||
1657 | 166 | Iterator operator++(int){ Iterator old(*this); ++ptr_; return old; } | ||
1658 | 167 | Iterator operator--(int){ Iterator old(*this); --ptr_; return old; } | ||
1659 | 168 | //@} | ||
1660 | 169 | |||
1661 | 170 | //! @name increment/decrement | ||
1662 | 171 | //@{ | ||
1663 | 172 | Iterator operator+(DifferenceType n) const { return Iterator(ptr_+n); } | ||
1664 | 173 | Iterator operator-(DifferenceType n) const { return Iterator(ptr_-n); } | ||
1665 | 174 | |||
1666 | 175 | Iterator& operator+=(DifferenceType n) { ptr_+=n; return *this; } | ||
1667 | 176 | Iterator& operator-=(DifferenceType n) { ptr_-=n; return *this; } | ||
1668 | 177 | //@} | ||
1669 | 178 | |||
1670 | 179 | //! @name relations | ||
1671 | 180 | //@{ | ||
1672 | 181 | bool operator==(ConstIterator that) const { return ptr_ == that.ptr_; } | ||
1673 | 182 | bool operator!=(ConstIterator that) const { return ptr_ != that.ptr_; } | ||
1674 | 183 | bool operator<=(ConstIterator that) const { return ptr_ <= that.ptr_; } | ||
1675 | 184 | bool operator>=(ConstIterator that) const { return ptr_ >= that.ptr_; } | ||
1676 | 185 | bool operator< (ConstIterator that) const { return ptr_ < that.ptr_; } | ||
1677 | 186 | bool operator> (ConstIterator that) const { return ptr_ > that.ptr_; } | ||
1678 | 187 | //@} | ||
1679 | 188 | |||
1680 | 189 | //! @name dereference | ||
1681 | 190 | //@{ | ||
1682 | 191 | Reference operator*() const { return *ptr_; } | ||
1683 | 192 | Pointer operator->() const { return ptr_; } | ||
1684 | 193 | Reference operator[](DifferenceType n) const { return ptr_[n]; } | ||
1685 | 194 | //@} | ||
1686 | 195 | |||
1687 | 196 | //! Distance | ||
1688 | 197 | DifferenceType operator-(ConstIterator that) const { return ptr_-that.ptr_; } | ||
1689 | 198 | |||
1690 | 199 | private: | ||
1691 | 200 | //! Internal constructor from plain pointer | ||
1692 | 201 | explicit GenericMemberIterator(Pointer p) : ptr_(p) {} | ||
1693 | 202 | |||
1694 | 203 | Pointer ptr_; //!< raw pointer | ||
1695 | 204 | }; | ||
1696 | 205 | |||
1697 | 206 | #else // RAPIDJSON_NOMEMBERITERATORCLASS | ||
1698 | 207 | |||
1699 | 208 | // class-based member iterator implementation disabled, use plain pointers | ||
1700 | 209 | |||
1701 | 210 | template <bool Const, typename Encoding, typename Allocator> | ||
1702 | 211 | struct GenericMemberIterator; | ||
1703 | 212 | |||
1704 | 213 | //! non-const GenericMemberIterator | ||
1705 | 214 | template <typename Encoding, typename Allocator> | ||
1706 | 215 | struct GenericMemberIterator<false,Encoding,Allocator> { | ||
1707 | 216 | //! use plain pointer as iterator type | ||
1708 | 217 | typedef GenericMember<Encoding,Allocator>* Iterator; | ||
1709 | 218 | }; | ||
1710 | 219 | //! const GenericMemberIterator | ||
1711 | 220 | template <typename Encoding, typename Allocator> | ||
1712 | 221 | struct GenericMemberIterator<true,Encoding,Allocator> { | ||
1713 | 222 | //! use plain const pointer as iterator type | ||
1714 | 223 | typedef const GenericMember<Encoding,Allocator>* Iterator; | ||
1715 | 224 | }; | ||
1716 | 225 | |||
1717 | 226 | #endif // RAPIDJSON_NOMEMBERITERATORCLASS | ||
1718 | 227 | |||
1719 | 228 | /////////////////////////////////////////////////////////////////////////////// | ||
1720 | 229 | // GenericStringRef | ||
1721 | 230 | |||
1722 | 231 | //! Reference to a constant string (not taking a copy) | ||
1723 | 232 | /*! | ||
1724 | 233 | \tparam CharType character type of the string | ||
1725 | 234 | |||
1726 | 235 | This helper class is used to automatically infer constant string | ||
1727 | 236 | references for string literals, especially from \c const \b (!) | ||
1728 | 237 | character arrays. | ||
1729 | 238 | |||
1730 | 239 | The main use is for creating JSON string values without copying the | ||
1731 | 240 | source string via an \ref Allocator. This requires that the referenced | ||
1732 | 241 | string pointers have a sufficient lifetime, which exceeds the lifetime | ||
1733 | 242 | of the associated GenericValue. | ||
1734 | 243 | |||
1735 | 244 | \b Example | ||
1736 | 245 | \code | ||
1737 | 246 | Value v("foo"); // ok, no need to copy & calculate length | ||
1738 | 247 | const char foo[] = "foo"; | ||
1739 | 248 | v.SetString(foo); // ok | ||
1740 | 249 | |||
1741 | 250 | const char* bar = foo; | ||
1742 | 251 | // Value x(bar); // not ok, can't rely on bar's lifetime | ||
1743 | 252 | Value x(StringRef(bar)); // lifetime explicitly guaranteed by user | ||
1744 | 253 | Value y(StringRef(bar, 3)); // ok, explicitly pass length | ||
1745 | 254 | \endcode | ||
1746 | 255 | |||
1747 | 256 | \see StringRef, GenericValue::SetString | ||
1748 | 257 | */ | ||
1749 | 258 | template<typename CharType> | ||
1750 | 259 | struct GenericStringRef { | ||
1751 | 260 | typedef CharType Ch; //!< character type of the string | ||
1752 | 261 | |||
1753 | 262 | //! Create string reference from \c const character array | ||
1754 | 263 | /*! | ||
1755 | 264 | This constructor implicitly creates a constant string reference from | ||
1756 | 265 | a \c const character array. It has better performance than | ||
1757 | 266 | \ref StringRef(const CharType*) by inferring the string \ref length | ||
1758 | 267 | from the array length, and also supports strings containing null | ||
1759 | 268 | characters. | ||
1760 | 269 | |||
1761 | 270 | \tparam N length of the string, automatically inferred | ||
1762 | 271 | |||
1763 | 272 | \param str Constant character array, lifetime assumed to be longer | ||
1764 | 273 | than the use of the string in e.g. a GenericValue | ||
1765 | 274 | |||
1766 | 275 | \post \ref s == str | ||
1767 | 276 | |||
1768 | 277 | \note Constant complexity. | ||
1769 | 278 | \note There is a hidden, private overload to disallow references to | ||
1770 | 279 | non-const character arrays to be created via this constructor. | ||
1771 | 280 | By this, e.g. function-scope arrays used to be filled via | ||
1772 | 281 | \c snprintf are excluded from consideration. | ||
1773 | 282 | In such cases, the referenced string should be \b copied to the | ||
1774 | 283 | GenericValue instead. | ||
1775 | 284 | */ | ||
1776 | 285 | template<SizeType N> | ||
1777 | 286 | GenericStringRef(const CharType (&str)[N]) RAPIDJSON_NOEXCEPT | ||
1778 | 287 | : s(str), length(N-1) {} | ||
1779 | 288 | |||
1780 | 289 | //! Explicitly create string reference from \c const character pointer | ||
1781 | 290 | /*! | ||
1782 | 291 | This constructor can be used to \b explicitly create a reference to | ||
1783 | 292 | a constant string pointer. | ||
1784 | 293 | |||
1785 | 294 | \see StringRef(const CharType*) | ||
1786 | 295 | |||
1787 | 296 | \param str Constant character pointer, lifetime assumed to be longer | ||
1788 | 297 | than the use of the string in e.g. a GenericValue | ||
1789 | 298 | |||
1790 | 299 | \post \ref s == str | ||
1791 | 300 | |||
1792 | 301 | \note There is a hidden, private overload to disallow references to | ||
1793 | 302 | non-const character arrays to be created via this constructor. | ||
1794 | 303 | By this, e.g. function-scope arrays used to be filled via | ||
1795 | 304 | \c snprintf are excluded from consideration. | ||
1796 | 305 | In such cases, the referenced string should be \b copied to the | ||
1797 | 306 | GenericValue instead. | ||
1798 | 307 | */ | ||
1799 | 308 | explicit GenericStringRef(const CharType* str) | ||
1800 | 309 | : s(str), length(internal::StrLen(str)){ RAPIDJSON_ASSERT(s != NULL); } | ||
1801 | 310 | |||
1802 | 311 | //! Create constant string reference from pointer and length | ||
1803 | 312 | /*! \param str constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue | ||
1804 | 313 | \param len length of the string, excluding the trailing NULL terminator | ||
1805 | 314 | |||
1806 | 315 | \post \ref s == str && \ref length == len | ||
1807 | 316 | \note Constant complexity. | ||
1808 | 317 | */ | ||
1809 | 318 | GenericStringRef(const CharType* str, SizeType len) | ||
1810 | 319 | : s(str), length(len) { RAPIDJSON_ASSERT(s != NULL); } | ||
1811 | 320 | |||
1812 | 321 | //! implicit conversion to plain CharType pointer | ||
1813 | 322 | operator const Ch *() const { return s; } | ||
1814 | 323 | |||
1815 | 324 | const Ch* const s; //!< plain CharType pointer | ||
1816 | 325 | const SizeType length; //!< length of the string (excluding the trailing NULL terminator) | ||
1817 | 326 | |||
1818 | 327 | private: | ||
1819 | 328 | //! Disallow copy-assignment | ||
1820 | 329 | GenericStringRef operator=(const GenericStringRef&); | ||
1821 | 330 | //! Disallow construction from non-const array | ||
1822 | 331 | template<SizeType N> | ||
1823 | 332 | GenericStringRef(CharType (&str)[N]) /* = delete */; | ||
1824 | 333 | }; | ||
1825 | 334 | |||
1826 | 335 | //! Mark a character pointer as constant string | ||
1827 | 336 | /*! Mark a plain character pointer as a "string literal". This function | ||
1828 | 337 | can be used to avoid copying a character string to be referenced as a | ||
1829 | 338 | value in a JSON GenericValue object, if the string's lifetime is known | ||
1830 | 339 | to be valid long enough. | ||
1831 | 340 | \tparam CharType Character type of the string | ||
1832 | 341 | \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue | ||
1833 | 342 | \return GenericStringRef string reference object | ||
1834 | 343 | \relatesalso GenericStringRef | ||
1835 | 344 | |||
1836 | 345 | \see GenericValue::GenericValue(StringRefType), GenericValue::operator=(StringRefType), GenericValue::SetString(StringRefType), GenericValue::PushBack(StringRefType, Allocator&), GenericValue::AddMember | ||
1837 | 346 | */ | ||
1838 | 347 | template<typename CharType> | ||
1839 | 348 | inline GenericStringRef<CharType> StringRef(const CharType* str) { | ||
1840 | 349 | return GenericStringRef<CharType>(str, internal::StrLen(str)); | ||
1841 | 350 | } | ||
1842 | 351 | |||
1843 | 352 | //! Mark a character pointer as constant string | ||
1844 | 353 | /*! Mark a plain character pointer as a "string literal". This function | ||
1845 | 354 | can be used to avoid copying a character string to be referenced as a | ||
1846 | 355 | value in a JSON GenericValue object, if the string's lifetime is known | ||
1847 | 356 | to be valid long enough. | ||
1848 | 357 | |||
1849 | 358 | This version has better performance with supplied length, and also | ||
1850 | 359 | supports string containing null characters. | ||
1851 | 360 | |||
1852 | 361 | \tparam CharType character type of the string | ||
1853 | 362 | \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue | ||
1854 | 363 | \param length The length of source string. | ||
1855 | 364 | \return GenericStringRef string reference object | ||
1856 | 365 | \relatesalso GenericStringRef | ||
1857 | 366 | */ | ||
1858 | 367 | template<typename CharType> | ||
1859 | 368 | inline GenericStringRef<CharType> StringRef(const CharType* str, size_t length) { | ||
1860 | 369 | return GenericStringRef<CharType>(str, SizeType(length)); | ||
1861 | 370 | } | ||
1862 | 371 | |||
1863 | 372 | #if RAPIDJSON_HAS_STDSTRING | ||
1864 | 373 | //! Mark a string object as constant string | ||
1865 | 374 | /*! Mark a string object (e.g. \c std::string) as a "string literal". | ||
1866 | 375 | This function can be used to avoid copying a string to be referenced as a | ||
1867 | 376 | value in a JSON GenericValue object, if the string's lifetime is known | ||
1868 | 377 | to be valid long enough. | ||
1869 | 378 | |||
1870 | 379 | \tparam CharType character type of the string | ||
1871 | 380 | \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue | ||
1872 | 381 | \return GenericStringRef string reference object | ||
1873 | 382 | \relatesalso GenericStringRef | ||
1874 | 383 | \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. | ||
1875 | 384 | */ | ||
1876 | 385 | template<typename CharType> | ||
1877 | 386 | inline GenericStringRef<CharType> StringRef(const std::basic_string<CharType>& str) { | ||
1878 | 387 | return GenericStringRef<CharType>(str.data(), SizeType(str.size())); | ||
1879 | 388 | } | ||
1880 | 389 | #endif | ||
1881 | 390 | |||
1882 | 391 | /////////////////////////////////////////////////////////////////////////////// | ||
1883 | 392 | // GenericValue type traits | ||
1884 | 393 | namespace internal { | ||
1885 | 394 | |||
1886 | 395 | template <typename T, typename Encoding = void, typename Allocator = void> | ||
1887 | 396 | struct IsGenericValueImpl : FalseType {}; | ||
1888 | 397 | |||
1889 | 398 | // select candidates according to nested encoding and allocator types | ||
1890 | 399 | template <typename T> struct IsGenericValueImpl<T, typename Void<typename T::EncodingType>::Type, typename Void<typename T::AllocatorType>::Type> | ||
1891 | 400 | : IsBaseOf<GenericValue<typename T::EncodingType, typename T::AllocatorType>, T>::Type {}; | ||
1892 | 401 | |||
1893 | 402 | // helper to match arbitrary GenericValue instantiations, including derived classes | ||
1894 | 403 | template <typename T> struct IsGenericValue : IsGenericValueImpl<T>::Type {}; | ||
1895 | 404 | |||
1896 | 405 | } // namespace internal | ||
1897 | 406 | |||
1898 | 407 | /////////////////////////////////////////////////////////////////////////////// | ||
1899 | 408 | // GenericValue | ||
1900 | 409 | |||
1901 | 410 | //! Represents a JSON value. Use Value for UTF8 encoding and default allocator. | ||
1902 | 411 | /*! | ||
1903 | 412 | A JSON value can be one of 7 types. This class is a variant type supporting | ||
1904 | 413 | these types. | ||
1905 | 414 | |||
1906 | 415 | Use the Value if UTF8 and default allocator | ||
1907 | 416 | |||
1908 | 417 | \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document) | ||
1909 | 418 | \tparam Allocator Allocator type for allocating memory of object, array and string. | ||
1910 | 419 | */ | ||
1911 | 420 | template <typename Encoding, typename Allocator = MemoryPoolAllocator<> > | ||
1912 | 421 | class GenericValue { | ||
1913 | 422 | public: | ||
1914 | 423 | //! Name-value pair in an object. | ||
1915 | 424 | typedef GenericMember<Encoding, Allocator> Member; | ||
1916 | 425 | typedef Encoding EncodingType; //!< Encoding type from template parameter. | ||
1917 | 426 | typedef Allocator AllocatorType; //!< Allocator type from template parameter. | ||
1918 | 427 | typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. | ||
1919 | 428 | typedef GenericStringRef<Ch> StringRefType; //!< Reference to a constant string | ||
1920 | 429 | typedef typename GenericMemberIterator<false,Encoding,Allocator>::Iterator MemberIterator; //!< Member iterator for iterating in object. | ||
1921 | 430 | typedef typename GenericMemberIterator<true,Encoding,Allocator>::Iterator ConstMemberIterator; //!< Constant member iterator for iterating in object. | ||
1922 | 431 | typedef GenericValue* ValueIterator; //!< Value iterator for iterating in array. | ||
1923 | 432 | typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array. | ||
1924 | 433 | |||
1925 | 434 | //!@name Constructors and destructor. | ||
1926 | 435 | //@{ | ||
1927 | 436 | |||
1928 | 437 | //! Default constructor creates a null value. | ||
1929 | 438 | GenericValue() RAPIDJSON_NOEXCEPT : data_(), flags_(kNullFlag) {} | ||
1930 | 439 | |||
1931 | 440 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS | ||
1932 | 441 | //! Move constructor in C++11 | ||
1933 | 442 | GenericValue(GenericValue&& rhs) RAPIDJSON_NOEXCEPT : data_(rhs.data_), flags_(rhs.flags_) { | ||
1934 | 443 | rhs.flags_ = kNullFlag; // give up contents | ||
1935 | 444 | } | ||
1936 | 445 | #endif | ||
1937 | 446 | |||
1938 | 447 | private: | ||
1939 | 448 | //! Copy constructor is not permitted. | ||
1940 | 449 | GenericValue(const GenericValue& rhs); | ||
1941 | 450 | |||
1942 | 451 | public: | ||
1943 | 452 | |||
1944 | 453 | //! Constructor with JSON value type. | ||
1945 | 454 | /*! This creates a Value of specified type with default content. | ||
1946 | 455 | \param type Type of the value. | ||
1947 | 456 | \note Default content for number is zero. | ||
1948 | 457 | */ | ||
1949 | 458 | explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_(), flags_() { | ||
1950 | 459 | static const unsigned defaultFlags[7] = { | ||
1951 | 460 | kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag, | ||
1952 | 461 | kNumberAnyFlag | ||
1953 | 462 | }; | ||
1954 | 463 | RAPIDJSON_ASSERT(type <= kNumberType); | ||
1955 | 464 | flags_ = defaultFlags[type]; | ||
1956 | 465 | |||
1957 | 466 | // Use ShortString to store empty string. | ||
1958 | 467 | if (type == kStringType) | ||
1959 | 468 | data_.ss.SetLength(0); | ||
1960 | 469 | } | ||
1961 | 470 | |||
1962 | 471 | //! Explicit copy constructor (with allocator) | ||
1963 | 472 | /*! Creates a copy of a Value by using the given Allocator | ||
1964 | 473 | \tparam SourceAllocator allocator of \c rhs | ||
1965 | 474 | \param rhs Value to copy from (read-only) | ||
1966 | 475 | \param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator(). | ||
1967 | 476 | \see CopyFrom() | ||
1968 | 477 | */ | ||
1969 | 478 | template< typename SourceAllocator > | ||
1970 | 479 | GenericValue(const GenericValue<Encoding, SourceAllocator>& rhs, Allocator & allocator); | ||
1971 | 480 | |||
1972 | 481 | //! Constructor for boolean value. | ||
1973 | 482 | /*! \param b Boolean value | ||
1974 | 483 | \note This constructor is limited to \em real boolean values and rejects | ||
1975 | 484 | implicitly converted types like arbitrary pointers. Use an explicit cast | ||
1976 | 485 | to \c bool, if you want to construct a boolean JSON value in such cases. | ||
1977 | 486 | */ | ||
1978 | 487 | #ifndef RAPIDJSON_DOXYGEN_RUNNING // hide SFINAE from Doxygen | ||
1979 | 488 | template <typename T> | ||
1980 | 489 | explicit GenericValue(T b, RAPIDJSON_ENABLEIF((internal::IsSame<T,bool>))) RAPIDJSON_NOEXCEPT | ||
1981 | 490 | #else | ||
1982 | 491 | explicit GenericValue(bool b) RAPIDJSON_NOEXCEPT | ||
1983 | 492 | #endif | ||
1984 | 493 | : data_(), flags_(b ? kTrueFlag : kFalseFlag) { | ||
1985 | 494 | // safe-guard against failing SFINAE | ||
1986 | 495 | RAPIDJSON_STATIC_ASSERT((internal::IsSame<bool,T>::Value)); | ||
1987 | 496 | } | ||
1988 | 497 | |||
1989 | 498 | //! Constructor for int value. | ||
1990 | 499 | explicit GenericValue(int i) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberIntFlag) { | ||
1991 | 500 | data_.n.i64 = i; | ||
1992 | 501 | if (i >= 0) | ||
1993 | 502 | flags_ |= kUintFlag | kUint64Flag; | ||
1994 | 503 | } | ||
1995 | 504 | |||
1996 | 505 | //! Constructor for unsigned value. | ||
1997 | 506 | explicit GenericValue(unsigned u) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberUintFlag) { | ||
1998 | 507 | data_.n.u64 = u; | ||
1999 | 508 | if (!(u & 0x80000000)) | ||
2000 | 509 | flags_ |= kIntFlag | kInt64Flag; | ||
2001 | 510 | } | ||
2002 | 511 | |||
2003 | 512 | //! Constructor for int64_t value. | ||
2004 | 513 | explicit GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberInt64Flag) { | ||
2005 | 514 | data_.n.i64 = i64; | ||
2006 | 515 | if (i64 >= 0) { | ||
2007 | 516 | flags_ |= kNumberUint64Flag; | ||
2008 | 517 | if (!(static_cast<uint64_t>(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000))) | ||
2009 | 518 | flags_ |= kUintFlag; | ||
2010 | 519 | if (!(static_cast<uint64_t>(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) | ||
2011 | 520 | flags_ |= kIntFlag; | ||
2012 | 521 | } | ||
2013 | 522 | else if (i64 >= static_cast<int64_t>(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) | ||
2014 | 523 | flags_ |= kIntFlag; | ||
2015 | 524 | } | ||
2016 | 525 | |||
2017 | 526 | //! Constructor for uint64_t value. | ||
2018 | 527 | explicit GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberUint64Flag) { | ||
2019 | 528 | data_.n.u64 = u64; | ||
2020 | 529 | if (!(u64 & RAPIDJSON_UINT64_C2(0x80000000, 0x00000000))) | ||
2021 | 530 | flags_ |= kInt64Flag; | ||
2022 | 531 | if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000))) | ||
2023 | 532 | flags_ |= kUintFlag; | ||
2024 | 533 | if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000))) | ||
2025 | 534 | flags_ |= kIntFlag; | ||
2026 | 535 | } | ||
2027 | 536 | |||
2028 | 537 | //! Constructor for double value. | ||
2029 | 538 | explicit GenericValue(double d) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberDoubleFlag) { data_.n.d = d; } | ||
2030 | 539 | |||
2031 | 540 | //! Constructor for constant string (i.e. do not make a copy of string) | ||
2032 | 541 | GenericValue(const Ch* s, SizeType length) RAPIDJSON_NOEXCEPT : data_(), flags_() { SetStringRaw(StringRef(s, length)); } | ||
2033 | 542 | |||
2034 | 543 | //! Constructor for constant string (i.e. do not make a copy of string) | ||
2035 | 544 | explicit GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT : data_(), flags_() { SetStringRaw(s); } | ||
2036 | 545 | |||
2037 | 546 | //! Constructor for copy-string (i.e. do make a copy of string) | ||
2038 | 547 | GenericValue(const Ch* s, SizeType length, Allocator& allocator) : data_(), flags_() { SetStringRaw(StringRef(s, length), allocator); } | ||
2039 | 548 | |||
2040 | 549 | //! Constructor for copy-string (i.e. do make a copy of string) | ||
2041 | 550 | GenericValue(const Ch*s, Allocator& allocator) : data_(), flags_() { SetStringRaw(StringRef(s), allocator); } | ||
2042 | 551 | |||
2043 | 552 | #if RAPIDJSON_HAS_STDSTRING | ||
2044 | 553 | //! Constructor for copy-string from a string object (i.e. do make a copy of string) | ||
2045 | 554 | /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. | ||
2046 | 555 | */ | ||
2047 | 556 | GenericValue(const std::basic_string<Ch>& s, Allocator& allocator) : data_(), flags_() { SetStringRaw(StringRef(s), allocator); } | ||
2048 | 557 | #endif | ||
2049 | 558 | |||
2050 | 559 | //! Destructor. | ||
2051 | 560 | /*! Need to destruct elements of array, members of object, or copy-string. | ||
2052 | 561 | */ | ||
2053 | 562 | ~GenericValue() { | ||
2054 | 563 | if (Allocator::kNeedFree) { // Shortcut by Allocator's trait | ||
2055 | 564 | switch(flags_) { | ||
2056 | 565 | case kArrayFlag: | ||
2057 | 566 | for (GenericValue* v = data_.a.elements; v != data_.a.elements + data_.a.size; ++v) | ||
2058 | 567 | v->~GenericValue(); | ||
2059 | 568 | Allocator::Free(data_.a.elements); | ||
2060 | 569 | break; | ||
2061 | 570 | |||
2062 | 571 | case kObjectFlag: | ||
2063 | 572 | for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) | ||
2064 | 573 | m->~Member(); | ||
2065 | 574 | Allocator::Free(data_.o.members); | ||
2066 | 575 | break; | ||
2067 | 576 | |||
2068 | 577 | case kCopyStringFlag: | ||
2069 | 578 | Allocator::Free(const_cast<Ch*>(data_.s.str)); | ||
2070 | 579 | break; | ||
2071 | 580 | |||
2072 | 581 | default: | ||
2073 | 582 | break; // Do nothing for other types. | ||
2074 | 583 | } | ||
2075 | 584 | } | ||
2076 | 585 | } | ||
2077 | 586 | |||
2078 | 587 | //@} | ||
2079 | 588 | |||
2080 | 589 | //!@name Assignment operators | ||
2081 | 590 | //@{ | ||
2082 | 591 | |||
2083 | 592 | //! Assignment with move semantics. | ||
2084 | 593 | /*! \param rhs Source of the assignment. It will become a null value after assignment. | ||
2085 | 594 | */ | ||
2086 | 595 | GenericValue& operator=(GenericValue& rhs) RAPIDJSON_NOEXCEPT { | ||
2087 | 596 | RAPIDJSON_ASSERT(this != &rhs); | ||
2088 | 597 | this->~GenericValue(); | ||
2089 | 598 | RawAssign(rhs); | ||
2090 | 599 | return *this; | ||
2091 | 600 | } | ||
2092 | 601 | |||
2093 | 602 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS | ||
2094 | 603 | //! Move assignment in C++11 | ||
2095 | 604 | GenericValue& operator=(GenericValue&& rhs) RAPIDJSON_NOEXCEPT { | ||
2096 | 605 | return *this = rhs.Move(); | ||
2097 | 606 | } | ||
2098 | 607 | #endif | ||
2099 | 608 | |||
2100 | 609 | //! Assignment of constant string reference (no copy) | ||
2101 | 610 | /*! \param str Constant string reference to be assigned | ||
2102 | 611 | \note This overload is needed to avoid clashes with the generic primitive type assignment overload below. | ||
2103 | 612 | \see GenericStringRef, operator=(T) | ||
2104 | 613 | */ | ||
2105 | 614 | GenericValue& operator=(StringRefType str) RAPIDJSON_NOEXCEPT { | ||
2106 | 615 | GenericValue s(str); | ||
2107 | 616 | return *this = s; | ||
2108 | 617 | } | ||
2109 | 618 | |||
2110 | 619 | //! Assignment with primitive types. | ||
2111 | 620 | /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t | ||
2112 | 621 | \param value The value to be assigned. | ||
2113 | 622 | |||
2114 | 623 | \note The source type \c T explicitly disallows all pointer types, | ||
2115 | 624 | especially (\c const) \ref Ch*. This helps avoiding implicitly | ||
2116 | 625 | referencing character strings with insufficient lifetime, use | ||
2117 | 626 | \ref SetString(const Ch*, Allocator&) (for copying) or | ||
2118 | 627 | \ref StringRef() (to explicitly mark the pointer as constant) instead. | ||
2119 | 628 | All other pointer types would implicitly convert to \c bool, | ||
2120 | 629 | use \ref SetBool() instead. | ||
2121 | 630 | */ | ||
2122 | 631 | template <typename T> | ||
2123 | 632 | RAPIDJSON_DISABLEIF_RETURN((internal::IsPointer<T>), (GenericValue&)) | ||
2124 | 633 | operator=(T value) { | ||
2125 | 634 | GenericValue v(value); | ||
2126 | 635 | return *this = v; | ||
2127 | 636 | } | ||
2128 | 637 | |||
2129 | 638 | //! Deep-copy assignment from Value | ||
2130 | 639 | /*! Assigns a \b copy of the Value to the current Value object | ||
2131 | 640 | \tparam SourceAllocator Allocator type of \c rhs | ||
2132 | 641 | \param rhs Value to copy from (read-only) | ||
2133 | 642 | \param allocator Allocator to use for copying | ||
2134 | 643 | */ | ||
2135 | 644 | template <typename SourceAllocator> | ||
2136 | 645 | GenericValue& CopyFrom(const GenericValue<Encoding, SourceAllocator>& rhs, Allocator& allocator) { | ||
2137 | 646 | RAPIDJSON_ASSERT((void*)this != (void const*)&rhs); | ||
2138 | 647 | this->~GenericValue(); | ||
2139 | 648 | new (this) GenericValue(rhs, allocator); | ||
2140 | 649 | return *this; | ||
2141 | 650 | } | ||
2142 | 651 | |||
2143 | 652 | //! Exchange the contents of this value with those of other. | ||
2144 | 653 | /*! | ||
2145 | 654 | \param other Another value. | ||
2146 | 655 | \note Constant complexity. | ||
2147 | 656 | */ | ||
2148 | 657 | GenericValue& Swap(GenericValue& other) RAPIDJSON_NOEXCEPT { | ||
2149 | 658 | GenericValue temp; | ||
2150 | 659 | temp.RawAssign(*this); | ||
2151 | 660 | RawAssign(other); | ||
2152 | 661 | other.RawAssign(temp); | ||
2153 | 662 | return *this; | ||
2154 | 663 | } | ||
2155 | 664 | |||
2156 | 665 | //! Prepare Value for move semantics | ||
2157 | 666 | /*! \return *this */ | ||
2158 | 667 | GenericValue& Move() RAPIDJSON_NOEXCEPT { return *this; } | ||
2159 | 668 | //@} | ||
2160 | 669 | |||
2161 | 670 | //!@name Equal-to and not-equal-to operators | ||
2162 | 671 | //@{ | ||
2163 | 672 | //! Equal-to operator | ||
2164 | 673 | /*! | ||
2165 | 674 | \note If an object contains duplicated named member, comparing equality with any object is always \c false. | ||
2166 | 675 | \note Linear time complexity (number of all values in the subtree and total lengths of all strings). | ||
2167 | 676 | */ | ||
2168 | 677 | template <typename SourceAllocator> | ||
2169 | 678 | bool operator==(const GenericValue<Encoding, SourceAllocator>& rhs) const { | ||
2170 | 679 | typedef GenericValue<Encoding, SourceAllocator> RhsType; | ||
2171 | 680 | if (GetType() != rhs.GetType()) | ||
2172 | 681 | return false; | ||
2173 | 682 | |||
2174 | 683 | switch (GetType()) { | ||
2175 | 684 | case kObjectType: // Warning: O(n^2) inner-loop | ||
2176 | 685 | if (data_.o.size != rhs.data_.o.size) | ||
2177 | 686 | return false; | ||
2178 | 687 | for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) { | ||
2179 | 688 | typename RhsType::ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name); | ||
2180 | 689 | if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value) | ||
2181 | 690 | return false; | ||
2182 | 691 | } | ||
2183 | 692 | return true; | ||
2184 | 693 | |||
2185 | 694 | case kArrayType: | ||
2186 | 695 | if (data_.a.size != rhs.data_.a.size) | ||
2187 | 696 | return false; | ||
2188 | 697 | for (SizeType i = 0; i < data_.a.size; i++) | ||
2189 | 698 | if ((*this)[i] != rhs[i]) | ||
2190 | 699 | return false; | ||
2191 | 700 | return true; | ||
2192 | 701 | |||
2193 | 702 | case kStringType: | ||
2194 | 703 | return StringEqual(rhs); | ||
2195 | 704 | |||
2196 | 705 | case kNumberType: | ||
2197 | 706 | if (IsDouble() || rhs.IsDouble()) | ||
2198 | 707 | return GetDouble() == rhs.GetDouble(); // May convert one operand from integer to double. | ||
2199 | 708 | else | ||
2200 | 709 | return data_.n.u64 == rhs.data_.n.u64; | ||
2201 | 710 | |||
2202 | 711 | default: // kTrueType, kFalseType, kNullType | ||
2203 | 712 | return true; | ||
2204 | 713 | } | ||
2205 | 714 | } | ||
2206 | 715 | |||
2207 | 716 | //! Equal-to operator with const C-string pointer | ||
2208 | 717 | bool operator==(const Ch* rhs) const { return *this == GenericValue(StringRef(rhs)); } | ||
2209 | 718 | |||
2210 | 719 | #if RAPIDJSON_HAS_STDSTRING | ||
2211 | 720 | //! Equal-to operator with string object | ||
2212 | 721 | /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. | ||
2213 | 722 | */ | ||
2214 | 723 | bool operator==(const std::basic_string<Ch>& rhs) const { return *this == GenericValue(StringRef(rhs)); } | ||
2215 | 724 | #endif | ||
2216 | 725 | |||
2217 | 726 | //! Equal-to operator with primitive types | ||
2218 | 727 | /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c true, \c false | ||
2219 | 728 | */ | ||
2220 | 729 | template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>,internal::IsGenericValue<T> >), (bool)) operator==(const T& rhs) const { return *this == GenericValue(rhs); } | ||
2221 | 730 | |||
2222 | 731 | //! Not-equal-to operator | ||
2223 | 732 | /*! \return !(*this == rhs) | ||
2224 | 733 | */ | ||
2225 | 734 | template <typename SourceAllocator> | ||
2226 | 735 | bool operator!=(const GenericValue<Encoding, SourceAllocator>& rhs) const { return !(*this == rhs); } | ||
2227 | 736 | |||
2228 | 737 | //! Not-equal-to operator with const C-string pointer | ||
2229 | 738 | bool operator!=(const Ch* rhs) const { return !(*this == rhs); } | ||
2230 | 739 | |||
2231 | 740 | //! Not-equal-to operator with arbitrary types | ||
2232 | 741 | /*! \return !(*this == rhs) | ||
2233 | 742 | */ | ||
2234 | 743 | template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue<T>), (bool)) operator!=(const T& rhs) const { return !(*this == rhs); } | ||
2235 | 744 | |||
2236 | 745 | //! Equal-to operator with arbitrary types (symmetric version) | ||
2237 | 746 | /*! \return (rhs == lhs) | ||
2238 | 747 | */ | ||
2239 | 748 | template <typename T> friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue<T>), (bool)) operator==(const T& lhs, const GenericValue& rhs) { return rhs == lhs; } | ||
2240 | 749 | |||
2241 | 750 | //! Not-Equal-to operator with arbitrary types (symmetric version) | ||
2242 | 751 | /*! \return !(rhs == lhs) | ||
2243 | 752 | */ | ||
2244 | 753 | template <typename T> friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue<T>), (bool)) operator!=(const T& lhs, const GenericValue& rhs) { return !(rhs == lhs); } | ||
2245 | 754 | //@} | ||
2246 | 755 | |||
2247 | 756 | //!@name Type | ||
2248 | 757 | //@{ | ||
2249 | 758 | |||
2250 | 759 | Type GetType() const { return static_cast<Type>(flags_ & kTypeMask); } | ||
2251 | 760 | bool IsNull() const { return flags_ == kNullFlag; } | ||
2252 | 761 | bool IsFalse() const { return flags_ == kFalseFlag; } | ||
2253 | 762 | bool IsTrue() const { return flags_ == kTrueFlag; } | ||
2254 | 763 | bool IsBool() const { return (flags_ & kBoolFlag) != 0; } | ||
2255 | 764 | bool IsObject() const { return flags_ == kObjectFlag; } | ||
2256 | 765 | bool IsArray() const { return flags_ == kArrayFlag; } | ||
2257 | 766 | bool IsNumber() const { return (flags_ & kNumberFlag) != 0; } | ||
2258 | 767 | bool IsInt() const { return (flags_ & kIntFlag) != 0; } | ||
2259 | 768 | bool IsUint() const { return (flags_ & kUintFlag) != 0; } | ||
2260 | 769 | bool IsInt64() const { return (flags_ & kInt64Flag) != 0; } | ||
2261 | 770 | bool IsUint64() const { return (flags_ & kUint64Flag) != 0; } | ||
2262 | 771 | bool IsDouble() const { return (flags_ & kDoubleFlag) != 0; } | ||
2263 | 772 | bool IsString() const { return (flags_ & kStringFlag) != 0; } | ||
2264 | 773 | |||
2265 | 774 | //@} | ||
2266 | 775 | |||
2267 | 776 | //!@name Null | ||
2268 | 777 | //@{ | ||
2269 | 778 | |||
2270 | 779 | GenericValue& SetNull() { this->~GenericValue(); new (this) GenericValue(); return *this; } | ||
2271 | 780 | |||
2272 | 781 | //@} | ||
2273 | 782 | |||
2274 | 783 | //!@name Bool | ||
2275 | 784 | //@{ | ||
2276 | 785 | |||
2277 | 786 | bool GetBool() const { RAPIDJSON_ASSERT(IsBool()); return flags_ == kTrueFlag; } | ||
2278 | 787 | //!< Set boolean value | ||
2279 | 788 | /*! \post IsBool() == true */ | ||
2280 | 789 | GenericValue& SetBool(bool b) { this->~GenericValue(); new (this) GenericValue(b); return *this; } | ||
2281 | 790 | |||
2282 | 791 | //@} | ||
2283 | 792 | |||
2284 | 793 | //!@name Object | ||
2285 | 794 | //@{ | ||
2286 | 795 | |||
2287 | 796 | //! Set this value as an empty object. | ||
2288 | 797 | /*! \post IsObject() == true */ | ||
2289 | 798 | GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; } | ||
2290 | 799 | |||
2291 | 800 | //! Get the number of members in the object. | ||
2292 | 801 | SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; } | ||
2293 | 802 | |||
2294 | 803 | //! Check whether the object is empty. | ||
2295 | 804 | bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; } | ||
2296 | 805 | |||
2297 | 806 | //! Get a value from an object associated with the name. | ||
2298 | 807 | /*! \pre IsObject() == true | ||
2299 | 808 | \tparam T Either \c Ch or \c const \c Ch (template used for disambiguation with \ref operator[](SizeType)) | ||
2300 | 809 | \note In version 0.1x, if the member is not found, this function returns a null value. This makes issue 7. | ||
2301 | 810 | Since 0.2, if the name is not correct, it will assert. | ||
2302 | 811 | If user is unsure whether a member exists, user should use HasMember() first. | ||
2303 | 812 | A better approach is to use FindMember(). | ||
2304 | 813 | \note Linear time complexity. | ||
2305 | 814 | */ | ||
2306 | 815 | template <typename T> | ||
2307 | 816 | RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr<internal::IsSame<typename internal::RemoveConst<T>::Type, Ch> >),(GenericValue&)) operator[](T* name) { | ||
2308 | 817 | GenericValue n(StringRef(name)); | ||
2309 | 818 | return (*this)[n]; | ||
2310 | 819 | } | ||
2311 | 820 | template <typename T> | ||
2312 | 821 | RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr<internal::IsSame<typename internal::RemoveConst<T>::Type, Ch> >),(const GenericValue&)) operator[](T* name) const { return const_cast<GenericValue&>(*this)[name]; } | ||
2313 | 822 | |||
2314 | 823 | //! Get a value from an object associated with the name. | ||
2315 | 824 | /*! \pre IsObject() == true | ||
2316 | 825 | \tparam SourceAllocator Allocator of the \c name value | ||
2317 | 826 | |||
2318 | 827 | \note Compared to \ref operator[](T*), this version is faster because it does not need a StrLen(). | ||
2319 | 828 | And it can also handle strings with embedded null characters. | ||
2320 | 829 | |||
2321 | 830 | \note Linear time complexity. | ||
2322 | 831 | */ | ||
2323 | 832 | template <typename SourceAllocator> | ||
2324 | 833 | GenericValue& operator[](const GenericValue<Encoding, SourceAllocator>& name) { | ||
2325 | 834 | MemberIterator member = FindMember(name); | ||
2326 | 835 | if (member != MemberEnd()) | ||
2327 | 836 | return member->value; | ||
2328 | 837 | else { | ||
2329 | 838 | RAPIDJSON_ASSERT(false); // see above note | ||
2330 | 839 | static GenericValue NullValue; | ||
2331 | 840 | return NullValue; | ||
2332 | 841 | } | ||
2333 | 842 | } | ||
2334 | 843 | template <typename SourceAllocator> | ||
2335 | 844 | const GenericValue& operator[](const GenericValue<Encoding, SourceAllocator>& name) const { return const_cast<GenericValue&>(*this)[name]; } | ||
2336 | 845 | |||
2337 | 846 | //! Const member iterator | ||
2338 | 847 | /*! \pre IsObject() == true */ | ||
2339 | 848 | ConstMemberIterator MemberBegin() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(data_.o.members); } | ||
2340 | 849 | //! Const \em past-the-end member iterator | ||
2341 | 850 | /*! \pre IsObject() == true */ | ||
2342 | 851 | ConstMemberIterator MemberEnd() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(data_.o.members + data_.o.size); } | ||
2343 | 852 | //! Member iterator | ||
2344 | 853 | /*! \pre IsObject() == true */ | ||
2345 | 854 | MemberIterator MemberBegin() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(data_.o.members); } | ||
2346 | 855 | //! \em Past-the-end member iterator | ||
2347 | 856 | /*! \pre IsObject() == true */ | ||
2348 | 857 | MemberIterator MemberEnd() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(data_.o.members + data_.o.size); } | ||
2349 | 858 | |||
2350 | 859 | //! Check whether a member exists in the object. | ||
2351 | 860 | /*! | ||
2352 | 861 | \param name Member name to be searched. | ||
2353 | 862 | \pre IsObject() == true | ||
2354 | 863 | \return Whether a member with that name exists. | ||
2355 | 864 | \note It is better to use FindMember() directly if you need the obtain the value as well. | ||
2356 | 865 | \note Linear time complexity. | ||
2357 | 866 | */ | ||
2358 | 867 | bool HasMember(const Ch* name) const { return FindMember(name) != MemberEnd(); } | ||
2359 | 868 | |||
2360 | 869 | //! Check whether a member exists in the object with GenericValue name. | ||
2361 | 870 | /*! | ||
2362 | 871 | This version is faster because it does not need a StrLen(). It can also handle string with null character. | ||
2363 | 872 | \param name Member name to be searched. | ||
2364 | 873 | \pre IsObject() == true | ||
2365 | 874 | \return Whether a member with that name exists. | ||
2366 | 875 | \note It is better to use FindMember() directly if you need the obtain the value as well. | ||
2367 | 876 | \note Linear time complexity. | ||
2368 | 877 | */ | ||
2369 | 878 | template <typename SourceAllocator> | ||
2370 | 879 | bool HasMember(const GenericValue<Encoding, SourceAllocator>& name) const { return FindMember(name) != MemberEnd(); } | ||
2371 | 880 | |||
2372 | 881 | //! Find member by name. | ||
2373 | 882 | /*! | ||
2374 | 883 | \param name Member name to be searched. | ||
2375 | 884 | \pre IsObject() == true | ||
2376 | 885 | \return Iterator to member, if it exists. | ||
2377 | 886 | Otherwise returns \ref MemberEnd(). | ||
2378 | 887 | |||
2379 | 888 | \note Earlier versions of Rapidjson returned a \c NULL pointer, in case | ||
2380 | 889 | the requested member doesn't exist. For consistency with e.g. | ||
2381 | 890 | \c std::map, this has been changed to MemberEnd() now. | ||
2382 | 891 | \note Linear time complexity. | ||
2383 | 892 | */ | ||
2384 | 893 | MemberIterator FindMember(const Ch* name) { | ||
2385 | 894 | GenericValue n(StringRef(name)); | ||
2386 | 895 | return FindMember(n); | ||
2387 | 896 | } | ||
2388 | 897 | |||
2389 | 898 | ConstMemberIterator FindMember(const Ch* name) const { return const_cast<GenericValue&>(*this).FindMember(name); } | ||
2390 | 899 | |||
2391 | 900 | //! Find member by name. | ||
2392 | 901 | /*! | ||
2393 | 902 | This version is faster because it does not need a StrLen(). It can also handle string with null character. | ||
2394 | 903 | \param name Member name to be searched. | ||
2395 | 904 | \pre IsObject() == true | ||
2396 | 905 | \return Iterator to member, if it exists. | ||
2397 | 906 | Otherwise returns \ref MemberEnd(). | ||
2398 | 907 | |||
2399 | 908 | \note Earlier versions of Rapidjson returned a \c NULL pointer, in case | ||
2400 | 909 | the requested member doesn't exist. For consistency with e.g. | ||
2401 | 910 | \c std::map, this has been changed to MemberEnd() now. | ||
2402 | 911 | \note Linear time complexity. | ||
2403 | 912 | */ | ||
2404 | 913 | template <typename SourceAllocator> | ||
2405 | 914 | MemberIterator FindMember(const GenericValue<Encoding, SourceAllocator>& name) { | ||
2406 | 915 | RAPIDJSON_ASSERT(IsObject()); | ||
2407 | 916 | RAPIDJSON_ASSERT(name.IsString()); | ||
2408 | 917 | MemberIterator member = MemberBegin(); | ||
2409 | 918 | for ( ; member != MemberEnd(); ++member) | ||
2410 | 919 | if (name.StringEqual(member->name)) | ||
2411 | 920 | break; | ||
2412 | 921 | return member; | ||
2413 | 922 | } | ||
2414 | 923 | template <typename SourceAllocator> ConstMemberIterator FindMember(const GenericValue<Encoding, SourceAllocator>& name) const { return const_cast<GenericValue&>(*this).FindMember(name); } | ||
2415 | 924 | |||
2416 | 925 | //! Add a member (name-value pair) to the object. | ||
2417 | 926 | /*! \param name A string value as name of member. | ||
2418 | 927 | \param value Value of any type. | ||
2419 | 928 | \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). | ||
2420 | 929 | \return The value itself for fluent API. | ||
2421 | 930 | \note The ownership of \c name and \c value will be transferred to this object on success. | ||
2422 | 931 | \pre IsObject() && name.IsString() | ||
2423 | 932 | \post name.IsNull() && value.IsNull() | ||
2424 | 933 | \note Amortized Constant time complexity. | ||
2425 | 934 | */ | ||
2426 | 935 | GenericValue& AddMember(GenericValue& name, GenericValue& value, Allocator& allocator) { | ||
2427 | 936 | RAPIDJSON_ASSERT(IsObject()); | ||
2428 | 937 | RAPIDJSON_ASSERT(name.IsString()); | ||
2429 | 938 | |||
2430 | 939 | Object& o = data_.o; | ||
2431 | 940 | if (o.size >= o.capacity) { | ||
2432 | 941 | if (o.capacity == 0) { | ||
2433 | 942 | o.capacity = kDefaultObjectCapacity; | ||
2434 | 943 | o.members = reinterpret_cast<Member*>(allocator.Malloc(o.capacity * sizeof(Member))); | ||
2435 | 944 | } | ||
2436 | 945 | else { | ||
2437 | 946 | SizeType oldCapacity = o.capacity; | ||
2438 | 947 | o.capacity += (oldCapacity + 1) / 2; // grow by factor 1.5 | ||
2439 | 948 | o.members = reinterpret_cast<Member*>(allocator.Realloc(o.members, oldCapacity * sizeof(Member), o.capacity * sizeof(Member))); | ||
2440 | 949 | } | ||
2441 | 950 | } | ||
2442 | 951 | o.members[o.size].name.RawAssign(name); | ||
2443 | 952 | o.members[o.size].value.RawAssign(value); | ||
2444 | 953 | o.size++; | ||
2445 | 954 | return *this; | ||
2446 | 955 | } | ||
2447 | 956 | |||
2448 | 957 | //! Add a constant string value as member (name-value pair) to the object. | ||
2449 | 958 | /*! \param name A string value as name of member. | ||
2450 | 959 | \param value constant string reference as value of member. | ||
2451 | 960 | \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). | ||
2452 | 961 | \return The value itself for fluent API. | ||
2453 | 962 | \pre IsObject() | ||
2454 | 963 | \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below. | ||
2455 | 964 | \note Amortized Constant time complexity. | ||
2456 | 965 | */ | ||
2457 | 966 | GenericValue& AddMember(GenericValue& name, StringRefType value, Allocator& allocator) { | ||
2458 | 967 | GenericValue v(value); | ||
2459 | 968 | return AddMember(name, v, allocator); | ||
2460 | 969 | } | ||
2461 | 970 | |||
2462 | 971 | //! Add any primitive value as member (name-value pair) to the object. | ||
2463 | 972 | /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t | ||
2464 | 973 | \param name A string value as name of member. | ||
2465 | 974 | \param value Value of primitive type \c T as value of member | ||
2466 | 975 | \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator(). | ||
2467 | 976 | \return The value itself for fluent API. | ||
2468 | 977 | \pre IsObject() | ||
2469 | 978 | |||
2470 | 979 | \note The source type \c T explicitly disallows all pointer types, | ||
2471 | 980 | especially (\c const) \ref Ch*. This helps avoiding implicitly | ||
2472 | 981 | referencing character strings with insufficient lifetime, use | ||
2473 | 982 | \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref | ||
2474 | 983 | AddMember(StringRefType, StringRefType, Allocator&). | ||
2475 | 984 | All other pointer types would implicitly convert to \c bool, | ||
2476 | 985 | use an explicit cast instead, if needed. | ||
2477 | 986 | \note Amortized Constant time complexity. | ||
2478 | 987 | */ | ||
2479 | 988 | template <typename T> | ||
2480 | 989 | RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (GenericValue&)) | ||
2481 | 990 | AddMember(GenericValue& name, T value, Allocator& allocator) { | ||
2482 | 991 | GenericValue v(value); | ||
2483 | 992 | return AddMember(name, v, allocator); | ||
2484 | 993 | } | ||
2485 | 994 | |||
2486 | 995 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS | ||
2487 | 996 | GenericValue& AddMember(GenericValue&& name, GenericValue&& value, Allocator& allocator) { | ||
2488 | 997 | return AddMember(name, value, allocator); | ||
2489 | 998 | } | ||
2490 | 999 | GenericValue& AddMember(GenericValue&& name, GenericValue& value, Allocator& allocator) { | ||
2491 | 1000 | return AddMember(name, value, allocator); | ||
2492 | 1001 | } | ||
2493 | 1002 | GenericValue& AddMember(GenericValue& name, GenericValue&& value, Allocator& allocator) { | ||
2494 | 1003 | return AddMember(name, value, allocator); | ||
2495 | 1004 | } | ||
2496 | 1005 | GenericValue& AddMember(StringRefType name, GenericValue&& value, Allocator& allocator) { | ||
2497 | 1006 | GenericValue n(name); | ||
2498 | 1007 | return AddMember(n, value, allocator); | ||
2499 | 1008 | } | ||
2500 | 1009 | #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS | ||
2501 | 1010 | |||
2502 | 1011 | |||
2503 | 1012 | //! Add a member (name-value pair) to the object. | ||
2504 | 1013 | /*! \param name A constant string reference as name of member. | ||
2505 | 1014 | \param value Value of any type. | ||
2506 | 1015 | \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). | ||
2507 | 1016 | \return The value itself for fluent API. | ||
2508 | 1017 | \note The ownership of \c value will be transferred to this object on success. | ||
2509 | 1018 | \pre IsObject() | ||
2510 | 1019 | \post value.IsNull() | ||
2511 | 1020 | \note Amortized Constant time complexity. | ||
2512 | 1021 | */ | ||
2513 | 1022 | GenericValue& AddMember(StringRefType name, GenericValue& value, Allocator& allocator) { | ||
2514 | 1023 | GenericValue n(name); | ||
2515 | 1024 | return AddMember(n, value, allocator); | ||
2516 | 1025 | } | ||
2517 | 1026 | |||
2518 | 1027 | //! Add a constant string value as member (name-value pair) to the object. | ||
2519 | 1028 | /*! \param name A constant string reference as name of member. | ||
2520 | 1029 | \param value constant string reference as value of member. | ||
2521 | 1030 | \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). | ||
2522 | 1031 | \return The value itself for fluent API. | ||
2523 | 1032 | \pre IsObject() | ||
2524 | 1033 | \note This overload is needed to avoid clashes with the generic primitive type AddMember(StringRefType,T,Allocator&) overload below. | ||
2525 | 1034 | \note Amortized Constant time complexity. | ||
2526 | 1035 | */ | ||
2527 | 1036 | GenericValue& AddMember(StringRefType name, StringRefType value, Allocator& allocator) { | ||
2528 | 1037 | GenericValue v(value); | ||
2529 | 1038 | return AddMember(name, v, allocator); | ||
2530 | 1039 | } | ||
2531 | 1040 | |||
2532 | 1041 | //! Add any primitive value as member (name-value pair) to the object. | ||
2533 | 1042 | /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t | ||
2534 | 1043 | \param name A constant string reference as name of member. | ||
2535 | 1044 | \param value Value of primitive type \c T as value of member | ||
2536 | 1045 | \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator(). | ||
2537 | 1046 | \return The value itself for fluent API. | ||
2538 | 1047 | \pre IsObject() | ||
2539 | 1048 | |||
2540 | 1049 | \note The source type \c T explicitly disallows all pointer types, | ||
2541 | 1050 | especially (\c const) \ref Ch*. This helps avoiding implicitly | ||
2542 | 1051 | referencing character strings with insufficient lifetime, use | ||
2543 | 1052 | \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref | ||
2544 | 1053 | AddMember(StringRefType, StringRefType, Allocator&). | ||
2545 | 1054 | All other pointer types would implicitly convert to \c bool, | ||
2546 | 1055 | use an explicit cast instead, if needed. | ||
2547 | 1056 | \note Amortized Constant time complexity. | ||
2548 | 1057 | */ | ||
2549 | 1058 | template <typename T> | ||
2550 | 1059 | RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (GenericValue&)) | ||
2551 | 1060 | AddMember(StringRefType name, T value, Allocator& allocator) { | ||
2552 | 1061 | GenericValue n(name); | ||
2553 | 1062 | return AddMember(n, value, allocator); | ||
2554 | 1063 | } | ||
2555 | 1064 | |||
2556 | 1065 | //! Remove all members in the object. | ||
2557 | 1066 | /*! This function do not deallocate memory in the object, i.e. the capacity is unchanged. | ||
2558 | 1067 | \note Linear time complexity. | ||
2559 | 1068 | */ | ||
2560 | 1069 | void RemoveAllMembers() { | ||
2561 | 1070 | RAPIDJSON_ASSERT(IsObject()); | ||
2562 | 1071 | for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m) | ||
2563 | 1072 | m->~Member(); | ||
2564 | 1073 | data_.o.size = 0; | ||
2565 | 1074 | } | ||
2566 | 1075 | |||
2567 | 1076 | //! Remove a member in object by its name. | ||
2568 | 1077 | /*! \param name Name of member to be removed. | ||
2569 | 1078 | \return Whether the member existed. | ||
2570 | 1079 | \note This function may reorder the object members. Use \ref | ||
2571 | 1080 | EraseMember(ConstMemberIterator) if you need to preserve the | ||
2572 | 1081 | relative order of the remaining members. | ||
2573 | 1082 | \note Linear time complexity. | ||
2574 | 1083 | */ | ||
2575 | 1084 | bool RemoveMember(const Ch* name) { | ||
2576 | 1085 | GenericValue n(StringRef(name)); | ||
2577 | 1086 | return RemoveMember(n); | ||
2578 | 1087 | } | ||
2579 | 1088 | |||
2580 | 1089 | template <typename SourceAllocator> | ||
2581 | 1090 | bool RemoveMember(const GenericValue<Encoding, SourceAllocator>& name) { | ||
2582 | 1091 | MemberIterator m = FindMember(name); | ||
2583 | 1092 | if (m != MemberEnd()) { | ||
2584 | 1093 | RemoveMember(m); | ||
2585 | 1094 | return true; | ||
2586 | 1095 | } | ||
2587 | 1096 | else | ||
2588 | 1097 | return false; | ||
2589 | 1098 | } | ||
2590 | 1099 | |||
2591 | 1100 | //! Remove a member in object by iterator. | ||
2592 | 1101 | /*! \param m member iterator (obtained by FindMember() or MemberBegin()). | ||
2593 | 1102 | \return the new iterator after removal. | ||
2594 | 1103 | \note This function may reorder the object members. Use \ref | ||
2595 | 1104 | EraseMember(ConstMemberIterator) if you need to preserve the | ||
2596 | 1105 | relative order of the remaining members. | ||
2597 | 1106 | \note Constant time complexity. | ||
2598 | 1107 | */ | ||
2599 | 1108 | MemberIterator RemoveMember(MemberIterator m) { | ||
2600 | 1109 | RAPIDJSON_ASSERT(IsObject()); | ||
2601 | 1110 | RAPIDJSON_ASSERT(data_.o.size > 0); | ||
2602 | 1111 | RAPIDJSON_ASSERT(data_.o.members != 0); | ||
2603 | 1112 | RAPIDJSON_ASSERT(m >= MemberBegin() && m < MemberEnd()); | ||
2604 | 1113 | |||
2605 | 1114 | MemberIterator last(data_.o.members + (data_.o.size - 1)); | ||
2606 | 1115 | if (data_.o.size > 1 && m != last) { | ||
2607 | 1116 | // Move the last one to this place | ||
2608 | 1117 | *m = *last; | ||
2609 | 1118 | } | ||
2610 | 1119 | else { | ||
2611 | 1120 | // Only one left, just destroy | ||
2612 | 1121 | m->~Member(); | ||
2613 | 1122 | } | ||
2614 | 1123 | --data_.o.size; | ||
2615 | 1124 | return m; | ||
2616 | 1125 | } | ||
2617 | 1126 | |||
2618 | 1127 | //! Remove a member from an object by iterator. | ||
2619 | 1128 | /*! \param pos iterator to the member to remove | ||
2620 | 1129 | \pre IsObject() == true && \ref MemberBegin() <= \c pos < \ref MemberEnd() | ||
2621 | 1130 | \return Iterator following the removed element. | ||
2622 | 1131 | If the iterator \c pos refers to the last element, the \ref MemberEnd() iterator is returned. | ||
2623 | 1132 | \note This function preserves the relative order of the remaining object | ||
2624 | 1133 | members. If you do not need this, use the more efficient \ref RemoveMember(MemberIterator). | ||
2625 | 1134 | \note Linear time complexity. | ||
2626 | 1135 | */ | ||
2627 | 1136 | MemberIterator EraseMember(ConstMemberIterator pos) { | ||
2628 | 1137 | return EraseMember(pos, pos +1); | ||
2629 | 1138 | } | ||
2630 | 1139 | |||
2631 | 1140 | //! Remove members in the range [first, last) from an object. | ||
2632 | 1141 | /*! \param first iterator to the first member to remove | ||
2633 | 1142 | \param last iterator following the last member to remove | ||
2634 | 1143 | \pre IsObject() == true && \ref MemberBegin() <= \c first <= \c last <= \ref MemberEnd() | ||
2635 | 1144 | \return Iterator following the last removed element. | ||
2636 | 1145 | \note This function preserves the relative order of the remaining object | ||
2637 | 1146 | members. | ||
2638 | 1147 | \note Linear time complexity. | ||
2639 | 1148 | */ | ||
2640 | 1149 | MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last) { | ||
2641 | 1150 | RAPIDJSON_ASSERT(IsObject()); | ||
2642 | 1151 | RAPIDJSON_ASSERT(data_.o.size > 0); | ||
2643 | 1152 | RAPIDJSON_ASSERT(data_.o.members != 0); | ||
2644 | 1153 | RAPIDJSON_ASSERT(first >= MemberBegin()); | ||
2645 | 1154 | RAPIDJSON_ASSERT(first <= last); | ||
2646 | 1155 | RAPIDJSON_ASSERT(last <= MemberEnd()); | ||
2647 | 1156 | |||
2648 | 1157 | MemberIterator pos = MemberBegin() + (first - MemberBegin()); | ||
2649 | 1158 | for (MemberIterator itr = pos; itr != last; ++itr) | ||
2650 | 1159 | itr->~Member(); | ||
2651 | 1160 | std::memmove(&*pos, &*last, (MemberEnd() - last) * sizeof(Member)); | ||
2652 | 1161 | data_.o.size -= (last - first); | ||
2653 | 1162 | return pos; | ||
2654 | 1163 | } | ||
2655 | 1164 | |||
2656 | 1165 | //@} | ||
2657 | 1166 | |||
2658 | 1167 | //!@name Array | ||
2659 | 1168 | //@{ | ||
2660 | 1169 | |||
2661 | 1170 | //! Set this value as an empty array. | ||
2662 | 1171 | /*! \post IsArray == true */ | ||
2663 | 1172 | GenericValue& SetArray() { this->~GenericValue(); new (this) GenericValue(kArrayType); return *this; } | ||
2664 | 1173 | |||
2665 | 1174 | //! Get the number of elements in array. | ||
2666 | 1175 | SizeType Size() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size; } | ||
2667 | 1176 | |||
2668 | 1177 | //! Get the capacity of array. | ||
2669 | 1178 | SizeType Capacity() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.capacity; } | ||
2670 | 1179 | |||
2671 | 1180 | //! Check whether the array is empty. | ||
2672 | 1181 | bool Empty() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size == 0; } | ||
2673 | 1182 | |||
2674 | 1183 | //! Remove all elements in the array. | ||
2675 | 1184 | /*! This function do not deallocate memory in the array, i.e. the capacity is unchanged. | ||
2676 | 1185 | \note Linear time complexity. | ||
2677 | 1186 | */ | ||
2678 | 1187 | void Clear() { | ||
2679 | 1188 | RAPIDJSON_ASSERT(IsArray()); | ||
2680 | 1189 | for (SizeType i = 0; i < data_.a.size; ++i) | ||
2681 | 1190 | data_.a.elements[i].~GenericValue(); | ||
2682 | 1191 | data_.a.size = 0; | ||
2683 | 1192 | } | ||
2684 | 1193 | |||
2685 | 1194 | //! Get an element from array by index. | ||
2686 | 1195 | /*! \pre IsArray() == true | ||
2687 | 1196 | \param index Zero-based index of element. | ||
2688 | 1197 | \see operator[](T*) | ||
2689 | 1198 | */ | ||
2690 | 1199 | GenericValue& operator[](SizeType index) { | ||
2691 | 1200 | RAPIDJSON_ASSERT(IsArray()); | ||
2692 | 1201 | RAPIDJSON_ASSERT(index < data_.a.size); | ||
2693 | 1202 | return data_.a.elements[index]; | ||
2694 | 1203 | } | ||
2695 | 1204 | const GenericValue& operator[](SizeType index) const { return const_cast<GenericValue&>(*this)[index]; } | ||
2696 | 1205 | |||
2697 | 1206 | //! Element iterator | ||
2698 | 1207 | /*! \pre IsArray() == true */ | ||
2699 | 1208 | ValueIterator Begin() { RAPIDJSON_ASSERT(IsArray()); return data_.a.elements; } | ||
2700 | 1209 | //! \em Past-the-end element iterator | ||
2701 | 1210 | /*! \pre IsArray() == true */ | ||
2702 | 1211 | ValueIterator End() { RAPIDJSON_ASSERT(IsArray()); return data_.a.elements + data_.a.size; } | ||
2703 | 1212 | //! Constant element iterator | ||
2704 | 1213 | /*! \pre IsArray() == true */ | ||
2705 | 1214 | ConstValueIterator Begin() const { return const_cast<GenericValue&>(*this).Begin(); } | ||
2706 | 1215 | //! Constant \em past-the-end element iterator | ||
2707 | 1216 | /*! \pre IsArray() == true */ | ||
2708 | 1217 | ConstValueIterator End() const { return const_cast<GenericValue&>(*this).End(); } | ||
2709 | 1218 | |||
2710 | 1219 | //! Request the array to have enough capacity to store elements. | ||
2711 | 1220 | /*! \param newCapacity The capacity that the array at least need to have. | ||
2712 | 1221 | \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). | ||
2713 | 1222 | \return The value itself for fluent API. | ||
2714 | 1223 | \note Linear time complexity. | ||
2715 | 1224 | */ | ||
2716 | 1225 | GenericValue& Reserve(SizeType newCapacity, Allocator &allocator) { | ||
2717 | 1226 | RAPIDJSON_ASSERT(IsArray()); | ||
2718 | 1227 | if (newCapacity > data_.a.capacity) { | ||
2719 | 1228 | data_.a.elements = (GenericValue*)allocator.Realloc(data_.a.elements, data_.a.capacity * sizeof(GenericValue), newCapacity * sizeof(GenericValue)); | ||
2720 | 1229 | data_.a.capacity = newCapacity; | ||
2721 | 1230 | } | ||
2722 | 1231 | return *this; | ||
2723 | 1232 | } | ||
2724 | 1233 | |||
2725 | 1234 | //! Append a GenericValue at the end of the array. | ||
2726 | 1235 | /*! \param value Value to be appended. | ||
2727 | 1236 | \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). | ||
2728 | 1237 | \pre IsArray() == true | ||
2729 | 1238 | \post value.IsNull() == true | ||
2730 | 1239 | \return The value itself for fluent API. | ||
2731 | 1240 | \note The ownership of \c value will be transferred to this array on success. | ||
2732 | 1241 | \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. | ||
2733 | 1242 | \note Amortized constant time complexity. | ||
2734 | 1243 | */ | ||
2735 | 1244 | GenericValue& PushBack(GenericValue& value, Allocator& allocator) { | ||
2736 | 1245 | RAPIDJSON_ASSERT(IsArray()); | ||
2737 | 1246 | if (data_.a.size >= data_.a.capacity) | ||
2738 | 1247 | Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : (data_.a.capacity + (data_.a.capacity + 1) / 2), allocator); | ||
2739 | 1248 | data_.a.elements[data_.a.size++].RawAssign(value); | ||
2740 | 1249 | return *this; | ||
2741 | 1250 | } | ||
2742 | 1251 | |||
2743 | 1252 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS | ||
2744 | 1253 | GenericValue& PushBack(GenericValue&& value, Allocator& allocator) { | ||
2745 | 1254 | return PushBack(value, allocator); | ||
2746 | 1255 | } | ||
2747 | 1256 | #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS | ||
2748 | 1257 | |||
2749 | 1258 | //! Append a constant string reference at the end of the array. | ||
2750 | 1259 | /*! \param value Constant string reference to be appended. | ||
2751 | 1260 | \param allocator Allocator for reallocating memory. It must be the same one used previously. Commonly use GenericDocument::GetAllocator(). | ||
2752 | 1261 | \pre IsArray() == true | ||
2753 | 1262 | \return The value itself for fluent API. | ||
2754 | 1263 | \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. | ||
2755 | 1264 | \note Amortized constant time complexity. | ||
2756 | 1265 | \see GenericStringRef | ||
2757 | 1266 | */ | ||
2758 | 1267 | GenericValue& PushBack(StringRefType value, Allocator& allocator) { | ||
2759 | 1268 | return (*this).template PushBack<StringRefType>(value, allocator); | ||
2760 | 1269 | } | ||
2761 | 1270 | |||
2762 | 1271 | //! Append a primitive value at the end of the array. | ||
2763 | 1272 | /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t | ||
2764 | 1273 | \param value Value of primitive type T to be appended. | ||
2765 | 1274 | \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator(). | ||
2766 | 1275 | \pre IsArray() == true | ||
2767 | 1276 | \return The value itself for fluent API. | ||
2768 | 1277 | \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient. | ||
2769 | 1278 | |||
2770 | 1279 | \note The source type \c T explicitly disallows all pointer types, | ||
2771 | 1280 | especially (\c const) \ref Ch*. This helps avoiding implicitly | ||
2772 | 1281 | referencing character strings with insufficient lifetime, use | ||
2773 | 1282 | \ref PushBack(GenericValue&, Allocator&) or \ref | ||
2774 | 1283 | PushBack(StringRefType, Allocator&). | ||
2775 | 1284 | All other pointer types would implicitly convert to \c bool, | ||
2776 | 1285 | use an explicit cast instead, if needed. | ||
2777 | 1286 | \note Amortized constant time complexity. | ||
2778 | 1287 | */ | ||
2779 | 1288 | template <typename T> | ||
2780 | 1289 | RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (GenericValue&)) | ||
2781 | 1290 | PushBack(T value, Allocator& allocator) { | ||
2782 | 1291 | GenericValue v(value); | ||
2783 | 1292 | return PushBack(v, allocator); | ||
2784 | 1293 | } | ||
2785 | 1294 | |||
2786 | 1295 | //! Remove the last element in the array. | ||
2787 | 1296 | /*! | ||
2788 | 1297 | \note Constant time complexity. | ||
2789 | 1298 | */ | ||
2790 | 1299 | GenericValue& PopBack() { | ||
2791 | 1300 | RAPIDJSON_ASSERT(IsArray()); | ||
2792 | 1301 | RAPIDJSON_ASSERT(!Empty()); | ||
2793 | 1302 | data_.a.elements[--data_.a.size].~GenericValue(); | ||
2794 | 1303 | return *this; | ||
2795 | 1304 | } | ||
2796 | 1305 | |||
2797 | 1306 | //! Remove an element of array by iterator. | ||
2798 | 1307 | /*! | ||
2799 | 1308 | \param pos iterator to the element to remove | ||
2800 | 1309 | \pre IsArray() == true && \ref Begin() <= \c pos < \ref End() | ||
2801 | 1310 | \return Iterator following the removed element. If the iterator pos refers to the last element, the End() iterator is returned. | ||
2802 | 1311 | \note Linear time complexity. | ||
2803 | 1312 | */ | ||
2804 | 1313 | ValueIterator Erase(ConstValueIterator pos) { | ||
2805 | 1314 | return Erase(pos, pos + 1); | ||
2806 | 1315 | } | ||
2807 | 1316 | |||
2808 | 1317 | //! Remove elements in the range [first, last) of the array. | ||
2809 | 1318 | /*! | ||
2810 | 1319 | \param first iterator to the first element to remove | ||
2811 | 1320 | \param last iterator following the last element to remove | ||
2812 | 1321 | \pre IsArray() == true && \ref Begin() <= \c first <= \c last <= \ref End() | ||
2813 | 1322 | \return Iterator following the last removed element. | ||
2814 | 1323 | \note Linear time complexity. | ||
2815 | 1324 | */ | ||
2816 | 1325 | ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) { | ||
2817 | 1326 | RAPIDJSON_ASSERT(IsArray()); | ||
2818 | 1327 | RAPIDJSON_ASSERT(data_.a.size > 0); | ||
2819 | 1328 | RAPIDJSON_ASSERT(data_.a.elements != 0); | ||
2820 | 1329 | RAPIDJSON_ASSERT(first >= Begin()); | ||
2821 | 1330 | RAPIDJSON_ASSERT(first <= last); | ||
2822 | 1331 | RAPIDJSON_ASSERT(last <= End()); | ||
2823 | 1332 | ValueIterator pos = Begin() + (first - Begin()); | ||
2824 | 1333 | for (ValueIterator itr = pos; itr != last; ++itr) | ||
2825 | 1334 | itr->~GenericValue(); | ||
2826 | 1335 | std::memmove(pos, last, (End() - last) * sizeof(GenericValue)); | ||
2827 | 1336 | data_.a.size -= (last - first); | ||
2828 | 1337 | return pos; | ||
2829 | 1338 | } | ||
2830 | 1339 | |||
2831 | 1340 | //@} | ||
2832 | 1341 | |||
2833 | 1342 | //!@name Number | ||
2834 | 1343 | //@{ | ||
2835 | 1344 | |||
2836 | 1345 | int GetInt() const { RAPIDJSON_ASSERT(flags_ & kIntFlag); return data_.n.i.i; } | ||
2837 | 1346 | unsigned GetUint() const { RAPIDJSON_ASSERT(flags_ & kUintFlag); return data_.n.u.u; } | ||
2838 | 1347 | int64_t GetInt64() const { RAPIDJSON_ASSERT(flags_ & kInt64Flag); return data_.n.i64; } | ||
2839 | 1348 | uint64_t GetUint64() const { RAPIDJSON_ASSERT(flags_ & kUint64Flag); return data_.n.u64; } | ||
2840 | 1349 | |||
2841 | 1350 | double GetDouble() const { | ||
2842 | 1351 | RAPIDJSON_ASSERT(IsNumber()); | ||
2843 | 1352 | if ((flags_ & kDoubleFlag) != 0) return data_.n.d; // exact type, no conversion. | ||
2844 | 1353 | if ((flags_ & kIntFlag) != 0) return data_.n.i.i; // int -> double | ||
2845 | 1354 | if ((flags_ & kUintFlag) != 0) return data_.n.u.u; // unsigned -> double | ||
2846 | 1355 | if ((flags_ & kInt64Flag) != 0) return (double)data_.n.i64; // int64_t -> double (may lose precision) | ||
2847 | 1356 | RAPIDJSON_ASSERT((flags_ & kUint64Flag) != 0); return (double)data_.n.u64; // uint64_t -> double (may lose precision) | ||
2848 | 1357 | } | ||
2849 | 1358 | |||
2850 | 1359 | GenericValue& SetInt(int i) { this->~GenericValue(); new (this) GenericValue(i); return *this; } | ||
2851 | 1360 | GenericValue& SetUint(unsigned u) { this->~GenericValue(); new (this) GenericValue(u); return *this; } | ||
2852 | 1361 | GenericValue& SetInt64(int64_t i64) { this->~GenericValue(); new (this) GenericValue(i64); return *this; } | ||
2853 | 1362 | GenericValue& SetUint64(uint64_t u64) { this->~GenericValue(); new (this) GenericValue(u64); return *this; } | ||
2854 | 1363 | GenericValue& SetDouble(double d) { this->~GenericValue(); new (this) GenericValue(d); return *this; } | ||
2855 | 1364 | |||
2856 | 1365 | //@} | ||
2857 | 1366 | |||
2858 | 1367 | //!@name String | ||
2859 | 1368 | //@{ | ||
2860 | 1369 | |||
2861 | 1370 | const Ch* GetString() const { RAPIDJSON_ASSERT(IsString()); return ((flags_ & kInlineStrFlag) ? data_.ss.str : data_.s.str); } | ||
2862 | 1371 | |||
2863 | 1372 | //! Get the length of string. | ||
2864 | 1373 | /*! Since rapidjson permits "\\u0000" in the json string, strlen(v.GetString()) may not equal to v.GetStringLength(). | ||
2865 | 1374 | */ | ||
2866 | 1375 | SizeType GetStringLength() const { RAPIDJSON_ASSERT(IsString()); return ((flags_ & kInlineStrFlag) ? (data_.ss.GetLength()) : data_.s.length); } | ||
2867 | 1376 | |||
2868 | 1377 | //! Set this value as a string without copying source string. | ||
2869 | 1378 | /*! This version has better performance with supplied length, and also support string containing null character. | ||
2870 | 1379 | \param s source string pointer. | ||
2871 | 1380 | \param length The length of source string, excluding the trailing null terminator. | ||
2872 | 1381 | \return The value itself for fluent API. | ||
2873 | 1382 | \post IsString() == true && GetString() == s && GetStringLength() == length | ||
2874 | 1383 | \see SetString(StringRefType) | ||
2875 | 1384 | */ | ||
2876 | 1385 | GenericValue& SetString(const Ch* s, SizeType length) { return SetString(StringRef(s, length)); } | ||
2877 | 1386 | |||
2878 | 1387 | //! Set this value as a string without copying source string. | ||
2879 | 1388 | /*! \param s source string reference | ||
2880 | 1389 | \return The value itself for fluent API. | ||
2881 | 1390 | \post IsString() == true && GetString() == s && GetStringLength() == s.length | ||
2882 | 1391 | */ | ||
2883 | 1392 | GenericValue& SetString(StringRefType s) { this->~GenericValue(); SetStringRaw(s); return *this; } | ||
2884 | 1393 | |||
2885 | 1394 | //! Set this value as a string by copying from source string. | ||
2886 | 1395 | /*! This version has better performance with supplied length, and also support string containing null character. | ||
2887 | 1396 | \param s source string. | ||
2888 | 1397 | \param length The length of source string, excluding the trailing null terminator. | ||
2889 | 1398 | \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). | ||
2890 | 1399 | \return The value itself for fluent API. | ||
2891 | 1400 | \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length | ||
2892 | 1401 | */ | ||
2893 | 1402 | GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { this->~GenericValue(); SetStringRaw(StringRef(s, length), allocator); return *this; } | ||
2894 | 1403 | |||
2895 | 1404 | //! Set this value as a string by copying from source string. | ||
2896 | 1405 | /*! \param s source string. | ||
2897 | 1406 | \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). | ||
2898 | 1407 | \return The value itself for fluent API. | ||
2899 | 1408 | \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length | ||
2900 | 1409 | */ | ||
2901 | 1410 | GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(s, internal::StrLen(s), allocator); } | ||
2902 | 1411 | |||
2903 | 1412 | #if RAPIDJSON_HAS_STDSTRING | ||
2904 | 1413 | //! Set this value as a string by copying from source string. | ||
2905 | 1414 | /*! \param s source string. | ||
2906 | 1415 | \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator(). | ||
2907 | 1416 | \return The value itself for fluent API. | ||
2908 | 1417 | \post IsString() == true && GetString() != s.data() && strcmp(GetString(),s.data() == 0 && GetStringLength() == s.size() | ||
2909 | 1418 | \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING. | ||
2910 | 1419 | */ | ||
2911 | 1420 | GenericValue& SetString(const std::basic_string<Ch>& s, Allocator& allocator) { return SetString(s.data(), s.size(), allocator); } | ||
2912 | 1421 | #endif | ||
2913 | 1422 | |||
2914 | 1423 | //@} | ||
2915 | 1424 | |||
2916 | 1425 | //! Generate events of this value to a Handler. | ||
2917 | 1426 | /*! This function adopts the GoF visitor pattern. | ||
2918 | 1427 | Typical usage is to output this JSON value as JSON text via Writer, which is a Handler. | ||
2919 | 1428 | It can also be used to deep clone this value via GenericDocument, which is also a Handler. | ||
2920 | 1429 | \tparam Handler type of handler. | ||
2921 | 1430 | \param handler An object implementing concept Handler. | ||
2922 | 1431 | */ | ||
2923 | 1432 | template <typename Handler> | ||
2924 | 1433 | bool Accept(Handler& handler) const { | ||
2925 | 1434 | switch(GetType()) { | ||
2926 | 1435 | case kNullType: return handler.Null(); | ||
2927 | 1436 | case kFalseType: return handler.Bool(false); | ||
2928 | 1437 | case kTrueType: return handler.Bool(true); | ||
2929 | 1438 | |||
2930 | 1439 | case kObjectType: | ||
2931 | 1440 | if (!handler.StartObject()) | ||
2932 | 1441 | return false; | ||
2933 | 1442 | for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) { | ||
2934 | 1443 | RAPIDJSON_ASSERT(m->name.IsString()); // User may change the type of name by MemberIterator. | ||
2935 | 1444 | if (!handler.Key(m->name.GetString(), m->name.GetStringLength(), (m->name.flags_ & kCopyFlag) != 0)) | ||
2936 | 1445 | return false; | ||
2937 | 1446 | if (!m->value.Accept(handler)) | ||
2938 | 1447 | return false; | ||
2939 | 1448 | } | ||
2940 | 1449 | return handler.EndObject(data_.o.size); | ||
2941 | 1450 | |||
2942 | 1451 | case kArrayType: | ||
2943 | 1452 | if (!handler.StartArray()) | ||
2944 | 1453 | return false; | ||
2945 | 1454 | for (GenericValue* v = data_.a.elements; v != data_.a.elements + data_.a.size; ++v) | ||
2946 | 1455 | if (!v->Accept(handler)) | ||
2947 | 1456 | return false; | ||
2948 | 1457 | return handler.EndArray(data_.a.size); | ||
2949 | 1458 | |||
2950 | 1459 | case kStringType: | ||
2951 | 1460 | return handler.String(GetString(), GetStringLength(), (flags_ & kCopyFlag) != 0); | ||
2952 | 1461 | |||
2953 | 1462 | case kNumberType: | ||
2954 | 1463 | if (IsInt()) return handler.Int(data_.n.i.i); | ||
2955 | 1464 | else if (IsUint()) return handler.Uint(data_.n.u.u); | ||
2956 | 1465 | else if (IsInt64()) return handler.Int64(data_.n.i64); | ||
2957 | 1466 | else if (IsUint64()) return handler.Uint64(data_.n.u64); | ||
2958 | 1467 | else return handler.Double(data_.n.d); | ||
2959 | 1468 | |||
2960 | 1469 | default: | ||
2961 | 1470 | RAPIDJSON_ASSERT(false); | ||
2962 | 1471 | } | ||
2963 | 1472 | return false; | ||
2964 | 1473 | } | ||
2965 | 1474 | |||
2966 | 1475 | private: | ||
2967 | 1476 | template <typename, typename> friend class GenericValue; | ||
2968 | 1477 | template <typename, typename, typename> friend class GenericDocument; | ||
2969 | 1478 | |||
2970 | 1479 | enum { | ||
2971 | 1480 | kBoolFlag = 0x100, | ||
2972 | 1481 | kNumberFlag = 0x200, | ||
2973 | 1482 | kIntFlag = 0x400, | ||
2974 | 1483 | kUintFlag = 0x800, | ||
2975 | 1484 | kInt64Flag = 0x1000, | ||
2976 | 1485 | kUint64Flag = 0x2000, | ||
2977 | 1486 | kDoubleFlag = 0x4000, | ||
2978 | 1487 | kStringFlag = 0x100000, | ||
2979 | 1488 | kCopyFlag = 0x200000, | ||
2980 | 1489 | kInlineStrFlag = 0x400000, | ||
2981 | 1490 | |||
2982 | 1491 | // Initial flags of different types. | ||
2983 | 1492 | kNullFlag = kNullType, | ||
2984 | 1493 | kTrueFlag = kTrueType | kBoolFlag, | ||
2985 | 1494 | kFalseFlag = kFalseType | kBoolFlag, | ||
2986 | 1495 | kNumberIntFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag, | ||
2987 | 1496 | kNumberUintFlag = kNumberType | kNumberFlag | kUintFlag | kUint64Flag | kInt64Flag, | ||
2988 | 1497 | kNumberInt64Flag = kNumberType | kNumberFlag | kInt64Flag, | ||
2989 | 1498 | kNumberUint64Flag = kNumberType | kNumberFlag | kUint64Flag, | ||
2990 | 1499 | kNumberDoubleFlag = kNumberType | kNumberFlag | kDoubleFlag, | ||
2991 | 1500 | kNumberAnyFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag | kUintFlag | kUint64Flag | kDoubleFlag, | ||
2992 | 1501 | kConstStringFlag = kStringType | kStringFlag, | ||
2993 | 1502 | kCopyStringFlag = kStringType | kStringFlag | kCopyFlag, | ||
2994 | 1503 | kShortStringFlag = kStringType | kStringFlag | kCopyFlag | kInlineStrFlag, | ||
2995 | 1504 | kObjectFlag = kObjectType, | ||
2996 | 1505 | kArrayFlag = kArrayType, | ||
2997 | 1506 | |||
2998 | 1507 | kTypeMask = 0xFF // bitwise-and with mask of 0xFF can be optimized by compiler | ||
2999 | 1508 | }; | ||
3000 | 1509 | |||
3001 | 1510 | static const SizeType kDefaultArrayCapacity = 16; | ||
3002 | 1511 | static const SizeType kDefaultObjectCapacity = 16; | ||
3003 | 1512 | |||
3004 | 1513 | struct String { | ||
3005 | 1514 | const Ch* str; | ||
3006 | 1515 | SizeType length; | ||
3007 | 1516 | unsigned hashcode; //!< reserved | ||
3008 | 1517 | }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode | ||
3009 | 1518 | |||
3010 | 1519 | // implementation detail: ShortString can represent zero-terminated strings up to MaxSize chars | ||
3011 | 1520 | // (excluding the terminating zero) and store a value to determine the length of the contained | ||
3012 | 1521 | // string in the last character str[LenPos] by storing "MaxSize - length" there. If the string | ||
3013 | 1522 | // to store has the maximal length of MaxSize then str[LenPos] will be 0 and therefore act as | ||
3014 | 1523 | // the string terminator as well. For getting the string length back from that value just use | ||
3015 | 1524 | // "MaxSize - str[LenPos]". | ||
3016 | 1525 | // This allows to store 11-chars strings in 32-bit mode and 15-chars strings in 64-bit mode | ||
3017 | 1526 | // inline (for `UTF8`-encoded strings). | ||
3018 | 1527 | struct ShortString { | ||
3019 | 1528 | enum { MaxChars = sizeof(String) / sizeof(Ch), MaxSize = MaxChars - 1, LenPos = MaxSize }; | ||
3020 | 1529 | Ch str[MaxChars]; | ||
3021 | 1530 | |||
3022 | 1531 | inline static bool Usable(SizeType len) { return (MaxSize >= len); } | ||
3023 | 1532 | inline void SetLength(SizeType len) { str[LenPos] = (Ch)(MaxSize - len); } | ||
3024 | 1533 | inline SizeType GetLength() const { return (SizeType)(MaxSize - str[LenPos]); } | ||
3025 | 1534 | }; // at most as many bytes as "String" above => 12 bytes in 32-bit mode, 16 bytes in 64-bit mode | ||
3026 | 1535 | |||
3027 | 1536 | // By using proper binary layout, retrieval of different integer types do not need conversions. | ||
3028 | 1537 | union Number { | ||
3029 | 1538 | #if RAPIDJSON_ENDIAN == RAPIDJSON_LITTLEENDIAN | ||
3030 | 1539 | struct I { | ||
3031 | 1540 | int i; | ||
3032 | 1541 | char padding[4]; | ||
3033 | 1542 | }i; | ||
3034 | 1543 | struct U { | ||
3035 | 1544 | unsigned u; | ||
3036 | 1545 | char padding2[4]; | ||
3037 | 1546 | }u; | ||
3038 | 1547 | #else | ||
3039 | 1548 | struct I { | ||
3040 | 1549 | char padding[4]; | ||
3041 | 1550 | int i; | ||
3042 | 1551 | }i; | ||
3043 | 1552 | struct U { | ||
3044 | 1553 | char padding2[4]; | ||
3045 | 1554 | unsigned u; | ||
3046 | 1555 | }u; | ||
3047 | 1556 | #endif | ||
3048 | 1557 | int64_t i64; | ||
3049 | 1558 | uint64_t u64; | ||
3050 | 1559 | double d; | ||
3051 | 1560 | }; // 8 bytes | ||
3052 | 1561 | |||
3053 | 1562 | struct Object { | ||
3054 | 1563 | Member* members; | ||
3055 | 1564 | SizeType size; | ||
3056 | 1565 | SizeType capacity; | ||
3057 | 1566 | }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode | ||
3058 | 1567 | |||
3059 | 1568 | struct Array { | ||
3060 | 1569 | GenericValue* elements; | ||
3061 | 1570 | SizeType size; | ||
3062 | 1571 | SizeType capacity; | ||
3063 | 1572 | }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode | ||
3064 | 1573 | |||
3065 | 1574 | union Data { | ||
3066 | 1575 | String s; | ||
3067 | 1576 | ShortString ss; | ||
3068 | 1577 | Number n; | ||
3069 | 1578 | Object o; | ||
3070 | 1579 | Array a; | ||
3071 | 1580 | }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode | ||
3072 | 1581 | |||
3073 | 1582 | // Initialize this value as array with initial data, without calling destructor. | ||
3074 | 1583 | void SetArrayRaw(GenericValue* values, SizeType count, Allocator& allocator) { | ||
3075 | 1584 | flags_ = kArrayFlag; | ||
3076 | 1585 | data_.a.elements = (GenericValue*)allocator.Malloc(count * sizeof(GenericValue)); | ||
3077 | 1586 | std::memcpy(data_.a.elements, values, count * sizeof(GenericValue)); | ||
3078 | 1587 | data_.a.size = data_.a.capacity = count; | ||
3079 | 1588 | } | ||
3080 | 1589 | |||
3081 | 1590 | //! Initialize this value as object with initial data, without calling destructor. | ||
3082 | 1591 | void SetObjectRaw(Member* members, SizeType count, Allocator& allocator) { | ||
3083 | 1592 | flags_ = kObjectFlag; | ||
3084 | 1593 | data_.o.members = (Member*)allocator.Malloc(count * sizeof(Member)); | ||
3085 | 1594 | std::memcpy(data_.o.members, members, count * sizeof(Member)); | ||
3086 | 1595 | data_.o.size = data_.o.capacity = count; | ||
3087 | 1596 | } | ||
3088 | 1597 | |||
3089 | 1598 | //! Initialize this value as constant string, without calling destructor. | ||
3090 | 1599 | void SetStringRaw(StringRefType s) RAPIDJSON_NOEXCEPT { | ||
3091 | 1600 | flags_ = kConstStringFlag; | ||
3092 | 1601 | data_.s.str = s; | ||
3093 | 1602 | data_.s.length = s.length; | ||
3094 | 1603 | } | ||
3095 | 1604 | |||
3096 | 1605 | //! Initialize this value as copy string with initial data, without calling destructor. | ||
3097 | 1606 | void SetStringRaw(StringRefType s, Allocator& allocator) { | ||
3098 | 1607 | Ch* str = NULL; | ||
3099 | 1608 | if(ShortString::Usable(s.length)) { | ||
3100 | 1609 | flags_ = kShortStringFlag; | ||
3101 | 1610 | data_.ss.SetLength(s.length); | ||
3102 | 1611 | str = data_.ss.str; | ||
3103 | 1612 | } else { | ||
3104 | 1613 | flags_ = kCopyStringFlag; | ||
3105 | 1614 | data_.s.length = s.length; | ||
3106 | 1615 | str = (Ch *)allocator.Malloc((s.length + 1) * sizeof(Ch)); | ||
3107 | 1616 | data_.s.str = str; | ||
3108 | 1617 | } | ||
3109 | 1618 | std::memcpy(str, s, s.length * sizeof(Ch)); | ||
3110 | 1619 | str[s.length] = '\0'; | ||
3111 | 1620 | } | ||
3112 | 1621 | |||
3113 | 1622 | //! Assignment without calling destructor | ||
3114 | 1623 | void RawAssign(GenericValue& rhs) RAPIDJSON_NOEXCEPT { | ||
3115 | 1624 | data_ = rhs.data_; | ||
3116 | 1625 | flags_ = rhs.flags_; | ||
3117 | 1626 | rhs.flags_ = kNullFlag; | ||
3118 | 1627 | } | ||
3119 | 1628 | |||
3120 | 1629 | template <typename SourceAllocator> | ||
3121 | 1630 | bool StringEqual(const GenericValue<Encoding, SourceAllocator>& rhs) const { | ||
3122 | 1631 | RAPIDJSON_ASSERT(IsString()); | ||
3123 | 1632 | RAPIDJSON_ASSERT(rhs.IsString()); | ||
3124 | 1633 | |||
3125 | 1634 | const SizeType len1 = GetStringLength(); | ||
3126 | 1635 | const SizeType len2 = rhs.GetStringLength(); | ||
3127 | 1636 | if(len1 != len2) { return false; } | ||
3128 | 1637 | |||
3129 | 1638 | const Ch* const str1 = GetString(); | ||
3130 | 1639 | const Ch* const str2 = rhs.GetString(); | ||
3131 | 1640 | if(str1 == str2) { return true; } // fast path for constant string | ||
3132 | 1641 | |||
3133 | 1642 | return (std::memcmp(str1, str2, sizeof(Ch) * len1) == 0); | ||
3134 | 1643 | } | ||
3135 | 1644 | |||
3136 | 1645 | Data data_; | ||
3137 | 1646 | unsigned flags_; | ||
3138 | 1647 | }; | ||
3139 | 1648 | |||
3140 | 1649 | //! GenericValue with UTF8 encoding | ||
3141 | 1650 | typedef GenericValue<UTF8<> > Value; | ||
3142 | 1651 | |||
3143 | 1652 | /////////////////////////////////////////////////////////////////////////////// | ||
3144 | 1653 | // GenericDocument | ||
3145 | 1654 | |||
3146 | 1655 | //! A document for parsing JSON text as DOM. | ||
3147 | 1656 | /*! | ||
3148 | 1657 | \note implements Handler concept | ||
3149 | 1658 | \tparam Encoding Encoding for both parsing and string storage. | ||
3150 | 1659 | \tparam Allocator Allocator for allocating memory for the DOM | ||
3151 | 1660 | \tparam StackAllocator Allocator for allocating memory for stack during parsing. | ||
3152 | 1661 | \warning Although GenericDocument inherits from GenericValue, the API does \b not provide any virtual functions, especially no virtual destructor. To avoid memory leaks, do not \c delete a GenericDocument object via a pointer to a GenericValue. | ||
3153 | 1662 | */ | ||
3154 | 1663 | template <typename Encoding, typename Allocator = MemoryPoolAllocator<>, typename StackAllocator = CrtAllocator> | ||
3155 | 1664 | class GenericDocument : public GenericValue<Encoding, Allocator> { | ||
3156 | 1665 | public: | ||
3157 | 1666 | typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding. | ||
3158 | 1667 | typedef GenericValue<Encoding, Allocator> ValueType; //!< Value type of the document. | ||
3159 | 1668 | typedef Allocator AllocatorType; //!< Allocator type from template parameter. | ||
3160 | 1669 | |||
3161 | 1670 | //! Constructor | ||
3162 | 1671 | /*! \param allocator Optional allocator for allocating memory. | ||
3163 | 1672 | \param stackCapacity Optional initial capacity of stack in bytes. | ||
3164 | 1673 | \param stackAllocator Optional allocator for allocating memory for stack. | ||
3165 | 1674 | */ | ||
3166 | 1675 | GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) : | ||
3167 | 1676 | allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_() | ||
3168 | 1677 | { | ||
3169 | 1678 | if (!allocator_) | ||
3170 | 1679 | ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator()); | ||
3171 | 1680 | } | ||
3172 | 1681 | |||
3173 | 1682 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS | ||
3174 | 1683 | //! Move constructor in C++11 | ||
3175 | 1684 | GenericDocument(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT | ||
3176 | 1685 | : ValueType(std::move(rhs)), | ||
3177 | 1686 | allocator_(rhs.allocator_), | ||
3178 | 1687 | ownAllocator_(rhs.ownAllocator_), | ||
3179 | 1688 | stack_(std::move(rhs.stack_)), | ||
3180 | 1689 | parseResult_(rhs.parseResult_) | ||
3181 | 1690 | { | ||
3182 | 1691 | rhs.allocator_ = 0; | ||
3183 | 1692 | rhs.ownAllocator_ = 0; | ||
3184 | 1693 | rhs.parseResult_ = ParseResult(); | ||
3185 | 1694 | } | ||
3186 | 1695 | #endif | ||
3187 | 1696 | |||
3188 | 1697 | ~GenericDocument() { | ||
3189 | 1698 | Destroy(); | ||
3190 | 1699 | } | ||
3191 | 1700 | |||
3192 | 1701 | #if RAPIDJSON_HAS_CXX11_RVALUE_REFS | ||
3193 | 1702 | //! Move assignment in C++11 | ||
3194 | 1703 | GenericDocument& operator=(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT | ||
3195 | 1704 | { | ||
3196 | 1705 | // The cast to ValueType is necessary here, because otherwise it would | ||
3197 | 1706 | // attempt to call GenericValue's templated assignment operator. | ||
3198 | 1707 | ValueType::operator=(std::forward<ValueType>(rhs)); | ||
3199 | 1708 | |||
3200 | 1709 | // Calling the destructor here would prematurely call stack_'s destructor | ||
3201 | 1710 | Destroy(); | ||
3202 | 1711 | |||
3203 | 1712 | allocator_ = rhs.allocator_; | ||
3204 | 1713 | ownAllocator_ = rhs.ownAllocator_; | ||
3205 | 1714 | stack_ = std::move(rhs.stack_); | ||
3206 | 1715 | parseResult_ = rhs.parseResult_; | ||
3207 | 1716 | |||
3208 | 1717 | rhs.allocator_ = 0; | ||
3209 | 1718 | rhs.ownAllocator_ = 0; | ||
3210 | 1719 | rhs.parseResult_ = ParseResult(); | ||
3211 | 1720 | |||
3212 | 1721 | return *this; | ||
3213 | 1722 | } | ||
3214 | 1723 | #endif | ||
3215 | 1724 | |||
3216 | 1725 | //!@name Parse from stream | ||
3217 | 1726 | //!@{ | ||
3218 | 1727 | |||
3219 | 1728 | //! Parse JSON text from an input stream (with Encoding conversion) | ||
3220 | 1729 | /*! \tparam parseFlags Combination of \ref ParseFlag. | ||
3221 | 1730 | \tparam SourceEncoding Encoding of input stream | ||
3222 | 1731 | \tparam InputStream Type of input stream, implementing Stream concept | ||
3223 | 1732 | \param is Input stream to be parsed. | ||
3224 | 1733 | \return The document itself for fluent API. | ||
3225 | 1734 | */ | ||
3226 | 1735 | template <unsigned parseFlags, typename SourceEncoding, typename InputStream> | ||
3227 | 1736 | GenericDocument& ParseStream(InputStream& is) { | ||
3228 | 1737 | ValueType::SetNull(); // Remove existing root if exist | ||
3229 | 1738 | GenericReader<SourceEncoding, Encoding, Allocator> reader(&GetAllocator()); | ||
3230 | 1739 | ClearStackOnExit scope(*this); | ||
3231 | 1740 | parseResult_ = reader.template Parse<parseFlags>(is, *this); | ||
3232 | 1741 | if (parseResult_) { | ||
3233 | 1742 | RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object | ||
3234 | 1743 | this->RawAssign(*stack_.template Pop<ValueType>(1)); // Add this-> to prevent issue 13. | ||
3235 | 1744 | } | ||
3236 | 1745 | return *this; | ||
3237 | 1746 | } | ||
3238 | 1747 | |||
3239 | 1748 | //! Parse JSON text from an input stream | ||
3240 | 1749 | /*! \tparam parseFlags Combination of \ref ParseFlag. | ||
3241 | 1750 | \tparam InputStream Type of input stream, implementing Stream concept | ||
3242 | 1751 | \param is Input stream to be parsed. | ||
3243 | 1752 | \return The document itself for fluent API. | ||
3244 | 1753 | */ | ||
3245 | 1754 | template <unsigned parseFlags, typename InputStream> | ||
3246 | 1755 | GenericDocument& ParseStream(InputStream& is) { | ||
3247 | 1756 | return ParseStream<parseFlags,Encoding,InputStream>(is); | ||
3248 | 1757 | } | ||
3249 | 1758 | |||
3250 | 1759 | //! Parse JSON text from an input stream (with \ref kParseDefaultFlags) | ||
3251 | 1760 | /*! \tparam InputStream Type of input stream, implementing Stream concept | ||
3252 | 1761 | \param is Input stream to be parsed. | ||
3253 | 1762 | \return The document itself for fluent API. | ||
3254 | 1763 | */ | ||
3255 | 1764 | template <typename InputStream> | ||
3256 | 1765 | GenericDocument& ParseStream(InputStream& is) { | ||
3257 | 1766 | return ParseStream<kParseDefaultFlags, Encoding, InputStream>(is); | ||
3258 | 1767 | } | ||
3259 | 1768 | //!@} | ||
3260 | 1769 | |||
3261 | 1770 | //!@name Parse in-place from mutable string | ||
3262 | 1771 | //!@{ | ||
3263 | 1772 | |||
3264 | 1773 | //! Parse JSON text from a mutable string (with Encoding conversion) | ||
3265 | 1774 | /*! \tparam parseFlags Combination of \ref ParseFlag. | ||
3266 | 1775 | \tparam SourceEncoding Transcoding from input Encoding | ||
3267 | 1776 | \param str Mutable zero-terminated string to be parsed. | ||
3268 | 1777 | \return The document itself for fluent API. | ||
3269 | 1778 | */ | ||
3270 | 1779 | template <unsigned parseFlags, typename SourceEncoding> | ||
3271 | 1780 | GenericDocument& ParseInsitu(Ch* str) { | ||
3272 | 1781 | GenericInsituStringStream<Encoding> s(str); | ||
3273 | 1782 | return ParseStream<parseFlags | kParseInsituFlag, SourceEncoding>(s); | ||
3274 | 1783 | } | ||
3275 | 1784 | |||
3276 | 1785 | //! Parse JSON text from a mutable string | ||
3277 | 1786 | /*! \tparam parseFlags Combination of \ref ParseFlag. | ||
3278 | 1787 | \param str Mutable zero-terminated string to be parsed. | ||
3279 | 1788 | \return The document itself for fluent API. | ||
3280 | 1789 | */ | ||
3281 | 1790 | template <unsigned parseFlags> | ||
3282 | 1791 | GenericDocument& ParseInsitu(Ch* str) { | ||
3283 | 1792 | return ParseInsitu<parseFlags, Encoding>(str); | ||
3284 | 1793 | } | ||
3285 | 1794 | |||
3286 | 1795 | //! Parse JSON text from a mutable string (with \ref kParseDefaultFlags) | ||
3287 | 1796 | /*! \param str Mutable zero-terminated string to be parsed. | ||
3288 | 1797 | \return The document itself for fluent API. | ||
3289 | 1798 | */ | ||
3290 | 1799 | GenericDocument& ParseInsitu(Ch* str) { | ||
3291 | 1800 | return ParseInsitu<kParseDefaultFlags, Encoding>(str); | ||
3292 | 1801 | } | ||
3293 | 1802 | //!@} | ||
3294 | 1803 | |||
3295 | 1804 | //!@name Parse from read-only string | ||
3296 | 1805 | //!@{ | ||
3297 | 1806 | |||
3298 | 1807 | //! Parse JSON text from a read-only string (with Encoding conversion) | ||
3299 | 1808 | /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag). | ||
3300 | 1809 | \tparam SourceEncoding Transcoding from input Encoding | ||
3301 | 1810 | \param str Read-only zero-terminated string to be parsed. | ||
3302 | 1811 | */ | ||
3303 | 1812 | template <unsigned parseFlags, typename SourceEncoding> | ||
3304 | 1813 | GenericDocument& Parse(const Ch* str) { | ||
3305 | 1814 | RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag)); | ||
3306 | 1815 | GenericStringStream<SourceEncoding> s(str); | ||
3307 | 1816 | return ParseStream<parseFlags, SourceEncoding>(s); | ||
3308 | 1817 | } | ||
3309 | 1818 | |||
3310 | 1819 | //! Parse JSON text from a read-only string | ||
3311 | 1820 | /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag). | ||
3312 | 1821 | \param str Read-only zero-terminated string to be parsed. | ||
3313 | 1822 | */ | ||
3314 | 1823 | template <unsigned parseFlags> | ||
3315 | 1824 | GenericDocument& Parse(const Ch* str) { | ||
3316 | 1825 | return Parse<parseFlags, Encoding>(str); | ||
3317 | 1826 | } | ||
3318 | 1827 | |||
3319 | 1828 | //! Parse JSON text from a read-only string (with \ref kParseDefaultFlags) | ||
3320 | 1829 | /*! \param str Read-only zero-terminated string to be parsed. | ||
3321 | 1830 | */ | ||
3322 | 1831 | GenericDocument& Parse(const Ch* str) { | ||
3323 | 1832 | return Parse<kParseDefaultFlags>(str); | ||
3324 | 1833 | } | ||
3325 | 1834 | //!@} | ||
3326 | 1835 | |||
3327 | 1836 | //!@name Handling parse errors | ||
3328 | 1837 | //!@{ | ||
3329 | 1838 | |||
3330 | 1839 | //! Whether a parse error has occured in the last parsing. | ||
3331 | 1840 | bool HasParseError() const { return parseResult_.IsError(); } | ||
3332 | 1841 | |||
3333 | 1842 | //! Get the \ref ParseErrorCode of last parsing. | ||
3334 | 1843 | ParseErrorCode GetParseError() const { return parseResult_.Code(); } | ||
3335 | 1844 | |||
3336 | 1845 | //! Get the position of last parsing error in input, 0 otherwise. | ||
3337 | 1846 | size_t GetErrorOffset() const { return parseResult_.Offset(); } | ||
3338 | 1847 | |||
3339 | 1848 | //!@} | ||
3340 | 1849 | |||
3341 | 1850 | //! Get the allocator of this document. | ||
3342 | 1851 | Allocator& GetAllocator() { return *allocator_; } | ||
3343 | 1852 | |||
3344 | 1853 | //! Get the capacity of stack in bytes. | ||
3345 | 1854 | size_t GetStackCapacity() const { return stack_.GetCapacity(); } | ||
3346 | 1855 | |||
3347 | 1856 | private: | ||
3348 | 1857 | // clear stack on any exit from ParseStream, e.g. due to exception | ||
3349 | 1858 | struct ClearStackOnExit { | ||
3350 | 1859 | explicit ClearStackOnExit(GenericDocument& d) : d_(d) {} | ||
3351 | 1860 | ~ClearStackOnExit() { d_.ClearStack(); } | ||
3352 | 1861 | private: | ||
3353 | 1862 | ClearStackOnExit(const ClearStackOnExit&); | ||
3354 | 1863 | ClearStackOnExit& operator=(const ClearStackOnExit&); | ||
3355 | 1864 | GenericDocument& d_; | ||
3356 | 1865 | }; | ||
3357 | 1866 | |||
3358 | 1867 | // callers of the following private Handler functions | ||
3359 | 1868 | template <typename,typename,typename> friend class GenericReader; // for parsing | ||
3360 | 1869 | template <typename, typename> friend class GenericValue; // for deep copying | ||
3361 | 1870 | |||
3362 | 1871 | // Implementation of Handler | ||
3363 | 1872 | bool Null() { new (stack_.template Push<ValueType>()) ValueType(); return true; } | ||
3364 | 1873 | bool Bool(bool b) { new (stack_.template Push<ValueType>()) ValueType(b); return true; } | ||
3365 | 1874 | bool Int(int i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; } | ||
3366 | 1875 | bool Uint(unsigned i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; } | ||
3367 | 1876 | bool Int64(int64_t i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; } | ||
3368 | 1877 | bool Uint64(uint64_t i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; } | ||
3369 | 1878 | bool Double(double d) { new (stack_.template Push<ValueType>()) ValueType(d); return true; } | ||
3370 | 1879 | |||
3371 | 1880 | bool String(const Ch* str, SizeType length, bool copy) { | ||
3372 | 1881 | if (copy) | ||
3373 | 1882 | new (stack_.template Push<ValueType>()) ValueType(str, length, GetAllocator()); | ||
3374 | 1883 | else | ||
3375 | 1884 | new (stack_.template Push<ValueType>()) ValueType(str, length); | ||
3376 | 1885 | return true; | ||
3377 | 1886 | } | ||
3378 | 1887 | |||
3379 | 1888 | bool StartObject() { new (stack_.template Push<ValueType>()) ValueType(kObjectType); return true; } | ||
3380 | 1889 | |||
3381 | 1890 | bool Key(const Ch* str, SizeType length, bool copy) { return String(str, length, copy); } | ||
3382 | 1891 | |||
3383 | 1892 | bool EndObject(SizeType memberCount) { | ||
3384 | 1893 | typename ValueType::Member* members = stack_.template Pop<typename ValueType::Member>(memberCount); | ||
3385 | 1894 | stack_.template Top<ValueType>()->SetObjectRaw(members, (SizeType)memberCount, GetAllocator()); | ||
3386 | 1895 | return true; | ||
3387 | 1896 | } | ||
3388 | 1897 | |||
3389 | 1898 | bool StartArray() { new (stack_.template Push<ValueType>()) ValueType(kArrayType); return true; } | ||
3390 | 1899 | |||
3391 | 1900 | bool EndArray(SizeType elementCount) { | ||
3392 | 1901 | ValueType* elements = stack_.template Pop<ValueType>(elementCount); | ||
3393 | 1902 | stack_.template Top<ValueType>()->SetArrayRaw(elements, elementCount, GetAllocator()); | ||
3394 | 1903 | return true; | ||
3395 | 1904 | } | ||
3396 | 1905 | |||
3397 | 1906 | private: | ||
3398 | 1907 | //! Prohibit copying | ||
3399 | 1908 | GenericDocument(const GenericDocument&); | ||
3400 | 1909 | //! Prohibit assignment | ||
3401 | 1910 | GenericDocument& operator=(const GenericDocument&); | ||
3402 | 1911 | |||
3403 | 1912 | void ClearStack() { | ||
3404 | 1913 | if (Allocator::kNeedFree) | ||
3405 | 1914 | while (stack_.GetSize() > 0) // Here assumes all elements in stack array are GenericValue (Member is actually 2 GenericValue objects) | ||
3406 | 1915 | (stack_.template Pop<ValueType>(1))->~ValueType(); | ||
3407 | 1916 | else | ||
3408 | 1917 | stack_.Clear(); | ||
3409 | 1918 | stack_.ShrinkToFit(); | ||
3410 | 1919 | } | ||
3411 | 1920 | |||
3412 | 1921 | void Destroy() { | ||
3413 | 1922 | RAPIDJSON_DELETE(ownAllocator_); | ||
3414 | 1923 | } | ||
3415 | 1924 | |||
3416 | 1925 | static const size_t kDefaultStackCapacity = 1024; | ||
3417 | 1926 | Allocator* allocator_; | ||
3418 | 1927 | Allocator* ownAllocator_; | ||
3419 | 1928 | internal::Stack<StackAllocator> stack_; | ||
3420 | 1929 | ParseResult parseResult_; | ||
3421 | 1930 | }; | ||
3422 | 1931 | |||
3423 | 1932 | //! GenericDocument with UTF8 encoding | ||
3424 | 1933 | typedef GenericDocument<UTF8<> > Document; | ||
3425 | 1934 | |||
3426 | 1935 | // defined here due to the dependency on GenericDocument | ||
3427 | 1936 | template <typename Encoding, typename Allocator> | ||
3428 | 1937 | template <typename SourceAllocator> | ||
3429 | 1938 | inline | ||
3430 | 1939 | GenericValue<Encoding,Allocator>::GenericValue(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator& allocator) | ||
3431 | 1940 | { | ||
3432 | 1941 | switch (rhs.GetType()) { | ||
3433 | 1942 | case kObjectType: | ||
3434 | 1943 | case kArrayType: { // perform deep copy via SAX Handler | ||
3435 | 1944 | GenericDocument<Encoding,Allocator> d(&allocator); | ||
3436 | 1945 | rhs.Accept(d); | ||
3437 | 1946 | RawAssign(*d.stack_.template Pop<GenericValue>(1)); | ||
3438 | 1947 | } | ||
3439 | 1948 | break; | ||
3440 | 1949 | case kStringType: | ||
3441 | 1950 | if (rhs.flags_ == kConstStringFlag) { | ||
3442 | 1951 | flags_ = rhs.flags_; | ||
3443 | 1952 | data_ = *reinterpret_cast<const Data*>(&rhs.data_); | ||
3444 | 1953 | } else { | ||
3445 | 1954 | SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator); | ||
3446 | 1955 | } | ||
3447 | 1956 | break; | ||
3448 | 1957 | default: // kNumberType, kTrueType, kFalseType, kNullType | ||
3449 | 1958 | flags_ = rhs.flags_; | ||
3450 | 1959 | data_ = *reinterpret_cast<const Data*>(&rhs.data_); | ||
3451 | 1960 | } | ||
3452 | 1961 | } | ||
3453 | 1962 | |||
3454 | 1963 | RAPIDJSON_NAMESPACE_END | ||
3455 | 1964 | |||
3456 | 1965 | #if defined(_MSC_VER) || defined(__GNUC__) | ||
3457 | 1966 | RAPIDJSON_DIAG_POP | ||
3458 | 1967 | #endif | ||
3459 | 1968 | |||
3460 | 1969 | #endif // RAPIDJSON_DOCUMENT_H_ | ||
3461 | 0 | 1970 | ||
3462 | === added file 'shorts/xml2json/rapidjson/encodedstream.h' | |||
3463 | --- shorts/xml2json/rapidjson/encodedstream.h 1970-01-01 00:00:00 +0000 | |||
3464 | +++ shorts/xml2json/rapidjson/encodedstream.h 2015-12-15 16:05:21 +0000 | |||
3465 | @@ -0,0 +1,290 @@ | |||
3466 | 1 | // Copyright (C) 2011 Milo Yip | ||
3467 | 2 | // | ||
3468 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
3469 | 4 | // of this software and associated documentation files (the "Software"), to deal | ||
3470 | 5 | // in the Software without restriction, including without limitation the rights | ||
3471 | 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
3472 | 7 | // copies of the Software, and to permit persons to whom the Software is | ||
3473 | 8 | // furnished to do so, subject to the following conditions: | ||
3474 | 9 | // | ||
3475 | 10 | // The above copyright notice and this permission notice shall be included in | ||
3476 | 11 | // all copies or substantial portions of the Software. | ||
3477 | 12 | // | ||
3478 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
3479 | 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
3480 | 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
3481 | 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
3482 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
3483 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
3484 | 19 | // THE SOFTWARE. | ||
3485 | 20 | |||
3486 | 21 | #ifndef RAPIDJSON_ENCODEDSTREAM_H_ | ||
3487 | 22 | #define RAPIDJSON_ENCODEDSTREAM_H_ | ||
3488 | 23 | |||
3489 | 24 | #include "rapidjson.h" | ||
3490 | 25 | |||
3491 | 26 | #ifdef __GNUC__ | ||
3492 | 27 | RAPIDJSON_DIAG_PUSH | ||
3493 | 28 | RAPIDJSON_DIAG_OFF(effc++) | ||
3494 | 29 | #endif | ||
3495 | 30 | |||
3496 | 31 | RAPIDJSON_NAMESPACE_BEGIN | ||
3497 | 32 | |||
3498 | 33 | //! Input byte stream wrapper with a statically bound encoding. | ||
3499 | 34 | /*! | ||
3500 | 35 | \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. | ||
3501 | 36 | \tparam InputByteStream Type of input byte stream. For example, FileReadStream. | ||
3502 | 37 | */ | ||
3503 | 38 | template <typename Encoding, typename InputByteStream> | ||
3504 | 39 | class EncodedInputStream { | ||
3505 | 40 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
3506 | 41 | public: | ||
3507 | 42 | typedef typename Encoding::Ch Ch; | ||
3508 | 43 | |||
3509 | 44 | EncodedInputStream(InputByteStream& is) : is_(is) { | ||
3510 | 45 | current_ = Encoding::TakeBOM(is_); | ||
3511 | 46 | } | ||
3512 | 47 | |||
3513 | 48 | Ch Peek() const { return current_; } | ||
3514 | 49 | Ch Take() { Ch c = current_; current_ = Encoding::Take(is_); return c; } | ||
3515 | 50 | size_t Tell() const { return is_.Tell(); } | ||
3516 | 51 | |||
3517 | 52 | // Not implemented | ||
3518 | 53 | void Put(Ch) { RAPIDJSON_ASSERT(false); } | ||
3519 | 54 | void Flush() { RAPIDJSON_ASSERT(false); } | ||
3520 | 55 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } | ||
3521 | 56 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } | ||
3522 | 57 | |||
3523 | 58 | private: | ||
3524 | 59 | EncodedInputStream(const EncodedInputStream&); | ||
3525 | 60 | EncodedInputStream& operator=(const EncodedInputStream&); | ||
3526 | 61 | |||
3527 | 62 | InputByteStream& is_; | ||
3528 | 63 | Ch current_; | ||
3529 | 64 | }; | ||
3530 | 65 | |||
3531 | 66 | //! Output byte stream wrapper with statically bound encoding. | ||
3532 | 67 | /*! | ||
3533 | 68 | \tparam Encoding The interpretation of encoding of the stream. Either UTF8, UTF16LE, UTF16BE, UTF32LE, UTF32BE. | ||
3534 | 69 | \tparam InputByteStream Type of input byte stream. For example, FileWriteStream. | ||
3535 | 70 | */ | ||
3536 | 71 | template <typename Encoding, typename OutputByteStream> | ||
3537 | 72 | class EncodedOutputStream { | ||
3538 | 73 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
3539 | 74 | public: | ||
3540 | 75 | typedef typename Encoding::Ch Ch; | ||
3541 | 76 | |||
3542 | 77 | EncodedOutputStream(OutputByteStream& os, bool putBOM = true) : os_(os) { | ||
3543 | 78 | if (putBOM) | ||
3544 | 79 | Encoding::PutBOM(os_); | ||
3545 | 80 | } | ||
3546 | 81 | |||
3547 | 82 | void Put(Ch c) { Encoding::Put(os_, c); } | ||
3548 | 83 | void Flush() { os_.Flush(); } | ||
3549 | 84 | |||
3550 | 85 | // Not implemented | ||
3551 | 86 | Ch Peek() const { RAPIDJSON_ASSERT(false); } | ||
3552 | 87 | Ch Take() { RAPIDJSON_ASSERT(false); } | ||
3553 | 88 | size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } | ||
3554 | 89 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } | ||
3555 | 90 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } | ||
3556 | 91 | |||
3557 | 92 | private: | ||
3558 | 93 | EncodedOutputStream(const EncodedOutputStream&); | ||
3559 | 94 | EncodedOutputStream& operator=(const EncodedOutputStream&); | ||
3560 | 95 | |||
3561 | 96 | OutputByteStream& os_; | ||
3562 | 97 | }; | ||
3563 | 98 | |||
3564 | 99 | #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x | ||
3565 | 100 | |||
3566 | 101 | //! Input stream wrapper with dynamically bound encoding and automatic encoding detection. | ||
3567 | 102 | /*! | ||
3568 | 103 | \tparam CharType Type of character for reading. | ||
3569 | 104 | \tparam InputByteStream type of input byte stream to be wrapped. | ||
3570 | 105 | */ | ||
3571 | 106 | template <typename CharType, typename InputByteStream> | ||
3572 | 107 | class AutoUTFInputStream { | ||
3573 | 108 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
3574 | 109 | public: | ||
3575 | 110 | typedef CharType Ch; | ||
3576 | 111 | |||
3577 | 112 | //! Constructor. | ||
3578 | 113 | /*! | ||
3579 | 114 | \param is input stream to be wrapped. | ||
3580 | 115 | \param type UTF encoding type if it is not detected from the stream. | ||
3581 | 116 | */ | ||
3582 | 117 | AutoUTFInputStream(InputByteStream& is, UTFType type = kUTF8) : is_(&is), type_(type), hasBOM_(false) { | ||
3583 | 118 | DetectType(); | ||
3584 | 119 | static const TakeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Take) }; | ||
3585 | 120 | takeFunc_ = f[type_]; | ||
3586 | 121 | current_ = takeFunc_(*is_); | ||
3587 | 122 | } | ||
3588 | 123 | |||
3589 | 124 | UTFType GetType() const { return type_; } | ||
3590 | 125 | bool HasBOM() const { return hasBOM_; } | ||
3591 | 126 | |||
3592 | 127 | Ch Peek() const { return current_; } | ||
3593 | 128 | Ch Take() { Ch c = current_; current_ = takeFunc_(*is_); return c; } | ||
3594 | 129 | size_t Tell() const { return is_->Tell(); } | ||
3595 | 130 | |||
3596 | 131 | // Not implemented | ||
3597 | 132 | void Put(Ch) { RAPIDJSON_ASSERT(false); } | ||
3598 | 133 | void Flush() { RAPIDJSON_ASSERT(false); } | ||
3599 | 134 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } | ||
3600 | 135 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } | ||
3601 | 136 | |||
3602 | 137 | private: | ||
3603 | 138 | AutoUTFInputStream(const AutoUTFInputStream&); | ||
3604 | 139 | AutoUTFInputStream& operator=(const AutoUTFInputStream&); | ||
3605 | 140 | |||
3606 | 141 | // Detect encoding type with BOM or RFC 4627 | ||
3607 | 142 | void DetectType() { | ||
3608 | 143 | // BOM (Byte Order Mark): | ||
3609 | 144 | // 00 00 FE FF UTF-32BE | ||
3610 | 145 | // FF FE 00 00 UTF-32LE | ||
3611 | 146 | // FE FF UTF-16BE | ||
3612 | 147 | // FF FE UTF-16LE | ||
3613 | 148 | // EF BB BF UTF-8 | ||
3614 | 149 | |||
3615 | 150 | const unsigned char* c = (const unsigned char *)is_->Peek4(); | ||
3616 | 151 | if (!c) | ||
3617 | 152 | return; | ||
3618 | 153 | |||
3619 | 154 | unsigned bom = c[0] | (c[1] << 8) | (c[2] << 16) | (c[3] << 24); | ||
3620 | 155 | hasBOM_ = false; | ||
3621 | 156 | if (bom == 0xFFFE0000) { type_ = kUTF32BE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } | ||
3622 | 157 | else if (bom == 0x0000FEFF) { type_ = kUTF32LE; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); is_->Take(); } | ||
3623 | 158 | else if ((bom & 0xFFFF) == 0xFFFE) { type_ = kUTF16BE; hasBOM_ = true; is_->Take(); is_->Take(); } | ||
3624 | 159 | else if ((bom & 0xFFFF) == 0xFEFF) { type_ = kUTF16LE; hasBOM_ = true; is_->Take(); is_->Take(); } | ||
3625 | 160 | else if ((bom & 0xFFFFFF) == 0xBFBBEF) { type_ = kUTF8; hasBOM_ = true; is_->Take(); is_->Take(); is_->Take(); } | ||
3626 | 161 | |||
3627 | 162 | // RFC 4627: Section 3 | ||
3628 | 163 | // "Since the first two characters of a JSON text will always be ASCII | ||
3629 | 164 | // characters [RFC0020], it is possible to determine whether an octet | ||
3630 | 165 | // stream is UTF-8, UTF-16 (BE or LE), or UTF-32 (BE or LE) by looking | ||
3631 | 166 | // at the pattern of nulls in the first four octets." | ||
3632 | 167 | // 00 00 00 xx UTF-32BE | ||
3633 | 168 | // 00 xx 00 xx UTF-16BE | ||
3634 | 169 | // xx 00 00 00 UTF-32LE | ||
3635 | 170 | // xx 00 xx 00 UTF-16LE | ||
3636 | 171 | // xx xx xx xx UTF-8 | ||
3637 | 172 | |||
3638 | 173 | if (!hasBOM_) { | ||
3639 | 174 | unsigned pattern = (c[0] ? 1 : 0) | (c[1] ? 2 : 0) | (c[2] ? 4 : 0) | (c[3] ? 8 : 0); | ||
3640 | 175 | switch (pattern) { | ||
3641 | 176 | case 0x08: type_ = kUTF32BE; break; | ||
3642 | 177 | case 0x0A: type_ = kUTF16BE; break; | ||
3643 | 178 | case 0x01: type_ = kUTF32LE; break; | ||
3644 | 179 | case 0x05: type_ = kUTF16LE; break; | ||
3645 | 180 | case 0x0F: type_ = kUTF8; break; | ||
3646 | 181 | default: break; // Use type defined by user. | ||
3647 | 182 | } | ||
3648 | 183 | } | ||
3649 | 184 | |||
3650 | 185 | // Runtime check whether the size of character type is sufficient. It only perform checks with assertion. | ||
3651 | 186 | switch (type_) { | ||
3652 | 187 | case kUTF8: | ||
3653 | 188 | // Do nothing | ||
3654 | 189 | break; | ||
3655 | 190 | case kUTF16LE: | ||
3656 | 191 | case kUTF16BE: | ||
3657 | 192 | RAPIDJSON_ASSERT(sizeof(Ch) >= 2); | ||
3658 | 193 | break; | ||
3659 | 194 | case kUTF32LE: | ||
3660 | 195 | case kUTF32BE: | ||
3661 | 196 | RAPIDJSON_ASSERT(sizeof(Ch) >= 4); | ||
3662 | 197 | break; | ||
3663 | 198 | default: | ||
3664 | 199 | RAPIDJSON_ASSERT(false); // Invalid type | ||
3665 | 200 | } | ||
3666 | 201 | } | ||
3667 | 202 | |||
3668 | 203 | typedef Ch (*TakeFunc)(InputByteStream& is); | ||
3669 | 204 | InputByteStream* is_; | ||
3670 | 205 | UTFType type_; | ||
3671 | 206 | Ch current_; | ||
3672 | 207 | TakeFunc takeFunc_; | ||
3673 | 208 | bool hasBOM_; | ||
3674 | 209 | }; | ||
3675 | 210 | |||
3676 | 211 | //! Output stream wrapper with dynamically bound encoding and automatic encoding detection. | ||
3677 | 212 | /*! | ||
3678 | 213 | \tparam CharType Type of character for writing. | ||
3679 | 214 | \tparam InputByteStream type of output byte stream to be wrapped. | ||
3680 | 215 | */ | ||
3681 | 216 | template <typename CharType, typename OutputByteStream> | ||
3682 | 217 | class AutoUTFOutputStream { | ||
3683 | 218 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
3684 | 219 | public: | ||
3685 | 220 | typedef CharType Ch; | ||
3686 | 221 | |||
3687 | 222 | //! Constructor. | ||
3688 | 223 | /*! | ||
3689 | 224 | \param os output stream to be wrapped. | ||
3690 | 225 | \param type UTF encoding type. | ||
3691 | 226 | \param putBOM Whether to write BOM at the beginning of the stream. | ||
3692 | 227 | */ | ||
3693 | 228 | AutoUTFOutputStream(OutputByteStream& os, UTFType type, bool putBOM) : os_(&os), type_(type) { | ||
3694 | 229 | // RUntime check whether the size of character type is sufficient. It only perform checks with assertion. | ||
3695 | 230 | switch (type_) { | ||
3696 | 231 | case kUTF16LE: | ||
3697 | 232 | case kUTF16BE: | ||
3698 | 233 | RAPIDJSON_ASSERT(sizeof(Ch) >= 2); | ||
3699 | 234 | break; | ||
3700 | 235 | case kUTF32LE: | ||
3701 | 236 | case kUTF32BE: | ||
3702 | 237 | RAPIDJSON_ASSERT(sizeof(Ch) >= 4); | ||
3703 | 238 | break; | ||
3704 | 239 | case kUTF8: | ||
3705 | 240 | // Do nothing | ||
3706 | 241 | break; | ||
3707 | 242 | default: | ||
3708 | 243 | RAPIDJSON_ASSERT(false); // Invalid UTFType | ||
3709 | 244 | } | ||
3710 | 245 | |||
3711 | 246 | static const PutFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Put) }; | ||
3712 | 247 | putFunc_ = f[type_]; | ||
3713 | 248 | |||
3714 | 249 | if (putBOM) | ||
3715 | 250 | PutBOM(); | ||
3716 | 251 | } | ||
3717 | 252 | |||
3718 | 253 | UTFType GetType() const { return type_; } | ||
3719 | 254 | |||
3720 | 255 | void Put(Ch c) { putFunc_(*os_, c); } | ||
3721 | 256 | void Flush() { os_->Flush(); } | ||
3722 | 257 | |||
3723 | 258 | // Not implemented | ||
3724 | 259 | Ch Peek() const { RAPIDJSON_ASSERT(false); } | ||
3725 | 260 | Ch Take() { RAPIDJSON_ASSERT(false); } | ||
3726 | 261 | size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } | ||
3727 | 262 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } | ||
3728 | 263 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } | ||
3729 | 264 | |||
3730 | 265 | private: | ||
3731 | 266 | AutoUTFOutputStream(const AutoUTFOutputStream&); | ||
3732 | 267 | AutoUTFOutputStream& operator=(const AutoUTFOutputStream&); | ||
3733 | 268 | |||
3734 | 269 | void PutBOM() { | ||
3735 | 270 | typedef void (*PutBOMFunc)(OutputByteStream&); | ||
3736 | 271 | static const PutBOMFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(PutBOM) }; | ||
3737 | 272 | f[type_](*os_); | ||
3738 | 273 | } | ||
3739 | 274 | |||
3740 | 275 | typedef void (*PutFunc)(OutputByteStream&, Ch); | ||
3741 | 276 | |||
3742 | 277 | OutputByteStream* os_; | ||
3743 | 278 | UTFType type_; | ||
3744 | 279 | PutFunc putFunc_; | ||
3745 | 280 | }; | ||
3746 | 281 | |||
3747 | 282 | #undef RAPIDJSON_ENCODINGS_FUNC | ||
3748 | 283 | |||
3749 | 284 | RAPIDJSON_NAMESPACE_END | ||
3750 | 285 | |||
3751 | 286 | #ifdef __GNUC__ | ||
3752 | 287 | RAPIDJSON_DIAG_POP | ||
3753 | 288 | #endif | ||
3754 | 289 | |||
3755 | 290 | #endif // RAPIDJSON_FILESTREAM_H_ | ||
3756 | 0 | 291 | ||
3757 | === added file 'shorts/xml2json/rapidjson/encodings.h' | |||
3758 | --- shorts/xml2json/rapidjson/encodings.h 1970-01-01 00:00:00 +0000 | |||
3759 | +++ shorts/xml2json/rapidjson/encodings.h 2015-12-15 16:05:21 +0000 | |||
3760 | @@ -0,0 +1,630 @@ | |||
3761 | 1 | // Copyright (C) 2011 Milo Yip | ||
3762 | 2 | // | ||
3763 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
3764 | 4 | // of this software and associated documentation files (the "Software"), to deal | ||
3765 | 5 | // in the Software without restriction, including without limitation the rights | ||
3766 | 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
3767 | 7 | // copies of the Software, and to permit persons to whom the Software is | ||
3768 | 8 | // furnished to do so, subject to the following conditions: | ||
3769 | 9 | // | ||
3770 | 10 | // The above copyright notice and this permission notice shall be included in | ||
3771 | 11 | // all copies or substantial portions of the Software. | ||
3772 | 12 | // | ||
3773 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
3774 | 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
3775 | 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
3776 | 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
3777 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
3778 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
3779 | 19 | // THE SOFTWARE. | ||
3780 | 20 | |||
3781 | 21 | #ifndef RAPIDJSON_ENCODINGS_H_ | ||
3782 | 22 | #define RAPIDJSON_ENCODINGS_H_ | ||
3783 | 23 | |||
3784 | 24 | #include "rapidjson.h" | ||
3785 | 25 | |||
3786 | 26 | #ifdef _MSC_VER | ||
3787 | 27 | RAPIDJSON_DIAG_PUSH | ||
3788 | 28 | RAPIDJSON_DIAG_OFF(4244) // conversion from 'type1' to 'type2', possible loss of data | ||
3789 | 29 | RAPIDJSON_DIAG_OFF(4702) // unreachable code | ||
3790 | 30 | #elif defined(__GNUC__) | ||
3791 | 31 | RAPIDJSON_DIAG_PUSH | ||
3792 | 32 | RAPIDJSON_DIAG_OFF(effc++) | ||
3793 | 33 | #endif | ||
3794 | 34 | |||
3795 | 35 | RAPIDJSON_NAMESPACE_BEGIN | ||
3796 | 36 | |||
3797 | 37 | /////////////////////////////////////////////////////////////////////////////// | ||
3798 | 38 | // Encoding | ||
3799 | 39 | |||
3800 | 40 | /*! \class rapidjson::Encoding | ||
3801 | 41 | \brief Concept for encoding of Unicode characters. | ||
3802 | 42 | |||
3803 | 43 | \code | ||
3804 | 44 | concept Encoding { | ||
3805 | 45 | typename Ch; //! Type of character. A "character" is actually a code unit in unicode's definition. | ||
3806 | 46 | |||
3807 | 47 | enum { supportUnicode = 1 }; // or 0 if not supporting unicode | ||
3808 | 48 | |||
3809 | 49 | //! \brief Encode a Unicode codepoint to an output stream. | ||
3810 | 50 | //! \param os Output stream. | ||
3811 | 51 | //! \param codepoint An unicode codepoint, ranging from 0x0 to 0x10FFFF inclusively. | ||
3812 | 52 | template<typename OutputStream> | ||
3813 | 53 | static void Encode(OutputStream& os, unsigned codepoint); | ||
3814 | 54 | |||
3815 | 55 | //! \brief Decode a Unicode codepoint from an input stream. | ||
3816 | 56 | //! \param is Input stream. | ||
3817 | 57 | //! \param codepoint Output of the unicode codepoint. | ||
3818 | 58 | //! \return true if a valid codepoint can be decoded from the stream. | ||
3819 | 59 | template <typename InputStream> | ||
3820 | 60 | static bool Decode(InputStream& is, unsigned* codepoint); | ||
3821 | 61 | |||
3822 | 62 | //! \brief Validate one Unicode codepoint from an encoded stream. | ||
3823 | 63 | //! \param is Input stream to obtain codepoint. | ||
3824 | 64 | //! \param os Output for copying one codepoint. | ||
3825 | 65 | //! \return true if it is valid. | ||
3826 | 66 | //! \note This function just validating and copying the codepoint without actually decode it. | ||
3827 | 67 | template <typename InputStream, typename OutputStream> | ||
3828 | 68 | static bool Validate(InputStream& is, OutputStream& os); | ||
3829 | 69 | |||
3830 | 70 | // The following functions are deal with byte streams. | ||
3831 | 71 | |||
3832 | 72 | //! Take a character from input byte stream, skip BOM if exist. | ||
3833 | 73 | template <typename InputByteStream> | ||
3834 | 74 | static CharType TakeBOM(InputByteStream& is); | ||
3835 | 75 | |||
3836 | 76 | //! Take a character from input byte stream. | ||
3837 | 77 | template <typename InputByteStream> | ||
3838 | 78 | static Ch Take(InputByteStream& is); | ||
3839 | 79 | |||
3840 | 80 | //! Put BOM to output byte stream. | ||
3841 | 81 | template <typename OutputByteStream> | ||
3842 | 82 | static void PutBOM(OutputByteStream& os); | ||
3843 | 83 | |||
3844 | 84 | //! Put a character to output byte stream. | ||
3845 | 85 | template <typename OutputByteStream> | ||
3846 | 86 | static void Put(OutputByteStream& os, Ch c); | ||
3847 | 87 | }; | ||
3848 | 88 | \endcode | ||
3849 | 89 | */ | ||
3850 | 90 | |||
3851 | 91 | /////////////////////////////////////////////////////////////////////////////// | ||
3852 | 92 | // UTF8 | ||
3853 | 93 | |||
3854 | 94 | //! UTF-8 encoding. | ||
3855 | 95 | /*! http://en.wikipedia.org/wiki/UTF-8 | ||
3856 | 96 | http://tools.ietf.org/html/rfc3629 | ||
3857 | 97 | \tparam CharType Code unit for storing 8-bit UTF-8 data. Default is char. | ||
3858 | 98 | \note implements Encoding concept | ||
3859 | 99 | */ | ||
3860 | 100 | template<typename CharType = char> | ||
3861 | 101 | struct UTF8 { | ||
3862 | 102 | typedef CharType Ch; | ||
3863 | 103 | |||
3864 | 104 | enum { supportUnicode = 1 }; | ||
3865 | 105 | |||
3866 | 106 | template<typename OutputStream> | ||
3867 | 107 | static void Encode(OutputStream& os, unsigned codepoint) { | ||
3868 | 108 | if (codepoint <= 0x7F) | ||
3869 | 109 | os.Put(static_cast<Ch>(codepoint & 0xFF)); | ||
3870 | 110 | else if (codepoint <= 0x7FF) { | ||
3871 | 111 | os.Put(static_cast<Ch>(0xC0 | ((codepoint >> 6) & 0xFF))); | ||
3872 | 112 | os.Put(static_cast<Ch>(0x80 | ((codepoint & 0x3F)))); | ||
3873 | 113 | } | ||
3874 | 114 | else if (codepoint <= 0xFFFF) { | ||
3875 | 115 | os.Put(static_cast<Ch>(0xE0 | ((codepoint >> 12) & 0xFF))); | ||
3876 | 116 | os.Put(static_cast<Ch>(0x80 | ((codepoint >> 6) & 0x3F))); | ||
3877 | 117 | os.Put(static_cast<Ch>(0x80 | (codepoint & 0x3F))); | ||
3878 | 118 | } | ||
3879 | 119 | else { | ||
3880 | 120 | RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); | ||
3881 | 121 | os.Put(static_cast<Ch>(0xF0 | ((codepoint >> 18) & 0xFF))); | ||
3882 | 122 | os.Put(static_cast<Ch>(0x80 | ((codepoint >> 12) & 0x3F))); | ||
3883 | 123 | os.Put(static_cast<Ch>(0x80 | ((codepoint >> 6) & 0x3F))); | ||
3884 | 124 | os.Put(static_cast<Ch>(0x80 | (codepoint & 0x3F))); | ||
3885 | 125 | } | ||
3886 | 126 | } | ||
3887 | 127 | |||
3888 | 128 | template <typename InputStream> | ||
3889 | 129 | static bool Decode(InputStream& is, unsigned* codepoint) { | ||
3890 | 130 | #define COPY() c = is.Take(); *codepoint = (*codepoint << 6) | ((unsigned char)c & 0x3Fu) | ||
3891 | 131 | #define TRANS(mask) result &= ((GetRange((unsigned char)c) & mask) != 0) | ||
3892 | 132 | #define TAIL() COPY(); TRANS(0x70) | ||
3893 | 133 | Ch c = is.Take(); | ||
3894 | 134 | if (!(c & 0x80)) { | ||
3895 | 135 | *codepoint = (unsigned char)c; | ||
3896 | 136 | return true; | ||
3897 | 137 | } | ||
3898 | 138 | |||
3899 | 139 | unsigned char type = GetRange((unsigned char)c); | ||
3900 | 140 | *codepoint = (0xFF >> type) & (unsigned char)c; | ||
3901 | 141 | bool result = true; | ||
3902 | 142 | switch (type) { | ||
3903 | 143 | case 2: TAIL(); return result; | ||
3904 | 144 | case 3: TAIL(); TAIL(); return result; | ||
3905 | 145 | case 4: COPY(); TRANS(0x50); TAIL(); return result; | ||
3906 | 146 | case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; | ||
3907 | 147 | case 6: TAIL(); TAIL(); TAIL(); return result; | ||
3908 | 148 | case 10: COPY(); TRANS(0x20); TAIL(); return result; | ||
3909 | 149 | case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; | ||
3910 | 150 | default: return false; | ||
3911 | 151 | } | ||
3912 | 152 | #undef COPY | ||
3913 | 153 | #undef TRANS | ||
3914 | 154 | #undef TAIL | ||
3915 | 155 | } | ||
3916 | 156 | |||
3917 | 157 | template <typename InputStream, typename OutputStream> | ||
3918 | 158 | static bool Validate(InputStream& is, OutputStream& os) { | ||
3919 | 159 | #define COPY() os.Put(c = is.Take()) | ||
3920 | 160 | #define TRANS(mask) result &= ((GetRange((unsigned char)c) & mask) != 0) | ||
3921 | 161 | #define TAIL() COPY(); TRANS(0x70) | ||
3922 | 162 | Ch c; | ||
3923 | 163 | COPY(); | ||
3924 | 164 | if (!(c & 0x80)) | ||
3925 | 165 | return true; | ||
3926 | 166 | |||
3927 | 167 | bool result = true; | ||
3928 | 168 | switch (GetRange((unsigned char)c)) { | ||
3929 | 169 | case 2: TAIL(); return result; | ||
3930 | 170 | case 3: TAIL(); TAIL(); return result; | ||
3931 | 171 | case 4: COPY(); TRANS(0x50); TAIL(); return result; | ||
3932 | 172 | case 5: COPY(); TRANS(0x10); TAIL(); TAIL(); return result; | ||
3933 | 173 | case 6: TAIL(); TAIL(); TAIL(); return result; | ||
3934 | 174 | case 10: COPY(); TRANS(0x20); TAIL(); return result; | ||
3935 | 175 | case 11: COPY(); TRANS(0x60); TAIL(); TAIL(); return result; | ||
3936 | 176 | default: return false; | ||
3937 | 177 | } | ||
3938 | 178 | #undef COPY | ||
3939 | 179 | #undef TRANS | ||
3940 | 180 | #undef TAIL | ||
3941 | 181 | } | ||
3942 | 182 | |||
3943 | 183 | static unsigned char GetRange(unsigned char c) { | ||
3944 | 184 | // Referring to DFA of http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ | ||
3945 | 185 | // With new mapping 1 -> 0x10, 7 -> 0x20, 9 -> 0x40, such that AND operation can test multiple types. | ||
3946 | 186 | static const unsigned char type[] = { | ||
3947 | 187 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||
3948 | 188 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||
3949 | 189 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||
3950 | 190 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, | ||
3951 | 191 | 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10, | ||
3952 | 192 | 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40, | ||
3953 | 193 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, | ||
3954 | 194 | 0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20, | ||
3955 | 195 | 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, | ||
3956 | 196 | 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8, | ||
3957 | 197 | }; | ||
3958 | 198 | return type[c]; | ||
3959 | 199 | } | ||
3960 | 200 | |||
3961 | 201 | template <typename InputByteStream> | ||
3962 | 202 | static CharType TakeBOM(InputByteStream& is) { | ||
3963 | 203 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
3964 | 204 | Ch c = Take(is); | ||
3965 | 205 | if ((unsigned char)c != 0xEFu) return c; | ||
3966 | 206 | c = is.Take(); | ||
3967 | 207 | if ((unsigned char)c != 0xBBu) return c; | ||
3968 | 208 | c = is.Take(); | ||
3969 | 209 | if ((unsigned char)c != 0xBFu) return c; | ||
3970 | 210 | c = is.Take(); | ||
3971 | 211 | return c; | ||
3972 | 212 | } | ||
3973 | 213 | |||
3974 | 214 | template <typename InputByteStream> | ||
3975 | 215 | static Ch Take(InputByteStream& is) { | ||
3976 | 216 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
3977 | 217 | return is.Take(); | ||
3978 | 218 | } | ||
3979 | 219 | |||
3980 | 220 | template <typename OutputByteStream> | ||
3981 | 221 | static void PutBOM(OutputByteStream& os) { | ||
3982 | 222 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
3983 | 223 | os.Put(0xEFu); os.Put(0xBBu); os.Put(0xBFu); | ||
3984 | 224 | } | ||
3985 | 225 | |||
3986 | 226 | template <typename OutputByteStream> | ||
3987 | 227 | static void Put(OutputByteStream& os, Ch c) { | ||
3988 | 228 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
3989 | 229 | os.Put(static_cast<typename OutputByteStream::Ch>(c)); | ||
3990 | 230 | } | ||
3991 | 231 | }; | ||
3992 | 232 | |||
3993 | 233 | /////////////////////////////////////////////////////////////////////////////// | ||
3994 | 234 | // UTF16 | ||
3995 | 235 | |||
3996 | 236 | //! UTF-16 encoding. | ||
3997 | 237 | /*! http://en.wikipedia.org/wiki/UTF-16 | ||
3998 | 238 | http://tools.ietf.org/html/rfc2781 | ||
3999 | 239 | \tparam CharType Type for storing 16-bit UTF-16 data. Default is wchar_t. C++11 may use char16_t instead. | ||
4000 | 240 | \note implements Encoding concept | ||
4001 | 241 | |||
4002 | 242 | \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. | ||
4003 | 243 | For streaming, use UTF16LE and UTF16BE, which handle endianness. | ||
4004 | 244 | */ | ||
4005 | 245 | template<typename CharType = wchar_t> | ||
4006 | 246 | struct UTF16 { | ||
4007 | 247 | typedef CharType Ch; | ||
4008 | 248 | RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 2); | ||
4009 | 249 | |||
4010 | 250 | enum { supportUnicode = 1 }; | ||
4011 | 251 | |||
4012 | 252 | template<typename OutputStream> | ||
4013 | 253 | static void Encode(OutputStream& os, unsigned codepoint) { | ||
4014 | 254 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); | ||
4015 | 255 | if (codepoint <= 0xFFFF) { | ||
4016 | 256 | RAPIDJSON_ASSERT(codepoint < 0xD800 || codepoint > 0xDFFF); // Code point itself cannot be surrogate pair | ||
4017 | 257 | os.Put(static_cast<typename OutputStream::Ch>(codepoint)); | ||
4018 | 258 | } | ||
4019 | 259 | else { | ||
4020 | 260 | RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); | ||
4021 | 261 | unsigned v = codepoint - 0x10000; | ||
4022 | 262 | os.Put(static_cast<typename OutputStream::Ch>((v >> 10) | 0xD800)); | ||
4023 | 263 | os.Put((v & 0x3FF) | 0xDC00); | ||
4024 | 264 | } | ||
4025 | 265 | } | ||
4026 | 266 | |||
4027 | 267 | template <typename InputStream> | ||
4028 | 268 | static bool Decode(InputStream& is, unsigned* codepoint) { | ||
4029 | 269 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); | ||
4030 | 270 | Ch c = is.Take(); | ||
4031 | 271 | if (c < 0xD800 || c > 0xDFFF) { | ||
4032 | 272 | *codepoint = c; | ||
4033 | 273 | return true; | ||
4034 | 274 | } | ||
4035 | 275 | else if (c <= 0xDBFF) { | ||
4036 | 276 | *codepoint = (c & 0x3FF) << 10; | ||
4037 | 277 | c = is.Take(); | ||
4038 | 278 | *codepoint |= (c & 0x3FF); | ||
4039 | 279 | *codepoint += 0x10000; | ||
4040 | 280 | return c >= 0xDC00 && c <= 0xDFFF; | ||
4041 | 281 | } | ||
4042 | 282 | return false; | ||
4043 | 283 | } | ||
4044 | 284 | |||
4045 | 285 | template <typename InputStream, typename OutputStream> | ||
4046 | 286 | static bool Validate(InputStream& is, OutputStream& os) { | ||
4047 | 287 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 2); | ||
4048 | 288 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 2); | ||
4049 | 289 | Ch c; | ||
4050 | 290 | os.Put(c = is.Take()); | ||
4051 | 291 | if (c < 0xD800 || c > 0xDFFF) | ||
4052 | 292 | return true; | ||
4053 | 293 | else if (c <= 0xDBFF) { | ||
4054 | 294 | os.Put(c = is.Take()); | ||
4055 | 295 | return c >= 0xDC00 && c <= 0xDFFF; | ||
4056 | 296 | } | ||
4057 | 297 | return false; | ||
4058 | 298 | } | ||
4059 | 299 | }; | ||
4060 | 300 | |||
4061 | 301 | //! UTF-16 little endian encoding. | ||
4062 | 302 | template<typename CharType = wchar_t> | ||
4063 | 303 | struct UTF16LE : UTF16<CharType> { | ||
4064 | 304 | template <typename InputByteStream> | ||
4065 | 305 | static CharType TakeBOM(InputByteStream& is) { | ||
4066 | 306 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
4067 | 307 | CharType c = Take(is); | ||
4068 | 308 | return (unsigned short)c == 0xFEFFu ? Take(is) : c; | ||
4069 | 309 | } | ||
4070 | 310 | |||
4071 | 311 | template <typename InputByteStream> | ||
4072 | 312 | static CharType Take(InputByteStream& is) { | ||
4073 | 313 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
4074 | 314 | CharType c = (unsigned char)is.Take(); | ||
4075 | 315 | c |= (unsigned char)is.Take() << 8; | ||
4076 | 316 | return c; | ||
4077 | 317 | } | ||
4078 | 318 | |||
4079 | 319 | template <typename OutputByteStream> | ||
4080 | 320 | static void PutBOM(OutputByteStream& os) { | ||
4081 | 321 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
4082 | 322 | os.Put(0xFFu); os.Put(0xFEu); | ||
4083 | 323 | } | ||
4084 | 324 | |||
4085 | 325 | template <typename OutputByteStream> | ||
4086 | 326 | static void Put(OutputByteStream& os, CharType c) { | ||
4087 | 327 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
4088 | 328 | os.Put(c & 0xFFu); | ||
4089 | 329 | os.Put((c >> 8) & 0xFFu); | ||
4090 | 330 | } | ||
4091 | 331 | }; | ||
4092 | 332 | |||
4093 | 333 | //! UTF-16 big endian encoding. | ||
4094 | 334 | template<typename CharType = wchar_t> | ||
4095 | 335 | struct UTF16BE : UTF16<CharType> { | ||
4096 | 336 | template <typename InputByteStream> | ||
4097 | 337 | static CharType TakeBOM(InputByteStream& is) { | ||
4098 | 338 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
4099 | 339 | CharType c = Take(is); | ||
4100 | 340 | return (unsigned short)c == 0xFEFFu ? Take(is) : c; | ||
4101 | 341 | } | ||
4102 | 342 | |||
4103 | 343 | template <typename InputByteStream> | ||
4104 | 344 | static CharType Take(InputByteStream& is) { | ||
4105 | 345 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
4106 | 346 | CharType c = (unsigned char)is.Take() << 8; | ||
4107 | 347 | c |= (unsigned char)is.Take(); | ||
4108 | 348 | return c; | ||
4109 | 349 | } | ||
4110 | 350 | |||
4111 | 351 | template <typename OutputByteStream> | ||
4112 | 352 | static void PutBOM(OutputByteStream& os) { | ||
4113 | 353 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
4114 | 354 | os.Put(0xFEu); os.Put(0xFFu); | ||
4115 | 355 | } | ||
4116 | 356 | |||
4117 | 357 | template <typename OutputByteStream> | ||
4118 | 358 | static void Put(OutputByteStream& os, CharType c) { | ||
4119 | 359 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
4120 | 360 | os.Put((c >> 8) & 0xFFu); | ||
4121 | 361 | os.Put(c & 0xFFu); | ||
4122 | 362 | } | ||
4123 | 363 | }; | ||
4124 | 364 | |||
4125 | 365 | /////////////////////////////////////////////////////////////////////////////// | ||
4126 | 366 | // UTF32 | ||
4127 | 367 | |||
4128 | 368 | //! UTF-32 encoding. | ||
4129 | 369 | /*! http://en.wikipedia.org/wiki/UTF-32 | ||
4130 | 370 | \tparam CharType Type for storing 32-bit UTF-32 data. Default is unsigned. C++11 may use char32_t instead. | ||
4131 | 371 | \note implements Encoding concept | ||
4132 | 372 | |||
4133 | 373 | \note For in-memory access, no need to concern endianness. The code units and code points are represented by CPU's endianness. | ||
4134 | 374 | For streaming, use UTF32LE and UTF32BE, which handle endianness. | ||
4135 | 375 | */ | ||
4136 | 376 | template<typename CharType = unsigned> | ||
4137 | 377 | struct UTF32 { | ||
4138 | 378 | typedef CharType Ch; | ||
4139 | 379 | RAPIDJSON_STATIC_ASSERT(sizeof(Ch) >= 4); | ||
4140 | 380 | |||
4141 | 381 | enum { supportUnicode = 1 }; | ||
4142 | 382 | |||
4143 | 383 | template<typename OutputStream> | ||
4144 | 384 | static void Encode(OutputStream& os, unsigned codepoint) { | ||
4145 | 385 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputStream::Ch) >= 4); | ||
4146 | 386 | RAPIDJSON_ASSERT(codepoint <= 0x10FFFF); | ||
4147 | 387 | os.Put(codepoint); | ||
4148 | 388 | } | ||
4149 | 389 | |||
4150 | 390 | template <typename InputStream> | ||
4151 | 391 | static bool Decode(InputStream& is, unsigned* codepoint) { | ||
4152 | 392 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); | ||
4153 | 393 | Ch c = is.Take(); | ||
4154 | 394 | *codepoint = c; | ||
4155 | 395 | return c <= 0x10FFFF; | ||
4156 | 396 | } | ||
4157 | 397 | |||
4158 | 398 | template <typename InputStream, typename OutputStream> | ||
4159 | 399 | static bool Validate(InputStream& is, OutputStream& os) { | ||
4160 | 400 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputStream::Ch) >= 4); | ||
4161 | 401 | Ch c; | ||
4162 | 402 | os.Put(c = is.Take()); | ||
4163 | 403 | return c <= 0x10FFFF; | ||
4164 | 404 | } | ||
4165 | 405 | }; | ||
4166 | 406 | |||
4167 | 407 | //! UTF-32 little endian enocoding. | ||
4168 | 408 | template<typename CharType = unsigned> | ||
4169 | 409 | struct UTF32LE : UTF32<CharType> { | ||
4170 | 410 | template <typename InputByteStream> | ||
4171 | 411 | static CharType TakeBOM(InputByteStream& is) { | ||
4172 | 412 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
4173 | 413 | CharType c = Take(is); | ||
4174 | 414 | return (unsigned)c == 0x0000FEFFu ? Take(is) : c; | ||
4175 | 415 | } | ||
4176 | 416 | |||
4177 | 417 | template <typename InputByteStream> | ||
4178 | 418 | static CharType Take(InputByteStream& is) { | ||
4179 | 419 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
4180 | 420 | CharType c = (unsigned char)is.Take(); | ||
4181 | 421 | c |= (unsigned char)is.Take() << 8; | ||
4182 | 422 | c |= (unsigned char)is.Take() << 16; | ||
4183 | 423 | c |= (unsigned char)is.Take() << 24; | ||
4184 | 424 | return c; | ||
4185 | 425 | } | ||
4186 | 426 | |||
4187 | 427 | template <typename OutputByteStream> | ||
4188 | 428 | static void PutBOM(OutputByteStream& os) { | ||
4189 | 429 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
4190 | 430 | os.Put(0xFFu); os.Put(0xFEu); os.Put(0x00u); os.Put(0x00u); | ||
4191 | 431 | } | ||
4192 | 432 | |||
4193 | 433 | template <typename OutputByteStream> | ||
4194 | 434 | static void Put(OutputByteStream& os, CharType c) { | ||
4195 | 435 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
4196 | 436 | os.Put(c & 0xFFu); | ||
4197 | 437 | os.Put((c >> 8) & 0xFFu); | ||
4198 | 438 | os.Put((c >> 16) & 0xFFu); | ||
4199 | 439 | os.Put((c >> 24) & 0xFFu); | ||
4200 | 440 | } | ||
4201 | 441 | }; | ||
4202 | 442 | |||
4203 | 443 | //! UTF-32 big endian encoding. | ||
4204 | 444 | template<typename CharType = unsigned> | ||
4205 | 445 | struct UTF32BE : UTF32<CharType> { | ||
4206 | 446 | template <typename InputByteStream> | ||
4207 | 447 | static CharType TakeBOM(InputByteStream& is) { | ||
4208 | 448 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
4209 | 449 | CharType c = Take(is); | ||
4210 | 450 | return (unsigned)c == 0x0000FEFFu ? Take(is) : c; | ||
4211 | 451 | } | ||
4212 | 452 | |||
4213 | 453 | template <typename InputByteStream> | ||
4214 | 454 | static CharType Take(InputByteStream& is) { | ||
4215 | 455 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
4216 | 456 | CharType c = (unsigned char)is.Take() << 24; | ||
4217 | 457 | c |= (unsigned char)is.Take() << 16; | ||
4218 | 458 | c |= (unsigned char)is.Take() << 8; | ||
4219 | 459 | c |= (unsigned char)is.Take(); | ||
4220 | 460 | return c; | ||
4221 | 461 | } | ||
4222 | 462 | |||
4223 | 463 | template <typename OutputByteStream> | ||
4224 | 464 | static void PutBOM(OutputByteStream& os) { | ||
4225 | 465 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
4226 | 466 | os.Put(0x00u); os.Put(0x00u); os.Put(0xFEu); os.Put(0xFFu); | ||
4227 | 467 | } | ||
4228 | 468 | |||
4229 | 469 | template <typename OutputByteStream> | ||
4230 | 470 | static void Put(OutputByteStream& os, CharType c) { | ||
4231 | 471 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
4232 | 472 | os.Put((c >> 24) & 0xFFu); | ||
4233 | 473 | os.Put((c >> 16) & 0xFFu); | ||
4234 | 474 | os.Put((c >> 8) & 0xFFu); | ||
4235 | 475 | os.Put(c & 0xFFu); | ||
4236 | 476 | } | ||
4237 | 477 | }; | ||
4238 | 478 | |||
4239 | 479 | /////////////////////////////////////////////////////////////////////////////// | ||
4240 | 480 | // ASCII | ||
4241 | 481 | |||
4242 | 482 | //! ASCII encoding. | ||
4243 | 483 | /*! http://en.wikipedia.org/wiki/ASCII | ||
4244 | 484 | \tparam CharType Code unit for storing 7-bit ASCII data. Default is char. | ||
4245 | 485 | \note implements Encoding concept | ||
4246 | 486 | */ | ||
4247 | 487 | template<typename CharType = char> | ||
4248 | 488 | struct ASCII { | ||
4249 | 489 | typedef CharType Ch; | ||
4250 | 490 | |||
4251 | 491 | enum { supportUnicode = 0 }; | ||
4252 | 492 | |||
4253 | 493 | template<typename OutputStream> | ||
4254 | 494 | static void Encode(OutputStream& os, unsigned codepoint) { | ||
4255 | 495 | RAPIDJSON_ASSERT(codepoint <= 0x7F); | ||
4256 | 496 | os.Put(static_cast<Ch>(codepoint & 0xFF)); | ||
4257 | 497 | } | ||
4258 | 498 | |||
4259 | 499 | template <typename InputStream> | ||
4260 | 500 | static bool Decode(InputStream& is, unsigned* codepoint) { | ||
4261 | 501 | unsigned char c = static_cast<unsigned char>(is.Take()); | ||
4262 | 502 | *codepoint = c; | ||
4263 | 503 | return c <= 0X7F; | ||
4264 | 504 | } | ||
4265 | 505 | |||
4266 | 506 | template <typename InputStream, typename OutputStream> | ||
4267 | 507 | static bool Validate(InputStream& is, OutputStream& os) { | ||
4268 | 508 | unsigned char c = is.Take(); | ||
4269 | 509 | os.Put(c); | ||
4270 | 510 | return c <= 0x7F; | ||
4271 | 511 | } | ||
4272 | 512 | |||
4273 | 513 | template <typename InputByteStream> | ||
4274 | 514 | static CharType TakeBOM(InputByteStream& is) { | ||
4275 | 515 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
4276 | 516 | Ch c = Take(is); | ||
4277 | 517 | return c; | ||
4278 | 518 | } | ||
4279 | 519 | |||
4280 | 520 | template <typename InputByteStream> | ||
4281 | 521 | static Ch Take(InputByteStream& is) { | ||
4282 | 522 | RAPIDJSON_STATIC_ASSERT(sizeof(typename InputByteStream::Ch) == 1); | ||
4283 | 523 | return is.Take(); | ||
4284 | 524 | } | ||
4285 | 525 | |||
4286 | 526 | template <typename OutputByteStream> | ||
4287 | 527 | static void PutBOM(OutputByteStream& os) { | ||
4288 | 528 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
4289 | 529 | (void)os; | ||
4290 | 530 | } | ||
4291 | 531 | |||
4292 | 532 | template <typename OutputByteStream> | ||
4293 | 533 | static void Put(OutputByteStream& os, Ch c) { | ||
4294 | 534 | RAPIDJSON_STATIC_ASSERT(sizeof(typename OutputByteStream::Ch) == 1); | ||
4295 | 535 | os.Put(static_cast<typename OutputByteStream::Ch>(c)); | ||
4296 | 536 | } | ||
4297 | 537 | }; | ||
4298 | 538 | |||
4299 | 539 | /////////////////////////////////////////////////////////////////////////////// | ||
4300 | 540 | // AutoUTF | ||
4301 | 541 | |||
4302 | 542 | //! Runtime-specified UTF encoding type of a stream. | ||
4303 | 543 | enum UTFType { | ||
4304 | 544 | kUTF8 = 0, //!< UTF-8. | ||
4305 | 545 | kUTF16LE = 1, //!< UTF-16 little endian. | ||
4306 | 546 | kUTF16BE = 2, //!< UTF-16 big endian. | ||
4307 | 547 | kUTF32LE = 3, //!< UTF-32 little endian. | ||
4308 | 548 | kUTF32BE = 4 //!< UTF-32 big endian. | ||
4309 | 549 | }; | ||
4310 | 550 | |||
4311 | 551 | //! Dynamically select encoding according to stream's runtime-specified UTF encoding type. | ||
4312 | 552 | /*! \note This class can be used with AutoUTFInputtStream and AutoUTFOutputStream, which provides GetType(). | ||
4313 | 553 | */ | ||
4314 | 554 | template<typename CharType> | ||
4315 | 555 | struct AutoUTF { | ||
4316 | 556 | typedef CharType Ch; | ||
4317 | 557 | |||
4318 | 558 | enum { supportUnicode = 1 }; | ||
4319 | 559 | |||
4320 | 560 | #define RAPIDJSON_ENCODINGS_FUNC(x) UTF8<Ch>::x, UTF16LE<Ch>::x, UTF16BE<Ch>::x, UTF32LE<Ch>::x, UTF32BE<Ch>::x | ||
4321 | 561 | |||
4322 | 562 | template<typename OutputStream> | ||
4323 | 563 | RAPIDJSON_FORCEINLINE static void Encode(OutputStream& os, unsigned codepoint) { | ||
4324 | 564 | typedef void (*EncodeFunc)(OutputStream&, unsigned); | ||
4325 | 565 | static const EncodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Encode) }; | ||
4326 | 566 | (*f[os.GetType()])(os, codepoint); | ||
4327 | 567 | } | ||
4328 | 568 | |||
4329 | 569 | template <typename InputStream> | ||
4330 | 570 | RAPIDJSON_FORCEINLINE static bool Decode(InputStream& is, unsigned* codepoint) { | ||
4331 | 571 | typedef bool (*DecodeFunc)(InputStream&, unsigned*); | ||
4332 | 572 | static const DecodeFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Decode) }; | ||
4333 | 573 | return (*f[is.GetType()])(is, codepoint); | ||
4334 | 574 | } | ||
4335 | 575 | |||
4336 | 576 | template <typename InputStream, typename OutputStream> | ||
4337 | 577 | RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { | ||
4338 | 578 | typedef bool (*ValidateFunc)(InputStream&, OutputStream&); | ||
4339 | 579 | static const ValidateFunc f[] = { RAPIDJSON_ENCODINGS_FUNC(Validate) }; | ||
4340 | 580 | return (*f[is.GetType()])(is, os); | ||
4341 | 581 | } | ||
4342 | 582 | |||
4343 | 583 | #undef RAPIDJSON_ENCODINGS_FUNC | ||
4344 | 584 | }; | ||
4345 | 585 | |||
4346 | 586 | /////////////////////////////////////////////////////////////////////////////// | ||
4347 | 587 | // Transcoder | ||
4348 | 588 | |||
4349 | 589 | //! Encoding conversion. | ||
4350 | 590 | template<typename SourceEncoding, typename TargetEncoding> | ||
4351 | 591 | struct Transcoder { | ||
4352 | 592 | //! Take one Unicode codepoint from source encoding, convert it to target encoding and put it to the output stream. | ||
4353 | 593 | template<typename InputStream, typename OutputStream> | ||
4354 | 594 | RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { | ||
4355 | 595 | unsigned codepoint; | ||
4356 | 596 | if (!SourceEncoding::Decode(is, &codepoint)) | ||
4357 | 597 | return false; | ||
4358 | 598 | TargetEncoding::Encode(os, codepoint); | ||
4359 | 599 | return true; | ||
4360 | 600 | } | ||
4361 | 601 | |||
4362 | 602 | //! Validate one Unicode codepoint from an encoded stream. | ||
4363 | 603 | template<typename InputStream, typename OutputStream> | ||
4364 | 604 | RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { | ||
4365 | 605 | return Transcode(is, os); // Since source/target encoding is different, must transcode. | ||
4366 | 606 | } | ||
4367 | 607 | }; | ||
4368 | 608 | |||
4369 | 609 | //! Specialization of Transcoder with same source and target encoding. | ||
4370 | 610 | template<typename Encoding> | ||
4371 | 611 | struct Transcoder<Encoding, Encoding> { | ||
4372 | 612 | template<typename InputStream, typename OutputStream> | ||
4373 | 613 | RAPIDJSON_FORCEINLINE static bool Transcode(InputStream& is, OutputStream& os) { | ||
4374 | 614 | os.Put(is.Take()); // Just copy one code unit. This semantic is different from primary template class. | ||
4375 | 615 | return true; | ||
4376 | 616 | } | ||
4377 | 617 | |||
4378 | 618 | template<typename InputStream, typename OutputStream> | ||
4379 | 619 | RAPIDJSON_FORCEINLINE static bool Validate(InputStream& is, OutputStream& os) { | ||
4380 | 620 | return Encoding::Validate(is, os); // source/target encoding are the same | ||
4381 | 621 | } | ||
4382 | 622 | }; | ||
4383 | 623 | |||
4384 | 624 | RAPIDJSON_NAMESPACE_END | ||
4385 | 625 | |||
4386 | 626 | #if defined(__GNUC__) || defined(_MSV_VER) | ||
4387 | 627 | RAPIDJSON_DIAG_POP | ||
4388 | 628 | #endif | ||
4389 | 629 | |||
4390 | 630 | #endif // RAPIDJSON_ENCODINGS_H_ | ||
4391 | 0 | 631 | ||
4392 | === added directory 'shorts/xml2json/rapidjson/error' | |||
4393 | === added file 'shorts/xml2json/rapidjson/error/en.h' | |||
4394 | --- shorts/xml2json/rapidjson/error/en.h 1970-01-01 00:00:00 +0000 | |||
4395 | +++ shorts/xml2json/rapidjson/error/en.h 2015-12-15 16:05:21 +0000 | |||
4396 | @@ -0,0 +1,71 @@ | |||
4397 | 1 | // Copyright (C) 2011 Milo Yip | ||
4398 | 2 | // | ||
4399 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
4400 | 4 | // of this software and associated documentation files (the "Software"), to deal | ||
4401 | 5 | // in the Software without restriction, including without limitation the rights | ||
4402 | 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
4403 | 7 | // copies of the Software, and to permit persons to whom the Software is | ||
4404 | 8 | // furnished to do so, subject to the following conditions: | ||
4405 | 9 | // | ||
4406 | 10 | // The above copyright notice and this permission notice shall be included in | ||
4407 | 11 | // all copies or substantial portions of the Software. | ||
4408 | 12 | // | ||
4409 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
4410 | 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
4411 | 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
4412 | 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
4413 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
4414 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
4415 | 19 | // THE SOFTWARE. | ||
4416 | 20 | |||
4417 | 21 | #ifndef RAPIDJSON_ERROR_EN_H__ | ||
4418 | 22 | #define RAPIDJSON_ERROR_EN_H__ | ||
4419 | 23 | |||
4420 | 24 | #include "error.h" | ||
4421 | 25 | |||
4422 | 26 | RAPIDJSON_NAMESPACE_BEGIN | ||
4423 | 27 | |||
4424 | 28 | //! Maps error code of parsing into error message. | ||
4425 | 29 | /*! | ||
4426 | 30 | \ingroup RAPIDJSON_ERRORS | ||
4427 | 31 | \param parseErrorCode Error code obtained in parsing. | ||
4428 | 32 | \return the error message. | ||
4429 | 33 | \note User can make a copy of this function for localization. | ||
4430 | 34 | Using switch-case is safer for future modification of error codes. | ||
4431 | 35 | */ | ||
4432 | 36 | inline const RAPIDJSON_ERROR_CHARTYPE* GetParseError_En(ParseErrorCode parseErrorCode) { | ||
4433 | 37 | switch (parseErrorCode) { | ||
4434 | 38 | case kParseErrorNone: return RAPIDJSON_ERROR_STRING("No error."); | ||
4435 | 39 | |||
4436 | 40 | case kParseErrorDocumentEmpty: return RAPIDJSON_ERROR_STRING("The document is empty."); | ||
4437 | 41 | case kParseErrorDocumentRootNotSingular: return RAPIDJSON_ERROR_STRING("The document root must not follow by other values."); | ||
4438 | 42 | |||
4439 | 43 | case kParseErrorValueInvalid: return RAPIDJSON_ERROR_STRING("Invalid value."); | ||
4440 | 44 | |||
4441 | 45 | case kParseErrorObjectMissName: return RAPIDJSON_ERROR_STRING("Missing a name for object member."); | ||
4442 | 46 | case kParseErrorObjectMissColon: return RAPIDJSON_ERROR_STRING("Missing a colon after a name of object member."); | ||
4443 | 47 | case kParseErrorObjectMissCommaOrCurlyBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or '}' after an object member."); | ||
4444 | 48 | |||
4445 | 49 | case kParseErrorArrayMissCommaOrSquareBracket: return RAPIDJSON_ERROR_STRING("Missing a comma or ']' after an array element."); | ||
4446 | 50 | |||
4447 | 51 | case kParseErrorStringUnicodeEscapeInvalidHex: return RAPIDJSON_ERROR_STRING("Incorrect hex digit after \\u escape in string."); | ||
4448 | 52 | case kParseErrorStringUnicodeSurrogateInvalid: return RAPIDJSON_ERROR_STRING("The surrogate pair in string is invalid."); | ||
4449 | 53 | case kParseErrorStringEscapeInvalid: return RAPIDJSON_ERROR_STRING("Invalid escape character in string."); | ||
4450 | 54 | case kParseErrorStringMissQuotationMark: return RAPIDJSON_ERROR_STRING("Missing a closing quotation mark in string."); | ||
4451 | 55 | case kParseErrorStringInvalidEncoding: return RAPIDJSON_ERROR_STRING("Invalid encoding in string."); | ||
4452 | 56 | |||
4453 | 57 | case kParseErrorNumberTooBig: return RAPIDJSON_ERROR_STRING("Number too big to be stored in double."); | ||
4454 | 58 | case kParseErrorNumberMissFraction: return RAPIDJSON_ERROR_STRING("Miss fraction part in number."); | ||
4455 | 59 | case kParseErrorNumberMissExponent: return RAPIDJSON_ERROR_STRING("Miss exponent in number."); | ||
4456 | 60 | |||
4457 | 61 | case kParseErrorTermination: return RAPIDJSON_ERROR_STRING("Terminate parsing due to Handler error."); | ||
4458 | 62 | case kParseErrorUnspecificSyntaxError: return RAPIDJSON_ERROR_STRING("Unspecific syntax error."); | ||
4459 | 63 | |||
4460 | 64 | default: | ||
4461 | 65 | return RAPIDJSON_ERROR_STRING("Unknown error."); | ||
4462 | 66 | } | ||
4463 | 67 | } | ||
4464 | 68 | |||
4465 | 69 | RAPIDJSON_NAMESPACE_END | ||
4466 | 70 | |||
4467 | 71 | #endif // RAPIDJSON_ERROR_EN_H__ | ||
4468 | 0 | 72 | ||
4469 | === added file 'shorts/xml2json/rapidjson/error/error.h' | |||
4470 | --- shorts/xml2json/rapidjson/error/error.h 1970-01-01 00:00:00 +0000 | |||
4471 | +++ shorts/xml2json/rapidjson/error/error.h 2015-12-15 16:05:21 +0000 | |||
4472 | @@ -0,0 +1,150 @@ | |||
4473 | 1 | // Copyright (C) 2011 Milo Yip | ||
4474 | 2 | // | ||
4475 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
4476 | 4 | // of this software and associated documentation files (the "Software"), to deal | ||
4477 | 5 | // in the Software without restriction, including without limitation the rights | ||
4478 | 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
4479 | 7 | // copies of the Software, and to permit persons to whom the Software is | ||
4480 | 8 | // furnished to do so, subject to the following conditions: | ||
4481 | 9 | // | ||
4482 | 10 | // The above copyright notice and this permission notice shall be included in | ||
4483 | 11 | // all copies or substantial portions of the Software. | ||
4484 | 12 | // | ||
4485 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
4486 | 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
4487 | 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
4488 | 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
4489 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
4490 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
4491 | 19 | // THE SOFTWARE. | ||
4492 | 20 | |||
4493 | 21 | #ifndef RAPIDJSON_ERROR_ERROR_H__ | ||
4494 | 22 | #define RAPIDJSON_ERROR_ERROR_H__ | ||
4495 | 23 | |||
4496 | 24 | /*! \file error.h */ | ||
4497 | 25 | |||
4498 | 26 | /*! \defgroup RAPIDJSON_ERRORS RapidJSON error handling */ | ||
4499 | 27 | |||
4500 | 28 | /////////////////////////////////////////////////////////////////////////////// | ||
4501 | 29 | // RAPIDJSON_ERROR_CHARTYPE | ||
4502 | 30 | |||
4503 | 31 | //! Character type of error messages. | ||
4504 | 32 | /*! \ingroup RAPIDJSON_ERRORS | ||
4505 | 33 | The default character type is \c char. | ||
4506 | 34 | On Windows, user can define this macro as \c TCHAR for supporting both | ||
4507 | 35 | unicode/non-unicode settings. | ||
4508 | 36 | */ | ||
4509 | 37 | #ifndef RAPIDJSON_ERROR_CHARTYPE | ||
4510 | 38 | #define RAPIDJSON_ERROR_CHARTYPE char | ||
4511 | 39 | #endif | ||
4512 | 40 | |||
4513 | 41 | /////////////////////////////////////////////////////////////////////////////// | ||
4514 | 42 | // RAPIDJSON_ERROR_STRING | ||
4515 | 43 | |||
4516 | 44 | //! Macro for converting string literial to \ref RAPIDJSON_ERROR_CHARTYPE[]. | ||
4517 | 45 | /*! \ingroup RAPIDJSON_ERRORS | ||
4518 | 46 | By default this conversion macro does nothing. | ||
4519 | 47 | On Windows, user can define this macro as \c _T(x) for supporting both | ||
4520 | 48 | unicode/non-unicode settings. | ||
4521 | 49 | */ | ||
4522 | 50 | #ifndef RAPIDJSON_ERROR_STRING | ||
4523 | 51 | #define RAPIDJSON_ERROR_STRING(x) x | ||
4524 | 52 | #endif | ||
4525 | 53 | |||
4526 | 54 | RAPIDJSON_NAMESPACE_BEGIN | ||
4527 | 55 | |||
4528 | 56 | /////////////////////////////////////////////////////////////////////////////// | ||
4529 | 57 | // ParseErrorCode | ||
4530 | 58 | |||
4531 | 59 | //! Error code of parsing. | ||
4532 | 60 | /*! \ingroup RAPIDJSON_ERRORS | ||
4533 | 61 | \see GenericReader::Parse, GenericReader::GetParseErrorCode | ||
4534 | 62 | */ | ||
4535 | 63 | enum ParseErrorCode { | ||
4536 | 64 | kParseErrorNone = 0, //!< No error. | ||
4537 | 65 | |||
4538 | 66 | kParseErrorDocumentEmpty, //!< The document is empty. | ||
4539 | 67 | kParseErrorDocumentRootNotSingular, //!< The document root must not follow by other values. | ||
4540 | 68 | |||
4541 | 69 | kParseErrorValueInvalid, //!< Invalid value. | ||
4542 | 70 | |||
4543 | 71 | kParseErrorObjectMissName, //!< Missing a name for object member. | ||
4544 | 72 | kParseErrorObjectMissColon, //!< Missing a colon after a name of object member. | ||
4545 | 73 | kParseErrorObjectMissCommaOrCurlyBracket, //!< Missing a comma or '}' after an object member. | ||
4546 | 74 | |||
4547 | 75 | kParseErrorArrayMissCommaOrSquareBracket, //!< Missing a comma or ']' after an array element. | ||
4548 | 76 | |||
4549 | 77 | kParseErrorStringUnicodeEscapeInvalidHex, //!< Incorrect hex digit after \\u escape in string. | ||
4550 | 78 | kParseErrorStringUnicodeSurrogateInvalid, //!< The surrogate pair in string is invalid. | ||
4551 | 79 | kParseErrorStringEscapeInvalid, //!< Invalid escape character in string. | ||
4552 | 80 | kParseErrorStringMissQuotationMark, //!< Missing a closing quotation mark in string. | ||
4553 | 81 | kParseErrorStringInvalidEncoding, //!< Invalid encoding in string. | ||
4554 | 82 | |||
4555 | 83 | kParseErrorNumberTooBig, //!< Number too big to be stored in double. | ||
4556 | 84 | kParseErrorNumberMissFraction, //!< Miss fraction part in number. | ||
4557 | 85 | kParseErrorNumberMissExponent, //!< Miss exponent in number. | ||
4558 | 86 | |||
4559 | 87 | kParseErrorTermination, //!< Parsing was terminated. | ||
4560 | 88 | kParseErrorUnspecificSyntaxError //!< Unspecific syntax error. | ||
4561 | 89 | }; | ||
4562 | 90 | |||
4563 | 91 | //! Result of parsing (wraps ParseErrorCode) | ||
4564 | 92 | /*! | ||
4565 | 93 | \ingroup RAPIDJSON_ERRORS | ||
4566 | 94 | \code | ||
4567 | 95 | Document doc; | ||
4568 | 96 | ParseResult ok = doc.Parse("[42]"); | ||
4569 | 97 | if (!ok) { | ||
4570 | 98 | fprintf(stderr, "JSON parse error: %s (%u)", | ||
4571 | 99 | GetParseError_En(ok.Code()), ok.Offset()); | ||
4572 | 100 | exit(EXIT_FAILURE); | ||
4573 | 101 | } | ||
4574 | 102 | \endcode | ||
4575 | 103 | \see GenericReader::Parse, GenericDocument::Parse | ||
4576 | 104 | */ | ||
4577 | 105 | struct ParseResult { | ||
4578 | 106 | |||
4579 | 107 | //! Default constructor, no error. | ||
4580 | 108 | ParseResult() : code_(kParseErrorNone), offset_(0) {} | ||
4581 | 109 | //! Constructor to set an error. | ||
4582 | 110 | ParseResult(ParseErrorCode code, size_t offset) : code_(code), offset_(offset) {} | ||
4583 | 111 | |||
4584 | 112 | //! Get the error code. | ||
4585 | 113 | ParseErrorCode Code() const { return code_; } | ||
4586 | 114 | //! Get the error offset, if \ref IsError(), 0 otherwise. | ||
4587 | 115 | size_t Offset() const { return offset_; } | ||
4588 | 116 | |||
4589 | 117 | //! Conversion to \c bool, returns \c true, iff !\ref IsError(). | ||
4590 | 118 | operator bool() const { return !IsError(); } | ||
4591 | 119 | //! Whether the result is an error. | ||
4592 | 120 | bool IsError() const { return code_ != kParseErrorNone; } | ||
4593 | 121 | |||
4594 | 122 | bool operator==(const ParseResult& that) const { return code_ == that.code_; } | ||
4595 | 123 | bool operator==(ParseErrorCode code) const { return code_ == code; } | ||
4596 | 124 | friend bool operator==(ParseErrorCode code, const ParseResult & err) { return code == err.code_; } | ||
4597 | 125 | |||
4598 | 126 | //! Reset error code. | ||
4599 | 127 | void Clear() { Set(kParseErrorNone); } | ||
4600 | 128 | //! Update error code and offset. | ||
4601 | 129 | void Set(ParseErrorCode code, size_t offset = 0) { code_ = code; offset_ = offset; } | ||
4602 | 130 | |||
4603 | 131 | private: | ||
4604 | 132 | ParseErrorCode code_; | ||
4605 | 133 | size_t offset_; | ||
4606 | 134 | }; | ||
4607 | 135 | |||
4608 | 136 | //! Function pointer type of GetParseError(). | ||
4609 | 137 | /*! \ingroup RAPIDJSON_ERRORS | ||
4610 | 138 | |||
4611 | 139 | This is the prototype for \c GetParseError_X(), where \c X is a locale. | ||
4612 | 140 | User can dynamically change locale in runtime, e.g.: | ||
4613 | 141 | \code | ||
4614 | 142 | GetParseErrorFunc GetParseError = GetParseError_En; // or whatever | ||
4615 | 143 | const RAPIDJSON_ERROR_CHARTYPE* s = GetParseError(document.GetParseErrorCode()); | ||
4616 | 144 | \endcode | ||
4617 | 145 | */ | ||
4618 | 146 | typedef const RAPIDJSON_ERROR_CHARTYPE* (*GetParseErrorFunc)(ParseErrorCode); | ||
4619 | 147 | |||
4620 | 148 | RAPIDJSON_NAMESPACE_END | ||
4621 | 149 | |||
4622 | 150 | #endif // RAPIDJSON_ERROR_ERROR_H__ | ||
4623 | 0 | 151 | ||
4624 | === added file 'shorts/xml2json/rapidjson/filereadstream.h' | |||
4625 | --- shorts/xml2json/rapidjson/filereadstream.h 1970-01-01 00:00:00 +0000 | |||
4626 | +++ shorts/xml2json/rapidjson/filereadstream.h 2015-12-15 16:05:21 +0000 | |||
4627 | @@ -0,0 +1,94 @@ | |||
4628 | 1 | // Copyright (C) 2011 Milo Yip | ||
4629 | 2 | // | ||
4630 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
4631 | 4 | // of this software and associated documentation files (the "Software"), to deal | ||
4632 | 5 | // in the Software without restriction, including without limitation the rights | ||
4633 | 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
4634 | 7 | // copies of the Software, and to permit persons to whom the Software is | ||
4635 | 8 | // furnished to do so, subject to the following conditions: | ||
4636 | 9 | // | ||
4637 | 10 | // The above copyright notice and this permission notice shall be included in | ||
4638 | 11 | // all copies or substantial portions of the Software. | ||
4639 | 12 | // | ||
4640 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
4641 | 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
4642 | 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
4643 | 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
4644 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
4645 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
4646 | 19 | // THE SOFTWARE. | ||
4647 | 20 | |||
4648 | 21 | #ifndef RAPIDJSON_FILEREADSTREAM_H_ | ||
4649 | 22 | #define RAPIDJSON_FILEREADSTREAM_H_ | ||
4650 | 23 | |||
4651 | 24 | #include "rapidjson.h" | ||
4652 | 25 | #include <cstdio> | ||
4653 | 26 | |||
4654 | 27 | RAPIDJSON_NAMESPACE_BEGIN | ||
4655 | 28 | |||
4656 | 29 | //! File byte stream for input using fread(). | ||
4657 | 30 | /*! | ||
4658 | 31 | \note implements Stream concept | ||
4659 | 32 | */ | ||
4660 | 33 | class FileReadStream { | ||
4661 | 34 | public: | ||
4662 | 35 | typedef char Ch; //!< Character type (byte). | ||
4663 | 36 | |||
4664 | 37 | //! Constructor. | ||
4665 | 38 | /*! | ||
4666 | 39 | \param fp File pointer opened for read. | ||
4667 | 40 | \param buffer user-supplied buffer. | ||
4668 | 41 | \param bufferSize size of buffer in bytes. Must >=4 bytes. | ||
4669 | 42 | */ | ||
4670 | 43 | FileReadStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { | ||
4671 | 44 | RAPIDJSON_ASSERT(fp_ != 0); | ||
4672 | 45 | RAPIDJSON_ASSERT(bufferSize >= 4); | ||
4673 | 46 | Read(); | ||
4674 | 47 | } | ||
4675 | 48 | |||
4676 | 49 | Ch Peek() const { return *current_; } | ||
4677 | 50 | Ch Take() { Ch c = *current_; Read(); return c; } | ||
4678 | 51 | size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); } | ||
4679 | 52 | |||
4680 | 53 | // Not implemented | ||
4681 | 54 | void Put(Ch) { RAPIDJSON_ASSERT(false); } | ||
4682 | 55 | void Flush() { RAPIDJSON_ASSERT(false); } | ||
4683 | 56 | Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } | ||
4684 | 57 | size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } | ||
4685 | 58 | |||
4686 | 59 | // For encoding detection only. | ||
4687 | 60 | const Ch* Peek4() const { | ||
4688 | 61 | return (current_ + 4 <= bufferLast_) ? current_ : 0; | ||
4689 | 62 | } | ||
4690 | 63 | |||
4691 | 64 | private: | ||
4692 | 65 | void Read() { | ||
4693 | 66 | if (current_ < bufferLast_) | ||
4694 | 67 | ++current_; | ||
4695 | 68 | else if (!eof_) { | ||
4696 | 69 | count_ += readCount_; | ||
4697 | 70 | readCount_ = fread(buffer_, 1, bufferSize_, fp_); | ||
4698 | 71 | bufferLast_ = buffer_ + readCount_ - 1; | ||
4699 | 72 | current_ = buffer_; | ||
4700 | 73 | |||
4701 | 74 | if (readCount_ < bufferSize_) { | ||
4702 | 75 | buffer_[readCount_] = '\0'; | ||
4703 | 76 | ++bufferLast_; | ||
4704 | 77 | eof_ = true; | ||
4705 | 78 | } | ||
4706 | 79 | } | ||
4707 | 80 | } | ||
4708 | 81 | |||
4709 | 82 | std::FILE* fp_; | ||
4710 | 83 | Ch *buffer_; | ||
4711 | 84 | size_t bufferSize_; | ||
4712 | 85 | Ch *bufferLast_; | ||
4713 | 86 | Ch *current_; | ||
4714 | 87 | size_t readCount_; | ||
4715 | 88 | size_t count_; //!< Number of characters read | ||
4716 | 89 | bool eof_; | ||
4717 | 90 | }; | ||
4718 | 91 | |||
4719 | 92 | RAPIDJSON_NAMESPACE_END | ||
4720 | 93 | |||
4721 | 94 | #endif // RAPIDJSON_FILESTREAM_H_ | ||
4722 | 0 | 95 | ||
4723 | === added file 'shorts/xml2json/rapidjson/filestream.h' | |||
4724 | --- shorts/xml2json/rapidjson/filestream.h 1970-01-01 00:00:00 +0000 | |||
4725 | +++ shorts/xml2json/rapidjson/filestream.h 2015-12-15 16:05:21 +0000 | |||
4726 | @@ -0,0 +1,73 @@ | |||
4727 | 1 | // Copyright (C) 2011 Milo Yip | ||
4728 | 2 | // | ||
4729 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
4730 | 4 | // of this software and associated documentation files (the "Software"), to deal | ||
4731 | 5 | // in the Software without restriction, including without limitation the rights | ||
4732 | 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
4733 | 7 | // copies of the Software, and to permit persons to whom the Software is | ||
4734 | 8 | // furnished to do so, subject to the following conditions: | ||
4735 | 9 | // | ||
4736 | 10 | // The above copyright notice and this permission notice shall be included in | ||
4737 | 11 | // all copies or substantial portions of the Software. | ||
4738 | 12 | // | ||
4739 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
4740 | 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
4741 | 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
4742 | 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
4743 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
4744 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
4745 | 19 | // THE SOFTWARE. | ||
4746 | 20 | |||
4747 | 21 | #ifndef RAPIDJSON_FILESTREAM_H_ | ||
4748 | 22 | #define RAPIDJSON_FILESTREAM_H_ | ||
4749 | 23 | |||
4750 | 24 | #include "rapidjson.h" | ||
4751 | 25 | #include <cstdio> | ||
4752 | 26 | |||
4753 | 27 | RAPIDJSON_NAMESPACE_BEGIN | ||
4754 | 28 | |||
4755 | 29 | //! (Deprecated) Wrapper of C file stream for input or output. | ||
4756 | 30 | /*! | ||
4757 | 31 | This simple wrapper does not check the validity of the stream. | ||
4758 | 32 | \note implements Stream concept | ||
4759 | 33 | \note deprecated: This was only for basic testing in version 0.1, it is found that the performance is very low by using fgetc(). Use FileReadStream instead. | ||
4760 | 34 | */ | ||
4761 | 35 | class FileStream { | ||
4762 | 36 | public: | ||
4763 | 37 | typedef char Ch; //!< Character type. Only support char. | ||
4764 | 38 | |||
4765 | 39 | FileStream(std::FILE* fp) : fp_(fp), current_('\0'), count_(0) { Read(); } | ||
4766 | 40 | char Peek() const { return current_; } | ||
4767 | 41 | char Take() { char c = current_; Read(); return c; } | ||
4768 | 42 | size_t Tell() const { return count_; } | ||
4769 | 43 | void Put(char c) { fputc(c, fp_); } | ||
4770 | 44 | void Flush() { fflush(fp_); } | ||
4771 | 45 | |||
4772 | 46 | // Not implemented | ||
4773 | 47 | char* PutBegin() { return 0; } | ||
4774 | 48 | size_t PutEnd(char*) { return 0; } | ||
4775 | 49 | |||
4776 | 50 | private: | ||
4777 | 51 | // Prohibit copy constructor & assignment operator. | ||
4778 | 52 | FileStream(const FileStream&); | ||
4779 | 53 | FileStream& operator=(const FileStream&); | ||
4780 | 54 | |||
4781 | 55 | void Read() { | ||
4782 | 56 | RAPIDJSON_ASSERT(fp_ != 0); | ||
4783 | 57 | int c = fgetc(fp_); | ||
4784 | 58 | if (c != EOF) { | ||
4785 | 59 | current_ = (char)c; | ||
4786 | 60 | count_++; | ||
4787 | 61 | } | ||
4788 | 62 | else if (current_ != '\0') | ||
4789 | 63 | current_ = '\0'; | ||
4790 | 64 | } | ||
4791 | 65 | |||
4792 | 66 | std::FILE* fp_; | ||
4793 | 67 | char current_; | ||
4794 | 68 | size_t count_; | ||
4795 | 69 | }; | ||
4796 | 70 | |||
4797 | 71 | RAPIDJSON_NAMESPACE_END | ||
4798 | 72 | |||
4799 | 73 | #endif // RAPIDJSON_FILESTREAM_H_ | ||
4800 | 0 | 74 | ||
4801 | === added file 'shorts/xml2json/rapidjson/filewritestream.h' | |||
4802 | --- shorts/xml2json/rapidjson/filewritestream.h 1970-01-01 00:00:00 +0000 | |||
4803 | +++ shorts/xml2json/rapidjson/filewritestream.h 2015-12-15 16:05:21 +0000 | |||
4804 | @@ -0,0 +1,97 @@ | |||
4805 | 1 | // Copyright (C) 2011 Milo Yip | ||
4806 | 2 | // | ||
4807 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
4808 | 4 | // of this software and associated documentation files (the "Software"), to deal | ||
4809 | 5 | // in the Software without restriction, including without limitation the rights | ||
4810 | 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
4811 | 7 | // copies of the Software, and to permit persons to whom the Software is | ||
4812 | 8 | // furnished to do so, subject to the following conditions: | ||
4813 | 9 | // | ||
4814 | 10 | // The above copyright notice and this permission notice shall be included in | ||
4815 | 11 | // all copies or substantial portions of the Software. | ||
4816 | 12 | // | ||
4817 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
4818 | 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
4819 | 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
4820 | 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
4821 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
4822 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
4823 | 19 | // THE SOFTWARE. | ||
4824 | 20 | |||
4825 | 21 | #ifndef RAPIDJSON_FILEWRITESTREAM_H_ | ||
4826 | 22 | #define RAPIDJSON_FILEWRITESTREAM_H_ | ||
4827 | 23 | |||
4828 | 24 | #include "rapidjson.h" | ||
4829 | 25 | #include <cstdio> | ||
4830 | 26 | |||
4831 | 27 | RAPIDJSON_NAMESPACE_BEGIN | ||
4832 | 28 | |||
4833 | 29 | //! Wrapper of C file stream for input using fread(). | ||
4834 | 30 | /*! | ||
4835 | 31 | \note implements Stream concept | ||
4836 | 32 | */ | ||
4837 | 33 | class FileWriteStream { | ||
4838 | 34 | public: | ||
4839 | 35 | typedef char Ch; //!< Character type. Only support char. | ||
4840 | 36 | |||
4841 | 37 | FileWriteStream(std::FILE* fp, char* buffer, size_t bufferSize) : fp_(fp), buffer_(buffer), bufferEnd_(buffer + bufferSize), current_(buffer_) { | ||
4842 | 38 | RAPIDJSON_ASSERT(fp_ != 0); | ||
4843 | 39 | } | ||
4844 | 40 | |||
4845 | 41 | void Put(char c) { | ||
4846 | 42 | if (current_ >= bufferEnd_) | ||
4847 | 43 | Flush(); | ||
4848 | 44 | |||
4849 | 45 | *current_++ = c; | ||
4850 | 46 | } | ||
4851 | 47 | |||
4852 | 48 | void PutN(char c, size_t n) { | ||
4853 | 49 | size_t avail = static_cast<size_t>(bufferEnd_ - current_); | ||
4854 | 50 | while (n > avail) { | ||
4855 | 51 | std::memset(current_, c, avail); | ||
4856 | 52 | current_ += avail; | ||
4857 | 53 | Flush(); | ||
4858 | 54 | n -= avail; | ||
4859 | 55 | avail = static_cast<size_t>(bufferEnd_ - current_); | ||
4860 | 56 | } | ||
4861 | 57 | |||
4862 | 58 | if (n > 0) { | ||
4863 | 59 | std::memset(current_, c, n); | ||
4864 | 60 | current_ += n; | ||
4865 | 61 | } | ||
4866 | 62 | } | ||
4867 | 63 | |||
4868 | 64 | void Flush() { | ||
4869 | 65 | if (current_ != buffer_) { | ||
4870 | 66 | fwrite(buffer_, 1, static_cast<size_t>(current_ - buffer_), fp_); | ||
4871 | 67 | current_ = buffer_; | ||
4872 | 68 | } | ||
4873 | 69 | } | ||
4874 | 70 | |||
4875 | 71 | // Not implemented | ||
4876 | 72 | char Peek() const { RAPIDJSON_ASSERT(false); return 0; } | ||
4877 | 73 | char Take() { RAPIDJSON_ASSERT(false); return 0; } | ||
4878 | 74 | size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; } | ||
4879 | 75 | char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } | ||
4880 | 76 | size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; } | ||
4881 | 77 | |||
4882 | 78 | private: | ||
4883 | 79 | // Prohibit copy constructor & assignment operator. | ||
4884 | 80 | FileWriteStream(const FileWriteStream&); | ||
4885 | 81 | FileWriteStream& operator=(const FileWriteStream&); | ||
4886 | 82 | |||
4887 | 83 | std::FILE* fp_; | ||
4888 | 84 | char *buffer_; | ||
4889 | 85 | char *bufferEnd_; | ||
4890 | 86 | char *current_; | ||
4891 | 87 | }; | ||
4892 | 88 | |||
4893 | 89 | //! Implement specialized version of PutN() with memset() for better performance. | ||
4894 | 90 | template<> | ||
4895 | 91 | inline void PutN(FileWriteStream& stream, char c, size_t n) { | ||
4896 | 92 | stream.PutN(c, n); | ||
4897 | 93 | } | ||
4898 | 94 | |||
4899 | 95 | RAPIDJSON_NAMESPACE_END | ||
4900 | 96 | |||
4901 | 97 | #endif // RAPIDJSON_FILESTREAM_H_ | ||
4902 | 0 | 98 | ||
4903 | === added directory 'shorts/xml2json/rapidjson/internal' | |||
4904 | === added file 'shorts/xml2json/rapidjson/internal/biginteger.h' | |||
4905 | --- shorts/xml2json/rapidjson/internal/biginteger.h 1970-01-01 00:00:00 +0000 | |||
4906 | +++ shorts/xml2json/rapidjson/internal/biginteger.h 2015-12-15 16:05:21 +0000 | |||
4907 | @@ -0,0 +1,294 @@ | |||
4908 | 1 | // Copyright (C) 2011 Milo Yip | ||
4909 | 2 | // | ||
4910 | 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy | ||
4911 | 4 | // of this software and associated documentation files (the "Software"), to deal | ||
4912 | 5 | // in the Software without restriction, including without limitation the rights | ||
4913 | 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
4914 | 7 | // copies of the Software, and to permit persons to whom the Software is | ||
4915 | 8 | // furnished to do so, subject to the following conditions: | ||
4916 | 9 | // | ||
4917 | 10 | // The above copyright notice and this permission notice shall be included in | ||
4918 | 11 | // all copies or substantial portions of the Software. | ||
4919 | 12 | // | ||
4920 | 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
4921 | 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
4922 | 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
4923 | 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
4924 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
4925 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
4926 | 19 | // THE SOFTWARE. | ||
4927 | 20 | |||
4928 | 21 | #ifndef RAPIDJSON_BIGINTEGER_H_ | ||
4929 | 22 | #define RAPIDJSON_BIGINTEGER_H_ | ||
4930 | 23 | |||
4931 | 24 | #include "../rapidjson.h" | ||
4932 | 25 | |||
4933 | 26 | #if defined(_MSC_VER) && defined(_M_AMD64) | ||
4934 | 27 | #include <intrin.h> // for _umul128 | ||
4935 | 28 | #endif | ||
4936 | 29 | |||
4937 | 30 | RAPIDJSON_NAMESPACE_BEGIN | ||
4938 | 31 | namespace internal { | ||
4939 | 32 | |||
4940 | 33 | class BigInteger { | ||
4941 | 34 | public: | ||
4942 | 35 | typedef uint64_t Type; | ||
4943 | 36 | |||
4944 | 37 | BigInteger(const BigInteger& rhs) : count_(rhs.count_) { | ||
4945 | 38 | std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type)); | ||
4946 | 39 | } | ||
4947 | 40 | |||
4948 | 41 | explicit BigInteger(uint64_t u) : count_(1) { | ||
4949 | 42 | digits_[0] = u; | ||
4950 | 43 | } | ||
4951 | 44 | |||
4952 | 45 | BigInteger(const char* decimals, size_t length) : count_(1) { | ||
4953 | 46 | RAPIDJSON_ASSERT(length > 0); | ||
4954 | 47 | digits_[0] = 0; | ||
4955 | 48 | size_t i = 0; | ||
4956 | 49 | const size_t kMaxDigitPerIteration = 19; // 2^64 = 18446744073709551616 > 10^19 | ||
4957 | 50 | while (length >= kMaxDigitPerIteration) { | ||
4958 | 51 | AppendDecimal64(decimals + i, decimals + i + kMaxDigitPerIteration); | ||
4959 | 52 | length -= kMaxDigitPerIteration; | ||
4960 | 53 | i += kMaxDigitPerIteration; | ||
4961 | 54 | } | ||
4962 | 55 | |||
4963 | 56 | if (length > 0) | ||
4964 | 57 | AppendDecimal64(decimals + i, decimals + i + length); | ||
4965 | 58 | } | ||
4966 | 59 | |||
4967 | 60 | BigInteger& operator=(uint64_t u) { | ||
4968 | 61 | digits_[0] = u; | ||
4969 | 62 | count_ = 1; | ||
4970 | 63 | return *this; | ||
4971 | 64 | } | ||
4972 | 65 | |||
4973 | 66 | BigInteger& operator+=(uint64_t u) { | ||
4974 | 67 | Type backup = digits_[0]; | ||
4975 | 68 | digits_[0] += u; | ||
4976 | 69 | for (size_t i = 0; i < count_ - 1; i++) { | ||
4977 | 70 | if (digits_[i] >= backup) | ||
4978 | 71 | return *this; // no carry | ||
4979 | 72 | backup = digits_[i + 1]; | ||
4980 | 73 | digits_[i + 1] += 1; | ||
4981 | 74 | } | ||
4982 | 75 | |||
4983 | 76 | // Last carry | ||
4984 | 77 | if (digits_[count_ - 1] < backup) | ||
4985 | 78 | PushBack(1); | ||
4986 | 79 | |||
4987 | 80 | return *this; | ||
4988 | 81 | } | ||
4989 | 82 | |||
4990 | 83 | BigInteger& operator*=(uint64_t u) { | ||
4991 | 84 | if (u == 0) return *this = 0; | ||
4992 | 85 | if (u == 1) return *this; | ||
4993 | 86 | if (*this == 1) return *this = u; | ||
4994 | 87 | |||
4995 | 88 | uint64_t k = 0; | ||
4996 | 89 | for (size_t i = 0; i < count_; i++) { | ||
4997 | 90 | uint64_t hi; | ||
4998 | 91 | digits_[i] = MulAdd64(digits_[i], u, k, &hi); | ||
4999 | 92 | k = hi; | ||
5000 | 93 | } |
The diff has been truncated for viewing.
PASSED: Continuous integration, rev:417 /core-apps- jenkins. ubuntu. com/job/ shorts- app-ci/ 10/ /core-apps- jenkins. ubuntu. com/job/ generic- update- mp/258/ console
https:/
Executed test runs:
None: https:/
Click here to trigger a rebuild: /core-apps- jenkins. ubuntu. com/job/ shorts- app-ci/ 10/rebuild
https:/