| 1 | /** |
| 2 | * pugixml parser - version 1.4 |
| 3 | * -------------------------------------------------------- |
| 4 | * Copyright (C) 2006-2014, by Arseny Kapoulkine ([email protected]) |
| 5 | * Report bugs and download new versions at http://pugixml.org/ |
| 6 | * |
| 7 | * This library is distributed under the MIT License. See notice at the end |
| 8 | * of this file. |
| 9 | * |
| 10 | * This work is based on the pugxml parser, which is: |
| 11 | * Copyright (C) 2003, by Kristen Wegner ([email protected]) |
| 12 | */ |
| 13 | #ifndef SOURCE_PUGIXML_CPP |
| 14 | #define SOURCE_PUGIXML_CPP |
| 15 | |
| 16 | #include "pugixml.h" |
| 17 | |
| 18 | #include <stdlib.h> |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <assert.h> |
| 22 | |
| 23 | #ifdef PUGIXML_WCHAR_MODE |
| 24 | # include <wchar.h> |
| 25 | #endif |
| 26 | |
| 27 | #ifndef PUGIXML_NO_XPATH |
| 28 | # include <math.h> |
| 29 | # include <float.h> |
| 30 | # ifdef PUGIXML_NO_EXCEPTIONS |
| 31 | # include <setjmp.h> |
| 32 | # endif |
| 33 | #endif |
| 34 | |
| 35 | #ifndef PUGIXML_NO_STL |
| 36 | # include <istream> |
| 37 | # include <ostream> |
| 38 | # include <string> |
| 39 | #endif |
| 40 | |
| 41 | // For placement new |
| 42 | #include <new> |
| 43 | |
| 44 | #ifdef _MSC_VER |
| 45 | # pragma warning(push) |
| 46 | # pragma warning(disable: 4127) // conditional expression is constant |
| 47 | # pragma warning(disable: 4324) // structure was padded due to __declspec(align()) |
| 48 | # pragma warning(disable: 4611) // interaction between '_setjmp' and C++ object destruction is non-portable |
| 49 | # pragma warning(disable: 4702) // unreachable code |
| 50 | # pragma warning(disable: 4996) // this function or variable may be unsafe |
| 51 | # pragma warning(disable: 4793) // function compiled as native: presence of '_setjmp' makes a function unmanaged |
| 52 | #endif |
| 53 | |
| 54 | #ifdef __INTEL_COMPILER |
| 55 | # pragma warning(disable: 177) // function was declared but never referenced |
| 56 | # pragma warning(disable: 279) // controlling expression is constant |
| 57 | # pragma warning(disable: 1478 1786) // function was declared "deprecated" |
| 58 | # pragma warning(disable: 1684) // conversion from pointer to same-sized integral type |
| 59 | #endif |
| 60 | |
| 61 | #if defined(__BORLANDC__) && defined(PUGIXML_HEADER_ONLY) |
| 62 | # pragma warn -8080 // symbol is declared but never used; disabling this inside push/pop bracket does not make the warning go away |
| 63 | #endif |
| 64 | |
| 65 | #ifdef __BORLANDC__ |
| 66 | # pragma option push |
| 67 | # pragma warn -8008 // condition is always false |
| 68 | # pragma warn -8066 // unreachable code |
| 69 | #endif |
| 70 | |
| 71 | #ifdef __SNC__ |
| 72 | // Using diag_push/diag_pop does not disable the warnings inside templates due to a compiler bug |
| 73 | # pragma diag_suppress=178 // function was declared but never referenced |
| 74 | # pragma diag_suppress=237 // controlling expression is constant |
| 75 | #endif |
| 76 | |
| 77 | // Inlining controls |
| 78 | #if defined(_MSC_VER) && _MSC_VER >= 1300 |
| 79 | # define PUGI__NO_INLINE __declspec(noinline) |
| 80 | #elif defined(__GNUC__) |
| 81 | # define PUGI__NO_INLINE __attribute__((noinline)) |
| 82 | #else |
| 83 | # define PUGI__NO_INLINE |
| 84 | #endif |
| 85 | |
| 86 | // Simple static assertion |
| 87 | #define PUGI__STATIC_ASSERT(cond) { static const char condition_failed[(cond) ? 1 : -1] = {0}; (void)condition_failed[0]; } |
| 88 | |
| 89 | // Digital Mars C++ bug workaround for passing char loaded from memory via stack |
| 90 | #ifdef __DMC__ |
| 91 | # define PUGI__DMC_VOLATILE volatile |
| 92 | #else |
| 93 | # define PUGI__DMC_VOLATILE |
| 94 | #endif |
| 95 | |
| 96 | // Borland C++ bug workaround for not defining ::memcpy depending on header include order (can't always use std::memcpy because some compilers don't have it at all) |
| 97 | #if defined(__BORLANDC__) && !defined(__MEM_H_USING_LIST) |
| 98 | using std::memcpy; |
| 99 | using std::memmove; |
| 100 | #endif |
| 101 | |
| 102 | // In some environments MSVC is a compiler but the CRT lacks certain MSVC-specific features |
| 103 | #if defined(_MSC_VER) && !defined(__S3E__) |
| 104 | # define PUGI__MSVC_CRT_VERSION _MSC_VER |
| 105 | #endif |
| 106 | |
| 107 | #ifdef PUGIXML_HEADER_ONLY |
| 108 | # define PUGI__NS_BEGIN namespace pugi { namespace impl { |
| 109 | # define PUGI__NS_END } } |
| 110 | # define PUGI__FN inline |
| 111 | # define PUGI__FN_NO_INLINE inline |
| 112 | #else |
| 113 | # if defined(_MSC_VER) && _MSC_VER < 1300 // MSVC6 seems to have an amusing bug with anonymous namespaces inside namespaces |
| 114 | # define PUGI__NS_BEGIN namespace pugi { namespace impl { |
| 115 | # define PUGI__NS_END } } |
| 116 | # else |
| 117 | # define PUGI__NS_BEGIN namespace pugi { namespace impl { namespace { |
| 118 | # define PUGI__NS_END } } } |
| 119 | # endif |
| 120 | # define PUGI__FN |
| 121 | # define PUGI__FN_NO_INLINE PUGI__NO_INLINE |
| 122 | #endif |
| 123 | |
| 124 | // uintptr_t |
| 125 | #if !defined(_MSC_VER) || _MSC_VER >= 1600 |
| 126 | # include <stdint.h> |
| 127 | #else |
| 128 | # ifndef _UINTPTR_T_DEFINED |
| 129 | // No native uintptr_t in MSVC6 and in some WinCE versions |
| 130 | typedef size_t uintptr_t; |
| 131 | #define _UINTPTR_T_DEFINED |
| 132 | # endif |
| 133 | PUGI__NS_BEGIN |
| 134 | typedef unsigned __int8 uint8_t; |
| 135 | typedef unsigned __int16 uint16_t; |
| 136 | typedef unsigned __int32 uint32_t; |
| 137 | PUGI__NS_END |
| 138 | #endif |
| 139 | |
| 140 | // Memory allocation |
| 141 | PUGI__NS_BEGIN |
| 142 | PUGI__FN void* default_allocate(size_t size) |
| 143 | { |
| 144 | return malloc(size); |
| 145 | } |
| 146 | |
| 147 | PUGI__FN void default_deallocate(void* ptr) |
| 148 | { |
| 149 | free(ptr); |
| 150 | } |
| 151 | |
| 152 | template <typename T> |
| 153 | struct xml_memory_management_function_storage |
| 154 | { |
| 155 | static allocation_function allocate; |
| 156 | static deallocation_function deallocate; |
| 157 | }; |
| 158 | |
| 159 | template <typename T> allocation_function xml_memory_management_function_storage<T>::allocate = default_allocate; |
| 160 | template <typename T> deallocation_function xml_memory_management_function_storage<T>::deallocate = default_deallocate; |
| 161 | |
| 162 | typedef xml_memory_management_function_storage<int> xml_memory; |
| 163 | PUGI__NS_END |
| 164 | |
| 165 | // String utilities |
| 166 | PUGI__NS_BEGIN |
| 167 | // Get string length |
| 168 | PUGI__FN size_t strlength(const char_t* s) |
| 169 | { |
| 170 | assert(s); |
| 171 | |
| 172 | #ifdef PUGIXML_WCHAR_MODE |
| 173 | return wcslen(s); |
| 174 | #else |
| 175 | return strlen(s); |
| 176 | #endif |
| 177 | } |
| 178 | |
| 179 | // Compare two strings |
| 180 | PUGI__FN bool strequal(const char_t* src, const char_t* dst) |
| 181 | { |
| 182 | assert(src && dst); |
| 183 | |
| 184 | #ifdef PUGIXML_WCHAR_MODE |
| 185 | return wcscmp(src, dst) == 0; |
| 186 | #else |
| 187 | return strcmp(src, dst) == 0; |
| 188 | #endif |
| 189 | } |
| 190 | |
| 191 | // Compare lhs with [rhs_begin, rhs_end) |
| 192 | PUGI__FN bool strequalrange(const char_t* lhs, const char_t* rhs, size_t count) |
| 193 | { |
| 194 | for (size_t i = 0; i < count; ++i) |
| 195 | if (lhs[i] != rhs[i]) |
| 196 | return false; |
| 197 | |
| 198 | return lhs[count] == 0; |
| 199 | } |
| 200 | |
| 201 | // Get length of wide string, even if CRT lacks wide character support |
| 202 | PUGI__FN size_t strlength_wide(const wchar_t* s) |
| 203 | { |
| 204 | assert(s); |
| 205 | |
| 206 | #ifdef PUGIXML_WCHAR_MODE |
| 207 | return wcslen(s); |
| 208 | #else |
| 209 | const wchar_t* end = s; |
| 210 | while (*end) end++; |
| 211 | return static_cast<size_t>(end - s); |
| 212 | #endif |
| 213 | } |
| 214 | |
| 215 | #ifdef PUGIXML_WCHAR_MODE |
| 216 | // Convert string to wide string, assuming all symbols are ASCII |
| 217 | PUGI__FN void widen_ascii(wchar_t* dest, const char* source) |
| 218 | { |
| 219 | for (const char* i = source; *i; ++i) *dest++ = *i; |
| 220 | *dest = 0; |
| 221 | } |
| 222 | #endif |
| 223 | PUGI__NS_END |
| 224 | |
| 225 | #if !defined(PUGIXML_NO_STL) || !defined(PUGIXML_NO_XPATH) |
| 226 | // auto_ptr-like buffer holder for exception recovery |
| 227 | PUGI__NS_BEGIN |
| 228 | struct buffer_holder |
| 229 | { |
| 230 | void* data; |
| 231 | void (*deleter)(void*); |
| 232 | |
| 233 | buffer_holder(void* data_, void (*deleter_)(void*)): data(data_), deleter(deleter_) |
| 234 | { |
| 235 | } |
| 236 | |
| 237 | ~buffer_holder() |
| 238 | { |
| 239 | if (data) deleter(data); |
| 240 | } |
| 241 | |
| 242 | void* release() |
| 243 | { |
| 244 | void* result = data; |
| 245 | data = 0; |
| 246 | return result; |
| 247 | } |
| 248 | }; |
| 249 | PUGI__NS_END |
| 250 | #endif |
| 251 | |
| 252 | PUGI__NS_BEGIN |
| 253 | static const size_t xml_memory_page_size = |
| 254 | #ifdef PUGIXML_MEMORY_PAGE_SIZE |
| 255 | PUGIXML_MEMORY_PAGE_SIZE |
| 256 | #else |
| 257 | 32768 |
| 258 | #endif |
| 259 | ; |
| 260 | |
| 261 | static const uintptr_t xml_memory_page_alignment = 32; |
| 262 | static const uintptr_t xml_memory_page_pointer_mask = ~(xml_memory_page_alignment - 1); |
| 263 | static const uintptr_t xml_memory_page_name_allocated_mask = 16; |
| 264 | static const uintptr_t xml_memory_page_value_allocated_mask = 8; |
| 265 | static const uintptr_t xml_memory_page_type_mask = 7; |
| 266 | |
| 267 | struct xml_allocator; |
| 268 | |
| 269 | struct xml_memory_page |
| 270 | { |
| 271 | static xml_memory_page* construct(void* memory) |
| 272 | { |
| 273 | if (!memory) return 0; //$ redundant, left for performance |
| 274 | |
| 275 | xml_memory_page* result = static_cast<xml_memory_page*>(memory); |
| 276 | |
| 277 | result->allocator = 0; |
| 278 | result->memory = 0; |
| 279 | result->prev = 0; |
| 280 | result->next = 0; |
| 281 | result->busy_size = 0; |
| 282 | result->freed_size = 0; |
| 283 | |
| 284 | return result; |
| 285 | } |
| 286 | |
| 287 | xml_allocator* allocator; |
| 288 | |
| 289 | void* memory; |
| 290 | |
| 291 | xml_memory_page* prev; |
| 292 | xml_memory_page* next; |
| 293 | |
| 294 | size_t busy_size; |
| 295 | size_t freed_size; |
| 296 | |
| 297 | char data[1]; |
| 298 | }; |
| 299 | |
| 300 | struct xml_memory_string_header |
| 301 | { |
| 302 | uint16_t page_offset; // offset from page->data |
| 303 | uint16_t full_size; // 0 if string occupies whole page |
| 304 | }; |
| 305 | |
| 306 | struct xml_allocator |
| 307 | { |
| 308 | xml_allocator(xml_memory_page* root): _root(root), _busy_size(root->busy_size) |
| 309 | { |
| 310 | } |
| 311 | |
| 312 | xml_memory_page* allocate_page(size_t data_size) |
| 313 | { |
| 314 | size_t size = offsetof(xml_memory_page, data) + data_size; |
| 315 | |
| 316 | // allocate block with some alignment, leaving memory for worst-case padding |
| 317 | void* memory = xml_memory::allocate(size + xml_memory_page_alignment); |
| 318 | if (!memory) return 0; |
| 319 | |
| 320 | // align upwards to page boundary |
| 321 | void* page_memory = reinterpret_cast<void*>((reinterpret_cast<uintptr_t>(memory) + (xml_memory_page_alignment - 1)) & ~(xml_memory_page_alignment - 1)); |
| 322 | |
| 323 | // prepare page structure |
| 324 | xml_memory_page* page = xml_memory_page::construct(page_memory); |
| 325 | assert(page); |
| 326 | |
| 327 | page->memory = memory; |
| 328 | page->allocator = _root->allocator; |
| 329 | |
| 330 | return page; |
| 331 | } |
| 332 | |
| 333 | static void deallocate_page(xml_memory_page* page) |
| 334 | { |
| 335 | xml_memory::deallocate(page->memory); |
| 336 | } |
| 337 | |
| 338 | void* allocate_memory_oob(size_t size, xml_memory_page*& out_page); |
| 339 | |
| 340 | void* allocate_memory(size_t size, xml_memory_page*& out_page) |
| 341 | { |
| 342 | if (_busy_size + size > xml_memory_page_size) return allocate_memory_oob(size, out_page); |
| 343 | |
| 344 | void* buf = _root->data + _busy_size; |
| 345 | |
| 346 | _busy_size += size; |
| 347 | |
| 348 | out_page = _root; |
| 349 | |
| 350 | return buf; |
| 351 | } |
| 352 | |
| 353 | void deallocate_memory(void* ptr, size_t size, xml_memory_page* page) |
| 354 | { |
| 355 | if (page == _root) page->busy_size = _busy_size; |
| 356 | |
| 357 | assert(ptr >= page->data && ptr < page->data + page->busy_size); |
| 358 | (void)!ptr; |
| 359 | |
| 360 | page->freed_size += size; |
| 361 | assert(page->freed_size <= page->busy_size); |
| 362 | |
| 363 | if (page->freed_size == page->busy_size) |
| 364 | { |
| 365 | if (page->next == 0) |
| 366 | { |
| 367 | assert(_root == page); |
| 368 | |
| 369 | // top page freed, just reset sizes |
| 370 | page->busy_size = page->freed_size = 0; |
| 371 | _busy_size = 0; |
| 372 | } |
| 373 | else |
| 374 | { |
| 375 | assert(_root != page); |
| 376 | assert(page->prev); |
| 377 | |
| 378 | // remove from the list |
| 379 | page->prev->next = page->next; |
| 380 | page->next->prev = page->prev; |
| 381 | |
| 382 | // deallocate |
| 383 | deallocate_page(page); |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | char_t* allocate_string(size_t length) |
| 389 | { |
| 390 | // allocate memory for string and header block |
| 391 | size_t size = sizeof(xml_memory_string_header) + length * sizeof(char_t); |
| 392 | |
| 393 | // round size up to pointer alignment boundary |
| 394 | size_t full_size = (size + (sizeof(void*) - 1)) & ~(sizeof(void*) - 1); |
| 395 | |
| 396 | xml_memory_page* page; |
| 397 | xml_memory_string_header* header = static_cast<xml_memory_string_header*>(allocate_memory(full_size, page)); |
| 398 | |
| 399 | if (!header) return 0; |
| 400 | |
| 401 | // setup header |
| 402 | ptrdiff_t page_offset = reinterpret_cast<char*>(header) - page->data; |
| 403 | |
| 404 | assert(page_offset >= 0 && page_offset < (1 << 16)); |
| 405 | header->page_offset = static_cast<uint16_t>(page_offset); |
| 406 | |
| 407 | // full_size == 0 for large strings that occupy the whole page |
| 408 | assert(full_size < (1 << 16) || (page->busy_size == full_size && page_offset == 0)); |
| 409 | header->full_size = static_cast<uint16_t>(full_size < (1 << 16) ? full_size : 0); |
| 410 | |
| 411 | // round-trip through void* to avoid 'cast increases required alignment of target type' warning |
| 412 | // header is guaranteed a pointer-sized alignment, which should be enough for char_t |
| 413 | return static_cast<char_t*>(static_cast<void*>(header + 1)); |
| 414 | } |
| 415 | |
| 416 | void deallocate_string(char_t* string) |
| 417 | { |
| 418 | // this function casts pointers through void* to avoid 'cast increases required alignment of target type' warnings |
| 419 | // we're guaranteed the proper (pointer-sized) alignment on the input string if it was allocated via allocate_string |
| 420 | |
| 421 | // get header |
| 422 | xml_memory_string_header* header = static_cast<xml_memory_string_header*>(static_cast<void*>(string)) - 1; |
| 423 | |
| 424 | // deallocate |
| 425 | size_t page_offset = offsetof(xml_memory_page, data) + header->page_offset; |
| 426 | xml_memory_page* page = reinterpret_cast<xml_memory_page*>(static_cast<void*>(reinterpret_cast<char*>(header) - page_offset)); |
| 427 | |
| 428 | // if full_size == 0 then this string occupies the whole page |
| 429 | size_t full_size = header->full_size == 0 ? page->busy_size : header->full_size; |
| 430 | |
| 431 | deallocate_memory(header, full_size, page); |
| 432 | } |
| 433 | |
| 434 | xml_memory_page* _root; |
| 435 | size_t _busy_size; |
| 436 | }; |
| 437 | |
| 438 | PUGI__FN_NO_INLINE void* xml_allocator::allocate_memory_oob(size_t size, xml_memory_page*& out_page) |
| 439 | { |
| 440 | const size_t large_allocation_threshold = xml_memory_page_size / 4; |
| 441 | |
| 442 | xml_memory_page* page = allocate_page(size <= large_allocation_threshold ? xml_memory_page_size : size); |
| 443 | out_page = page; |
| 444 | |
| 445 | if (!page) return 0; |
| 446 | |
| 447 | if (size <= large_allocation_threshold) |
| 448 | { |
| 449 | _root->busy_size = _busy_size; |
| 450 | |
| 451 | // insert page at the end of linked list |
| 452 | page->prev = _root; |
| 453 | _root->next = page; |
| 454 | _root = page; |
| 455 | |
| 456 | _busy_size = size; |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | // insert page before the end of linked list, so that it is deleted as soon as possible |
| 461 | // the last page is not deleted even if it's empty (see deallocate_memory) |
| 462 | assert(_root->prev); |
| 463 | |
| 464 | page->prev = _root->prev; |
| 465 | page->next = _root; |
| 466 | |
| 467 | _root->prev->next = page; |
| 468 | _root->prev = page; |
| 469 | } |
| 470 | |
| 471 | // allocate inside page |
| 472 | page->busy_size = size; |
| 473 | |
| 474 | return page->data; |
| 475 | } |
| 476 | PUGI__NS_END |
| 477 | |
| 478 | namespace pugi |
| 479 | { |
| 480 | /// A 'name=value' XML attribute structure. |
| 481 | struct xml_attribute_struct |
| 482 | { |
| 483 | /// Default ctor |
| 484 | xml_attribute_struct(impl::xml_memory_page* page): header(reinterpret_cast<uintptr_t>(page)), name(0), value(0), prev_attribute_c(0), next_attribute(0) |
| 485 | { |
| 486 | } |
| 487 | |
| 488 | uintptr_t header; |
| 489 | |
| 490 | char_t* name; ///< Pointer to attribute name. |
| 491 | char_t* value; ///< Pointer to attribute value. |
| 492 | |
| 493 | xml_attribute_struct* prev_attribute_c; ///< Previous attribute (cyclic list) |
| 494 | xml_attribute_struct* next_attribute; ///< Next attribute |
| 495 | }; |
| 496 | |
| 497 | /// An XML document tree node. |
| 498 | struct xml_node_struct |
| 499 | { |
| 500 | /// Default ctor |
| 501 | /// \param type - node type |
| 502 | xml_node_struct(impl::xml_memory_page* page, xml_node_type type): header(reinterpret_cast<uintptr_t>(page) | (type - 1)), parent(0), name(0), value(0), first_child(0), prev_sibling_c(0), next_sibling(0), first_attribute(0) |
| 503 | { |
| 504 | } |
| 505 | |
| 506 | uintptr_t header; |
| 507 | |
| 508 | xml_node_struct* parent; ///< Pointer to parent |
| 509 | |
| 510 | char_t* name; ///< Pointer to element name. |
| 511 | char_t* value; ///< Pointer to any associated string data. |
| 512 | |
| 513 | xml_node_struct* first_child; ///< First child |
| 514 | |
| 515 | xml_node_struct* prev_sibling_c; ///< Left brother (cyclic list) |
| 516 | xml_node_struct* next_sibling; ///< Right brother |
| 517 | |
| 518 | xml_attribute_struct* first_attribute; ///< First attribute |
| 519 | }; |
| 520 | } |
| 521 | |
| 522 | PUGI__NS_BEGIN |
| 523 | struct xml_extra_buffer |
| 524 | { |
| 525 | char_t* buffer; |
| 526 | xml_extra_buffer* next; |
| 527 | }; |
| 528 | |
| 529 | struct xml_document_struct: public xml_node_struct, public xml_allocator |
| 530 | { |
| 531 | xml_document_struct(xml_memory_page* page): xml_node_struct(page, node_document), xml_allocator(page), buffer(0), extra_buffers(0) |
| 532 | { |
| 533 | } |
| 534 | |
| 535 | const char_t* buffer; |
| 536 | |
| 537 | xml_extra_buffer* extra_buffers; |
| 538 | }; |
| 539 | |
| 540 | inline xml_allocator& get_allocator(const xml_node_struct* node) |
| 541 | { |
| 542 | assert(node); |
| 543 | |
| 544 | return *reinterpret_cast<xml_memory_page*>(node->header & xml_memory_page_pointer_mask)->allocator; |
| 545 | } |
| 546 | PUGI__NS_END |
| 547 | |
| 548 | // Low-level DOM operations |
| 549 | PUGI__NS_BEGIN |
| 550 | inline xml_attribute_struct* allocate_attribute(xml_allocator& alloc) |
| 551 | { |
| 552 | xml_memory_page* page; |
| 553 | void* memory = alloc.allocate_memory(sizeof(xml_attribute_struct), page); |
| 554 | |
| 555 | return new (memory) xml_attribute_struct(page); |
| 556 | } |
| 557 | |
| 558 | inline xml_node_struct* allocate_node(xml_allocator& alloc, xml_node_type type) |
| 559 | { |
| 560 | xml_memory_page* page; |
| 561 | void* memory = alloc.allocate_memory(sizeof(xml_node_struct), page); |
| 562 | |
| 563 | return new (memory) xml_node_struct(page, type); |
| 564 | } |
| 565 | |
| 566 | inline void destroy_attribute(xml_attribute_struct* a, xml_allocator& alloc) |
| 567 | { |
| 568 | uintptr_t header = a->header; |
| 569 | |
| 570 | if (header & impl::xml_memory_page_name_allocated_mask) alloc.deallocate_string(a->name); |
| 571 | if (header & impl::xml_memory_page_value_allocated_mask) alloc.deallocate_string(a->value); |
| 572 | |
| 573 | alloc.deallocate_memory(a, sizeof(xml_attribute_struct), reinterpret_cast<xml_memory_page*>(header & xml_memory_page_pointer_mask)); |
| 574 | } |
| 575 | |
| 576 | inline void destroy_node(xml_node_struct* n, xml_allocator& alloc) |
| 577 | { |
| 578 | uintptr_t header = n->header; |
| 579 | |
| 580 | if (header & impl::xml_memory_page_name_allocated_mask) alloc.deallocate_string(n->name); |
| 581 | if (header & impl::xml_memory_page_value_allocated_mask) alloc.deallocate_string(n->value); |
| 582 | |
| 583 | for (xml_attribute_struct* attr = n->first_attribute; attr; ) |
| 584 | { |
| 585 | xml_attribute_struct* next = attr->next_attribute; |
| 586 | |
| 587 | destroy_attribute(attr, alloc); |
| 588 | |
| 589 | attr = next; |
| 590 | } |
| 591 | |
| 592 | for (xml_node_struct* child = n->first_child; child; ) |
| 593 | { |
| 594 | xml_node_struct* next = child->next_sibling; |
| 595 | |
| 596 | destroy_node(child, alloc); |
| 597 | |
| 598 | child = next; |
| 599 | } |
| 600 | |
| 601 | alloc.deallocate_memory(n, sizeof(xml_node_struct), reinterpret_cast<xml_memory_page*>(header & xml_memory_page_pointer_mask)); |
| 602 | } |
| 603 | |
| 604 | PUGI__FN_NO_INLINE xml_node_struct* append_node(xml_node_struct* node, xml_allocator& alloc, xml_node_type type = node_element) |
| 605 | { |
| 606 | xml_node_struct* child = allocate_node(alloc, type); |
| 607 | if (!child) return 0; |
| 608 | |
| 609 | child->parent = node; |
| 610 | |
| 611 | xml_node_struct* first_child = node->first_child; |
| 612 | |
| 613 | if (first_child) |
| 614 | { |
| 615 | xml_node_struct* last_child = first_child->prev_sibling_c; |
| 616 | |
| 617 | last_child->next_sibling = child; |
| 618 | child->prev_sibling_c = last_child; |
| 619 | first_child->prev_sibling_c = child; |
| 620 | } |
| 621 | else |
| 622 | { |
| 623 | node->first_child = child; |
| 624 | child->prev_sibling_c = child; |
| 625 | } |
| 626 | |
| 627 | return child; |
| 628 | } |
| 629 | |
| 630 | PUGI__FN_NO_INLINE xml_attribute_struct* append_attribute_ll(xml_node_struct* node, xml_allocator& alloc) |
| 631 | { |
| 632 | xml_attribute_struct* a = allocate_attribute(alloc); |
| 633 | if (!a) return 0; |
| 634 | |
| 635 | xml_attribute_struct* first_attribute = node->first_attribute; |
| 636 | |
| 637 | if (first_attribute) |
| 638 | { |
| 639 | xml_attribute_struct* last_attribute = first_attribute->prev_attribute_c; |
| 640 | |
| 641 | last_attribute->next_attribute = a; |
| 642 | a->prev_attribute_c = last_attribute; |
| 643 | first_attribute->prev_attribute_c = a; |
| 644 | } |
| 645 | else |
| 646 | { |
| 647 | node->first_attribute = a; |
| 648 | a->prev_attribute_c = a; |
| 649 | } |
| 650 | |
| 651 | return a; |
| 652 | } |
| 653 | PUGI__NS_END |
| 654 | |
| 655 | // Helper classes for code generation |
| 656 | PUGI__NS_BEGIN |
| 657 | struct opt_false |
| 658 | { |
| 659 | enum { value = 0 }; |
| 660 | }; |
| 661 | |
| 662 | struct opt_true |
| 663 | { |
| 664 | enum { value = 1 }; |
| 665 | }; |
| 666 | PUGI__NS_END |
| 667 | |
| 668 | // Unicode utilities |
| 669 | PUGI__NS_BEGIN |
| 670 | inline uint16_t endian_swap(uint16_t value) |
| 671 | { |
| 672 | return static_cast<uint16_t>(((value & 0xff) << 8) | (value >> 8)); |
| 673 | } |
| 674 | |
| 675 | inline uint32_t endian_swap(uint32_t value) |
| 676 | { |
| 677 | return ((value & 0xff) << 24) | ((value & 0xff00) << 8) | ((value & 0xff0000) >> 8) | (value >> 24); |
| 678 | } |
| 679 | |
| 680 | struct utf8_counter |
| 681 | { |
| 682 | typedef size_t value_type; |
| 683 | |
| 684 | static value_type low(value_type result, uint32_t ch) |
| 685 | { |
| 686 | // U+0000..U+007F |
| 687 | if (ch < 0x80) return result + 1; |
| 688 | // U+0080..U+07FF |
| 689 | else if (ch < 0x800) return result + 2; |
| 690 | // U+0800..U+FFFF |
| 691 | else return result + 3; |
| 692 | } |
| 693 | |
| 694 | static value_type high(value_type result, uint32_t) |
| 695 | { |
| 696 | // U+10000..U+10FFFF |
| 697 | return result + 4; |
| 698 | } |
| 699 | }; |
| 700 | |
| 701 | struct utf8_writer |
| 702 | { |
| 703 | typedef uint8_t* value_type; |
| 704 | |
| 705 | static value_type low(value_type result, uint32_t ch) |
| 706 | { |
| 707 | // U+0000..U+007F |
| 708 | if (ch < 0x80) |
| 709 | { |
| 710 | *result = static_cast<uint8_t>(ch); |
| 711 | return result + 1; |
| 712 | } |
| 713 | // U+0080..U+07FF |
| 714 | else if (ch < 0x800) |
| 715 | { |
| 716 | result[0] = static_cast<uint8_t>(0xC0 | (ch >> 6)); |
| 717 | result[1] = static_cast<uint8_t>(0x80 | (ch & 0x3F)); |
| 718 | return result + 2; |
| 719 | } |
| 720 | // U+0800..U+FFFF |
| 721 | else |
| 722 | { |
| 723 | result[0] = static_cast<uint8_t>(0xE0 | (ch >> 12)); |
| 724 | result[1] = static_cast<uint8_t>(0x80 | ((ch >> 6) & 0x3F)); |
| 725 | result[2] = static_cast<uint8_t>(0x80 | (ch & 0x3F)); |
| 726 | return result + 3; |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | static value_type high(value_type result, uint32_t ch) |
| 731 | { |
| 732 | // U+10000..U+10FFFF |
| 733 | result[0] = static_cast<uint8_t>(0xF0 | (ch >> 18)); |
| 734 | result[1] = static_cast<uint8_t>(0x80 | ((ch >> 12) & 0x3F)); |
| 735 | result[2] = static_cast<uint8_t>(0x80 | ((ch >> 6) & 0x3F)); |
| 736 | result[3] = static_cast<uint8_t>(0x80 | (ch & 0x3F)); |
| 737 | return result + 4; |
| 738 | } |
| 739 | |
| 740 | static value_type any(value_type result, uint32_t ch) |
| 741 | { |
| 742 | return (ch < 0x10000) ? low(result, ch) : high(result, ch); |
| 743 | } |
| 744 | }; |
| 745 | |
| 746 | struct utf16_counter |
| 747 | { |
| 748 | typedef size_t value_type; |
| 749 | |
| 750 | static value_type low(value_type result, uint32_t) |
| 751 | { |
| 752 | return result + 1; |
| 753 | } |
| 754 | |
| 755 | static value_type high(value_type result, uint32_t) |
| 756 | { |
| 757 | return result + 2; |
| 758 | } |
| 759 | }; |
| 760 | |
| 761 | struct utf16_writer |
| 762 | { |
| 763 | typedef uint16_t* value_type; |
| 764 | |
| 765 | static value_type low(value_type result, uint32_t ch) |
| 766 | { |
| 767 | *result = static_cast<uint16_t>(ch); |
| 768 | |
| 769 | return result + 1; |
| 770 | } |
| 771 | |
| 772 | static value_type high(value_type result, uint32_t ch) |
| 773 | { |
| 774 | uint32_t msh = static_cast<uint32_t>(ch - 0x10000) >> 10; |
| 775 | uint32_t lsh = static_cast<uint32_t>(ch - 0x10000) & 0x3ff; |
| 776 | |
| 777 | result[0] = static_cast<uint16_t>(0xD800 + msh); |
| 778 | result[1] = static_cast<uint16_t>(0xDC00 + lsh); |
| 779 | |
| 780 | return result + 2; |
| 781 | } |
| 782 | |
| 783 | static value_type any(value_type result, uint32_t ch) |
| 784 | { |
| 785 | return (ch < 0x10000) ? low(result, ch) : high(result, ch); |
| 786 | } |
| 787 | }; |
| 788 | |
| 789 | struct utf32_counter |
| 790 | { |
| 791 | typedef size_t value_type; |
| 792 | |
| 793 | static value_type low(value_type result, uint32_t) |
| 794 | { |
| 795 | return result + 1; |
| 796 | } |
| 797 | |
| 798 | static value_type high(value_type result, uint32_t) |
| 799 | { |
| 800 | return result + 1; |
| 801 | } |
| 802 | }; |
| 803 | |
| 804 | struct utf32_writer |
| 805 | { |
| 806 | typedef uint32_t* value_type; |
| 807 | |
| 808 | static value_type low(value_type result, uint32_t ch) |
| 809 | { |
| 810 | *result = ch; |
| 811 | |
| 812 | return result + 1; |
| 813 | } |
| 814 | |
| 815 | static value_type high(value_type result, uint32_t ch) |
| 816 | { |
| 817 | *result = ch; |
| 818 | |
| 819 | return result + 1; |
| 820 | } |
| 821 | |
| 822 | static value_type any(value_type result, uint32_t ch) |
| 823 | { |
| 824 | *result = ch; |
| 825 | |
| 826 | return result + 1; |
| 827 | } |
| 828 | }; |
| 829 | |
| 830 | struct latin1_writer |
| 831 | { |
| 832 | typedef uint8_t* value_type; |
| 833 | |
| 834 | static value_type low(value_type result, uint32_t ch) |
| 835 | { |
| 836 | *result = static_cast<uint8_t>(ch > 255 ? '?' : ch); |
| 837 | |
| 838 | return result + 1; |
| 839 | } |
| 840 | |
| 841 | static value_type high(value_type result, uint32_t ch) |
| 842 | { |
| 843 | (void)ch; |
| 844 | |
| 845 | *result = '?'; |
| 846 | |
| 847 | return result + 1; |
| 848 | } |
| 849 | }; |
| 850 | |
| 851 | template <size_t size> struct wchar_selector; |
| 852 | |
| 853 | template <> struct wchar_selector<2> |
| 854 | { |
| 855 | typedef uint16_t type; |
| 856 | typedef utf16_counter counter; |
| 857 | typedef utf16_writer writer; |
| 858 | }; |
| 859 | |
| 860 | template <> struct wchar_selector<4> |
| 861 | { |
| 862 | typedef uint32_t type; |
| 863 | typedef utf32_counter counter; |
| 864 | typedef utf32_writer writer; |
| 865 | }; |
| 866 | |
| 867 | typedef wchar_selector<sizeof(wchar_t)>::counter wchar_counter; |
| 868 | typedef wchar_selector<sizeof(wchar_t)>::writer wchar_writer; |
| 869 | |
| 870 | template <typename Traits, typename opt_swap = opt_false> struct utf_decoder |
| 871 | { |
| 872 | static inline typename Traits::value_type decode_utf8_block(const uint8_t* data, size_t size, typename Traits::value_type result) |
| 873 | { |
| 874 | const uint8_t utf8_byte_mask = 0x3f; |
| 875 | |
| 876 | while (size) |
| 877 | { |
| 878 | uint8_t lead = *data; |
| 879 | |
| 880 | // 0xxxxxxx -> U+0000..U+007F |
| 881 | if (lead < 0x80) |
| 882 | { |
| 883 | result = Traits::low(result, lead); |
| 884 | data += 1; |
| 885 | size -= 1; |
| 886 | |
| 887 | // process aligned single-byte (ascii) blocks |
| 888 | if ((reinterpret_cast<uintptr_t>(data) & 3) == 0) |
| 889 | { |
| 890 | // round-trip through void* to silence 'cast increases required alignment of target type' warnings |
| 891 | while (size >= 4 && (*static_cast<const uint32_t*>(static_cast<const void*>(data)) & 0x80808080) == 0) |
| 892 | { |
| 893 | result = Traits::low(result, data[0]); |
| 894 | result = Traits::low(result, data[1]); |
| 895 | result = Traits::low(result, data[2]); |
| 896 | result = Traits::low(result, data[3]); |
| 897 | data += 4; |
| 898 | size -= 4; |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | // 110xxxxx -> U+0080..U+07FF |
| 903 | else if (static_cast<unsigned int>(lead - 0xC0) < 0x20 && size >= 2 && (data[1] & 0xc0) == 0x80) |
| 904 | { |
| 905 | result = Traits::low(result, ((lead & ~0xC0) << 6) | (data[1] & utf8_byte_mask)); |
| 906 | data += 2; |
| 907 | size -= 2; |
| 908 | } |
| 909 | // 1110xxxx -> U+0800-U+FFFF |
| 910 | else if (static_cast<unsigned int>(lead - 0xE0) < 0x10 && size >= 3 && (data[1] & 0xc0) == 0x80 && (data[2] & 0xc0) == 0x80) |
| 911 | { |
| 912 | result = Traits::low(result, ((lead & ~0xE0) << 12) | ((data[1] & utf8_byte_mask) << 6) | (data[2] & utf8_byte_mask)); |
| 913 | data += 3; |
| 914 | size -= 3; |
| 915 | } |
| 916 | // 11110xxx -> U+10000..U+10FFFF |
| 917 | else if (static_cast<unsigned int>(lead - 0xF0) < 0x08 && size >= 4 && (data[1] & 0xc0) == 0x80 && (data[2] & 0xc0) == 0x80 && (data[3] & 0xc0) == 0x80) |
| 918 | { |
| 919 | result = Traits::high(result, ((lead & ~0xF0) << 18) | ((data[1] & utf8_byte_mask) << 12) | ((data[2] & utf8_byte_mask) << 6) | (data[3] & utf8_byte_mask)); |
| 920 | data += 4; |
| 921 | size -= 4; |
| 922 | } |
| 923 | // 10xxxxxx or 11111xxx -> invalid |
| 924 | else |
| 925 | { |
| 926 | data += 1; |
| 927 | size -= 1; |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | return result; |
| 932 | } |
| 933 | |
| 934 | static inline typename Traits::value_type decode_utf16_block(const uint16_t* data, size_t size, typename Traits::value_type result) |
| 935 | { |
| 936 | const uint16_t* end = data + size; |
| 937 | |
| 938 | while (data < end) |
| 939 | { |
| 940 | unsigned int lead = opt_swap::value ? endian_swap(*data) : *data; |
| 941 | |
| 942 | // U+0000..U+D7FF |
| 943 | if (lead < 0xD800) |
| 944 | { |
| 945 | result = Traits::low(result, lead); |
| 946 | data += 1; |
| 947 | } |
| 948 | // U+E000..U+FFFF |
| 949 | else if (static_cast<unsigned int>(lead - 0xE000) < 0x2000) |
| 950 | { |
| 951 | result = Traits::low(result, lead); |
| 952 | data += 1; |
| 953 | } |
| 954 | // surrogate pair lead |
| 955 | else if (static_cast<unsigned int>(lead - 0xD800) < 0x400 && data + 1 < end) |
| 956 | { |
| 957 | uint16_t next = opt_swap::value ? endian_swap(data[1]) : data[1]; |
| 958 | |
| 959 | if (static_cast<unsigned int>(next - 0xDC00) < 0x400) |
| 960 | { |
| 961 | result = Traits::high(result, 0x10000 + ((lead & 0x3ff) << 10) + (next & 0x3ff)); |
| 962 | data += 2; |
| 963 | } |
| 964 | else |
| 965 | { |
| 966 | data += 1; |
| 967 | } |
| 968 | } |
| 969 | else |
| 970 | { |
| 971 | data += 1; |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | return result; |
| 976 | } |
| 977 | |
| 978 | static inline typename Traits::value_type decode_utf32_block(const uint32_t* data, size_t size, typename Traits::value_type result) |
| 979 | { |
| 980 | const uint32_t* end = data + size; |
| 981 | |
| 982 | while (data < end) |
| 983 | { |
| 984 | uint32_t lead = opt_swap::value ? endian_swap(*data) : *data; |
| 985 | |
| 986 | // U+0000..U+FFFF |
| 987 | if (lead < 0x10000) |
| 988 | { |
| 989 | result = Traits::low(result, lead); |
| 990 | data += 1; |
| 991 | } |
| 992 | // U+10000..U+10FFFF |
| 993 | else |
| 994 | { |
| 995 | result = Traits::high(result, lead); |
| 996 | data += 1; |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | return result; |
| 1001 | } |
| 1002 | |
| 1003 | static inline typename Traits::value_type decode_latin1_block(const uint8_t* data, size_t size, typename Traits::value_type result) |
| 1004 | { |
| 1005 | for (size_t i = 0; i < size; ++i) |
| 1006 | { |
| 1007 | result = Traits::low(result, data[i]); |
| 1008 | } |
| 1009 | |
| 1010 | return result; |
| 1011 | } |
| 1012 | |
| 1013 | static inline typename Traits::value_type decode_wchar_block_impl(const uint16_t* data, size_t size, typename Traits::value_type result) |
| 1014 | { |
| 1015 | return decode_utf16_block(data, size, result); |
| 1016 | } |
| 1017 | |
| 1018 | static inline typename Traits::value_type decode_wchar_block_impl(const uint32_t* data, size_t size, typename Traits::value_type result) |
| 1019 | { |
| 1020 | return decode_utf32_block(data, size, result); |
| 1021 | } |
| 1022 | |
| 1023 | static inline typename Traits::value_type decode_wchar_block(const wchar_t* data, size_t size, typename Traits::value_type result) |
| 1024 | { |
| 1025 | return decode_wchar_block_impl(reinterpret_cast<const wchar_selector<sizeof(wchar_t)>::type*>(data), size, result); |
| 1026 | } |
| 1027 | }; |
| 1028 | |
| 1029 | template <typename T> PUGI__FN void convert_utf_endian_swap(T* result, const T* data, size_t length) |
| 1030 | { |
| 1031 | for (size_t i = 0; i < length; ++i) result[i] = endian_swap(data[i]); |
| 1032 | } |
| 1033 | |
| 1034 | #ifdef PUGIXML_WCHAR_MODE |
| 1035 | PUGI__FN void convert_wchar_endian_swap(wchar_t* result, const wchar_t* data, size_t length) |
| 1036 | { |
| 1037 | for (size_t i = 0; i < length; ++i) result[i] = static_cast<wchar_t>(endian_swap(static_cast<wchar_selector<sizeof(wchar_t)>::type>(data[i]))); |
| 1038 | } |
| 1039 | #endif |
| 1040 | PUGI__NS_END |
| 1041 | |
| 1042 | PUGI__NS_BEGIN |
| 1043 | enum chartype_t |
| 1044 | { |
| 1045 | ct_parse_pcdata = 1, // \0, &, \r, < |
| 1046 | ct_parse_attr = 2, // \0, &, \r, ', " |
| 1047 | ct_parse_attr_ws = 4, // \0, &, \r, ', ", \n, tab |
| 1048 | ct_space = 8, // \r, \n, space, tab |
| 1049 | ct_parse_cdata = 16, // \0, ], >, \r |
| 1050 | ct_parse_comment = 32, // \0, -, >, \r |
| 1051 | ct_symbol = 64, // Any symbol > 127, a-z, A-Z, 0-9, _, :, -, . |
| 1052 | ct_start_symbol = 128 // Any symbol > 127, a-z, A-Z, _, : |
| 1053 | }; |
| 1054 | |
| 1055 | static const unsigned char chartype_table[256] = |
| 1056 | { |
| 1057 | 55, 0, 0, 0, 0, 0, 0, 0, 0, 12, 12, 0, 0, 63, 0, 0, // 0-15 |
| 1058 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 16-31 |
| 1059 | 8, 0, 6, 0, 0, 0, 7, 6, 0, 0, 0, 0, 0, 96, 64, 0, // 32-47 |
| 1060 | 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 192, 0, 1, 0, 48, 0, // 48-63 |
| 1061 | 0, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, // 64-79 |
| 1062 | 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 0, 0, 16, 0, 192, // 80-95 |
| 1063 | 0, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, // 96-111 |
| 1064 | 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 0, 0, 0, 0, 0, // 112-127 |
| 1065 | |
| 1066 | 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, // 128+ |
| 1067 | 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, |
| 1068 | 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, |
| 1069 | 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, |
| 1070 | 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, |
| 1071 | 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, |
| 1072 | 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, |
| 1073 | 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192, 192 |
| 1074 | }; |
| 1075 | |
| 1076 | enum chartypex_t |
| 1077 | { |
| 1078 | ctx_special_pcdata = 1, // Any symbol >= 0 and < 32 (except \t, \r, \n), &, <, > |
| 1079 | ctx_special_attr = 2, // Any symbol >= 0 and < 32 (except \t), &, <, >, " |
| 1080 | ctx_start_symbol = 4, // Any symbol > 127, a-z, A-Z, _ |
| 1081 | ctx_digit = 8, // 0-9 |
| 1082 | ctx_symbol = 16 // Any symbol > 127, a-z, A-Z, 0-9, _, -, . |
| 1083 | }; |
| 1084 | |
| 1085 | static const unsigned char chartypex_table[256] = |
| 1086 | { |
| 1087 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 2, 3, 3, 2, 3, 3, // 0-15 |
| 1088 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 16-31 |
| 1089 | 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 16, 16, 0, // 32-47 |
| 1090 | 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 0, 0, 3, 0, 3, 0, // 48-63 |
| 1091 | |
| 1092 | 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, // 64-79 |
| 1093 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 0, 0, 0, 0, 20, // 80-95 |
| 1094 | 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, // 96-111 |
| 1095 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 0, 0, 0, 0, 0, // 112-127 |
| 1096 | |
| 1097 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, // 128+ |
| 1098 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, |
| 1099 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, |
| 1100 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, |
| 1101 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, |
| 1102 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, |
| 1103 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, |
| 1104 | 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 |
| 1105 | }; |
| 1106 | |
| 1107 | #ifdef PUGIXML_WCHAR_MODE |
| 1108 | #define PUGI__IS_CHARTYPE_IMPL(c, ct, table) ((static_cast<unsigned int>(c) < 128 ? table[static_cast<unsigned int>(c)] : table[128]) & (ct)) |
| 1109 | #else |
| 1110 | #define PUGI__IS_CHARTYPE_IMPL(c, ct, table) (table[static_cast<unsigned char>(c)] & (ct)) |
| 1111 | #endif |
| 1112 | |
| 1113 | #define PUGI__IS_CHARTYPE(c, ct) PUGI__IS_CHARTYPE_IMPL(c, ct, chartype_table) |
| 1114 | #define PUGI__IS_CHARTYPEX(c, ct) PUGI__IS_CHARTYPE_IMPL(c, ct, chartypex_table) |
| 1115 | |
| 1116 | PUGI__FN bool is_little_endian() |
| 1117 | { |
| 1118 | unsigned int ui = 1; |
| 1119 | |
| 1120 | return *reinterpret_cast<unsigned char*>(&ui) == 1; |
| 1121 | } |
| 1122 | |
| 1123 | PUGI__FN xml_encoding get_wchar_encoding() |
| 1124 | { |
| 1125 | PUGI__STATIC_ASSERT(sizeof(wchar_t) == 2 || sizeof(wchar_t) == 4); |
| 1126 | |
| 1127 | if (sizeof(wchar_t) == 2) |
| 1128 | return is_little_endian() ? encoding_utf16_le : encoding_utf16_be; |
| 1129 | else |
| 1130 | return is_little_endian() ? encoding_utf32_le : encoding_utf32_be; |
| 1131 | } |
| 1132 | |
| 1133 | PUGI__FN xml_encoding guess_buffer_encoding(uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3) |
| 1134 | { |
| 1135 | // look for BOM in first few bytes |
| 1136 | if (d0 == 0 && d1 == 0 && d2 == 0xfe && d3 == 0xff) return encoding_utf32_be; |
| 1137 | if (d0 == 0xff && d1 == 0xfe && d2 == 0 && d3 == 0) return encoding_utf32_le; |
| 1138 | if (d0 == 0xfe && d1 == 0xff) return encoding_utf16_be; |
| 1139 | if (d0 == 0xff && d1 == 0xfe) return encoding_utf16_le; |
| 1140 | if (d0 == 0xef && d1 == 0xbb && d2 == 0xbf) return encoding_utf8; |
| 1141 | |
| 1142 | // look for <, <? or <?xm in various encodings |
| 1143 | if (d0 == 0 && d1 == 0 && d2 == 0 && d3 == 0x3c) return encoding_utf32_be; |
| 1144 | if (d0 == 0x3c && d1 == 0 && d2 == 0 && d3 == 0) return encoding_utf32_le; |
| 1145 | if (d0 == 0 && d1 == 0x3c && d2 == 0 && d3 == 0x3f) return encoding_utf16_be; |
| 1146 | if (d0 == 0x3c && d1 == 0 && d2 == 0x3f && d3 == 0) return encoding_utf16_le; |
| 1147 | if (d0 == 0x3c && d1 == 0x3f && d2 == 0x78 && d3 == 0x6d) return encoding_utf8; |
| 1148 | |
| 1149 | // look for utf16 < followed by node name (this may fail, but is better than utf8 since it's zero terminated so early) |
| 1150 | if (d0 == 0 && d1 == 0x3c) return encoding_utf16_be; |
| 1151 | if (d0 == 0x3c && d1 == 0) return encoding_utf16_le; |
| 1152 | |
| 1153 | // no known BOM detected, assume utf8 |
| 1154 | return encoding_utf8; |
| 1155 | } |
| 1156 | |
| 1157 | PUGI__FN xml_encoding get_buffer_encoding(xml_encoding encoding, const void* contents, size_t size) |
| 1158 | { |
| 1159 | // replace wchar encoding with utf implementation |
| 1160 | if (encoding == encoding_wchar) return get_wchar_encoding(); |
| 1161 | |
| 1162 | // replace utf16 encoding with utf16 with specific endianness |
| 1163 | if (encoding == encoding_utf16) return is_little_endian() ? encoding_utf16_le : encoding_utf16_be; |
| 1164 | |
| 1165 | // replace utf32 encoding with utf32 with specific endianness |
| 1166 | if (encoding == encoding_utf32) return is_little_endian() ? encoding_utf32_le : encoding_utf32_be; |
| 1167 | |
| 1168 | // only do autodetection if no explicit encoding is requested |
| 1169 | if (encoding != encoding_auto) return encoding; |
| 1170 | |
| 1171 | // skip encoding autodetection if input buffer is too small |
| 1172 | if (size < 4) return encoding_utf8; |
| 1173 | |
| 1174 | // try to guess encoding (based on XML specification, Appendix F.1) |
| 1175 | const uint8_t* data = static_cast<const uint8_t*>(contents); |
| 1176 | |
| 1177 | PUGI__DMC_VOLATILE uint8_t d0 = data[0], d1 = data[1], d2 = data[2], d3 = data[3]; |
| 1178 | |
| 1179 | return guess_buffer_encoding(d0, d1, d2, d3); |
| 1180 | } |
| 1181 | |
| 1182 | PUGI__FN bool get_mutable_buffer(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, bool is_mutable) |
| 1183 | { |
| 1184 | size_t length = size / sizeof(char_t); |
| 1185 | |
| 1186 | if (is_mutable) |
| 1187 | { |
| 1188 | out_buffer = static_cast<char_t*>(const_cast<void*>(contents)); |
| 1189 | out_length = length; |
| 1190 | } |
| 1191 | else |
| 1192 | { |
| 1193 | char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t))); |
| 1194 | if (!buffer) return false; |
| 1195 | |
| 1196 | memcpy(buffer, contents, length * sizeof(char_t)); |
| 1197 | buffer[length] = 0; |
| 1198 | |
| 1199 | out_buffer = buffer; |
| 1200 | out_length = length + 1; |
| 1201 | } |
| 1202 | |
| 1203 | return true; |
| 1204 | } |
| 1205 | |
| 1206 | #ifdef PUGIXML_WCHAR_MODE |
| 1207 | PUGI__FN bool need_endian_swap_utf(xml_encoding le, xml_encoding re) |
| 1208 | { |
| 1209 | return (le == encoding_utf16_be && re == encoding_utf16_le) || (le == encoding_utf16_le && re == encoding_utf16_be) || |
| 1210 | (le == encoding_utf32_be && re == encoding_utf32_le) || (le == encoding_utf32_le && re == encoding_utf32_be); |
| 1211 | } |
| 1212 | |
| 1213 | PUGI__FN bool convert_buffer_endian_swap(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, bool is_mutable) |
| 1214 | { |
| 1215 | const char_t* data = static_cast<const char_t*>(contents); |
| 1216 | size_t length = size / sizeof(char_t); |
| 1217 | |
| 1218 | if (is_mutable) |
| 1219 | { |
| 1220 | char_t* buffer = const_cast<char_t*>(data); |
| 1221 | |
| 1222 | convert_wchar_endian_swap(buffer, data, length); |
| 1223 | |
| 1224 | out_buffer = buffer; |
| 1225 | out_length = length; |
| 1226 | } |
| 1227 | else |
| 1228 | { |
| 1229 | char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t))); |
| 1230 | if (!buffer) return false; |
| 1231 | |
| 1232 | convert_wchar_endian_swap(buffer, data, length); |
| 1233 | buffer[length] = 0; |
| 1234 | |
| 1235 | out_buffer = buffer; |
| 1236 | out_length = length + 1; |
| 1237 | } |
| 1238 | |
| 1239 | return true; |
| 1240 | } |
| 1241 | |
| 1242 | PUGI__FN bool convert_buffer_utf8(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size) |
| 1243 | { |
| 1244 | const uint8_t* data = static_cast<const uint8_t*>(contents); |
| 1245 | size_t data_length = size; |
| 1246 | |
| 1247 | // first pass: get length in wchar_t units |
| 1248 | size_t length = utf_decoder<wchar_counter>::decode_utf8_block(data, data_length, 0); |
| 1249 | |
| 1250 | // allocate buffer of suitable length |
| 1251 | char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t))); |
| 1252 | if (!buffer) return false; |
| 1253 | |
| 1254 | // second pass: convert utf8 input to wchar_t |
| 1255 | wchar_writer::value_type obegin = reinterpret_cast<wchar_writer::value_type>(buffer); |
| 1256 | wchar_writer::value_type oend = utf_decoder<wchar_writer>::decode_utf8_block(data, data_length, obegin); |
| 1257 | |
| 1258 | assert(oend == obegin + length); |
| 1259 | *oend = 0; |
| 1260 | |
| 1261 | out_buffer = buffer; |
| 1262 | out_length = length + 1; |
| 1263 | |
| 1264 | return true; |
| 1265 | } |
| 1266 | |
| 1267 | template <typename opt_swap> PUGI__FN bool convert_buffer_utf16(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, opt_swap) |
| 1268 | { |
| 1269 | const uint16_t* data = static_cast<const uint16_t*>(contents); |
| 1270 | size_t data_length = size / sizeof(uint16_t); |
| 1271 | |
| 1272 | // first pass: get length in wchar_t units |
| 1273 | size_t length = utf_decoder<wchar_counter, opt_swap>::decode_utf16_block(data, data_length, 0); |
| 1274 | |
| 1275 | // allocate buffer of suitable length |
| 1276 | char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t))); |
| 1277 | if (!buffer) return false; |
| 1278 | |
| 1279 | // second pass: convert utf16 input to wchar_t |
| 1280 | wchar_writer::value_type obegin = reinterpret_cast<wchar_writer::value_type>(buffer); |
| 1281 | wchar_writer::value_type oend = utf_decoder<wchar_writer, opt_swap>::decode_utf16_block(data, data_length, obegin); |
| 1282 | |
| 1283 | assert(oend == obegin + length); |
| 1284 | *oend = 0; |
| 1285 | |
| 1286 | out_buffer = buffer; |
| 1287 | out_length = length + 1; |
| 1288 | |
| 1289 | return true; |
| 1290 | } |
| 1291 | |
| 1292 | template <typename opt_swap> PUGI__FN bool convert_buffer_utf32(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, opt_swap) |
| 1293 | { |
| 1294 | const uint32_t* data = static_cast<const uint32_t*>(contents); |
| 1295 | size_t data_length = size / sizeof(uint32_t); |
| 1296 | |
| 1297 | // first pass: get length in wchar_t units |
| 1298 | size_t length = utf_decoder<wchar_counter, opt_swap>::decode_utf32_block(data, data_length, 0); |
| 1299 | |
| 1300 | // allocate buffer of suitable length |
| 1301 | char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t))); |
| 1302 | if (!buffer) return false; |
| 1303 | |
| 1304 | // second pass: convert utf32 input to wchar_t |
| 1305 | wchar_writer::value_type obegin = reinterpret_cast<wchar_writer::value_type>(buffer); |
| 1306 | wchar_writer::value_type oend = utf_decoder<wchar_writer, opt_swap>::decode_utf32_block(data, data_length, obegin); |
| 1307 | |
| 1308 | assert(oend == obegin + length); |
| 1309 | *oend = 0; |
| 1310 | |
| 1311 | out_buffer = buffer; |
| 1312 | out_length = length + 1; |
| 1313 | |
| 1314 | return true; |
| 1315 | } |
| 1316 | |
| 1317 | PUGI__FN bool convert_buffer_latin1(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size) |
| 1318 | { |
| 1319 | const uint8_t* data = static_cast<const uint8_t*>(contents); |
| 1320 | size_t data_length = size; |
| 1321 | |
| 1322 | // get length in wchar_t units |
| 1323 | size_t length = data_length; |
| 1324 | |
| 1325 | // allocate buffer of suitable length |
| 1326 | char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t))); |
| 1327 | if (!buffer) return false; |
| 1328 | |
| 1329 | // convert latin1 input to wchar_t |
| 1330 | wchar_writer::value_type obegin = reinterpret_cast<wchar_writer::value_type>(buffer); |
| 1331 | wchar_writer::value_type oend = utf_decoder<wchar_writer>::decode_latin1_block(data, data_length, obegin); |
| 1332 | |
| 1333 | assert(oend == obegin + length); |
| 1334 | *oend = 0; |
| 1335 | |
| 1336 | out_buffer = buffer; |
| 1337 | out_length = length + 1; |
| 1338 | |
| 1339 | return true; |
| 1340 | } |
| 1341 | |
| 1342 | PUGI__FN bool convert_buffer(char_t*& out_buffer, size_t& out_length, xml_encoding encoding, const void* contents, size_t size, bool is_mutable) |
| 1343 | { |
| 1344 | // get native encoding |
| 1345 | xml_encoding wchar_encoding = get_wchar_encoding(); |
| 1346 | |
| 1347 | // fast path: no conversion required |
| 1348 | if (encoding == wchar_encoding) return get_mutable_buffer(out_buffer, out_length, contents, size, is_mutable); |
| 1349 | |
| 1350 | // only endian-swapping is required |
| 1351 | if (need_endian_swap_utf(encoding, wchar_encoding)) return convert_buffer_endian_swap(out_buffer, out_length, contents, size, is_mutable); |
| 1352 | |
| 1353 | // source encoding is utf8 |
| 1354 | if (encoding == encoding_utf8) return convert_buffer_utf8(out_buffer, out_length, contents, size); |
| 1355 | |
| 1356 | // source encoding is utf16 |
| 1357 | if (encoding == encoding_utf16_be || encoding == encoding_utf16_le) |
| 1358 | { |
| 1359 | xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be; |
| 1360 | |
| 1361 | return (native_encoding == encoding) ? |
| 1362 | convert_buffer_utf16(out_buffer, out_length, contents, size, opt_false()) : |
| 1363 | convert_buffer_utf16(out_buffer, out_length, contents, size, opt_true()); |
| 1364 | } |
| 1365 | |
| 1366 | // source encoding is utf32 |
| 1367 | if (encoding == encoding_utf32_be || encoding == encoding_utf32_le) |
| 1368 | { |
| 1369 | xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be; |
| 1370 | |
| 1371 | return (native_encoding == encoding) ? |
| 1372 | convert_buffer_utf32(out_buffer, out_length, contents, size, opt_false()) : |
| 1373 | convert_buffer_utf32(out_buffer, out_length, contents, size, opt_true()); |
| 1374 | } |
| 1375 | |
| 1376 | // source encoding is latin1 |
| 1377 | if (encoding == encoding_latin1) return convert_buffer_latin1(out_buffer, out_length, contents, size); |
| 1378 | |
| 1379 | assert(!"Invalid encoding"); |
| 1380 | return false; |
| 1381 | } |
| 1382 | #else |
| 1383 | template <typename opt_swap> PUGI__FN bool convert_buffer_utf16(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, opt_swap) |
| 1384 | { |
| 1385 | const uint16_t* data = static_cast<const uint16_t*>(contents); |
| 1386 | size_t data_length = size / sizeof(uint16_t); |
| 1387 | |
| 1388 | // first pass: get length in utf8 units |
| 1389 | size_t length = utf_decoder<utf8_counter, opt_swap>::decode_utf16_block(data, data_length, 0); |
| 1390 | |
| 1391 | // allocate buffer of suitable length |
| 1392 | char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t))); |
| 1393 | if (!buffer) return false; |
| 1394 | |
| 1395 | // second pass: convert utf16 input to utf8 |
| 1396 | uint8_t* obegin = reinterpret_cast<uint8_t*>(buffer); |
| 1397 | uint8_t* oend = utf_decoder<utf8_writer, opt_swap>::decode_utf16_block(data, data_length, obegin); |
| 1398 | |
| 1399 | assert(oend == obegin + length); |
| 1400 | *oend = 0; |
| 1401 | |
| 1402 | out_buffer = buffer; |
| 1403 | out_length = length + 1; |
| 1404 | |
| 1405 | return true; |
| 1406 | } |
| 1407 | |
| 1408 | template <typename opt_swap> PUGI__FN bool convert_buffer_utf32(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, opt_swap) |
| 1409 | { |
| 1410 | const uint32_t* data = static_cast<const uint32_t*>(contents); |
| 1411 | size_t data_length = size / sizeof(uint32_t); |
| 1412 | |
| 1413 | // first pass: get length in utf8 units |
| 1414 | size_t length = utf_decoder<utf8_counter, opt_swap>::decode_utf32_block(data, data_length, 0); |
| 1415 | |
| 1416 | // allocate buffer of suitable length |
| 1417 | char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t))); |
| 1418 | if (!buffer) return false; |
| 1419 | |
| 1420 | // second pass: convert utf32 input to utf8 |
| 1421 | uint8_t* obegin = reinterpret_cast<uint8_t*>(buffer); |
| 1422 | uint8_t* oend = utf_decoder<utf8_writer, opt_swap>::decode_utf32_block(data, data_length, obegin); |
| 1423 | |
| 1424 | assert(oend == obegin + length); |
| 1425 | *oend = 0; |
| 1426 | |
| 1427 | out_buffer = buffer; |
| 1428 | out_length = length + 1; |
| 1429 | |
| 1430 | return true; |
| 1431 | } |
| 1432 | |
| 1433 | PUGI__FN size_t get_latin1_7bit_prefix_length(const uint8_t* data, size_t size) |
| 1434 | { |
| 1435 | for (size_t i = 0; i < size; ++i) |
| 1436 | if (data[i] > 127) |
| 1437 | return i; |
| 1438 | |
| 1439 | return size; |
| 1440 | } |
| 1441 | |
| 1442 | PUGI__FN bool convert_buffer_latin1(char_t*& out_buffer, size_t& out_length, const void* contents, size_t size, bool is_mutable) |
| 1443 | { |
| 1444 | const uint8_t* data = static_cast<const uint8_t*>(contents); |
| 1445 | size_t data_length = size; |
| 1446 | |
| 1447 | // get size of prefix that does not need utf8 conversion |
| 1448 | size_t prefix_length = get_latin1_7bit_prefix_length(data, data_length); |
| 1449 | assert(prefix_length <= data_length); |
| 1450 | |
| 1451 | const uint8_t* postfix = data + prefix_length; |
| 1452 | size_t postfix_length = data_length - prefix_length; |
| 1453 | |
| 1454 | // if no conversion is needed, just return the original buffer |
| 1455 | if (postfix_length == 0) return get_mutable_buffer(out_buffer, out_length, contents, size, is_mutable); |
| 1456 | |
| 1457 | // first pass: get length in utf8 units |
| 1458 | size_t length = prefix_length + utf_decoder<utf8_counter>::decode_latin1_block(postfix, postfix_length, 0); |
| 1459 | |
| 1460 | // allocate buffer of suitable length |
| 1461 | char_t* buffer = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t))); |
| 1462 | if (!buffer) return false; |
| 1463 | |
| 1464 | // second pass: convert latin1 input to utf8 |
| 1465 | memcpy(buffer, data, prefix_length); |
| 1466 | |
| 1467 | uint8_t* obegin = reinterpret_cast<uint8_t*>(buffer); |
| 1468 | uint8_t* oend = utf_decoder<utf8_writer>::decode_latin1_block(postfix, postfix_length, obegin + prefix_length); |
| 1469 | |
| 1470 | assert(oend == obegin + length); |
| 1471 | *oend = 0; |
| 1472 | |
| 1473 | out_buffer = buffer; |
| 1474 | out_length = length + 1; |
| 1475 | |
| 1476 | return true; |
| 1477 | } |
| 1478 | |
| 1479 | PUGI__FN bool convert_buffer(char_t*& out_buffer, size_t& out_length, xml_encoding encoding, const void* contents, size_t size, bool is_mutable) |
| 1480 | { |
| 1481 | // fast path: no conversion required |
| 1482 | if (encoding == encoding_utf8) return get_mutable_buffer(out_buffer, out_length, contents, size, is_mutable); |
| 1483 | |
| 1484 | // source encoding is utf16 |
| 1485 | if (encoding == encoding_utf16_be || encoding == encoding_utf16_le) |
| 1486 | { |
| 1487 | xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be; |
| 1488 | |
| 1489 | return (native_encoding == encoding) ? |
| 1490 | convert_buffer_utf16(out_buffer, out_length, contents, size, opt_false()) : |
| 1491 | convert_buffer_utf16(out_buffer, out_length, contents, size, opt_true()); |
| 1492 | } |
| 1493 | |
| 1494 | // source encoding is utf32 |
| 1495 | if (encoding == encoding_utf32_be || encoding == encoding_utf32_le) |
| 1496 | { |
| 1497 | xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be; |
| 1498 | |
| 1499 | return (native_encoding == encoding) ? |
| 1500 | convert_buffer_utf32(out_buffer, out_length, contents, size, opt_false()) : |
| 1501 | convert_buffer_utf32(out_buffer, out_length, contents, size, opt_true()); |
| 1502 | } |
| 1503 | |
| 1504 | // source encoding is latin1 |
| 1505 | if (encoding == encoding_latin1) return convert_buffer_latin1(out_buffer, out_length, contents, size, is_mutable); |
| 1506 | |
| 1507 | assert(!"Invalid encoding"); |
| 1508 | return false; |
| 1509 | } |
| 1510 | #endif |
| 1511 | |
| 1512 | PUGI__FN size_t as_utf8_begin(const wchar_t* str, size_t length) |
| 1513 | { |
| 1514 | // get length in utf8 characters |
| 1515 | return utf_decoder<utf8_counter>::decode_wchar_block(str, length, 0); |
| 1516 | } |
| 1517 | |
| 1518 | PUGI__FN void as_utf8_end(char* buffer, size_t size, const wchar_t* str, size_t length) |
| 1519 | { |
| 1520 | // convert to utf8 |
| 1521 | uint8_t* begin = reinterpret_cast<uint8_t*>(buffer); |
| 1522 | uint8_t* end = utf_decoder<utf8_writer>::decode_wchar_block(str, length, begin); |
| 1523 | |
| 1524 | assert(begin + size == end); |
| 1525 | (void)!end; |
| 1526 | |
| 1527 | // zero-terminate |
| 1528 | buffer[size] = 0; |
| 1529 | } |
| 1530 | |
| 1531 | #ifndef PUGIXML_NO_STL |
| 1532 | PUGI__FN std::string as_utf8_impl(const wchar_t* str, size_t length) |
| 1533 | { |
| 1534 | // first pass: get length in utf8 characters |
| 1535 | size_t size = as_utf8_begin(str, length); |
| 1536 | |
| 1537 | // allocate resulting string |
| 1538 | std::string result; |
| 1539 | result.resize(size); |
| 1540 | |
| 1541 | // second pass: convert to utf8 |
| 1542 | if (size > 0) as_utf8_end(&result[0], size, str, length); |
| 1543 | |
| 1544 | return result; |
| 1545 | } |
| 1546 | |
| 1547 | PUGI__FN std::basic_string<wchar_t> as_wide_impl(const char* str, size_t size) |
| 1548 | { |
| 1549 | const uint8_t* data = reinterpret_cast<const uint8_t*>(str); |
| 1550 | |
| 1551 | // first pass: get length in wchar_t units |
| 1552 | size_t length = utf_decoder<wchar_counter>::decode_utf8_block(data, size, 0); |
| 1553 | |
| 1554 | // allocate resulting string |
| 1555 | std::basic_string<wchar_t> result; |
| 1556 | result.resize(length); |
| 1557 | |
| 1558 | // second pass: convert to wchar_t |
| 1559 | if (length > 0) |
| 1560 | { |
| 1561 | wchar_writer::value_type begin = reinterpret_cast<wchar_writer::value_type>(&result[0]); |
| 1562 | wchar_writer::value_type end = utf_decoder<wchar_writer>::decode_utf8_block(data, size, begin); |
| 1563 | |
| 1564 | assert(begin + length == end); |
| 1565 | (void)!end; |
| 1566 | } |
| 1567 | |
| 1568 | return result; |
| 1569 | } |
| 1570 | #endif |
| 1571 | |
| 1572 | inline bool strcpy_insitu_allow(size_t length, uintptr_t allocated, char_t* target) |
| 1573 | { |
| 1574 | assert(target); |
| 1575 | size_t target_length = strlength(target); |
| 1576 | |
| 1577 | // always reuse document buffer memory if possible |
| 1578 | if (!allocated) return target_length >= length; |
| 1579 | |
| 1580 | // reuse heap memory if waste is not too great |
| 1581 | const size_t reuse_threshold = 32; |
| 1582 | |
| 1583 | return target_length >= length && (target_length < reuse_threshold || target_length - length < target_length / 2); |
| 1584 | } |
| 1585 | |
| 1586 | PUGI__FN bool strcpy_insitu(char_t*& dest, uintptr_t& header, uintptr_t header_mask, const char_t* source) |
| 1587 | { |
| 1588 | assert(header); |
| 1589 | |
| 1590 | size_t source_length = strlength(source); |
| 1591 | |
| 1592 | if (source_length == 0) |
| 1593 | { |
| 1594 | // empty string and null pointer are equivalent, so just deallocate old memory |
| 1595 | xml_allocator* alloc = reinterpret_cast<xml_memory_page*>(header & xml_memory_page_pointer_mask)->allocator; |
| 1596 | |
| 1597 | if (header & header_mask) alloc->deallocate_string(dest); |
| 1598 | |
| 1599 | // mark the string as not allocated |
| 1600 | dest = 0; |
| 1601 | header &= ~header_mask; |
| 1602 | |
| 1603 | return true; |
| 1604 | } |
| 1605 | else if (dest && strcpy_insitu_allow(source_length, header & header_mask, dest)) |
| 1606 | { |
| 1607 | // we can reuse old buffer, so just copy the new data (including zero terminator) |
| 1608 | memcpy(dest, source, (source_length + 1) * sizeof(char_t)); |
| 1609 | |
| 1610 | return true; |
| 1611 | } |
| 1612 | else |
| 1613 | { |
| 1614 | xml_allocator* alloc = reinterpret_cast<xml_memory_page*>(header & xml_memory_page_pointer_mask)->allocator; |
| 1615 | |
| 1616 | // allocate new buffer |
| 1617 | char_t* buf = alloc->allocate_string(source_length + 1); |
| 1618 | if (!buf) return false; |
| 1619 | |
| 1620 | // copy the string (including zero terminator) |
| 1621 | memcpy(buf, source, (source_length + 1) * sizeof(char_t)); |
| 1622 | |
| 1623 | // deallocate old buffer (*after* the above to protect against overlapping memory and/or allocation failures) |
| 1624 | if (header & header_mask) alloc->deallocate_string(dest); |
| 1625 | |
| 1626 | // the string is now allocated, so set the flag |
| 1627 | dest = buf; |
| 1628 | header |= header_mask; |
| 1629 | |
| 1630 | return true; |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | struct gap |
| 1635 | { |
| 1636 | char_t* end; |
| 1637 | size_t size; |
| 1638 | |
| 1639 | gap(): end(0), size(0) |
| 1640 | { |
| 1641 | } |
| 1642 | |
| 1643 | // Push new gap, move s count bytes further (skipping the gap). |
| 1644 | // Collapse previous gap. |
| 1645 | void push(char_t*& s, size_t count) |
| 1646 | { |
| 1647 | if (end) // there was a gap already; collapse it |
| 1648 | { |
| 1649 | // Move [old_gap_end, new_gap_start) to [old_gap_start, ...) |
| 1650 | assert(s >= end); |
| 1651 | memmove(end - size, end, reinterpret_cast<char*>(s) - reinterpret_cast<char*>(end)); |
| 1652 | } |
| 1653 | |
| 1654 | s += count; // end of current gap |
| 1655 | |
| 1656 | // "merge" two gaps |
| 1657 | end = s; |
| 1658 | size += count; |
| 1659 | } |
| 1660 | |
| 1661 | // Collapse all gaps, return past-the-end pointer |
| 1662 | char_t* flush(char_t* s) |
| 1663 | { |
| 1664 | if (end) |
| 1665 | { |
| 1666 | // Move [old_gap_end, current_pos) to [old_gap_start, ...) |
| 1667 | assert(s >= end); |
| 1668 | memmove(end - size, end, reinterpret_cast<char*>(s) - reinterpret_cast<char*>(end)); |
| 1669 | |
| 1670 | return s - size; |
| 1671 | } |
| 1672 | else return s; |
| 1673 | } |
| 1674 | }; |
| 1675 | |
| 1676 | PUGI__FN char_t* strconv_escape(char_t* s, gap& g) |
| 1677 | { |
| 1678 | char_t* stre = s + 1; |
| 1679 | |
| 1680 | switch (*stre) |
| 1681 | { |
| 1682 | case '#': // &#... |
| 1683 | { |
| 1684 | unsigned int ucsc = 0; |
| 1685 | |
| 1686 | if (stre[1] == 'x') // &#x... (hex code) |
| 1687 | { |
| 1688 | stre += 2; |
| 1689 | |
| 1690 | char_t ch = *stre; |
| 1691 | |
| 1692 | if (ch == ';') return stre; |
| 1693 | |
| 1694 | for (;;) |
| 1695 | { |
| 1696 | if (static_cast<unsigned int>(ch - '0') <= 9) |
| 1697 | ucsc = 16 * ucsc + (ch - '0'); |
| 1698 | else if (static_cast<unsigned int>((ch | ' ') - 'a') <= 5) |
| 1699 | ucsc = 16 * ucsc + ((ch | ' ') - 'a' + 10); |
| 1700 | else if (ch == ';') |
| 1701 | break; |
| 1702 | else // cancel |
| 1703 | return stre; |
| 1704 | |
| 1705 | ch = *++stre; |
| 1706 | } |
| 1707 | |
| 1708 | ++stre; |
| 1709 | } |
| 1710 | else // &#... (dec code) |
| 1711 | { |
| 1712 | char_t ch = *++stre; |
| 1713 | |
| 1714 | if (ch == ';') return stre; |
| 1715 | |
| 1716 | for (;;) |
| 1717 | { |
| 1718 | if (static_cast<unsigned int>(static_cast<unsigned int>(ch) - '0') <= 9) |
| 1719 | ucsc = 10 * ucsc + (ch - '0'); |
| 1720 | else if (ch == ';') |
| 1721 | break; |
| 1722 | else // cancel |
| 1723 | return stre; |
| 1724 | |
| 1725 | ch = *++stre; |
| 1726 | } |
| 1727 | |
| 1728 | ++stre; |
| 1729 | } |
| 1730 | |
| 1731 | #ifdef PUGIXML_WCHAR_MODE |
| 1732 | s = reinterpret_cast<char_t*>(wchar_writer::any(reinterpret_cast<wchar_writer::value_type>(s), ucsc)); |
| 1733 | #else |
| 1734 | s = reinterpret_cast<char_t*>(utf8_writer::any(reinterpret_cast<uint8_t*>(s), ucsc)); |
| 1735 | #endif |
| 1736 | |
| 1737 | g.push(s, stre - s); |
| 1738 | return stre; |
| 1739 | } |
| 1740 | |
| 1741 | case 'a': // &a |
| 1742 | { |
| 1743 | ++stre; |
| 1744 | |
| 1745 | if (*stre == 'm') // &am |
| 1746 | { |
| 1747 | if (*++stre == 'p' && *++stre == ';') // & |
| 1748 | { |
| 1749 | *s++ = '&'; |
| 1750 | ++stre; |
| 1751 | |
| 1752 | g.push(s, stre - s); |
| 1753 | return stre; |
| 1754 | } |
| 1755 | } |
| 1756 | else if (*stre == 'p') // &ap |
| 1757 | { |
| 1758 | if (*++stre == 'o' && *++stre == 's' && *++stre == ';') // ' |
| 1759 | { |
| 1760 | *s++ = '\''; |
| 1761 | ++stre; |
| 1762 | |
| 1763 | g.push(s, stre - s); |
| 1764 | return stre; |
| 1765 | } |
| 1766 | } |
| 1767 | break; |
| 1768 | } |
| 1769 | |
| 1770 | case 'g': // &g |
| 1771 | { |
| 1772 | if (*++stre == 't' && *++stre == ';') // > |
| 1773 | { |
| 1774 | *s++ = '>'; |
| 1775 | ++stre; |
| 1776 | |
| 1777 | g.push(s, stre - s); |
| 1778 | return stre; |
| 1779 | } |
| 1780 | break; |
| 1781 | } |
| 1782 | |
| 1783 | case 'l': // &l |
| 1784 | { |
| 1785 | if (*++stre == 't' && *++stre == ';') // < |
| 1786 | { |
| 1787 | *s++ = '<'; |
| 1788 | ++stre; |
| 1789 | |
| 1790 | g.push(s, stre - s); |
| 1791 | return stre; |
| 1792 | } |
| 1793 | break; |
| 1794 | } |
| 1795 | |
| 1796 | case 'q': // &q |
| 1797 | { |
| 1798 | if (*++stre == 'u' && *++stre == 'o' && *++stre == 't' && *++stre == ';') // " |
| 1799 | { |
| 1800 | *s++ = '"'; |
| 1801 | ++stre; |
| 1802 | |
| 1803 | g.push(s, stre - s); |
| 1804 | return stre; |
| 1805 | } |
| 1806 | break; |
| 1807 | } |
| 1808 | |
| 1809 | default: |
| 1810 | break; |
| 1811 | } |
| 1812 | |
| 1813 | return stre; |
| 1814 | } |
| 1815 | |
| 1816 | // Utility macro for last character handling |
| 1817 | #define ENDSWITH(c, e) ((c) == (e) || ((c) == 0 && endch == (e))) |
| 1818 | |
| 1819 | PUGI__FN char_t* strconv_comment(char_t* s, char_t endch) |
| 1820 | { |
| 1821 | gap g; |
| 1822 | |
| 1823 | while (true) |
| 1824 | { |
| 1825 | while (!PUGI__IS_CHARTYPE(*s, ct_parse_comment)) ++s; |
| 1826 | |
| 1827 | if (*s == '\r') // Either a single 0x0d or 0x0d 0x0a pair |
| 1828 | { |
| 1829 | *s++ = '\n'; // replace first one with 0x0a |
| 1830 | |
| 1831 | if (*s == '\n') g.push(s, 1); |
| 1832 | } |
| 1833 | else if (s[0] == '-' && s[1] == '-' && ENDSWITH(s[2], '>')) // comment ends here |
| 1834 | { |
| 1835 | *g.flush(s) = 0; |
| 1836 | |
| 1837 | return s + (s[2] == '>' ? 3 : 2); |
| 1838 | } |
| 1839 | else if (*s == 0) |
| 1840 | { |
| 1841 | return 0; |
| 1842 | } |
| 1843 | else ++s; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | PUGI__FN char_t* strconv_cdata(char_t* s, char_t endch) |
| 1848 | { |
| 1849 | gap g; |
| 1850 | |
| 1851 | while (true) |
| 1852 | { |
| 1853 | while (!PUGI__IS_CHARTYPE(*s, ct_parse_cdata)) ++s; |
| 1854 | |
| 1855 | if (*s == '\r') // Either a single 0x0d or 0x0d 0x0a pair |
| 1856 | { |
| 1857 | *s++ = '\n'; // replace first one with 0x0a |
| 1858 | |
| 1859 | if (*s == '\n') g.push(s, 1); |
| 1860 | } |
| 1861 | else if (s[0] == ']' && s[1] == ']' && ENDSWITH(s[2], '>')) // CDATA ends here |
| 1862 | { |
| 1863 | *g.flush(s) = 0; |
| 1864 | |
| 1865 | return s + 1; |
| 1866 | } |
| 1867 | else if (*s == 0) |
| 1868 | { |
| 1869 | return 0; |
| 1870 | } |
| 1871 | else ++s; |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | typedef char_t* (*strconv_pcdata_t)(char_t*); |
| 1876 | |
| 1877 | template <typename opt_trim, typename opt_eol, typename opt_escape> struct strconv_pcdata_impl |
| 1878 | { |
| 1879 | static char_t* parse(char_t* s) |
| 1880 | { |
| 1881 | gap g; |
| 1882 | |
| 1883 | char_t* begin = s; |
| 1884 | |
| 1885 | while (true) |
| 1886 | { |
| 1887 | while (!PUGI__IS_CHARTYPE(*s, ct_parse_pcdata)) ++s; |
| 1888 | |
| 1889 | if (*s == '<') // PCDATA ends here |
| 1890 | { |
| 1891 | char_t* end = g.flush(s); |
| 1892 | |
| 1893 | if (opt_trim::value) |
| 1894 | while (end > begin && PUGI__IS_CHARTYPE(end[-1], ct_space)) |
| 1895 | --end; |
| 1896 | |
| 1897 | *end = 0; |
| 1898 | |
| 1899 | return s + 1; |
| 1900 | } |
| 1901 | else if (opt_eol::value && *s == '\r') // Either a single 0x0d or 0x0d 0x0a pair |
| 1902 | { |
| 1903 | *s++ = '\n'; // replace first one with 0x0a |
| 1904 | |
| 1905 | if (*s == '\n') g.push(s, 1); |
| 1906 | } |
| 1907 | else if (opt_escape::value && *s == '&') |
| 1908 | { |
| 1909 | s = strconv_escape(s, g); |
| 1910 | } |
| 1911 | else if (*s == 0) |
| 1912 | { |
| 1913 | char_t* end = g.flush(s); |
| 1914 | |
| 1915 | if (opt_trim::value) |
| 1916 | while (end > begin && PUGI__IS_CHARTYPE(end[-1], ct_space)) |
| 1917 | --end; |
| 1918 | |
| 1919 | *end = 0; |
| 1920 | |
| 1921 | return s; |
| 1922 | } |
| 1923 | else ++s; |
| 1924 | } |
| 1925 | } |
| 1926 | }; |
| 1927 | |
| 1928 | PUGI__FN strconv_pcdata_t get_strconv_pcdata(unsigned int optmask) |
| 1929 | { |
| 1930 | PUGI__STATIC_ASSERT(parse_escapes == 0x10 && parse_eol == 0x20 && parse_trim_pcdata == 0x0800); |
| 1931 | |
| 1932 | switch (((optmask >> 4) & 3) | ((optmask >> 9) & 4)) // get bitmask for flags (eol escapes trim) |
| 1933 | { |
| 1934 | case 0: return strconv_pcdata_impl<opt_false, opt_false, opt_false>::parse; |
| 1935 | case 1: return strconv_pcdata_impl<opt_false, opt_false, opt_true>::parse; |
| 1936 | case 2: return strconv_pcdata_impl<opt_false, opt_true, opt_false>::parse; |
| 1937 | case 3: return strconv_pcdata_impl<opt_false, opt_true, opt_true>::parse; |
| 1938 | case 4: return strconv_pcdata_impl<opt_true, opt_false, opt_false>::parse; |
| 1939 | case 5: return strconv_pcdata_impl<opt_true, opt_false, opt_true>::parse; |
| 1940 | case 6: return strconv_pcdata_impl<opt_true, opt_true, opt_false>::parse; |
| 1941 | case 7: return strconv_pcdata_impl<opt_true, opt_true, opt_true>::parse; |
| 1942 | default: assert(false); return 0; // should not get here |
| 1943 | } |
| 1944 | } |
| 1945 | |
| 1946 | typedef char_t* (*strconv_attribute_t)(char_t*, char_t); |
| 1947 | |
| 1948 | template <typename opt_escape> struct strconv_attribute_impl |
| 1949 | { |
| 1950 | static char_t* parse_wnorm(char_t* s, char_t end_quote) |
| 1951 | { |
| 1952 | gap g; |
| 1953 | |
| 1954 | // trim leading whitespaces |
| 1955 | if (PUGI__IS_CHARTYPE(*s, ct_space)) |
| 1956 | { |
| 1957 | char_t* str = s; |
| 1958 | |
| 1959 | do ++str; |
| 1960 | while (PUGI__IS_CHARTYPE(*str, ct_space)); |
| 1961 | |
| 1962 | g.push(s, str - s); |
| 1963 | } |
| 1964 | |
| 1965 | while (true) |
| 1966 | { |
| 1967 | while (!PUGI__IS_CHARTYPE(*s, ct_parse_attr_ws | ct_space)) ++s; |
| 1968 | |
| 1969 | if (*s == end_quote) |
| 1970 | { |
| 1971 | char_t* str = g.flush(s); |
| 1972 | |
| 1973 | do *str-- = 0; |
| 1974 | while (PUGI__IS_CHARTYPE(*str, ct_space)); |
| 1975 | |
| 1976 | return s + 1; |
| 1977 | } |
| 1978 | else if (PUGI__IS_CHARTYPE(*s, ct_space)) |
| 1979 | { |
| 1980 | *s++ = ' '; |
| 1981 | |
| 1982 | if (PUGI__IS_CHARTYPE(*s, ct_space)) |
| 1983 | { |
| 1984 | char_t* str = s + 1; |
| 1985 | while (PUGI__IS_CHARTYPE(*str, ct_space)) ++str; |
| 1986 | |
| 1987 | g.push(s, str - s); |
| 1988 | } |
| 1989 | } |
| 1990 | else if (opt_escape::value && *s == '&') |
| 1991 | { |
| 1992 | s = strconv_escape(s, g); |
| 1993 | } |
| 1994 | else if (!*s) |
| 1995 | { |
| 1996 | return 0; |
| 1997 | } |
| 1998 | else ++s; |
| 1999 | } |
| 2000 | } |
| 2001 | |
| 2002 | static char_t* parse_wconv(char_t* s, char_t end_quote) |
| 2003 | { |
| 2004 | gap g; |
| 2005 | |
| 2006 | while (true) |
| 2007 | { |
| 2008 | while (!PUGI__IS_CHARTYPE(*s, ct_parse_attr_ws)) ++s; |
| 2009 | |
| 2010 | if (*s == end_quote) |
| 2011 | { |
| 2012 | *g.flush(s) = 0; |
| 2013 | |
| 2014 | return s + 1; |
| 2015 | } |
| 2016 | else if (PUGI__IS_CHARTYPE(*s, ct_space)) |
| 2017 | { |
| 2018 | if (*s == '\r') |
| 2019 | { |
| 2020 | *s++ = ' '; |
| 2021 | |
| 2022 | if (*s == '\n') g.push(s, 1); |
| 2023 | } |
| 2024 | else *s++ = ' '; |
| 2025 | } |
| 2026 | else if (opt_escape::value && *s == '&') |
| 2027 | { |
| 2028 | s = strconv_escape(s, g); |
| 2029 | } |
| 2030 | else if (!*s) |
| 2031 | { |
| 2032 | return 0; |
| 2033 | } |
| 2034 | else ++s; |
| 2035 | } |
| 2036 | } |
| 2037 | |
| 2038 | static char_t* parse_eol(char_t* s, char_t end_quote) |
| 2039 | { |
| 2040 | gap g; |
| 2041 | |
| 2042 | while (true) |
| 2043 | { |
| 2044 | while (!PUGI__IS_CHARTYPE(*s, ct_parse_attr)) ++s; |
| 2045 | |
| 2046 | if (*s == end_quote) |
| 2047 | { |
| 2048 | *g.flush(s) = 0; |
| 2049 | |
| 2050 | return s + 1; |
| 2051 | } |
| 2052 | else if (*s == '\r') |
| 2053 | { |
| 2054 | *s++ = '\n'; |
| 2055 | |
| 2056 | if (*s == '\n') g.push(s, 1); |
| 2057 | } |
| 2058 | else if (opt_escape::value && *s == '&') |
| 2059 | { |
| 2060 | s = strconv_escape(s, g); |
| 2061 | } |
| 2062 | else if (!*s) |
| 2063 | { |
| 2064 | return 0; |
| 2065 | } |
| 2066 | else ++s; |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | static char_t* parse_simple(char_t* s, char_t end_quote) |
| 2071 | { |
| 2072 | gap g; |
| 2073 | |
| 2074 | while (true) |
| 2075 | { |
| 2076 | while (!PUGI__IS_CHARTYPE(*s, ct_parse_attr)) ++s; |
| 2077 | |
| 2078 | if (*s == end_quote) |
| 2079 | { |
| 2080 | *g.flush(s) = 0; |
| 2081 | |
| 2082 | return s + 1; |
| 2083 | } |
| 2084 | else if (opt_escape::value && *s == '&') |
| 2085 | { |
| 2086 | s = strconv_escape(s, g); |
| 2087 | } |
| 2088 | else if (!*s) |
| 2089 | { |
| 2090 | return 0; |
| 2091 | } |
| 2092 | else ++s; |
| 2093 | } |
| 2094 | } |
| 2095 | }; |
| 2096 | |
| 2097 | PUGI__FN strconv_attribute_t get_strconv_attribute(unsigned int optmask) |
| 2098 | { |
| 2099 | PUGI__STATIC_ASSERT(parse_escapes == 0x10 && parse_eol == 0x20 && parse_wconv_attribute == 0x40 && parse_wnorm_attribute == 0x80); |
| 2100 | |
| 2101 | switch ((optmask >> 4) & 15) // get bitmask for flags (wconv wnorm eol escapes) |
| 2102 | { |
| 2103 | case 0: return strconv_attribute_impl<opt_false>::parse_simple; |
| 2104 | case 1: return strconv_attribute_impl<opt_true>::parse_simple; |
| 2105 | case 2: return strconv_attribute_impl<opt_false>::parse_eol; |
| 2106 | case 3: return strconv_attribute_impl<opt_true>::parse_eol; |
| 2107 | case 4: return strconv_attribute_impl<opt_false>::parse_wconv; |
| 2108 | case 5: return strconv_attribute_impl<opt_true>::parse_wconv; |
| 2109 | case 6: return strconv_attribute_impl<opt_false>::parse_wconv; |
| 2110 | case 7: return strconv_attribute_impl<opt_true>::parse_wconv; |
| 2111 | case 8: return strconv_attribute_impl<opt_false>::parse_wnorm; |
| 2112 | case 9: return strconv_attribute_impl<opt_true>::parse_wnorm; |
| 2113 | case 10: return strconv_attribute_impl<opt_false>::parse_wnorm; |
| 2114 | case 11: return strconv_attribute_impl<opt_true>::parse_wnorm; |
| 2115 | case 12: return strconv_attribute_impl<opt_false>::parse_wnorm; |
| 2116 | case 13: return strconv_attribute_impl<opt_true>::parse_wnorm; |
| 2117 | case 14: return strconv_attribute_impl<opt_false>::parse_wnorm; |
| 2118 | case 15: return strconv_attribute_impl<opt_true>::parse_wnorm; |
| 2119 | default: assert(false); return 0; // should not get here |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | inline xml_parse_result make_parse_result(xml_parse_status status, ptrdiff_t offset = 0) |
| 2124 | { |
| 2125 | xml_parse_result result; |
| 2126 | result.status = status; |
| 2127 | result.offset = offset; |
| 2128 | |
| 2129 | return result; |
| 2130 | } |
| 2131 | |
| 2132 | struct xml_parser |
| 2133 | { |
| 2134 | xml_allocator alloc; |
| 2135 | char_t* error_offset; |
| 2136 | xml_parse_status error_status; |
| 2137 | |
| 2138 | // Parser utilities. |
| 2139 | #define PUGI__SKIPWS() { while (PUGI__IS_CHARTYPE(*s, ct_space)) ++s; } |
| 2140 | #define PUGI__OPTSET(OPT) ( optmsk & (OPT) ) |
| 2141 | #define PUGI__PUSHNODE(TYPE) { cursor = append_node(cursor, alloc, TYPE); if (!cursor) PUGI__THROW_ERROR(status_out_of_memory, s); } |
| 2142 | #define PUGI__POPNODE() { cursor = cursor->parent; } |
| 2143 | #define PUGI__SCANFOR(X) { while (*s != 0 && !(X)) ++s; } |
| 2144 | #define PUGI__SCANWHILE(X) { while ((X)) ++s; } |
| 2145 | #define PUGI__ENDSEG() { ch = *s; *s = 0; ++s; } |
| 2146 | #define PUGI__THROW_ERROR(err, m) return error_offset = m, error_status = err, static_cast<char_t*>(0) |
| 2147 | #define PUGI__CHECK_ERROR(err, m) { if (*s == 0) PUGI__THROW_ERROR(err, m); } |
| 2148 | |
| 2149 | xml_parser(const xml_allocator& alloc_): alloc(alloc_), error_offset(0), error_status(status_ok) |
| 2150 | { |
| 2151 | } |
| 2152 | |
| 2153 | // DOCTYPE consists of nested sections of the following possible types: |
| 2154 | // <!-- ... -->, <? ... ?>, "...", '...' |
| 2155 | // <![...]]> |
| 2156 | // <!...> |
| 2157 | // First group can not contain nested groups |
| 2158 | // Second group can contain nested groups of the same type |
| 2159 | // Third group can contain all other groups |
| 2160 | char_t* parse_doctype_primitive(char_t* s) |
| 2161 | { |
| 2162 | if (*s == '"' || *s == '\'') |
| 2163 | { |
| 2164 | // quoted string |
| 2165 | char_t ch = *s++; |
| 2166 | PUGI__SCANFOR(*s == ch); |
| 2167 | if (!*s) PUGI__THROW_ERROR(status_bad_doctype, s); |
| 2168 | |
| 2169 | s++; |
| 2170 | } |
| 2171 | else if (s[0] == '<' && s[1] == '?') |
| 2172 | { |
| 2173 | // <? ... ?> |
| 2174 | s += 2; |
| 2175 | PUGI__SCANFOR(s[0] == '?' && s[1] == '>'); // no need for ENDSWITH because ?> can't terminate proper doctype |
| 2176 | if (!*s) PUGI__THROW_ERROR(status_bad_doctype, s); |
| 2177 | |
| 2178 | s += 2; |
| 2179 | } |
| 2180 | else if (s[0] == '<' && s[1] == '!' && s[2] == '-' && s[3] == '-') |
| 2181 | { |
| 2182 | s += 4; |
| 2183 | PUGI__SCANFOR(s[0] == '-' && s[1] == '-' && s[2] == '>'); // no need for ENDSWITH because --> can't terminate proper doctype |
| 2184 | if (!*s) PUGI__THROW_ERROR(status_bad_doctype, s); |
| 2185 | |
| 2186 | s += 4; |
| 2187 | } |
| 2188 | else PUGI__THROW_ERROR(status_bad_doctype, s); |
| 2189 | |
| 2190 | return s; |
| 2191 | } |
| 2192 | |
| 2193 | char_t* parse_doctype_ignore(char_t* s) |
| 2194 | { |
| 2195 | assert(s[0] == '<' && s[1] == '!' && s[2] == '['); |
| 2196 | s++; |
| 2197 | |
| 2198 | while (*s) |
| 2199 | { |
| 2200 | if (s[0] == '<' && s[1] == '!' && s[2] == '[') |
| 2201 | { |
| 2202 | // nested ignore section |
| 2203 | s = parse_doctype_ignore(s); |
| 2204 | if (!s) return s; |
| 2205 | } |
| 2206 | else if (s[0] == ']' && s[1] == ']' && s[2] == '>') |
| 2207 | { |
| 2208 | // ignore section end |
| 2209 | s += 3; |
| 2210 | |
| 2211 | return s; |
| 2212 | } |
| 2213 | else s++; |
| 2214 | } |
| 2215 | |
| 2216 | PUGI__THROW_ERROR(status_bad_doctype, s); |
| 2217 | } |
| 2218 | |
| 2219 | char_t* parse_doctype_group(char_t* s, char_t endch, bool toplevel) |
| 2220 | { |
| 2221 | assert((s[0] == '<' || s[0] == 0) && s[1] == '!'); |
| 2222 | s++; |
| 2223 | |
| 2224 | while (*s) |
| 2225 | { |
| 2226 | if (s[0] == '<' && s[1] == '!' && s[2] != '-') |
| 2227 | { |
| 2228 | if (s[2] == '[') |
| 2229 | { |
| 2230 | // ignore |
| 2231 | s = parse_doctype_ignore(s); |
| 2232 | if (!s) return s; |
| 2233 | } |
| 2234 | else |
| 2235 | { |
| 2236 | // some control group |
| 2237 | s = parse_doctype_group(s, endch, false); |
| 2238 | if (!s) return s; |
| 2239 | |
| 2240 | // skip > |
| 2241 | assert(*s == '>'); |
| 2242 | s++; |
| 2243 | } |
| 2244 | } |
| 2245 | else if (s[0] == '<' || s[0] == '"' || s[0] == '\'') |
| 2246 | { |
| 2247 | // unknown tag (forbidden), or some primitive group |
| 2248 | s = parse_doctype_primitive(s); |
| 2249 | if (!s) return s; |
| 2250 | } |
| 2251 | else if (*s == '>') |
| 2252 | { |
| 2253 | return s; |
| 2254 | } |
| 2255 | else s++; |
| 2256 | } |
| 2257 | |
| 2258 | if (!toplevel || endch != '>') PUGI__THROW_ERROR(status_bad_doctype, s); |
| 2259 | |
| 2260 | return s; |
| 2261 | } |
| 2262 | |
| 2263 | char_t* parse_exclamation(char_t* s, xml_node_struct* cursor, unsigned int optmsk, char_t endch) |
| 2264 | { |
| 2265 | // parse node contents, starting with exclamation mark |
| 2266 | ++s; |
| 2267 | |
| 2268 | if (*s == '-') // '<!-...' |
| 2269 | { |
| 2270 | ++s; |
| 2271 | |
| 2272 | if (*s == '-') // '<!--...' |
| 2273 | { |
| 2274 | ++s; |
| 2275 | |
| 2276 | if (PUGI__OPTSET(parse_comments)) |
| 2277 | { |
| 2278 | PUGI__PUSHNODE(node_comment); // Append a new node on the tree. |
| 2279 | cursor->value = s; // Save the offset. |
| 2280 | } |
| 2281 | |
| 2282 | if (PUGI__OPTSET(parse_eol) && PUGI__OPTSET(parse_comments)) |
| 2283 | { |
| 2284 | s = strconv_comment(s, endch); |
| 2285 | |
| 2286 | if (!s) PUGI__THROW_ERROR(status_bad_comment, cursor->value); |
| 2287 | } |
| 2288 | else |
| 2289 | { |
| 2290 | // Scan for terminating '-->'. |
| 2291 | PUGI__SCANFOR(s[0] == '-' && s[1] == '-' && ENDSWITH(s[2], '>')); |
| 2292 | PUGI__CHECK_ERROR(status_bad_comment, s); |
| 2293 | |
| 2294 | if (PUGI__OPTSET(parse_comments)) |
| 2295 | *s = 0; // Zero-terminate this segment at the first terminating '-'. |
| 2296 | |
| 2297 | s += (s[2] == '>' ? 3 : 2); // Step over the '\0->'. |
| 2298 | } |
| 2299 | } |
| 2300 | else PUGI__THROW_ERROR(status_bad_comment, s); |
| 2301 | } |
| 2302 | else if (*s == '[') |
| 2303 | { |
| 2304 | // '<![CDATA[...' |
| 2305 | if (*++s=='C' && *++s=='D' && *++s=='A' && *++s=='T' && *++s=='A' && *++s == '[') |
| 2306 | { |
| 2307 | ++s; |
| 2308 | |
| 2309 | if (PUGI__OPTSET(parse_cdata)) |
| 2310 | { |
| 2311 | PUGI__PUSHNODE(node_cdata); // Append a new node on the tree. |
| 2312 | cursor->value = s; // Save the offset. |
| 2313 | |
| 2314 | if (PUGI__OPTSET(parse_eol)) |
| 2315 | { |
| 2316 | s = strconv_cdata(s, endch); |
| 2317 | |
| 2318 | if (!s) PUGI__THROW_ERROR(status_bad_cdata, cursor->value); |
| 2319 | } |
| 2320 | else |
| 2321 | { |
| 2322 | // Scan for terminating ']]>'. |
| 2323 | PUGI__SCANFOR(s[0] == ']' && s[1] == ']' && ENDSWITH(s[2], '>')); |
| 2324 | PUGI__CHECK_ERROR(status_bad_cdata, s); |
| 2325 | |
| 2326 | *s++ = 0; // Zero-terminate this segment. |
| 2327 | } |
| 2328 | } |
| 2329 | else // Flagged for discard, but we still have to scan for the terminator. |
| 2330 | { |
| 2331 | // Scan for terminating ']]>'. |
| 2332 | PUGI__SCANFOR(s[0] == ']' && s[1] == ']' && ENDSWITH(s[2], '>')); |
| 2333 | PUGI__CHECK_ERROR(status_bad_cdata, s); |
| 2334 | |
| 2335 | ++s; |
| 2336 | } |
| 2337 | |
| 2338 | s += (s[1] == '>' ? 2 : 1); // Step over the last ']>'. |
| 2339 | } |
| 2340 | else PUGI__THROW_ERROR(status_bad_cdata, s); |
| 2341 | } |
| 2342 | else if (s[0] == 'D' && s[1] == 'O' && s[2] == 'C' && s[3] == 'T' && s[4] == 'Y' && s[5] == 'P' && ENDSWITH(s[6], 'E')) |
| 2343 | { |
| 2344 | s -= 2; |
| 2345 | |
| 2346 | if (cursor->parent) PUGI__THROW_ERROR(status_bad_doctype, s); |
| 2347 | |
| 2348 | char_t* mark = s + 9; |
| 2349 | |
| 2350 | s = parse_doctype_group(s, endch, true); |
| 2351 | if (!s) return s; |
| 2352 | |
| 2353 | assert((*s == 0 && endch == '>') || *s == '>'); |
| 2354 | if (*s) *s++ = 0; |
| 2355 | |
| 2356 | if (PUGI__OPTSET(parse_doctype)) |
| 2357 | { |
| 2358 | while (PUGI__IS_CHARTYPE(*mark, ct_space)) ++mark; |
| 2359 | |
| 2360 | PUGI__PUSHNODE(node_doctype); |
| 2361 | |
| 2362 | cursor->value = mark; |
| 2363 | |
| 2364 | PUGI__POPNODE(); |
| 2365 | } |
| 2366 | } |
| 2367 | else if (*s == 0 && endch == '-') PUGI__THROW_ERROR(status_bad_comment, s); |
| 2368 | else if (*s == 0 && endch == '[') PUGI__THROW_ERROR(status_bad_cdata, s); |
| 2369 | else PUGI__THROW_ERROR(status_unrecognized_tag, s); |
| 2370 | |
| 2371 | return s; |
| 2372 | } |
| 2373 | |
| 2374 | char_t* parse_question(char_t* s, xml_node_struct*& ref_cursor, unsigned int optmsk, char_t endch) |
| 2375 | { |
| 2376 | // load into registers |
| 2377 | xml_node_struct* cursor = ref_cursor; |
| 2378 | char_t ch = 0; |
| 2379 | |
| 2380 | // parse node contents, starting with question mark |
| 2381 | ++s; |
| 2382 | |
| 2383 | // read PI target |
| 2384 | char_t* target = s; |
| 2385 | |
| 2386 | if (!PUGI__IS_CHARTYPE(*s, ct_start_symbol)) PUGI__THROW_ERROR(status_bad_pi, s); |
| 2387 | |
| 2388 | PUGI__SCANWHILE(PUGI__IS_CHARTYPE(*s, ct_symbol)); |
| 2389 | PUGI__CHECK_ERROR(status_bad_pi, s); |
| 2390 | |
| 2391 | // determine node type; stricmp / strcasecmp is not portable |
| 2392 | bool declaration = (target[0] | ' ') == 'x' && (target[1] | ' ') == 'm' && (target[2] | ' ') == 'l' && target + 3 == s; |
| 2393 | |
| 2394 | if (declaration ? PUGI__OPTSET(parse_declaration) : PUGI__OPTSET(parse_pi)) |
| 2395 | { |
| 2396 | if (declaration) |
| 2397 | { |
| 2398 | // disallow non top-level declarations |
| 2399 | if (cursor->parent) PUGI__THROW_ERROR(status_bad_pi, s); |
| 2400 | |
| 2401 | PUGI__PUSHNODE(node_declaration); |
| 2402 | } |
| 2403 | else |
| 2404 | { |
| 2405 | PUGI__PUSHNODE(node_pi); |
| 2406 | } |
| 2407 | |
| 2408 | cursor->name = target; |
| 2409 | |
| 2410 | PUGI__ENDSEG(); |
| 2411 | |
| 2412 | // parse value/attributes |
| 2413 | if (ch == '?') |
| 2414 | { |
| 2415 | // empty node |
| 2416 | if (!ENDSWITH(*s, '>')) PUGI__THROW_ERROR(status_bad_pi, s); |
| 2417 | s += (*s == '>'); |
| 2418 | |
| 2419 | PUGI__POPNODE(); |
| 2420 | } |
| 2421 | else if (PUGI__IS_CHARTYPE(ch, ct_space)) |
| 2422 | { |
| 2423 | PUGI__SKIPWS(); |
| 2424 | |
| 2425 | // scan for tag end |
| 2426 | char_t* value = s; |
| 2427 | |
| 2428 | PUGI__SCANFOR(s[0] == '?' && ENDSWITH(s[1], '>')); |
| 2429 | PUGI__CHECK_ERROR(status_bad_pi, s); |
| 2430 | |
| 2431 | if (declaration) |
| 2432 | { |
| 2433 | // replace ending ? with / so that 'element' terminates properly |
| 2434 | *s = '/'; |
| 2435 | |
| 2436 | // we exit from this function with cursor at node_declaration, which is a signal to parse() to go to LOC_ATTRIBUTES |
| 2437 | s = value; |
| 2438 | } |
| 2439 | else |
| 2440 | { |
| 2441 | // store value and step over > |
| 2442 | cursor->value = value; |
| 2443 | PUGI__POPNODE(); |
| 2444 | |
| 2445 | PUGI__ENDSEG(); |
| 2446 | |
| 2447 | s += (*s == '>'); |
| 2448 | } |
| 2449 | } |
| 2450 | else PUGI__THROW_ERROR(status_bad_pi, s); |
| 2451 | } |
| 2452 | else |
| 2453 | { |
| 2454 | // scan for tag end |
| 2455 | PUGI__SCANFOR(s[0] == '?' && ENDSWITH(s[1], '>')); |
| 2456 | PUGI__CHECK_ERROR(status_bad_pi, s); |
| 2457 | |
| 2458 | s += (s[1] == '>' ? 2 : 1); |
| 2459 | } |
| 2460 | |
| 2461 | // store from registers |
| 2462 | ref_cursor = cursor; |
| 2463 | |
| 2464 | return s; |
| 2465 | } |
| 2466 | |
| 2467 | char_t* parse_tree(char_t* s, xml_node_struct* root, unsigned int optmsk, char_t endch) |
| 2468 | { |
| 2469 | strconv_attribute_t strconv_attribute = get_strconv_attribute(optmsk); |
| 2470 | strconv_pcdata_t strconv_pcdata = get_strconv_pcdata(optmsk); |
| 2471 | |
| 2472 | char_t ch = 0; |
| 2473 | xml_node_struct* cursor = root; |
| 2474 | char_t* mark = s; |
| 2475 | |
| 2476 | while (*s != 0) |
| 2477 | { |
| 2478 | if (*s == '<') |
| 2479 | { |
| 2480 | ++s; |
| 2481 | |
| 2482 | LOC_TAG: |
| 2483 | if (PUGI__IS_CHARTYPE(*s, ct_start_symbol)) // '<#...' |
| 2484 | { |
| 2485 | PUGI__PUSHNODE(node_element); // Append a new node to the tree. |
| 2486 | |
| 2487 | cursor->name = s; |
| 2488 | |
| 2489 | PUGI__SCANWHILE(PUGI__IS_CHARTYPE(*s, ct_symbol)); // Scan for a terminator. |
| 2490 | PUGI__ENDSEG(); // Save char in 'ch', terminate & step over. |
| 2491 | |
| 2492 | if (ch == '>') |
| 2493 | { |
| 2494 | // end of tag |
| 2495 | } |
| 2496 | else if (PUGI__IS_CHARTYPE(ch, ct_space)) |
| 2497 | { |
| 2498 | LOC_ATTRIBUTES: |
| 2499 | while (true) |
| 2500 | { |
| 2501 | PUGI__SKIPWS(); // Eat any whitespace. |
| 2502 | |
| 2503 | if (PUGI__IS_CHARTYPE(*s, ct_start_symbol)) // <... #... |
| 2504 | { |
| 2505 | xml_attribute_struct* a = append_attribute_ll(cursor, alloc); // Make space for this attribute. |
| 2506 | if (!a) PUGI__THROW_ERROR(status_out_of_memory, s); |
| 2507 | |
| 2508 | a->name = s; // Save the offset. |
| 2509 | |
| 2510 | PUGI__SCANWHILE(PUGI__IS_CHARTYPE(*s, ct_symbol)); // Scan for a terminator. |
| 2511 | PUGI__CHECK_ERROR(status_bad_attribute, s); //$ redundant, left for performance |
| 2512 | |
| 2513 | PUGI__ENDSEG(); // Save char in 'ch', terminate & step over. |
| 2514 | PUGI__CHECK_ERROR(status_bad_attribute, s); //$ redundant, left for performance |
| 2515 | |
| 2516 | if (PUGI__IS_CHARTYPE(ch, ct_space)) |
| 2517 | { |
| 2518 | PUGI__SKIPWS(); // Eat any whitespace. |
| 2519 | PUGI__CHECK_ERROR(status_bad_attribute, s); //$ redundant, left for performance |
| 2520 | |
| 2521 | ch = *s; |
| 2522 | ++s; |
| 2523 | } |
| 2524 | |
| 2525 | if (ch == '=') // '<... #=...' |
| 2526 | { |
| 2527 | PUGI__SKIPWS(); // Eat any whitespace. |
| 2528 | |
| 2529 | if (*s == '"' || *s == '\'') // '<... #="...' |
| 2530 | { |
| 2531 | ch = *s; // Save quote char to avoid breaking on "''" -or- '""'. |
| 2532 | ++s; // Step over the quote. |
| 2533 | a->value = s; // Save the offset. |
| 2534 | |
| 2535 | s = strconv_attribute(s, ch); |
| 2536 | |
| 2537 | if (!s) PUGI__THROW_ERROR(status_bad_attribute, a->value); |
| 2538 | |
| 2539 | // After this line the loop continues from the start; |
| 2540 | // Whitespaces, / and > are ok, symbols and EOF are wrong, |
| 2541 | // everything else will be detected |
| 2542 | if (PUGI__IS_CHARTYPE(*s, ct_start_symbol)) PUGI__THROW_ERROR(status_bad_attribute, s); |
| 2543 | } |
| 2544 | else PUGI__THROW_ERROR(status_bad_attribute, s); |
| 2545 | } |
| 2546 | else PUGI__THROW_ERROR(status_bad_attribute, s); |
| 2547 | } |
| 2548 | else if (*s == '/') |
| 2549 | { |
| 2550 | ++s; |
| 2551 | |
| 2552 | if (*s == '>') |
| 2553 | { |
| 2554 | PUGI__POPNODE(); |
| 2555 | s++; |
| 2556 | break; |
| 2557 | } |
| 2558 | else if (*s == 0 && endch == '>') |
| 2559 | { |
| 2560 | PUGI__POPNODE(); |
| 2561 | break; |
| 2562 | } |
| 2563 | else PUGI__THROW_ERROR(status_bad_start_element, s); |
| 2564 | } |
| 2565 | else if (*s == '>') |
| 2566 | { |
| 2567 | ++s; |
| 2568 | |
| 2569 | break; |
| 2570 | } |
| 2571 | else if (*s == 0 && endch == '>') |
| 2572 | { |
| 2573 | break; |
| 2574 | } |
| 2575 | else PUGI__THROW_ERROR(status_bad_start_element, s); |
| 2576 | } |
| 2577 | |
| 2578 | // !!! |
| 2579 | } |
| 2580 | else if (ch == '/') // '<#.../' |
| 2581 | { |
| 2582 | if (!ENDSWITH(*s, '>')) PUGI__THROW_ERROR(status_bad_start_element, s); |
| 2583 | |
| 2584 | PUGI__POPNODE(); // Pop. |
| 2585 | |
| 2586 | s += (*s == '>'); |
| 2587 | } |
| 2588 | else if (ch == 0) |
| 2589 | { |
| 2590 | // we stepped over null terminator, backtrack & handle closing tag |
| 2591 | --s; |
| 2592 | |
| 2593 | if (endch != '>') PUGI__THROW_ERROR(status_bad_start_element, s); |
| 2594 | } |
| 2595 | else PUGI__THROW_ERROR(status_bad_start_element, s); |
| 2596 | } |
| 2597 | else if (*s == '/') |
| 2598 | { |
| 2599 | ++s; |
| 2600 | |
| 2601 | char_t* name = cursor->name; |
| 2602 | if (!name) PUGI__THROW_ERROR(status_end_element_mismatch, s); |
| 2603 | |
| 2604 | while (PUGI__IS_CHARTYPE(*s, ct_symbol)) |
| 2605 | { |
| 2606 | if (*s++ != *name++) PUGI__THROW_ERROR(status_end_element_mismatch, s); |
| 2607 | } |
| 2608 | |
| 2609 | if (*name) |
| 2610 | { |
| 2611 | if (*s == 0 && name[0] == endch && name[1] == 0) PUGI__THROW_ERROR(status_bad_end_element, s); |
| 2612 | else PUGI__THROW_ERROR(status_end_element_mismatch, s); |
| 2613 | } |
| 2614 | |
| 2615 | PUGI__POPNODE(); // Pop. |
| 2616 | |
| 2617 | PUGI__SKIPWS(); |
| 2618 | |
| 2619 | if (*s == 0) |
| 2620 | { |
| 2621 | if (endch != '>') PUGI__THROW_ERROR(status_bad_end_element, s); |
| 2622 | } |
| 2623 | else |
| 2624 | { |
| 2625 | if (*s != '>') PUGI__THROW_ERROR(status_bad_end_element, s); |
| 2626 | ++s; |
| 2627 | } |
| 2628 | } |
| 2629 | else if (*s == '?') // '<?...' |
| 2630 | { |
| 2631 | s = parse_question(s, cursor, optmsk, endch); |
| 2632 | if (!s) return s; |
| 2633 | |
| 2634 | assert(cursor); |
| 2635 | if ((cursor->header & xml_memory_page_type_mask) + 1 == node_declaration) goto LOC_ATTRIBUTES; |
| 2636 | } |
| 2637 | else if (*s == '!') // '<!...' |
| 2638 | { |
| 2639 | s = parse_exclamation(s, cursor, optmsk, endch); |
| 2640 | if (!s) return s; |
| 2641 | } |
| 2642 | else if (*s == 0 && endch == '?') PUGI__THROW_ERROR(status_bad_pi, s); |
| 2643 | else PUGI__THROW_ERROR(status_unrecognized_tag, s); |
| 2644 | } |
| 2645 | else |
| 2646 | { |
| 2647 | mark = s; // Save this offset while searching for a terminator. |
| 2648 | |
| 2649 | PUGI__SKIPWS(); // Eat whitespace if no genuine PCDATA here. |
| 2650 | |
| 2651 | if (*s == '<' || !*s) |
| 2652 | { |
| 2653 | // We skipped some whitespace characters because otherwise we would take the tag branch instead of PCDATA one |
| 2654 | assert(mark != s); |
| 2655 | |
| 2656 | if (!PUGI__OPTSET(parse_ws_pcdata | parse_ws_pcdata_single) || PUGI__OPTSET(parse_trim_pcdata)) |
| 2657 | { |
| 2658 | continue; |
| 2659 | } |
| 2660 | else if (PUGI__OPTSET(parse_ws_pcdata_single)) |
| 2661 | { |
| 2662 | if (s[0] != '<' || s[1] != '/' || cursor->first_child) continue; |
| 2663 | } |
| 2664 | } |
| 2665 | |
| 2666 | if (!PUGI__OPTSET(parse_trim_pcdata)) |
| 2667 | s = mark; |
| 2668 | |
| 2669 | if (cursor->parent || PUGI__OPTSET(parse_fragment)) |
| 2670 | { |
| 2671 | PUGI__PUSHNODE(node_pcdata); // Append a new node on the tree. |
| 2672 | cursor->value = s; // Save the offset. |
| 2673 | |
| 2674 | s = strconv_pcdata(s); |
| 2675 | |
| 2676 | PUGI__POPNODE(); // Pop since this is a standalone. |
| 2677 | |
| 2678 | if (!*s) break; |
| 2679 | } |
| 2680 | else |
| 2681 | { |
| 2682 | PUGI__SCANFOR(*s == '<'); // '...<' |
| 2683 | if (!*s) break; |
| 2684 | |
| 2685 | ++s; |
| 2686 | } |
| 2687 | |
| 2688 | // We're after '<' |
| 2689 | goto LOC_TAG; |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | // check that last tag is closed |
| 2694 | if (cursor != root) PUGI__THROW_ERROR(status_end_element_mismatch, s); |
| 2695 | |
| 2696 | return s; |
| 2697 | } |
| 2698 | |
| 2699 | #ifdef PUGIXML_WCHAR_MODE |
| 2700 | static char_t* parse_skip_bom(char_t* s) |
| 2701 | { |
| 2702 | unsigned int bom = 0xfeff; |
| 2703 | return (s[0] == static_cast<wchar_t>(bom)) ? s + 1 : s; |
| 2704 | } |
| 2705 | #else |
| 2706 | static char_t* parse_skip_bom(char_t* s) |
| 2707 | { |
| 2708 | return (s[0] == '\xef' && s[1] == '\xbb' && s[2] == '\xbf') ? s + 3 : s; |
| 2709 | } |
| 2710 | #endif |
| 2711 | |
| 2712 | static bool has_element_node_siblings(xml_node_struct* node) |
| 2713 | { |
| 2714 | while (node) |
| 2715 | { |
| 2716 | xml_node_type type = static_cast<xml_node_type>((node->header & impl::xml_memory_page_type_mask) + 1); |
| 2717 | if (type == node_element) return true; |
| 2718 | |
| 2719 | node = node->next_sibling; |
| 2720 | } |
| 2721 | |
| 2722 | return false; |
| 2723 | } |
| 2724 | |
| 2725 | static xml_parse_result parse(char_t* buffer, size_t length, xml_document_struct* xmldoc, xml_node_struct* root, unsigned int optmsk) |
| 2726 | { |
| 2727 | // allocator object is a part of document object |
| 2728 | xml_allocator& alloc = *static_cast<xml_allocator*>(xmldoc); |
| 2729 | |
| 2730 | // early-out for empty documents |
| 2731 | if (length == 0) |
| 2732 | return make_parse_result(PUGI__OPTSET(parse_fragment) ? status_ok : status_no_document_element); |
| 2733 | |
| 2734 | // get last child of the root before parsing |
| 2735 | xml_node_struct* last_root_child = root->first_child ? root->first_child->prev_sibling_c : 0; |
| 2736 | |
| 2737 | // create parser on stack |
| 2738 | xml_parser parser(alloc); |
| 2739 | |
| 2740 | // save last character and make buffer zero-terminated (speeds up parsing) |
| 2741 | char_t endch = buffer[length - 1]; |
| 2742 | buffer[length - 1] = 0; |
| 2743 | |
| 2744 | // skip BOM to make sure it does not end up as part of parse output |
| 2745 | char_t* buffer_data = parse_skip_bom(buffer); |
| 2746 | |
| 2747 | // perform actual parsing |
| 2748 | parser.parse_tree(buffer_data, root, optmsk, endch); |
| 2749 | |
| 2750 | // update allocator state |
| 2751 | alloc = parser.alloc; |
| 2752 | |
| 2753 | xml_parse_result result = make_parse_result(parser.error_status, parser.error_offset ? parser.error_offset - buffer : 0); |
| 2754 | assert(result.offset >= 0 && static_cast<size_t>(result.offset) <= length); |
| 2755 | |
| 2756 | if (result) |
| 2757 | { |
| 2758 | // since we removed last character, we have to handle the only possible false positive (stray <) |
| 2759 | if (endch == '<') |
| 2760 | return make_parse_result(status_unrecognized_tag, length - 1); |
| 2761 | |
| 2762 | // check if there are any element nodes parsed |
| 2763 | xml_node_struct* first_root_child_parsed = last_root_child ? last_root_child->next_sibling : root->first_child; |
| 2764 | |
| 2765 | if (!PUGI__OPTSET(parse_fragment) && !has_element_node_siblings(first_root_child_parsed)) |
| 2766 | return make_parse_result(status_no_document_element, length - 1); |
| 2767 | } |
| 2768 | else |
| 2769 | { |
| 2770 | // roll back offset if it occurs on a null terminator in the source buffer |
| 2771 | if (result.offset > 0 && static_cast<size_t>(result.offset) == length - 1 && endch == 0) |
| 2772 | result.offset--; |
| 2773 | } |
| 2774 | |
| 2775 | return result; |
| 2776 | } |
| 2777 | }; |
| 2778 | |
| 2779 | // Output facilities |
| 2780 | PUGI__FN xml_encoding get_write_native_encoding() |
| 2781 | { |
| 2782 | #ifdef PUGIXML_WCHAR_MODE |
| 2783 | return get_wchar_encoding(); |
| 2784 | #else |
| 2785 | return encoding_utf8; |
| 2786 | #endif |
| 2787 | } |
| 2788 | |
| 2789 | PUGI__FN xml_encoding get_write_encoding(xml_encoding encoding) |
| 2790 | { |
| 2791 | // replace wchar encoding with utf implementation |
| 2792 | if (encoding == encoding_wchar) return get_wchar_encoding(); |
| 2793 | |
| 2794 | // replace utf16 encoding with utf16 with specific endianness |
| 2795 | if (encoding == encoding_utf16) return is_little_endian() ? encoding_utf16_le : encoding_utf16_be; |
| 2796 | |
| 2797 | // replace utf32 encoding with utf32 with specific endianness |
| 2798 | if (encoding == encoding_utf32) return is_little_endian() ? encoding_utf32_le : encoding_utf32_be; |
| 2799 | |
| 2800 | // only do autodetection if no explicit encoding is requested |
| 2801 | if (encoding != encoding_auto) return encoding; |
| 2802 | |
| 2803 | // assume utf8 encoding |
| 2804 | return encoding_utf8; |
| 2805 | } |
| 2806 | |
| 2807 | #ifdef PUGIXML_WCHAR_MODE |
| 2808 | PUGI__FN size_t get_valid_length(const char_t* data, size_t length) |
| 2809 | { |
| 2810 | assert(length > 0); |
| 2811 | |
| 2812 | // discard last character if it's the lead of a surrogate pair |
| 2813 | return (sizeof(wchar_t) == 2 && static_cast<unsigned int>(static_cast<uint16_t>(data[length - 1]) - 0xD800) < 0x400) ? length - 1 : length; |
| 2814 | } |
| 2815 | |
| 2816 | PUGI__FN size_t convert_buffer_output(char_t* r_char, uint8_t* r_u8, uint16_t* r_u16, uint32_t* r_u32, const char_t* data, size_t length, xml_encoding encoding) |
| 2817 | { |
| 2818 | // only endian-swapping is required |
| 2819 | if (need_endian_swap_utf(encoding, get_wchar_encoding())) |
| 2820 | { |
| 2821 | convert_wchar_endian_swap(r_char, data, length); |
| 2822 | |
| 2823 | return length * sizeof(char_t); |
| 2824 | } |
| 2825 | |
| 2826 | // convert to utf8 |
| 2827 | if (encoding == encoding_utf8) |
| 2828 | { |
| 2829 | uint8_t* dest = r_u8; |
| 2830 | uint8_t* end = utf_decoder<utf8_writer>::decode_wchar_block(data, length, dest); |
| 2831 | |
| 2832 | return static_cast<size_t>(end - dest); |
| 2833 | } |
| 2834 | |
| 2835 | // convert to utf16 |
| 2836 | if (encoding == encoding_utf16_be || encoding == encoding_utf16_le) |
| 2837 | { |
| 2838 | uint16_t* dest = r_u16; |
| 2839 | |
| 2840 | // convert to native utf16 |
| 2841 | uint16_t* end = utf_decoder<utf16_writer>::decode_wchar_block(data, length, dest); |
| 2842 | |
| 2843 | // swap if necessary |
| 2844 | xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be; |
| 2845 | |
| 2846 | if (native_encoding != encoding) convert_utf_endian_swap(dest, dest, static_cast<size_t>(end - dest)); |
| 2847 | |
| 2848 | return static_cast<size_t>(end - dest) * sizeof(uint16_t); |
| 2849 | } |
| 2850 | |
| 2851 | // convert to utf32 |
| 2852 | if (encoding == encoding_utf32_be || encoding == encoding_utf32_le) |
| 2853 | { |
| 2854 | uint32_t* dest = r_u32; |
| 2855 | |
| 2856 | // convert to native utf32 |
| 2857 | uint32_t* end = utf_decoder<utf32_writer>::decode_wchar_block(data, length, dest); |
| 2858 | |
| 2859 | // swap if necessary |
| 2860 | xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be; |
| 2861 | |
| 2862 | if (native_encoding != encoding) convert_utf_endian_swap(dest, dest, static_cast<size_t>(end - dest)); |
| 2863 | |
| 2864 | return static_cast<size_t>(end - dest) * sizeof(uint32_t); |
| 2865 | } |
| 2866 | |
| 2867 | // convert to latin1 |
| 2868 | if (encoding == encoding_latin1) |
| 2869 | { |
| 2870 | uint8_t* dest = r_u8; |
| 2871 | uint8_t* end = utf_decoder<latin1_writer>::decode_wchar_block(data, length, dest); |
| 2872 | |
| 2873 | return static_cast<size_t>(end - dest); |
| 2874 | } |
| 2875 | |
| 2876 | assert(!"Invalid encoding"); |
| 2877 | return 0; |
| 2878 | } |
| 2879 | #else |
| 2880 | PUGI__FN size_t get_valid_length(const char_t* data, size_t length) |
| 2881 | { |
| 2882 | assert(length > 4); |
| 2883 | |
| 2884 | for (size_t i = 1; i <= 4; ++i) |
| 2885 | { |
| 2886 | uint8_t ch = static_cast<uint8_t>(data[length - i]); |
| 2887 | |
| 2888 | // either a standalone character or a leading one |
| 2889 | if ((ch & 0xc0) != 0x80) return length - i; |
| 2890 | } |
| 2891 | |
| 2892 | // there are four non-leading characters at the end, sequence tail is broken so might as well process the whole chunk |
| 2893 | return length; |
| 2894 | } |
| 2895 | |
| 2896 | PUGI__FN size_t convert_buffer_output(char_t* /* r_char */, uint8_t* r_u8, uint16_t* r_u16, uint32_t* r_u32, const char_t* data, size_t length, xml_encoding encoding) |
| 2897 | { |
| 2898 | if (encoding == encoding_utf16_be || encoding == encoding_utf16_le) |
| 2899 | { |
| 2900 | uint16_t* dest = r_u16; |
| 2901 | |
| 2902 | // convert to native utf16 |
| 2903 | uint16_t* end = utf_decoder<utf16_writer>::decode_utf8_block(reinterpret_cast<const uint8_t*>(data), length, dest); |
| 2904 | |
| 2905 | // swap if necessary |
| 2906 | xml_encoding native_encoding = is_little_endian() ? encoding_utf16_le : encoding_utf16_be; |
| 2907 | |
| 2908 | if (native_encoding != encoding) convert_utf_endian_swap(dest, dest, static_cast<size_t>(end - dest)); |
| 2909 | |
| 2910 | return static_cast<size_t>(end - dest) * sizeof(uint16_t); |
| 2911 | } |
| 2912 | |
| 2913 | if (encoding == encoding_utf32_be || encoding == encoding_utf32_le) |
| 2914 | { |
| 2915 | uint32_t* dest = r_u32; |
| 2916 | |
| 2917 | // convert to native utf32 |
| 2918 | uint32_t* end = utf_decoder<utf32_writer>::decode_utf8_block(reinterpret_cast<const uint8_t*>(data), length, dest); |
| 2919 | |
| 2920 | // swap if necessary |
| 2921 | xml_encoding native_encoding = is_little_endian() ? encoding_utf32_le : encoding_utf32_be; |
| 2922 | |
| 2923 | if (native_encoding != encoding) convert_utf_endian_swap(dest, dest, static_cast<size_t>(end - dest)); |
| 2924 | |
| 2925 | return static_cast<size_t>(end - dest) * sizeof(uint32_t); |
| 2926 | } |
| 2927 | |
| 2928 | if (encoding == encoding_latin1) |
| 2929 | { |
| 2930 | uint8_t* dest = r_u8; |
| 2931 | uint8_t* end = utf_decoder<latin1_writer>::decode_utf8_block(reinterpret_cast<const uint8_t*>(data), length, dest); |
| 2932 | |
| 2933 | return static_cast<size_t>(end - dest); |
| 2934 | } |
| 2935 | |
| 2936 | assert(!"Invalid encoding"); |
| 2937 | return 0; |
| 2938 | } |
| 2939 | #endif |
| 2940 | |
| 2941 | class xml_buffered_writer |
| 2942 | { |
| 2943 | xml_buffered_writer(const xml_buffered_writer&); |
| 2944 | xml_buffered_writer& operator=(const xml_buffered_writer&); |
| 2945 | |
| 2946 | public: |
| 2947 | xml_buffered_writer(xml_writer& writer_, xml_encoding user_encoding): writer(writer_), bufsize(0), encoding(get_write_encoding(user_encoding)) |
| 2948 | { |
| 2949 | PUGI__STATIC_ASSERT(bufcapacity >= 8); |
| 2950 | } |
| 2951 | |
| 2952 | ~xml_buffered_writer() |
| 2953 | { |
| 2954 | flush(); |
| 2955 | } |
| 2956 | |
| 2957 | void flush() |
| 2958 | { |
| 2959 | flush(buffer, bufsize); |
| 2960 | bufsize = 0; |
| 2961 | } |
| 2962 | |
| 2963 | void flush(const char_t* data, size_t size) |
| 2964 | { |
| 2965 | if (size == 0) return; |
| 2966 | |
| 2967 | // fast path, just write data |
| 2968 | if (encoding == get_write_native_encoding()) |
| 2969 | writer.write(data, size * sizeof(char_t)); |
| 2970 | else |
| 2971 | { |
| 2972 | // convert chunk |
| 2973 | size_t result = convert_buffer_output(scratch.data_char, scratch.data_u8, scratch.data_u16, scratch.data_u32, data, size, encoding); |
| 2974 | assert(result <= sizeof(scratch)); |
| 2975 | |
| 2976 | // write data |
| 2977 | writer.write(scratch.data_u8, result); |
| 2978 | } |
| 2979 | } |
| 2980 | |
| 2981 | void write(const char_t* data, size_t length) |
| 2982 | { |
| 2983 | if (bufsize + length > bufcapacity) |
| 2984 | { |
| 2985 | // flush the remaining buffer contents |
| 2986 | flush(); |
| 2987 | |
| 2988 | // handle large chunks |
| 2989 | if (length > bufcapacity) |
| 2990 | { |
| 2991 | if (encoding == get_write_native_encoding()) |
| 2992 | { |
| 2993 | // fast path, can just write data chunk |
| 2994 | writer.write(data, length * sizeof(char_t)); |
| 2995 | return; |
| 2996 | } |
| 2997 | |
| 2998 | // need to convert in suitable chunks |
| 2999 | while (length > bufcapacity) |
| 3000 | { |
| 3001 | // get chunk size by selecting such number of characters that are guaranteed to fit into scratch buffer |
| 3002 | // and form a complete codepoint sequence (i.e. discard start of last codepoint if necessary) |
| 3003 | size_t chunk_size = get_valid_length(data, bufcapacity); |
| 3004 | |
| 3005 | // convert chunk and write |
| 3006 | flush(data, chunk_size); |
| 3007 | |
| 3008 | // iterate |
| 3009 | data += chunk_size; |
| 3010 | length -= chunk_size; |
| 3011 | } |
| 3012 | |
| 3013 | // small tail is copied below |
| 3014 | bufsize = 0; |
| 3015 | } |
| 3016 | } |
| 3017 | |
| 3018 | memcpy(buffer + bufsize, data, length * sizeof(char_t)); |
| 3019 | bufsize += length; |
| 3020 | } |
| 3021 | |
| 3022 | void write(const char_t* data) |
| 3023 | { |
| 3024 | write(data, strlength(data)); |
| 3025 | } |
| 3026 | |
| 3027 | void write(char_t d0) |
| 3028 | { |
| 3029 | if (bufsize + 1 > bufcapacity) flush(); |
| 3030 | |
| 3031 | buffer[bufsize + 0] = d0; |
| 3032 | bufsize += 1; |
| 3033 | } |
| 3034 | |
| 3035 | void write(char_t d0, char_t d1) |
| 3036 | { |
| 3037 | if (bufsize + 2 > bufcapacity) flush(); |
| 3038 | |
| 3039 | buffer[bufsize + 0] = d0; |
| 3040 | buffer[bufsize + 1] = d1; |
| 3041 | bufsize += 2; |
| 3042 | } |
| 3043 | |
| 3044 | void write(char_t d0, char_t d1, char_t d2) |
| 3045 | { |
| 3046 | if (bufsize + 3 > bufcapacity) flush(); |
| 3047 | |
| 3048 | buffer[bufsize + 0] = d0; |
| 3049 | buffer[bufsize + 1] = d1; |
| 3050 | buffer[bufsize + 2] = d2; |
| 3051 | bufsize += 3; |
| 3052 | } |
| 3053 | |
| 3054 | void write(char_t d0, char_t d1, char_t d2, char_t d3) |
| 3055 | { |
| 3056 | if (bufsize + 4 > bufcapacity) flush(); |
| 3057 | |
| 3058 | buffer[bufsize + 0] = d0; |
| 3059 | buffer[bufsize + 1] = d1; |
| 3060 | buffer[bufsize + 2] = d2; |
| 3061 | buffer[bufsize + 3] = d3; |
| 3062 | bufsize += 4; |
| 3063 | } |
| 3064 | |
| 3065 | void write(char_t d0, char_t d1, char_t d2, char_t d3, char_t d4) |
| 3066 | { |
| 3067 | if (bufsize + 5 > bufcapacity) flush(); |
| 3068 | |
| 3069 | buffer[bufsize + 0] = d0; |
| 3070 | buffer[bufsize + 1] = d1; |
| 3071 | buffer[bufsize + 2] = d2; |
| 3072 | buffer[bufsize + 3] = d3; |
| 3073 | buffer[bufsize + 4] = d4; |
| 3074 | bufsize += 5; |
| 3075 | } |
| 3076 | |
| 3077 | void write(char_t d0, char_t d1, char_t d2, char_t d3, char_t d4, char_t d5) |
| 3078 | { |
| 3079 | if (bufsize + 6 > bufcapacity) flush(); |
| 3080 | |
| 3081 | buffer[bufsize + 0] = d0; |
| 3082 | buffer[bufsize + 1] = d1; |
| 3083 | buffer[bufsize + 2] = d2; |
| 3084 | buffer[bufsize + 3] = d3; |
| 3085 | buffer[bufsize + 4] = d4; |
| 3086 | buffer[bufsize + 5] = d5; |
| 3087 | bufsize += 6; |
| 3088 | } |
| 3089 | |
| 3090 | // utf8 maximum expansion: x4 (-> utf32) |
| 3091 | // utf16 maximum expansion: x2 (-> utf32) |
| 3092 | // utf32 maximum expansion: x1 |
| 3093 | enum |
| 3094 | { |
| 3095 | bufcapacitybytes = |
| 3096 | #ifdef PUGIXML_MEMORY_OUTPUT_STACK |
| 3097 | PUGIXML_MEMORY_OUTPUT_STACK |
| 3098 | #else |
| 3099 | 10240 |
| 3100 | #endif |
| 3101 | , |
| 3102 | bufcapacity = bufcapacitybytes / (sizeof(char_t) + 4) |
| 3103 | }; |
| 3104 | |
| 3105 | char_t buffer[bufcapacity]; |
| 3106 | |
| 3107 | union |
| 3108 | { |
| 3109 | uint8_t data_u8[4 * bufcapacity]; |
| 3110 | uint16_t data_u16[2 * bufcapacity]; |
| 3111 | uint32_t data_u32[bufcapacity]; |
| 3112 | char_t data_char[bufcapacity]; |
| 3113 | } scratch; |
| 3114 | |
| 3115 | xml_writer& writer; |
| 3116 | size_t bufsize; |
| 3117 | xml_encoding encoding; |
| 3118 | }; |
| 3119 | |
| 3120 | PUGI__FN void text_output_escaped(xml_buffered_writer& writer, const char_t* s, chartypex_t type) |
| 3121 | { |
| 3122 | while (*s) |
| 3123 | { |
| 3124 | const char_t* prev = s; |
| 3125 | |
| 3126 | // While *s is a usual symbol |
| 3127 | while (!PUGI__IS_CHARTYPEX(*s, type)) ++s; |
| 3128 | |
| 3129 | writer.write(prev, static_cast<size_t>(s - prev)); |
| 3130 | |
| 3131 | switch (*s) |
| 3132 | { |
| 3133 | case 0: break; |
| 3134 | case '&': |
| 3135 | writer.write('&', 'a', 'm', 'p', ';'); |
| 3136 | ++s; |
| 3137 | break; |
| 3138 | case '<': |
| 3139 | writer.write('&', 'l', 't', ';'); |
| 3140 | ++s; |
| 3141 | break; |
| 3142 | case '>': |
| 3143 | writer.write('&', 'g', 't', ';'); |
| 3144 | ++s; |
| 3145 | break; |
| 3146 | case '"': |
| 3147 | writer.write('&', 'q', 'u', 'o', 't', ';'); |
| 3148 | ++s; |
| 3149 | break; |
| 3150 | default: // s is not a usual symbol |
| 3151 | { |
| 3152 | unsigned int ch = static_cast<unsigned int>(*s++); |
| 3153 | assert(ch < 32); |
| 3154 | |
| 3155 | writer.write('&', '#', static_cast<char_t>((ch / 10) + '0'), static_cast<char_t>((ch % 10) + '0'), ';'); |
| 3156 | } |
| 3157 | } |
| 3158 | } |
| 3159 | } |
| 3160 | |
| 3161 | PUGI__FN void text_output(xml_buffered_writer& writer, const char_t* s, chartypex_t type, unsigned int flags) |
| 3162 | { |
| 3163 | if (flags & format_no_escapes) |
| 3164 | writer.write(s); |
| 3165 | else |
| 3166 | text_output_escaped(writer, s, type); |
| 3167 | } |
| 3168 | |
| 3169 | PUGI__FN void text_output_cdata(xml_buffered_writer& writer, const char_t* s) |
| 3170 | { |
| 3171 | do |
| 3172 | { |
| 3173 | writer.write('<', '!', '[', 'C', 'D'); |
| 3174 | writer.write('A', 'T', 'A', '['); |
| 3175 | |
| 3176 | const char_t* prev = s; |
| 3177 | |
| 3178 | // look for ]]> sequence - we can't output it as is since it terminates CDATA |
| 3179 | while (*s && !(s[0] == ']' && s[1] == ']' && s[2] == '>')) ++s; |
| 3180 | |
| 3181 | // skip ]] if we stopped at ]]>, > will go to the next CDATA section |
| 3182 | if (*s) s += 2; |
| 3183 | |
| 3184 | writer.write(prev, static_cast<size_t>(s - prev)); |
| 3185 | |
| 3186 | writer.write(']', ']', '>'); |
| 3187 | } |
| 3188 | while (*s); |
| 3189 | } |
| 3190 | |
| 3191 | PUGI__FN void node_output_attributes(xml_buffered_writer& writer, const xml_node& node, unsigned int flags) |
| 3192 | { |
| 3193 | const char_t* default_name = PUGIXML_TEXT(":anonymous"); |
| 3194 | |
| 3195 | for (xml_attribute a = node.first_attribute(); a; a = a.next_attribute()) |
| 3196 | { |
| 3197 | writer.write(' '); |
| 3198 | writer.write(a.name()[0] ? a.name() : default_name); |
| 3199 | writer.write('=', '"'); |
| 3200 | |
| 3201 | text_output(writer, a.value(), ctx_special_attr, flags); |
| 3202 | |
| 3203 | writer.write('"'); |
| 3204 | } |
| 3205 | } |
| 3206 | |
| 3207 | PUGI__FN void node_output(xml_buffered_writer& writer, const xml_node& node, const char_t* indent, unsigned int flags, unsigned int depth) |
| 3208 | { |
| 3209 | const char_t* default_name = PUGIXML_TEXT(":anonymous"); |
| 3210 | |
| 3211 | if ((flags & format_indent) != 0 && (flags & format_raw) == 0) |
| 3212 | for (unsigned int i = 0; i < depth; ++i) writer.write(indent); |
| 3213 | |
| 3214 | switch (node.type()) |
| 3215 | { |
| 3216 | case node_document: |
| 3217 | { |
| 3218 | for (xml_node n = node.first_child(); n; n = n.next_sibling()) |
| 3219 | node_output(writer, n, indent, flags, depth); |
| 3220 | break; |
| 3221 | } |
| 3222 | |
| 3223 | case node_element: |
| 3224 | { |
| 3225 | const char_t* name = node.name()[0] ? node.name() : default_name; |
| 3226 | |
| 3227 | writer.write('<'); |
| 3228 | writer.write(name); |
| 3229 | |
| 3230 | node_output_attributes(writer, node, flags); |
| 3231 | |
| 3232 | if (flags & format_raw) |
| 3233 | { |
| 3234 | if (!node.first_child()) |
| 3235 | writer.write(' ', '/', '>'); |
| 3236 | else |
| 3237 | { |
| 3238 | writer.write('>'); |
| 3239 | |
| 3240 | for (xml_node n = node.first_child(); n; n = n.next_sibling()) |
| 3241 | node_output(writer, n, indent, flags, depth + 1); |
| 3242 | |
| 3243 | writer.write('<', '/'); |
| 3244 | writer.write(name); |
| 3245 | writer.write('>'); |
| 3246 | } |
| 3247 | } |
| 3248 | else if (!node.first_child()) |
| 3249 | writer.write(' ', '/', '>', '\n'); |
| 3250 | else if (node.first_child() == node.last_child() && (node.first_child().type() == node_pcdata || node.first_child().type() == node_cdata)) |
| 3251 | { |
| 3252 | writer.write('>'); |
| 3253 | |
| 3254 | if (node.first_child().type() == node_pcdata) |
| 3255 | text_output(writer, node.first_child().value(), ctx_special_pcdata, flags); |
| 3256 | else |
| 3257 | text_output_cdata(writer, node.first_child().value()); |
| 3258 | |
| 3259 | writer.write('<', '/'); |
| 3260 | writer.write(name); |
| 3261 | writer.write('>', '\n'); |
| 3262 | } |
| 3263 | else |
| 3264 | { |
| 3265 | writer.write('>', '\n'); |
| 3266 | |
| 3267 | for (xml_node n = node.first_child(); n; n = n.next_sibling()) |
| 3268 | node_output(writer, n, indent, flags, depth + 1); |
| 3269 | |
| 3270 | if ((flags & format_indent) != 0 && (flags & format_raw) == 0) |
| 3271 | for (unsigned int i = 0; i < depth; ++i) writer.write(indent); |
| 3272 | |
| 3273 | writer.write('<', '/'); |
| 3274 | writer.write(name); |
| 3275 | writer.write('>', '\n'); |
| 3276 | } |
| 3277 | |
| 3278 | break; |
| 3279 | } |
| 3280 | |
| 3281 | case node_pcdata: |
| 3282 | text_output(writer, node.value(), ctx_special_pcdata, flags); |
| 3283 | if ((flags & format_raw) == 0) writer.write('\n'); |
| 3284 | break; |
| 3285 | |
| 3286 | case node_cdata: |
| 3287 | text_output_cdata(writer, node.value()); |
| 3288 | if ((flags & format_raw) == 0) writer.write('\n'); |
| 3289 | break; |
| 3290 | |
| 3291 | case node_comment: |
| 3292 | writer.write('<', '!', '-', '-'); |
| 3293 | writer.write(node.value()); |
| 3294 | writer.write('-', '-', '>'); |
| 3295 | if ((flags & format_raw) == 0) writer.write('\n'); |
| 3296 | break; |
| 3297 | |
| 3298 | case node_pi: |
| 3299 | case node_declaration: |
| 3300 | writer.write('<', '?'); |
| 3301 | writer.write(node.name()[0] ? node.name() : default_name); |
| 3302 | |
| 3303 | if (node.type() == node_declaration) |
| 3304 | { |
| 3305 | node_output_attributes(writer, node, flags); |
| 3306 | } |
| 3307 | else if (node.value()[0]) |
| 3308 | { |
| 3309 | writer.write(' '); |
| 3310 | writer.write(node.value()); |
| 3311 | } |
| 3312 | |
| 3313 | writer.write('?', '>'); |
| 3314 | if ((flags & format_raw) == 0) writer.write('\n'); |
| 3315 | break; |
| 3316 | |
| 3317 | case node_doctype: |
| 3318 | writer.write('<', '!', 'D', 'O', 'C'); |
| 3319 | writer.write('T', 'Y', 'P', 'E'); |
| 3320 | |
| 3321 | if (node.value()[0]) |
| 3322 | { |
| 3323 | writer.write(' '); |
| 3324 | writer.write(node.value()); |
| 3325 | } |
| 3326 | |
| 3327 | writer.write('>'); |
| 3328 | if ((flags & format_raw) == 0) writer.write('\n'); |
| 3329 | break; |
| 3330 | |
| 3331 | default: |
| 3332 | assert(!"Invalid node type"); |
| 3333 | } |
| 3334 | } |
| 3335 | |
| 3336 | inline bool has_declaration(const xml_node& node) |
| 3337 | { |
| 3338 | for (xml_node child = node.first_child(); child; child = child.next_sibling()) |
| 3339 | { |
| 3340 | xml_node_type type = child.type(); |
| 3341 | |
| 3342 | if (type == node_declaration) return true; |
| 3343 | if (type == node_element) return false; |
| 3344 | } |
| 3345 | |
| 3346 | return false; |
| 3347 | } |
| 3348 | |
| 3349 | inline bool allow_insert_child(xml_node_type parent, xml_node_type child) |
| 3350 | { |
| 3351 | if (parent != node_document && parent != node_element) return false; |
| 3352 | if (child == node_document || child == node_null) return false; |
| 3353 | if (parent != node_document && (child == node_declaration || child == node_doctype)) return false; |
| 3354 | |
| 3355 | return true; |
| 3356 | } |
| 3357 | |
| 3358 | PUGI__FN void recursive_copy_skip(xml_node& dest, const xml_node& source, const xml_node& skip) |
| 3359 | { |
| 3360 | assert(dest.type() == source.type()); |
| 3361 | |
| 3362 | switch (source.type()) |
| 3363 | { |
| 3364 | case node_element: |
| 3365 | { |
| 3366 | dest.set_name(source.name()); |
| 3367 | |
| 3368 | for (xml_attribute a = source.first_attribute(); a; a = a.next_attribute()) |
| 3369 | dest.append_attribute(a.name()).set_value(a.value()); |
| 3370 | |
| 3371 | for (xml_node c = source.first_child(); c; c = c.next_sibling()) |
| 3372 | { |
| 3373 | if (c == skip) continue; |
| 3374 | |
| 3375 | xml_node cc = dest.append_child(c.type()); |
| 3376 | assert(cc); |
| 3377 | |
| 3378 | recursive_copy_skip(cc, c, skip); |
| 3379 | } |
| 3380 | |
| 3381 | break; |
| 3382 | } |
| 3383 | |
| 3384 | case node_pcdata: |
| 3385 | case node_cdata: |
| 3386 | case node_comment: |
| 3387 | case node_doctype: |
| 3388 | dest.set_value(source.value()); |
| 3389 | break; |
| 3390 | |
| 3391 | case node_pi: |
| 3392 | dest.set_name(source.name()); |
| 3393 | dest.set_value(source.value()); |
| 3394 | break; |
| 3395 | |
| 3396 | case node_declaration: |
| 3397 | { |
| 3398 | dest.set_name(source.name()); |
| 3399 | |
| 3400 | for (xml_attribute a = source.first_attribute(); a; a = a.next_attribute()) |
| 3401 | dest.append_attribute(a.name()).set_value(a.value()); |
| 3402 | |
| 3403 | break; |
| 3404 | } |
| 3405 | |
| 3406 | default: |
| 3407 | assert(!"Invalid node type"); |
| 3408 | } |
| 3409 | } |
| 3410 | |
| 3411 | inline bool is_text_node(xml_node_struct* node) |
| 3412 | { |
| 3413 | xml_node_type type = static_cast<xml_node_type>((node->header & impl::xml_memory_page_type_mask) + 1); |
| 3414 | |
| 3415 | return type == node_pcdata || type == node_cdata; |
| 3416 | } |
| 3417 | |
| 3418 | // get value with conversion functions |
| 3419 | PUGI__FN int get_integer_base(const char_t* value) |
| 3420 | { |
| 3421 | const char_t* s = value; |
| 3422 | |
| 3423 | while (PUGI__IS_CHARTYPE(*s, ct_space)) |
| 3424 | s++; |
| 3425 | |
| 3426 | if (*s == '-') |
| 3427 | s++; |
| 3428 | |
| 3429 | return (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? 16 : 10; |
| 3430 | } |
| 3431 | |
| 3432 | PUGI__FN int get_value_int(const char_t* value, int def) |
| 3433 | { |
| 3434 | if (!value) return def; |
| 3435 | |
| 3436 | int base = get_integer_base(value); |
| 3437 | |
| 3438 | #ifdef PUGIXML_WCHAR_MODE |
| 3439 | return static_cast<int>(wcstol(value, 0, base)); |
| 3440 | #else |
| 3441 | return static_cast<int>(strtol(value, 0, base)); |
| 3442 | #endif |
| 3443 | } |
| 3444 | |
| 3445 | PUGI__FN unsigned int get_value_uint(const char_t* value, unsigned int def) |
| 3446 | { |
| 3447 | if (!value) return def; |
| 3448 | |
| 3449 | int base = get_integer_base(value); |
| 3450 | |
| 3451 | #ifdef PUGIXML_WCHAR_MODE |
| 3452 | return static_cast<unsigned int>(wcstoul(value, 0, base)); |
| 3453 | #else |
| 3454 | return static_cast<unsigned int>(strtoul(value, 0, base)); |
| 3455 | #endif |
| 3456 | } |
| 3457 | |
| 3458 | PUGI__FN double get_value_double(const char_t* value, double def) |
| 3459 | { |
| 3460 | if (!value) return def; |
| 3461 | |
| 3462 | #ifdef PUGIXML_WCHAR_MODE |
| 3463 | return wcstod(value, 0); |
| 3464 | #else |
| 3465 | return strtod(value, 0); |
| 3466 | #endif |
| 3467 | } |
| 3468 | |
| 3469 | PUGI__FN float get_value_float(const char_t* value, float def) |
| 3470 | { |
| 3471 | if (!value) return def; |
| 3472 | |
| 3473 | #ifdef PUGIXML_WCHAR_MODE |
| 3474 | return static_cast<float>(wcstod(value, 0)); |
| 3475 | #else |
| 3476 | return static_cast<float>(strtod(value, 0)); |
| 3477 | #endif |
| 3478 | } |
| 3479 | |
| 3480 | PUGI__FN bool get_value_bool(const char_t* value, bool def) |
| 3481 | { |
| 3482 | if (!value) return def; |
| 3483 | |
| 3484 | // only look at first char |
| 3485 | char_t first = *value; |
| 3486 | |
| 3487 | // 1*, t* (true), T* (True), y* (yes), Y* (YES) |
| 3488 | return (first == '1' || first == 't' || first == 'T' || first == 'y' || first == 'Y'); |
| 3489 | } |
| 3490 | |
| 3491 | #ifdef PUGIXML_HAS_LONG_LONG |
| 3492 | PUGI__FN long long get_value_llong(const char_t* value, long long def) |
| 3493 | { |
| 3494 | if (!value) return def; |
| 3495 | |
| 3496 | int base = get_integer_base(value); |
| 3497 | |
| 3498 | #ifdef PUGIXML_WCHAR_MODE |
| 3499 | #ifdef PUGI__MSVC_CRT_VERSION |
| 3500 | return _wcstoi64(value, 0, base); |
| 3501 | #else |
| 3502 | return wcstoll(value, 0, base); |
| 3503 | #endif |
| 3504 | #else |
| 3505 | #ifdef PUGI__MSVC_CRT_VERSION |
| 3506 | return _strtoi64(value, 0, base); |
| 3507 | #else |
| 3508 | return strtoll(value, 0, base); |
| 3509 | #endif |
| 3510 | #endif |
| 3511 | } |
| 3512 | |
| 3513 | PUGI__FN unsigned long long get_value_ullong(const char_t* value, unsigned long long def) |
| 3514 | { |
| 3515 | if (!value) return def; |
| 3516 | |
| 3517 | int base = get_integer_base(value); |
| 3518 | |
| 3519 | #ifdef PUGIXML_WCHAR_MODE |
| 3520 | #ifdef PUGI__MSVC_CRT_VERSION |
| 3521 | return _wcstoui64(value, 0, base); |
| 3522 | #else |
| 3523 | return wcstoull(value, 0, base); |
| 3524 | #endif |
| 3525 | #else |
| 3526 | #ifdef PUGI__MSVC_CRT_VERSION |
| 3527 | return _strtoui64(value, 0, base); |
| 3528 | #else |
| 3529 | return strtoull(value, 0, base); |
| 3530 | #endif |
| 3531 | #endif |
| 3532 | } |
| 3533 | #endif |
| 3534 | |
| 3535 | // set value with conversion functions |
| 3536 | PUGI__FN bool set_value_buffer(char_t*& dest, uintptr_t& header, uintptr_t header_mask, char (&buf)[128]) |
| 3537 | { |
| 3538 | #ifdef PUGIXML_WCHAR_MODE |
| 3539 | char_t wbuf[128]; |
| 3540 | impl::widen_ascii(wbuf, buf); |
| 3541 | |
| 3542 | return strcpy_insitu(dest, header, header_mask, wbuf); |
| 3543 | #else |
| 3544 | return strcpy_insitu(dest, header, header_mask, buf); |
| 3545 | #endif |
| 3546 | } |
| 3547 | |
| 3548 | PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, int value) |
| 3549 | { |
| 3550 | char buf[128]; |
| 3551 | sprintf(buf, "%d", value); |
| 3552 | |
| 3553 | return set_value_buffer(dest, header, header_mask, buf); |
| 3554 | } |
| 3555 | |
| 3556 | PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, unsigned int value) |
| 3557 | { |
| 3558 | char buf[128]; |
| 3559 | sprintf(buf, "%u", value); |
| 3560 | |
| 3561 | return set_value_buffer(dest, header, header_mask, buf); |
| 3562 | } |
| 3563 | |
| 3564 | PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, double value) |
| 3565 | { |
| 3566 | char buf[128]; |
| 3567 | sprintf(buf, "%g", value); |
| 3568 | |
| 3569 | return set_value_buffer(dest, header, header_mask, buf); |
| 3570 | } |
| 3571 | |
| 3572 | PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, bool value) |
| 3573 | { |
| 3574 | return strcpy_insitu(dest, header, header_mask, value ? PUGIXML_TEXT("true") : PUGIXML_TEXT("false")); |
| 3575 | } |
| 3576 | |
| 3577 | #ifdef PUGIXML_HAS_LONG_LONG |
| 3578 | PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, long long value) |
| 3579 | { |
| 3580 | char buf[128]; |
| 3581 | sprintf(buf, "%lld", value); |
| 3582 | |
| 3583 | return set_value_buffer(dest, header, header_mask, buf); |
| 3584 | } |
| 3585 | |
| 3586 | PUGI__FN bool set_value_convert(char_t*& dest, uintptr_t& header, uintptr_t header_mask, unsigned long long value) |
| 3587 | { |
| 3588 | char buf[128]; |
| 3589 | sprintf(buf, "%llu", value); |
| 3590 | |
| 3591 | return set_value_buffer(dest, header, header_mask, buf); |
| 3592 | } |
| 3593 | #endif |
| 3594 | |
| 3595 | // we need to get length of entire file to load it in memory; the only (relatively) sane way to do it is via seek/tell trick |
| 3596 | PUGI__FN xml_parse_status get_file_size(FILE* file, size_t& out_result) |
| 3597 | { |
| 3598 | #if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400 && !defined(_WIN32_WCE) |
| 3599 | // there are 64-bit versions of fseek/ftell, let's use them |
| 3600 | typedef __int64 length_type; |
| 3601 | |
| 3602 | _fseeki64(file, 0, SEEK_END); |
| 3603 | length_type length = _ftelli64(file); |
| 3604 | _fseeki64(file, 0, SEEK_SET); |
| 3605 | #elif defined(__MINGW32__) && !defined(__NO_MINGW_LFS) && !defined(__STRICT_ANSI__) |
| 3606 | // there are 64-bit versions of fseek/ftell, let's use them |
| 3607 | typedef off64_t length_type; |
| 3608 | |
| 3609 | fseeko64(file, 0, SEEK_END); |
| 3610 | length_type length = ftello64(file); |
| 3611 | fseeko64(file, 0, SEEK_SET); |
| 3612 | #else |
| 3613 | // if this is a 32-bit OS, long is enough; if this is a unix system, long is 64-bit, which is enough; otherwise we can't do anything anyway. |
| 3614 | typedef long length_type; |
| 3615 | |
| 3616 | fseek(file, 0, SEEK_END); |
| 3617 | length_type length = ftell(file); |
| 3618 | fseek(file, 0, SEEK_SET); |
| 3619 | #endif |
| 3620 | |
| 3621 | // check for I/O errors |
| 3622 | if (length < 0) return status_io_error; |
| 3623 | |
| 3624 | // check for overflow |
| 3625 | size_t result = static_cast<size_t>(length); |
| 3626 | |
| 3627 | if (static_cast<length_type>(result) != length) return status_out_of_memory; |
| 3628 | |
| 3629 | // finalize |
| 3630 | out_result = result; |
| 3631 | |
| 3632 | return status_ok; |
| 3633 | } |
| 3634 | |
| 3635 | PUGI__FN size_t zero_terminate_buffer(void* buffer, size_t size, xml_encoding encoding) |
| 3636 | { |
| 3637 | // We only need to zero-terminate if encoding conversion does not do it for us |
| 3638 | #ifdef PUGIXML_WCHAR_MODE |
| 3639 | xml_encoding wchar_encoding = get_wchar_encoding(); |
| 3640 | |
| 3641 | if (encoding == wchar_encoding || need_endian_swap_utf(encoding, wchar_encoding)) |
| 3642 | { |
| 3643 | size_t length = size / sizeof(char_t); |
| 3644 | |
| 3645 | static_cast<char_t*>(buffer)[length] = 0; |
| 3646 | return (length + 1) * sizeof(char_t); |
| 3647 | } |
| 3648 | #else |
| 3649 | if (encoding == encoding_utf8) |
| 3650 | { |
| 3651 | static_cast<char*>(buffer)[size] = 0; |
| 3652 | return size + 1; |
| 3653 | } |
| 3654 | #endif |
| 3655 | |
| 3656 | return size; |
| 3657 | } |
| 3658 | |
| 3659 | PUGI__FN xml_parse_result load_file_impl(xml_document& doc, FILE* file, unsigned int options, xml_encoding encoding) |
| 3660 | { |
| 3661 | if (!file) return make_parse_result(status_file_not_found); |
| 3662 | |
| 3663 | // get file size (can result in I/O errors) |
| 3664 | size_t size = 0; |
| 3665 | xml_parse_status size_status = get_file_size(file, size); |
| 3666 | |
| 3667 | if (size_status != status_ok) |
| 3668 | { |
| 3669 | fclose(file); |
| 3670 | return make_parse_result(size_status); |
| 3671 | } |
| 3672 | |
| 3673 | size_t max_suffix_size = sizeof(char_t); |
| 3674 | |
| 3675 | // allocate buffer for the whole file |
| 3676 | char* contents = static_cast<char*>(xml_memory::allocate(size + max_suffix_size)); |
| 3677 | |
| 3678 | if (!contents) |
| 3679 | { |
| 3680 | fclose(file); |
| 3681 | return make_parse_result(status_out_of_memory); |
| 3682 | } |
| 3683 | |
| 3684 | // read file in memory |
| 3685 | size_t read_size = fread(contents, 1, size, file); |
| 3686 | fclose(file); |
| 3687 | |
| 3688 | if (read_size != size) |
| 3689 | { |
| 3690 | xml_memory::deallocate(contents); |
| 3691 | return make_parse_result(status_io_error); |
| 3692 | } |
| 3693 | |
| 3694 | xml_encoding real_encoding = get_buffer_encoding(encoding, contents, size); |
| 3695 | |
| 3696 | return doc.load_buffer_inplace_own(contents, zero_terminate_buffer(contents, size, real_encoding), options, real_encoding); |
| 3697 | } |
| 3698 | |
| 3699 | #ifndef PUGIXML_NO_STL |
| 3700 | template <typename T> struct xml_stream_chunk |
| 3701 | { |
| 3702 | static xml_stream_chunk* create() |
| 3703 | { |
| 3704 | void* memory = xml_memory::allocate(sizeof(xml_stream_chunk)); |
| 3705 | |
| 3706 | return new (memory) xml_stream_chunk(); |
| 3707 | } |
| 3708 | |
| 3709 | static void destroy(void* ptr) |
| 3710 | { |
| 3711 | xml_stream_chunk* chunk = static_cast<xml_stream_chunk*>(ptr); |
| 3712 | |
| 3713 | // free chunk chain |
| 3714 | while (chunk) |
| 3715 | { |
| 3716 | xml_stream_chunk* next = chunk->next; |
| 3717 | xml_memory::deallocate(chunk); |
| 3718 | chunk = next; |
| 3719 | } |
| 3720 | } |
| 3721 | |
| 3722 | xml_stream_chunk(): next(0), size(0) |
| 3723 | { |
| 3724 | } |
| 3725 | |
| 3726 | xml_stream_chunk* next; |
| 3727 | size_t size; |
| 3728 | |
| 3729 | T data[xml_memory_page_size / sizeof(T)]; |
| 3730 | }; |
| 3731 | |
| 3732 | template <typename T> PUGI__FN xml_parse_status load_stream_data_noseek(std::basic_istream<T>& stream, void** out_buffer, size_t* out_size) |
| 3733 | { |
| 3734 | buffer_holder chunks(0, xml_stream_chunk<T>::destroy); |
| 3735 | |
| 3736 | // read file to a chunk list |
| 3737 | size_t total = 0; |
| 3738 | xml_stream_chunk<T>* last = 0; |
| 3739 | |
| 3740 | while (!stream.eof()) |
| 3741 | { |
| 3742 | // allocate new chunk |
| 3743 | xml_stream_chunk<T>* chunk = xml_stream_chunk<T>::create(); |
| 3744 | if (!chunk) return status_out_of_memory; |
| 3745 | |
| 3746 | // append chunk to list |
| 3747 | if (last) last = last->next = chunk; |
| 3748 | else chunks.data = last = chunk; |
| 3749 | |
| 3750 | // read data to chunk |
| 3751 | stream.read(chunk->data, static_cast<std::streamsize>(sizeof(chunk->data) / sizeof(T))); |
| 3752 | chunk->size = static_cast<size_t>(stream.gcount()) * sizeof(T); |
| 3753 | |
| 3754 | // read may set failbit | eofbit in case gcount() is less than read length, so check for other I/O errors |
| 3755 | if (stream.bad() || (!stream.eof() && stream.fail())) return status_io_error; |
| 3756 | |
| 3757 | // guard against huge files (chunk size is small enough to make this overflow check work) |
| 3758 | if (total + chunk->size < total) return status_out_of_memory; |
| 3759 | total += chunk->size; |
| 3760 | } |
| 3761 | |
| 3762 | size_t max_suffix_size = sizeof(char_t); |
| 3763 | |
| 3764 | // copy chunk list to a contiguous buffer |
| 3765 | char* buffer = static_cast<char*>(xml_memory::allocate(total + max_suffix_size)); |
| 3766 | if (!buffer) return status_out_of_memory; |
| 3767 | |
| 3768 | char* write = buffer; |
| 3769 | |
| 3770 | for (xml_stream_chunk<T>* chunk = static_cast<xml_stream_chunk<T>*>(chunks.data); chunk; chunk = chunk->next) |
| 3771 | { |
| 3772 | assert(write + chunk->size <= buffer + total); |
| 3773 | memcpy(write, chunk->data, chunk->size); |
| 3774 | write += chunk->size; |
| 3775 | } |
| 3776 | |
| 3777 | assert(write == buffer + total); |
| 3778 | |
| 3779 | // return buffer |
| 3780 | *out_buffer = buffer; |
| 3781 | *out_size = total; |
| 3782 | |
| 3783 | return status_ok; |
| 3784 | } |
| 3785 | |
| 3786 | template <typename T> PUGI__FN xml_parse_status load_stream_data_seek(std::basic_istream<T>& stream, void** out_buffer, size_t* out_size) |
| 3787 | { |
| 3788 | // get length of remaining data in stream |
| 3789 | typename std::basic_istream<T>::pos_type pos = stream.tellg(); |
| 3790 | stream.seekg(0, std::ios::end); |
| 3791 | std::streamoff length = stream.tellg() - pos; |
| 3792 | stream.seekg(pos); |
| 3793 | |
| 3794 | if (stream.fail() || pos < 0) return status_io_error; |
| 3795 | |
| 3796 | // guard against huge files |
| 3797 | size_t read_length = static_cast<size_t>(length); |
| 3798 | |
| 3799 | if (static_cast<std::streamsize>(read_length) != length || length < 0) return status_out_of_memory; |
| 3800 | |
| 3801 | size_t max_suffix_size = sizeof(char_t); |
| 3802 | |
| 3803 | // read stream data into memory (guard against stream exceptions with buffer holder) |
| 3804 | buffer_holder buffer(xml_memory::allocate(read_length * sizeof(T) + max_suffix_size), xml_memory::deallocate); |
| 3805 | if (!buffer.data) return status_out_of_memory; |
| 3806 | |
| 3807 | stream.read(static_cast<T*>(buffer.data), static_cast<std::streamsize>(read_length)); |
| 3808 | |
| 3809 | // read may set failbit | eofbit in case gcount() is less than read_length (i.e. line ending conversion), so check for other I/O errors |
| 3810 | if (stream.bad() || (!stream.eof() && stream.fail())) return status_io_error; |
| 3811 | |
| 3812 | // return buffer |
| 3813 | size_t actual_length = static_cast<size_t>(stream.gcount()); |
| 3814 | assert(actual_length <= read_length); |
| 3815 | |
| 3816 | *out_buffer = buffer.release(); |
| 3817 | *out_size = actual_length * sizeof(T); |
| 3818 | |
| 3819 | return status_ok; |
| 3820 | } |
| 3821 | |
| 3822 | template <typename T> PUGI__FN xml_parse_result load_stream_impl(xml_document& doc, std::basic_istream<T>& stream, unsigned int options, xml_encoding encoding) |
| 3823 | { |
| 3824 | void* buffer = 0; |
| 3825 | size_t size = 0; |
| 3826 | xml_parse_status status = status_ok; |
| 3827 | |
| 3828 | // if stream has an error bit set, bail out (otherwise tellg() can fail and we'll clear error bits) |
| 3829 | if (stream.fail()) return make_parse_result(status_io_error); |
| 3830 | |
| 3831 | // load stream to memory (using seek-based implementation if possible, since it's faster and takes less memory) |
| 3832 | if (stream.tellg() < 0) |
| 3833 | { |
| 3834 | stream.clear(); // clear error flags that could be set by a failing tellg |
| 3835 | status = load_stream_data_noseek(stream, &buffer, &size); |
| 3836 | } |
| 3837 | else |
| 3838 | status = load_stream_data_seek(stream, &buffer, &size); |
| 3839 | |
| 3840 | if (status != status_ok) return make_parse_result(status); |
| 3841 | |
| 3842 | xml_encoding real_encoding = get_buffer_encoding(encoding, buffer, size); |
| 3843 | |
| 3844 | return doc.load_buffer_inplace_own(buffer, zero_terminate_buffer(buffer, size, real_encoding), options, real_encoding); |
| 3845 | } |
| 3846 | #endif |
| 3847 | |
| 3848 | #if defined(PUGI__MSVC_CRT_VERSION) || defined(__BORLANDC__) || (defined(__MINGW32__) && !defined(__STRICT_ANSI__)) |
| 3849 | PUGI__FN FILE* open_file_wide(const wchar_t* path, const wchar_t* mode) |
| 3850 | { |
| 3851 | return _wfopen(path, mode); |
| 3852 | } |
| 3853 | #else |
| 3854 | PUGI__FN char* convert_path_heap(const wchar_t* str) |
| 3855 | { |
| 3856 | assert(str); |
| 3857 | |
| 3858 | // first pass: get length in utf8 characters |
| 3859 | size_t length = strlength_wide(str); |
| 3860 | size_t size = as_utf8_begin(str, length); |
| 3861 | |
| 3862 | // allocate resulting string |
| 3863 | char* result = static_cast<char*>(xml_memory::allocate(size + 1)); |
| 3864 | if (!result) return 0; |
| 3865 | |
| 3866 | // second pass: convert to utf8 |
| 3867 | as_utf8_end(result, size, str, length); |
| 3868 | |
| 3869 | return result; |
| 3870 | } |
| 3871 | |
| 3872 | PUGI__FN FILE* open_file_wide(const wchar_t* path, const wchar_t* mode) |
| 3873 | { |
| 3874 | // there is no standard function to open wide paths, so our best bet is to try utf8 path |
| 3875 | char* path_utf8 = convert_path_heap(path); |
| 3876 | if (!path_utf8) return 0; |
| 3877 | |
| 3878 | // convert mode to ASCII (we mirror _wfopen interface) |
| 3879 | char mode_ascii[4] = {0}; |
| 3880 | for (size_t i = 0; mode[i]; ++i) mode_ascii[i] = static_cast<char>(mode[i]); |
| 3881 | |
| 3882 | // try to open the utf8 path |
| 3883 | FILE* result = fopen(path_utf8, mode_ascii); |
| 3884 | |
| 3885 | // free dummy buffer |
| 3886 | xml_memory::deallocate(path_utf8); |
| 3887 | |
| 3888 | return result; |
| 3889 | } |
| 3890 | #endif |
| 3891 | |
| 3892 | PUGI__FN bool save_file_impl(const xml_document& doc, FILE* file, const char_t* indent, unsigned int flags, xml_encoding encoding) |
| 3893 | { |
| 3894 | if (!file) return false; |
| 3895 | |
| 3896 | xml_writer_file writer(file); |
| 3897 | doc.save(writer, indent, flags, encoding); |
| 3898 | |
| 3899 | int result = ferror(file); |
| 3900 | |
| 3901 | fclose(file); |
| 3902 | |
| 3903 | return result == 0; |
| 3904 | } |
| 3905 | |
| 3906 | PUGI__FN xml_parse_result load_buffer_impl(xml_document_struct* doc, xml_node_struct* root, void* contents, size_t size, unsigned int options, xml_encoding encoding, bool is_mutable, bool own, char_t** out_buffer) |
| 3907 | { |
| 3908 | // check input buffer |
| 3909 | assert(contents || size == 0); |
| 3910 | |
| 3911 | // get actual encoding |
| 3912 | xml_encoding buffer_encoding = impl::get_buffer_encoding(encoding, contents, size); |
| 3913 | |
| 3914 | // get private buffer |
| 3915 | char_t* buffer = 0; |
| 3916 | size_t length = 0; |
| 3917 | |
| 3918 | if (!impl::convert_buffer(buffer, length, buffer_encoding, contents, size, is_mutable)) return impl::make_parse_result(status_out_of_memory); |
| 3919 | |
| 3920 | // delete original buffer if we performed a conversion |
| 3921 | if (own && buffer != contents && contents) impl::xml_memory::deallocate(contents); |
| 3922 | |
| 3923 | // store buffer for offset_debug |
| 3924 | doc->buffer = buffer; |
| 3925 | |
| 3926 | // parse |
| 3927 | xml_parse_result res = impl::xml_parser::parse(buffer, length, doc, root, options); |
| 3928 | |
| 3929 | // remember encoding |
| 3930 | res.encoding = buffer_encoding; |
| 3931 | |
| 3932 | // grab onto buffer if it's our buffer, user is responsible for deallocating contents himself |
| 3933 | if (own || buffer != contents) *out_buffer = buffer; |
| 3934 | |
| 3935 | return res; |
| 3936 | } |
| 3937 | PUGI__NS_END |
| 3938 | |
| 3939 | namespace pugi |
| 3940 | { |
| 3941 | PUGI__FN xml_writer_file::xml_writer_file(void* file_): file(file_) |
| 3942 | { |
| 3943 | } |
| 3944 | |
| 3945 | PUGI__FN void xml_writer_file::write(const void* data, size_t size) |
| 3946 | { |
| 3947 | size_t result = fwrite(data, 1, size, static_cast<FILE*>(file)); |
| 3948 | (void)!result; // unfortunately we can't do proper error handling here |
| 3949 | } |
| 3950 | |
| 3951 | #ifndef PUGIXML_NO_STL |
| 3952 | PUGI__FN xml_writer_stream::xml_writer_stream(std::basic_ostream<char, std::char_traits<char> >& stream): narrow_stream(&stream), wide_stream(0) |
| 3953 | { |
| 3954 | } |
| 3955 | |
| 3956 | PUGI__FN xml_writer_stream::xml_writer_stream(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream): narrow_stream(0), wide_stream(&stream) |
| 3957 | { |
| 3958 | } |
| 3959 | |
| 3960 | PUGI__FN void xml_writer_stream::write(const void* data, size_t size) |
| 3961 | { |
| 3962 | if (narrow_stream) |
| 3963 | { |
| 3964 | assert(!wide_stream); |
| 3965 | narrow_stream->write(reinterpret_cast<const char*>(data), static_cast<std::streamsize>(size)); |
| 3966 | } |
| 3967 | else |
| 3968 | { |
| 3969 | assert(wide_stream); |
| 3970 | assert(size % sizeof(wchar_t) == 0); |
| 3971 | |
| 3972 | wide_stream->write(reinterpret_cast<const wchar_t*>(data), static_cast<std::streamsize>(size / sizeof(wchar_t))); |
| 3973 | } |
| 3974 | } |
| 3975 | #endif |
| 3976 | |
| 3977 | PUGI__FN xml_tree_walker::xml_tree_walker(): _depth(0) |
| 3978 | { |
| 3979 | } |
| 3980 | |
| 3981 | PUGI__FN xml_tree_walker::~xml_tree_walker() |
| 3982 | { |
| 3983 | } |
| 3984 | |
| 3985 | PUGI__FN int xml_tree_walker::depth() const |
| 3986 | { |
| 3987 | return _depth; |
| 3988 | } |
| 3989 | |
| 3990 | PUGI__FN bool xml_tree_walker::begin(xml_node&) |
| 3991 | { |
| 3992 | return true; |
| 3993 | } |
| 3994 | |
| 3995 | PUGI__FN bool xml_tree_walker::end(xml_node&) |
| 3996 | { |
| 3997 | return true; |
| 3998 | } |
| 3999 | |
| 4000 | PUGI__FN xml_attribute::xml_attribute(): _attr(0) |
| 4001 | { |
| 4002 | } |
| 4003 | |
| 4004 | PUGI__FN xml_attribute::xml_attribute(xml_attribute_struct* attr): _attr(attr) |
| 4005 | { |
| 4006 | } |
| 4007 | |
| 4008 | PUGI__FN static void unspecified_bool_xml_attribute(xml_attribute***) |
| 4009 | { |
| 4010 | } |
| 4011 | |
| 4012 | PUGI__FN xml_attribute::operator xml_attribute::unspecified_bool_type() const |
| 4013 | { |
| 4014 | return _attr ? unspecified_bool_xml_attribute : 0; |
| 4015 | } |
| 4016 | |
| 4017 | PUGI__FN bool xml_attribute::operator!() const |
| 4018 | { |
| 4019 | return !_attr; |
| 4020 | } |
| 4021 | |
| 4022 | PUGI__FN bool xml_attribute::operator==(const xml_attribute& r) const |
| 4023 | { |
| 4024 | return (_attr == r._attr); |
| 4025 | } |
| 4026 | |
| 4027 | PUGI__FN bool xml_attribute::operator!=(const xml_attribute& r) const |
| 4028 | { |
| 4029 | return (_attr != r._attr); |
| 4030 | } |
| 4031 | |
| 4032 | PUGI__FN bool xml_attribute::operator<(const xml_attribute& r) const |
| 4033 | { |
| 4034 | return (_attr < r._attr); |
| 4035 | } |
| 4036 | |
| 4037 | PUGI__FN bool xml_attribute::operator>(const xml_attribute& r) const |
| 4038 | { |
| 4039 | return (_attr > r._attr); |
| 4040 | } |
| 4041 | |
| 4042 | PUGI__FN bool xml_attribute::operator<=(const xml_attribute& r) const |
| 4043 | { |
| 4044 | return (_attr <= r._attr); |
| 4045 | } |
| 4046 | |
| 4047 | PUGI__FN bool xml_attribute::operator>=(const xml_attribute& r) const |
| 4048 | { |
| 4049 | return (_attr >= r._attr); |
| 4050 | } |
| 4051 | |
| 4052 | PUGI__FN xml_attribute xml_attribute::next_attribute() const |
| 4053 | { |
| 4054 | return _attr ? xml_attribute(_attr->next_attribute) : xml_attribute(); |
| 4055 | } |
| 4056 | |
| 4057 | PUGI__FN xml_attribute xml_attribute::previous_attribute() const |
| 4058 | { |
| 4059 | return _attr && _attr->prev_attribute_c->next_attribute ? xml_attribute(_attr->prev_attribute_c) : xml_attribute(); |
| 4060 | } |
| 4061 | |
| 4062 | PUGI__FN const char_t* xml_attribute::as_string(const char_t* def) const |
| 4063 | { |
| 4064 | return (_attr && _attr->value) ? _attr->value : def; |
| 4065 | } |
| 4066 | |
| 4067 | PUGI__FN int xml_attribute::as_int(int def) const |
| 4068 | { |
| 4069 | return impl::get_value_int(_attr ? _attr->value : 0, def); |
| 4070 | } |
| 4071 | |
| 4072 | PUGI__FN unsigned int xml_attribute::as_uint(unsigned int def) const |
| 4073 | { |
| 4074 | return impl::get_value_uint(_attr ? _attr->value : 0, def); |
| 4075 | } |
| 4076 | |
| 4077 | PUGI__FN double xml_attribute::as_double(double def) const |
| 4078 | { |
| 4079 | return impl::get_value_double(_attr ? _attr->value : 0, def); |
| 4080 | } |
| 4081 | |
| 4082 | PUGI__FN float xml_attribute::as_float(float def) const |
| 4083 | { |
| 4084 | return impl::get_value_float(_attr ? _attr->value : 0, def); |
| 4085 | } |
| 4086 | |
| 4087 | PUGI__FN bool xml_attribute::as_bool(bool def) const |
| 4088 | { |
| 4089 | return impl::get_value_bool(_attr ? _attr->value : 0, def); |
| 4090 | } |
| 4091 | |
| 4092 | #ifdef PUGIXML_HAS_LONG_LONG |
| 4093 | PUGI__FN long long xml_attribute::as_llong(long long def) const |
| 4094 | { |
| 4095 | return impl::get_value_llong(_attr ? _attr->value : 0, def); |
| 4096 | } |
| 4097 | |
| 4098 | PUGI__FN unsigned long long xml_attribute::as_ullong(unsigned long long def) const |
| 4099 | { |
| 4100 | return impl::get_value_ullong(_attr ? _attr->value : 0, def); |
| 4101 | } |
| 4102 | #endif |
| 4103 | |
| 4104 | PUGI__FN bool xml_attribute::empty() const |
| 4105 | { |
| 4106 | return !_attr; |
| 4107 | } |
| 4108 | |
| 4109 | PUGI__FN const char_t* xml_attribute::name() const |
| 4110 | { |
| 4111 | return (_attr && _attr->name) ? _attr->name : PUGIXML_TEXT(""); |
| 4112 | } |
| 4113 | |
| 4114 | PUGI__FN const char_t* xml_attribute::value() const |
| 4115 | { |
| 4116 | return (_attr && _attr->value) ? _attr->value : PUGIXML_TEXT(""); |
| 4117 | } |
| 4118 | |
| 4119 | PUGI__FN size_t xml_attribute::hash_value() const |
| 4120 | { |
| 4121 | return static_cast<size_t>(reinterpret_cast<uintptr_t>(_attr) / sizeof(xml_attribute_struct)); |
| 4122 | } |
| 4123 | |
| 4124 | PUGI__FN xml_attribute_struct* xml_attribute::internal_object() const |
| 4125 | { |
| 4126 | return _attr; |
| 4127 | } |
| 4128 | |
| 4129 | PUGI__FN xml_attribute& xml_attribute::operator=(const char_t* rhs) |
| 4130 | { |
| 4131 | set_value(rhs); |
| 4132 | return *this; |
| 4133 | } |
| 4134 | |
| 4135 | PUGI__FN xml_attribute& xml_attribute::operator=(int rhs) |
| 4136 | { |
| 4137 | set_value(rhs); |
| 4138 | return *this; |
| 4139 | } |
| 4140 | |
| 4141 | PUGI__FN xml_attribute& xml_attribute::operator=(unsigned int rhs) |
| 4142 | { |
| 4143 | set_value(rhs); |
| 4144 | return *this; |
| 4145 | } |
| 4146 | |
| 4147 | PUGI__FN xml_attribute& xml_attribute::operator=(double rhs) |
| 4148 | { |
| 4149 | set_value(rhs); |
| 4150 | return *this; |
| 4151 | } |
| 4152 | |
| 4153 | PUGI__FN xml_attribute& xml_attribute::operator=(bool rhs) |
| 4154 | { |
| 4155 | set_value(rhs); |
| 4156 | return *this; |
| 4157 | } |
| 4158 | |
| 4159 | #ifdef PUGIXML_HAS_LONG_LONG |
| 4160 | PUGI__FN xml_attribute& xml_attribute::operator=(long long rhs) |
| 4161 | { |
| 4162 | set_value(rhs); |
| 4163 | return *this; |
| 4164 | } |
| 4165 | |
| 4166 | PUGI__FN xml_attribute& xml_attribute::operator=(unsigned long long rhs) |
| 4167 | { |
| 4168 | set_value(rhs); |
| 4169 | return *this; |
| 4170 | } |
| 4171 | #endif |
| 4172 | |
| 4173 | PUGI__FN bool xml_attribute::set_name(const char_t* rhs) |
| 4174 | { |
| 4175 | if (!_attr) return false; |
| 4176 | |
| 4177 | return impl::strcpy_insitu(_attr->name, _attr->header, impl::xml_memory_page_name_allocated_mask, rhs); |
| 4178 | } |
| 4179 | |
| 4180 | PUGI__FN bool xml_attribute::set_value(const char_t* rhs) |
| 4181 | { |
| 4182 | if (!_attr) return false; |
| 4183 | |
| 4184 | return impl::strcpy_insitu(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs); |
| 4185 | } |
| 4186 | |
| 4187 | PUGI__FN bool xml_attribute::set_value(int rhs) |
| 4188 | { |
| 4189 | if (!_attr) return false; |
| 4190 | |
| 4191 | return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs); |
| 4192 | } |
| 4193 | |
| 4194 | PUGI__FN bool xml_attribute::set_value(unsigned int rhs) |
| 4195 | { |
| 4196 | if (!_attr) return false; |
| 4197 | |
| 4198 | return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs); |
| 4199 | } |
| 4200 | |
| 4201 | PUGI__FN bool xml_attribute::set_value(double rhs) |
| 4202 | { |
| 4203 | if (!_attr) return false; |
| 4204 | |
| 4205 | return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs); |
| 4206 | } |
| 4207 | |
| 4208 | PUGI__FN bool xml_attribute::set_value(bool rhs) |
| 4209 | { |
| 4210 | if (!_attr) return false; |
| 4211 | |
| 4212 | return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs); |
| 4213 | } |
| 4214 | |
| 4215 | #ifdef PUGIXML_HAS_LONG_LONG |
| 4216 | PUGI__FN bool xml_attribute::set_value(long long rhs) |
| 4217 | { |
| 4218 | if (!_attr) return false; |
| 4219 | |
| 4220 | return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs); |
| 4221 | } |
| 4222 | |
| 4223 | PUGI__FN bool xml_attribute::set_value(unsigned long long rhs) |
| 4224 | { |
| 4225 | if (!_attr) return false; |
| 4226 | |
| 4227 | return impl::set_value_convert(_attr->value, _attr->header, impl::xml_memory_page_value_allocated_mask, rhs); |
| 4228 | } |
| 4229 | #endif |
| 4230 | |
| 4231 | #ifdef __BORLANDC__ |
| 4232 | PUGI__FN bool operator&&(const xml_attribute& lhs, bool rhs) |
| 4233 | { |
| 4234 | return (bool)lhs && rhs; |
| 4235 | } |
| 4236 | |
| 4237 | PUGI__FN bool operator||(const xml_attribute& lhs, bool rhs) |
| 4238 | { |
| 4239 | return (bool)lhs || rhs; |
| 4240 | } |
| 4241 | #endif |
| 4242 | |
| 4243 | PUGI__FN xml_node::xml_node(): _root(0) |
| 4244 | { |
| 4245 | } |
| 4246 | |
| 4247 | PUGI__FN xml_node::xml_node(xml_node_struct* p): _root(p) |
| 4248 | { |
| 4249 | } |
| 4250 | |
| 4251 | PUGI__FN static void unspecified_bool_xml_node(xml_node***) |
| 4252 | { |
| 4253 | } |
| 4254 | |
| 4255 | PUGI__FN xml_node::operator xml_node::unspecified_bool_type() const |
| 4256 | { |
| 4257 | return _root ? unspecified_bool_xml_node : 0; |
| 4258 | } |
| 4259 | |
| 4260 | PUGI__FN bool xml_node::operator!() const |
| 4261 | { |
| 4262 | return !_root; |
| 4263 | } |
| 4264 | |
| 4265 | PUGI__FN xml_node::iterator xml_node::begin() const |
| 4266 | { |
| 4267 | return iterator(_root ? _root->first_child : 0, _root); |
| 4268 | } |
| 4269 | |
| 4270 | PUGI__FN xml_node::iterator xml_node::end() const |
| 4271 | { |
| 4272 | return iterator(0, _root); |
| 4273 | } |
| 4274 | |
| 4275 | PUGI__FN xml_node::attribute_iterator xml_node::attributes_begin() const |
| 4276 | { |
| 4277 | return attribute_iterator(_root ? _root->first_attribute : 0, _root); |
| 4278 | } |
| 4279 | |
| 4280 | PUGI__FN xml_node::attribute_iterator xml_node::attributes_end() const |
| 4281 | { |
| 4282 | return attribute_iterator(0, _root); |
| 4283 | } |
| 4284 | |
| 4285 | PUGI__FN xml_object_range<xml_node_iterator> xml_node::children() const |
| 4286 | { |
| 4287 | return xml_object_range<xml_node_iterator>(begin(), end()); |
| 4288 | } |
| 4289 | |
| 4290 | PUGI__FN xml_object_range<xml_named_node_iterator> xml_node::children(const char_t* name_) const |
| 4291 | { |
| 4292 | return xml_object_range<xml_named_node_iterator>(xml_named_node_iterator(child(name_)._root, _root, name_), xml_named_node_iterator(0, _root, name_)); |
| 4293 | } |
| 4294 | |
| 4295 | PUGI__FN xml_object_range<xml_attribute_iterator> xml_node::attributes() const |
| 4296 | { |
| 4297 | return xml_object_range<xml_attribute_iterator>(attributes_begin(), attributes_end()); |
| 4298 | } |
| 4299 | |
| 4300 | PUGI__FN bool xml_node::operator==(const xml_node& r) const |
| 4301 | { |
| 4302 | return (_root == r._root); |
| 4303 | } |
| 4304 | |
| 4305 | PUGI__FN bool xml_node::operator!=(const xml_node& r) const |
| 4306 | { |
| 4307 | return (_root != r._root); |
| 4308 | } |
| 4309 | |
| 4310 | PUGI__FN bool xml_node::operator<(const xml_node& r) const |
| 4311 | { |
| 4312 | return (_root < r._root); |
| 4313 | } |
| 4314 | |
| 4315 | PUGI__FN bool xml_node::operator>(const xml_node& r) const |
| 4316 | { |
| 4317 | return (_root > r._root); |
| 4318 | } |
| 4319 | |
| 4320 | PUGI__FN bool xml_node::operator<=(const xml_node& r) const |
| 4321 | { |
| 4322 | return (_root <= r._root); |
| 4323 | } |
| 4324 | |
| 4325 | PUGI__FN bool xml_node::operator>=(const xml_node& r) const |
| 4326 | { |
| 4327 | return (_root >= r._root); |
| 4328 | } |
| 4329 | |
| 4330 | PUGI__FN bool xml_node::empty() const |
| 4331 | { |
| 4332 | return !_root; |
| 4333 | } |
| 4334 | |
| 4335 | PUGI__FN const char_t* xml_node::name() const |
| 4336 | { |
| 4337 | return (_root && _root->name) ? _root->name : PUGIXML_TEXT(""); |
| 4338 | } |
| 4339 | |
| 4340 | PUGI__FN xml_node_type xml_node::type() const |
| 4341 | { |
| 4342 | return _root ? static_cast<xml_node_type>((_root->header & impl::xml_memory_page_type_mask) + 1) : node_null; |
| 4343 | } |
| 4344 | |
| 4345 | PUGI__FN const char_t* xml_node::value() const |
| 4346 | { |
| 4347 | return (_root && _root->value) ? _root->value : PUGIXML_TEXT(""); |
| 4348 | } |
| 4349 | |
| 4350 | PUGI__FN xml_node xml_node::child(const char_t* name_) const |
| 4351 | { |
| 4352 | if (!_root) return xml_node(); |
| 4353 | |
| 4354 | for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) |
| 4355 | if (i->name && impl::strequal(name_, i->name)) return xml_node(i); |
| 4356 | |
| 4357 | return xml_node(); |
| 4358 | } |
| 4359 | |
| 4360 | PUGI__FN xml_attribute xml_node::attribute(const char_t* name_) const |
| 4361 | { |
| 4362 | if (!_root) return xml_attribute(); |
| 4363 | |
| 4364 | for (xml_attribute_struct* i = _root->first_attribute; i; i = i->next_attribute) |
| 4365 | if (i->name && impl::strequal(name_, i->name)) |
| 4366 | return xml_attribute(i); |
| 4367 | |
| 4368 | return xml_attribute(); |
| 4369 | } |
| 4370 | |
| 4371 | PUGI__FN xml_node xml_node::next_sibling(const char_t* name_) const |
| 4372 | { |
| 4373 | if (!_root) return xml_node(); |
| 4374 | |
| 4375 | for (xml_node_struct* i = _root->next_sibling; i; i = i->next_sibling) |
| 4376 | if (i->name && impl::strequal(name_, i->name)) return xml_node(i); |
| 4377 | |
| 4378 | return xml_node(); |
| 4379 | } |
| 4380 | |
| 4381 | PUGI__FN xml_node xml_node::next_sibling() const |
| 4382 | { |
| 4383 | if (!_root) return xml_node(); |
| 4384 | |
| 4385 | if (_root->next_sibling) return xml_node(_root->next_sibling); |
| 4386 | else return xml_node(); |
| 4387 | } |
| 4388 | |
| 4389 | PUGI__FN xml_node xml_node::previous_sibling(const char_t* name_) const |
| 4390 | { |
| 4391 | if (!_root) return xml_node(); |
| 4392 | |
| 4393 | for (xml_node_struct* i = _root->prev_sibling_c; i->next_sibling; i = i->prev_sibling_c) |
| 4394 | if (i->name && impl::strequal(name_, i->name)) return xml_node(i); |
| 4395 | |
| 4396 | return xml_node(); |
| 4397 | } |
| 4398 | |
| 4399 | PUGI__FN xml_node xml_node::previous_sibling() const |
| 4400 | { |
| 4401 | if (!_root) return xml_node(); |
| 4402 | |
| 4403 | if (_root->prev_sibling_c->next_sibling) return xml_node(_root->prev_sibling_c); |
| 4404 | else return xml_node(); |
| 4405 | } |
| 4406 | |
| 4407 | PUGI__FN xml_node xml_node::parent() const |
| 4408 | { |
| 4409 | return _root ? xml_node(_root->parent) : xml_node(); |
| 4410 | } |
| 4411 | |
| 4412 | PUGI__FN xml_node xml_node::root() const |
| 4413 | { |
| 4414 | if (!_root) return xml_node(); |
| 4415 | |
| 4416 | impl::xml_memory_page* page = reinterpret_cast<impl::xml_memory_page*>(_root->header & impl::xml_memory_page_pointer_mask); |
| 4417 | |
| 4418 | return xml_node(static_cast<impl::xml_document_struct*>(page->allocator)); |
| 4419 | } |
| 4420 | |
| 4421 | PUGI__FN xml_text xml_node::text() const |
| 4422 | { |
| 4423 | return xml_text(_root); |
| 4424 | } |
| 4425 | |
| 4426 | PUGI__FN const char_t* xml_node::child_value() const |
| 4427 | { |
| 4428 | if (!_root) return PUGIXML_TEXT(""); |
| 4429 | |
| 4430 | for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) |
| 4431 | if (i->value && impl::is_text_node(i)) |
| 4432 | return i->value; |
| 4433 | |
| 4434 | return PUGIXML_TEXT(""); |
| 4435 | } |
| 4436 | |
| 4437 | PUGI__FN const char_t* xml_node::child_value(const char_t* name_) const |
| 4438 | { |
| 4439 | return child(name_).child_value(); |
| 4440 | } |
| 4441 | |
| 4442 | PUGI__FN xml_attribute xml_node::first_attribute() const |
| 4443 | { |
| 4444 | return _root ? xml_attribute(_root->first_attribute) : xml_attribute(); |
| 4445 | } |
| 4446 | |
| 4447 | PUGI__FN xml_attribute xml_node::last_attribute() const |
| 4448 | { |
| 4449 | return _root && _root->first_attribute ? xml_attribute(_root->first_attribute->prev_attribute_c) : xml_attribute(); |
| 4450 | } |
| 4451 | |
| 4452 | PUGI__FN xml_node xml_node::first_child() const |
| 4453 | { |
| 4454 | return _root ? xml_node(_root->first_child) : xml_node(); |
| 4455 | } |
| 4456 | |
| 4457 | PUGI__FN xml_node xml_node::last_child() const |
| 4458 | { |
| 4459 | return _root && _root->first_child ? xml_node(_root->first_child->prev_sibling_c) : xml_node(); |
| 4460 | } |
| 4461 | |
| 4462 | PUGI__FN bool xml_node::set_name(const char_t* rhs) |
| 4463 | { |
| 4464 | switch (type()) |
| 4465 | { |
| 4466 | case node_pi: |
| 4467 | case node_declaration: |
| 4468 | case node_element: |
| 4469 | return impl::strcpy_insitu(_root->name, _root->header, impl::xml_memory_page_name_allocated_mask, rhs); |
| 4470 | |
| 4471 | default: |
| 4472 | return false; |
| 4473 | } |
| 4474 | } |
| 4475 | |
| 4476 | PUGI__FN bool xml_node::set_value(const char_t* rhs) |
| 4477 | { |
| 4478 | switch (type()) |
| 4479 | { |
| 4480 | case node_pi: |
| 4481 | case node_cdata: |
| 4482 | case node_pcdata: |
| 4483 | case node_comment: |
| 4484 | case node_doctype: |
| 4485 | return impl::strcpy_insitu(_root->value, _root->header, impl::xml_memory_page_value_allocated_mask, rhs); |
| 4486 | |
| 4487 | default: |
| 4488 | return false; |
| 4489 | } |
| 4490 | } |
| 4491 | |
| 4492 | PUGI__FN xml_attribute xml_node::append_attribute(const char_t* name_) |
| 4493 | { |
| 4494 | if (type() != node_element && type() != node_declaration) return xml_attribute(); |
| 4495 | |
| 4496 | xml_attribute a(impl::append_attribute_ll(_root, impl::get_allocator(_root))); |
| 4497 | a.set_name(name_); |
| 4498 | |
| 4499 | return a; |
| 4500 | } |
| 4501 | |
| 4502 | PUGI__FN xml_attribute xml_node::prepend_attribute(const char_t* name_) |
| 4503 | { |
| 4504 | if (type() != node_element && type() != node_declaration) return xml_attribute(); |
| 4505 | |
| 4506 | xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root))); |
| 4507 | if (!a) return xml_attribute(); |
| 4508 | |
| 4509 | a.set_name(name_); |
| 4510 | |
| 4511 | xml_attribute_struct* head = _root->first_attribute; |
| 4512 | |
| 4513 | if (head) |
| 4514 | { |
| 4515 | a._attr->prev_attribute_c = head->prev_attribute_c; |
| 4516 | head->prev_attribute_c = a._attr; |
| 4517 | } |
| 4518 | else |
| 4519 | a._attr->prev_attribute_c = a._attr; |
| 4520 | |
| 4521 | a._attr->next_attribute = head; |
| 4522 | _root->first_attribute = a._attr; |
| 4523 | |
| 4524 | return a; |
| 4525 | } |
| 4526 | |
| 4527 | PUGI__FN xml_attribute xml_node::insert_attribute_before(const char_t* name_, const xml_attribute& attr) |
| 4528 | { |
| 4529 | if ((type() != node_element && type() != node_declaration) || attr.empty()) return xml_attribute(); |
| 4530 | |
| 4531 | // check that attribute belongs to *this |
| 4532 | xml_attribute_struct* cur = attr._attr; |
| 4533 | |
| 4534 | while (cur->prev_attribute_c->next_attribute) cur = cur->prev_attribute_c; |
| 4535 | |
| 4536 | if (cur != _root->first_attribute) return xml_attribute(); |
| 4537 | |
| 4538 | xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root))); |
| 4539 | if (!a) return xml_attribute(); |
| 4540 | |
| 4541 | a.set_name(name_); |
| 4542 | |
| 4543 | if (attr._attr->prev_attribute_c->next_attribute) |
| 4544 | attr._attr->prev_attribute_c->next_attribute = a._attr; |
| 4545 | else |
| 4546 | _root->first_attribute = a._attr; |
| 4547 | |
| 4548 | a._attr->prev_attribute_c = attr._attr->prev_attribute_c; |
| 4549 | a._attr->next_attribute = attr._attr; |
| 4550 | attr._attr->prev_attribute_c = a._attr; |
| 4551 | |
| 4552 | return a; |
| 4553 | } |
| 4554 | |
| 4555 | PUGI__FN xml_attribute xml_node::insert_attribute_after(const char_t* name_, const xml_attribute& attr) |
| 4556 | { |
| 4557 | if ((type() != node_element && type() != node_declaration) || attr.empty()) return xml_attribute(); |
| 4558 | |
| 4559 | // check that attribute belongs to *this |
| 4560 | xml_attribute_struct* cur = attr._attr; |
| 4561 | |
| 4562 | while (cur->prev_attribute_c->next_attribute) cur = cur->prev_attribute_c; |
| 4563 | |
| 4564 | if (cur != _root->first_attribute) return xml_attribute(); |
| 4565 | |
| 4566 | xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root))); |
| 4567 | if (!a) return xml_attribute(); |
| 4568 | |
| 4569 | a.set_name(name_); |
| 4570 | |
| 4571 | if (attr._attr->next_attribute) |
| 4572 | attr._attr->next_attribute->prev_attribute_c = a._attr; |
| 4573 | else |
| 4574 | _root->first_attribute->prev_attribute_c = a._attr; |
| 4575 | |
| 4576 | a._attr->next_attribute = attr._attr->next_attribute; |
| 4577 | a._attr->prev_attribute_c = attr._attr; |
| 4578 | attr._attr->next_attribute = a._attr; |
| 4579 | |
| 4580 | return a; |
| 4581 | } |
| 4582 | |
| 4583 | PUGI__FN xml_attribute xml_node::append_copy(const xml_attribute& proto) |
| 4584 | { |
| 4585 | if (!proto) return xml_attribute(); |
| 4586 | |
| 4587 | xml_attribute result = append_attribute(proto.name()); |
| 4588 | result.set_value(proto.value()); |
| 4589 | |
| 4590 | return result; |
| 4591 | } |
| 4592 | |
| 4593 | PUGI__FN xml_attribute xml_node::prepend_copy(const xml_attribute& proto) |
| 4594 | { |
| 4595 | if (!proto) return xml_attribute(); |
| 4596 | |
| 4597 | xml_attribute result = prepend_attribute(proto.name()); |
| 4598 | result.set_value(proto.value()); |
| 4599 | |
| 4600 | return result; |
| 4601 | } |
| 4602 | |
| 4603 | PUGI__FN xml_attribute xml_node::insert_copy_after(const xml_attribute& proto, const xml_attribute& attr) |
| 4604 | { |
| 4605 | if (!proto) return xml_attribute(); |
| 4606 | |
| 4607 | xml_attribute result = insert_attribute_after(proto.name(), attr); |
| 4608 | result.set_value(proto.value()); |
| 4609 | |
| 4610 | return result; |
| 4611 | } |
| 4612 | |
| 4613 | PUGI__FN xml_attribute xml_node::insert_copy_before(const xml_attribute& proto, const xml_attribute& attr) |
| 4614 | { |
| 4615 | if (!proto) return xml_attribute(); |
| 4616 | |
| 4617 | xml_attribute result = insert_attribute_before(proto.name(), attr); |
| 4618 | result.set_value(proto.value()); |
| 4619 | |
| 4620 | return result; |
| 4621 | } |
| 4622 | |
| 4623 | PUGI__FN xml_node xml_node::append_child(xml_node_type type_) |
| 4624 | { |
| 4625 | if (!impl::allow_insert_child(this->type(), type_)) return xml_node(); |
| 4626 | |
| 4627 | xml_node n(impl::append_node(_root, impl::get_allocator(_root), type_)); |
| 4628 | |
| 4629 | if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml")); |
| 4630 | |
| 4631 | return n; |
| 4632 | } |
| 4633 | |
| 4634 | PUGI__FN xml_node xml_node::prepend_child(xml_node_type type_) |
| 4635 | { |
| 4636 | if (!impl::allow_insert_child(this->type(), type_)) return xml_node(); |
| 4637 | |
| 4638 | xml_node n(impl::allocate_node(impl::get_allocator(_root), type_)); |
| 4639 | if (!n) return xml_node(); |
| 4640 | |
| 4641 | n._root->parent = _root; |
| 4642 | |
| 4643 | xml_node_struct* head = _root->first_child; |
| 4644 | |
| 4645 | if (head) |
| 4646 | { |
| 4647 | n._root->prev_sibling_c = head->prev_sibling_c; |
| 4648 | head->prev_sibling_c = n._root; |
| 4649 | } |
| 4650 | else |
| 4651 | n._root->prev_sibling_c = n._root; |
| 4652 | |
| 4653 | n._root->next_sibling = head; |
| 4654 | _root->first_child = n._root; |
| 4655 | |
| 4656 | if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml")); |
| 4657 | |
| 4658 | return n; |
| 4659 | } |
| 4660 | |
| 4661 | PUGI__FN xml_node xml_node::insert_child_before(xml_node_type type_, const xml_node& node) |
| 4662 | { |
| 4663 | if (!impl::allow_insert_child(this->type(), type_)) return xml_node(); |
| 4664 | if (!node._root || node._root->parent != _root) return xml_node(); |
| 4665 | |
| 4666 | xml_node n(impl::allocate_node(impl::get_allocator(_root), type_)); |
| 4667 | if (!n) return xml_node(); |
| 4668 | |
| 4669 | n._root->parent = _root; |
| 4670 | |
| 4671 | if (node._root->prev_sibling_c->next_sibling) |
| 4672 | node._root->prev_sibling_c->next_sibling = n._root; |
| 4673 | else |
| 4674 | _root->first_child = n._root; |
| 4675 | |
| 4676 | n._root->prev_sibling_c = node._root->prev_sibling_c; |
| 4677 | n._root->next_sibling = node._root; |
| 4678 | node._root->prev_sibling_c = n._root; |
| 4679 | |
| 4680 | if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml")); |
| 4681 | |
| 4682 | return n; |
| 4683 | } |
| 4684 | |
| 4685 | PUGI__FN xml_node xml_node::insert_child_after(xml_node_type type_, const xml_node& node) |
| 4686 | { |
| 4687 | if (!impl::allow_insert_child(this->type(), type_)) return xml_node(); |
| 4688 | if (!node._root || node._root->parent != _root) return xml_node(); |
| 4689 | |
| 4690 | xml_node n(impl::allocate_node(impl::get_allocator(_root), type_)); |
| 4691 | if (!n) return xml_node(); |
| 4692 | |
| 4693 | n._root->parent = _root; |
| 4694 | |
| 4695 | if (node._root->next_sibling) |
| 4696 | node._root->next_sibling->prev_sibling_c = n._root; |
| 4697 | else |
| 4698 | _root->first_child->prev_sibling_c = n._root; |
| 4699 | |
| 4700 | n._root->next_sibling = node._root->next_sibling; |
| 4701 | n._root->prev_sibling_c = node._root; |
| 4702 | node._root->next_sibling = n._root; |
| 4703 | |
| 4704 | if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml")); |
| 4705 | |
| 4706 | return n; |
| 4707 | } |
| 4708 | |
| 4709 | PUGI__FN xml_node xml_node::append_child(const char_t* name_) |
| 4710 | { |
| 4711 | xml_node result = append_child(node_element); |
| 4712 | |
| 4713 | result.set_name(name_); |
| 4714 | |
| 4715 | return result; |
| 4716 | } |
| 4717 | |
| 4718 | PUGI__FN xml_node xml_node::prepend_child(const char_t* name_) |
| 4719 | { |
| 4720 | xml_node result = prepend_child(node_element); |
| 4721 | |
| 4722 | result.set_name(name_); |
| 4723 | |
| 4724 | return result; |
| 4725 | } |
| 4726 | |
| 4727 | PUGI__FN xml_node xml_node::insert_child_after(const char_t* name_, const xml_node& node) |
| 4728 | { |
| 4729 | xml_node result = insert_child_after(node_element, node); |
| 4730 | |
| 4731 | result.set_name(name_); |
| 4732 | |
| 4733 | return result; |
| 4734 | } |
| 4735 | |
| 4736 | PUGI__FN xml_node xml_node::insert_child_before(const char_t* name_, const xml_node& node) |
| 4737 | { |
| 4738 | xml_node result = insert_child_before(node_element, node); |
| 4739 | |
| 4740 | result.set_name(name_); |
| 4741 | |
| 4742 | return result; |
| 4743 | } |
| 4744 | |
| 4745 | PUGI__FN xml_node xml_node::append_copy(const xml_node& proto) |
| 4746 | { |
| 4747 | xml_node result = append_child(proto.type()); |
| 4748 | |
| 4749 | if (result) impl::recursive_copy_skip(result, proto, result); |
| 4750 | |
| 4751 | return result; |
| 4752 | } |
| 4753 | |
| 4754 | PUGI__FN xml_node xml_node::prepend_copy(const xml_node& proto) |
| 4755 | { |
| 4756 | xml_node result = prepend_child(proto.type()); |
| 4757 | |
| 4758 | if (result) impl::recursive_copy_skip(result, proto, result); |
| 4759 | |
| 4760 | return result; |
| 4761 | } |
| 4762 | |
| 4763 | PUGI__FN xml_node xml_node::insert_copy_after(const xml_node& proto, const xml_node& node) |
| 4764 | { |
| 4765 | xml_node result = insert_child_after(proto.type(), node); |
| 4766 | |
| 4767 | if (result) impl::recursive_copy_skip(result, proto, result); |
| 4768 | |
| 4769 | return result; |
| 4770 | } |
| 4771 | |
| 4772 | PUGI__FN xml_node xml_node::insert_copy_before(const xml_node& proto, const xml_node& node) |
| 4773 | { |
| 4774 | xml_node result = insert_child_before(proto.type(), node); |
| 4775 | |
| 4776 | if (result) impl::recursive_copy_skip(result, proto, result); |
| 4777 | |
| 4778 | return result; |
| 4779 | } |
| 4780 | |
| 4781 | PUGI__FN bool xml_node::remove_attribute(const char_t* name_) |
| 4782 | { |
| 4783 | return remove_attribute(attribute(name_)); |
| 4784 | } |
| 4785 | |
| 4786 | PUGI__FN bool xml_node::remove_attribute(const xml_attribute& a) |
| 4787 | { |
| 4788 | if (!_root || !a._attr) return false; |
| 4789 | |
| 4790 | // check that attribute belongs to *this |
| 4791 | xml_attribute_struct* attr = a._attr; |
| 4792 | |
| 4793 | while (attr->prev_attribute_c->next_attribute) attr = attr->prev_attribute_c; |
| 4794 | |
| 4795 | if (attr != _root->first_attribute) return false; |
| 4796 | |
| 4797 | if (a._attr->next_attribute) a._attr->next_attribute->prev_attribute_c = a._attr->prev_attribute_c; |
| 4798 | else if (_root->first_attribute) _root->first_attribute->prev_attribute_c = a._attr->prev_attribute_c; |
| 4799 | |
| 4800 | if (a._attr->prev_attribute_c->next_attribute) a._attr->prev_attribute_c->next_attribute = a._attr->next_attribute; |
| 4801 | else _root->first_attribute = a._attr->next_attribute; |
| 4802 | |
| 4803 | impl::destroy_attribute(a._attr, impl::get_allocator(_root)); |
| 4804 | |
| 4805 | return true; |
| 4806 | } |
| 4807 | |
| 4808 | PUGI__FN bool xml_node::remove_child(const char_t* name_) |
| 4809 | { |
| 4810 | return remove_child(child(name_)); |
| 4811 | } |
| 4812 | |
| 4813 | PUGI__FN bool xml_node::remove_child(const xml_node& n) |
| 4814 | { |
| 4815 | if (!_root || !n._root || n._root->parent != _root) return false; |
| 4816 | |
| 4817 | if (n._root->next_sibling) n._root->next_sibling->prev_sibling_c = n._root->prev_sibling_c; |
| 4818 | else if (_root->first_child) _root->first_child->prev_sibling_c = n._root->prev_sibling_c; |
| 4819 | |
| 4820 | if (n._root->prev_sibling_c->next_sibling) n._root->prev_sibling_c->next_sibling = n._root->next_sibling; |
| 4821 | else _root->first_child = n._root->next_sibling; |
| 4822 | |
| 4823 | impl::destroy_node(n._root, impl::get_allocator(_root)); |
| 4824 | |
| 4825 | return true; |
| 4826 | } |
| 4827 | |
| 4828 | PUGI__FN xml_parse_result xml_node::append_buffer(const void* contents, size_t size, unsigned int options, xml_encoding encoding) |
| 4829 | { |
| 4830 | // append_buffer is only valid for elements/documents |
| 4831 | if (!impl::allow_insert_child(type(), node_element)) return impl::make_parse_result(status_append_invalid_root); |
| 4832 | |
| 4833 | // get document node |
| 4834 | impl::xml_document_struct* doc = static_cast<impl::xml_document_struct*>(root()._root); |
| 4835 | assert(doc); |
| 4836 | |
| 4837 | // get extra buffer element (we'll store the document fragment buffer there so that we can deallocate it later) |
| 4838 | impl::xml_memory_page* page = 0; |
| 4839 | impl::xml_extra_buffer* extra = static_cast<impl::xml_extra_buffer*>(doc->allocate_memory(sizeof(impl::xml_extra_buffer), page)); |
| 4840 | (void)page; |
| 4841 | |
| 4842 | if (!extra) return impl::make_parse_result(status_out_of_memory); |
| 4843 | |
| 4844 | // save name; name of the root has to be NULL before parsing - otherwise closing node mismatches will not be detected at the top level |
| 4845 | char_t* rootname = _root->name; |
| 4846 | _root->name = 0; |
| 4847 | |
| 4848 | // parse |
| 4849 | char_t* buffer = 0; |
| 4850 | xml_parse_result res = impl::load_buffer_impl(doc, _root, const_cast<void*>(contents), size, options, encoding, false, false, &buffer); |
| 4851 | |
| 4852 | // restore name |
| 4853 | _root->name = rootname; |
| 4854 | |
| 4855 | // add extra buffer to the list |
| 4856 | extra->buffer = buffer; |
| 4857 | extra->next = doc->extra_buffers; |
| 4858 | doc->extra_buffers = extra; |
| 4859 | |
| 4860 | return res; |
| 4861 | } |
| 4862 | |
| 4863 | PUGI__FN xml_node xml_node::find_child_by_attribute(const char_t* name_, const char_t* attr_name, const char_t* attr_value) const |
| 4864 | { |
| 4865 | if (!_root) return xml_node(); |
| 4866 | |
| 4867 | for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) |
| 4868 | if (i->name && impl::strequal(name_, i->name)) |
| 4869 | { |
| 4870 | for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute) |
| 4871 | if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value : PUGIXML_TEXT(""))) |
| 4872 | return xml_node(i); |
| 4873 | } |
| 4874 | |
| 4875 | return xml_node(); |
| 4876 | } |
| 4877 | |
| 4878 | PUGI__FN xml_node xml_node::find_child_by_attribute(const char_t* attr_name, const char_t* attr_value) const |
| 4879 | { |
| 4880 | if (!_root) return xml_node(); |
| 4881 | |
| 4882 | for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) |
| 4883 | for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute) |
| 4884 | if (a->name && impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value ? a->value : PUGIXML_TEXT(""))) |
| 4885 | return xml_node(i); |
| 4886 | |
| 4887 | return xml_node(); |
| 4888 | } |
| 4889 | |
| 4890 | #ifndef PUGIXML_NO_STL |
| 4891 | PUGI__FN string_t xml_node::path(char_t delimiter) const |
| 4892 | { |
| 4893 | xml_node cursor = *this; // Make a copy. |
| 4894 | |
| 4895 | string_t result = cursor.name(); |
| 4896 | |
| 4897 | while (cursor.parent()) |
| 4898 | { |
| 4899 | cursor = cursor.parent(); |
| 4900 | |
| 4901 | string_t temp = cursor.name(); |
| 4902 | temp += delimiter; |
| 4903 | temp += result; |
| 4904 | result.swap(temp); |
| 4905 | } |
| 4906 | |
| 4907 | return result; |
| 4908 | } |
| 4909 | #endif |
| 4910 | |
| 4911 | PUGI__FN xml_node xml_node::first_element_by_path(const char_t* path_, char_t delimiter) const |
| 4912 | { |
| 4913 | xml_node found = *this; // Current search context. |
| 4914 | |
| 4915 | if (!_root || !path_ || !path_[0]) return found; |
| 4916 | |
| 4917 | if (path_[0] == delimiter) |
| 4918 | { |
| 4919 | // Absolute path; e.g. '/foo/bar' |
| 4920 | found = found.root(); |
| 4921 | ++path_; |
| 4922 | } |
| 4923 | |
| 4924 | const char_t* path_segment = path_; |
| 4925 | |
| 4926 | while (*path_segment == delimiter) ++path_segment; |
| 4927 | |
| 4928 | const char_t* path_segment_end = path_segment; |
| 4929 | |
| 4930 | while (*path_segment_end && *path_segment_end != delimiter) ++path_segment_end; |
| 4931 | |
| 4932 | if (path_segment == path_segment_end) return found; |
| 4933 | |
| 4934 | const char_t* next_segment = path_segment_end; |
| 4935 | |
| 4936 | while (*next_segment == delimiter) ++next_segment; |
| 4937 | |
| 4938 | if (*path_segment == '.' && path_segment + 1 == path_segment_end) |
| 4939 | return found.first_element_by_path(next_segment, delimiter); |
| 4940 | else if (*path_segment == '.' && *(path_segment+1) == '.' && path_segment + 2 == path_segment_end) |
| 4941 | return found.parent().first_element_by_path(next_segment, delimiter); |
| 4942 | else |
| 4943 | { |
| 4944 | for (xml_node_struct* j = found._root->first_child; j; j = j->next_sibling) |
| 4945 | { |
| 4946 | if (j->name && impl::strequalrange(j->name, path_segment, static_cast<size_t>(path_segment_end - path_segment))) |
| 4947 | { |
| 4948 | xml_node subsearch = xml_node(j).first_element_by_path(next_segment, delimiter); |
| 4949 | |
| 4950 | if (subsearch) return subsearch; |
| 4951 | } |
| 4952 | } |
| 4953 | |
| 4954 | return xml_node(); |
| 4955 | } |
| 4956 | } |
| 4957 | |
| 4958 | PUGI__FN bool xml_node::traverse(xml_tree_walker& walker) |
| 4959 | { |
| 4960 | walker._depth = -1; |
| 4961 | |
| 4962 | xml_node arg_begin = *this; |
| 4963 | if (!walker.begin(arg_begin)) return false; |
| 4964 | |
| 4965 | xml_node cur = first_child(); |
| 4966 | |
| 4967 | if (cur) |
| 4968 | { |
| 4969 | ++walker._depth; |
| 4970 | |
| 4971 | do |
| 4972 | { |
| 4973 | xml_node arg_for_each = cur; |
| 4974 | if (!walker.for_each(arg_for_each)) |
| 4975 | return false; |
| 4976 | |
| 4977 | if (cur.first_child()) |
| 4978 | { |
| 4979 | ++walker._depth; |
| 4980 | cur = cur.first_child(); |
| 4981 | } |
| 4982 | else if (cur.next_sibling()) |
| 4983 | cur = cur.next_sibling(); |
| 4984 | else |
| 4985 | { |
| 4986 | // Borland C++ workaround |
| 4987 | while (!cur.next_sibling() && cur != *this && !cur.parent().empty()) |
| 4988 | { |
| 4989 | --walker._depth; |
| 4990 | cur = cur.parent(); |
| 4991 | } |
| 4992 | |
| 4993 | if (cur != *this) |
| 4994 | cur = cur.next_sibling(); |
| 4995 | } |
| 4996 | } |
| 4997 | while (cur && cur != *this); |
| 4998 | } |
| 4999 | |
| 5000 | assert(walker._depth == -1); |
| 5001 | |
| 5002 | xml_node arg_end = *this; |
| 5003 | return walker.end(arg_end); |
| 5004 | } |
| 5005 | |
| 5006 | PUGI__FN size_t xml_node::hash_value() const |
| 5007 | { |
| 5008 | return static_cast<size_t>(reinterpret_cast<uintptr_t>(_root) / sizeof(xml_node_struct)); |
| 5009 | } |
| 5010 | |
| 5011 | PUGI__FN xml_node_struct* xml_node::internal_object() const |
| 5012 | { |
| 5013 | return _root; |
| 5014 | } |
| 5015 | |
| 5016 | PUGI__FN void xml_node::print(xml_writer& writer, const char_t* indent, unsigned int flags, xml_encoding encoding, unsigned int depth) const |
| 5017 | { |
| 5018 | if (!_root) return; |
| 5019 | |
| 5020 | impl::xml_buffered_writer buffered_writer(writer, encoding); |
| 5021 | |
| 5022 | impl::node_output(buffered_writer, *this, indent, flags, depth); |
| 5023 | } |
| 5024 | |
| 5025 | #ifndef PUGIXML_NO_STL |
| 5026 | PUGI__FN void xml_node::print(std::basic_ostream<char, std::char_traits<char> >& stream, const char_t* indent, unsigned int flags, xml_encoding encoding, unsigned int depth) const |
| 5027 | { |
| 5028 | xml_writer_stream writer(stream); |
| 5029 | |
| 5030 | print(writer, indent, flags, encoding, depth); |
| 5031 | } |
| 5032 | |
| 5033 | PUGI__FN void xml_node::print(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream, const char_t* indent, unsigned int flags, unsigned int depth) const |
| 5034 | { |
| 5035 | xml_writer_stream writer(stream); |
| 5036 | |
| 5037 | print(writer, indent, flags, encoding_wchar, depth); |
| 5038 | } |
| 5039 | #endif |
| 5040 | |
| 5041 | PUGI__FN ptrdiff_t xml_node::offset_debug() const |
| 5042 | { |
| 5043 | xml_node_struct* r = root()._root; |
| 5044 | |
| 5045 | if (!r) return -1; |
| 5046 | |
| 5047 | const char_t* buffer = static_cast<impl::xml_document_struct*>(r)->buffer; |
| 5048 | |
| 5049 | if (!buffer) return -1; |
| 5050 | |
| 5051 | switch (type()) |
| 5052 | { |
| 5053 | case node_document: |
| 5054 | return 0; |
| 5055 | |
| 5056 | case node_element: |
| 5057 | case node_declaration: |
| 5058 | case node_pi: |
| 5059 | return (_root->header & impl::xml_memory_page_name_allocated_mask) ? -1 : _root->name - buffer; |
| 5060 | |
| 5061 | case node_pcdata: |
| 5062 | case node_cdata: |
| 5063 | case node_comment: |
| 5064 | case node_doctype: |
| 5065 | return (_root->header & impl::xml_memory_page_value_allocated_mask) ? -1 : _root->value - buffer; |
| 5066 | |
| 5067 | default: |
| 5068 | return -1; |
| 5069 | } |
| 5070 | } |
| 5071 | |
| 5072 | #ifdef __BORLANDC__ |
| 5073 | PUGI__FN bool operator&&(const xml_node& lhs, bool rhs) |
| 5074 | { |
| 5075 | return (bool)lhs && rhs; |
| 5076 | } |
| 5077 | |
| 5078 | PUGI__FN bool operator||(const xml_node& lhs, bool rhs) |
| 5079 | { |
| 5080 | return (bool)lhs || rhs; |
| 5081 | } |
| 5082 | #endif |
| 5083 | |
| 5084 | PUGI__FN xml_text::xml_text(xml_node_struct* root): _root(root) |
| 5085 | { |
| 5086 | } |
| 5087 | |
| 5088 | PUGI__FN xml_node_struct* xml_text::_data() const |
| 5089 | { |
| 5090 | if (!_root || impl::is_text_node(_root)) return _root; |
| 5091 | |
| 5092 | for (xml_node_struct* node = _root->first_child; node; node = node->next_sibling) |
| 5093 | if (impl::is_text_node(node)) |
| 5094 | return node; |
| 5095 | |
| 5096 | return 0; |
| 5097 | } |
| 5098 | |
| 5099 | PUGI__FN xml_node_struct* xml_text::_data_new() |
| 5100 | { |
| 5101 | xml_node_struct* d = _data(); |
| 5102 | if (d) return d; |
| 5103 | |
| 5104 | return xml_node(_root).append_child(node_pcdata).internal_object(); |
| 5105 | } |
| 5106 | |
| 5107 | PUGI__FN xml_text::xml_text(): _root(0) |
| 5108 | { |
| 5109 | } |
| 5110 | |
| 5111 | PUGI__FN static void unspecified_bool_xml_text(xml_text***) |
| 5112 | { |
| 5113 | } |
| 5114 | |
| 5115 | PUGI__FN xml_text::operator xml_text::unspecified_bool_type() const |
| 5116 | { |
| 5117 | return _data() ? unspecified_bool_xml_text : 0; |
| 5118 | } |
| 5119 | |
| 5120 | PUGI__FN bool xml_text::operator!() const |
| 5121 | { |
| 5122 | return !_data(); |
| 5123 | } |
| 5124 | |
| 5125 | PUGI__FN bool xml_text::empty() const |
| 5126 | { |
| 5127 | return _data() == 0; |
| 5128 | } |
| 5129 | |
| 5130 | PUGI__FN const char_t* xml_text::get() const |
| 5131 | { |
| 5132 | xml_node_struct* d = _data(); |
| 5133 | |
| 5134 | return (d && d->value) ? d->value : PUGIXML_TEXT(""); |
| 5135 | } |
| 5136 | |
| 5137 | PUGI__FN const char_t* xml_text::as_string(const char_t* def) const |
| 5138 | { |
| 5139 | xml_node_struct* d = _data(); |
| 5140 | |
| 5141 | return (d && d->value) ? d->value : def; |
| 5142 | } |
| 5143 | |
| 5144 | PUGI__FN int xml_text::as_int(int def) const |
| 5145 | { |
| 5146 | xml_node_struct* d = _data(); |
| 5147 | |
| 5148 | return impl::get_value_int(d ? d->value : 0, def); |
| 5149 | } |
| 5150 | |
| 5151 | PUGI__FN unsigned int xml_text::as_uint(unsigned int def) const |
| 5152 | { |
| 5153 | xml_node_struct* d = _data(); |
| 5154 | |
| 5155 | return impl::get_value_uint(d ? d->value : 0, def); |
| 5156 | } |
| 5157 | |
| 5158 | PUGI__FN double xml_text::as_double(double def) const |
| 5159 | { |
| 5160 | xml_node_struct* d = _data(); |
| 5161 | |
| 5162 | return impl::get_value_double(d ? d->value : 0, def); |
| 5163 | } |
| 5164 | |
| 5165 | PUGI__FN float xml_text::as_float(float def) const |
| 5166 | { |
| 5167 | xml_node_struct* d = _data(); |
| 5168 | |
| 5169 | return impl::get_value_float(d ? d->value : 0, def); |
| 5170 | } |
| 5171 | |
| 5172 | PUGI__FN bool xml_text::as_bool(bool def) const |
| 5173 | { |
| 5174 | xml_node_struct* d = _data(); |
| 5175 | |
| 5176 | return impl::get_value_bool(d ? d->value : 0, def); |
| 5177 | } |
| 5178 | |
| 5179 | #ifdef PUGIXML_HAS_LONG_LONG |
| 5180 | PUGI__FN long long xml_text::as_llong(long long def) const |
| 5181 | { |
| 5182 | xml_node_struct* d = _data(); |
| 5183 | |
| 5184 | return impl::get_value_llong(d ? d->value : 0, def); |
| 5185 | } |
| 5186 | |
| 5187 | PUGI__FN unsigned long long xml_text::as_ullong(unsigned long long def) const |
| 5188 | { |
| 5189 | xml_node_struct* d = _data(); |
| 5190 | |
| 5191 | return impl::get_value_ullong(d ? d->value : 0, def); |
| 5192 | } |
| 5193 | #endif |
| 5194 | |
| 5195 | PUGI__FN bool xml_text::set(const char_t* rhs) |
| 5196 | { |
| 5197 | xml_node_struct* dn = _data_new(); |
| 5198 | |
| 5199 | return dn ? impl::strcpy_insitu(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; |
| 5200 | } |
| 5201 | |
| 5202 | PUGI__FN bool xml_text::set(int rhs) |
| 5203 | { |
| 5204 | xml_node_struct* dn = _data_new(); |
| 5205 | |
| 5206 | return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; |
| 5207 | } |
| 5208 | |
| 5209 | PUGI__FN bool xml_text::set(unsigned int rhs) |
| 5210 | { |
| 5211 | xml_node_struct* dn = _data_new(); |
| 5212 | |
| 5213 | return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; |
| 5214 | } |
| 5215 | |
| 5216 | PUGI__FN bool xml_text::set(double rhs) |
| 5217 | { |
| 5218 | xml_node_struct* dn = _data_new(); |
| 5219 | |
| 5220 | return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; |
| 5221 | } |
| 5222 | |
| 5223 | PUGI__FN bool xml_text::set(bool rhs) |
| 5224 | { |
| 5225 | xml_node_struct* dn = _data_new(); |
| 5226 | |
| 5227 | return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; |
| 5228 | } |
| 5229 | |
| 5230 | #ifdef PUGIXML_HAS_LONG_LONG |
| 5231 | PUGI__FN bool xml_text::set(long long rhs) |
| 5232 | { |
| 5233 | xml_node_struct* dn = _data_new(); |
| 5234 | |
| 5235 | return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; |
| 5236 | } |
| 5237 | |
| 5238 | PUGI__FN bool xml_text::set(unsigned long long rhs) |
| 5239 | { |
| 5240 | xml_node_struct* dn = _data_new(); |
| 5241 | |
| 5242 | return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false; |
| 5243 | } |
| 5244 | #endif |
| 5245 | |
| 5246 | PUGI__FN xml_text& xml_text::operator=(const char_t* rhs) |
| 5247 | { |
| 5248 | set(rhs); |
| 5249 | return *this; |
| 5250 | } |
| 5251 | |
| 5252 | PUGI__FN xml_text& xml_text::operator=(int rhs) |
| 5253 | { |
| 5254 | set(rhs); |
| 5255 | return *this; |
| 5256 | } |
| 5257 | |
| 5258 | PUGI__FN xml_text& xml_text::operator=(unsigned int rhs) |
| 5259 | { |
| 5260 | set(rhs); |
| 5261 | return *this; |
| 5262 | } |
| 5263 | |
| 5264 | PUGI__FN xml_text& xml_text::operator=(double rhs) |
| 5265 | { |
| 5266 | set(rhs); |
| 5267 | return *this; |
| 5268 | } |
| 5269 | |
| 5270 | PUGI__FN xml_text& xml_text::operator=(bool rhs) |
| 5271 | { |
| 5272 | set(rhs); |
| 5273 | return *this; |
| 5274 | } |
| 5275 | |
| 5276 | #ifdef PUGIXML_HAS_LONG_LONG |
| 5277 | PUGI__FN xml_text& xml_text::operator=(long long rhs) |
| 5278 | { |
| 5279 | set(rhs); |
| 5280 | return *this; |
| 5281 | } |
| 5282 | |
| 5283 | PUGI__FN xml_text& xml_text::operator=(unsigned long long rhs) |
| 5284 | { |
| 5285 | set(rhs); |
| 5286 | return *this; |
| 5287 | } |
| 5288 | #endif |
| 5289 | |
| 5290 | PUGI__FN xml_node xml_text::data() const |
| 5291 | { |
| 5292 | return xml_node(_data()); |
| 5293 | } |
| 5294 | |
| 5295 | #ifdef __BORLANDC__ |
| 5296 | PUGI__FN bool operator&&(const xml_text& lhs, bool rhs) |
| 5297 | { |
| 5298 | return (bool)lhs && rhs; |
| 5299 | } |
| 5300 | |
| 5301 | PUGI__FN bool operator||(const xml_text& lhs, bool rhs) |
| 5302 | { |
| 5303 | return (bool)lhs || rhs; |
| 5304 | } |
| 5305 | #endif |
| 5306 | |
| 5307 | PUGI__FN xml_node_iterator::xml_node_iterator() |
| 5308 | { |
| 5309 | } |
| 5310 | |
| 5311 | PUGI__FN xml_node_iterator::xml_node_iterator(const xml_node& node): _wrap(node), _parent(node.parent()) |
| 5312 | { |
| 5313 | } |
| 5314 | |
| 5315 | PUGI__FN xml_node_iterator::xml_node_iterator(xml_node_struct* ref, xml_node_struct* parent): _wrap(ref), _parent(parent) |
| 5316 | { |
| 5317 | } |
| 5318 | |
| 5319 | PUGI__FN bool xml_node_iterator::operator==(const xml_node_iterator& rhs) const |
| 5320 | { |
| 5321 | return _wrap._root == rhs._wrap._root && _parent._root == rhs._parent._root; |
| 5322 | } |
| 5323 | |
| 5324 | PUGI__FN bool xml_node_iterator::operator!=(const xml_node_iterator& rhs) const |
| 5325 | { |
| 5326 | return _wrap._root != rhs._wrap._root || _parent._root != rhs._parent._root; |
| 5327 | } |
| 5328 | |
| 5329 | PUGI__FN xml_node& xml_node_iterator::operator*() const |
| 5330 | { |
| 5331 | assert(_wrap._root); |
| 5332 | return _wrap; |
| 5333 | } |
| 5334 | |
| 5335 | PUGI__FN xml_node* xml_node_iterator::operator->() const |
| 5336 | { |
| 5337 | assert(_wrap._root); |
| 5338 | return const_cast<xml_node*>(&_wrap); // BCC32 workaround |
| 5339 | } |
| 5340 | |
| 5341 | PUGI__FN const xml_node_iterator& xml_node_iterator::operator++() |
| 5342 | { |
| 5343 | assert(_wrap._root); |
| 5344 | _wrap._root = _wrap._root->next_sibling; |
| 5345 | return *this; |
| 5346 | } |
| 5347 | |
| 5348 | PUGI__FN xml_node_iterator xml_node_iterator::operator++(int) |
| 5349 | { |
| 5350 | xml_node_iterator temp = *this; |
| 5351 | ++*this; |
| 5352 | return temp; |
| 5353 | } |
| 5354 | |
| 5355 | PUGI__FN const xml_node_iterator& xml_node_iterator::operator--() |
| 5356 | { |
| 5357 | _wrap = _wrap._root ? _wrap.previous_sibling() : _parent.last_child(); |
| 5358 | return *this; |
| 5359 | } |
| 5360 | |
| 5361 | PUGI__FN xml_node_iterator xml_node_iterator::operator--(int) |
| 5362 | { |
| 5363 | xml_node_iterator temp = *this; |
| 5364 | --*this; |
| 5365 | return temp; |
| 5366 | } |
| 5367 | |
| 5368 | PUGI__FN xml_attribute_iterator::xml_attribute_iterator() |
| 5369 | { |
| 5370 | } |
| 5371 | |
| 5372 | PUGI__FN xml_attribute_iterator::xml_attribute_iterator(const xml_attribute& attr, const xml_node& parent): _wrap(attr), _parent(parent) |
| 5373 | { |
| 5374 | } |
| 5375 | |
| 5376 | PUGI__FN xml_attribute_iterator::xml_attribute_iterator(xml_attribute_struct* ref, xml_node_struct* parent): _wrap(ref), _parent(parent) |
| 5377 | { |
| 5378 | } |
| 5379 | |
| 5380 | PUGI__FN bool xml_attribute_iterator::operator==(const xml_attribute_iterator& rhs) const |
| 5381 | { |
| 5382 | return _wrap._attr == rhs._wrap._attr && _parent._root == rhs._parent._root; |
| 5383 | } |
| 5384 | |
| 5385 | PUGI__FN bool xml_attribute_iterator::operator!=(const xml_attribute_iterator& rhs) const |
| 5386 | { |
| 5387 | return _wrap._attr != rhs._wrap._attr || _parent._root != rhs._parent._root; |
| 5388 | } |
| 5389 | |
| 5390 | PUGI__FN xml_attribute& xml_attribute_iterator::operator*() const |
| 5391 | { |
| 5392 | assert(_wrap._attr); |
| 5393 | return _wrap; |
| 5394 | } |
| 5395 | |
| 5396 | PUGI__FN xml_attribute* xml_attribute_iterator::operator->() const |
| 5397 | { |
| 5398 | assert(_wrap._attr); |
| 5399 | return const_cast<xml_attribute*>(&_wrap); // BCC32 workaround |
| 5400 | } |
| 5401 | |
| 5402 | PUGI__FN const xml_attribute_iterator& xml_attribute_iterator::operator++() |
| 5403 | { |
| 5404 | assert(_wrap._attr); |
| 5405 | _wrap._attr = _wrap._attr->next_attribute; |
| 5406 | return *this; |
| 5407 | } |
| 5408 | |
| 5409 | PUGI__FN xml_attribute_iterator xml_attribute_iterator::operator++(int) |
| 5410 | { |
| 5411 | xml_attribute_iterator temp = *this; |
| 5412 | ++*this; |
| 5413 | return temp; |
| 5414 | } |
| 5415 | |
| 5416 | PUGI__FN const xml_attribute_iterator& xml_attribute_iterator::operator--() |
| 5417 | { |
| 5418 | _wrap = _wrap._attr ? _wrap.previous_attribute() : _parent.last_attribute(); |
| 5419 | return *this; |
| 5420 | } |
| 5421 | |
| 5422 | PUGI__FN xml_attribute_iterator xml_attribute_iterator::operator--(int) |
| 5423 | { |
| 5424 | xml_attribute_iterator temp = *this; |
| 5425 | --*this; |
| 5426 | return temp; |
| 5427 | } |
| 5428 | |
| 5429 | PUGI__FN xml_named_node_iterator::xml_named_node_iterator(): _name(0) |
| 5430 | { |
| 5431 | } |
| 5432 | |
| 5433 | PUGI__FN xml_named_node_iterator::xml_named_node_iterator(const xml_node& node, const char_t* name): _wrap(node), _parent(node.parent()), _name(name) |
| 5434 | { |
| 5435 | } |
| 5436 | |
| 5437 | PUGI__FN xml_named_node_iterator::xml_named_node_iterator(xml_node_struct* ref, xml_node_struct* parent, const char_t* name): _wrap(ref), _parent(parent), _name(name) |
| 5438 | { |
| 5439 | } |
| 5440 | |
| 5441 | PUGI__FN bool xml_named_node_iterator::operator==(const xml_named_node_iterator& rhs) const |
| 5442 | { |
| 5443 | return _wrap._root == rhs._wrap._root && _parent._root == rhs._parent._root; |
| 5444 | } |
| 5445 | |
| 5446 | PUGI__FN bool xml_named_node_iterator::operator!=(const xml_named_node_iterator& rhs) const |
| 5447 | { |
| 5448 | return _wrap._root != rhs._wrap._root || _parent._root != rhs._parent._root; |
| 5449 | } |
| 5450 | |
| 5451 | PUGI__FN xml_node& xml_named_node_iterator::operator*() const |
| 5452 | { |
| 5453 | assert(_wrap._root); |
| 5454 | return _wrap; |
| 5455 | } |
| 5456 | |
| 5457 | PUGI__FN xml_node* xml_named_node_iterator::operator->() const |
| 5458 | { |
| 5459 | assert(_wrap._root); |
| 5460 | return const_cast<xml_node*>(&_wrap); // BCC32 workaround |
| 5461 | } |
| 5462 | |
| 5463 | PUGI__FN const xml_named_node_iterator& xml_named_node_iterator::operator++() |
| 5464 | { |
| 5465 | assert(_wrap._root); |
| 5466 | _wrap = _wrap.next_sibling(_name); |
| 5467 | return *this; |
| 5468 | } |
| 5469 | |
| 5470 | PUGI__FN xml_named_node_iterator xml_named_node_iterator::operator++(int) |
| 5471 | { |
| 5472 | xml_named_node_iterator temp = *this; |
| 5473 | ++*this; |
| 5474 | return temp; |
| 5475 | } |
| 5476 | |
| 5477 | PUGI__FN const xml_named_node_iterator& xml_named_node_iterator::operator--() |
| 5478 | { |
| 5479 | if (_wrap._root) |
| 5480 | _wrap = _wrap.previous_sibling(_name); |
| 5481 | else |
| 5482 | { |
| 5483 | _wrap = _parent.last_child(); |
| 5484 | |
| 5485 | if (!impl::strequal(_wrap.name(), _name)) |
| 5486 | _wrap = _wrap.previous_sibling(_name); |
| 5487 | } |
| 5488 | |
| 5489 | return *this; |
| 5490 | } |
| 5491 | |
| 5492 | PUGI__FN xml_named_node_iterator xml_named_node_iterator::operator--(int) |
| 5493 | { |
| 5494 | xml_named_node_iterator temp = *this; |
| 5495 | --*this; |
| 5496 | return temp; |
| 5497 | } |
| 5498 | |
| 5499 | PUGI__FN xml_parse_result::xml_parse_result(): status(status_internal_error), offset(0), encoding(encoding_auto) |
| 5500 | { |
| 5501 | } |
| 5502 | |
| 5503 | PUGI__FN xml_parse_result::operator bool() const |
| 5504 | { |
| 5505 | return status == status_ok; |
| 5506 | } |
| 5507 | |
| 5508 | PUGI__FN const char* xml_parse_result::description() const |
| 5509 | { |
| 5510 | switch (status) |
| 5511 | { |
| 5512 | case status_ok: return "No error"; |
| 5513 | |
| 5514 | case status_file_not_found: return "File was not found"; |
| 5515 | case status_io_error: return "Error reading from file/stream"; |
| 5516 | case status_out_of_memory: return "Could not allocate memory"; |
| 5517 | case status_internal_error: return "Internal error occurred"; |
| 5518 | |
| 5519 | case status_unrecognized_tag: return "Could not determine tag type"; |
| 5520 | |
| 5521 | case status_bad_pi: return "Error parsing document declaration/processing instruction"; |
| 5522 | case status_bad_comment: return "Error parsing comment"; |
| 5523 | case status_bad_cdata: return "Error parsing CDATA section"; |
| 5524 | case status_bad_doctype: return "Error parsing document type declaration"; |
| 5525 | case status_bad_pcdata: return "Error parsing PCDATA section"; |
| 5526 | case status_bad_start_element: return "Error parsing start element tag"; |
| 5527 | case status_bad_attribute: return "Error parsing element attribute"; |
| 5528 | case status_bad_end_element: return "Error parsing end element tag"; |
| 5529 | case status_end_element_mismatch: return "Start-end tags mismatch"; |
| 5530 | |
| 5531 | case status_append_invalid_root: return "Unable to append nodes: root is not an element or document"; |
| 5532 | |
| 5533 | case status_no_document_element: return "No document element found"; |
| 5534 | |
| 5535 | default: return "Unknown error"; |
| 5536 | } |
| 5537 | } |
| 5538 | |
| 5539 | PUGI__FN xml_document::xml_document(): _buffer(0) |
| 5540 | { |
| 5541 | create(); |
| 5542 | } |
| 5543 | |
| 5544 | PUGI__FN xml_document::~xml_document() |
| 5545 | { |
| 5546 | destroy(); |
| 5547 | } |
| 5548 | |
| 5549 | PUGI__FN void xml_document::reset() |
| 5550 | { |
| 5551 | destroy(); |
| 5552 | create(); |
| 5553 | } |
| 5554 | |
| 5555 | PUGI__FN void xml_document::reset(const xml_document& proto) |
| 5556 | { |
| 5557 | reset(); |
| 5558 | |
| 5559 | for (xml_node cur = proto.first_child(); cur; cur = cur.next_sibling()) |
| 5560 | append_copy(cur); |
| 5561 | } |
| 5562 | |
| 5563 | PUGI__FN void xml_document::create() |
| 5564 | { |
| 5565 | assert(!_root); |
| 5566 | |
| 5567 | // initialize sentinel page |
| 5568 | PUGI__STATIC_ASSERT(sizeof(impl::xml_memory_page) + sizeof(impl::xml_document_struct) + impl::xml_memory_page_alignment <= sizeof(_memory)); |
| 5569 | |
| 5570 | // align upwards to page boundary |
| 5571 | void* page_memory = reinterpret_cast<void*>((reinterpret_cast<uintptr_t>(_memory) + (impl::xml_memory_page_alignment - 1)) & ~(impl::xml_memory_page_alignment - 1)); |
| 5572 | |
| 5573 | // prepare page structure |
| 5574 | impl::xml_memory_page* page = impl::xml_memory_page::construct(page_memory); |
| 5575 | assert(page); |
| 5576 | |
| 5577 | page->busy_size = impl::xml_memory_page_size; |
| 5578 | |
| 5579 | // allocate new root |
| 5580 | _root = new (page->data) impl::xml_document_struct(page); |
| 5581 | _root->prev_sibling_c = _root; |
| 5582 | |
| 5583 | // setup sentinel page |
| 5584 | page->allocator = static_cast<impl::xml_document_struct*>(_root); |
| 5585 | } |
| 5586 | |
| 5587 | PUGI__FN void xml_document::destroy() |
| 5588 | { |
| 5589 | assert(_root); |
| 5590 | |
| 5591 | // destroy static storage |
| 5592 | if (_buffer) |
| 5593 | { |
| 5594 | impl::xml_memory::deallocate(_buffer); |
| 5595 | _buffer = 0; |
| 5596 | } |
| 5597 | |
| 5598 | // destroy extra buffers (note: no need to destroy linked list nodes, they're allocated using document allocator) |
| 5599 | for (impl::xml_extra_buffer* extra = static_cast<impl::xml_document_struct*>(_root)->extra_buffers; extra; extra = extra->next) |
| 5600 | { |
| 5601 | if (extra->buffer) impl::xml_memory::deallocate(extra->buffer); |
| 5602 | } |
| 5603 | |
| 5604 | // destroy dynamic storage, leave sentinel page (it's in static memory) |
| 5605 | impl::xml_memory_page* root_page = reinterpret_cast<impl::xml_memory_page*>(_root->header & impl::xml_memory_page_pointer_mask); |
| 5606 | assert(root_page && !root_page->prev && !root_page->memory); |
| 5607 | |
| 5608 | for (impl::xml_memory_page* page = root_page->next; page; ) |
| 5609 | { |
| 5610 | impl::xml_memory_page* next = page->next; |
| 5611 | |
| 5612 | impl::xml_allocator::deallocate_page(page); |
| 5613 | |
| 5614 | page = next; |
| 5615 | } |
| 5616 | |
| 5617 | _root = 0; |
| 5618 | } |
| 5619 | |
| 5620 | #ifndef PUGIXML_NO_STL |
| 5621 | PUGI__FN xml_parse_result xml_document::load(std::basic_istream<char, std::char_traits<char> >& stream, unsigned int options, xml_encoding encoding) |
| 5622 | { |
| 5623 | reset(); |
| 5624 | |
| 5625 | return impl::load_stream_impl(*this, stream, options, encoding); |
| 5626 | } |
| 5627 | |
| 5628 | PUGI__FN xml_parse_result xml_document::load(std::basic_istream<wchar_t, std::char_traits<wchar_t> >& stream, unsigned int options) |
| 5629 | { |
| 5630 | reset(); |
| 5631 | |
| 5632 | return impl::load_stream_impl(*this, stream, options, encoding_wchar); |
| 5633 | } |
| 5634 | #endif |
| 5635 | |
| 5636 | PUGI__FN xml_parse_result xml_document::load(const char_t* contents, unsigned int options) |
| 5637 | { |
| 5638 | // Force native encoding (skip autodetection) |
| 5639 | #ifdef PUGIXML_WCHAR_MODE |
| 5640 | xml_encoding encoding = encoding_wchar; |
| 5641 | #else |
| 5642 | xml_encoding encoding = encoding_utf8; |
| 5643 | #endif |
| 5644 | |
| 5645 | return load_buffer(contents, impl::strlength(contents) * sizeof(char_t), options, encoding); |
| 5646 | } |
| 5647 | |
| 5648 | PUGI__FN xml_parse_result xml_document::load_file(const char* path_, unsigned int options, xml_encoding encoding) |
| 5649 | { |
| 5650 | reset(); |
| 5651 | |
| 5652 | FILE* file = fopen(path_, "rb"); |
| 5653 | |
| 5654 | return impl::load_file_impl(*this, file, options, encoding); |
| 5655 | } |
| 5656 | |
| 5657 | PUGI__FN xml_parse_result xml_document::load_file(const wchar_t* path_, unsigned int options, xml_encoding encoding) |
| 5658 | { |
| 5659 | reset(); |
| 5660 | |
| 5661 | FILE* file = impl::open_file_wide(path_, L"rb"); |
| 5662 | |
| 5663 | return impl::load_file_impl(*this, file, options, encoding); |
| 5664 | } |
| 5665 | |
| 5666 | PUGI__FN xml_parse_result xml_document::load_buffer(const void* contents, size_t size, unsigned int options, xml_encoding encoding) |
| 5667 | { |
| 5668 | reset(); |
| 5669 | |
| 5670 | return impl::load_buffer_impl(static_cast<impl::xml_document_struct*>(_root), _root, const_cast<void*>(contents), size, options, encoding, false, false, &_buffer); |
| 5671 | } |
| 5672 | |
| 5673 | PUGI__FN xml_parse_result xml_document::load_buffer_inplace(void* contents, size_t size, unsigned int options, xml_encoding encoding) |
| 5674 | { |
| 5675 | reset(); |
| 5676 | |
| 5677 | return impl::load_buffer_impl(static_cast<impl::xml_document_struct*>(_root), _root, contents, size, options, encoding, true, false, &_buffer); |
| 5678 | } |
| 5679 | |
| 5680 | PUGI__FN xml_parse_result xml_document::load_buffer_inplace_own(void* contents, size_t size, unsigned int options, xml_encoding encoding) |
| 5681 | { |
| 5682 | reset(); |
| 5683 | |
| 5684 | return impl::load_buffer_impl(static_cast<impl::xml_document_struct*>(_root), _root, contents, size, options, encoding, true, true, &_buffer); |
| 5685 | } |
| 5686 | |
| 5687 | PUGI__FN void xml_document::save(xml_writer& writer, const char_t* indent, unsigned int flags, xml_encoding encoding) const |
| 5688 | { |
| 5689 | impl::xml_buffered_writer buffered_writer(writer, encoding); |
| 5690 | |
| 5691 | if ((flags & format_write_bom) && encoding != encoding_latin1) |
| 5692 | { |
| 5693 | // BOM always represents the codepoint U+FEFF, so just write it in native encoding |
| 5694 | #ifdef PUGIXML_WCHAR_MODE |
| 5695 | unsigned int bom = 0xfeff; |
| 5696 | buffered_writer.write(static_cast<wchar_t>(bom)); |
| 5697 | #else |
| 5698 | buffered_writer.write('\xef', '\xbb', '\xbf'); |
| 5699 | #endif |
| 5700 | } |
| 5701 | |
| 5702 | if (!(flags & format_no_declaration) && !impl::has_declaration(*this)) |
| 5703 | { |
| 5704 | buffered_writer.write(PUGIXML_TEXT("<?xml version=\"1.0\"")); |
| 5705 | if (encoding == encoding_latin1) buffered_writer.write(PUGIXML_TEXT(" encoding=\"ISO-8859-1\"")); |
| 5706 | buffered_writer.write('?', '>'); |
| 5707 | if (!(flags & format_raw)) buffered_writer.write('\n'); |
| 5708 | } |
| 5709 | |
| 5710 | impl::node_output(buffered_writer, *this, indent, flags, 0); |
| 5711 | } |
| 5712 | |
| 5713 | #ifndef PUGIXML_NO_STL |
| 5714 | PUGI__FN void xml_document::save(std::basic_ostream<char, std::char_traits<char> >& stream, const char_t* indent, unsigned int flags, xml_encoding encoding) const |
| 5715 | { |
| 5716 | xml_writer_stream writer(stream); |
| 5717 | |
| 5718 | save(writer, indent, flags, encoding); |
| 5719 | } |
| 5720 | |
| 5721 | PUGI__FN void xml_document::save(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream, const char_t* indent, unsigned int flags) const |
| 5722 | { |
| 5723 | xml_writer_stream writer(stream); |
| 5724 | |
| 5725 | save(writer, indent, flags, encoding_wchar); |
| 5726 | } |
| 5727 | #endif |
| 5728 | |
| 5729 | PUGI__FN bool xml_document::save_file(const char* path_, const char_t* indent, unsigned int flags, xml_encoding encoding) const |
| 5730 | { |
| 5731 | FILE* file = fopen(path_, (flags & format_save_file_text) ? "w" : "wb"); |
| 5732 | return impl::save_file_impl(*this, file, indent, flags, encoding); |
| 5733 | } |
| 5734 | |
| 5735 | PUGI__FN bool xml_document::save_file(const wchar_t* path_, const char_t* indent, unsigned int flags, xml_encoding encoding) const |
| 5736 | { |
| 5737 | FILE* file = impl::open_file_wide(path_, (flags & format_save_file_text) ? L"w" : L"wb"); |
| 5738 | return impl::save_file_impl(*this, file, indent, flags, encoding); |
| 5739 | } |
| 5740 | |
| 5741 | PUGI__FN xml_node xml_document::document_element() const |
| 5742 | { |
| 5743 | assert(_root); |
| 5744 | |
| 5745 | for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling) |
| 5746 | if ((i->header & impl::xml_memory_page_type_mask) + 1 == node_element) |
| 5747 | return xml_node(i); |
| 5748 | |
| 5749 | return xml_node(); |
| 5750 | } |
| 5751 | |
| 5752 | #ifndef PUGIXML_NO_STL |
| 5753 | PUGI__FN std::string PUGIXML_FUNCTION as_utf8(const wchar_t* str) |
| 5754 | { |
| 5755 | assert(str); |
| 5756 | |
| 5757 | return impl::as_utf8_impl(str, impl::strlength_wide(str)); |
| 5758 | } |
| 5759 | |
| 5760 | PUGI__FN std::string PUGIXML_FUNCTION as_utf8(const std::basic_string<wchar_t>& str) |
| 5761 | { |
| 5762 | return impl::as_utf8_impl(str.c_str(), str.size()); |
| 5763 | } |
| 5764 | |
| 5765 | PUGI__FN std::basic_string<wchar_t> PUGIXML_FUNCTION as_wide(const char* str) |
| 5766 | { |
| 5767 | assert(str); |
| 5768 | |
| 5769 | return impl::as_wide_impl(str, strlen(str)); |
| 5770 | } |
| 5771 | |
| 5772 | PUGI__FN std::basic_string<wchar_t> PUGIXML_FUNCTION as_wide(const std::string& str) |
| 5773 | { |
| 5774 | return impl::as_wide_impl(str.c_str(), str.size()); |
| 5775 | } |
| 5776 | #endif |
| 5777 | |
| 5778 | PUGI__FN void PUGIXML_FUNCTION set_memory_management_functions(allocation_function allocate, deallocation_function deallocate) |
| 5779 | { |
| 5780 | impl::xml_memory::allocate = allocate; |
| 5781 | impl::xml_memory::deallocate = deallocate; |
| 5782 | } |
| 5783 | |
| 5784 | PUGI__FN allocation_function PUGIXML_FUNCTION get_memory_allocation_function() |
| 5785 | { |
| 5786 | return impl::xml_memory::allocate; |
| 5787 | } |
| 5788 | |
| 5789 | PUGI__FN deallocation_function PUGIXML_FUNCTION get_memory_deallocation_function() |
| 5790 | { |
| 5791 | return impl::xml_memory::deallocate; |
| 5792 | } |
| 5793 | } |
| 5794 | |
| 5795 | #if !defined(PUGIXML_NO_STL) && (defined(_MSC_VER) || defined(__ICC)) |
| 5796 | namespace std |
| 5797 | { |
| 5798 | // Workarounds for (non-standard) iterator category detection for older versions (MSVC7/IC8 and earlier) |
| 5799 | PUGI__FN std::bidirectional_iterator_tag _Iter_cat(const pugi::xml_node_iterator&) |
| 5800 | { |
| 5801 | return std::bidirectional_iterator_tag(); |
| 5802 | } |
| 5803 | |
| 5804 | PUGI__FN std::bidirectional_iterator_tag _Iter_cat(const pugi::xml_attribute_iterator&) |
| 5805 | { |
| 5806 | return std::bidirectional_iterator_tag(); |
| 5807 | } |
| 5808 | |
| 5809 | PUGI__FN std::bidirectional_iterator_tag _Iter_cat(const pugi::xml_named_node_iterator&) |
| 5810 | { |
| 5811 | return std::bidirectional_iterator_tag(); |
| 5812 | } |
| 5813 | } |
| 5814 | #endif |
| 5815 | |
| 5816 | #if !defined(PUGIXML_NO_STL) && defined(__SUNPRO_CC) |
| 5817 | namespace std |
| 5818 | { |
| 5819 | // Workarounds for (non-standard) iterator category detection |
| 5820 | PUGI__FN std::bidirectional_iterator_tag __iterator_category(const pugi::xml_node_iterator&) |
| 5821 | { |
| 5822 | return std::bidirectional_iterator_tag(); |
| 5823 | } |
| 5824 | |
| 5825 | PUGI__FN std::bidirectional_iterator_tag __iterator_category(const pugi::xml_attribute_iterator&) |
| 5826 | { |
| 5827 | return std::bidirectional_iterator_tag(); |
| 5828 | } |
| 5829 | |
| 5830 | PUGI__FN std::bidirectional_iterator_tag __iterator_category(const pugi::xml_named_node_iterator&) |
| 5831 | { |
| 5832 | return std::bidirectional_iterator_tag(); |
| 5833 | } |
| 5834 | } |
| 5835 | #endif |
| 5836 | |
| 5837 | #ifndef PUGIXML_NO_XPATH |
| 5838 | |
| 5839 | // STL replacements |
| 5840 | PUGI__NS_BEGIN |
| 5841 | struct equal_to |
| 5842 | { |
| 5843 | template <typename T> bool operator()(const T& lhs, const T& rhs) const |
| 5844 | { |
| 5845 | return lhs == rhs; |
| 5846 | } |
| 5847 | }; |
| 5848 | |
| 5849 | struct not_equal_to |
| 5850 | { |
| 5851 | template <typename T> bool operator()(const T& lhs, const T& rhs) const |
| 5852 | { |
| 5853 | return lhs != rhs; |
| 5854 | } |
| 5855 | }; |
| 5856 | |
| 5857 | struct less |
| 5858 | { |
| 5859 | template <typename T> bool operator()(const T& lhs, const T& rhs) const |
| 5860 | { |
| 5861 | return lhs < rhs; |
| 5862 | } |
| 5863 | }; |
| 5864 | |
| 5865 | struct less_equal |
| 5866 | { |
| 5867 | template <typename T> bool operator()(const T& lhs, const T& rhs) const |
| 5868 | { |
| 5869 | return lhs <= rhs; |
| 5870 | } |
| 5871 | }; |
| 5872 | |
| 5873 | template <typename T> void swap(T& lhs, T& rhs) |
| 5874 | { |
| 5875 | T temp = lhs; |
| 5876 | lhs = rhs; |
| 5877 | rhs = temp; |
| 5878 | } |
| 5879 | |
| 5880 | template <typename I, typename Pred> I min_element(I begin, I end, const Pred& pred) |
| 5881 | { |
| 5882 | I result = begin; |
| 5883 | |
| 5884 | for (I it = begin + 1; it != end; ++it) |
| 5885 | if (pred(*it, *result)) |
| 5886 | result = it; |
| 5887 | |
| 5888 | return result; |
| 5889 | } |
| 5890 | |
| 5891 | template <typename I> void reverse(I begin, I end) |
| 5892 | { |
| 5893 | while (end - begin > 1) swap(*begin++, *--end); |
| 5894 | } |
| 5895 | |
| 5896 | template <typename I> I unique(I begin, I end) |
| 5897 | { |
| 5898 | // fast skip head |
| 5899 | while (end - begin > 1 && *begin != *(begin + 1)) begin++; |
| 5900 | |
| 5901 | if (begin == end) return begin; |
| 5902 | |
| 5903 | // last written element |
| 5904 | I write = begin++; |
| 5905 | |
| 5906 | // merge unique elements |
| 5907 | while (begin != end) |
| 5908 | { |
| 5909 | if (*begin != *write) |
| 5910 | *++write = *begin++; |
| 5911 | else |
| 5912 | begin++; |
| 5913 | } |
| 5914 | |
| 5915 | // past-the-end (write points to live element) |
| 5916 | return write + 1; |
| 5917 | } |
| 5918 | |
| 5919 | template <typename I> void copy_backwards(I begin, I end, I target) |
| 5920 | { |
| 5921 | while (begin != end) *--target = *--end; |
| 5922 | } |
| 5923 | |
| 5924 | template <typename I, typename Pred, typename T> void insertion_sort(I begin, I end, const Pred& pred, T*) |
| 5925 | { |
| 5926 | assert(begin != end); |
| 5927 | |
| 5928 | for (I it = begin + 1; it != end; ++it) |
| 5929 | { |
| 5930 | T val = *it; |
| 5931 | |
| 5932 | if (pred(val, *begin)) |
| 5933 | { |
| 5934 | // move to front |
| 5935 | copy_backwards(begin, it, it + 1); |
| 5936 | *begin = val; |
| 5937 | } |
| 5938 | else |
| 5939 | { |
| 5940 | I hole = it; |
| 5941 | |
| 5942 | // move hole backwards |
| 5943 | while (pred(val, *(hole - 1))) |
| 5944 | { |
| 5945 | *hole = *(hole - 1); |
| 5946 | hole--; |
| 5947 | } |
| 5948 | |
| 5949 | // fill hole with element |
| 5950 | *hole = val; |
| 5951 | } |
| 5952 | } |
| 5953 | } |
| 5954 | |
| 5955 | // std variant for elements with == |
| 5956 | template <typename I, typename Pred> void partition(I begin, I middle, I end, const Pred& pred, I* out_eqbeg, I* out_eqend) |
| 5957 | { |
| 5958 | I eqbeg = middle, eqend = middle + 1; |
| 5959 | |
| 5960 | // expand equal range |
| 5961 | while (eqbeg != begin && *(eqbeg - 1) == *eqbeg) --eqbeg; |
| 5962 | while (eqend != end && *eqend == *eqbeg) ++eqend; |
| 5963 | |
| 5964 | // process outer elements |
| 5965 | I ltend = eqbeg, gtbeg = eqend; |
| 5966 | |
| 5967 | for (;;) |
| 5968 | { |
| 5969 | // find the element from the right side that belongs to the left one |
| 5970 | for (; gtbeg != end; ++gtbeg) |
| 5971 | if (!pred(*eqbeg, *gtbeg)) |
| 5972 | { |
| 5973 | if (*gtbeg == *eqbeg) swap(*gtbeg, *eqend++); |
| 5974 | else break; |
| 5975 | } |
| 5976 | |
| 5977 | // find the element from the left side that belongs to the right one |
| 5978 | for (; ltend != begin; --ltend) |
| 5979 | if (!pred(*(ltend - 1), *eqbeg)) |
| 5980 | { |
| 5981 | if (*eqbeg == *(ltend - 1)) swap(*(ltend - 1), *--eqbeg); |
| 5982 | else break; |
| 5983 | } |
| 5984 | |
| 5985 | // scanned all elements |
| 5986 | if (gtbeg == end && ltend == begin) |
| 5987 | { |
| 5988 | *out_eqbeg = eqbeg; |
| 5989 | *out_eqend = eqend; |
| 5990 | return; |
| 5991 | } |
| 5992 | |
| 5993 | // make room for elements by moving equal area |
| 5994 | if (gtbeg == end) |
| 5995 | { |
| 5996 | if (--ltend != --eqbeg) swap(*ltend, *eqbeg); |
| 5997 | swap(*eqbeg, *--eqend); |
| 5998 | } |
| 5999 | else if (ltend == begin) |
| 6000 | { |
| 6001 | if (eqend != gtbeg) swap(*eqbeg, *eqend); |
| 6002 | ++eqend; |
| 6003 | swap(*gtbeg++, *eqbeg++); |
| 6004 | } |
| 6005 | else swap(*gtbeg++, *--ltend); |
| 6006 | } |
| 6007 | } |
| 6008 | |
| 6009 | template <typename I, typename Pred> void median3(I first, I middle, I last, const Pred& pred) |
| 6010 | { |
| 6011 | if (pred(*middle, *first)) swap(*middle, *first); |
| 6012 | if (pred(*last, *middle)) swap(*last, *middle); |
| 6013 | if (pred(*middle, *first)) swap(*middle, *first); |
| 6014 | } |
| 6015 | |
| 6016 | template <typename I, typename Pred> void median(I first, I middle, I last, const Pred& pred) |
| 6017 | { |
| 6018 | if (last - first <= 40) |
| 6019 | { |
| 6020 | // median of three for small chunks |
| 6021 | median3(first, middle, last, pred); |
| 6022 | } |
| 6023 | else |
| 6024 | { |
| 6025 | // median of nine |
| 6026 | size_t step = (last - first + 1) / 8; |
| 6027 | |
| 6028 | median3(first, first + step, first + 2 * step, pred); |
| 6029 | median3(middle - step, middle, middle + step, pred); |
| 6030 | median3(last - 2 * step, last - step, last, pred); |
| 6031 | median3(first + step, middle, last - step, pred); |
| 6032 | } |
| 6033 | } |
| 6034 | |
| 6035 | template <typename I, typename Pred> void sort(I begin, I end, const Pred& pred) |
| 6036 | { |
| 6037 | // sort large chunks |
| 6038 | while (end - begin > 32) |
| 6039 | { |
| 6040 | // find median element |
| 6041 | I middle = begin + (end - begin) / 2; |
| 6042 | median(begin, middle, end - 1, pred); |
| 6043 | |
| 6044 | // partition in three chunks (< = >) |
| 6045 | I eqbeg, eqend; |
| 6046 | partition(begin, middle, end, pred, &eqbeg, &eqend); |
| 6047 | |
| 6048 | // loop on larger half |
| 6049 | if (eqbeg - begin > end - eqend) |
| 6050 | { |
| 6051 | sort(eqend, end, pred); |
| 6052 | end = eqbeg; |
| 6053 | } |
| 6054 | else |
| 6055 | { |
| 6056 | sort(begin, eqbeg, pred); |
| 6057 | begin = eqend; |
| 6058 | } |
| 6059 | } |
| 6060 | |
| 6061 | // insertion sort small chunk |
| 6062 | if (begin != end) insertion_sort(begin, end, pred, &*begin); |
| 6063 | } |
| 6064 | PUGI__NS_END |
| 6065 | |
| 6066 | // Allocator used for AST and evaluation stacks |
| 6067 | PUGI__NS_BEGIN |
| 6068 | struct xpath_memory_block |
| 6069 | { |
| 6070 | xpath_memory_block* next; |
| 6071 | |
| 6072 | char data[ |
| 6073 | #ifdef PUGIXML_MEMORY_XPATH_PAGE_SIZE |
| 6074 | PUGIXML_MEMORY_XPATH_PAGE_SIZE |
| 6075 | #else |
| 6076 | 4096 |
| 6077 | #endif |
| 6078 | ]; |
| 6079 | }; |
| 6080 | |
| 6081 | class xpath_allocator |
| 6082 | { |
| 6083 | xpath_memory_block* _root; |
| 6084 | size_t _root_size; |
| 6085 | |
| 6086 | public: |
| 6087 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 6088 | jmp_buf* error_handler; |
| 6089 | #endif |
| 6090 | |
| 6091 | xpath_allocator(xpath_memory_block* root, size_t root_size = 0): _root(root), _root_size(root_size) |
| 6092 | { |
| 6093 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 6094 | error_handler = 0; |
| 6095 | #endif |
| 6096 | } |
| 6097 | |
| 6098 | void* allocate_nothrow(size_t size) |
| 6099 | { |
| 6100 | const size_t block_capacity = sizeof(_root->data); |
| 6101 | |
| 6102 | // align size so that we're able to store pointers in subsequent blocks |
| 6103 | size = (size + sizeof(void*) - 1) & ~(sizeof(void*) - 1); |
| 6104 | |
| 6105 | if (_root_size + size <= block_capacity) |
| 6106 | { |
| 6107 | void* buf = _root->data + _root_size; |
| 6108 | _root_size += size; |
| 6109 | return buf; |
| 6110 | } |
| 6111 | else |
| 6112 | { |
| 6113 | size_t block_data_size = (size > block_capacity) ? size : block_capacity; |
| 6114 | size_t block_size = block_data_size + offsetof(xpath_memory_block, data); |
| 6115 | |
| 6116 | xpath_memory_block* block = static_cast<xpath_memory_block*>(xml_memory::allocate(block_size)); |
| 6117 | if (!block) return 0; |
| 6118 | |
| 6119 | block->next = _root; |
| 6120 | |
| 6121 | _root = block; |
| 6122 | _root_size = size; |
| 6123 | |
| 6124 | return block->data; |
| 6125 | } |
| 6126 | } |
| 6127 | |
| 6128 | void* allocate(size_t size) |
| 6129 | { |
| 6130 | void* result = allocate_nothrow(size); |
| 6131 | |
| 6132 | if (!result) |
| 6133 | { |
| 6134 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 6135 | assert(error_handler); |
| 6136 | longjmp(*error_handler, 1); |
| 6137 | #else |
| 6138 | throw std::bad_alloc(); |
| 6139 | #endif |
| 6140 | } |
| 6141 | |
| 6142 | return result; |
| 6143 | } |
| 6144 | |
| 6145 | void* reallocate(void* ptr, size_t old_size, size_t new_size) |
| 6146 | { |
| 6147 | // align size so that we're able to store pointers in subsequent blocks |
| 6148 | old_size = (old_size + sizeof(void*) - 1) & ~(sizeof(void*) - 1); |
| 6149 | new_size = (new_size + sizeof(void*) - 1) & ~(sizeof(void*) - 1); |
| 6150 | |
| 6151 | // we can only reallocate the last object |
| 6152 | assert(ptr == 0 || static_cast<char*>(ptr) + old_size == _root->data + _root_size); |
| 6153 | |
| 6154 | // adjust root size so that we have not allocated the object at all |
| 6155 | bool only_object = (_root_size == old_size); |
| 6156 | |
| 6157 | if (ptr) _root_size -= old_size; |
| 6158 | |
| 6159 | // allocate a new version (this will obviously reuse the memory if possible) |
| 6160 | void* result = allocate(new_size); |
| 6161 | assert(result); |
| 6162 | |
| 6163 | // we have a new block |
| 6164 | if (result != ptr && ptr) |
| 6165 | { |
| 6166 | // copy old data |
| 6167 | assert(new_size >= old_size); |
| 6168 | memcpy(result, ptr, old_size); |
| 6169 | |
| 6170 | // free the previous page if it had no other objects |
| 6171 | if (only_object) |
| 6172 | { |
| 6173 | assert(_root->data == result); |
| 6174 | assert(_root->next); |
| 6175 | |
| 6176 | xpath_memory_block* next = _root->next->next; |
| 6177 | |
| 6178 | if (next) |
| 6179 | { |
| 6180 | // deallocate the whole page, unless it was the first one |
| 6181 | xml_memory::deallocate(_root->next); |
| 6182 | _root->next = next; |
| 6183 | } |
| 6184 | } |
| 6185 | } |
| 6186 | |
| 6187 | return result; |
| 6188 | } |
| 6189 | |
| 6190 | void revert(const xpath_allocator& state) |
| 6191 | { |
| 6192 | // free all new pages |
| 6193 | xpath_memory_block* cur = _root; |
| 6194 | |
| 6195 | while (cur != state._root) |
| 6196 | { |
| 6197 | xpath_memory_block* next = cur->next; |
| 6198 | |
| 6199 | xml_memory::deallocate(cur); |
| 6200 | |
| 6201 | cur = next; |
| 6202 | } |
| 6203 | |
| 6204 | // restore state |
| 6205 | _root = state._root; |
| 6206 | _root_size = state._root_size; |
| 6207 | } |
| 6208 | |
| 6209 | void release() |
| 6210 | { |
| 6211 | xpath_memory_block* cur = _root; |
| 6212 | assert(cur); |
| 6213 | |
| 6214 | while (cur->next) |
| 6215 | { |
| 6216 | xpath_memory_block* next = cur->next; |
| 6217 | |
| 6218 | xml_memory::deallocate(cur); |
| 6219 | |
| 6220 | cur = next; |
| 6221 | } |
| 6222 | } |
| 6223 | }; |
| 6224 | |
| 6225 | struct xpath_allocator_capture |
| 6226 | { |
| 6227 | xpath_allocator_capture(xpath_allocator* alloc): _target(alloc), _state(*alloc) |
| 6228 | { |
| 6229 | } |
| 6230 | |
| 6231 | ~xpath_allocator_capture() |
| 6232 | { |
| 6233 | _target->revert(_state); |
| 6234 | } |
| 6235 | |
| 6236 | xpath_allocator* _target; |
| 6237 | xpath_allocator _state; |
| 6238 | }; |
| 6239 | |
| 6240 | struct xpath_stack |
| 6241 | { |
| 6242 | xpath_allocator* result; |
| 6243 | xpath_allocator* temp; |
| 6244 | }; |
| 6245 | |
| 6246 | struct xpath_stack_data |
| 6247 | { |
| 6248 | xpath_memory_block blocks[2]; |
| 6249 | xpath_allocator result; |
| 6250 | xpath_allocator temp; |
| 6251 | xpath_stack stack; |
| 6252 | |
| 6253 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 6254 | jmp_buf error_handler; |
| 6255 | #endif |
| 6256 | |
| 6257 | xpath_stack_data(): result(blocks + 0), temp(blocks + 1) |
| 6258 | { |
| 6259 | blocks[0].next = blocks[1].next = 0; |
| 6260 | |
| 6261 | stack.result = &result; |
| 6262 | stack.temp = &temp; |
| 6263 | |
| 6264 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 6265 | result.error_handler = temp.error_handler = &error_handler; |
| 6266 | #endif |
| 6267 | } |
| 6268 | |
| 6269 | ~xpath_stack_data() |
| 6270 | { |
| 6271 | result.release(); |
| 6272 | temp.release(); |
| 6273 | } |
| 6274 | }; |
| 6275 | PUGI__NS_END |
| 6276 | |
| 6277 | // String class |
| 6278 | PUGI__NS_BEGIN |
| 6279 | class xpath_string |
| 6280 | { |
| 6281 | const char_t* _buffer; |
| 6282 | bool _uses_heap; |
| 6283 | |
| 6284 | static char_t* duplicate_string(const char_t* string, size_t length, xpath_allocator* alloc) |
| 6285 | { |
| 6286 | char_t* result = static_cast<char_t*>(alloc->allocate((length + 1) * sizeof(char_t))); |
| 6287 | assert(result); |
| 6288 | |
| 6289 | memcpy(result, string, length * sizeof(char_t)); |
| 6290 | result[length] = 0; |
| 6291 | |
| 6292 | return result; |
| 6293 | } |
| 6294 | |
| 6295 | static char_t* duplicate_string(const char_t* string, xpath_allocator* alloc) |
| 6296 | { |
| 6297 | return duplicate_string(string, strlength(string), alloc); |
| 6298 | } |
| 6299 | |
| 6300 | public: |
| 6301 | xpath_string(): _buffer(PUGIXML_TEXT("")), _uses_heap(false) |
| 6302 | { |
| 6303 | } |
| 6304 | |
| 6305 | explicit xpath_string(const char_t* str, xpath_allocator* alloc) |
| 6306 | { |
| 6307 | bool empty_ = (*str == 0); |
| 6308 | |
| 6309 | _buffer = empty_ ? PUGIXML_TEXT("") : duplicate_string(str, alloc); |
| 6310 | _uses_heap = !empty_; |
| 6311 | } |
| 6312 | |
| 6313 | explicit xpath_string(const char_t* str, bool use_heap): _buffer(str), _uses_heap(use_heap) |
| 6314 | { |
| 6315 | } |
| 6316 | |
| 6317 | xpath_string(const char_t* begin, const char_t* end, xpath_allocator* alloc) |
| 6318 | { |
| 6319 | assert(begin <= end); |
| 6320 | |
| 6321 | bool empty_ = (begin == end); |
| 6322 | |
| 6323 | _buffer = empty_ ? PUGIXML_TEXT("") : duplicate_string(begin, static_cast<size_t>(end - begin), alloc); |
| 6324 | _uses_heap = !empty_; |
| 6325 | } |
| 6326 | |
| 6327 | void append(const xpath_string& o, xpath_allocator* alloc) |
| 6328 | { |
| 6329 | // skip empty sources |
| 6330 | if (!*o._buffer) return; |
| 6331 | |
| 6332 | // fast append for constant empty target and constant source |
| 6333 | if (!*_buffer && !_uses_heap && !o._uses_heap) |
| 6334 | { |
| 6335 | _buffer = o._buffer; |
| 6336 | } |
| 6337 | else |
| 6338 | { |
| 6339 | // need to make heap copy |
| 6340 | size_t target_length = strlength(_buffer); |
| 6341 | size_t source_length = strlength(o._buffer); |
| 6342 | size_t result_length = target_length + source_length; |
| 6343 | |
| 6344 | // allocate new buffer |
| 6345 | char_t* result = static_cast<char_t*>(alloc->reallocate(_uses_heap ? const_cast<char_t*>(_buffer) : 0, (target_length + 1) * sizeof(char_t), (result_length + 1) * sizeof(char_t))); |
| 6346 | assert(result); |
| 6347 | |
| 6348 | // append first string to the new buffer in case there was no reallocation |
| 6349 | if (!_uses_heap) memcpy(result, _buffer, target_length * sizeof(char_t)); |
| 6350 | |
| 6351 | // append second string to the new buffer |
| 6352 | memcpy(result + target_length, o._buffer, source_length * sizeof(char_t)); |
| 6353 | result[result_length] = 0; |
| 6354 | |
| 6355 | // finalize |
| 6356 | _buffer = result; |
| 6357 | _uses_heap = true; |
| 6358 | } |
| 6359 | } |
| 6360 | |
| 6361 | const char_t* c_str() const |
| 6362 | { |
| 6363 | return _buffer; |
| 6364 | } |
| 6365 | |
| 6366 | size_t length() const |
| 6367 | { |
| 6368 | return strlength(_buffer); |
| 6369 | } |
| 6370 | |
| 6371 | char_t* data(xpath_allocator* alloc) |
| 6372 | { |
| 6373 | // make private heap copy |
| 6374 | if (!_uses_heap) |
| 6375 | { |
| 6376 | _buffer = duplicate_string(_buffer, alloc); |
| 6377 | _uses_heap = true; |
| 6378 | } |
| 6379 | |
| 6380 | return const_cast<char_t*>(_buffer); |
| 6381 | } |
| 6382 | |
| 6383 | bool empty() const |
| 6384 | { |
| 6385 | return *_buffer == 0; |
| 6386 | } |
| 6387 | |
| 6388 | bool operator==(const xpath_string& o) const |
| 6389 | { |
| 6390 | return strequal(_buffer, o._buffer); |
| 6391 | } |
| 6392 | |
| 6393 | bool operator!=(const xpath_string& o) const |
| 6394 | { |
| 6395 | return !strequal(_buffer, o._buffer); |
| 6396 | } |
| 6397 | |
| 6398 | bool uses_heap() const |
| 6399 | { |
| 6400 | return _uses_heap; |
| 6401 | } |
| 6402 | }; |
| 6403 | |
| 6404 | PUGI__FN xpath_string xpath_string_const(const char_t* str) |
| 6405 | { |
| 6406 | return xpath_string(str, false); |
| 6407 | } |
| 6408 | PUGI__NS_END |
| 6409 | |
| 6410 | PUGI__NS_BEGIN |
| 6411 | PUGI__FN bool starts_with(const char_t* string, const char_t* pattern) |
| 6412 | { |
| 6413 | while (*pattern && *string == *pattern) |
| 6414 | { |
| 6415 | string++; |
| 6416 | pattern++; |
| 6417 | } |
| 6418 | |
| 6419 | return *pattern == 0; |
| 6420 | } |
| 6421 | |
| 6422 | PUGI__FN const char_t* find_char(const char_t* s, char_t c) |
| 6423 | { |
| 6424 | #ifdef PUGIXML_WCHAR_MODE |
| 6425 | return wcschr(s, c); |
| 6426 | #else |
| 6427 | return strchr(s, c); |
| 6428 | #endif |
| 6429 | } |
| 6430 | |
| 6431 | PUGI__FN const char_t* find_substring(const char_t* s, const char_t* p) |
| 6432 | { |
| 6433 | #ifdef PUGIXML_WCHAR_MODE |
| 6434 | // MSVC6 wcsstr bug workaround (if s is empty it always returns 0) |
| 6435 | return (*p == 0) ? s : wcsstr(s, p); |
| 6436 | #else |
| 6437 | return strstr(s, p); |
| 6438 | #endif |
| 6439 | } |
| 6440 | |
| 6441 | // Converts symbol to lower case, if it is an ASCII one |
| 6442 | PUGI__FN char_t tolower_ascii(char_t ch) |
| 6443 | { |
| 6444 | return static_cast<unsigned int>(ch - 'A') < 26 ? static_cast<char_t>(ch | ' ') : ch; |
| 6445 | } |
| 6446 | |
| 6447 | PUGI__FN xpath_string string_value(const xpath_node& na, xpath_allocator* alloc) |
| 6448 | { |
| 6449 | if (na.attribute()) |
| 6450 | return xpath_string_const(na.attribute().value()); |
| 6451 | else |
| 6452 | { |
| 6453 | const xml_node& n = na.node(); |
| 6454 | |
| 6455 | switch (n.type()) |
| 6456 | { |
| 6457 | case node_pcdata: |
| 6458 | case node_cdata: |
| 6459 | case node_comment: |
| 6460 | case node_pi: |
| 6461 | return xpath_string_const(n.value()); |
| 6462 | |
| 6463 | case node_document: |
| 6464 | case node_element: |
| 6465 | { |
| 6466 | xpath_string result; |
| 6467 | |
| 6468 | xml_node cur = n.first_child(); |
| 6469 | |
| 6470 | while (cur && cur != n) |
| 6471 | { |
| 6472 | if (cur.type() == node_pcdata || cur.type() == node_cdata) |
| 6473 | result.append(xpath_string_const(cur.value()), alloc); |
| 6474 | |
| 6475 | if (cur.first_child()) |
| 6476 | cur = cur.first_child(); |
| 6477 | else if (cur.next_sibling()) |
| 6478 | cur = cur.next_sibling(); |
| 6479 | else |
| 6480 | { |
| 6481 | while (!cur.next_sibling() && cur != n) |
| 6482 | cur = cur.parent(); |
| 6483 | |
| 6484 | if (cur != n) cur = cur.next_sibling(); |
| 6485 | } |
| 6486 | } |
| 6487 | |
| 6488 | return result; |
| 6489 | } |
| 6490 | |
| 6491 | default: |
| 6492 | return xpath_string(); |
| 6493 | } |
| 6494 | } |
| 6495 | } |
| 6496 | |
| 6497 | PUGI__FN unsigned int node_height(xml_node n) |
| 6498 | { |
| 6499 | unsigned int result = 0; |
| 6500 | |
| 6501 | while (n) |
| 6502 | { |
| 6503 | ++result; |
| 6504 | n = n.parent(); |
| 6505 | } |
| 6506 | |
| 6507 | return result; |
| 6508 | } |
| 6509 | |
| 6510 | PUGI__FN bool node_is_before(xml_node ln, unsigned int lh, xml_node rn, unsigned int rh) |
| 6511 | { |
| 6512 | // normalize heights |
| 6513 | for (unsigned int i = rh; i < lh; i++) ln = ln.parent(); |
| 6514 | for (unsigned int j = lh; j < rh; j++) rn = rn.parent(); |
| 6515 | |
| 6516 | // one node is the ancestor of the other |
| 6517 | if (ln == rn) return lh < rh; |
| 6518 | |
| 6519 | // find common ancestor |
| 6520 | while (ln.parent() != rn.parent()) |
| 6521 | { |
| 6522 | ln = ln.parent(); |
| 6523 | rn = rn.parent(); |
| 6524 | } |
| 6525 | |
| 6526 | // there is no common ancestor (the shared parent is null), nodes are from different documents |
| 6527 | if (!ln.parent()) return ln < rn; |
| 6528 | |
| 6529 | // determine sibling order |
| 6530 | for (; ln; ln = ln.next_sibling()) |
| 6531 | if (ln == rn) |
| 6532 | return true; |
| 6533 | |
| 6534 | return false; |
| 6535 | } |
| 6536 | |
| 6537 | PUGI__FN bool node_is_ancestor(xml_node parent, xml_node node) |
| 6538 | { |
| 6539 | while (node && node != parent) node = node.parent(); |
| 6540 | |
| 6541 | return parent && node == parent; |
| 6542 | } |
| 6543 | |
| 6544 | PUGI__FN const void* document_order(const xpath_node& xnode) |
| 6545 | { |
| 6546 | xml_node_struct* node = xnode.node().internal_object(); |
| 6547 | |
| 6548 | if (node) |
| 6549 | { |
| 6550 | if (node->name && (node->header & xml_memory_page_name_allocated_mask) == 0) return node->name; |
| 6551 | if (node->value && (node->header & xml_memory_page_value_allocated_mask) == 0) return node->value; |
| 6552 | return 0; |
| 6553 | } |
| 6554 | |
| 6555 | xml_attribute_struct* attr = xnode.attribute().internal_object(); |
| 6556 | |
| 6557 | if (attr) |
| 6558 | { |
| 6559 | if ((attr->header & xml_memory_page_name_allocated_mask) == 0) return attr->name; |
| 6560 | if ((attr->header & xml_memory_page_value_allocated_mask) == 0) return attr->value; |
| 6561 | return 0; |
| 6562 | } |
| 6563 | |
| 6564 | return 0; |
| 6565 | } |
| 6566 | |
| 6567 | struct document_order_comparator |
| 6568 | { |
| 6569 | bool operator()(const xpath_node& lhs, const xpath_node& rhs) const |
| 6570 | { |
| 6571 | // optimized document order based check |
| 6572 | const void* lo = document_order(lhs); |
| 6573 | const void* ro = document_order(rhs); |
| 6574 | |
| 6575 | if (lo && ro) return lo < ro; |
| 6576 | |
| 6577 | // slow comparison |
| 6578 | xml_node ln = lhs.node(), rn = rhs.node(); |
| 6579 | |
| 6580 | // compare attributes |
| 6581 | if (lhs.attribute() && rhs.attribute()) |
| 6582 | { |
| 6583 | // shared parent |
| 6584 | if (lhs.parent() == rhs.parent()) |
| 6585 | { |
| 6586 | // determine sibling order |
| 6587 | for (xml_attribute a = lhs.attribute(); a; a = a.next_attribute()) |
| 6588 | if (a == rhs.attribute()) |
| 6589 | return true; |
| 6590 | |
| 6591 | return false; |
| 6592 | } |
| 6593 | |
| 6594 | // compare attribute parents |
| 6595 | ln = lhs.parent(); |
| 6596 | rn = rhs.parent(); |
| 6597 | } |
| 6598 | else if (lhs.attribute()) |
| 6599 | { |
| 6600 | // attributes go after the parent element |
| 6601 | if (lhs.parent() == rhs.node()) return false; |
| 6602 | |
| 6603 | ln = lhs.parent(); |
| 6604 | } |
| 6605 | else if (rhs.attribute()) |
| 6606 | { |
| 6607 | // attributes go after the parent element |
| 6608 | if (rhs.parent() == lhs.node()) return true; |
| 6609 | |
| 6610 | rn = rhs.parent(); |
| 6611 | } |
| 6612 | |
| 6613 | if (ln == rn) return false; |
| 6614 | |
| 6615 | unsigned int lh = node_height(ln); |
| 6616 | unsigned int rh = node_height(rn); |
| 6617 | |
| 6618 | return node_is_before(ln, lh, rn, rh); |
| 6619 | } |
| 6620 | }; |
| 6621 | |
| 6622 | struct duplicate_comparator |
| 6623 | { |
| 6624 | bool operator()(const xpath_node& lhs, const xpath_node& rhs) const |
| 6625 | { |
| 6626 | if (lhs.attribute()) return rhs.attribute() ? lhs.attribute() < rhs.attribute() : true; |
| 6627 | else return rhs.attribute() ? false : lhs.node() < rhs.node(); |
| 6628 | } |
| 6629 | }; |
| 6630 | |
| 6631 | PUGI__FN double gen_nan() |
| 6632 | { |
| 6633 | #if defined(__STDC_IEC_559__) || ((FLT_RADIX - 0 == 2) && (FLT_MAX_EXP - 0 == 128) && (FLT_MANT_DIG - 0 == 24)) |
| 6634 | union { float f; uint32_t i; } u[sizeof(float) == sizeof(uint32_t) ? 1 : -1]; |
| 6635 | u[0].i = 0x7fc00000; |
| 6636 | return u[0].f; |
| 6637 | #else |
| 6638 | // fallback |
| 6639 | const volatile double zero = 0.0; |
| 6640 | return zero / zero; |
| 6641 | #endif |
| 6642 | } |
| 6643 | |
| 6644 | PUGI__FN bool is_nan(double value) |
| 6645 | { |
| 6646 | #if defined(PUGI__MSVC_CRT_VERSION) || defined(__BORLANDC__) |
| 6647 | return !!_isnan(value); |
| 6648 | #elif defined(fpclassify) && defined(FP_NAN) |
| 6649 | return fpclassify(value) == FP_NAN; |
| 6650 | #else |
| 6651 | // fallback |
| 6652 | const volatile double v = value; |
| 6653 | return v != v; |
| 6654 | #endif |
| 6655 | } |
| 6656 | |
| 6657 | PUGI__FN const char_t* convert_number_to_string_special(double value) |
| 6658 | { |
| 6659 | #if defined(PUGI__MSVC_CRT_VERSION) || defined(__BORLANDC__) |
| 6660 | if (_finite(value)) return (value == 0) ? PUGIXML_TEXT("0") : 0; |
| 6661 | if (_isnan(value)) return PUGIXML_TEXT("NaN"); |
| 6662 | return value > 0 ? PUGIXML_TEXT("Infinity") : PUGIXML_TEXT("-Infinity"); |
| 6663 | #elif defined(fpclassify) && defined(FP_NAN) && defined(FP_INFINITE) && defined(FP_ZERO) |
| 6664 | switch (fpclassify(value)) |
| 6665 | { |
| 6666 | case FP_NAN: |
| 6667 | return PUGIXML_TEXT("NaN"); |
| 6668 | |
| 6669 | case FP_INFINITE: |
| 6670 | return value > 0 ? PUGIXML_TEXT("Infinity") : PUGIXML_TEXT("-Infinity"); |
| 6671 | |
| 6672 | case FP_ZERO: |
| 6673 | return PUGIXML_TEXT("0"); |
| 6674 | |
| 6675 | default: |
| 6676 | return 0; |
| 6677 | } |
| 6678 | #else |
| 6679 | // fallback |
| 6680 | const volatile double v = value; |
| 6681 | |
| 6682 | if (v == 0) return PUGIXML_TEXT("0"); |
| 6683 | if (v != v) return PUGIXML_TEXT("NaN"); |
| 6684 | if (v * 2 == v) return value > 0 ? PUGIXML_TEXT("Infinity") : PUGIXML_TEXT("-Infinity"); |
| 6685 | return 0; |
| 6686 | #endif |
| 6687 | } |
| 6688 | |
| 6689 | PUGI__FN bool convert_number_to_boolean(double value) |
| 6690 | { |
| 6691 | return (value != 0 && !is_nan(value)); |
| 6692 | } |
| 6693 | |
| 6694 | PUGI__FN void truncate_zeros(char* begin, char* end) |
| 6695 | { |
| 6696 | while (begin != end && end[-1] == '0') end--; |
| 6697 | |
| 6698 | *end = 0; |
| 6699 | } |
| 6700 | |
| 6701 | // gets mantissa digits in the form of 0.xxxxx with 0. implied and the exponent |
| 6702 | #if defined(PUGI__MSVC_CRT_VERSION) && PUGI__MSVC_CRT_VERSION >= 1400 && !defined(_WIN32_WCE) |
| 6703 | PUGI__FN void convert_number_to_mantissa_exponent(double value, char* buffer, size_t buffer_size, char** out_mantissa, int* out_exponent) |
| 6704 | { |
| 6705 | // get base values |
| 6706 | int sign, exponent; |
| 6707 | _ecvt_s(buffer, buffer_size, value, DBL_DIG + 1, &exponent, &sign); |
| 6708 | |
| 6709 | // truncate redundant zeros |
| 6710 | truncate_zeros(buffer, buffer + strlen(buffer)); |
| 6711 | |
| 6712 | // fill results |
| 6713 | *out_mantissa = buffer; |
| 6714 | *out_exponent = exponent; |
| 6715 | } |
| 6716 | #else |
| 6717 | PUGI__FN void convert_number_to_mantissa_exponent(double value, char* buffer, size_t buffer_size, char** out_mantissa, int* out_exponent) |
| 6718 | { |
| 6719 | // get a scientific notation value with IEEE DBL_DIG decimals |
| 6720 | sprintf(buffer, "%.*e", DBL_DIG, value); |
| 6721 | assert(strlen(buffer) < buffer_size); |
| 6722 | (void)!buffer_size; |
| 6723 | |
| 6724 | // get the exponent (possibly negative) |
| 6725 | char* exponent_string = strchr(buffer, 'e'); |
| 6726 | assert(exponent_string); |
| 6727 | |
| 6728 | int exponent = atoi(exponent_string + 1); |
| 6729 | |
| 6730 | // extract mantissa string: skip sign |
| 6731 | char* mantissa = buffer[0] == '-' ? buffer + 1 : buffer; |
| 6732 | assert(mantissa[0] != '0' && mantissa[1] == '.'); |
| 6733 | |
| 6734 | // divide mantissa by 10 to eliminate integer part |
| 6735 | mantissa[1] = mantissa[0]; |
| 6736 | mantissa++; |
| 6737 | exponent++; |
| 6738 | |
| 6739 | // remove extra mantissa digits and zero-terminate mantissa |
| 6740 | truncate_zeros(mantissa, exponent_string); |
| 6741 | |
| 6742 | // fill results |
| 6743 | *out_mantissa = mantissa; |
| 6744 | *out_exponent = exponent; |
| 6745 | } |
| 6746 | #endif |
| 6747 | |
| 6748 | PUGI__FN xpath_string convert_number_to_string(double value, xpath_allocator* alloc) |
| 6749 | { |
| 6750 | // try special number conversion |
| 6751 | const char_t* special = convert_number_to_string_special(value); |
| 6752 | if (special) return xpath_string_const(special); |
| 6753 | |
| 6754 | // get mantissa + exponent form |
| 6755 | char mantissa_buffer[32]; |
| 6756 | |
| 6757 | char* mantissa; |
| 6758 | int exponent; |
| 6759 | convert_number_to_mantissa_exponent(value, mantissa_buffer, sizeof(mantissa_buffer), &mantissa, &exponent); |
| 6760 | |
| 6761 | // allocate a buffer of suitable length for the number |
| 6762 | size_t result_size = strlen(mantissa_buffer) + (exponent > 0 ? exponent : -exponent) + 4; |
| 6763 | char_t* result = static_cast<char_t*>(alloc->allocate(sizeof(char_t) * result_size)); |
| 6764 | assert(result); |
| 6765 | |
| 6766 | // make the number! |
| 6767 | char_t* s = result; |
| 6768 | |
| 6769 | // sign |
| 6770 | if (value < 0) *s++ = '-'; |
| 6771 | |
| 6772 | // integer part |
| 6773 | if (exponent <= 0) |
| 6774 | { |
| 6775 | *s++ = '0'; |
| 6776 | } |
| 6777 | else |
| 6778 | { |
| 6779 | while (exponent > 0) |
| 6780 | { |
| 6781 | assert(*mantissa == 0 || static_cast<unsigned int>(static_cast<unsigned int>(*mantissa) - '0') <= 9); |
| 6782 | *s++ = *mantissa ? *mantissa++ : '0'; |
| 6783 | exponent--; |
| 6784 | } |
| 6785 | } |
| 6786 | |
| 6787 | // fractional part |
| 6788 | if (*mantissa) |
| 6789 | { |
| 6790 | // decimal point |
| 6791 | *s++ = '.'; |
| 6792 | |
| 6793 | // extra zeroes from negative exponent |
| 6794 | while (exponent < 0) |
| 6795 | { |
| 6796 | *s++ = '0'; |
| 6797 | exponent++; |
| 6798 | } |
| 6799 | |
| 6800 | // extra mantissa digits |
| 6801 | while (*mantissa) |
| 6802 | { |
| 6803 | assert(static_cast<unsigned int>(*mantissa - '0') <= 9); |
| 6804 | *s++ = *mantissa++; |
| 6805 | } |
| 6806 | } |
| 6807 | |
| 6808 | // zero-terminate |
| 6809 | assert(s < result + result_size); |
| 6810 | *s = 0; |
| 6811 | |
| 6812 | return xpath_string(result, true); |
| 6813 | } |
| 6814 | |
| 6815 | PUGI__FN bool check_string_to_number_format(const char_t* string) |
| 6816 | { |
| 6817 | // parse leading whitespace |
| 6818 | while (PUGI__IS_CHARTYPE(*string, ct_space)) ++string; |
| 6819 | |
| 6820 | // parse sign |
| 6821 | if (*string == '-') ++string; |
| 6822 | |
| 6823 | if (!*string) return false; |
| 6824 | |
| 6825 | // if there is no integer part, there should be a decimal part with at least one digit |
| 6826 | if (!PUGI__IS_CHARTYPEX(string[0], ctx_digit) && (string[0] != '.' || !PUGI__IS_CHARTYPEX(string[1], ctx_digit))) return false; |
| 6827 | |
| 6828 | // parse integer part |
| 6829 | while (PUGI__IS_CHARTYPEX(*string, ctx_digit)) ++string; |
| 6830 | |
| 6831 | // parse decimal part |
| 6832 | if (*string == '.') |
| 6833 | { |
| 6834 | ++string; |
| 6835 | |
| 6836 | while (PUGI__IS_CHARTYPEX(*string, ctx_digit)) ++string; |
| 6837 | } |
| 6838 | |
| 6839 | // parse trailing whitespace |
| 6840 | while (PUGI__IS_CHARTYPE(*string, ct_space)) ++string; |
| 6841 | |
| 6842 | return *string == 0; |
| 6843 | } |
| 6844 | |
| 6845 | PUGI__FN double convert_string_to_number(const char_t* string) |
| 6846 | { |
| 6847 | // check string format |
| 6848 | if (!check_string_to_number_format(string)) return gen_nan(); |
| 6849 | |
| 6850 | // parse string |
| 6851 | #ifdef PUGIXML_WCHAR_MODE |
| 6852 | return wcstod(string, 0); |
| 6853 | #else |
| 6854 | return atof(string); |
| 6855 | #endif |
| 6856 | } |
| 6857 | |
| 6858 | PUGI__FN bool convert_string_to_number_scratch(char_t (&buffer)[32], const char_t* begin, const char_t* end, double* out_result) |
| 6859 | { |
| 6860 | size_t length = static_cast<size_t>(end - begin); |
| 6861 | char_t* scratch = buffer; |
| 6862 | |
| 6863 | if (length >= sizeof(buffer) / sizeof(buffer[0])) |
| 6864 | { |
| 6865 | // need to make dummy on-heap copy |
| 6866 | scratch = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t))); |
| 6867 | if (!scratch) return false; |
| 6868 | } |
| 6869 | |
| 6870 | // copy string to zero-terminated buffer and perform conversion |
| 6871 | memcpy(scratch, begin, length * sizeof(char_t)); |
| 6872 | scratch[length] = 0; |
| 6873 | |
| 6874 | *out_result = convert_string_to_number(scratch); |
| 6875 | |
| 6876 | // free dummy buffer |
| 6877 | if (scratch != buffer) xml_memory::deallocate(scratch); |
| 6878 | |
| 6879 | return true; |
| 6880 | } |
| 6881 | |
| 6882 | PUGI__FN double round_nearest(double value) |
| 6883 | { |
| 6884 | return floor(value + 0.5); |
| 6885 | } |
| 6886 | |
| 6887 | PUGI__FN double round_nearest_nzero(double value) |
| 6888 | { |
| 6889 | // same as round_nearest, but returns -0 for [-0.5, -0] |
| 6890 | // ceil is used to differentiate between +0 and -0 (we return -0 for [-0.5, -0] and +0 for +0) |
| 6891 | return (value >= -0.5 && value <= 0) ? ceil(value) : floor(value + 0.5); |
| 6892 | } |
| 6893 | |
| 6894 | PUGI__FN const char_t* qualified_name(const xpath_node& node) |
| 6895 | { |
| 6896 | return node.attribute() ? node.attribute().name() : node.node().name(); |
| 6897 | } |
| 6898 | |
| 6899 | PUGI__FN const char_t* local_name(const xpath_node& node) |
| 6900 | { |
| 6901 | const char_t* name = qualified_name(node); |
| 6902 | const char_t* p = find_char(name, ':'); |
| 6903 | |
| 6904 | return p ? p + 1 : name; |
| 6905 | } |
| 6906 | |
| 6907 | struct namespace_uri_predicate |
| 6908 | { |
| 6909 | const char_t* prefix; |
| 6910 | size_t prefix_length; |
| 6911 | |
| 6912 | namespace_uri_predicate(const char_t* name) |
| 6913 | { |
| 6914 | const char_t* pos = find_char(name, ':'); |
| 6915 | |
| 6916 | prefix = pos ? name : 0; |
| 6917 | prefix_length = pos ? static_cast<size_t>(pos - name) : 0; |
| 6918 | } |
| 6919 | |
| 6920 | bool operator()(const xml_attribute& a) const |
| 6921 | { |
| 6922 | const char_t* name = a.name(); |
| 6923 | |
| 6924 | if (!starts_with(name, PUGIXML_TEXT("xmlns"))) return false; |
| 6925 | |
| 6926 | return prefix ? name[5] == ':' && strequalrange(name + 6, prefix, prefix_length) : name[5] == 0; |
| 6927 | } |
| 6928 | }; |
| 6929 | |
| 6930 | PUGI__FN const char_t* namespace_uri(const xml_node& node) |
| 6931 | { |
| 6932 | namespace_uri_predicate pred = node.name(); |
| 6933 | |
| 6934 | xml_node p = node; |
| 6935 | |
| 6936 | while (p) |
| 6937 | { |
| 6938 | xml_attribute a = p.find_attribute(pred); |
| 6939 | |
| 6940 | if (a) return a.value(); |
| 6941 | |
| 6942 | p = p.parent(); |
| 6943 | } |
| 6944 | |
| 6945 | return PUGIXML_TEXT(""); |
| 6946 | } |
| 6947 | |
| 6948 | PUGI__FN const char_t* namespace_uri(const xml_attribute& attr, const xml_node& parent) |
| 6949 | { |
| 6950 | namespace_uri_predicate pred = attr.name(); |
| 6951 | |
| 6952 | // Default namespace does not apply to attributes |
| 6953 | if (!pred.prefix) return PUGIXML_TEXT(""); |
| 6954 | |
| 6955 | xml_node p = parent; |
| 6956 | |
| 6957 | while (p) |
| 6958 | { |
| 6959 | xml_attribute a = p.find_attribute(pred); |
| 6960 | |
| 6961 | if (a) return a.value(); |
| 6962 | |
| 6963 | p = p.parent(); |
| 6964 | } |
| 6965 | |
| 6966 | return PUGIXML_TEXT(""); |
| 6967 | } |
| 6968 | |
| 6969 | PUGI__FN const char_t* namespace_uri(const xpath_node& node) |
| 6970 | { |
| 6971 | return node.attribute() ? namespace_uri(node.attribute(), node.parent()) : namespace_uri(node.node()); |
| 6972 | } |
| 6973 | |
| 6974 | PUGI__FN void normalize_space(char_t* buffer) |
| 6975 | { |
| 6976 | char_t* write = buffer; |
| 6977 | |
| 6978 | for (char_t* it = buffer; *it; ) |
| 6979 | { |
| 6980 | char_t ch = *it++; |
| 6981 | |
| 6982 | if (PUGI__IS_CHARTYPE(ch, ct_space)) |
| 6983 | { |
| 6984 | // replace whitespace sequence with single space |
| 6985 | while (PUGI__IS_CHARTYPE(*it, ct_space)) it++; |
| 6986 | |
| 6987 | // avoid leading spaces |
| 6988 | if (write != buffer) *write++ = ' '; |
| 6989 | } |
| 6990 | else *write++ = ch; |
| 6991 | } |
| 6992 | |
| 6993 | // remove trailing space |
| 6994 | if (write != buffer && PUGI__IS_CHARTYPE(write[-1], ct_space)) write--; |
| 6995 | |
| 6996 | // zero-terminate |
| 6997 | *write = 0; |
| 6998 | } |
| 6999 | |
| 7000 | PUGI__FN void translate(char_t* buffer, const char_t* from, const char_t* to) |
| 7001 | { |
| 7002 | size_t to_length = strlength(to); |
| 7003 | |
| 7004 | char_t* write = buffer; |
| 7005 | |
| 7006 | while (*buffer) |
| 7007 | { |
| 7008 | PUGI__DMC_VOLATILE char_t ch = *buffer++; |
| 7009 | |
| 7010 | const char_t* pos = find_char(from, ch); |
| 7011 | |
| 7012 | if (!pos) |
| 7013 | *write++ = ch; // do not process |
| 7014 | else if (static_cast<size_t>(pos - from) < to_length) |
| 7015 | *write++ = to[pos - from]; // replace |
| 7016 | } |
| 7017 | |
| 7018 | // zero-terminate |
| 7019 | *write = 0; |
| 7020 | } |
| 7021 | |
| 7022 | struct xpath_variable_boolean: xpath_variable |
| 7023 | { |
| 7024 | xpath_variable_boolean(): value(false) |
| 7025 | { |
| 7026 | } |
| 7027 | |
| 7028 | bool value; |
| 7029 | char_t name[1]; |
| 7030 | }; |
| 7031 | |
| 7032 | struct xpath_variable_number: xpath_variable |
| 7033 | { |
| 7034 | xpath_variable_number(): value(0) |
| 7035 | { |
| 7036 | } |
| 7037 | |
| 7038 | double value; |
| 7039 | char_t name[1]; |
| 7040 | }; |
| 7041 | |
| 7042 | struct xpath_variable_string: xpath_variable |
| 7043 | { |
| 7044 | xpath_variable_string(): value(0) |
| 7045 | { |
| 7046 | } |
| 7047 | |
| 7048 | ~xpath_variable_string() |
| 7049 | { |
| 7050 | if (value) xml_memory::deallocate(value); |
| 7051 | } |
| 7052 | |
| 7053 | char_t* value; |
| 7054 | char_t name[1]; |
| 7055 | }; |
| 7056 | |
| 7057 | struct xpath_variable_node_set: xpath_variable |
| 7058 | { |
| 7059 | xpath_node_set value; |
| 7060 | char_t name[1]; |
| 7061 | }; |
| 7062 | |
| 7063 | static const xpath_node_set dummy_node_set; |
| 7064 | |
| 7065 | PUGI__FN unsigned int hash_string(const char_t* str) |
| 7066 | { |
| 7067 | // Jenkins one-at-a-time hash (http://en.wikipedia.org/wiki/Jenkins_hash_function#one-at-a-time) |
| 7068 | unsigned int result = 0; |
| 7069 | |
| 7070 | while (*str) |
| 7071 | { |
| 7072 | result += static_cast<unsigned int>(*str++); |
| 7073 | result += result << 10; |
| 7074 | result ^= result >> 6; |
| 7075 | } |
| 7076 | |
| 7077 | result += result << 3; |
| 7078 | result ^= result >> 11; |
| 7079 | result += result << 15; |
| 7080 | |
| 7081 | return result; |
| 7082 | } |
| 7083 | |
| 7084 | template <typename T> PUGI__FN T* new_xpath_variable(const char_t* name) |
| 7085 | { |
| 7086 | size_t length = strlength(name); |
| 7087 | if (length == 0) return 0; // empty variable names are invalid |
| 7088 | |
| 7089 | // $$ we can't use offsetof(T, name) because T is non-POD, so we just allocate additional length characters |
| 7090 | void* memory = xml_memory::allocate(sizeof(T) + length * sizeof(char_t)); |
| 7091 | if (!memory) return 0; |
| 7092 | |
| 7093 | T* result = new (memory) T(); |
| 7094 | |
| 7095 | memcpy(result->name, name, (length + 1) * sizeof(char_t)); |
| 7096 | |
| 7097 | return result; |
| 7098 | } |
| 7099 | |
| 7100 | PUGI__FN xpath_variable* new_xpath_variable(xpath_value_type type, const char_t* name) |
| 7101 | { |
| 7102 | switch (type) |
| 7103 | { |
| 7104 | case xpath_type_node_set: |
| 7105 | return new_xpath_variable<xpath_variable_node_set>(name); |
| 7106 | |
| 7107 | case xpath_type_number: |
| 7108 | return new_xpath_variable<xpath_variable_number>(name); |
| 7109 | |
| 7110 | case xpath_type_string: |
| 7111 | return new_xpath_variable<xpath_variable_string>(name); |
| 7112 | |
| 7113 | case xpath_type_boolean: |
| 7114 | return new_xpath_variable<xpath_variable_boolean>(name); |
| 7115 | |
| 7116 | default: |
| 7117 | return 0; |
| 7118 | } |
| 7119 | } |
| 7120 | |
| 7121 | template <typename T> PUGI__FN void delete_xpath_variable(T* var) |
| 7122 | { |
| 7123 | var->~T(); |
| 7124 | xml_memory::deallocate(var); |
| 7125 | } |
| 7126 | |
| 7127 | PUGI__FN void delete_xpath_variable(xpath_value_type type, xpath_variable* var) |
| 7128 | { |
| 7129 | switch (type) |
| 7130 | { |
| 7131 | case xpath_type_node_set: |
| 7132 | delete_xpath_variable(static_cast<xpath_variable_node_set*>(var)); |
| 7133 | break; |
| 7134 | |
| 7135 | case xpath_type_number: |
| 7136 | delete_xpath_variable(static_cast<xpath_variable_number*>(var)); |
| 7137 | break; |
| 7138 | |
| 7139 | case xpath_type_string: |
| 7140 | delete_xpath_variable(static_cast<xpath_variable_string*>(var)); |
| 7141 | break; |
| 7142 | |
| 7143 | case xpath_type_boolean: |
| 7144 | delete_xpath_variable(static_cast<xpath_variable_boolean*>(var)); |
| 7145 | break; |
| 7146 | |
| 7147 | default: |
| 7148 | assert(!"Invalid variable type"); |
| 7149 | } |
| 7150 | } |
| 7151 | |
| 7152 | PUGI__FN xpath_variable* get_variable_scratch(char_t (&buffer)[32], xpath_variable_set* set, const char_t* begin, const char_t* end) |
| 7153 | { |
| 7154 | size_t length = static_cast<size_t>(end - begin); |
| 7155 | char_t* scratch = buffer; |
| 7156 | |
| 7157 | if (length >= sizeof(buffer) / sizeof(buffer[0])) |
| 7158 | { |
| 7159 | // need to make dummy on-heap copy |
| 7160 | scratch = static_cast<char_t*>(xml_memory::allocate((length + 1) * sizeof(char_t))); |
| 7161 | if (!scratch) return 0; |
| 7162 | } |
| 7163 | |
| 7164 | // copy string to zero-terminated buffer and perform lookup |
| 7165 | memcpy(scratch, begin, length * sizeof(char_t)); |
| 7166 | scratch[length] = 0; |
| 7167 | |
| 7168 | xpath_variable* result = set->get(scratch); |
| 7169 | |
| 7170 | // free dummy buffer |
| 7171 | if (scratch != buffer) xml_memory::deallocate(scratch); |
| 7172 | |
| 7173 | return result; |
| 7174 | } |
| 7175 | PUGI__NS_END |
| 7176 | |
| 7177 | // Internal node set class |
| 7178 | PUGI__NS_BEGIN |
| 7179 | PUGI__FN xpath_node_set::type_t xpath_sort(xpath_node* begin, xpath_node* end, xpath_node_set::type_t type, bool rev) |
| 7180 | { |
| 7181 | xpath_node_set::type_t order = rev ? xpath_node_set::type_sorted_reverse : xpath_node_set::type_sorted; |
| 7182 | |
| 7183 | if (type == xpath_node_set::type_unsorted) |
| 7184 | { |
| 7185 | sort(begin, end, document_order_comparator()); |
| 7186 | |
| 7187 | type = xpath_node_set::type_sorted; |
| 7188 | } |
| 7189 | |
| 7190 | if (type != order) reverse(begin, end); |
| 7191 | |
| 7192 | return order; |
| 7193 | } |
| 7194 | |
| 7195 | PUGI__FN xpath_node xpath_first(const xpath_node* begin, const xpath_node* end, xpath_node_set::type_t type) |
| 7196 | { |
| 7197 | if (begin == end) return xpath_node(); |
| 7198 | |
| 7199 | switch (type) |
| 7200 | { |
| 7201 | case xpath_node_set::type_sorted: |
| 7202 | return *begin; |
| 7203 | |
| 7204 | case xpath_node_set::type_sorted_reverse: |
| 7205 | return *(end - 1); |
| 7206 | |
| 7207 | case xpath_node_set::type_unsorted: |
| 7208 | return *min_element(begin, end, document_order_comparator()); |
| 7209 | |
| 7210 | default: |
| 7211 | assert(!"Invalid node set type"); |
| 7212 | return xpath_node(); |
| 7213 | } |
| 7214 | } |
| 7215 | |
| 7216 | class xpath_node_set_raw |
| 7217 | { |
| 7218 | xpath_node_set::type_t _type; |
| 7219 | |
| 7220 | xpath_node* _begin; |
| 7221 | xpath_node* _end; |
| 7222 | xpath_node* _eos; |
| 7223 | |
| 7224 | public: |
| 7225 | xpath_node_set_raw(): _type(xpath_node_set::type_unsorted), _begin(0), _end(0), _eos(0) |
| 7226 | { |
| 7227 | } |
| 7228 | |
| 7229 | xpath_node* begin() const |
| 7230 | { |
| 7231 | return _begin; |
| 7232 | } |
| 7233 | |
| 7234 | xpath_node* end() const |
| 7235 | { |
| 7236 | return _end; |
| 7237 | } |
| 7238 | |
| 7239 | bool empty() const |
| 7240 | { |
| 7241 | return _begin == _end; |
| 7242 | } |
| 7243 | |
| 7244 | size_t size() const |
| 7245 | { |
| 7246 | return static_cast<size_t>(_end - _begin); |
| 7247 | } |
| 7248 | |
| 7249 | xpath_node first() const |
| 7250 | { |
| 7251 | return xpath_first(_begin, _end, _type); |
| 7252 | } |
| 7253 | |
| 7254 | void push_back(const xpath_node& node, xpath_allocator* alloc) |
| 7255 | { |
| 7256 | if (_end == _eos) |
| 7257 | { |
| 7258 | size_t capacity = static_cast<size_t>(_eos - _begin); |
| 7259 | |
| 7260 | // get new capacity (1.5x rule) |
| 7261 | size_t new_capacity = capacity + capacity / 2 + 1; |
| 7262 | |
| 7263 | // reallocate the old array or allocate a new one |
| 7264 | xpath_node* data = static_cast<xpath_node*>(alloc->reallocate(_begin, capacity * sizeof(xpath_node), new_capacity * sizeof(xpath_node))); |
| 7265 | assert(data); |
| 7266 | |
| 7267 | // finalize |
| 7268 | _begin = data; |
| 7269 | _end = data + capacity; |
| 7270 | _eos = data + new_capacity; |
| 7271 | } |
| 7272 | |
| 7273 | *_end++ = node; |
| 7274 | } |
| 7275 | |
| 7276 | void append(const xpath_node* begin_, const xpath_node* end_, xpath_allocator* alloc) |
| 7277 | { |
| 7278 | size_t size_ = static_cast<size_t>(_end - _begin); |
| 7279 | size_t capacity = static_cast<size_t>(_eos - _begin); |
| 7280 | size_t count = static_cast<size_t>(end_ - begin_); |
| 7281 | |
| 7282 | if (size_ + count > capacity) |
| 7283 | { |
| 7284 | // reallocate the old array or allocate a new one |
| 7285 | xpath_node* data = static_cast<xpath_node*>(alloc->reallocate(_begin, capacity * sizeof(xpath_node), (size_ + count) * sizeof(xpath_node))); |
| 7286 | assert(data); |
| 7287 | |
| 7288 | // finalize |
| 7289 | _begin = data; |
| 7290 | _end = data + size_; |
| 7291 | _eos = data + size_ + count; |
| 7292 | } |
| 7293 | |
| 7294 | memcpy(_end, begin_, count * sizeof(xpath_node)); |
| 7295 | _end += count; |
| 7296 | } |
| 7297 | |
| 7298 | void sort_do() |
| 7299 | { |
| 7300 | _type = xpath_sort(_begin, _end, _type, false); |
| 7301 | } |
| 7302 | |
| 7303 | void truncate(xpath_node* pos) |
| 7304 | { |
| 7305 | assert(_begin <= pos && pos <= _end); |
| 7306 | |
| 7307 | _end = pos; |
| 7308 | } |
| 7309 | |
| 7310 | void remove_duplicates() |
| 7311 | { |
| 7312 | if (_type == xpath_node_set::type_unsorted) |
| 7313 | sort(_begin, _end, duplicate_comparator()); |
| 7314 | |
| 7315 | _end = unique(_begin, _end); |
| 7316 | } |
| 7317 | |
| 7318 | xpath_node_set::type_t type() const |
| 7319 | { |
| 7320 | return _type; |
| 7321 | } |
| 7322 | |
| 7323 | void set_type(xpath_node_set::type_t value) |
| 7324 | { |
| 7325 | _type = value; |
| 7326 | } |
| 7327 | }; |
| 7328 | PUGI__NS_END |
| 7329 | |
| 7330 | PUGI__NS_BEGIN |
| 7331 | struct xpath_context |
| 7332 | { |
| 7333 | xpath_node n; |
| 7334 | size_t position, size; |
| 7335 | |
| 7336 | xpath_context(const xpath_node& n_, size_t position_, size_t size_): n(n_), position(position_), size(size_) |
| 7337 | { |
| 7338 | } |
| 7339 | }; |
| 7340 | |
| 7341 | enum lexeme_t |
| 7342 | { |
| 7343 | lex_none = 0, |
| 7344 | lex_equal, |
| 7345 | lex_not_equal, |
| 7346 | lex_less, |
| 7347 | lex_greater, |
| 7348 | lex_less_or_equal, |
| 7349 | lex_greater_or_equal, |
| 7350 | lex_plus, |
| 7351 | lex_minus, |
| 7352 | lex_multiply, |
| 7353 | lex_union, |
| 7354 | lex_var_ref, |
| 7355 | lex_open_brace, |
| 7356 | lex_close_brace, |
| 7357 | lex_quoted_string, |
| 7358 | lex_number, |
| 7359 | lex_slash, |
| 7360 | lex_double_slash, |
| 7361 | lex_open_square_brace, |
| 7362 | lex_close_square_brace, |
| 7363 | lex_string, |
| 7364 | lex_comma, |
| 7365 | lex_axis_attribute, |
| 7366 | lex_dot, |
| 7367 | lex_double_dot, |
| 7368 | lex_double_colon, |
| 7369 | lex_eof |
| 7370 | }; |
| 7371 | |
| 7372 | struct xpath_lexer_string |
| 7373 | { |
| 7374 | const char_t* begin; |
| 7375 | const char_t* end; |
| 7376 | |
| 7377 | xpath_lexer_string(): begin(0), end(0) |
| 7378 | { |
| 7379 | } |
| 7380 | |
| 7381 | bool operator==(const char_t* other) const |
| 7382 | { |
| 7383 | size_t length = static_cast<size_t>(end - begin); |
| 7384 | |
| 7385 | return strequalrange(other, begin, length); |
| 7386 | } |
| 7387 | }; |
| 7388 | |
| 7389 | class xpath_lexer |
| 7390 | { |
| 7391 | const char_t* _cur; |
| 7392 | const char_t* _cur_lexeme_pos; |
| 7393 | xpath_lexer_string _cur_lexeme_contents; |
| 7394 | |
| 7395 | lexeme_t _cur_lexeme; |
| 7396 | |
| 7397 | public: |
| 7398 | explicit xpath_lexer(const char_t* query): _cur(query) |
| 7399 | { |
| 7400 | next(); |
| 7401 | } |
| 7402 | |
| 7403 | const char_t* state() const |
| 7404 | { |
| 7405 | return _cur; |
| 7406 | } |
| 7407 | |
| 7408 | void next() |
| 7409 | { |
| 7410 | const char_t* cur = _cur; |
| 7411 | |
| 7412 | while (PUGI__IS_CHARTYPE(*cur, ct_space)) ++cur; |
| 7413 | |
| 7414 | // save lexeme position for error reporting |
| 7415 | _cur_lexeme_pos = cur; |
| 7416 | |
| 7417 | switch (*cur) |
| 7418 | { |
| 7419 | case 0: |
| 7420 | _cur_lexeme = lex_eof; |
| 7421 | break; |
| 7422 | |
| 7423 | case '>': |
| 7424 | if (*(cur+1) == '=') |
| 7425 | { |
| 7426 | cur += 2; |
| 7427 | _cur_lexeme = lex_greater_or_equal; |
| 7428 | } |
| 7429 | else |
| 7430 | { |
| 7431 | cur += 1; |
| 7432 | _cur_lexeme = lex_greater; |
| 7433 | } |
| 7434 | break; |
| 7435 | |
| 7436 | case '<': |
| 7437 | if (*(cur+1) == '=') |
| 7438 | { |
| 7439 | cur += 2; |
| 7440 | _cur_lexeme = lex_less_or_equal; |
| 7441 | } |
| 7442 | else |
| 7443 | { |
| 7444 | cur += 1; |
| 7445 | _cur_lexeme = lex_less; |
| 7446 | } |
| 7447 | break; |
| 7448 | |
| 7449 | case '!': |
| 7450 | if (*(cur+1) == '=') |
| 7451 | { |
| 7452 | cur += 2; |
| 7453 | _cur_lexeme = lex_not_equal; |
| 7454 | } |
| 7455 | else |
| 7456 | { |
| 7457 | _cur_lexeme = lex_none; |
| 7458 | } |
| 7459 | break; |
| 7460 | |
| 7461 | case '=': |
| 7462 | cur += 1; |
| 7463 | _cur_lexeme = lex_equal; |
| 7464 | |
| 7465 | break; |
| 7466 | |
| 7467 | case '+': |
| 7468 | cur += 1; |
| 7469 | _cur_lexeme = lex_plus; |
| 7470 | |
| 7471 | break; |
| 7472 | |
| 7473 | case '-': |
| 7474 | cur += 1; |
| 7475 | _cur_lexeme = lex_minus; |
| 7476 | |
| 7477 | break; |
| 7478 | |
| 7479 | case '*': |
| 7480 | cur += 1; |
| 7481 | _cur_lexeme = lex_multiply; |
| 7482 | |
| 7483 | break; |
| 7484 | |
| 7485 | case '|': |
| 7486 | cur += 1; |
| 7487 | _cur_lexeme = lex_union; |
| 7488 | |
| 7489 | break; |
| 7490 | |
| 7491 | case '$': |
| 7492 | cur += 1; |
| 7493 | |
| 7494 | if (PUGI__IS_CHARTYPEX(*cur, ctx_start_symbol)) |
| 7495 | { |
| 7496 | _cur_lexeme_contents.begin = cur; |
| 7497 | |
| 7498 | while (PUGI__IS_CHARTYPEX(*cur, ctx_symbol)) cur++; |
| 7499 | |
| 7500 | if (cur[0] == ':' && PUGI__IS_CHARTYPEX(cur[1], ctx_symbol)) // qname |
| 7501 | { |
| 7502 | cur++; // : |
| 7503 | |
| 7504 | while (PUGI__IS_CHARTYPEX(*cur, ctx_symbol)) cur++; |
| 7505 | } |
| 7506 | |
| 7507 | _cur_lexeme_contents.end = cur; |
| 7508 | |
| 7509 | _cur_lexeme = lex_var_ref; |
| 7510 | } |
| 7511 | else |
| 7512 | { |
| 7513 | _cur_lexeme = lex_none; |
| 7514 | } |
| 7515 | |
| 7516 | break; |
| 7517 | |
| 7518 | case '(': |
| 7519 | cur += 1; |
| 7520 | _cur_lexeme = lex_open_brace; |
| 7521 | |
| 7522 | break; |
| 7523 | |
| 7524 | case ')': |
| 7525 | cur += 1; |
| 7526 | _cur_lexeme = lex_close_brace; |
| 7527 | |
| 7528 | break; |
| 7529 | |
| 7530 | case '[': |
| 7531 | cur += 1; |
| 7532 | _cur_lexeme = lex_open_square_brace; |
| 7533 | |
| 7534 | break; |
| 7535 | |
| 7536 | case ']': |
| 7537 | cur += 1; |
| 7538 | _cur_lexeme = lex_close_square_brace; |
| 7539 | |
| 7540 | break; |
| 7541 | |
| 7542 | case ',': |
| 7543 | cur += 1; |
| 7544 | _cur_lexeme = lex_comma; |
| 7545 | |
| 7546 | break; |
| 7547 | |
| 7548 | case '/': |
| 7549 | if (*(cur+1) == '/') |
| 7550 | { |
| 7551 | cur += 2; |
| 7552 | _cur_lexeme = lex_double_slash; |
| 7553 | } |
| 7554 | else |
| 7555 | { |
| 7556 | cur += 1; |
| 7557 | _cur_lexeme = lex_slash; |
| 7558 | } |
| 7559 | break; |
| 7560 | |
| 7561 | case '.': |
| 7562 | if (*(cur+1) == '.') |
| 7563 | { |
| 7564 | cur += 2; |
| 7565 | _cur_lexeme = lex_double_dot; |
| 7566 | } |
| 7567 | else if (PUGI__IS_CHARTYPEX(*(cur+1), ctx_digit)) |
| 7568 | { |
| 7569 | _cur_lexeme_contents.begin = cur; // . |
| 7570 | |
| 7571 | ++cur; |
| 7572 | |
| 7573 | while (PUGI__IS_CHARTYPEX(*cur, ctx_digit)) cur++; |
| 7574 | |
| 7575 | _cur_lexeme_contents.end = cur; |
| 7576 | |
| 7577 | _cur_lexeme = lex_number; |
| 7578 | } |
| 7579 | else |
| 7580 | { |
| 7581 | cur += 1; |
| 7582 | _cur_lexeme = lex_dot; |
| 7583 | } |
| 7584 | break; |
| 7585 | |
| 7586 | case '@': |
| 7587 | cur += 1; |
| 7588 | _cur_lexeme = lex_axis_attribute; |
| 7589 | |
| 7590 | break; |
| 7591 | |
| 7592 | case '"': |
| 7593 | case '\'': |
| 7594 | { |
| 7595 | char_t terminator = *cur; |
| 7596 | |
| 7597 | ++cur; |
| 7598 | |
| 7599 | _cur_lexeme_contents.begin = cur; |
| 7600 | while (*cur && *cur != terminator) cur++; |
| 7601 | _cur_lexeme_contents.end = cur; |
| 7602 | |
| 7603 | if (!*cur) |
| 7604 | _cur_lexeme = lex_none; |
| 7605 | else |
| 7606 | { |
| 7607 | cur += 1; |
| 7608 | _cur_lexeme = lex_quoted_string; |
| 7609 | } |
| 7610 | |
| 7611 | break; |
| 7612 | } |
| 7613 | |
| 7614 | case ':': |
| 7615 | if (*(cur+1) == ':') |
| 7616 | { |
| 7617 | cur += 2; |
| 7618 | _cur_lexeme = lex_double_colon; |
| 7619 | } |
| 7620 | else |
| 7621 | { |
| 7622 | _cur_lexeme = lex_none; |
| 7623 | } |
| 7624 | break; |
| 7625 | |
| 7626 | default: |
| 7627 | if (PUGI__IS_CHARTYPEX(*cur, ctx_digit)) |
| 7628 | { |
| 7629 | _cur_lexeme_contents.begin = cur; |
| 7630 | |
| 7631 | while (PUGI__IS_CHARTYPEX(*cur, ctx_digit)) cur++; |
| 7632 | |
| 7633 | if (*cur == '.') |
| 7634 | { |
| 7635 | cur++; |
| 7636 | |
| 7637 | while (PUGI__IS_CHARTYPEX(*cur, ctx_digit)) cur++; |
| 7638 | } |
| 7639 | |
| 7640 | _cur_lexeme_contents.end = cur; |
| 7641 | |
| 7642 | _cur_lexeme = lex_number; |
| 7643 | } |
| 7644 | else if (PUGI__IS_CHARTYPEX(*cur, ctx_start_symbol)) |
| 7645 | { |
| 7646 | _cur_lexeme_contents.begin = cur; |
| 7647 | |
| 7648 | while (PUGI__IS_CHARTYPEX(*cur, ctx_symbol)) cur++; |
| 7649 | |
| 7650 | if (cur[0] == ':') |
| 7651 | { |
| 7652 | if (cur[1] == '*') // namespace test ncname:* |
| 7653 | { |
| 7654 | cur += 2; // :* |
| 7655 | } |
| 7656 | else if (PUGI__IS_CHARTYPEX(cur[1], ctx_symbol)) // namespace test qname |
| 7657 | { |
| 7658 | cur++; // : |
| 7659 | |
| 7660 | while (PUGI__IS_CHARTYPEX(*cur, ctx_symbol)) cur++; |
| 7661 | } |
| 7662 | } |
| 7663 | |
| 7664 | _cur_lexeme_contents.end = cur; |
| 7665 | |
| 7666 | _cur_lexeme = lex_string; |
| 7667 | } |
| 7668 | else |
| 7669 | { |
| 7670 | _cur_lexeme = lex_none; |
| 7671 | } |
| 7672 | } |
| 7673 | |
| 7674 | _cur = cur; |
| 7675 | } |
| 7676 | |
| 7677 | lexeme_t current() const |
| 7678 | { |
| 7679 | return _cur_lexeme; |
| 7680 | } |
| 7681 | |
| 7682 | const char_t* current_pos() const |
| 7683 | { |
| 7684 | return _cur_lexeme_pos; |
| 7685 | } |
| 7686 | |
| 7687 | const xpath_lexer_string& contents() const |
| 7688 | { |
| 7689 | assert(_cur_lexeme == lex_var_ref || _cur_lexeme == lex_number || _cur_lexeme == lex_string || _cur_lexeme == lex_quoted_string); |
| 7690 | |
| 7691 | return _cur_lexeme_contents; |
| 7692 | } |
| 7693 | }; |
| 7694 | |
| 7695 | enum ast_type_t |
| 7696 | { |
| 7697 | ast_unknown, |
| 7698 | ast_op_or, // left or right |
| 7699 | ast_op_and, // left and right |
| 7700 | ast_op_equal, // left = right |
| 7701 | ast_op_not_equal, // left != right |
| 7702 | ast_op_less, // left < right |
| 7703 | ast_op_greater, // left > right |
| 7704 | ast_op_less_or_equal, // left <= right |
| 7705 | ast_op_greater_or_equal, // left >= right |
| 7706 | ast_op_add, // left + right |
| 7707 | ast_op_subtract, // left - right |
| 7708 | ast_op_multiply, // left * right |
| 7709 | ast_op_divide, // left / right |
| 7710 | ast_op_mod, // left % right |
| 7711 | ast_op_negate, // left - right |
| 7712 | ast_op_union, // left | right |
| 7713 | ast_predicate, // apply predicate to set; next points to next predicate |
| 7714 | ast_filter, // select * from left where right |
| 7715 | ast_filter_posinv, // select * from left where right; proximity position invariant |
| 7716 | ast_string_constant, // string constant |
| 7717 | ast_number_constant, // number constant |
| 7718 | ast_variable, // variable |
| 7719 | ast_func_last, // last() |
| 7720 | ast_func_position, // position() |
| 7721 | ast_func_count, // count(left) |
| 7722 | ast_func_id, // id(left) |
| 7723 | ast_func_local_name_0, // local-name() |
| 7724 | ast_func_local_name_1, // local-name(left) |
| 7725 | ast_func_namespace_uri_0, // namespace-uri() |
| 7726 | ast_func_namespace_uri_1, // namespace-uri(left) |
| 7727 | ast_func_name_0, // name() |
| 7728 | ast_func_name_1, // name(left) |
| 7729 | ast_func_string_0, // string() |
| 7730 | ast_func_string_1, // string(left) |
| 7731 | ast_func_concat, // concat(left, right, siblings) |
| 7732 | ast_func_starts_with, // starts_with(left, right) |
| 7733 | ast_func_contains, // contains(left, right) |
| 7734 | ast_func_substring_before, // substring-before(left, right) |
| 7735 | ast_func_substring_after, // substring-after(left, right) |
| 7736 | ast_func_substring_2, // substring(left, right) |
| 7737 | ast_func_substring_3, // substring(left, right, third) |
| 7738 | ast_func_string_length_0, // string-length() |
| 7739 | ast_func_string_length_1, // string-length(left) |
| 7740 | ast_func_normalize_space_0, // normalize-space() |
| 7741 | ast_func_normalize_space_1, // normalize-space(left) |
| 7742 | ast_func_translate, // translate(left, right, third) |
| 7743 | ast_func_boolean, // boolean(left) |
| 7744 | ast_func_not, // not(left) |
| 7745 | ast_func_true, // true() |
| 7746 | ast_func_false, // false() |
| 7747 | ast_func_lang, // lang(left) |
| 7748 | ast_func_number_0, // number() |
| 7749 | ast_func_number_1, // number(left) |
| 7750 | ast_func_sum, // sum(left) |
| 7751 | ast_func_floor, // floor(left) |
| 7752 | ast_func_ceiling, // ceiling(left) |
| 7753 | ast_func_round, // round(left) |
| 7754 | ast_step, // process set left with step |
| 7755 | ast_step_root // select root node |
| 7756 | }; |
| 7757 | |
| 7758 | enum axis_t |
| 7759 | { |
| 7760 | axis_ancestor, |
| 7761 | axis_ancestor_or_self, |
| 7762 | axis_attribute, |
| 7763 | axis_child, |
| 7764 | axis_descendant, |
| 7765 | axis_descendant_or_self, |
| 7766 | axis_following, |
| 7767 | axis_following_sibling, |
| 7768 | axis_namespace, |
| 7769 | axis_parent, |
| 7770 | axis_preceding, |
| 7771 | axis_preceding_sibling, |
| 7772 | axis_self |
| 7773 | }; |
| 7774 | |
| 7775 | enum nodetest_t |
| 7776 | { |
| 7777 | nodetest_none, |
| 7778 | nodetest_name, |
| 7779 | nodetest_type_node, |
| 7780 | nodetest_type_comment, |
| 7781 | nodetest_type_pi, |
| 7782 | nodetest_type_text, |
| 7783 | nodetest_pi, |
| 7784 | nodetest_all, |
| 7785 | nodetest_all_in_namespace |
| 7786 | }; |
| 7787 | |
| 7788 | template <axis_t N> struct axis_to_type |
| 7789 | { |
| 7790 | static const axis_t axis; |
| 7791 | }; |
| 7792 | |
| 7793 | template <axis_t N> const axis_t axis_to_type<N>::axis = N; |
| 7794 | |
| 7795 | class xpath_ast_node |
| 7796 | { |
| 7797 | private: |
| 7798 | // node type |
| 7799 | char _type; |
| 7800 | char _rettype; |
| 7801 | |
| 7802 | // for ast_step / ast_predicate |
| 7803 | char _axis; |
| 7804 | char _test; |
| 7805 | |
| 7806 | // tree node structure |
| 7807 | xpath_ast_node* _left; |
| 7808 | xpath_ast_node* _right; |
| 7809 | xpath_ast_node* _next; |
| 7810 | |
| 7811 | union |
| 7812 | { |
| 7813 | // value for ast_string_constant |
| 7814 | const char_t* string; |
| 7815 | // value for ast_number_constant |
| 7816 | double number; |
| 7817 | // variable for ast_variable |
| 7818 | xpath_variable* variable; |
| 7819 | // node test for ast_step (node name/namespace/node type/pi target) |
| 7820 | const char_t* nodetest; |
| 7821 | } _data; |
| 7822 | |
| 7823 | xpath_ast_node(const xpath_ast_node&); |
| 7824 | xpath_ast_node& operator=(const xpath_ast_node&); |
| 7825 | |
| 7826 | template <class Comp> static bool compare_eq(xpath_ast_node* lhs, xpath_ast_node* rhs, const xpath_context& c, const xpath_stack& stack, const Comp& comp) |
| 7827 | { |
| 7828 | xpath_value_type lt = lhs->rettype(), rt = rhs->rettype(); |
| 7829 | |
| 7830 | if (lt != xpath_type_node_set && rt != xpath_type_node_set) |
| 7831 | { |
| 7832 | if (lt == xpath_type_boolean || rt == xpath_type_boolean) |
| 7833 | return comp(lhs->eval_boolean(c, stack), rhs->eval_boolean(c, stack)); |
| 7834 | else if (lt == xpath_type_number || rt == xpath_type_number) |
| 7835 | return comp(lhs->eval_number(c, stack), rhs->eval_number(c, stack)); |
| 7836 | else if (lt == xpath_type_string || rt == xpath_type_string) |
| 7837 | { |
| 7838 | xpath_allocator_capture cr(stack.result); |
| 7839 | |
| 7840 | xpath_string ls = lhs->eval_string(c, stack); |
| 7841 | xpath_string rs = rhs->eval_string(c, stack); |
| 7842 | |
| 7843 | return comp(ls, rs); |
| 7844 | } |
| 7845 | } |
| 7846 | else if (lt == xpath_type_node_set && rt == xpath_type_node_set) |
| 7847 | { |
| 7848 | xpath_allocator_capture cr(stack.result); |
| 7849 | |
| 7850 | xpath_node_set_raw ls = lhs->eval_node_set(c, stack); |
| 7851 | xpath_node_set_raw rs = rhs->eval_node_set(c, stack); |
| 7852 | |
| 7853 | for (const xpath_node* li = ls.begin(); li != ls.end(); ++li) |
| 7854 | for (const xpath_node* ri = rs.begin(); ri != rs.end(); ++ri) |
| 7855 | { |
| 7856 | xpath_allocator_capture cri(stack.result); |
| 7857 | |
| 7858 | if (comp(string_value(*li, stack.result), string_value(*ri, stack.result))) |
| 7859 | return true; |
| 7860 | } |
| 7861 | |
| 7862 | return false; |
| 7863 | } |
| 7864 | else |
| 7865 | { |
| 7866 | if (lt == xpath_type_node_set) |
| 7867 | { |
| 7868 | swap(lhs, rhs); |
| 7869 | swap(lt, rt); |
| 7870 | } |
| 7871 | |
| 7872 | if (lt == xpath_type_boolean) |
| 7873 | return comp(lhs->eval_boolean(c, stack), rhs->eval_boolean(c, stack)); |
| 7874 | else if (lt == xpath_type_number) |
| 7875 | { |
| 7876 | xpath_allocator_capture cr(stack.result); |
| 7877 | |
| 7878 | double l = lhs->eval_number(c, stack); |
| 7879 | xpath_node_set_raw rs = rhs->eval_node_set(c, stack); |
| 7880 | |
| 7881 | for (const xpath_node* ri = rs.begin(); ri != rs.end(); ++ri) |
| 7882 | { |
| 7883 | xpath_allocator_capture cri(stack.result); |
| 7884 | |
| 7885 | if (comp(l, convert_string_to_number(string_value(*ri, stack.result).c_str()))) |
| 7886 | return true; |
| 7887 | } |
| 7888 | |
| 7889 | return false; |
| 7890 | } |
| 7891 | else if (lt == xpath_type_string) |
| 7892 | { |
| 7893 | xpath_allocator_capture cr(stack.result); |
| 7894 | |
| 7895 | xpath_string l = lhs->eval_string(c, stack); |
| 7896 | xpath_node_set_raw rs = rhs->eval_node_set(c, stack); |
| 7897 | |
| 7898 | for (const xpath_node* ri = rs.begin(); ri != rs.end(); ++ri) |
| 7899 | { |
| 7900 | xpath_allocator_capture cri(stack.result); |
| 7901 | |
| 7902 | if (comp(l, string_value(*ri, stack.result))) |
| 7903 | return true; |
| 7904 | } |
| 7905 | |
| 7906 | return false; |
| 7907 | } |
| 7908 | } |
| 7909 | |
| 7910 | assert(!"Wrong types"); |
| 7911 | return false; |
| 7912 | } |
| 7913 | |
| 7914 | template <class Comp> static bool compare_rel(xpath_ast_node* lhs, xpath_ast_node* rhs, const xpath_context& c, const xpath_stack& stack, const Comp& comp) |
| 7915 | { |
| 7916 | xpath_value_type lt = lhs->rettype(), rt = rhs->rettype(); |
| 7917 | |
| 7918 | if (lt != xpath_type_node_set && rt != xpath_type_node_set) |
| 7919 | return comp(lhs->eval_number(c, stack), rhs->eval_number(c, stack)); |
| 7920 | else if (lt == xpath_type_node_set && rt == xpath_type_node_set) |
| 7921 | { |
| 7922 | xpath_allocator_capture cr(stack.result); |
| 7923 | |
| 7924 | xpath_node_set_raw ls = lhs->eval_node_set(c, stack); |
| 7925 | xpath_node_set_raw rs = rhs->eval_node_set(c, stack); |
| 7926 | |
| 7927 | for (const xpath_node* li = ls.begin(); li != ls.end(); ++li) |
| 7928 | { |
| 7929 | xpath_allocator_capture cri(stack.result); |
| 7930 | |
| 7931 | double l = convert_string_to_number(string_value(*li, stack.result).c_str()); |
| 7932 | |
| 7933 | for (const xpath_node* ri = rs.begin(); ri != rs.end(); ++ri) |
| 7934 | { |
| 7935 | xpath_allocator_capture crii(stack.result); |
| 7936 | |
| 7937 | if (comp(l, convert_string_to_number(string_value(*ri, stack.result).c_str()))) |
| 7938 | return true; |
| 7939 | } |
| 7940 | } |
| 7941 | |
| 7942 | return false; |
| 7943 | } |
| 7944 | else if (lt != xpath_type_node_set && rt == xpath_type_node_set) |
| 7945 | { |
| 7946 | xpath_allocator_capture cr(stack.result); |
| 7947 | |
| 7948 | double l = lhs->eval_number(c, stack); |
| 7949 | xpath_node_set_raw rs = rhs->eval_node_set(c, stack); |
| 7950 | |
| 7951 | for (const xpath_node* ri = rs.begin(); ri != rs.end(); ++ri) |
| 7952 | { |
| 7953 | xpath_allocator_capture cri(stack.result); |
| 7954 | |
| 7955 | if (comp(l, convert_string_to_number(string_value(*ri, stack.result).c_str()))) |
| 7956 | return true; |
| 7957 | } |
| 7958 | |
| 7959 | return false; |
| 7960 | } |
| 7961 | else if (lt == xpath_type_node_set && rt != xpath_type_node_set) |
| 7962 | { |
| 7963 | xpath_allocator_capture cr(stack.result); |
| 7964 | |
| 7965 | xpath_node_set_raw ls = lhs->eval_node_set(c, stack); |
| 7966 | double r = rhs->eval_number(c, stack); |
| 7967 | |
| 7968 | for (const xpath_node* li = ls.begin(); li != ls.end(); ++li) |
| 7969 | { |
| 7970 | xpath_allocator_capture cri(stack.result); |
| 7971 | |
| 7972 | if (comp(convert_string_to_number(string_value(*li, stack.result).c_str()), r)) |
| 7973 | return true; |
| 7974 | } |
| 7975 | |
| 7976 | return false; |
| 7977 | } |
| 7978 | else |
| 7979 | { |
| 7980 | assert(!"Wrong types"); |
| 7981 | return false; |
| 7982 | } |
| 7983 | } |
| 7984 | |
| 7985 | void apply_predicate(xpath_node_set_raw& ns, size_t first, xpath_ast_node* expr, const xpath_stack& stack) |
| 7986 | { |
| 7987 | assert(ns.size() >= first); |
| 7988 | |
| 7989 | size_t i = 1; |
| 7990 | size_t size = ns.size() - first; |
| 7991 | |
| 7992 | xpath_node* last = ns.begin() + first; |
| 7993 | |
| 7994 | // remove_if... or well, sort of |
| 7995 | for (xpath_node* it = last; it != ns.end(); ++it, ++i) |
| 7996 | { |
| 7997 | xpath_context c(*it, i, size); |
| 7998 | |
| 7999 | if (expr->rettype() == xpath_type_number) |
| 8000 | { |
| 8001 | if (expr->eval_number(c, stack) == i) |
| 8002 | *last++ = *it; |
| 8003 | } |
| 8004 | else if (expr->eval_boolean(c, stack)) |
| 8005 | *last++ = *it; |
| 8006 | } |
| 8007 | |
| 8008 | ns.truncate(last); |
| 8009 | } |
| 8010 | |
| 8011 | void apply_predicates(xpath_node_set_raw& ns, size_t first, const xpath_stack& stack) |
| 8012 | { |
| 8013 | if (ns.size() == first) return; |
| 8014 | |
| 8015 | for (xpath_ast_node* pred = _right; pred; pred = pred->_next) |
| 8016 | { |
| 8017 | apply_predicate(ns, first, pred->_left, stack); |
| 8018 | } |
| 8019 | } |
| 8020 | |
| 8021 | void step_push(xpath_node_set_raw& ns, const xml_attribute& a, const xml_node& parent, xpath_allocator* alloc) |
| 8022 | { |
| 8023 | if (!a) return; |
| 8024 | |
| 8025 | const char_t* name = a.name(); |
| 8026 | |
| 8027 | // There are no attribute nodes corresponding to attributes that declare namespaces |
| 8028 | // That is, "xmlns:..." or "xmlns" |
| 8029 | if (starts_with(name, PUGIXML_TEXT("xmlns")) && (name[5] == 0 || name[5] == ':')) return; |
| 8030 | |
| 8031 | switch (_test) |
| 8032 | { |
| 8033 | case nodetest_name: |
| 8034 | if (strequal(name, _data.nodetest)) ns.push_back(xpath_node(a, parent), alloc); |
| 8035 | break; |
| 8036 | |
| 8037 | case nodetest_type_node: |
| 8038 | case nodetest_all: |
| 8039 | ns.push_back(xpath_node(a, parent), alloc); |
| 8040 | break; |
| 8041 | |
| 8042 | case nodetest_all_in_namespace: |
| 8043 | if (starts_with(name, _data.nodetest)) |
| 8044 | ns.push_back(xpath_node(a, parent), alloc); |
| 8045 | break; |
| 8046 | |
| 8047 | default: |
| 8048 | ; |
| 8049 | } |
| 8050 | } |
| 8051 | |
| 8052 | void step_push(xpath_node_set_raw& ns, const xml_node& n, xpath_allocator* alloc) |
| 8053 | { |
| 8054 | if (!n) return; |
| 8055 | |
| 8056 | switch (_test) |
| 8057 | { |
| 8058 | case nodetest_name: |
| 8059 | if (n.type() == node_element && strequal(n.name(), _data.nodetest)) ns.push_back(n, alloc); |
| 8060 | break; |
| 8061 | |
| 8062 | case nodetest_type_node: |
| 8063 | ns.push_back(n, alloc); |
| 8064 | break; |
| 8065 | |
| 8066 | case nodetest_type_comment: |
| 8067 | if (n.type() == node_comment) |
| 8068 | ns.push_back(n, alloc); |
| 8069 | break; |
| 8070 | |
| 8071 | case nodetest_type_text: |
| 8072 | if (n.type() == node_pcdata || n.type() == node_cdata) |
| 8073 | ns.push_back(n, alloc); |
| 8074 | break; |
| 8075 | |
| 8076 | case nodetest_type_pi: |
| 8077 | if (n.type() == node_pi) |
| 8078 | ns.push_back(n, alloc); |
| 8079 | break; |
| 8080 | |
| 8081 | case nodetest_pi: |
| 8082 | if (n.type() == node_pi && strequal(n.name(), _data.nodetest)) |
| 8083 | ns.push_back(n, alloc); |
| 8084 | break; |
| 8085 | |
| 8086 | case nodetest_all: |
| 8087 | if (n.type() == node_element) |
| 8088 | ns.push_back(n, alloc); |
| 8089 | break; |
| 8090 | |
| 8091 | case nodetest_all_in_namespace: |
| 8092 | if (n.type() == node_element && starts_with(n.name(), _data.nodetest)) |
| 8093 | ns.push_back(n, alloc); |
| 8094 | break; |
| 8095 | |
| 8096 | default: |
| 8097 | assert(!"Unknown axis"); |
| 8098 | } |
| 8099 | } |
| 8100 | |
| 8101 | template <class T> void step_fill(xpath_node_set_raw& ns, const xml_node& n, xpath_allocator* alloc, T) |
| 8102 | { |
| 8103 | const axis_t axis = T::axis; |
| 8104 | |
| 8105 | switch (axis) |
| 8106 | { |
| 8107 | case axis_attribute: |
| 8108 | { |
| 8109 | for (xml_attribute a = n.first_attribute(); a; a = a.next_attribute()) |
| 8110 | step_push(ns, a, n, alloc); |
| 8111 | |
| 8112 | break; |
| 8113 | } |
| 8114 | |
| 8115 | case axis_child: |
| 8116 | { |
| 8117 | for (xml_node c = n.first_child(); c; c = c.next_sibling()) |
| 8118 | step_push(ns, c, alloc); |
| 8119 | |
| 8120 | break; |
| 8121 | } |
| 8122 | |
| 8123 | case axis_descendant: |
| 8124 | case axis_descendant_or_self: |
| 8125 | { |
| 8126 | if (axis == axis_descendant_or_self) |
| 8127 | step_push(ns, n, alloc); |
| 8128 | |
| 8129 | xml_node cur = n.first_child(); |
| 8130 | |
| 8131 | while (cur && cur != n) |
| 8132 | { |
| 8133 | step_push(ns, cur, alloc); |
| 8134 | |
| 8135 | if (cur.first_child()) |
| 8136 | cur = cur.first_child(); |
| 8137 | else if (cur.next_sibling()) |
| 8138 | cur = cur.next_sibling(); |
| 8139 | else |
| 8140 | { |
| 8141 | while (!cur.next_sibling() && cur != n) |
| 8142 | cur = cur.parent(); |
| 8143 | |
| 8144 | if (cur != n) cur = cur.next_sibling(); |
| 8145 | } |
| 8146 | } |
| 8147 | |
| 8148 | break; |
| 8149 | } |
| 8150 | |
| 8151 | case axis_following_sibling: |
| 8152 | { |
| 8153 | for (xml_node c = n.next_sibling(); c; c = c.next_sibling()) |
| 8154 | step_push(ns, c, alloc); |
| 8155 | |
| 8156 | break; |
| 8157 | } |
| 8158 | |
| 8159 | case axis_preceding_sibling: |
| 8160 | { |
| 8161 | for (xml_node c = n.previous_sibling(); c; c = c.previous_sibling()) |
| 8162 | step_push(ns, c, alloc); |
| 8163 | |
| 8164 | break; |
| 8165 | } |
| 8166 | |
| 8167 | case axis_following: |
| 8168 | { |
| 8169 | xml_node cur = n; |
| 8170 | |
| 8171 | // exit from this node so that we don't include descendants |
| 8172 | while (cur && !cur.next_sibling()) cur = cur.parent(); |
| 8173 | cur = cur.next_sibling(); |
| 8174 | |
| 8175 | for (;;) |
| 8176 | { |
| 8177 | step_push(ns, cur, alloc); |
| 8178 | |
| 8179 | if (cur.first_child()) |
| 8180 | cur = cur.first_child(); |
| 8181 | else if (cur.next_sibling()) |
| 8182 | cur = cur.next_sibling(); |
| 8183 | else |
| 8184 | { |
| 8185 | while (cur && !cur.next_sibling()) cur = cur.parent(); |
| 8186 | cur = cur.next_sibling(); |
| 8187 | |
| 8188 | if (!cur) break; |
| 8189 | } |
| 8190 | } |
| 8191 | |
| 8192 | break; |
| 8193 | } |
| 8194 | |
| 8195 | case axis_preceding: |
| 8196 | { |
| 8197 | xml_node cur = n; |
| 8198 | |
| 8199 | while (cur && !cur.previous_sibling()) cur = cur.parent(); |
| 8200 | cur = cur.previous_sibling(); |
| 8201 | |
| 8202 | for (;;) |
| 8203 | { |
| 8204 | if (cur.last_child()) |
| 8205 | cur = cur.last_child(); |
| 8206 | else |
| 8207 | { |
| 8208 | // leaf node, can't be ancestor |
| 8209 | step_push(ns, cur, alloc); |
| 8210 | |
| 8211 | if (cur.previous_sibling()) |
| 8212 | cur = cur.previous_sibling(); |
| 8213 | else |
| 8214 | { |
| 8215 | do |
| 8216 | { |
| 8217 | cur = cur.parent(); |
| 8218 | if (!cur) break; |
| 8219 | |
| 8220 | if (!node_is_ancestor(cur, n)) step_push(ns, cur, alloc); |
| 8221 | } |
| 8222 | while (!cur.previous_sibling()); |
| 8223 | |
| 8224 | cur = cur.previous_sibling(); |
| 8225 | |
| 8226 | if (!cur) break; |
| 8227 | } |
| 8228 | } |
| 8229 | } |
| 8230 | |
| 8231 | break; |
| 8232 | } |
| 8233 | |
| 8234 | case axis_ancestor: |
| 8235 | case axis_ancestor_or_self: |
| 8236 | { |
| 8237 | if (axis == axis_ancestor_or_self) |
| 8238 | step_push(ns, n, alloc); |
| 8239 | |
| 8240 | xml_node cur = n.parent(); |
| 8241 | |
| 8242 | while (cur) |
| 8243 | { |
| 8244 | step_push(ns, cur, alloc); |
| 8245 | |
| 8246 | cur = cur.parent(); |
| 8247 | } |
| 8248 | |
| 8249 | break; |
| 8250 | } |
| 8251 | |
| 8252 | case axis_self: |
| 8253 | { |
| 8254 | step_push(ns, n, alloc); |
| 8255 | |
| 8256 | break; |
| 8257 | } |
| 8258 | |
| 8259 | case axis_parent: |
| 8260 | { |
| 8261 | if (n.parent()) step_push(ns, n.parent(), alloc); |
| 8262 | |
| 8263 | break; |
| 8264 | } |
| 8265 | |
| 8266 | default: |
| 8267 | assert(!"Unimplemented axis"); |
| 8268 | } |
| 8269 | } |
| 8270 | |
| 8271 | template <class T> void step_fill(xpath_node_set_raw& ns, const xml_attribute& a, const xml_node& p, xpath_allocator* alloc, T v) |
| 8272 | { |
| 8273 | const axis_t axis = T::axis; |
| 8274 | |
| 8275 | switch (axis) |
| 8276 | { |
| 8277 | case axis_ancestor: |
| 8278 | case axis_ancestor_or_self: |
| 8279 | { |
| 8280 | if (axis == axis_ancestor_or_self && _test == nodetest_type_node) // reject attributes based on principal node type test |
| 8281 | step_push(ns, a, p, alloc); |
| 8282 | |
| 8283 | xml_node cur = p; |
| 8284 | |
| 8285 | while (cur) |
| 8286 | { |
| 8287 | step_push(ns, cur, alloc); |
| 8288 | |
| 8289 | cur = cur.parent(); |
| 8290 | } |
| 8291 | |
| 8292 | break; |
| 8293 | } |
| 8294 | |
| 8295 | case axis_descendant_or_self: |
| 8296 | case axis_self: |
| 8297 | { |
| 8298 | if (_test == nodetest_type_node) // reject attributes based on principal node type test |
| 8299 | step_push(ns, a, p, alloc); |
| 8300 | |
| 8301 | break; |
| 8302 | } |
| 8303 | |
| 8304 | case axis_following: |
| 8305 | { |
| 8306 | xml_node cur = p; |
| 8307 | |
| 8308 | for (;;) |
| 8309 | { |
| 8310 | if (cur.first_child()) |
| 8311 | cur = cur.first_child(); |
| 8312 | else if (cur.next_sibling()) |
| 8313 | cur = cur.next_sibling(); |
| 8314 | else |
| 8315 | { |
| 8316 | while (cur && !cur.next_sibling()) cur = cur.parent(); |
| 8317 | cur = cur.next_sibling(); |
| 8318 | |
| 8319 | if (!cur) break; |
| 8320 | } |
| 8321 | |
| 8322 | step_push(ns, cur, alloc); |
| 8323 | } |
| 8324 | |
| 8325 | break; |
| 8326 | } |
| 8327 | |
| 8328 | case axis_parent: |
| 8329 | { |
| 8330 | step_push(ns, p, alloc); |
| 8331 | |
| 8332 | break; |
| 8333 | } |
| 8334 | |
| 8335 | case axis_preceding: |
| 8336 | { |
| 8337 | // preceding:: axis does not include attribute nodes and attribute ancestors (they are the same as parent's ancestors), so we can reuse node preceding |
| 8338 | step_fill(ns, p, alloc, v); |
| 8339 | break; |
| 8340 | } |
| 8341 | |
| 8342 | default: |
| 8343 | assert(!"Unimplemented axis"); |
| 8344 | } |
| 8345 | } |
| 8346 | |
| 8347 | template <class T> xpath_node_set_raw step_do(const xpath_context& c, const xpath_stack& stack, T v) |
| 8348 | { |
| 8349 | const axis_t axis = T::axis; |
| 8350 | bool attributes = (axis == axis_ancestor || axis == axis_ancestor_or_self || axis == axis_descendant_or_self || axis == axis_following || axis == axis_parent || axis == axis_preceding || axis == axis_self); |
| 8351 | |
| 8352 | xpath_node_set_raw ns; |
| 8353 | ns.set_type((axis == axis_ancestor || axis == axis_ancestor_or_self || axis == axis_preceding || axis == axis_preceding_sibling) ? xpath_node_set::type_sorted_reverse : xpath_node_set::type_sorted); |
| 8354 | |
| 8355 | if (_left) |
| 8356 | { |
| 8357 | xpath_node_set_raw s = _left->eval_node_set(c, stack); |
| 8358 | |
| 8359 | // self axis preserves the original order |
| 8360 | if (axis == axis_self) ns.set_type(s.type()); |
| 8361 | |
| 8362 | for (const xpath_node* it = s.begin(); it != s.end(); ++it) |
| 8363 | { |
| 8364 | size_t size = ns.size(); |
| 8365 | |
| 8366 | // in general, all axes generate elements in a particular order, but there is no order guarantee if axis is applied to two nodes |
| 8367 | if (axis != axis_self && size != 0) ns.set_type(xpath_node_set::type_unsorted); |
| 8368 | |
| 8369 | if (it->node()) |
| 8370 | step_fill(ns, it->node(), stack.result, v); |
| 8371 | else if (attributes) |
| 8372 | step_fill(ns, it->attribute(), it->parent(), stack.result, v); |
| 8373 | |
| 8374 | apply_predicates(ns, size, stack); |
| 8375 | } |
| 8376 | } |
| 8377 | else |
| 8378 | { |
| 8379 | if (c.n.node()) |
| 8380 | step_fill(ns, c.n.node(), stack.result, v); |
| 8381 | else if (attributes) |
| 8382 | step_fill(ns, c.n.attribute(), c.n.parent(), stack.result, v); |
| 8383 | |
| 8384 | apply_predicates(ns, 0, stack); |
| 8385 | } |
| 8386 | |
| 8387 | // child, attribute and self axes always generate unique set of nodes |
| 8388 | // for other axis, if the set stayed sorted, it stayed unique because the traversal algorithms do not visit the same node twice |
| 8389 | if (axis != axis_child && axis != axis_attribute && axis != axis_self && ns.type() == xpath_node_set::type_unsorted) |
| 8390 | ns.remove_duplicates(); |
| 8391 | |
| 8392 | return ns; |
| 8393 | } |
| 8394 | |
| 8395 | public: |
| 8396 | xpath_ast_node(ast_type_t type, xpath_value_type rettype_, const char_t* value): |
| 8397 | _type(static_cast<char>(type)), _rettype(static_cast<char>(rettype_)), _axis(0), _test(0), _left(0), _right(0), _next(0) |
| 8398 | { |
| 8399 | assert(type == ast_string_constant); |
| 8400 | _data.string = value; |
| 8401 | } |
| 8402 | |
| 8403 | xpath_ast_node(ast_type_t type, xpath_value_type rettype_, double value): |
| 8404 | _type(static_cast<char>(type)), _rettype(static_cast<char>(rettype_)), _axis(0), _test(0), _left(0), _right(0), _next(0) |
| 8405 | { |
| 8406 | assert(type == ast_number_constant); |
| 8407 | _data.number = value; |
| 8408 | } |
| 8409 | |
| 8410 | xpath_ast_node(ast_type_t type, xpath_value_type rettype_, xpath_variable* value): |
| 8411 | _type(static_cast<char>(type)), _rettype(static_cast<char>(rettype_)), _axis(0), _test(0), _left(0), _right(0), _next(0) |
| 8412 | { |
| 8413 | assert(type == ast_variable); |
| 8414 | _data.variable = value; |
| 8415 | } |
| 8416 | |
| 8417 | xpath_ast_node(ast_type_t type, xpath_value_type rettype_, xpath_ast_node* left = 0, xpath_ast_node* right = 0): |
| 8418 | _type(static_cast<char>(type)), _rettype(static_cast<char>(rettype_)), _axis(0), _test(0), _left(left), _right(right), _next(0) |
| 8419 | { |
| 8420 | } |
| 8421 | |
| 8422 | xpath_ast_node(ast_type_t type, xpath_ast_node* left, axis_t axis, nodetest_t test, const char_t* contents): |
| 8423 | _type(static_cast<char>(type)), _rettype(xpath_type_node_set), _axis(static_cast<char>(axis)), _test(static_cast<char>(test)), _left(left), _right(0), _next(0) |
| 8424 | { |
| 8425 | _data.nodetest = contents; |
| 8426 | } |
| 8427 | |
| 8428 | void set_next(xpath_ast_node* value) |
| 8429 | { |
| 8430 | _next = value; |
| 8431 | } |
| 8432 | |
| 8433 | void set_right(xpath_ast_node* value) |
| 8434 | { |
| 8435 | _right = value; |
| 8436 | } |
| 8437 | |
| 8438 | bool eval_boolean(const xpath_context& c, const xpath_stack& stack) |
| 8439 | { |
| 8440 | switch (_type) |
| 8441 | { |
| 8442 | case ast_op_or: |
| 8443 | return _left->eval_boolean(c, stack) || _right->eval_boolean(c, stack); |
| 8444 | |
| 8445 | case ast_op_and: |
| 8446 | return _left->eval_boolean(c, stack) && _right->eval_boolean(c, stack); |
| 8447 | |
| 8448 | case ast_op_equal: |
| 8449 | return compare_eq(_left, _right, c, stack, equal_to()); |
| 8450 | |
| 8451 | case ast_op_not_equal: |
| 8452 | return compare_eq(_left, _right, c, stack, not_equal_to()); |
| 8453 | |
| 8454 | case ast_op_less: |
| 8455 | return compare_rel(_left, _right, c, stack, less()); |
| 8456 | |
| 8457 | case ast_op_greater: |
| 8458 | return compare_rel(_right, _left, c, stack, less()); |
| 8459 | |
| 8460 | case ast_op_less_or_equal: |
| 8461 | return compare_rel(_left, _right, c, stack, less_equal()); |
| 8462 | |
| 8463 | case ast_op_greater_or_equal: |
| 8464 | return compare_rel(_right, _left, c, stack, less_equal()); |
| 8465 | |
| 8466 | case ast_func_starts_with: |
| 8467 | { |
| 8468 | xpath_allocator_capture cr(stack.result); |
| 8469 | |
| 8470 | xpath_string lr = _left->eval_string(c, stack); |
| 8471 | xpath_string rr = _right->eval_string(c, stack); |
| 8472 | |
| 8473 | return starts_with(lr.c_str(), rr.c_str()); |
| 8474 | } |
| 8475 | |
| 8476 | case ast_func_contains: |
| 8477 | { |
| 8478 | xpath_allocator_capture cr(stack.result); |
| 8479 | |
| 8480 | xpath_string lr = _left->eval_string(c, stack); |
| 8481 | xpath_string rr = _right->eval_string(c, stack); |
| 8482 | |
| 8483 | return find_substring(lr.c_str(), rr.c_str()) != 0; |
| 8484 | } |
| 8485 | |
| 8486 | case ast_func_boolean: |
| 8487 | return _left->eval_boolean(c, stack); |
| 8488 | |
| 8489 | case ast_func_not: |
| 8490 | return !_left->eval_boolean(c, stack); |
| 8491 | |
| 8492 | case ast_func_true: |
| 8493 | return true; |
| 8494 | |
| 8495 | case ast_func_false: |
| 8496 | return false; |
| 8497 | |
| 8498 | case ast_func_lang: |
| 8499 | { |
| 8500 | if (c.n.attribute()) return false; |
| 8501 | |
| 8502 | xpath_allocator_capture cr(stack.result); |
| 8503 | |
| 8504 | xpath_string lang = _left->eval_string(c, stack); |
| 8505 | |
| 8506 | for (xml_node n = c.n.node(); n; n = n.parent()) |
| 8507 | { |
| 8508 | xml_attribute a = n.attribute(PUGIXML_TEXT("xml:lang")); |
| 8509 | |
| 8510 | if (a) |
| 8511 | { |
| 8512 | const char_t* value = a.value(); |
| 8513 | |
| 8514 | // strnicmp / strncasecmp is not portable |
| 8515 | for (const char_t* lit = lang.c_str(); *lit; ++lit) |
| 8516 | { |
| 8517 | if (tolower_ascii(*lit) != tolower_ascii(*value)) return false; |
| 8518 | ++value; |
| 8519 | } |
| 8520 | |
| 8521 | return *value == 0 || *value == '-'; |
| 8522 | } |
| 8523 | } |
| 8524 | |
| 8525 | return false; |
| 8526 | } |
| 8527 | |
| 8528 | case ast_variable: |
| 8529 | { |
| 8530 | assert(_rettype == _data.variable->type()); |
| 8531 | |
| 8532 | if (_rettype == xpath_type_boolean) |
| 8533 | return _data.variable->get_boolean(); |
| 8534 | |
| 8535 | // fallthrough to type conversion |
| 8536 | } |
| 8537 | |
| 8538 | default: |
| 8539 | { |
| 8540 | switch (_rettype) |
| 8541 | { |
| 8542 | case xpath_type_number: |
| 8543 | return convert_number_to_boolean(eval_number(c, stack)); |
| 8544 | |
| 8545 | case xpath_type_string: |
| 8546 | { |
| 8547 | xpath_allocator_capture cr(stack.result); |
| 8548 | |
| 8549 | return !eval_string(c, stack).empty(); |
| 8550 | } |
| 8551 | |
| 8552 | case xpath_type_node_set: |
| 8553 | { |
| 8554 | xpath_allocator_capture cr(stack.result); |
| 8555 | |
| 8556 | return !eval_node_set(c, stack).empty(); |
| 8557 | } |
| 8558 | |
| 8559 | default: |
| 8560 | assert(!"Wrong expression for return type boolean"); |
| 8561 | return false; |
| 8562 | } |
| 8563 | } |
| 8564 | } |
| 8565 | } |
| 8566 | |
| 8567 | double eval_number(const xpath_context& c, const xpath_stack& stack) |
| 8568 | { |
| 8569 | switch (_type) |
| 8570 | { |
| 8571 | case ast_op_add: |
| 8572 | return _left->eval_number(c, stack) + _right->eval_number(c, stack); |
| 8573 | |
| 8574 | case ast_op_subtract: |
| 8575 | return _left->eval_number(c, stack) - _right->eval_number(c, stack); |
| 8576 | |
| 8577 | case ast_op_multiply: |
| 8578 | return _left->eval_number(c, stack) * _right->eval_number(c, stack); |
| 8579 | |
| 8580 | case ast_op_divide: |
| 8581 | return _left->eval_number(c, stack) / _right->eval_number(c, stack); |
| 8582 | |
| 8583 | case ast_op_mod: |
| 8584 | return fmod(_left->eval_number(c, stack), _right->eval_number(c, stack)); |
| 8585 | |
| 8586 | case ast_op_negate: |
| 8587 | return -_left->eval_number(c, stack); |
| 8588 | |
| 8589 | case ast_number_constant: |
| 8590 | return _data.number; |
| 8591 | |
| 8592 | case ast_func_last: |
| 8593 | return static_cast<double>(c.size); |
| 8594 | |
| 8595 | case ast_func_position: |
| 8596 | return static_cast<double>(c.position); |
| 8597 | |
| 8598 | case ast_func_count: |
| 8599 | { |
| 8600 | xpath_allocator_capture cr(stack.result); |
| 8601 | |
| 8602 | return static_cast<double>(_left->eval_node_set(c, stack).size()); |
| 8603 | } |
| 8604 | |
| 8605 | case ast_func_string_length_0: |
| 8606 | { |
| 8607 | xpath_allocator_capture cr(stack.result); |
| 8608 | |
| 8609 | return static_cast<double>(string_value(c.n, stack.result).length()); |
| 8610 | } |
| 8611 | |
| 8612 | case ast_func_string_length_1: |
| 8613 | { |
| 8614 | xpath_allocator_capture cr(stack.result); |
| 8615 | |
| 8616 | return static_cast<double>(_left->eval_string(c, stack).length()); |
| 8617 | } |
| 8618 | |
| 8619 | case ast_func_number_0: |
| 8620 | { |
| 8621 | xpath_allocator_capture cr(stack.result); |
| 8622 | |
| 8623 | return convert_string_to_number(string_value(c.n, stack.result).c_str()); |
| 8624 | } |
| 8625 | |
| 8626 | case ast_func_number_1: |
| 8627 | return _left->eval_number(c, stack); |
| 8628 | |
| 8629 | case ast_func_sum: |
| 8630 | { |
| 8631 | xpath_allocator_capture cr(stack.result); |
| 8632 | |
| 8633 | double r = 0; |
| 8634 | |
| 8635 | xpath_node_set_raw ns = _left->eval_node_set(c, stack); |
| 8636 | |
| 8637 | for (const xpath_node* it = ns.begin(); it != ns.end(); ++it) |
| 8638 | { |
| 8639 | xpath_allocator_capture cri(stack.result); |
| 8640 | |
| 8641 | r += convert_string_to_number(string_value(*it, stack.result).c_str()); |
| 8642 | } |
| 8643 | |
| 8644 | return r; |
| 8645 | } |
| 8646 | |
| 8647 | case ast_func_floor: |
| 8648 | { |
| 8649 | double r = _left->eval_number(c, stack); |
| 8650 | |
| 8651 | return r == r ? floor(r) : r; |
| 8652 | } |
| 8653 | |
| 8654 | case ast_func_ceiling: |
| 8655 | { |
| 8656 | double r = _left->eval_number(c, stack); |
| 8657 | |
| 8658 | return r == r ? ceil(r) : r; |
| 8659 | } |
| 8660 | |
| 8661 | case ast_func_round: |
| 8662 | return round_nearest_nzero(_left->eval_number(c, stack)); |
| 8663 | |
| 8664 | case ast_variable: |
| 8665 | { |
| 8666 | assert(_rettype == _data.variable->type()); |
| 8667 | |
| 8668 | if (_rettype == xpath_type_number) |
| 8669 | return _data.variable->get_number(); |
| 8670 | |
| 8671 | // fallthrough to type conversion |
| 8672 | } |
| 8673 | |
| 8674 | default: |
| 8675 | { |
| 8676 | switch (_rettype) |
| 8677 | { |
| 8678 | case xpath_type_boolean: |
| 8679 | return eval_boolean(c, stack) ? 1 : 0; |
| 8680 | |
| 8681 | case xpath_type_string: |
| 8682 | { |
| 8683 | xpath_allocator_capture cr(stack.result); |
| 8684 | |
| 8685 | return convert_string_to_number(eval_string(c, stack).c_str()); |
| 8686 | } |
| 8687 | |
| 8688 | case xpath_type_node_set: |
| 8689 | { |
| 8690 | xpath_allocator_capture cr(stack.result); |
| 8691 | |
| 8692 | return convert_string_to_number(eval_string(c, stack).c_str()); |
| 8693 | } |
| 8694 | |
| 8695 | default: |
| 8696 | assert(!"Wrong expression for return type number"); |
| 8697 | return 0; |
| 8698 | } |
| 8699 | |
| 8700 | } |
| 8701 | } |
| 8702 | } |
| 8703 | |
| 8704 | xpath_string eval_string_concat(const xpath_context& c, const xpath_stack& stack) |
| 8705 | { |
| 8706 | assert(_type == ast_func_concat); |
| 8707 | |
| 8708 | xpath_allocator_capture ct(stack.temp); |
| 8709 | |
| 8710 | // count the string number |
| 8711 | size_t count = 1; |
| 8712 | for (xpath_ast_node* nc = _right; nc; nc = nc->_next) count++; |
| 8713 | |
| 8714 | // gather all strings |
| 8715 | xpath_string static_buffer[4]; |
| 8716 | xpath_string* buffer = static_buffer; |
| 8717 | |
| 8718 | // allocate on-heap for large concats |
| 8719 | if (count > sizeof(static_buffer) / sizeof(static_buffer[0])) |
| 8720 | { |
| 8721 | buffer = static_cast<xpath_string*>(stack.temp->allocate(count * sizeof(xpath_string))); |
| 8722 | assert(buffer); |
| 8723 | } |
| 8724 | |
| 8725 | // evaluate all strings to temporary stack |
| 8726 | xpath_stack swapped_stack = {stack.temp, stack.result}; |
| 8727 | |
| 8728 | buffer[0] = _left->eval_string(c, swapped_stack); |
| 8729 | |
| 8730 | size_t pos = 1; |
| 8731 | for (xpath_ast_node* n = _right; n; n = n->_next, ++pos) buffer[pos] = n->eval_string(c, swapped_stack); |
| 8732 | assert(pos == count); |
| 8733 | |
| 8734 | // get total length |
| 8735 | size_t length = 0; |
| 8736 | for (size_t i = 0; i < count; ++i) length += buffer[i].length(); |
| 8737 | |
| 8738 | // create final string |
| 8739 | char_t* result = static_cast<char_t*>(stack.result->allocate((length + 1) * sizeof(char_t))); |
| 8740 | assert(result); |
| 8741 | |
| 8742 | char_t* ri = result; |
| 8743 | |
| 8744 | for (size_t j = 0; j < count; ++j) |
| 8745 | for (const char_t* bi = buffer[j].c_str(); *bi; ++bi) |
| 8746 | *ri++ = *bi; |
| 8747 | |
| 8748 | *ri = 0; |
| 8749 | |
| 8750 | return xpath_string(result, true); |
| 8751 | } |
| 8752 | |
| 8753 | xpath_string eval_string(const xpath_context& c, const xpath_stack& stack) |
| 8754 | { |
| 8755 | switch (_type) |
| 8756 | { |
| 8757 | case ast_string_constant: |
| 8758 | return xpath_string_const(_data.string); |
| 8759 | |
| 8760 | case ast_func_local_name_0: |
| 8761 | { |
| 8762 | xpath_node na = c.n; |
| 8763 | |
| 8764 | return xpath_string_const(local_name(na)); |
| 8765 | } |
| 8766 | |
| 8767 | case ast_func_local_name_1: |
| 8768 | { |
| 8769 | xpath_allocator_capture cr(stack.result); |
| 8770 | |
| 8771 | xpath_node_set_raw ns = _left->eval_node_set(c, stack); |
| 8772 | xpath_node na = ns.first(); |
| 8773 | |
| 8774 | return xpath_string_const(local_name(na)); |
| 8775 | } |
| 8776 | |
| 8777 | case ast_func_name_0: |
| 8778 | { |
| 8779 | xpath_node na = c.n; |
| 8780 | |
| 8781 | return xpath_string_const(qualified_name(na)); |
| 8782 | } |
| 8783 | |
| 8784 | case ast_func_name_1: |
| 8785 | { |
| 8786 | xpath_allocator_capture cr(stack.result); |
| 8787 | |
| 8788 | xpath_node_set_raw ns = _left->eval_node_set(c, stack); |
| 8789 | xpath_node na = ns.first(); |
| 8790 | |
| 8791 | return xpath_string_const(qualified_name(na)); |
| 8792 | } |
| 8793 | |
| 8794 | case ast_func_namespace_uri_0: |
| 8795 | { |
| 8796 | xpath_node na = c.n; |
| 8797 | |
| 8798 | return xpath_string_const(namespace_uri(na)); |
| 8799 | } |
| 8800 | |
| 8801 | case ast_func_namespace_uri_1: |
| 8802 | { |
| 8803 | xpath_allocator_capture cr(stack.result); |
| 8804 | |
| 8805 | xpath_node_set_raw ns = _left->eval_node_set(c, stack); |
| 8806 | xpath_node na = ns.first(); |
| 8807 | |
| 8808 | return xpath_string_const(namespace_uri(na)); |
| 8809 | } |
| 8810 | |
| 8811 | case ast_func_string_0: |
| 8812 | return string_value(c.n, stack.result); |
| 8813 | |
| 8814 | case ast_func_string_1: |
| 8815 | return _left->eval_string(c, stack); |
| 8816 | |
| 8817 | case ast_func_concat: |
| 8818 | return eval_string_concat(c, stack); |
| 8819 | |
| 8820 | case ast_func_substring_before: |
| 8821 | { |
| 8822 | xpath_allocator_capture cr(stack.temp); |
| 8823 | |
| 8824 | xpath_stack swapped_stack = {stack.temp, stack.result}; |
| 8825 | |
| 8826 | xpath_string s = _left->eval_string(c, swapped_stack); |
| 8827 | xpath_string p = _right->eval_string(c, swapped_stack); |
| 8828 | |
| 8829 | const char_t* pos = find_substring(s.c_str(), p.c_str()); |
| 8830 | |
| 8831 | return pos ? xpath_string(s.c_str(), pos, stack.result) : xpath_string(); |
| 8832 | } |
| 8833 | |
| 8834 | case ast_func_substring_after: |
| 8835 | { |
| 8836 | xpath_allocator_capture cr(stack.temp); |
| 8837 | |
| 8838 | xpath_stack swapped_stack = {stack.temp, stack.result}; |
| 8839 | |
| 8840 | xpath_string s = _left->eval_string(c, swapped_stack); |
| 8841 | xpath_string p = _right->eval_string(c, swapped_stack); |
| 8842 | |
| 8843 | const char_t* pos = find_substring(s.c_str(), p.c_str()); |
| 8844 | if (!pos) return xpath_string(); |
| 8845 | |
| 8846 | const char_t* result = pos + p.length(); |
| 8847 | |
| 8848 | return s.uses_heap() ? xpath_string(result, stack.result) : xpath_string_const(result); |
| 8849 | } |
| 8850 | |
| 8851 | case ast_func_substring_2: |
| 8852 | { |
| 8853 | xpath_allocator_capture cr(stack.temp); |
| 8854 | |
| 8855 | xpath_stack swapped_stack = {stack.temp, stack.result}; |
| 8856 | |
| 8857 | xpath_string s = _left->eval_string(c, swapped_stack); |
| 8858 | size_t s_length = s.length(); |
| 8859 | |
| 8860 | double first = round_nearest(_right->eval_number(c, stack)); |
| 8861 | |
| 8862 | if (is_nan(first)) return xpath_string(); // NaN |
| 8863 | else if (first >= s_length + 1) return xpath_string(); |
| 8864 | |
| 8865 | size_t pos = first < 1 ? 1 : static_cast<size_t>(first); |
| 8866 | assert(1 <= pos && pos <= s_length + 1); |
| 8867 | |
| 8868 | const char_t* rbegin = s.c_str() + (pos - 1); |
| 8869 | |
| 8870 | return s.uses_heap() ? xpath_string(rbegin, stack.result) : xpath_string_const(rbegin); |
| 8871 | } |
| 8872 | |
| 8873 | case ast_func_substring_3: |
| 8874 | { |
| 8875 | xpath_allocator_capture cr(stack.temp); |
| 8876 | |
| 8877 | xpath_stack swapped_stack = {stack.temp, stack.result}; |
| 8878 | |
| 8879 | xpath_string s = _left->eval_string(c, swapped_stack); |
| 8880 | size_t s_length = s.length(); |
| 8881 | |
| 8882 | double first = round_nearest(_right->eval_number(c, stack)); |
| 8883 | double last = first + round_nearest(_right->_next->eval_number(c, stack)); |
| 8884 | |
| 8885 | if (is_nan(first) || is_nan(last)) return xpath_string(); |
| 8886 | else if (first >= s_length + 1) return xpath_string(); |
| 8887 | else if (first >= last) return xpath_string(); |
| 8888 | else if (last < 1) return xpath_string(); |
| 8889 | |
| 8890 | size_t pos = first < 1 ? 1 : static_cast<size_t>(first); |
| 8891 | size_t end = last >= s_length + 1 ? s_length + 1 : static_cast<size_t>(last); |
| 8892 | |
| 8893 | assert(1 <= pos && pos <= end && end <= s_length + 1); |
| 8894 | const char_t* rbegin = s.c_str() + (pos - 1); |
| 8895 | const char_t* rend = s.c_str() + (end - 1); |
| 8896 | |
| 8897 | return (end == s_length + 1 && !s.uses_heap()) ? xpath_string_const(rbegin) : xpath_string(rbegin, rend, stack.result); |
| 8898 | } |
| 8899 | |
| 8900 | case ast_func_normalize_space_0: |
| 8901 | { |
| 8902 | xpath_string s = string_value(c.n, stack.result); |
| 8903 | |
| 8904 | normalize_space(s.data(stack.result)); |
| 8905 | |
| 8906 | return s; |
| 8907 | } |
| 8908 | |
| 8909 | case ast_func_normalize_space_1: |
| 8910 | { |
| 8911 | xpath_string s = _left->eval_string(c, stack); |
| 8912 | |
| 8913 | normalize_space(s.data(stack.result)); |
| 8914 | |
| 8915 | return s; |
| 8916 | } |
| 8917 | |
| 8918 | case ast_func_translate: |
| 8919 | { |
| 8920 | xpath_allocator_capture cr(stack.temp); |
| 8921 | |
| 8922 | xpath_stack swapped_stack = {stack.temp, stack.result}; |
| 8923 | |
| 8924 | xpath_string s = _left->eval_string(c, stack); |
| 8925 | xpath_string from = _right->eval_string(c, swapped_stack); |
| 8926 | xpath_string to = _right->_next->eval_string(c, swapped_stack); |
| 8927 | |
| 8928 | translate(s.data(stack.result), from.c_str(), to.c_str()); |
| 8929 | |
| 8930 | return s; |
| 8931 | } |
| 8932 | |
| 8933 | case ast_variable: |
| 8934 | { |
| 8935 | assert(_rettype == _data.variable->type()); |
| 8936 | |
| 8937 | if (_rettype == xpath_type_string) |
| 8938 | return xpath_string_const(_data.variable->get_string()); |
| 8939 | |
| 8940 | // fallthrough to type conversion |
| 8941 | } |
| 8942 | |
| 8943 | default: |
| 8944 | { |
| 8945 | switch (_rettype) |
| 8946 | { |
| 8947 | case xpath_type_boolean: |
| 8948 | return xpath_string_const(eval_boolean(c, stack) ? PUGIXML_TEXT("true") : PUGIXML_TEXT("false")); |
| 8949 | |
| 8950 | case xpath_type_number: |
| 8951 | return convert_number_to_string(eval_number(c, stack), stack.result); |
| 8952 | |
| 8953 | case xpath_type_node_set: |
| 8954 | { |
| 8955 | xpath_allocator_capture cr(stack.temp); |
| 8956 | |
| 8957 | xpath_stack swapped_stack = {stack.temp, stack.result}; |
| 8958 | |
| 8959 | xpath_node_set_raw ns = eval_node_set(c, swapped_stack); |
| 8960 | return ns.empty() ? xpath_string() : string_value(ns.first(), stack.result); |
| 8961 | } |
| 8962 | |
| 8963 | default: |
| 8964 | assert(!"Wrong expression for return type string"); |
| 8965 | return xpath_string(); |
| 8966 | } |
| 8967 | } |
| 8968 | } |
| 8969 | } |
| 8970 | |
| 8971 | xpath_node_set_raw eval_node_set(const xpath_context& c, const xpath_stack& stack) |
| 8972 | { |
| 8973 | switch (_type) |
| 8974 | { |
| 8975 | case ast_op_union: |
| 8976 | { |
| 8977 | xpath_allocator_capture cr(stack.temp); |
| 8978 | |
| 8979 | xpath_stack swapped_stack = {stack.temp, stack.result}; |
| 8980 | |
| 8981 | xpath_node_set_raw ls = _left->eval_node_set(c, swapped_stack); |
| 8982 | xpath_node_set_raw rs = _right->eval_node_set(c, stack); |
| 8983 | |
| 8984 | // we can optimize merging two sorted sets, but this is a very rare operation, so don't bother |
| 8985 | rs.set_type(xpath_node_set::type_unsorted); |
| 8986 | |
| 8987 | rs.append(ls.begin(), ls.end(), stack.result); |
| 8988 | rs.remove_duplicates(); |
| 8989 | |
| 8990 | return rs; |
| 8991 | } |
| 8992 | |
| 8993 | case ast_filter: |
| 8994 | case ast_filter_posinv: |
| 8995 | { |
| 8996 | xpath_node_set_raw set = _left->eval_node_set(c, stack); |
| 8997 | |
| 8998 | // either expression is a number or it contains position() call; sort by document order |
| 8999 | if (_type == ast_filter) set.sort_do(); |
| 9000 | |
| 9001 | apply_predicate(set, 0, _right, stack); |
| 9002 | |
| 9003 | return set; |
| 9004 | } |
| 9005 | |
| 9006 | case ast_func_id: |
| 9007 | return xpath_node_set_raw(); |
| 9008 | |
| 9009 | case ast_step: |
| 9010 | { |
| 9011 | switch (_axis) |
| 9012 | { |
| 9013 | case axis_ancestor: |
| 9014 | return step_do(c, stack, axis_to_type<axis_ancestor>()); |
| 9015 | |
| 9016 | case axis_ancestor_or_self: |
| 9017 | return step_do(c, stack, axis_to_type<axis_ancestor_or_self>()); |
| 9018 | |
| 9019 | case axis_attribute: |
| 9020 | return step_do(c, stack, axis_to_type<axis_attribute>()); |
| 9021 | |
| 9022 | case axis_child: |
| 9023 | return step_do(c, stack, axis_to_type<axis_child>()); |
| 9024 | |
| 9025 | case axis_descendant: |
| 9026 | return step_do(c, stack, axis_to_type<axis_descendant>()); |
| 9027 | |
| 9028 | case axis_descendant_or_self: |
| 9029 | return step_do(c, stack, axis_to_type<axis_descendant_or_self>()); |
| 9030 | |
| 9031 | case axis_following: |
| 9032 | return step_do(c, stack, axis_to_type<axis_following>()); |
| 9033 | |
| 9034 | case axis_following_sibling: |
| 9035 | return step_do(c, stack, axis_to_type<axis_following_sibling>()); |
| 9036 | |
| 9037 | case axis_namespace: |
| 9038 | // namespaced axis is not supported |
| 9039 | return xpath_node_set_raw(); |
| 9040 | |
| 9041 | case axis_parent: |
| 9042 | return step_do(c, stack, axis_to_type<axis_parent>()); |
| 9043 | |
| 9044 | case axis_preceding: |
| 9045 | return step_do(c, stack, axis_to_type<axis_preceding>()); |
| 9046 | |
| 9047 | case axis_preceding_sibling: |
| 9048 | return step_do(c, stack, axis_to_type<axis_preceding_sibling>()); |
| 9049 | |
| 9050 | case axis_self: |
| 9051 | return step_do(c, stack, axis_to_type<axis_self>()); |
| 9052 | |
| 9053 | default: |
| 9054 | assert(!"Unknown axis"); |
| 9055 | return xpath_node_set_raw(); |
| 9056 | } |
| 9057 | } |
| 9058 | |
| 9059 | case ast_step_root: |
| 9060 | { |
| 9061 | assert(!_right); // root step can't have any predicates |
| 9062 | |
| 9063 | xpath_node_set_raw ns; |
| 9064 | |
| 9065 | ns.set_type(xpath_node_set::type_sorted); |
| 9066 | |
| 9067 | if (c.n.node()) ns.push_back(c.n.node().root(), stack.result); |
| 9068 | else if (c.n.attribute()) ns.push_back(c.n.parent().root(), stack.result); |
| 9069 | |
| 9070 | return ns; |
| 9071 | } |
| 9072 | |
| 9073 | case ast_variable: |
| 9074 | { |
| 9075 | assert(_rettype == _data.variable->type()); |
| 9076 | |
| 9077 | if (_rettype == xpath_type_node_set) |
| 9078 | { |
| 9079 | const xpath_node_set& s = _data.variable->get_node_set(); |
| 9080 | |
| 9081 | xpath_node_set_raw ns; |
| 9082 | |
| 9083 | ns.set_type(s.type()); |
| 9084 | ns.append(s.begin(), s.end(), stack.result); |
| 9085 | |
| 9086 | return ns; |
| 9087 | } |
| 9088 | |
| 9089 | // fallthrough to type conversion |
| 9090 | } |
| 9091 | |
| 9092 | default: |
| 9093 | assert(!"Wrong expression for return type node set"); |
| 9094 | return xpath_node_set_raw(); |
| 9095 | } |
| 9096 | } |
| 9097 | |
| 9098 | bool is_posinv() |
| 9099 | { |
| 9100 | switch (_type) |
| 9101 | { |
| 9102 | case ast_func_position: |
| 9103 | return false; |
| 9104 | |
| 9105 | case ast_string_constant: |
| 9106 | case ast_number_constant: |
| 9107 | case ast_variable: |
| 9108 | return true; |
| 9109 | |
| 9110 | case ast_step: |
| 9111 | case ast_step_root: |
| 9112 | return true; |
| 9113 | |
| 9114 | case ast_predicate: |
| 9115 | case ast_filter: |
| 9116 | case ast_filter_posinv: |
| 9117 | return true; |
| 9118 | |
| 9119 | default: |
| 9120 | if (_left && !_left->is_posinv()) return false; |
| 9121 | |
| 9122 | for (xpath_ast_node* n = _right; n; n = n->_next) |
| 9123 | if (!n->is_posinv()) return false; |
| 9124 | |
| 9125 | return true; |
| 9126 | } |
| 9127 | } |
| 9128 | |
| 9129 | xpath_value_type rettype() const |
| 9130 | { |
| 9131 | return static_cast<xpath_value_type>(_rettype); |
| 9132 | } |
| 9133 | }; |
| 9134 | |
| 9135 | struct xpath_parser |
| 9136 | { |
| 9137 | xpath_allocator* _alloc; |
| 9138 | xpath_lexer _lexer; |
| 9139 | |
| 9140 | const char_t* _query; |
| 9141 | xpath_variable_set* _variables; |
| 9142 | |
| 9143 | xpath_parse_result* _result; |
| 9144 | |
| 9145 | char_t _scratch[32]; |
| 9146 | |
| 9147 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 9148 | jmp_buf _error_handler; |
| 9149 | #endif |
| 9150 | |
| 9151 | void throw_error(const char* message) |
| 9152 | { |
| 9153 | _result->error = message; |
| 9154 | _result->offset = _lexer.current_pos() - _query; |
| 9155 | |
| 9156 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 9157 | longjmp(_error_handler, 1); |
| 9158 | #else |
| 9159 | throw xpath_exception(*_result); |
| 9160 | #endif |
| 9161 | } |
| 9162 | |
| 9163 | void throw_error_oom() |
| 9164 | { |
| 9165 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 9166 | throw_error("Out of memory"); |
| 9167 | #else |
| 9168 | throw std::bad_alloc(); |
| 9169 | #endif |
| 9170 | } |
| 9171 | |
| 9172 | void* alloc_node() |
| 9173 | { |
| 9174 | void* result = _alloc->allocate_nothrow(sizeof(xpath_ast_node)); |
| 9175 | |
| 9176 | if (!result) throw_error_oom(); |
| 9177 | |
| 9178 | return result; |
| 9179 | } |
| 9180 | |
| 9181 | const char_t* alloc_string(const xpath_lexer_string& value) |
| 9182 | { |
| 9183 | if (value.begin) |
| 9184 | { |
| 9185 | size_t length = static_cast<size_t>(value.end - value.begin); |
| 9186 | |
| 9187 | char_t* c = static_cast<char_t*>(_alloc->allocate_nothrow((length + 1) * sizeof(char_t))); |
| 9188 | if (!c) throw_error_oom(); |
| 9189 | assert(c); // workaround for clang static analysis |
| 9190 | |
| 9191 | memcpy(c, value.begin, length * sizeof(char_t)); |
| 9192 | c[length] = 0; |
| 9193 | |
| 9194 | return c; |
| 9195 | } |
| 9196 | else return 0; |
| 9197 | } |
| 9198 | |
| 9199 | xpath_ast_node* parse_function_helper(ast_type_t type0, ast_type_t type1, size_t argc, xpath_ast_node* args[2]) |
| 9200 | { |
| 9201 | assert(argc <= 1); |
| 9202 | |
| 9203 | if (argc == 1 && args[0]->rettype() != xpath_type_node_set) throw_error("Function has to be applied to node set"); |
| 9204 | |
| 9205 | return new (alloc_node()) xpath_ast_node(argc == 0 ? type0 : type1, xpath_type_string, args[0]); |
| 9206 | } |
| 9207 | |
| 9208 | xpath_ast_node* parse_function(const xpath_lexer_string& name, size_t argc, xpath_ast_node* args[2]) |
| 9209 | { |
| 9210 | switch (name.begin[0]) |
| 9211 | { |
| 9212 | case 'b': |
| 9213 | if (name == PUGIXML_TEXT("boolean") && argc == 1) |
| 9214 | return new (alloc_node()) xpath_ast_node(ast_func_boolean, xpath_type_boolean, args[0]); |
| 9215 | |
| 9216 | break; |
| 9217 | |
| 9218 | case 'c': |
| 9219 | if (name == PUGIXML_TEXT("count") && argc == 1) |
| 9220 | { |
| 9221 | if (args[0]->rettype() != xpath_type_node_set) throw_error("Function has to be applied to node set"); |
| 9222 | return new (alloc_node()) xpath_ast_node(ast_func_count, xpath_type_number, args[0]); |
| 9223 | } |
| 9224 | else if (name == PUGIXML_TEXT("contains") && argc == 2) |
| 9225 | return new (alloc_node()) xpath_ast_node(ast_func_contains, xpath_type_boolean, args[0], args[1]); |
| 9226 | else if (name == PUGIXML_TEXT("concat") && argc >= 2) |
| 9227 | return new (alloc_node()) xpath_ast_node(ast_func_concat, xpath_type_string, args[0], args[1]); |
| 9228 | else if (name == PUGIXML_TEXT("ceiling") && argc == 1) |
| 9229 | return new (alloc_node()) xpath_ast_node(ast_func_ceiling, xpath_type_number, args[0]); |
| 9230 | |
| 9231 | break; |
| 9232 | |
| 9233 | case 'f': |
| 9234 | if (name == PUGIXML_TEXT("false") && argc == 0) |
| 9235 | return new (alloc_node()) xpath_ast_node(ast_func_false, xpath_type_boolean); |
| 9236 | else if (name == PUGIXML_TEXT("floor") && argc == 1) |
| 9237 | return new (alloc_node()) xpath_ast_node(ast_func_floor, xpath_type_number, args[0]); |
| 9238 | |
| 9239 | break; |
| 9240 | |
| 9241 | case 'i': |
| 9242 | if (name == PUGIXML_TEXT("id") && argc == 1) |
| 9243 | return new (alloc_node()) xpath_ast_node(ast_func_id, xpath_type_node_set, args[0]); |
| 9244 | |
| 9245 | break; |
| 9246 | |
| 9247 | case 'l': |
| 9248 | if (name == PUGIXML_TEXT("last") && argc == 0) |
| 9249 | return new (alloc_node()) xpath_ast_node(ast_func_last, xpath_type_number); |
| 9250 | else if (name == PUGIXML_TEXT("lang") && argc == 1) |
| 9251 | return new (alloc_node()) xpath_ast_node(ast_func_lang, xpath_type_boolean, args[0]); |
| 9252 | else if (name == PUGIXML_TEXT("local-name") && argc <= 1) |
| 9253 | return parse_function_helper(ast_func_local_name_0, ast_func_local_name_1, argc, args); |
| 9254 | |
| 9255 | break; |
| 9256 | |
| 9257 | case 'n': |
| 9258 | if (name == PUGIXML_TEXT("name") && argc <= 1) |
| 9259 | return parse_function_helper(ast_func_name_0, ast_func_name_1, argc, args); |
| 9260 | else if (name == PUGIXML_TEXT("namespace-uri") && argc <= 1) |
| 9261 | return parse_function_helper(ast_func_namespace_uri_0, ast_func_namespace_uri_1, argc, args); |
| 9262 | else if (name == PUGIXML_TEXT("normalize-space") && argc <= 1) |
| 9263 | return new (alloc_node()) xpath_ast_node(argc == 0 ? ast_func_normalize_space_0 : ast_func_normalize_space_1, xpath_type_string, args[0], args[1]); |
| 9264 | else if (name == PUGIXML_TEXT("not") && argc == 1) |
| 9265 | return new (alloc_node()) xpath_ast_node(ast_func_not, xpath_type_boolean, args[0]); |
| 9266 | else if (name == PUGIXML_TEXT("number") && argc <= 1) |
| 9267 | return new (alloc_node()) xpath_ast_node(argc == 0 ? ast_func_number_0 : ast_func_number_1, xpath_type_number, args[0]); |
| 9268 | |
| 9269 | break; |
| 9270 | |
| 9271 | case 'p': |
| 9272 | if (name == PUGIXML_TEXT("position") && argc == 0) |
| 9273 | return new (alloc_node()) xpath_ast_node(ast_func_position, xpath_type_number); |
| 9274 | |
| 9275 | break; |
| 9276 | |
| 9277 | case 'r': |
| 9278 | if (name == PUGIXML_TEXT("round") && argc == 1) |
| 9279 | return new (alloc_node()) xpath_ast_node(ast_func_round, xpath_type_number, args[0]); |
| 9280 | |
| 9281 | break; |
| 9282 | |
| 9283 | case 's': |
| 9284 | if (name == PUGIXML_TEXT("string") && argc <= 1) |
| 9285 | return new (alloc_node()) xpath_ast_node(argc == 0 ? ast_func_string_0 : ast_func_string_1, xpath_type_string, args[0]); |
| 9286 | else if (name == PUGIXML_TEXT("string-length") && argc <= 1) |
| 9287 | return new (alloc_node()) xpath_ast_node(argc == 0 ? ast_func_string_length_0 : ast_func_string_length_1, xpath_type_number, args[0]); |
| 9288 | else if (name == PUGIXML_TEXT("starts-with") && argc == 2) |
| 9289 | return new (alloc_node()) xpath_ast_node(ast_func_starts_with, xpath_type_boolean, args[0], args[1]); |
| 9290 | else if (name == PUGIXML_TEXT("substring-before") && argc == 2) |
| 9291 | return new (alloc_node()) xpath_ast_node(ast_func_substring_before, xpath_type_string, args[0], args[1]); |
| 9292 | else if (name == PUGIXML_TEXT("substring-after") && argc == 2) |
| 9293 | return new (alloc_node()) xpath_ast_node(ast_func_substring_after, xpath_type_string, args[0], args[1]); |
| 9294 | else if (name == PUGIXML_TEXT("substring") && (argc == 2 || argc == 3)) |
| 9295 | return new (alloc_node()) xpath_ast_node(argc == 2 ? ast_func_substring_2 : ast_func_substring_3, xpath_type_string, args[0], args[1]); |
| 9296 | else if (name == PUGIXML_TEXT("sum") && argc == 1) |
| 9297 | { |
| 9298 | if (args[0]->rettype() != xpath_type_node_set) throw_error("Function has to be applied to node set"); |
| 9299 | return new (alloc_node()) xpath_ast_node(ast_func_sum, xpath_type_number, args[0]); |
| 9300 | } |
| 9301 | |
| 9302 | break; |
| 9303 | |
| 9304 | case 't': |
| 9305 | if (name == PUGIXML_TEXT("translate") && argc == 3) |
| 9306 | return new (alloc_node()) xpath_ast_node(ast_func_translate, xpath_type_string, args[0], args[1]); |
| 9307 | else if (name == PUGIXML_TEXT("true") && argc == 0) |
| 9308 | return new (alloc_node()) xpath_ast_node(ast_func_true, xpath_type_boolean); |
| 9309 | |
| 9310 | break; |
| 9311 | |
| 9312 | default: |
| 9313 | break; |
| 9314 | } |
| 9315 | |
| 9316 | throw_error("Unrecognized function or wrong parameter count"); |
| 9317 | |
| 9318 | return 0; |
| 9319 | } |
| 9320 | |
| 9321 | axis_t parse_axis_name(const xpath_lexer_string& name, bool& specified) |
| 9322 | { |
| 9323 | specified = true; |
| 9324 | |
| 9325 | switch (name.begin[0]) |
| 9326 | { |
| 9327 | case 'a': |
| 9328 | if (name == PUGIXML_TEXT("ancestor")) |
| 9329 | return axis_ancestor; |
| 9330 | else if (name == PUGIXML_TEXT("ancestor-or-self")) |
| 9331 | return axis_ancestor_or_self; |
| 9332 | else if (name == PUGIXML_TEXT("attribute")) |
| 9333 | return axis_attribute; |
| 9334 | |
| 9335 | break; |
| 9336 | |
| 9337 | case 'c': |
| 9338 | if (name == PUGIXML_TEXT("child")) |
| 9339 | return axis_child; |
| 9340 | |
| 9341 | break; |
| 9342 | |
| 9343 | case 'd': |
| 9344 | if (name == PUGIXML_TEXT("descendant")) |
| 9345 | return axis_descendant; |
| 9346 | else if (name == PUGIXML_TEXT("descendant-or-self")) |
| 9347 | return axis_descendant_or_self; |
| 9348 | |
| 9349 | break; |
| 9350 | |
| 9351 | case 'f': |
| 9352 | if (name == PUGIXML_TEXT("following")) |
| 9353 | return axis_following; |
| 9354 | else if (name == PUGIXML_TEXT("following-sibling")) |
| 9355 | return axis_following_sibling; |
| 9356 | |
| 9357 | break; |
| 9358 | |
| 9359 | case 'n': |
| 9360 | if (name == PUGIXML_TEXT("namespace")) |
| 9361 | return axis_namespace; |
| 9362 | |
| 9363 | break; |
| 9364 | |
| 9365 | case 'p': |
| 9366 | if (name == PUGIXML_TEXT("parent")) |
| 9367 | return axis_parent; |
| 9368 | else if (name == PUGIXML_TEXT("preceding")) |
| 9369 | return axis_preceding; |
| 9370 | else if (name == PUGIXML_TEXT("preceding-sibling")) |
| 9371 | return axis_preceding_sibling; |
| 9372 | |
| 9373 | break; |
| 9374 | |
| 9375 | case 's': |
| 9376 | if (name == PUGIXML_TEXT("self")) |
| 9377 | return axis_self; |
| 9378 | |
| 9379 | break; |
| 9380 | |
| 9381 | default: |
| 9382 | break; |
| 9383 | } |
| 9384 | |
| 9385 | specified = false; |
| 9386 | return axis_child; |
| 9387 | } |
| 9388 | |
| 9389 | nodetest_t parse_node_test_type(const xpath_lexer_string& name) |
| 9390 | { |
| 9391 | switch (name.begin[0]) |
| 9392 | { |
| 9393 | case 'c': |
| 9394 | if (name == PUGIXML_TEXT("comment")) |
| 9395 | return nodetest_type_comment; |
| 9396 | |
| 9397 | break; |
| 9398 | |
| 9399 | case 'n': |
| 9400 | if (name == PUGIXML_TEXT("node")) |
| 9401 | return nodetest_type_node; |
| 9402 | |
| 9403 | break; |
| 9404 | |
| 9405 | case 'p': |
| 9406 | if (name == PUGIXML_TEXT("processing-instruction")) |
| 9407 | return nodetest_type_pi; |
| 9408 | |
| 9409 | break; |
| 9410 | |
| 9411 | case 't': |
| 9412 | if (name == PUGIXML_TEXT("text")) |
| 9413 | return nodetest_type_text; |
| 9414 | |
| 9415 | break; |
| 9416 | |
| 9417 | default: |
| 9418 | break; |
| 9419 | } |
| 9420 | |
| 9421 | return nodetest_none; |
| 9422 | } |
| 9423 | |
| 9424 | // PrimaryExpr ::= VariableReference | '(' Expr ')' | Literal | Number | FunctionCall |
| 9425 | xpath_ast_node* parse_primary_expression() |
| 9426 | { |
| 9427 | switch (_lexer.current()) |
| 9428 | { |
| 9429 | case lex_var_ref: |
| 9430 | { |
| 9431 | xpath_lexer_string name = _lexer.contents(); |
| 9432 | |
| 9433 | if (!_variables) |
| 9434 | throw_error("Unknown variable: variable set is not provided"); |
| 9435 | |
| 9436 | xpath_variable* var = get_variable_scratch(_scratch, _variables, name.begin, name.end); |
| 9437 | |
| 9438 | if (!var) |
| 9439 | throw_error("Unknown variable: variable set does not contain the given name"); |
| 9440 | |
| 9441 | _lexer.next(); |
| 9442 | |
| 9443 | return new (alloc_node()) xpath_ast_node(ast_variable, var->type(), var); |
| 9444 | } |
| 9445 | |
| 9446 | case lex_open_brace: |
| 9447 | { |
| 9448 | _lexer.next(); |
| 9449 | |
| 9450 | xpath_ast_node* n = parse_expression(); |
| 9451 | |
| 9452 | if (_lexer.current() != lex_close_brace) |
| 9453 | throw_error("Unmatched braces"); |
| 9454 | |
| 9455 | _lexer.next(); |
| 9456 | |
| 9457 | return n; |
| 9458 | } |
| 9459 | |
| 9460 | case lex_quoted_string: |
| 9461 | { |
| 9462 | const char_t* value = alloc_string(_lexer.contents()); |
| 9463 | |
| 9464 | xpath_ast_node* n = new (alloc_node()) xpath_ast_node(ast_string_constant, xpath_type_string, value); |
| 9465 | _lexer.next(); |
| 9466 | |
| 9467 | return n; |
| 9468 | } |
| 9469 | |
| 9470 | case lex_number: |
| 9471 | { |
| 9472 | double value = 0; |
| 9473 | |
| 9474 | if (!convert_string_to_number_scratch(_scratch, _lexer.contents().begin, _lexer.contents().end, &value)) |
| 9475 | throw_error_oom(); |
| 9476 | |
| 9477 | xpath_ast_node* n = new (alloc_node()) xpath_ast_node(ast_number_constant, xpath_type_number, value); |
| 9478 | _lexer.next(); |
| 9479 | |
| 9480 | return n; |
| 9481 | } |
| 9482 | |
| 9483 | case lex_string: |
| 9484 | { |
| 9485 | xpath_ast_node* args[2] = {0}; |
| 9486 | size_t argc = 0; |
| 9487 | |
| 9488 | xpath_lexer_string function = _lexer.contents(); |
| 9489 | _lexer.next(); |
| 9490 | |
| 9491 | xpath_ast_node* last_arg = 0; |
| 9492 | |
| 9493 | if (_lexer.current() != lex_open_brace) |
| 9494 | throw_error("Unrecognized function call"); |
| 9495 | _lexer.next(); |
| 9496 | |
| 9497 | if (_lexer.current() != lex_close_brace) |
| 9498 | args[argc++] = parse_expression(); |
| 9499 | |
| 9500 | while (_lexer.current() != lex_close_brace) |
| 9501 | { |
| 9502 | if (_lexer.current() != lex_comma) |
| 9503 | throw_error("No comma between function arguments"); |
| 9504 | _lexer.next(); |
| 9505 | |
| 9506 | xpath_ast_node* n = parse_expression(); |
| 9507 | |
| 9508 | if (argc < 2) args[argc] = n; |
| 9509 | else last_arg->set_next(n); |
| 9510 | |
| 9511 | argc++; |
| 9512 | last_arg = n; |
| 9513 | } |
| 9514 | |
| 9515 | _lexer.next(); |
| 9516 | |
| 9517 | return parse_function(function, argc, args); |
| 9518 | } |
| 9519 | |
| 9520 | default: |
| 9521 | throw_error("Unrecognizable primary expression"); |
| 9522 | |
| 9523 | return 0; |
| 9524 | } |
| 9525 | } |
| 9526 | |
| 9527 | // FilterExpr ::= PrimaryExpr | FilterExpr Predicate |
| 9528 | // Predicate ::= '[' PredicateExpr ']' |
| 9529 | // PredicateExpr ::= Expr |
| 9530 | xpath_ast_node* parse_filter_expression() |
| 9531 | { |
| 9532 | xpath_ast_node* n = parse_primary_expression(); |
| 9533 | |
| 9534 | while (_lexer.current() == lex_open_square_brace) |
| 9535 | { |
| 9536 | _lexer.next(); |
| 9537 | |
| 9538 | xpath_ast_node* expr = parse_expression(); |
| 9539 | |
| 9540 | if (n->rettype() != xpath_type_node_set) throw_error("Predicate has to be applied to node set"); |
| 9541 | |
| 9542 | bool posinv = expr->rettype() != xpath_type_number && expr->is_posinv(); |
| 9543 | |
| 9544 | n = new (alloc_node()) xpath_ast_node(posinv ? ast_filter_posinv : ast_filter, xpath_type_node_set, n, expr); |
| 9545 | |
| 9546 | if (_lexer.current() != lex_close_square_brace) |
| 9547 | throw_error("Unmatched square brace"); |
| 9548 | |
| 9549 | _lexer.next(); |
| 9550 | } |
| 9551 | |
| 9552 | return n; |
| 9553 | } |
| 9554 | |
| 9555 | // Step ::= AxisSpecifier NodeTest Predicate* | AbbreviatedStep |
| 9556 | // AxisSpecifier ::= AxisName '::' | '@'? |
| 9557 | // NodeTest ::= NameTest | NodeType '(' ')' | 'processing-instruction' '(' Literal ')' |
| 9558 | // NameTest ::= '*' | NCName ':' '*' | QName |
| 9559 | // AbbreviatedStep ::= '.' | '..' |
| 9560 | xpath_ast_node* parse_step(xpath_ast_node* set) |
| 9561 | { |
| 9562 | if (set && set->rettype() != xpath_type_node_set) |
| 9563 | throw_error("Step has to be applied to node set"); |
| 9564 | |
| 9565 | bool axis_specified = false; |
| 9566 | axis_t axis = axis_child; // implied child axis |
| 9567 | |
| 9568 | if (_lexer.current() == lex_axis_attribute) |
| 9569 | { |
| 9570 | axis = axis_attribute; |
| 9571 | axis_specified = true; |
| 9572 | |
| 9573 | _lexer.next(); |
| 9574 | } |
| 9575 | else if (_lexer.current() == lex_dot) |
| 9576 | { |
| 9577 | _lexer.next(); |
| 9578 | |
| 9579 | return new (alloc_node()) xpath_ast_node(ast_step, set, axis_self, nodetest_type_node, 0); |
| 9580 | } |
| 9581 | else if (_lexer.current() == lex_double_dot) |
| 9582 | { |
| 9583 | _lexer.next(); |
| 9584 | |
| 9585 | return new (alloc_node()) xpath_ast_node(ast_step, set, axis_parent, nodetest_type_node, 0); |
| 9586 | } |
| 9587 | |
| 9588 | nodetest_t nt_type = nodetest_none; |
| 9589 | xpath_lexer_string nt_name; |
| 9590 | |
| 9591 | if (_lexer.current() == lex_string) |
| 9592 | { |
| 9593 | // node name test |
| 9594 | nt_name = _lexer.contents(); |
| 9595 | _lexer.next(); |
| 9596 | |
| 9597 | // was it an axis name? |
| 9598 | if (_lexer.current() == lex_double_colon) |
| 9599 | { |
| 9600 | // parse axis name |
| 9601 | if (axis_specified) throw_error("Two axis specifiers in one step"); |
| 9602 | |
| 9603 | axis = parse_axis_name(nt_name, axis_specified); |
| 9604 | |
| 9605 | if (!axis_specified) throw_error("Unknown axis"); |
| 9606 | |
| 9607 | // read actual node test |
| 9608 | _lexer.next(); |
| 9609 | |
| 9610 | if (_lexer.current() == lex_multiply) |
| 9611 | { |
| 9612 | nt_type = nodetest_all; |
| 9613 | nt_name = xpath_lexer_string(); |
| 9614 | _lexer.next(); |
| 9615 | } |
| 9616 | else if (_lexer.current() == lex_string) |
| 9617 | { |
| 9618 | nt_name = _lexer.contents(); |
| 9619 | _lexer.next(); |
| 9620 | } |
| 9621 | else throw_error("Unrecognized node test"); |
| 9622 | } |
| 9623 | |
| 9624 | if (nt_type == nodetest_none) |
| 9625 | { |
| 9626 | // node type test or processing-instruction |
| 9627 | if (_lexer.current() == lex_open_brace) |
| 9628 | { |
| 9629 | _lexer.next(); |
| 9630 | |
| 9631 | if (_lexer.current() == lex_close_brace) |
| 9632 | { |
| 9633 | _lexer.next(); |
| 9634 | |
| 9635 | nt_type = parse_node_test_type(nt_name); |
| 9636 | |
| 9637 | if (nt_type == nodetest_none) throw_error("Unrecognized node type"); |
| 9638 | |
| 9639 | nt_name = xpath_lexer_string(); |
| 9640 | } |
| 9641 | else if (nt_name == PUGIXML_TEXT("processing-instruction")) |
| 9642 | { |
| 9643 | if (_lexer.current() != lex_quoted_string) |
| 9644 | throw_error("Only literals are allowed as arguments to processing-instruction()"); |
| 9645 | |
| 9646 | nt_type = nodetest_pi; |
| 9647 | nt_name = _lexer.contents(); |
| 9648 | _lexer.next(); |
| 9649 | |
| 9650 | if (_lexer.current() != lex_close_brace) |
| 9651 | throw_error("Unmatched brace near processing-instruction()"); |
| 9652 | _lexer.next(); |
| 9653 | } |
| 9654 | else |
| 9655 | throw_error("Unmatched brace near node type test"); |
| 9656 | |
| 9657 | } |
| 9658 | // QName or NCName:* |
| 9659 | else |
| 9660 | { |
| 9661 | if (nt_name.end - nt_name.begin > 2 && nt_name.end[-2] == ':' && nt_name.end[-1] == '*') // NCName:* |
| 9662 | { |
| 9663 | nt_name.end--; // erase * |
| 9664 | |
| 9665 | nt_type = nodetest_all_in_namespace; |
| 9666 | } |
| 9667 | else nt_type = nodetest_name; |
| 9668 | } |
| 9669 | } |
| 9670 | } |
| 9671 | else if (_lexer.current() == lex_multiply) |
| 9672 | { |
| 9673 | nt_type = nodetest_all; |
| 9674 | _lexer.next(); |
| 9675 | } |
| 9676 | else throw_error("Unrecognized node test"); |
| 9677 | |
| 9678 | xpath_ast_node* n = new (alloc_node()) xpath_ast_node(ast_step, set, axis, nt_type, alloc_string(nt_name)); |
| 9679 | |
| 9680 | xpath_ast_node* last = 0; |
| 9681 | |
| 9682 | while (_lexer.current() == lex_open_square_brace) |
| 9683 | { |
| 9684 | _lexer.next(); |
| 9685 | |
| 9686 | xpath_ast_node* expr = parse_expression(); |
| 9687 | |
| 9688 | xpath_ast_node* pred = new (alloc_node()) xpath_ast_node(ast_predicate, xpath_type_node_set, expr); |
| 9689 | |
| 9690 | if (_lexer.current() != lex_close_square_brace) |
| 9691 | throw_error("Unmatched square brace"); |
| 9692 | _lexer.next(); |
| 9693 | |
| 9694 | if (last) last->set_next(pred); |
| 9695 | else n->set_right(pred); |
| 9696 | |
| 9697 | last = pred; |
| 9698 | } |
| 9699 | |
| 9700 | return n; |
| 9701 | } |
| 9702 | |
| 9703 | // RelativeLocationPath ::= Step | RelativeLocationPath '/' Step | RelativeLocationPath '//' Step |
| 9704 | xpath_ast_node* parse_relative_location_path(xpath_ast_node* set) |
| 9705 | { |
| 9706 | xpath_ast_node* n = parse_step(set); |
| 9707 | |
| 9708 | while (_lexer.current() == lex_slash || _lexer.current() == lex_double_slash) |
| 9709 | { |
| 9710 | lexeme_t l = _lexer.current(); |
| 9711 | _lexer.next(); |
| 9712 | |
| 9713 | if (l == lex_double_slash) |
| 9714 | n = new (alloc_node()) xpath_ast_node(ast_step, n, axis_descendant_or_self, nodetest_type_node, 0); |
| 9715 | |
| 9716 | n = parse_step(n); |
| 9717 | } |
| 9718 | |
| 9719 | return n; |
| 9720 | } |
| 9721 | |
| 9722 | // LocationPath ::= RelativeLocationPath | AbsoluteLocationPath |
| 9723 | // AbsoluteLocationPath ::= '/' RelativeLocationPath? | '//' RelativeLocationPath |
| 9724 | xpath_ast_node* parse_location_path() |
| 9725 | { |
| 9726 | if (_lexer.current() == lex_slash) |
| 9727 | { |
| 9728 | _lexer.next(); |
| 9729 | |
| 9730 | xpath_ast_node* n = new (alloc_node()) xpath_ast_node(ast_step_root, xpath_type_node_set); |
| 9731 | |
| 9732 | // relative location path can start from axis_attribute, dot, double_dot, multiply and string lexemes; any other lexeme means standalone root path |
| 9733 | lexeme_t l = _lexer.current(); |
| 9734 | |
| 9735 | if (l == lex_string || l == lex_axis_attribute || l == lex_dot || l == lex_double_dot || l == lex_multiply) |
| 9736 | return parse_relative_location_path(n); |
| 9737 | else |
| 9738 | return n; |
| 9739 | } |
| 9740 | else if (_lexer.current() == lex_double_slash) |
| 9741 | { |
| 9742 | _lexer.next(); |
| 9743 | |
| 9744 | xpath_ast_node* n = new (alloc_node()) xpath_ast_node(ast_step_root, xpath_type_node_set); |
| 9745 | n = new (alloc_node()) xpath_ast_node(ast_step, n, axis_descendant_or_self, nodetest_type_node, 0); |
| 9746 | |
| 9747 | return parse_relative_location_path(n); |
| 9748 | } |
| 9749 | |
| 9750 | // else clause moved outside of if because of bogus warning 'control may reach end of non-void function being inlined' in gcc 4.0.1 |
| 9751 | return parse_relative_location_path(0); |
| 9752 | } |
| 9753 | |
| 9754 | // PathExpr ::= LocationPath |
| 9755 | // | FilterExpr |
| 9756 | // | FilterExpr '/' RelativeLocationPath |
| 9757 | // | FilterExpr '//' RelativeLocationPath |
| 9758 | // UnionExpr ::= PathExpr | UnionExpr '|' PathExpr |
| 9759 | // UnaryExpr ::= UnionExpr | '-' UnaryExpr |
| 9760 | xpath_ast_node* parse_path_or_unary_expression() |
| 9761 | { |
| 9762 | // Clarification. |
| 9763 | // PathExpr begins with either LocationPath or FilterExpr. |
| 9764 | // FilterExpr begins with PrimaryExpr |
| 9765 | // PrimaryExpr begins with '$' in case of it being a variable reference, |
| 9766 | // '(' in case of it being an expression, string literal, number constant or |
| 9767 | // function call. |
| 9768 | |
| 9769 | if (_lexer.current() == lex_var_ref || _lexer.current() == lex_open_brace || |
| 9770 | _lexer.current() == lex_quoted_string || _lexer.current() == lex_number || |
| 9771 | _lexer.current() == lex_string) |
| 9772 | { |
| 9773 | if (_lexer.current() == lex_string) |
| 9774 | { |
| 9775 | // This is either a function call, or not - if not, we shall proceed with location path |
| 9776 | const char_t* state = _lexer.state(); |
| 9777 | |
| 9778 | while (PUGI__IS_CHARTYPE(*state, ct_space)) ++state; |
| 9779 | |
| 9780 | if (*state != '(') return parse_location_path(); |
| 9781 | |
| 9782 | // This looks like a function call; however this still can be a node-test. Check it. |
| 9783 | if (parse_node_test_type(_lexer.contents()) != nodetest_none) return parse_location_path(); |
| 9784 | } |
| 9785 | |
| 9786 | xpath_ast_node* n = parse_filter_expression(); |
| 9787 | |
| 9788 | if (_lexer.current() == lex_slash || _lexer.current() == lex_double_slash) |
| 9789 | { |
| 9790 | lexeme_t l = _lexer.current(); |
| 9791 | _lexer.next(); |
| 9792 | |
| 9793 | if (l == lex_double_slash) |
| 9794 | { |
| 9795 | if (n->rettype() != xpath_type_node_set) throw_error("Step has to be applied to node set"); |
| 9796 | |
| 9797 | n = new (alloc_node()) xpath_ast_node(ast_step, n, axis_descendant_or_self, nodetest_type_node, 0); |
| 9798 | } |
| 9799 | |
| 9800 | // select from location path |
| 9801 | return parse_relative_location_path(n); |
| 9802 | } |
| 9803 | |
| 9804 | return n; |
| 9805 | } |
| 9806 | else if (_lexer.current() == lex_minus) |
| 9807 | { |
| 9808 | _lexer.next(); |
| 9809 | |
| 9810 | // precedence 7+ - only parses union expressions |
| 9811 | xpath_ast_node* expr = parse_expression_rec(parse_path_or_unary_expression(), 7); |
| 9812 | |
| 9813 | return new (alloc_node()) xpath_ast_node(ast_op_negate, xpath_type_number, expr); |
| 9814 | } |
| 9815 | else |
| 9816 | return parse_location_path(); |
| 9817 | } |
| 9818 | |
| 9819 | struct binary_op_t |
| 9820 | { |
| 9821 | ast_type_t asttype; |
| 9822 | xpath_value_type rettype; |
| 9823 | int precedence; |
| 9824 | |
| 9825 | binary_op_t(): asttype(ast_unknown), rettype(xpath_type_none), precedence(0) |
| 9826 | { |
| 9827 | } |
| 9828 | |
| 9829 | binary_op_t(ast_type_t asttype_, xpath_value_type rettype_, int precedence_): asttype(asttype_), rettype(rettype_), precedence(precedence_) |
| 9830 | { |
| 9831 | } |
| 9832 | |
| 9833 | static binary_op_t parse(xpath_lexer& lexer) |
| 9834 | { |
| 9835 | switch (lexer.current()) |
| 9836 | { |
| 9837 | case lex_string: |
| 9838 | if (lexer.contents() == PUGIXML_TEXT("or")) |
| 9839 | return binary_op_t(ast_op_or, xpath_type_boolean, 1); |
| 9840 | else if (lexer.contents() == PUGIXML_TEXT("and")) |
| 9841 | return binary_op_t(ast_op_and, xpath_type_boolean, 2); |
| 9842 | else if (lexer.contents() == PUGIXML_TEXT("div")) |
| 9843 | return binary_op_t(ast_op_divide, xpath_type_number, 6); |
| 9844 | else if (lexer.contents() == PUGIXML_TEXT("mod")) |
| 9845 | return binary_op_t(ast_op_mod, xpath_type_number, 6); |
| 9846 | else |
| 9847 | return binary_op_t(); |
| 9848 | |
| 9849 | case lex_equal: |
| 9850 | return binary_op_t(ast_op_equal, xpath_type_boolean, 3); |
| 9851 | |
| 9852 | case lex_not_equal: |
| 9853 | return binary_op_t(ast_op_not_equal, xpath_type_boolean, 3); |
| 9854 | |
| 9855 | case lex_less: |
| 9856 | return binary_op_t(ast_op_less, xpath_type_boolean, 4); |
| 9857 | |
| 9858 | case lex_greater: |
| 9859 | return binary_op_t(ast_op_greater, xpath_type_boolean, 4); |
| 9860 | |
| 9861 | case lex_less_or_equal: |
| 9862 | return binary_op_t(ast_op_less_or_equal, xpath_type_boolean, 4); |
| 9863 | |
| 9864 | case lex_greater_or_equal: |
| 9865 | return binary_op_t(ast_op_greater_or_equal, xpath_type_boolean, 4); |
| 9866 | |
| 9867 | case lex_plus: |
| 9868 | return binary_op_t(ast_op_add, xpath_type_number, 5); |
| 9869 | |
| 9870 | case lex_minus: |
| 9871 | return binary_op_t(ast_op_subtract, xpath_type_number, 5); |
| 9872 | |
| 9873 | case lex_multiply: |
| 9874 | return binary_op_t(ast_op_multiply, xpath_type_number, 6); |
| 9875 | |
| 9876 | case lex_union: |
| 9877 | return binary_op_t(ast_op_union, xpath_type_node_set, 7); |
| 9878 | |
| 9879 | default: |
| 9880 | return binary_op_t(); |
| 9881 | } |
| 9882 | } |
| 9883 | }; |
| 9884 | |
| 9885 | xpath_ast_node* parse_expression_rec(xpath_ast_node* lhs, int limit) |
| 9886 | { |
| 9887 | binary_op_t op = binary_op_t::parse(_lexer); |
| 9888 | |
| 9889 | while (op.asttype != ast_unknown && op.precedence >= limit) |
| 9890 | { |
| 9891 | _lexer.next(); |
| 9892 | |
| 9893 | xpath_ast_node* rhs = parse_path_or_unary_expression(); |
| 9894 | |
| 9895 | binary_op_t nextop = binary_op_t::parse(_lexer); |
| 9896 | |
| 9897 | while (nextop.asttype != ast_unknown && nextop.precedence > op.precedence) |
| 9898 | { |
| 9899 | rhs = parse_expression_rec(rhs, nextop.precedence); |
| 9900 | |
| 9901 | nextop = binary_op_t::parse(_lexer); |
| 9902 | } |
| 9903 | |
| 9904 | if (op.asttype == ast_op_union && (lhs->rettype() != xpath_type_node_set || rhs->rettype() != xpath_type_node_set)) |
| 9905 | throw_error("Union operator has to be applied to node sets"); |
| 9906 | |
| 9907 | lhs = new (alloc_node()) xpath_ast_node(op.asttype, op.rettype, lhs, rhs); |
| 9908 | |
| 9909 | op = binary_op_t::parse(_lexer); |
| 9910 | } |
| 9911 | |
| 9912 | return lhs; |
| 9913 | } |
| 9914 | |
| 9915 | // Expr ::= OrExpr |
| 9916 | // OrExpr ::= AndExpr | OrExpr 'or' AndExpr |
| 9917 | // AndExpr ::= EqualityExpr | AndExpr 'and' EqualityExpr |
| 9918 | // EqualityExpr ::= RelationalExpr |
| 9919 | // | EqualityExpr '=' RelationalExpr |
| 9920 | // | EqualityExpr '!=' RelationalExpr |
| 9921 | // RelationalExpr ::= AdditiveExpr |
| 9922 | // | RelationalExpr '<' AdditiveExpr |
| 9923 | // | RelationalExpr '>' AdditiveExpr |
| 9924 | // | RelationalExpr '<=' AdditiveExpr |
| 9925 | // | RelationalExpr '>=' AdditiveExpr |
| 9926 | // AdditiveExpr ::= MultiplicativeExpr |
| 9927 | // | AdditiveExpr '+' MultiplicativeExpr |
| 9928 | // | AdditiveExpr '-' MultiplicativeExpr |
| 9929 | // MultiplicativeExpr ::= UnaryExpr |
| 9930 | // | MultiplicativeExpr '*' UnaryExpr |
| 9931 | // | MultiplicativeExpr 'div' UnaryExpr |
| 9932 | // | MultiplicativeExpr 'mod' UnaryExpr |
| 9933 | xpath_ast_node* parse_expression() |
| 9934 | { |
| 9935 | return parse_expression_rec(parse_path_or_unary_expression(), 0); |
| 9936 | } |
| 9937 | |
| 9938 | xpath_parser(const char_t* query, xpath_variable_set* variables, xpath_allocator* alloc, xpath_parse_result* result): _alloc(alloc), _lexer(query), _query(query), _variables(variables), _result(result) |
| 9939 | { |
| 9940 | } |
| 9941 | |
| 9942 | xpath_ast_node* parse() |
| 9943 | { |
| 9944 | xpath_ast_node* result = parse_expression(); |
| 9945 | |
| 9946 | if (_lexer.current() != lex_eof) |
| 9947 | { |
| 9948 | // there are still unparsed tokens left, error |
| 9949 | throw_error("Incorrect query"); |
| 9950 | } |
| 9951 | |
| 9952 | return result; |
| 9953 | } |
| 9954 | |
| 9955 | static xpath_ast_node* parse(const char_t* query, xpath_variable_set* variables, xpath_allocator* alloc, xpath_parse_result* result) |
| 9956 | { |
| 9957 | xpath_parser parser(query, variables, alloc, result); |
| 9958 | |
| 9959 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 9960 | int error = setjmp(parser._error_handler); |
| 9961 | |
| 9962 | return (error == 0) ? parser.parse() : 0; |
| 9963 | #else |
| 9964 | return parser.parse(); |
| 9965 | #endif |
| 9966 | } |
| 9967 | }; |
| 9968 | |
| 9969 | struct xpath_query_impl |
| 9970 | { |
| 9971 | static xpath_query_impl* create() |
| 9972 | { |
| 9973 | void* memory = xml_memory::allocate(sizeof(xpath_query_impl)); |
| 9974 | |
| 9975 | return new (memory) xpath_query_impl(); |
| 9976 | } |
| 9977 | |
| 9978 | static void destroy(void* ptr) |
| 9979 | { |
| 9980 | if (!ptr) return; |
| 9981 | |
| 9982 | // free all allocated pages |
| 9983 | static_cast<xpath_query_impl*>(ptr)->alloc.release(); |
| 9984 | |
| 9985 | // free allocator memory (with the first page) |
| 9986 | xml_memory::deallocate(ptr); |
| 9987 | } |
| 9988 | |
| 9989 | xpath_query_impl(): root(0), alloc(&block) |
| 9990 | { |
| 9991 | block.next = 0; |
| 9992 | } |
| 9993 | |
| 9994 | xpath_ast_node* root; |
| 9995 | xpath_allocator alloc; |
| 9996 | xpath_memory_block block; |
| 9997 | }; |
| 9998 | |
| 9999 | PUGI__FN xpath_string evaluate_string_impl(xpath_query_impl* impl, const xpath_node& n, xpath_stack_data& sd) |
| 10000 | { |
| 10001 | if (!impl) return xpath_string(); |
| 10002 | |
| 10003 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 10004 | if (setjmp(sd.error_handler)) return xpath_string(); |
| 10005 | #endif |
| 10006 | |
| 10007 | xpath_context c(n, 1, 1); |
| 10008 | |
| 10009 | return impl->root->eval_string(c, sd.stack); |
| 10010 | } |
| 10011 | PUGI__NS_END |
| 10012 | |
| 10013 | namespace pugi |
| 10014 | { |
| 10015 | #ifndef PUGIXML_NO_EXCEPTIONS |
| 10016 | PUGI__FN xpath_exception::xpath_exception(const xpath_parse_result& result_): _result(result_) |
| 10017 | { |
| 10018 | assert(_result.error); |
| 10019 | } |
| 10020 | |
| 10021 | PUGI__FN const char* xpath_exception::what() const throw() |
| 10022 | { |
| 10023 | return _result.error; |
| 10024 | } |
| 10025 | |
| 10026 | PUGI__FN const xpath_parse_result& xpath_exception::result() const |
| 10027 | { |
| 10028 | return _result; |
| 10029 | } |
| 10030 | #endif |
| 10031 | |
| 10032 | PUGI__FN xpath_node::xpath_node() |
| 10033 | { |
| 10034 | } |
| 10035 | |
| 10036 | PUGI__FN xpath_node::xpath_node(const xml_node& node_): _node(node_) |
| 10037 | { |
| 10038 | } |
| 10039 | |
| 10040 | PUGI__FN xpath_node::xpath_node(const xml_attribute& attribute_, const xml_node& parent_): _node(attribute_ ? parent_ : xml_node()), _attribute(attribute_) |
| 10041 | { |
| 10042 | } |
| 10043 | |
| 10044 | PUGI__FN xml_node xpath_node::node() const |
| 10045 | { |
| 10046 | return _attribute ? xml_node() : _node; |
| 10047 | } |
| 10048 | |
| 10049 | PUGI__FN xml_attribute xpath_node::attribute() const |
| 10050 | { |
| 10051 | return _attribute; |
| 10052 | } |
| 10053 | |
| 10054 | PUGI__FN xml_node xpath_node::parent() const |
| 10055 | { |
| 10056 | return _attribute ? _node : _node.parent(); |
| 10057 | } |
| 10058 | |
| 10059 | PUGI__FN static void unspecified_bool_xpath_node(xpath_node***) |
| 10060 | { |
| 10061 | } |
| 10062 | |
| 10063 | PUGI__FN xpath_node::operator xpath_node::unspecified_bool_type() const |
| 10064 | { |
| 10065 | return (_node || _attribute) ? unspecified_bool_xpath_node : 0; |
| 10066 | } |
| 10067 | |
| 10068 | PUGI__FN bool xpath_node::operator!() const |
| 10069 | { |
| 10070 | return !(_node || _attribute); |
| 10071 | } |
| 10072 | |
| 10073 | PUGI__FN bool xpath_node::operator==(const xpath_node& n) const |
| 10074 | { |
| 10075 | return _node == n._node && _attribute == n._attribute; |
| 10076 | } |
| 10077 | |
| 10078 | PUGI__FN bool xpath_node::operator!=(const xpath_node& n) const |
| 10079 | { |
| 10080 | return _node != n._node || _attribute != n._attribute; |
| 10081 | } |
| 10082 | |
| 10083 | #ifdef __BORLANDC__ |
| 10084 | PUGI__FN bool operator&&(const xpath_node& lhs, bool rhs) |
| 10085 | { |
| 10086 | return (bool)lhs && rhs; |
| 10087 | } |
| 10088 | |
| 10089 | PUGI__FN bool operator||(const xpath_node& lhs, bool rhs) |
| 10090 | { |
| 10091 | return (bool)lhs || rhs; |
| 10092 | } |
| 10093 | #endif |
| 10094 | |
| 10095 | PUGI__FN void xpath_node_set::_assign(const_iterator begin_, const_iterator end_) |
| 10096 | { |
| 10097 | assert(begin_ <= end_); |
| 10098 | |
| 10099 | size_t size_ = static_cast<size_t>(end_ - begin_); |
| 10100 | |
| 10101 | if (size_ <= 1) |
| 10102 | { |
| 10103 | // deallocate old buffer |
| 10104 | if (_begin != &_storage) impl::xml_memory::deallocate(_begin); |
| 10105 | |
| 10106 | // use internal buffer |
| 10107 | if (begin_ != end_) _storage = *begin_; |
| 10108 | |
| 10109 | _begin = &_storage; |
| 10110 | _end = &_storage + size_; |
| 10111 | } |
| 10112 | else |
| 10113 | { |
| 10114 | // make heap copy |
| 10115 | xpath_node* storage = static_cast<xpath_node*>(impl::xml_memory::allocate(size_ * sizeof(xpath_node))); |
| 10116 | |
| 10117 | if (!storage) |
| 10118 | { |
| 10119 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 10120 | return; |
| 10121 | #else |
| 10122 | throw std::bad_alloc(); |
| 10123 | #endif |
| 10124 | } |
| 10125 | |
| 10126 | memcpy(storage, begin_, size_ * sizeof(xpath_node)); |
| 10127 | |
| 10128 | // deallocate old buffer |
| 10129 | if (_begin != &_storage) impl::xml_memory::deallocate(_begin); |
| 10130 | |
| 10131 | // finalize |
| 10132 | _begin = storage; |
| 10133 | _end = storage + size_; |
| 10134 | } |
| 10135 | } |
| 10136 | |
| 10137 | PUGI__FN xpath_node_set::xpath_node_set(): _type(type_unsorted), _begin(&_storage), _end(&_storage) |
| 10138 | { |
| 10139 | } |
| 10140 | |
| 10141 | PUGI__FN xpath_node_set::xpath_node_set(const_iterator begin_, const_iterator end_, type_t type_): _type(type_), _begin(&_storage), _end(&_storage) |
| 10142 | { |
| 10143 | _assign(begin_, end_); |
| 10144 | } |
| 10145 | |
| 10146 | PUGI__FN xpath_node_set::~xpath_node_set() |
| 10147 | { |
| 10148 | if (_begin != &_storage) impl::xml_memory::deallocate(_begin); |
| 10149 | } |
| 10150 | |
| 10151 | PUGI__FN xpath_node_set::xpath_node_set(const xpath_node_set& ns): _type(ns._type), _begin(&_storage), _end(&_storage) |
| 10152 | { |
| 10153 | _assign(ns._begin, ns._end); |
| 10154 | } |
| 10155 | |
| 10156 | PUGI__FN xpath_node_set& xpath_node_set::operator=(const xpath_node_set& ns) |
| 10157 | { |
| 10158 | if (this == &ns) return *this; |
| 10159 | |
| 10160 | _type = ns._type; |
| 10161 | _assign(ns._begin, ns._end); |
| 10162 | |
| 10163 | return *this; |
| 10164 | } |
| 10165 | |
| 10166 | PUGI__FN xpath_node_set::type_t xpath_node_set::type() const |
| 10167 | { |
| 10168 | return _type; |
| 10169 | } |
| 10170 | |
| 10171 | PUGI__FN size_t xpath_node_set::size() const |
| 10172 | { |
| 10173 | return _end - _begin; |
| 10174 | } |
| 10175 | |
| 10176 | PUGI__FN bool xpath_node_set::empty() const |
| 10177 | { |
| 10178 | return _begin == _end; |
| 10179 | } |
| 10180 | |
| 10181 | PUGI__FN const xpath_node& xpath_node_set::operator[](size_t index) const |
| 10182 | { |
| 10183 | assert(index < size()); |
| 10184 | return _begin[index]; |
| 10185 | } |
| 10186 | |
| 10187 | PUGI__FN xpath_node_set::const_iterator xpath_node_set::begin() const |
| 10188 | { |
| 10189 | return _begin; |
| 10190 | } |
| 10191 | |
| 10192 | PUGI__FN xpath_node_set::const_iterator xpath_node_set::end() const |
| 10193 | { |
| 10194 | return _end; |
| 10195 | } |
| 10196 | |
| 10197 | PUGI__FN void xpath_node_set::sort(bool reverse) |
| 10198 | { |
| 10199 | _type = impl::xpath_sort(_begin, _end, _type, reverse); |
| 10200 | } |
| 10201 | |
| 10202 | PUGI__FN xpath_node xpath_node_set::first() const |
| 10203 | { |
| 10204 | return impl::xpath_first(_begin, _end, _type); |
| 10205 | } |
| 10206 | |
| 10207 | PUGI__FN xpath_parse_result::xpath_parse_result(): error("Internal error"), offset(0) |
| 10208 | { |
| 10209 | } |
| 10210 | |
| 10211 | PUGI__FN xpath_parse_result::operator bool() const |
| 10212 | { |
| 10213 | return error == 0; |
| 10214 | } |
| 10215 | |
| 10216 | PUGI__FN const char* xpath_parse_result::description() const |
| 10217 | { |
| 10218 | return error ? error : "No error"; |
| 10219 | } |
| 10220 | |
| 10221 | PUGI__FN xpath_variable::xpath_variable(): _type(xpath_type_none), _next(0) |
| 10222 | { |
| 10223 | } |
| 10224 | |
| 10225 | PUGI__FN const char_t* xpath_variable::name() const |
| 10226 | { |
| 10227 | switch (_type) |
| 10228 | { |
| 10229 | case xpath_type_node_set: |
| 10230 | return static_cast<const impl::xpath_variable_node_set*>(this)->name; |
| 10231 | |
| 10232 | case xpath_type_number: |
| 10233 | return static_cast<const impl::xpath_variable_number*>(this)->name; |
| 10234 | |
| 10235 | case xpath_type_string: |
| 10236 | return static_cast<const impl::xpath_variable_string*>(this)->name; |
| 10237 | |
| 10238 | case xpath_type_boolean: |
| 10239 | return static_cast<const impl::xpath_variable_boolean*>(this)->name; |
| 10240 | |
| 10241 | default: |
| 10242 | assert(!"Invalid variable type"); |
| 10243 | return 0; |
| 10244 | } |
| 10245 | } |
| 10246 | |
| 10247 | PUGI__FN xpath_value_type xpath_variable::type() const |
| 10248 | { |
| 10249 | return _type; |
| 10250 | } |
| 10251 | |
| 10252 | PUGI__FN bool xpath_variable::get_boolean() const |
| 10253 | { |
| 10254 | return (_type == xpath_type_boolean) ? static_cast<const impl::xpath_variable_boolean*>(this)->value : false; |
| 10255 | } |
| 10256 | |
| 10257 | PUGI__FN double xpath_variable::get_number() const |
| 10258 | { |
| 10259 | return (_type == xpath_type_number) ? static_cast<const impl::xpath_variable_number*>(this)->value : impl::gen_nan(); |
| 10260 | } |
| 10261 | |
| 10262 | PUGI__FN const char_t* xpath_variable::get_string() const |
| 10263 | { |
| 10264 | const char_t* value = (_type == xpath_type_string) ? static_cast<const impl::xpath_variable_string*>(this)->value : 0; |
| 10265 | return value ? value : PUGIXML_TEXT(""); |
| 10266 | } |
| 10267 | |
| 10268 | PUGI__FN const xpath_node_set& xpath_variable::get_node_set() const |
| 10269 | { |
| 10270 | return (_type == xpath_type_node_set) ? static_cast<const impl::xpath_variable_node_set*>(this)->value : impl::dummy_node_set; |
| 10271 | } |
| 10272 | |
| 10273 | PUGI__FN bool xpath_variable::set(bool value) |
| 10274 | { |
| 10275 | if (_type != xpath_type_boolean) return false; |
| 10276 | |
| 10277 | static_cast<impl::xpath_variable_boolean*>(this)->value = value; |
| 10278 | return true; |
| 10279 | } |
| 10280 | |
| 10281 | PUGI__FN bool xpath_variable::set(double value) |
| 10282 | { |
| 10283 | if (_type != xpath_type_number) return false; |
| 10284 | |
| 10285 | static_cast<impl::xpath_variable_number*>(this)->value = value; |
| 10286 | return true; |
| 10287 | } |
| 10288 | |
| 10289 | PUGI__FN bool xpath_variable::set(const char_t* value) |
| 10290 | { |
| 10291 | if (_type != xpath_type_string) return false; |
| 10292 | |
| 10293 | impl::xpath_variable_string* var = static_cast<impl::xpath_variable_string*>(this); |
| 10294 | |
| 10295 | // duplicate string |
| 10296 | size_t size = (impl::strlength(value) + 1) * sizeof(char_t); |
| 10297 | |
| 10298 | char_t* copy = static_cast<char_t*>(impl::xml_memory::allocate(size)); |
| 10299 | if (!copy) return false; |
| 10300 | |
| 10301 | memcpy(copy, value, size); |
| 10302 | |
| 10303 | // replace old string |
| 10304 | if (var->value) impl::xml_memory::deallocate(var->value); |
| 10305 | var->value = copy; |
| 10306 | |
| 10307 | return true; |
| 10308 | } |
| 10309 | |
| 10310 | PUGI__FN bool xpath_variable::set(const xpath_node_set& value) |
| 10311 | { |
| 10312 | if (_type != xpath_type_node_set) return false; |
| 10313 | |
| 10314 | static_cast<impl::xpath_variable_node_set*>(this)->value = value; |
| 10315 | return true; |
| 10316 | } |
| 10317 | |
| 10318 | PUGI__FN xpath_variable_set::xpath_variable_set() |
| 10319 | { |
| 10320 | for (size_t i = 0; i < sizeof(_data) / sizeof(_data[0]); ++i) _data[i] = 0; |
| 10321 | } |
| 10322 | |
| 10323 | PUGI__FN xpath_variable_set::~xpath_variable_set() |
| 10324 | { |
| 10325 | for (size_t i = 0; i < sizeof(_data) / sizeof(_data[0]); ++i) |
| 10326 | { |
| 10327 | xpath_variable* var = _data[i]; |
| 10328 | |
| 10329 | while (var) |
| 10330 | { |
| 10331 | xpath_variable* next = var->_next; |
| 10332 | |
| 10333 | impl::delete_xpath_variable(var->_type, var); |
| 10334 | |
| 10335 | var = next; |
| 10336 | } |
| 10337 | } |
| 10338 | } |
| 10339 | |
| 10340 | PUGI__FN xpath_variable* xpath_variable_set::find(const char_t* name) const |
| 10341 | { |
| 10342 | const size_t hash_size = sizeof(_data) / sizeof(_data[0]); |
| 10343 | size_t hash = impl::hash_string(name) % hash_size; |
| 10344 | |
| 10345 | // look for existing variable |
| 10346 | for (xpath_variable* var = _data[hash]; var; var = var->_next) |
| 10347 | if (impl::strequal(var->name(), name)) |
| 10348 | return var; |
| 10349 | |
| 10350 | return 0; |
| 10351 | } |
| 10352 | |
| 10353 | PUGI__FN xpath_variable* xpath_variable_set::add(const char_t* name, xpath_value_type type) |
| 10354 | { |
| 10355 | const size_t hash_size = sizeof(_data) / sizeof(_data[0]); |
| 10356 | size_t hash = impl::hash_string(name) % hash_size; |
| 10357 | |
| 10358 | // look for existing variable |
| 10359 | for (xpath_variable* var = _data[hash]; var; var = var->_next) |
| 10360 | if (impl::strequal(var->name(), name)) |
| 10361 | return var->type() == type ? var : 0; |
| 10362 | |
| 10363 | // add new variable |
| 10364 | xpath_variable* result = impl::new_xpath_variable(type, name); |
| 10365 | |
| 10366 | if (result) |
| 10367 | { |
| 10368 | result->_type = type; |
| 10369 | result->_next = _data[hash]; |
| 10370 | |
| 10371 | _data[hash] = result; |
| 10372 | } |
| 10373 | |
| 10374 | return result; |
| 10375 | } |
| 10376 | |
| 10377 | PUGI__FN bool xpath_variable_set::set(const char_t* name, bool value) |
| 10378 | { |
| 10379 | xpath_variable* var = add(name, xpath_type_boolean); |
| 10380 | return var ? var->set(value) : false; |
| 10381 | } |
| 10382 | |
| 10383 | PUGI__FN bool xpath_variable_set::set(const char_t* name, double value) |
| 10384 | { |
| 10385 | xpath_variable* var = add(name, xpath_type_number); |
| 10386 | return var ? var->set(value) : false; |
| 10387 | } |
| 10388 | |
| 10389 | PUGI__FN bool xpath_variable_set::set(const char_t* name, const char_t* value) |
| 10390 | { |
| 10391 | xpath_variable* var = add(name, xpath_type_string); |
| 10392 | return var ? var->set(value) : false; |
| 10393 | } |
| 10394 | |
| 10395 | PUGI__FN bool xpath_variable_set::set(const char_t* name, const xpath_node_set& value) |
| 10396 | { |
| 10397 | xpath_variable* var = add(name, xpath_type_node_set); |
| 10398 | return var ? var->set(value) : false; |
| 10399 | } |
| 10400 | |
| 10401 | PUGI__FN xpath_variable* xpath_variable_set::get(const char_t* name) |
| 10402 | { |
| 10403 | return find(name); |
| 10404 | } |
| 10405 | |
| 10406 | PUGI__FN const xpath_variable* xpath_variable_set::get(const char_t* name) const |
| 10407 | { |
| 10408 | return find(name); |
| 10409 | } |
| 10410 | |
| 10411 | PUGI__FN xpath_query::xpath_query(const char_t* query, xpath_variable_set* variables): _impl(0) |
| 10412 | { |
| 10413 | impl::xpath_query_impl* qimpl = impl::xpath_query_impl::create(); |
| 10414 | |
| 10415 | if (!qimpl) |
| 10416 | { |
| 10417 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 10418 | _result.error = "Out of memory"; |
| 10419 | #else |
| 10420 | throw std::bad_alloc(); |
| 10421 | #endif |
| 10422 | } |
| 10423 | else |
| 10424 | { |
| 10425 | impl::buffer_holder impl_holder(qimpl, impl::xpath_query_impl::destroy); |
| 10426 | |
| 10427 | qimpl->root = impl::xpath_parser::parse(query, variables, &qimpl->alloc, &_result); |
| 10428 | |
| 10429 | if (qimpl->root) |
| 10430 | { |
| 10431 | _impl = static_cast<impl::xpath_query_impl*>(impl_holder.release()); |
| 10432 | _result.error = 0; |
| 10433 | } |
| 10434 | } |
| 10435 | } |
| 10436 | |
| 10437 | PUGI__FN xpath_query::~xpath_query() |
| 10438 | { |
| 10439 | impl::xpath_query_impl::destroy(_impl); |
| 10440 | } |
| 10441 | |
| 10442 | PUGI__FN xpath_value_type xpath_query::return_type() const |
| 10443 | { |
| 10444 | if (!_impl) return xpath_type_none; |
| 10445 | |
| 10446 | return static_cast<impl::xpath_query_impl*>(_impl)->root->rettype(); |
| 10447 | } |
| 10448 | |
| 10449 | PUGI__FN bool xpath_query::evaluate_boolean(const xpath_node& n) const |
| 10450 | { |
| 10451 | if (!_impl) return false; |
| 10452 | |
| 10453 | impl::xpath_context c(n, 1, 1); |
| 10454 | impl::xpath_stack_data sd; |
| 10455 | |
| 10456 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 10457 | if (setjmp(sd.error_handler)) return false; |
| 10458 | #endif |
| 10459 | |
| 10460 | return static_cast<impl::xpath_query_impl*>(_impl)->root->eval_boolean(c, sd.stack); |
| 10461 | } |
| 10462 | |
| 10463 | PUGI__FN double xpath_query::evaluate_number(const xpath_node& n) const |
| 10464 | { |
| 10465 | if (!_impl) return impl::gen_nan(); |
| 10466 | |
| 10467 | impl::xpath_context c(n, 1, 1); |
| 10468 | impl::xpath_stack_data sd; |
| 10469 | |
| 10470 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 10471 | if (setjmp(sd.error_handler)) return impl::gen_nan(); |
| 10472 | #endif |
| 10473 | |
| 10474 | return static_cast<impl::xpath_query_impl*>(_impl)->root->eval_number(c, sd.stack); |
| 10475 | } |
| 10476 | |
| 10477 | #ifndef PUGIXML_NO_STL |
| 10478 | PUGI__FN string_t xpath_query::evaluate_string(const xpath_node& n) const |
| 10479 | { |
| 10480 | impl::xpath_stack_data sd; |
| 10481 | |
| 10482 | return impl::evaluate_string_impl(static_cast<impl::xpath_query_impl*>(_impl), n, sd).c_str(); |
| 10483 | } |
| 10484 | #endif |
| 10485 | |
| 10486 | PUGI__FN size_t xpath_query::evaluate_string(char_t* buffer, size_t capacity, const xpath_node& n) const |
| 10487 | { |
| 10488 | impl::xpath_stack_data sd; |
| 10489 | |
| 10490 | impl::xpath_string r = impl::evaluate_string_impl(static_cast<impl::xpath_query_impl*>(_impl), n, sd); |
| 10491 | |
| 10492 | size_t full_size = r.length() + 1; |
| 10493 | |
| 10494 | if (capacity > 0) |
| 10495 | { |
| 10496 | size_t size = (full_size < capacity) ? full_size : capacity; |
| 10497 | assert(size > 0); |
| 10498 | |
| 10499 | memcpy(buffer, r.c_str(), (size - 1) * sizeof(char_t)); |
| 10500 | buffer[size - 1] = 0; |
| 10501 | } |
| 10502 | |
| 10503 | return full_size; |
| 10504 | } |
| 10505 | |
| 10506 | PUGI__FN xpath_node_set xpath_query::evaluate_node_set(const xpath_node& n) const |
| 10507 | { |
| 10508 | if (!_impl) return xpath_node_set(); |
| 10509 | |
| 10510 | impl::xpath_ast_node* root = static_cast<impl::xpath_query_impl*>(_impl)->root; |
| 10511 | |
| 10512 | if (root->rettype() != xpath_type_node_set) |
| 10513 | { |
| 10514 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 10515 | return xpath_node_set(); |
| 10516 | #else |
| 10517 | xpath_parse_result res; |
| 10518 | res.error = "Expression does not evaluate to node set"; |
| 10519 | |
| 10520 | throw xpath_exception(res); |
| 10521 | #endif |
| 10522 | } |
| 10523 | |
| 10524 | impl::xpath_context c(n, 1, 1); |
| 10525 | impl::xpath_stack_data sd; |
| 10526 | |
| 10527 | #ifdef PUGIXML_NO_EXCEPTIONS |
| 10528 | if (setjmp(sd.error_handler)) return xpath_node_set(); |
| 10529 | #endif |
| 10530 | |
| 10531 | impl::xpath_node_set_raw r = root->eval_node_set(c, sd.stack); |
| 10532 | |
| 10533 | return xpath_node_set(r.begin(), r.end(), r.type()); |
| 10534 | } |
| 10535 | |
| 10536 | PUGI__FN const xpath_parse_result& xpath_query::result() const |
| 10537 | { |
| 10538 | return _result; |
| 10539 | } |
| 10540 | |
| 10541 | PUGI__FN static void unspecified_bool_xpath_query(xpath_query***) |
| 10542 | { |
| 10543 | } |
| 10544 | |
| 10545 | PUGI__FN xpath_query::operator xpath_query::unspecified_bool_type() const |
| 10546 | { |
| 10547 | return _impl ? unspecified_bool_xpath_query : 0; |
| 10548 | } |
| 10549 | |
| 10550 | PUGI__FN bool xpath_query::operator!() const |
| 10551 | { |
| 10552 | return !_impl; |
| 10553 | } |
| 10554 | |
| 10555 | PUGI__FN xpath_node xml_node::select_single_node(const char_t* query, xpath_variable_set* variables) const |
| 10556 | { |
| 10557 | xpath_query q(query, variables); |
| 10558 | return select_single_node(q); |
| 10559 | } |
| 10560 | |
| 10561 | PUGI__FN xpath_node xml_node::select_single_node(const xpath_query& query) const |
| 10562 | { |
| 10563 | xpath_node_set s = query.evaluate_node_set(*this); |
| 10564 | return s.empty() ? xpath_node() : s.first(); |
| 10565 | } |
| 10566 | |
| 10567 | PUGI__FN xpath_node_set xml_node::select_nodes(const char_t* query, xpath_variable_set* variables) const |
| 10568 | { |
| 10569 | xpath_query q(query, variables); |
| 10570 | return select_nodes(q); |
| 10571 | } |
| 10572 | |
| 10573 | PUGI__FN xpath_node_set xml_node::select_nodes(const xpath_query& query) const |
| 10574 | { |
| 10575 | return query.evaluate_node_set(*this); |
| 10576 | } |
| 10577 | } |
| 10578 | |
| 10579 | #endif |
| 10580 | |
| 10581 | #ifdef __BORLANDC__ |
| 10582 | # pragma option pop |
| 10583 | #endif |
| 10584 | |
| 10585 | // Intel C++ does not properly keep warning state for function templates, |
| 10586 | // so popping warning state at the end of translation unit leads to warnings in the middle. |
| 10587 | #if defined(_MSC_VER) && !defined(__INTEL_COMPILER) |
| 10588 | # pragma warning(pop) |
| 10589 | #endif |
| 10590 | |
| 10591 | // Undefine all local macros (makes sure we're not leaking macros in header-only mode) |
| 10592 | #undef PUGI__NO_INLINE |
| 10593 | #undef PUGI__STATIC_ASSERT |
| 10594 | #undef PUGI__DMC_VOLATILE |
| 10595 | #undef PUGI__MSVC_CRT_VERSION |
| 10596 | #undef PUGI__NS_BEGIN |
| 10597 | #undef PUGI__NS_END |
| 10598 | #undef PUGI__FN |
| 10599 | #undef PUGI__FN_NO_INLINE |
| 10600 | #undef PUGI__IS_CHARTYPE_IMPL |
| 10601 | #undef PUGI__IS_CHARTYPE |
| 10602 | #undef PUGI__IS_CHARTYPEX |
| 10603 | #undef PUGI__SKIPWS |
| 10604 | #undef PUGI__OPTSET |
| 10605 | #undef PUGI__PUSHNODE |
| 10606 | #undef PUGI__POPNODE |
| 10607 | #undef PUGI__SCANFOR |
| 10608 | #undef PUGI__SCANWHILE |
| 10609 | #undef PUGI__ENDSEG |
| 10610 | #undef PUGI__THROW_ERROR |
| 10611 | #undef PUGI__CHECK_ERROR |
| 10612 | |
| 10613 | #endif |
| 10614 | |
| 10615 | /** |
| 10616 | * Copyright (c) 2006-2014 Arseny Kapoulkine |
| 10617 | * |
| 10618 | * Permission is hereby granted, free of charge, to any person |
| 10619 | * obtaining a copy of this software and associated documentation |
| 10620 | * files (the "Software"), to deal in the Software without |
| 10621 | * restriction, including without limitation the rights to use, |
| 10622 | * copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10623 | * copies of the Software, and to permit persons to whom the |
| 10624 | * Software is furnished to do so, subject to the following |
| 10625 | * conditions: |
| 10626 | * |
| 10627 | * The above copyright notice and this permission notice shall be |
| 10628 | * included in all copies or substantial portions of the Software. |
| 10629 | * |
| 10630 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 10631 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 10632 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 10633 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 10634 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 10635 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 10636 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 10637 | * OTHER DEALINGS IN THE SOFTWARE. |
| 10638 | */ |