| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
| 2 | /* librevenge |
| 3 | * Version: MPL 2.0 / LGPLv2.1+ |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | * |
| 9 | * Major Contributor(s): |
| 10 | * Copyright (C) 2004 William Lachance ([email protected]) |
| 11 | * Copyright (C) 2005 Net Integration Technologies (http://www.net-itech.com) |
| 12 | * Copyright (C) 2006 Fridrich Strba ([email protected]) |
| 13 | * |
| 14 | * For minor contributions see the git repository. |
| 15 | * |
| 16 | * Alternatively, the contents of this file may be used under the terms |
| 17 | * of the GNU Lesser General Public License Version 2.1 or later |
| 18 | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
| 19 | * applicable instead of those above. |
| 20 | */ |
| 21 | |
| 22 | #include <librevenge/librevenge.h> |
| 23 | |
| 24 | #include <map> |
| 25 | #include <memory> |
| 26 | #include <string> |
| 27 | #include <utility> |
| 28 | |
| 29 | #include <boost/spirit/include/qi.hpp> |
| 30 | |
| 31 | namespace |
| 32 | { |
| 33 | |
| 34 | bool findDouble(const librevenge::RVNGString &str, double &res, librevenge::RVNGUnit &unit) |
| 35 | { |
| 36 | if (str.empty()) |
| 37 | return false; |
| 38 | |
| 39 | const char *first = str.cstr(); |
| 40 | const char *const last = first + str.size(); |
| 41 | |
| 42 | using namespace boost::spirit::qi; |
| 43 | using namespace librevenge; |
| 44 | double ratio = 1; |
| 45 | symbols<char, RVNGUnit> simpleUnit; |
| 46 | simpleUnit.add("pt", RVNG_POINT)("in", RVNG_INCH)("*", RVNG_TWIP); |
| 47 | if (phrase_parse(first, last, |
| 48 | // Begin grammar |
| 49 | ( |
| 50 | double_ >> simpleUnit |
| 51 | | double_ >> "%" >> attr(RVNG_PERCENT) >> attr(100.0) |
| 52 | | double_ >> "cm" >> attr(RVNG_INCH) >> attr(2.54) |
| 53 | | double_ >> "mm" >> attr(RVNG_INCH) >> attr(25.4) |
| 54 | | double_ >> attr(RVNG_GENERIC) |
| 55 | ), |
| 56 | // End grammar |
| 57 | space, |
| 58 | res, unit, ratio)) |
| 59 | { |
| 60 | res /= ratio; |
| 61 | return first == last; |
| 62 | } |
| 63 | |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | bool findInt(const librevenge::RVNGString &str, int &res) |
| 68 | { |
| 69 | if (str.empty()) |
| 70 | return false; |
| 71 | |
| 72 | const char *first = str.cstr(); |
| 73 | const char *const last = first + str.size(); |
| 74 | using namespace boost::spirit::qi; |
| 75 | return phrase_parse(first, last, int_, space, res) && first == last; |
| 76 | } |
| 77 | |
| 78 | bool findBool(const librevenge::RVNGString &str, bool &res) |
| 79 | { |
| 80 | if (str.empty()) |
| 81 | return false; |
| 82 | |
| 83 | const char *first = str.cstr(); |
| 84 | const char *const last = first + str.size(); |
| 85 | using namespace boost::spirit::qi; |
| 86 | return phrase_parse(first, last, no_case[bool_], space, res) && first == last; |
| 87 | } |
| 88 | |
| 89 | } // anonymous namespace |
| 90 | |
| 91 | namespace librevenge |
| 92 | { |
| 93 | |
| 94 | class RVNGPropertyListElement |
| 95 | { |
| 96 | public: |
| 97 | RVNGPropertyListElement() : m_prop(nullptr), m_vec(nullptr) {} |
| 98 | RVNGPropertyListElement(const RVNGPropertyListElement &elem) |
| 99 | : m_prop(elem.m_prop ? elem.m_prop->clone() : nullptr), |
| 100 | m_vec(elem.m_vec ? static_cast<RVNGPropertyListVector *>(elem.m_vec->clone()) : nullptr) {} |
| 101 | /* |
| 102 | * Caution, following constructor does not allocate memory but takes as |
| 103 | * arguments pre-allocated memory that this class takes ownership of. |
| 104 | * Deallocating this memory outside this class can result in double free. |
| 105 | */ |
| 106 | RVNGPropertyListElement(RVNGProperty *prop, RVNGPropertyListVector *vec) |
| 107 | : m_prop(prop), m_vec(vec) {} |
| 108 | ~RVNGPropertyListElement() |
| 109 | { |
| 110 | } |
| 111 | RVNGPropertyListElement &operator=(const RVNGPropertyListElement &elem) |
| 112 | { |
| 113 | m_prop.reset(elem.m_prop ? elem.m_prop->clone() : nullptr); |
| 114 | m_vec.reset(elem.m_vec ? static_cast<RVNGPropertyListVector *>(elem.m_vec->clone()) : nullptr); |
| 115 | return *this; |
| 116 | } |
| 117 | std::unique_ptr<RVNGProperty> m_prop; |
| 118 | std::unique_ptr<RVNGPropertyListVector> m_vec; |
| 119 | }; |
| 120 | |
| 121 | class RVNGPropertyListImpl |
| 122 | { |
| 123 | public: |
| 124 | RVNGPropertyListImpl() : m_map() {} |
| 125 | RVNGPropertyListImpl(const RVNGPropertyListImpl &plist) : m_map(plist.m_map) {} |
| 126 | ~RVNGPropertyListImpl() {} |
| 127 | RVNGPropertyListImpl &operator=(const RVNGPropertyListImpl &plist); |
| 128 | void insert(const char *name, RVNGProperty *prop); |
| 129 | void insert(const char *name, RVNGPropertyListVector *vec); |
| 130 | const RVNGProperty *operator[](const char *name) const; |
| 131 | const RVNGPropertyListVector *child(const char *name) const; |
| 132 | void remove(const char *name); |
| 133 | void clear(); |
| 134 | bool empty() const; |
| 135 | |
| 136 | mutable std::map<std::string, RVNGPropertyListElement> m_map; |
| 137 | }; |
| 138 | |
| 139 | RVNGPropertyListImpl &RVNGPropertyListImpl::operator=(const RVNGPropertyListImpl &plist) |
| 140 | { |
| 141 | m_map = plist.m_map; |
| 142 | return *this; |
| 143 | } |
| 144 | |
| 145 | const RVNGProperty *RVNGPropertyListImpl::operator[](const char *name) const |
| 146 | { |
| 147 | auto i = m_map.find(name); |
| 148 | if (i != m_map.end()) |
| 149 | return i->second.m_prop.get(); |
| 150 | return nullptr; |
| 151 | } |
| 152 | |
| 153 | const RVNGPropertyListVector *RVNGPropertyListImpl::child(const char *name) const |
| 154 | { |
| 155 | auto i = m_map.find(name); |
| 156 | if (i != m_map.end()) |
| 157 | { |
| 158 | return i->second.m_vec.get(); |
| 159 | } |
| 160 | |
| 161 | return nullptr; |
| 162 | } |
| 163 | |
| 164 | void RVNGPropertyListImpl::insert(const char *name, RVNGProperty *prop) |
| 165 | { |
| 166 | auto i = m_map.lower_bound(name); |
| 167 | if (i != m_map.end() && !(m_map.key_comp()(name, i->first))) |
| 168 | { |
| 169 | i->second.m_vec = nullptr; |
| 170 | i->second.m_prop.reset(prop); |
| 171 | return; |
| 172 | } |
| 173 | m_map.insert(i, std::map<std::string, RVNGPropertyListElement>::value_type(name, RVNGPropertyListElement(prop, nullptr))); |
| 174 | } |
| 175 | |
| 176 | void RVNGPropertyListImpl::insert(const char *name, RVNGPropertyListVector *vec) |
| 177 | { |
| 178 | auto i = m_map.lower_bound(name); |
| 179 | if (i != m_map.end() && !(m_map.key_comp()(name, i->first))) |
| 180 | { |
| 181 | i->second.m_prop = nullptr; |
| 182 | i->second.m_vec.reset(vec); |
| 183 | return; |
| 184 | } |
| 185 | m_map.insert(i, std::map<std::string, RVNGPropertyListElement>::value_type(name, RVNGPropertyListElement(nullptr, vec))); |
| 186 | } |
| 187 | |
| 188 | void RVNGPropertyListImpl::remove(const char *name) |
| 189 | { |
| 190 | auto i = m_map.find(name); |
| 191 | if (i != m_map.end()) |
| 192 | { |
| 193 | m_map.erase(i); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void RVNGPropertyListImpl::clear() |
| 198 | { |
| 199 | m_map.clear(); |
| 200 | } |
| 201 | |
| 202 | bool RVNGPropertyListImpl::empty() const |
| 203 | { |
| 204 | return m_map.empty(); |
| 205 | } |
| 206 | |
| 207 | RVNGPropertyList::RVNGPropertyList() : |
| 208 | m_impl(new RVNGPropertyListImpl()) |
| 209 | { |
| 210 | } |
| 211 | |
| 212 | RVNGPropertyList::RVNGPropertyList(const RVNGPropertyList &propList) : |
| 213 | m_impl(new RVNGPropertyListImpl(*(propList.m_impl))) |
| 214 | { |
| 215 | } |
| 216 | |
| 217 | RVNGPropertyList::~RVNGPropertyList() |
| 218 | { |
| 219 | delete m_impl; |
| 220 | } |
| 221 | |
| 222 | void RVNGPropertyList::insert(const char *name, RVNGProperty *prop) |
| 223 | { |
| 224 | m_impl->insert(name, prop); |
| 225 | } |
| 226 | |
| 227 | void RVNGPropertyList::insert(const char *name, const int val) |
| 228 | { |
| 229 | m_impl->insert(name, RVNGPropertyFactory::newIntProp(val)); |
| 230 | } |
| 231 | |
| 232 | void RVNGPropertyList::insert(const char *name, const bool val) |
| 233 | { |
| 234 | m_impl->insert(name, RVNGPropertyFactory::newBoolProp(val)); |
| 235 | } |
| 236 | |
| 237 | void RVNGPropertyList::insert(const char *name, const char *val) |
| 238 | { |
| 239 | insert(name, RVNGString(val)); |
| 240 | } |
| 241 | |
| 242 | void RVNGPropertyList::insert(const char *name, const RVNGString &val) |
| 243 | { |
| 244 | int valInt = 0; |
| 245 | if (findInt(val, valInt)) |
| 246 | { |
| 247 | insert(name, valInt); |
| 248 | return; |
| 249 | } |
| 250 | double valDouble = 0.0; |
| 251 | RVNGUnit valUnit; |
| 252 | if (findDouble(val, valDouble, valUnit)) |
| 253 | { |
| 254 | insert(name, valDouble, valUnit); |
| 255 | return; |
| 256 | } |
| 257 | bool valBool = false; |
| 258 | if (findBool(val, valBool)) |
| 259 | { |
| 260 | insert(name, valBool); |
| 261 | return; |
| 262 | } |
| 263 | m_impl->insert(name, RVNGPropertyFactory::newStringProp(val)); |
| 264 | } |
| 265 | |
| 266 | void RVNGPropertyList::insert(const char *name, const unsigned char *buffer, const unsigned long bufferSize) |
| 267 | { |
| 268 | m_impl->insert(name, RVNGPropertyFactory::newBinaryDataProp(buffer, bufferSize)); |
| 269 | } |
| 270 | |
| 271 | void RVNGPropertyList::insert(const char *name, const RVNGBinaryData &data) |
| 272 | { |
| 273 | m_impl->insert(name, RVNGPropertyFactory::newBinaryDataProp(data)); |
| 274 | } |
| 275 | |
| 276 | void RVNGPropertyList::insert(const char *name, const double val, const RVNGUnit units) |
| 277 | { |
| 278 | if (units == RVNG_INCH) |
| 279 | m_impl->insert(name, RVNGPropertyFactory::newInchProp(val)); |
| 280 | else if (units == RVNG_PERCENT) |
| 281 | m_impl->insert(name, RVNGPropertyFactory::newPercentProp(val)); |
| 282 | else if (units == RVNG_POINT) |
| 283 | m_impl->insert(name, RVNGPropertyFactory::newPointProp(val)); |
| 284 | else if (units == RVNG_TWIP) |
| 285 | m_impl->insert(name, RVNGPropertyFactory::newTwipProp(val)); |
| 286 | else if (units == RVNG_GENERIC) |
| 287 | m_impl->insert(name, RVNGPropertyFactory::newDoubleProp(val)); |
| 288 | } |
| 289 | |
| 290 | void RVNGPropertyList::insert(const char *name, const RVNGPropertyListVector &vec) |
| 291 | { |
| 292 | m_impl->insert(name, static_cast<RVNGPropertyListVector *>(vec.clone())); |
| 293 | } |
| 294 | |
| 295 | void RVNGPropertyList::remove(const char *name) |
| 296 | { |
| 297 | m_impl->remove(name); |
| 298 | } |
| 299 | |
| 300 | const RVNGPropertyList &RVNGPropertyList::operator=(const RVNGPropertyList &propList) |
| 301 | { |
| 302 | RVNGPropertyList tmp(propList); |
| 303 | std::swap(m_impl, tmp.m_impl); |
| 304 | return *this; |
| 305 | } |
| 306 | |
| 307 | const RVNGProperty *RVNGPropertyList::operator[](const char *name) const |
| 308 | { |
| 309 | return (*m_impl)[name]; |
| 310 | } |
| 311 | |
| 312 | const RVNGPropertyListVector *RVNGPropertyList::child(const char *name) const |
| 313 | { |
| 314 | return m_impl->child(name); |
| 315 | } |
| 316 | |
| 317 | void RVNGPropertyList::clear() |
| 318 | { |
| 319 | m_impl->clear(); |
| 320 | } |
| 321 | |
| 322 | bool RVNGPropertyList::empty() const |
| 323 | { |
| 324 | return m_impl->empty(); |
| 325 | } |
| 326 | |
| 327 | |
| 328 | RVNGString RVNGPropertyList::getPropString() const |
| 329 | { |
| 330 | RVNGString propString; |
| 331 | RVNGPropertyList::Iter i(*this); |
| 332 | if (!i.last()) |
| 333 | { |
| 334 | propString.append(i.key()); |
| 335 | propString.append(": "); |
| 336 | if (i.child()) |
| 337 | propString.append(i.child()->getPropString().cstr()); |
| 338 | else |
| 339 | propString.append(i()->getStr().cstr()); |
| 340 | for (; i.next();) |
| 341 | { |
| 342 | propString.append(", "); |
| 343 | propString.append(i.key()); |
| 344 | propString.append(": "); |
| 345 | if (i.child()) |
| 346 | propString.append(i.child()->getPropString().cstr()); |
| 347 | else |
| 348 | propString.append(i()->getStr().cstr()); |
| 349 | } |
| 350 | } |
| 351 | return propString; |
| 352 | } |
| 353 | |
| 354 | #if 0 |
| 355 | void RVNGPropertyList::swap(RVNGPropertyList &other) |
| 356 | { |
| 357 | std::swap(m_impl, other.m_impl); |
| 358 | } |
| 359 | #endif |
| 360 | |
| 361 | class RVNGPropertyListIterImpl |
| 362 | { |
| 363 | public: |
| 364 | RVNGPropertyListIterImpl(const RVNGPropertyListImpl *impl); |
| 365 | void rewind(); |
| 366 | bool next(); |
| 367 | bool last(); |
| 368 | const RVNGProperty *operator()() const; |
| 369 | const RVNGPropertyListVector *child() const; |
| 370 | const char *key() const; |
| 371 | |
| 372 | private: |
| 373 | // not implemented |
| 374 | RVNGPropertyListIterImpl(const RVNGPropertyListIterImpl &other); |
| 375 | RVNGPropertyListIterImpl &operator=(const RVNGPropertyListIterImpl &other); |
| 376 | |
| 377 | private: |
| 378 | bool m_imaginaryFirst; |
| 379 | std::map<std::string, RVNGPropertyListElement>::iterator m_iter; |
| 380 | std::map<std::string, RVNGPropertyListElement> *m_map; |
| 381 | }; |
| 382 | |
| 383 | |
| 384 | RVNGPropertyListIterImpl::RVNGPropertyListIterImpl(const RVNGPropertyListImpl *impl) : |
| 385 | m_imaginaryFirst(false), |
| 386 | m_iter(impl->m_map.begin()), |
| 387 | m_map(&impl->m_map) |
| 388 | { |
| 389 | } |
| 390 | |
| 391 | void RVNGPropertyListIterImpl::rewind() |
| 392 | { |
| 393 | // rewind to an imaginary element that preceeds the first one |
| 394 | m_imaginaryFirst = true; |
| 395 | m_iter = m_map->begin(); |
| 396 | } |
| 397 | |
| 398 | bool RVNGPropertyListIterImpl::next() |
| 399 | { |
| 400 | if (!m_imaginaryFirst) |
| 401 | ++m_iter; |
| 402 | if (m_iter==m_map->end()) |
| 403 | return false; |
| 404 | m_imaginaryFirst = false; |
| 405 | |
| 406 | return true; |
| 407 | } |
| 408 | |
| 409 | bool RVNGPropertyListIterImpl::last() |
| 410 | { |
| 411 | return m_iter == m_map->end(); |
| 412 | } |
| 413 | |
| 414 | const RVNGProperty *RVNGPropertyListIterImpl::operator()() const |
| 415 | { |
| 416 | if (m_iter->second.m_prop) |
| 417 | return m_iter->second.m_prop.get(); |
| 418 | if (m_iter->second.m_vec) |
| 419 | return m_iter->second.m_vec.get(); |
| 420 | return nullptr; |
| 421 | } |
| 422 | |
| 423 | const RVNGPropertyListVector *RVNGPropertyListIterImpl::child() const |
| 424 | { |
| 425 | if (m_iter->second.m_vec) |
| 426 | return m_iter->second.m_vec.get(); |
| 427 | return nullptr; |
| 428 | } |
| 429 | |
| 430 | const char *RVNGPropertyListIterImpl::key() const |
| 431 | { |
| 432 | return m_iter->first.c_str(); |
| 433 | } |
| 434 | |
| 435 | RVNGPropertyList::Iter::Iter(const RVNGPropertyList &propList) : |
| 436 | m_iterImpl(new RVNGPropertyListIterImpl(propList.m_impl)) |
| 437 | { |
| 438 | } |
| 439 | |
| 440 | RVNGPropertyList::Iter::~Iter() |
| 441 | { |
| 442 | delete m_iterImpl; |
| 443 | } |
| 444 | |
| 445 | void RVNGPropertyList::Iter::rewind() |
| 446 | { |
| 447 | // rewind to an imaginary element that preceeds the first one |
| 448 | m_iterImpl->rewind(); |
| 449 | } |
| 450 | |
| 451 | bool RVNGPropertyList::Iter::next() |
| 452 | { |
| 453 | return m_iterImpl->next(); |
| 454 | } |
| 455 | |
| 456 | bool RVNGPropertyList::Iter::last() |
| 457 | { |
| 458 | return m_iterImpl->last(); |
| 459 | } |
| 460 | |
| 461 | const RVNGProperty *RVNGPropertyList::Iter::operator()() const |
| 462 | { |
| 463 | return (*m_iterImpl)(); |
| 464 | } |
| 465 | |
| 466 | const RVNGPropertyListVector *RVNGPropertyList::Iter::child() const |
| 467 | { |
| 468 | return m_iterImpl->child(); |
| 469 | } |
| 470 | |
| 471 | const char *RVNGPropertyList::Iter::key() const |
| 472 | { |
| 473 | return m_iterImpl->key(); |
| 474 | } |
| 475 | |
| 476 | } |
| 477 | |
| 478 | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |