| @@ -0,0 +1,1266 @@ | |||
| 1 | + | /** | |
| 2 | + | * pugixml parser - version 1.2 | |
| 3 | + | * -------------------------------------------------------- | |
| 4 | + | * Copyright (C) 2006-2012, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com) | |
| 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 (kristen@tima.net) | |
| 12 | + | */ | |
| 13 | + | ||
| 14 | + | #ifndef PUGIXML_VERSION | |
| 15 | + | // Define version macro; evaluates to major * 100 + minor so that it's safe to use in less-than comparisons | |
| 16 | + | # define PUGIXML_VERSION 120 | |
| 17 | + | #endif | |
| 18 | + | ||
| 19 | + | // Include user configuration file (this can define various configuration macros) | |
| 20 | + | #include "pugiconfig.hpp" | |
| 21 | + | ||
| 22 | + | #ifndef HEADER_PUGIXML_HPP | |
| 23 | + | #define HEADER_PUGIXML_HPP | |
| 24 | + | ||
| 25 | + | // Include stddef.h for size_t and ptrdiff_t | |
| 26 | + | #include <stddef.h> | |
| 27 | + | ||
| 28 | + | // Include exception header for XPath | |
| 29 | + | #if !defined(PUGIXML_NO_XPATH) && !defined(PUGIXML_NO_EXCEPTIONS) | |
| 30 | + | # include <exception> | |
| 31 | + | #endif | |
| 32 | + | ||
| 33 | + | // Include STL headers | |
| 34 | + | #ifndef PUGIXML_NO_STL | |
| 35 | + | # include <iterator> | |
| 36 | + | # include <iosfwd> | |
| 37 | + | # include <string> | |
| 38 | + | #endif | |
| 39 | + | ||
| 40 | + | // Macro for deprecated features | |
| 41 | + | #ifndef PUGIXML_DEPRECATED | |
| 42 | + | # if defined(__GNUC__) | |
| 43 | + | # define PUGIXML_DEPRECATED __attribute__((deprecated)) | |
| 44 | + | # elif defined(_MSC_VER) && _MSC_VER >= 1300 | |
| 45 | + | # define PUGIXML_DEPRECATED __declspec(deprecated) | |
| 46 | + | # else | |
| 47 | + | # define PUGIXML_DEPRECATED | |
| 48 | + | # endif | |
| 49 | + | #endif | |
| 50 | + | ||
| 51 | + | // If no API is defined, assume default | |
| 52 | + | #ifndef PUGIXML_API | |
| 53 | + | # define PUGIXML_API | |
| 54 | + | #endif | |
| 55 | + | ||
| 56 | + | // If no API for classes is defined, assume default | |
| 57 | + | #ifndef PUGIXML_CLASS | |
| 58 | + | # define PUGIXML_CLASS PUGIXML_API | |
| 59 | + | #endif | |
| 60 | + | ||
| 61 | + | // If no API for functions is defined, assume default | |
| 62 | + | #ifndef PUGIXML_FUNCTION | |
| 63 | + | # define PUGIXML_FUNCTION PUGIXML_API | |
| 64 | + | #endif | |
| 65 | + | ||
| 66 | + | // Character interface macros | |
| 67 | + | #ifdef PUGIXML_WCHAR_MODE | |
| 68 | + | # define PUGIXML_TEXT(t) L ## t | |
| 69 | + | # define PUGIXML_CHAR wchar_t | |
| 70 | + | #else | |
| 71 | + | # define PUGIXML_TEXT(t) t | |
| 72 | + | # define PUGIXML_CHAR char | |
| 73 | + | #endif | |
| 74 | + | ||
| 75 | + | namespace pugi | |
| 76 | + | { | |
| 77 | + | // Character type used for all internal storage and operations; depends on PUGIXML_WCHAR_MODE | |
| 78 | + | typedef PUGIXML_CHAR char_t; | |
| 79 | + | ||
| 80 | + | #ifndef PUGIXML_NO_STL | |
| 81 | + | // String type used for operations that work with STL string; depends on PUGIXML_WCHAR_MODE | |
| 82 | + | typedef std::basic_string<PUGIXML_CHAR, std::char_traits<PUGIXML_CHAR>, std::allocator<PUGIXML_CHAR> > string_t; | |
| 83 | + | #endif | |
| 84 | + | } | |
| 85 | + | ||
| 86 | + | // The PugiXML namespace | |
| 87 | + | namespace pugi | |
| 88 | + | { | |
| 89 | + | // Tree node types | |
| 90 | + | enum xml_node_type | |
| 91 | + | { | |
| 92 | + | node_null, // Empty (null) node handle | |
| 93 | + | node_document, // A document tree's absolute root | |
| 94 | + | node_element, // Element tag, i.e. '<node/>' | |
| 95 | + | node_pcdata, // Plain character data, i.e. 'text' | |
| 96 | + | node_cdata, // Character data, i.e. '<![CDATA[text]]>' | |
| 97 | + | node_comment, // Comment tag, i.e. '<!-- text -->' | |
| 98 | + | node_pi, // Processing instruction, i.e. '<?name?>' | |
| 99 | + | node_declaration, // Document declaration, i.e. '<?xml version="1.0"?>' | |
| 100 | + | node_doctype // Document type declaration, i.e. '<!DOCTYPE doc>' | |
| 101 | + | }; | |
| 102 | + | ||
| 103 | + | // Parsing options | |
| 104 | + | ||
| 105 | + | // Minimal parsing mode (equivalent to turning all other flags off). | |
| 106 | + | // Only elements and PCDATA sections are added to the DOM tree, no text conversions are performed. | |
| 107 | + | const unsigned int parse_minimal = 0x0000; | |
| 108 | + | ||
| 109 | + | // This flag determines if processing instructions (node_pi) are added to the DOM tree. This flag is off by default. | |
| 110 | + | const unsigned int parse_pi = 0x0001; | |
| 111 | + | ||
| 112 | + | // This flag determines if comments (node_comment) are added to the DOM tree. This flag is off by default. | |
| 113 | + | const unsigned int parse_comments = 0x0002; | |
| 114 | + | ||
| 115 | + | // This flag determines if CDATA sections (node_cdata) are added to the DOM tree. This flag is on by default. | |
| 116 | + | const unsigned int parse_cdata = 0x0004; | |
| 117 | + | ||
| 118 | + | // This flag determines if plain character data (node_pcdata) that consist only of whitespace are added to the DOM tree. | |
| 119 | + | // This flag is off by default; turning it on usually results in slower parsing and more memory consumption. | |
| 120 | + | const unsigned int parse_ws_pcdata = 0x0008; | |
| 121 | + | ||
| 122 | + | // This flag determines if character and entity references are expanded during parsing. This flag is on by default. | |
| 123 | + | const unsigned int parse_escapes = 0x0010; | |
| 124 | + | ||
| 125 | + | // This flag determines if EOL characters are normalized (converted to #xA) during parsing. This flag is on by default. | |
| 126 | + | const unsigned int parse_eol = 0x0020; | |
| 127 | + | ||
| 128 | + | // This flag determines if attribute values are normalized using CDATA normalization rules during parsing. This flag is on by default. | |
| 129 | + | const unsigned int parse_wconv_attribute = 0x0040; | |
| 130 | + | ||
| 131 | + | // This flag determines if attribute values are normalized using NMTOKENS normalization rules during parsing. This flag is off by default. | |
| 132 | + | const unsigned int parse_wnorm_attribute = 0x0080; | |
| 133 | + | ||
| 134 | + | // This flag determines if document declaration (node_declaration) is added to the DOM tree. This flag is off by default. | |
| 135 | + | const unsigned int parse_declaration = 0x0100; | |
| 136 | + | ||
| 137 | + | // This flag determines if document type declaration (node_doctype) is added to the DOM tree. This flag is off by default. | |
| 138 | + | const unsigned int parse_doctype = 0x0200; | |
| 139 | + | ||
| 140 | + | // This flag determines if plain character data (node_pcdata) that is the only child of the parent node and that consists only | |
| 141 | + | // of whitespace is added to the DOM tree. | |
| 142 | + | // This flag is off by default; turning it on may result in slower parsing and more memory consumption. | |
| 143 | + | const unsigned int parse_ws_pcdata_single = 0x0400; | |
| 144 | + | ||
| 145 | + | // The default parsing mode. | |
| 146 | + | // Elements, PCDATA and CDATA sections are added to the DOM tree, character/reference entities are expanded, | |
| 147 | + | // End-of-Line characters are normalized, attribute values are normalized using CDATA normalization rules. | |
| 148 | + | const unsigned int parse_default = parse_cdata | parse_escapes | parse_wconv_attribute | parse_eol; | |
| 149 | + | ||
| 150 | + | // The full parsing mode. | |
| 151 | + | // Nodes of all types are added to the DOM tree, character/reference entities are expanded, | |
| 152 | + | // End-of-Line characters are normalized, attribute values are normalized using CDATA normalization rules. | |
| 153 | + | const unsigned int parse_full = parse_default | parse_pi | parse_comments | parse_declaration | parse_doctype; | |
| 154 | + | ||
| 155 | + | // These flags determine the encoding of input data for XML document | |
| 156 | + | enum xml_encoding | |
| 157 | + | { | |
| 158 | + | encoding_auto, // Auto-detect input encoding using BOM or < / <? detection; use UTF8 if BOM is not found | |
| 159 | + | encoding_utf8, // UTF8 encoding | |
| 160 | + | encoding_utf16_le, // Little-endian UTF16 | |
| 161 | + | encoding_utf16_be, // Big-endian UTF16 | |
| 162 | + | encoding_utf16, // UTF16 with native endianness | |
| 163 | + | encoding_utf32_le, // Little-endian UTF32 | |
| 164 | + | encoding_utf32_be, // Big-endian UTF32 | |
| 165 | + | encoding_utf32, // UTF32 with native endianness | |
| 166 | + | encoding_wchar, // The same encoding wchar_t has (either UTF16 or UTF32) | |
| 167 | + | encoding_latin1 | |
| 168 | + | }; | |
| 169 | + | ||
| 170 | + | // Formatting flags | |
| 171 | + | ||
| 172 | + | // Indent the nodes that are written to output stream with as many indentation strings as deep the node is in DOM tree. This flag is on by default. | |
| 173 | + | const unsigned int format_indent = 0x01; | |
| 174 | + | ||
| 175 | + | // Write encoding-specific BOM to the output stream. This flag is off by default. | |
| 176 | + | const unsigned int format_write_bom = 0x02; | |
| 177 | + | ||
| 178 | + | // Use raw output mode (no indentation and no line breaks are written). This flag is off by default. | |
| 179 | + | const unsigned int format_raw = 0x04; | |
| 180 | + | ||
| 181 | + | // Omit default XML declaration even if there is no declaration in the document. This flag is off by default. | |
| 182 | + | const unsigned int format_no_declaration = 0x08; | |
| 183 | + | ||
| 184 | + | // Don't escape attribute values and PCDATA contents. This flag is off by default. | |
| 185 | + | const unsigned int format_no_escapes = 0x10; | |
| 186 | + | ||
| 187 | + | // Open file using text mode in xml_document::save_file. This enables special character (i.e. new-line) conversions on some systems. This flag is off by default. | |
| 188 | + | const unsigned int format_save_file_text = 0x20; | |
| 189 | + | ||
| 190 | + | // The default set of formatting flags. | |
| 191 | + | // Nodes are indented depending on their depth in DOM tree, a default declaration is output if document has none. | |
| 192 | + | const unsigned int format_default = format_indent; | |
| 193 | + | ||
| 194 | + | // Forward declarations | |
| 195 | + | struct xml_attribute_struct; | |
| 196 | + | struct xml_node_struct; | |
| 197 | + | ||
| 198 | + | class xml_node_iterator; | |
| 199 | + | class xml_attribute_iterator; | |
| 200 | + | class xml_named_node_iterator; | |
| 201 | + | ||
| 202 | + | class xml_tree_walker; | |
| 203 | + | ||
| 204 | + | class xml_node; | |
| 205 | + | ||
| 206 | + | class xml_text; | |
| 207 | + | ||
| 208 | + | #ifndef PUGIXML_NO_XPATH | |
| 209 | + | class xpath_node; | |
| 210 | + | class xpath_node_set; | |
| 211 | + | class xpath_query; | |
| 212 | + | class xpath_variable_set; | |
| 213 | + | #endif | |
| 214 | + | ||
| 215 | + | // Range-based for loop support | |
| 216 | + | template <typename It> class xml_object_range | |
| 217 | + | { | |
| 218 | + | public: | |
| 219 | + | typedef It const_iterator; | |
| 220 | + | ||
| 221 | + | xml_object_range(It b, It e): _begin(b), _end(e) | |
| 222 | + | { | |
| 223 | + | } | |
| 224 | + | ||
| 225 | + | It begin() const { return _begin; } | |
| 226 | + | It end() const { return _end; } | |
| 227 | + | ||
| 228 | + | private: | |
| 229 | + | It _begin, _end; | |
| 230 | + | }; | |
| 231 | + | ||
| 232 | + | // Writer interface for node printing (see xml_node::print) | |
| 233 | + | class PUGIXML_CLASS xml_writer | |
| 234 | + | { | |
| 235 | + | public: | |
| 236 | + | virtual ~xml_writer() {} | |
| 237 | + | ||
| 238 | + | // Write memory chunk into stream/file/whatever | |
| 239 | + | virtual void write(const void* data, size_t size) = 0; | |
| 240 | + | }; | |
| 241 | + | ||
| 242 | + | // xml_writer implementation for FILE* | |
| 243 | + | class PUGIXML_CLASS xml_writer_file: public xml_writer | |
| 244 | + | { | |
| 245 | + | public: | |
| 246 | + | // Construct writer from a FILE* object; void* is used to avoid header dependencies on stdio | |
| 247 | + | xml_writer_file(void* file); | |
| 248 | + | ||
| 249 | + | virtual void write(const void* data, size_t size); | |
| 250 | + | ||
| 251 | + | private: | |
| 252 | + | void* file; | |
| 253 | + | }; | |
| 254 | + | ||
| 255 | + | #ifndef PUGIXML_NO_STL | |
| 256 | + | // xml_writer implementation for streams | |
| 257 | + | class PUGIXML_CLASS xml_writer_stream: public xml_writer | |
| 258 | + | { | |
| 259 | + | public: | |
| 260 | + | // Construct writer from an output stream object | |
| 261 | + | xml_writer_stream(std::basic_ostream<char, std::char_traits<char> >& stream); | |
| 262 | + | xml_writer_stream(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream); | |
| 263 | + | ||
| 264 | + | virtual void write(const void* data, size_t size); | |
| 265 | + | ||
| 266 | + | private: | |
| 267 | + | std::basic_ostream<char, std::char_traits<char> >* narrow_stream; | |
| 268 | + | std::basic_ostream<wchar_t, std::char_traits<wchar_t> >* wide_stream; | |
| 269 | + | }; | |
| 270 | + | #endif | |
| 271 | + | ||
| 272 | + | // A light-weight handle for manipulating attributes in DOM tree | |
| 273 | + | class PUGIXML_CLASS xml_attribute | |
| 274 | + | { | |
| 275 | + | friend class xml_attribute_iterator; | |
| 276 | + | friend class xml_node; | |
| 277 | + | ||
| 278 | + | private: | |
| 279 | + | xml_attribute_struct* _attr; | |
| 280 | + | ||
| 281 | + | typedef void (*unspecified_bool_type)(xml_attribute***); | |
| 282 | + | ||
| 283 | + | public: | |
| 284 | + | // Default constructor. Constructs an empty attribute. | |
| 285 | + | xml_attribute(); | |
| 286 | + | ||
| 287 | + | // Constructs attribute from internal pointer | |
| 288 | + | explicit xml_attribute(xml_attribute_struct* attr); | |
| 289 | + | ||
| 290 | + | // Safe bool conversion operator | |
| 291 | + | operator unspecified_bool_type() const; | |
| 292 | + | ||
| 293 | + | // Borland C++ workaround | |
| 294 | + | bool operator!() const; | |
| 295 | + | ||
| 296 | + | // Comparison operators (compares wrapped attribute pointers) | |
| 297 | + | bool operator==(const xml_attribute& r) const; | |
| 298 | + | bool operator!=(const xml_attribute& r) const; | |
| 299 | + | bool operator<(const xml_attribute& r) const; | |
| 300 | + | bool operator>(const xml_attribute& r) const; | |
| 301 | + | bool operator<=(const xml_attribute& r) const; | |
| 302 | + | bool operator>=(const xml_attribute& r) const; | |
| 303 | + | ||
| 304 | + | // Check if attribute is empty | |
| 305 | + | bool empty() const; | |
| 306 | + | ||
| 307 | + | // Get attribute name/value, or "" if attribute is empty | |
| 308 | + | const char_t* name() const; | |
| 309 | + | const char_t* value() const; | |
| 310 | + | ||
| 311 | + | // Get attribute value, or the default value if attribute is empty | |
| 312 | + | const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const; | |
| 313 | + | ||
| 314 | + | // Get attribute value as a number, or the default value if conversion did not succeed or attribute is empty | |
| 315 | + | unsigned short as_ushort(unsigned short def = 0) const; | |
| 316 | + | int as_int(int def = 0) const; | |
| 317 | + | unsigned int as_uint(unsigned int def = 0) const; | |
| 318 | + | double as_double(double def = 0) const; | |
| 319 | + | float as_float(float def = 0) const; | |
| 320 | + | ||
| 321 | + | // Get attribute value as bool (returns true if first character is in '1tTyY' set), or the default value if attribute is empty | |
| 322 | + | bool as_bool(bool def = false) const; | |
| 323 | + | ||
| 324 | + | // Set attribute name/value (returns false if attribute is empty or there is not enough memory) | |
| 325 | + | bool set_name(const char_t* rhs); | |
| 326 | + | bool set_value(const char_t* rhs); | |
| 327 | + | ||
| 328 | + | // Set attribute value with type conversion (numbers are converted to strings, boolean is converted to "true"/"false") | |
| 329 | + | bool set_value(int rhs); | |
| 330 | + | bool set_value(unsigned int rhs); | |
| 331 | + | bool set_value(double rhs); | |
| 332 | + | bool set_value(bool rhs); | |
| 333 | + | ||
| 334 | + | // Set attribute value (equivalent to set_value without error checking) | |
| 335 | + | xml_attribute& operator=(const char_t* rhs); | |
| 336 | + | xml_attribute& operator=(int rhs); | |
| 337 | + | xml_attribute& operator=(unsigned int rhs); | |
| 338 | + | xml_attribute& operator=(double rhs); | |
| 339 | + | xml_attribute& operator=(bool rhs); | |
| 340 | + | ||
| 341 | + | // Get next/previous attribute in the attribute list of the parent node | |
| 342 | + | xml_attribute next_attribute() const; | |
| 343 | + | xml_attribute previous_attribute() const; | |
| 344 | + | ||
| 345 | + | // Get hash value (unique for handles to the same object) | |
| 346 | + | size_t hash_value() const; | |
| 347 | + | ||
| 348 | + | // Get internal pointer | |
| 349 | + | xml_attribute_struct* internal_object() const; | |
| 350 | + | }; | |
| 351 | + | ||
| 352 | + | #ifdef __BORLANDC__ | |
| 353 | + | // Borland C++ workaround | |
| 354 | + | bool PUGIXML_FUNCTION operator&&(const xml_attribute& lhs, bool rhs); | |
| 355 | + | bool PUGIXML_FUNCTION operator||(const xml_attribute& lhs, bool rhs); | |
| 356 | + | #endif | |
| 357 | + | ||
| 358 | + | // A light-weight handle for manipulating nodes in DOM tree | |
| 359 | + | class PUGIXML_CLASS xml_node | |
| 360 | + | { | |
| 361 | + | friend class xml_attribute_iterator; | |
| 362 | + | friend class xml_node_iterator; | |
| 363 | + | friend class xml_named_node_iterator; | |
| 364 | + | ||
| 365 | + | protected: | |
| 366 | + | xml_node_struct* _root; | |
| 367 | + | ||
| 368 | + | typedef void (*unspecified_bool_type)(xml_node***); | |
| 369 | + | ||
| 370 | + | public: | |
| 371 | + | // Default constructor. Constructs an empty node. | |
| 372 | + | xml_node(); | |
| 373 | + | ||
| 374 | + | // Constructs node from internal pointer | |
| 375 | + | explicit xml_node(xml_node_struct* p); | |
| 376 | + | ||
| 377 | + | // Safe bool conversion operator | |
| 378 | + | operator unspecified_bool_type() const; | |
| 379 | + | ||
| 380 | + | // Borland C++ workaround | |
| 381 | + | bool operator!() const; | |
| 382 | + | ||
| 383 | + | // Comparison operators (compares wrapped node pointers) | |
| 384 | + | bool operator==(const xml_node& r) const; | |
| 385 | + | bool operator!=(const xml_node& r) const; | |
| 386 | + | bool operator<(const xml_node& r) const; | |
| 387 | + | bool operator>(const xml_node& r) const; | |
| 388 | + | bool operator<=(const xml_node& r) const; | |
| 389 | + | bool operator>=(const xml_node& r) const; | |
| 390 | + | ||
| 391 | + | // Check if node is empty. | |
| 392 | + | bool empty() const; | |
| 393 | + | ||
| 394 | + | // Get node type | |
| 395 | + | xml_node_type type() const; | |
| 396 | + | ||
| 397 | + | // Get node name/value, or "" if node is empty or it has no name/value | |
| 398 | + | const char_t* name() const; | |
| 399 | + | const char_t* value() const; | |
| 400 | + | ||
| 401 | + | // Get attribute list | |
| 402 | + | xml_attribute first_attribute() const; | |
| 403 | + | xml_attribute last_attribute() const; | |
| 404 | + | ||
| 405 | + | // Get children list | |
| 406 | + | xml_node first_child() const; | |
| 407 | + | xml_node last_child() const; | |
| 408 | + | ||
| 409 | + | // Get next/previous sibling in the children list of the parent node | |
| 410 | + | xml_node next_sibling() const; | |
| 411 | + | xml_node previous_sibling() const; | |
| 412 | + | ||
| 413 | + | // Get parent node | |
| 414 | + | xml_node parent() const; | |
| 415 | + | ||
| 416 | + | // Get root of DOM tree this node belongs to | |
| 417 | + | xml_node root() const; | |
| 418 | + | ||
| 419 | + | // Get text object for the current node | |
| 420 | + | xml_text text() const; | |
| 421 | + | ||
| 422 | + | // Get child, attribute or next/previous sibling with the specified name | |
| 423 | + | xml_node child(const char_t* name) const; | |
| 424 | + | xml_attribute attribute(const char_t* name) const; | |
| 425 | + | xml_node next_sibling(const char_t* name) const; | |
| 426 | + | xml_node previous_sibling(const char_t* name) const; | |
| 427 | + | ||
| 428 | + | // Get child value of current node; that is, value of the first child node of type PCDATA/CDATA | |
| 429 | + | const char_t* child_value() const; | |
| 430 | + | ||
| 431 | + | // Get child value of child with specified name. Equivalent to child(name).child_value(). | |
| 432 | + | const char_t* child_value(const char_t* name) const; | |
| 433 | + | ||
| 434 | + | // Set node name/value (returns false if node is empty, there is not enough memory, or node can not have name/value) | |
| 435 | + | bool set_name(const char_t* rhs); | |
| 436 | + | bool set_value(const char_t* rhs); | |
| 437 | + | ||
| 438 | + | // Add attribute with specified name. Returns added attribute, or empty attribute on errors. | |
| 439 | + | xml_attribute append_attribute(const char_t* name); | |
| 440 | + | xml_attribute prepend_attribute(const char_t* name); | |
| 441 | + | xml_attribute insert_attribute_after(const char_t* name, const xml_attribute& attr); | |
| 442 | + | xml_attribute insert_attribute_before(const char_t* name, const xml_attribute& attr); | |
| 443 | + | ||
| 444 | + | // Add a copy of the specified attribute. Returns added attribute, or empty attribute on errors. | |
| 445 | + | xml_attribute append_copy(const xml_attribute& proto); | |
| 446 | + | xml_attribute prepend_copy(const xml_attribute& proto); | |
| 447 | + | xml_attribute insert_copy_after(const xml_attribute& proto, const xml_attribute& attr); | |
| 448 | + | xml_attribute insert_copy_before(const xml_attribute& proto, const xml_attribute& attr); | |
| 449 | + | ||
| 450 | + | // Add child node with specified type. Returns added node, or empty node on errors. | |
| 451 | + | xml_node append_child(xml_node_type type = node_element); | |
| 452 | + | xml_node prepend_child(xml_node_type type = node_element); | |
| 453 | + | xml_node insert_child_after(xml_node_type type, const xml_node& node); | |
| 454 | + | xml_node insert_child_before(xml_node_type type, const xml_node& node); | |
| 455 | + | ||
| 456 | + | // Add child element with specified name. Returns added node, or empty node on errors. | |
| 457 | + | xml_node append_child(const char_t* name); | |
| 458 | + | xml_node prepend_child(const char_t* name); | |
| 459 | + | xml_node insert_child_after(const char_t* name, const xml_node& node); | |
| 460 | + | xml_node insert_child_before(const char_t* name, const xml_node& node); | |
| 461 | + | ||
| 462 | + | // Add a copy of the specified node as a child. Returns added node, or empty node on errors. | |
| 463 | + | xml_node append_copy(const xml_node& proto); | |
| 464 | + | xml_node prepend_copy(const xml_node& proto); | |
| 465 | + | xml_node insert_copy_after(const xml_node& proto, const xml_node& node); | |
| 466 | + | xml_node insert_copy_before(const xml_node& proto, const xml_node& node); | |
| 467 | + | ||
| 468 | + | // Remove specified attribute | |
| 469 | + | bool remove_attribute(const xml_attribute& a); | |
| 470 | + | bool remove_attribute(const char_t* name); | |
| 471 | + | ||
| 472 | + | // Remove specified child | |
| 473 | + | bool remove_child(const xml_node& n); | |
| 474 | + | bool remove_child(const char_t* name); | |
| 475 | + | ||
| 476 | + | // Find attribute using predicate. Returns first attribute for which predicate returned true. | |
| 477 | + | template <typename Predicate> xml_attribute find_attribute(Predicate pred) const | |
| 478 | + | { | |
| 479 | + | if (!_root) return xml_attribute(); | |
| 480 | + | ||
| 481 | + | for (xml_attribute attrib = first_attribute(); attrib; attrib = attrib.next_attribute()) | |
| 482 | + | if (pred(attrib)) | |
| 483 | + | return attrib; | |
| 484 | + | ||
| 485 | + | return xml_attribute(); | |
| 486 | + | } | |
| 487 | + | ||
| 488 | + | // Find child node using predicate. Returns first child for which predicate returned true. | |
| 489 | + | template <typename Predicate> xml_node find_child(Predicate pred) const | |
| 490 | + | { | |
| 491 | + | if (!_root) return xml_node(); | |
| 492 | + | ||
| 493 | + | for (xml_node node = first_child(); node; node = node.next_sibling()) | |
| 494 | + | if (pred(node)) | |
| 495 | + | return node; | |
| 496 | + | ||
| 497 | + | return xml_node(); | |
| 498 | + | } | |
| 499 | + | ||
| 500 | + | // Find node from subtree using predicate. Returns first node from subtree (depth-first), for which predicate returned true. | |
| 501 | + | template <typename Predicate> xml_node find_node(Predicate pred) const | |
| 502 | + | { | |
| 503 | + | if (!_root) return xml_node(); | |
| 504 | + | ||
| 505 | + | xml_node cur = first_child(); | |
| 506 | + | ||
| 507 | + | while (cur._root && cur._root != _root) | |
| 508 | + | { | |
| 509 | + | if (pred(cur)) return cur; | |
| 510 | + | ||
| 511 | + | if (cur.first_child()) cur = cur.first_child(); | |
| 512 | + | else if (cur.next_sibling()) cur = cur.next_sibling(); | |
| 513 | + | else | |
| 514 | + | { | |
| 515 | + | while (!cur.next_sibling() && cur._root != _root) cur = cur.parent(); | |
| 516 | + | ||
| 517 | + | if (cur._root != _root) cur = cur.next_sibling(); | |
| 518 | + | } | |
| 519 | + | } | |
| 520 | + | ||
| 521 | + | return xml_node(); | |
| 522 | + | } | |
| 523 | + | ||
| 524 | + | // Find child node by attribute name/value | |
| 525 | + | xml_node find_child_by_attribute(const char_t* name, const char_t* attr_name, const char_t* attr_value) const; | |
| 526 | + | xml_node find_child_by_attribute(const char_t* attr_name, const char_t* attr_value) const; | |
| 527 | + | ||
| 528 | + | #ifndef PUGIXML_NO_STL | |
| 529 | + | // Get the absolute node path from root as a text string. | |
| 530 | + | string_t path(char_t delimiter = '/') const; | |
| 531 | + | #endif | |
| 532 | + | ||
| 533 | + | // Search for a node by path consisting of node names and . or .. elements. | |
| 534 | + | xml_node first_element_by_path(const char_t* path, char_t delimiter = '/') const; | |
| 535 | + | ||
| 536 | + | // Recursively traverse subtree with xml_tree_walker | |
| 537 | + | bool traverse(xml_tree_walker& walker); | |
| 538 | + | ||
| 539 | + | #ifndef PUGIXML_NO_XPATH | |
| 540 | + | // Select single node by evaluating XPath query. Returns first node from the resulting node set. | |
| 541 | + | xpath_node select_single_node(const char_t* query, xpath_variable_set* variables = 0) const; | |
| 542 | + | xpath_node select_single_node(const xpath_query& query) const; | |
| 543 | + | ||
| 544 | + | // Select node set by evaluating XPath query | |
| 545 | + | xpath_node_set select_nodes(const char_t* query, xpath_variable_set* variables = 0) const; | |
| 546 | + | xpath_node_set select_nodes(const xpath_query& query) const; | |
| 547 | + | #endif | |
| 548 | + | ||
| 549 | + | // Print subtree using a writer object | |
| 550 | + | void print(xml_writer& writer, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto, unsigned int depth = 0) const; | |
| 551 | + | ||
| 552 | + | #ifndef PUGIXML_NO_STL | |
| 553 | + | // Print subtree to stream | |
| 554 | + | void print(std::basic_ostream<char, std::char_traits<char> >& os, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto, unsigned int depth = 0) const; | |
| 555 | + | void print(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& os, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, unsigned int depth = 0) const; | |
| 556 | + | #endif | |
| 557 | + | ||
| 558 | + | // Child nodes iterators | |
| 559 | + | typedef xml_node_iterator iterator; | |
| 560 | + | ||
| 561 | + | iterator begin() const; | |
| 562 | + | iterator end() const; | |
| 563 | + | ||
| 564 | + | // Attribute iterators | |
| 565 | + | typedef xml_attribute_iterator attribute_iterator; | |
| 566 | + | ||
| 567 | + | attribute_iterator attributes_begin() const; | |
| 568 | + | attribute_iterator attributes_end() const; | |
| 569 | + | ||
| 570 | + | // Range-based for support | |
| 571 | + | xml_object_range<xml_node_iterator> children() const; | |
| 572 | + | xml_object_range<xml_named_node_iterator> children(const char_t* name) const; | |
| 573 | + | xml_object_range<xml_attribute_iterator> attributes() const; | |
| 574 | + | ||
| 575 | + | // Get node offset in parsed file/string (in char_t units) for debugging purposes | |
| 576 | + | ptrdiff_t offset_debug() const; | |
| 577 | + | ||
| 578 | + | // Get hash value (unique for handles to the same object) | |
| 579 | + | size_t hash_value() const; | |
| 580 | + | ||
| 581 | + | // Get internal pointer | |
| 582 | + | xml_node_struct* internal_object() const; | |
| 583 | + | }; | |
| 584 | + | ||
| 585 | + | #ifdef __BORLANDC__ | |
| 586 | + | // Borland C++ workaround | |
| 587 | + | bool PUGIXML_FUNCTION operator&&(const xml_node& lhs, bool rhs); | |
| 588 | + | bool PUGIXML_FUNCTION operator||(const xml_node& lhs, bool rhs); | |
| 589 | + | #endif | |
| 590 | + | ||
| 591 | + | // A helper for working with text inside PCDATA nodes | |
| 592 | + | class PUGIXML_CLASS xml_text | |
| 593 | + | { | |
| 594 | + | friend class xml_node; | |
| 595 | + | ||
| 596 | + | xml_node_struct* _root; | |
| 597 | + | ||
| 598 | + | typedef void (*unspecified_bool_type)(xml_text***); | |
| 599 | + | ||
| 600 | + | explicit xml_text(xml_node_struct* root); | |
| 601 | + | ||
| 602 | + | xml_node_struct* _data_new(); | |
| 603 | + | xml_node_struct* _data() const; | |
| 604 | + | ||
| 605 | + | public: | |
| 606 | + | // Default constructor. Constructs an empty object. | |
| 607 | + | xml_text(); | |
| 608 | + | ||
| 609 | + | // Safe bool conversion operator | |
| 610 | + | operator unspecified_bool_type() const; | |
| 611 | + | ||
| 612 | + | // Borland C++ workaround | |
| 613 | + | bool operator!() const; | |
| 614 | + | ||
| 615 | + | // Check if text object is empty | |
| 616 | + | bool empty() const; | |
| 617 | + | ||
| 618 | + | // Get text, or "" if object is empty | |
| 619 | + | const char_t* get() const; | |
| 620 | + | ||
| 621 | + | // Get text, or the default value if object is empty | |
| 622 | + | const char_t* as_string(const char_t* def = PUGIXML_TEXT("")) const; | |
| 623 | + | ||
| 624 | + | // Get text as a number, or the default value if conversion did not succeed or object is empty | |
| 625 | + | int as_int(int def = 0) const; | |
| 626 | + | unsigned int as_uint(unsigned int def = 0) const; | |
| 627 | + | double as_double(double def = 0) const; | |
| 628 | + | float as_float(float def = 0) const; | |
| 629 | + | ||
| 630 | + | // Get text as bool (returns true if first character is in '1tTyY' set), or the default value if object is empty | |
| 631 | + | bool as_bool(bool def = false) const; | |
| 632 | + | ||
| 633 | + | // Set text (returns false if object is empty or there is not enough memory) | |
| 634 | + | bool set(const char_t* rhs); | |
| 635 | + | ||
| 636 | + | // Set text with type conversion (numbers are converted to strings, boolean is converted to "true"/"false") | |
| 637 | + | bool set(int rhs); | |
| 638 | + | bool set(unsigned int rhs); | |
| 639 | + | bool set(double rhs); | |
| 640 | + | bool set(bool rhs); | |
| 641 | + | ||
| 642 | + | // Set text (equivalent to set without error checking) | |
| 643 | + | xml_text& operator=(const char_t* rhs); | |
| 644 | + | xml_text& operator=(int rhs); | |
| 645 | + | xml_text& operator=(unsigned int rhs); | |
| 646 | + | xml_text& operator=(double rhs); | |
| 647 | + | xml_text& operator=(bool rhs); | |
| 648 | + | ||
| 649 | + | // Get the data node (node_pcdata or node_cdata) for this object | |
| 650 | + | xml_node data() const; | |
| 651 | + | }; | |
| 652 | + | ||
| 653 | + | #ifdef __BORLANDC__ | |
| 654 | + | // Borland C++ workaround | |
| 655 | + | bool PUGIXML_FUNCTION operator&&(const xml_text& lhs, bool rhs); | |
| 656 | + | bool PUGIXML_FUNCTION operator||(const xml_text& lhs, bool rhs); | |
| 657 | + | #endif | |
| 658 | + | ||
| 659 | + | // Child node iterator (a bidirectional iterator over a collection of xml_node) | |
| 660 | + | class PUGIXML_CLASS xml_node_iterator | |
| 661 | + | { | |
| 662 | + | friend class xml_node; | |
| 663 | + | ||
| 664 | + | private: | |
| 665 | + | mutable xml_node _wrap; | |
| 666 | + | xml_node _parent; | |
| 667 | + | ||
| 668 | + | xml_node_iterator(xml_node_struct* ref, xml_node_struct* parent); | |
| 669 | + | ||
| 670 | + | public: | |
| 671 | + | // Iterator traits | |
| 672 | + | typedef ptrdiff_t difference_type; | |
| 673 | + | typedef xml_node value_type; | |
| 674 | + | typedef xml_node* pointer; | |
| 675 | + | typedef xml_node& reference; | |
| 676 | + | ||
| 677 | + | #ifndef PUGIXML_NO_STL | |
| 678 | + | typedef std::bidirectional_iterator_tag iterator_category; | |
| 679 | + | #endif | |
| 680 | + | ||
| 681 | + | // Default constructor | |
| 682 | + | xml_node_iterator(); | |
| 683 | + | ||
| 684 | + | // Construct an iterator which points to the specified node | |
| 685 | + | xml_node_iterator(const xml_node& node); | |
| 686 | + | ||
| 687 | + | // Iterator operators | |
| 688 | + | bool operator==(const xml_node_iterator& rhs) const; | |
| 689 | + | bool operator!=(const xml_node_iterator& rhs) const; | |
| 690 | + | ||
| 691 | + | xml_node& operator*() const; | |
| 692 | + | xml_node* operator->() const; | |
| 693 | + | ||
| 694 | + | const xml_node_iterator& operator++(); | |
| 695 | + | xml_node_iterator operator++(int); | |
| 696 | + | ||
| 697 | + | const xml_node_iterator& operator--(); | |
| 698 | + | xml_node_iterator operator--(int); | |
| 699 | + | }; | |
| 700 | + | ||
| 701 | + | // Attribute iterator (a bidirectional iterator over a collection of xml_attribute) | |
| 702 | + | class PUGIXML_CLASS xml_attribute_iterator | |
| 703 | + | { | |
| 704 | + | friend class xml_node; | |
| 705 | + | ||
| 706 | + | private: | |
| 707 | + | mutable xml_attribute _wrap; | |
| 708 | + | xml_node _parent; | |
| 709 | + | ||
| 710 | + | xml_attribute_iterator(xml_attribute_struct* ref, xml_node_struct* parent); | |
| 711 | + | ||
| 712 | + | public: | |
| 713 | + | // Iterator traits | |
| 714 | + | typedef ptrdiff_t difference_type; | |
| 715 | + | typedef xml_attribute value_type; | |
| 716 | + | typedef xml_attribute* pointer; | |
| 717 | + | typedef xml_attribute& reference; | |
| 718 | + | ||
| 719 | + | #ifndef PUGIXML_NO_STL | |
| 720 | + | typedef std::bidirectional_iterator_tag iterator_category; | |
| 721 | + | #endif | |
| 722 | + | ||
| 723 | + | // Default constructor | |
| 724 | + | xml_attribute_iterator(); | |
| 725 | + | ||
| 726 | + | // Construct an iterator which points to the specified attribute | |
| 727 | + | xml_attribute_iterator(const xml_attribute& attr, const xml_node& parent); | |
| 728 | + | ||
| 729 | + | // Iterator operators | |
| 730 | + | bool operator==(const xml_attribute_iterator& rhs) const; | |
| 731 | + | bool operator!=(const xml_attribute_iterator& rhs) const; | |
| 732 | + | ||
| 733 | + | xml_attribute& operator*() const; | |
| 734 | + | xml_attribute* operator->() const; | |
| 735 | + | ||
| 736 | + | const xml_attribute_iterator& operator++(); | |
| 737 | + | xml_attribute_iterator operator++(int); | |
| 738 | + | ||
| 739 | + | const xml_attribute_iterator& operator--(); | |
| 740 | + | xml_attribute_iterator operator--(int); | |
| 741 | + | }; | |
| 742 | + | ||
| 743 | + | // Named node range helper | |
| 744 | + | class xml_named_node_iterator | |
| 745 | + | { | |
| 746 | + | public: | |
| 747 | + | // Iterator traits | |
| 748 | + | typedef ptrdiff_t difference_type; | |
| 749 | + | typedef xml_node value_type; | |
| 750 | + | typedef xml_node* pointer; | |
| 751 | + | typedef xml_node& reference; | |
| 752 | + | ||
| 753 | + | #ifndef PUGIXML_NO_STL | |
| 754 | + | typedef std::forward_iterator_tag iterator_category; | |
| 755 | + | #endif | |
| 756 | + | ||
| 757 | + | // Default constructor | |
| 758 | + | xml_named_node_iterator(); | |
| 759 | + | ||
| 760 | + | // Construct an iterator which points to the specified node | |
| 761 | + | xml_named_node_iterator(const xml_node& node, const char_t* name); | |
| 762 | + | ||
| 763 | + | // Iterator operators | |
| 764 | + | bool operator==(const xml_named_node_iterator& rhs) const; | |
| 765 | + | bool operator!=(const xml_named_node_iterator& rhs) const; | |
| 766 | + | ||
| 767 | + | xml_node& operator*() const; | |
| 768 | + | xml_node* operator->() const; | |
| 769 | + | ||
| 770 | + | const xml_named_node_iterator& operator++(); | |
| 771 | + | xml_named_node_iterator operator++(int); | |
| 772 | + | ||
| 773 | + | private: | |
| 774 | + | mutable xml_node _node; | |
| 775 | + | const char_t* _name; | |
| 776 | + | }; | |
| 777 | + | ||
| 778 | + | // Abstract tree walker class (see xml_node::traverse) | |
| 779 | + | class PUGIXML_CLASS xml_tree_walker | |
| 780 | + | { | |
| 781 | + | friend class xml_node; | |
| 782 | + | ||
| 783 | + | private: | |
| 784 | + | int _depth; | |
| 785 | + | ||
| 786 | + | protected: | |
| 787 | + | // Get current traversal depth | |
| 788 | + | int depth() const; | |
| 789 | + | ||
| 790 | + | public: | |
| 791 | + | xml_tree_walker(); | |
| 792 | + | virtual ~xml_tree_walker(); | |
| 793 | + | ||
| 794 | + | // Callback that is called when traversal begins | |
| 795 | + | virtual bool begin(xml_node& node); | |
| 796 | + | ||
| 797 | + | // Callback that is called for each node traversed | |
| 798 | + | virtual bool for_each(xml_node& node) = 0; | |
| 799 | + | ||
| 800 | + | // Callback that is called when traversal ends | |
| 801 | + | virtual bool end(xml_node& node); | |
| 802 | + | }; | |
| 803 | + | ||
| 804 | + | // Parsing status, returned as part of xml_parse_result object | |
| 805 | + | enum xml_parse_status | |
| 806 | + | { | |
| 807 | + | status_ok = 0, // No error | |
| 808 | + | ||
| 809 | + | status_file_not_found, // File was not found during load_file() | |
| 810 | + | status_io_error, // Error reading from file/stream | |
| 811 | + | status_out_of_memory, // Could not allocate memory | |
| 812 | + | status_internal_error, // Internal error occurred | |
| 813 | + | ||
| 814 | + | status_unrecognized_tag, // Parser could not determine tag type | |
| 815 | + | ||
| 816 | + | status_bad_pi, // Parsing error occurred while parsing document declaration/processing instruction | |
| 817 | + | status_bad_comment, // Parsing error occurred while parsing comment | |
| 818 | + | status_bad_cdata, // Parsing error occurred while parsing CDATA section | |
| 819 | + | status_bad_doctype, // Parsing error occurred while parsing document type declaration | |
| 820 | + | status_bad_pcdata, // Parsing error occurred while parsing PCDATA section | |
| 821 | + | status_bad_start_element, // Parsing error occurred while parsing start element tag | |
| 822 | + | status_bad_attribute, // Parsing error occurred while parsing element attribute | |
| 823 | + | status_bad_end_element, // Parsing error occurred while parsing end element tag | |
| 824 | + | status_end_element_mismatch // There was a mismatch of start-end tags (closing tag had incorrect name, some tag was not closed or there was an excessive closing tag) | |
| 825 | + | }; | |
| 826 | + | ||
| 827 | + | // Parsing result | |
| 828 | + | struct PUGIXML_CLASS xml_parse_result | |
| 829 | + | { | |
| 830 | + | // Parsing status (see xml_parse_status) | |
| 831 | + | xml_parse_status status; | |
| 832 | + | ||
| 833 | + | // Last parsed offset (in char_t units from start of input data) | |
| 834 | + | ptrdiff_t offset; | |
| 835 | + | ||
| 836 | + | // Source document encoding | |
| 837 | + | xml_encoding encoding; | |
| 838 | + | ||
| 839 | + | // Default constructor, initializes object to failed state | |
| 840 | + | xml_parse_result(); | |
| 841 | + | ||
| 842 | + | // Cast to bool operator | |
| 843 | + | operator bool() const; | |
| 844 | + | ||
| 845 | + | // Get error description | |
| 846 | + | const char* description() const; | |
| 847 | + | }; | |
| 848 | + | ||
| 849 | + | // Document class (DOM tree root) | |
| 850 | + | class PUGIXML_CLASS xml_document: public xml_node | |
| 851 | + | { | |
| 852 | + | private: | |
| 853 | + | char_t* _buffer; | |
| 854 | + | ||
| 855 | + | char _memory[192]; | |
| 856 | + | ||
| 857 | + | // Non-copyable semantics | |
| 858 | + | xml_document(const xml_document&); | |
| 859 | + | const xml_document& operator=(const xml_document&); | |
| 860 | + | ||
| 861 | + | void create(); | |
| 862 | + | void destroy(); | |
| 863 | + | ||
| 864 | + | xml_parse_result load_buffer_impl(void* contents, size_t size, unsigned int options, xml_encoding encoding, bool is_mutable, bool own); | |
| 865 | + | ||
| 866 | + | public: | |
| 867 | + | // Default constructor, makes empty document | |
| 868 | + | xml_document(); | |
| 869 | + | ||
| 870 | + | // Destructor, invalidates all node/attribute handles to this document | |
| 871 | + | ~xml_document(); | |
| 872 | + | ||
| 873 | + | // Removes all nodes, leaving the empty document | |
| 874 | + | void reset(); | |
| 875 | + | ||
| 876 | + | // Removes all nodes, then copies the entire contents of the specified document | |
| 877 | + | void reset(const xml_document& proto); | |
| 878 | + | ||
| 879 | + | #ifndef PUGIXML_NO_STL | |
| 880 | + | // Load document from stream. | |
| 881 | + | xml_parse_result load(std::basic_istream<char, std::char_traits<char> >& stream, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); | |
| 882 | + | xml_parse_result load(std::basic_istream<wchar_t, std::char_traits<wchar_t> >& stream, unsigned int options = parse_default); | |
| 883 | + | #endif | |
| 884 | + | ||
| 885 | + | // Load document from zero-terminated string. No encoding conversions are applied. | |
| 886 | + | xml_parse_result load(const char_t* contents, unsigned int options = parse_default); | |
| 887 | + | ||
| 888 | + | // Load document from file | |
| 889 | + | xml_parse_result load_file(const char* path, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); | |
| 890 | + | xml_parse_result load_file(const wchar_t* path, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); | |
| 891 | + | ||
| 892 | + | // Load document from buffer. Copies/converts the buffer, so it may be deleted or changed after the function returns. | |
| 893 | + | xml_parse_result load_buffer(const void* contents, size_t size, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); | |
| 894 | + | ||
| 895 | + | // Load document from buffer, using the buffer for in-place parsing (the buffer is modified and used for storage of document data). | |
| 896 | + | // You should ensure that buffer data will persist throughout the document's lifetime, and free the buffer memory manually once document is destroyed. | |
| 897 | + | xml_parse_result load_buffer_inplace(void* contents, size_t size, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); | |
| 898 | + | ||
| 899 | + | // Load document from buffer, using the buffer for in-place parsing (the buffer is modified and used for storage of document data). | |
| 900 | + | // You should allocate the buffer with pugixml allocation function; document will free the buffer when it is no longer needed (you can't use it anymore). | |
| 901 | + | xml_parse_result load_buffer_inplace_own(void* contents, size_t size, unsigned int options = parse_default, xml_encoding encoding = encoding_auto); | |
| 902 | + | ||
| 903 | + | // Save XML document to writer (semantics is slightly different from xml_node::print, see documentation for details). | |
| 904 | + | void save(xml_writer& writer, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto) const; | |
| 905 | + | ||
| 906 | + | #ifndef PUGIXML_NO_STL | |
| 907 | + | // Save XML document to stream (semantics is slightly different from xml_node::print, see documentation for details). | |
| 908 | + | void save(std::basic_ostream<char, std::char_traits<char> >& stream, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto) const; | |
| 909 | + | void save(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default) const; | |
| 910 | + | #endif | |
| 911 | + | ||
| 912 | + | // Save XML to file | |
| 913 | + | bool save_file(const char* path, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto) const; | |
| 914 | + | bool save_file(const wchar_t* path, const char_t* indent = PUGIXML_TEXT("\t"), unsigned int flags = format_default, xml_encoding encoding = encoding_auto) const; | |
| 915 | + | ||
| 916 | + | // Get document element | |
| 917 | + | xml_node document_element() const; | |
| 918 | + | }; | |
| 919 | + | ||
| 920 | + | #ifndef PUGIXML_NO_XPATH | |
| 921 | + | // XPath query return type | |
| 922 | + | enum xpath_value_type | |
| 923 | + | { | |
| 924 | + | xpath_type_none, // Unknown type (query failed to compile) | |
| 925 | + | xpath_type_node_set, // Node set (xpath_node_set) | |
| 926 | + | xpath_type_number, // Number | |
| 927 | + | xpath_type_string, // String | |
| 928 | + | xpath_type_boolean // Boolean | |
| 929 | + | }; | |
| 930 | + | ||
| 931 | + | // XPath parsing result | |
| 932 | + | struct PUGIXML_CLASS xpath_parse_result | |
| 933 | + | { | |
| 934 | + | // Error message (0 if no error) | |
| 935 | + | const char* error; | |
| 936 | + | ||
| 937 | + | // Last parsed offset (in char_t units from string start) | |
| 938 | + | ptrdiff_t offset; | |
| 939 | + | ||
| 940 | + | // Default constructor, initializes object to failed state | |
| 941 | + | xpath_parse_result(); | |
| 942 | + | ||
| 943 | + | // Cast to bool operator | |
| 944 | + | operator bool() const; | |
| 945 | + | ||
| 946 | + | // Get error description | |
| 947 | + | const char* description() const; | |
| 948 | + | }; | |
| 949 | + | ||
| 950 | + | // A single XPath variable | |
| 951 | + | class PUGIXML_CLASS xpath_variable | |
| 952 | + | { | |
| 953 | + | friend class xpath_variable_set; | |
| 954 | + | ||
| 955 | + | protected: | |
| 956 | + | xpath_value_type _type; | |
| 957 | + | xpath_variable* _next; | |
| 958 | + | ||
| 959 | + | xpath_variable(); | |
| 960 | + | ||
| 961 | + | // Non-copyable semantics | |
| 962 | + | xpath_variable(const xpath_variable&); | |
| 963 | + | xpath_variable& operator=(const xpath_variable&); | |
| 964 | + | ||
| 965 | + | public: | |
| 966 | + | // Get variable name | |
| 967 | + | const char_t* name() const; | |
| 968 | + | ||
| 969 | + | // Get variable type | |
| 970 | + | xpath_value_type type() const; | |
| 971 | + | ||
| 972 | + | // Get variable value; no type conversion is performed, default value (false, NaN, empty string, empty node set) is returned on type mismatch error | |
| 973 | + | bool get_boolean() const; | |
| 974 | + | double get_number() const; | |
| 975 | + | const char_t* get_string() const; | |
| 976 | + | const xpath_node_set& get_node_set() const; | |
| 977 | + | ||
| 978 | + | // Set variable value; no type conversion is performed, false is returned on type mismatch error | |
| 979 | + | bool set(bool value); | |
| 980 | + | bool set(double value); | |
| 981 | + | bool set(const char_t* value); | |
| 982 | + | bool set(const xpath_node_set& value); | |
| 983 | + | }; | |
| 984 | + | ||
| 985 | + | // A set of XPath variables | |
| 986 | + | class PUGIXML_CLASS xpath_variable_set | |
| 987 | + | { | |
| 988 | + | private: | |
| 989 | + | xpath_variable* _data[64]; | |
| 990 | + | ||
| 991 | + | // Non-copyable semantics | |
| 992 | + | xpath_variable_set(const xpath_variable_set&); | |
| 993 | + | xpath_variable_set& operator=(const xpath_variable_set&); | |
| 994 | + | ||
| 995 | + | xpath_variable* find(const char_t* name) const; | |
| 996 | + | ||
| 997 | + | public: | |
| 998 | + | // Default constructor/destructor | |
| 999 | + | xpath_variable_set(); | |
| 1000 | + | ~xpath_variable_set(); | |
| 1001 | + | ||
| 1002 | + | // Add a new variable or get the existing one, if the types match | |
| 1003 | + | xpath_variable* add(const char_t* name, xpath_value_type type); | |
| 1004 | + | ||
| 1005 | + | // Set value of an existing variable; no type conversion is performed, false is returned if there is no such variable or if types mismatch | |
| 1006 | + | bool set(const char_t* name, bool value); | |
| 1007 | + | bool set(const char_t* name, double value); | |
| 1008 | + | bool set(const char_t* name, const char_t* value); | |
| 1009 | + | bool set(const char_t* name, const xpath_node_set& value); | |
| 1010 | + | ||
| 1011 | + | // Get existing variable by name | |
| 1012 | + | xpath_variable* get(const char_t* name); | |
| 1013 | + | const xpath_variable* get(const char_t* name) const; | |
| 1014 | + | }; | |
| 1015 | + | ||
| 1016 | + | // A compiled XPath query object | |
| 1017 | + | class PUGIXML_CLASS xpath_query | |
| 1018 | + | { | |
| 1019 | + | private: | |
| 1020 | + | void* _impl; | |
| 1021 | + | xpath_parse_result _result; | |
| 1022 | + | ||
| 1023 | + | typedef void (*unspecified_bool_type)(xpath_query***); | |
| 1024 | + | ||
| 1025 | + | // Non-copyable semantics | |
| 1026 | + | xpath_query(const xpath_query&); | |
| 1027 | + | xpath_query& operator=(const xpath_query&); | |
| 1028 | + | ||
| 1029 | + | public: | |
| 1030 | + | // Construct a compiled object from XPath expression. | |
| 1031 | + | // If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on compilation errors. | |
| 1032 | + | explicit xpath_query(const char_t* query, xpath_variable_set* variables = 0); | |
| 1033 | + | ||
| 1034 | + | // Destructor | |
| 1035 | + | ~xpath_query(); | |
| 1036 | + | ||
| 1037 | + | // Get query expression return type | |
| 1038 | + | xpath_value_type return_type() const; | |
| 1039 | + | ||
| 1040 | + | // Evaluate expression as boolean value in the specified context; performs type conversion if necessary. | |
| 1041 | + | // If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors. | |
| 1042 | + | bool evaluate_boolean(const xpath_node& n) const; | |
| 1043 | + | ||
| 1044 | + | // Evaluate expression as double value in the specified context; performs type conversion if necessary. | |
| 1045 | + | // If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors. | |
| 1046 | + | double evaluate_number(const xpath_node& n) const; | |
| 1047 | + | ||
| 1048 | + | #ifndef PUGIXML_NO_STL | |
| 1049 | + | // Evaluate expression as string value in the specified context; performs type conversion if necessary. | |
| 1050 | + | // If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors. | |
| 1051 | + | string_t evaluate_string(const xpath_node& n) const; | |
| 1052 | + | #endif | |
| 1053 | + | ||
| 1054 | + | // Evaluate expression as string value in the specified context; performs type conversion if necessary. | |
| 1055 | + | // At most capacity characters are written to the destination buffer, full result size is returned (includes terminating zero). | |
| 1056 | + | // If PUGIXML_NO_EXCEPTIONS is not defined, throws std::bad_alloc on out of memory errors. | |
| 1057 | + | // If PUGIXML_NO_EXCEPTIONS is defined, returns empty set instead. | |
| 1058 | + | size_t evaluate_string(char_t* buffer, size_t capacity, const xpath_node& n) const; | |
| 1059 | + | ||
| 1060 | + | // Evaluate expression as node set in the specified context. | |
| 1061 | + | // If PUGIXML_NO_EXCEPTIONS is not defined, throws xpath_exception on type mismatch and std::bad_alloc on out of memory errors. | |
| 1062 | + | // If PUGIXML_NO_EXCEPTIONS is defined, returns empty node set instead. | |
| 1063 | + | xpath_node_set evaluate_node_set(const xpath_node& n) const; | |
| 1064 | + | ||
| 1065 | + | // Get parsing result (used to get compilation errors in PUGIXML_NO_EXCEPTIONS mode) | |
| 1066 | + | const xpath_parse_result& result() const; | |
| 1067 | + | ||
| 1068 | + | // Safe bool conversion operator | |
| 1069 | + | operator unspecified_bool_type() const; | |
| 1070 | + | ||
| 1071 | + | // Borland C++ workaround | |
| 1072 | + | bool operator!() const; | |
| 1073 | + | }; | |
| 1074 | + | ||
| 1075 | + | #ifndef PUGIXML_NO_EXCEPTIONS | |
| 1076 | + | // XPath exception class | |
| 1077 | + | class PUGIXML_CLASS xpath_exception: public std::exception | |
| 1078 | + | { | |
| 1079 | + | private: | |
| 1080 | + | xpath_parse_result _result; | |
| 1081 | + | ||
| 1082 | + | public: | |
| 1083 | + | // Construct exception from parse result | |
| 1084 | + | explicit xpath_exception(const xpath_parse_result& result); | |
| 1085 | + | ||
| 1086 | + | // Get error message | |
| 1087 | + | virtual const char* what() const throw(); | |
| 1088 | + | ||
| 1089 | + | // Get parse result | |
| 1090 | + | const xpath_parse_result& result() const; | |
| 1091 | + | }; | |
| 1092 | + | #endif | |
| 1093 | + | ||
| 1094 | + | // XPath node class (either xml_node or xml_attribute) | |
| 1095 | + | class PUGIXML_CLASS xpath_node | |
| 1096 | + | { | |
| 1097 | + | private: | |
| 1098 | + | xml_node _node; | |
| 1099 | + | xml_attribute _attribute; | |
| 1100 | + | ||
| 1101 | + | typedef void (*unspecified_bool_type)(xpath_node***); | |
| 1102 | + | ||
| 1103 | + | public: | |
| 1104 | + | // Default constructor; constructs empty XPath node | |
| 1105 | + | xpath_node(); | |
| 1106 | + | ||
| 1107 | + | // Construct XPath node from XML node/attribute | |
| 1108 | + | xpath_node(const xml_node& node); | |
| 1109 | + | xpath_node(const xml_attribute& attribute, const xml_node& parent); | |
| 1110 | + | ||
| 1111 | + | // Get node/attribute, if any | |
| 1112 | + | xml_node node() const; | |
| 1113 | + | xml_attribute attribute() const; | |
| 1114 | + | ||
| 1115 | + | // Get parent of contained node/attribute | |
| 1116 | + | xml_node parent() const; | |
| 1117 | + | ||
| 1118 | + | // Safe bool conversion operator | |
| 1119 | + | operator unspecified_bool_type() const; | |
| 1120 | + | ||
| 1121 | + | // Borland C++ workaround | |
| 1122 | + | bool operator!() const; | |
| 1123 | + | ||
| 1124 | + | // Comparison operators | |
| 1125 | + | bool operator==(const xpath_node& n) const; | |
| 1126 | + | bool operator!=(const xpath_node& n) const; | |
| 1127 | + | }; | |
| 1128 | + | ||
| 1129 | + | #ifdef __BORLANDC__ | |
| 1130 | + | // Borland C++ workaround | |
| 1131 | + | bool PUGIXML_FUNCTION operator&&(const xpath_node& lhs, bool rhs); | |
| 1132 | + | bool PUGIXML_FUNCTION operator||(const xpath_node& lhs, bool rhs); | |
| 1133 | + | #endif | |
| 1134 | + | ||
| 1135 | + | // A fixed-size collection of XPath nodes | |
| 1136 | + | class PUGIXML_CLASS xpath_node_set | |
| 1137 | + | { | |
| 1138 | + | public: | |
| 1139 | + | // Collection type | |
| 1140 | + | enum type_t | |
| 1141 | + | { | |
| 1142 | + | type_unsorted, // Not ordered | |
| 1143 | + | type_sorted, // Sorted by document order (ascending) | |
| 1144 | + | type_sorted_reverse // Sorted by document order (descending) | |
| 1145 | + | }; | |
| 1146 | + | ||
| 1147 | + | // Constant iterator type | |
| 1148 | + | typedef const xpath_node* const_iterator; | |
| 1149 | + | ||
| 1150 | + | // Default constructor. Constructs empty set. | |
| 1151 | + | xpath_node_set(); | |
| 1152 | + | ||
| 1153 | + | // Constructs a set from iterator range; data is not checked for duplicates and is not sorted according to provided type, so be careful | |
| 1154 | + | xpath_node_set(const_iterator begin, const_iterator end, type_t type = type_unsorted); | |
| 1155 | + | ||
| 1156 | + | // Destructor | |
| 1157 | + | ~xpath_node_set(); | |
| 1158 | + | ||
| 1159 | + | // Copy constructor/assignment operator | |
| 1160 | + | xpath_node_set(const xpath_node_set& ns); | |
| 1161 | + | xpath_node_set& operator=(const xpath_node_set& ns); | |
| 1162 | + | ||
| 1163 | + | // Get collection type | |
| 1164 | + | type_t type() const; | |
| 1165 | + | ||
| 1166 | + | // Get collection size | |
| 1167 | + | size_t size() const; | |
| 1168 | + | ||
| 1169 | + | // Indexing operator | |
| 1170 | + | const xpath_node& operator[](size_t index) const; | |
| 1171 | + | ||
| 1172 | + | // Collection iterators | |
| 1173 | + | const_iterator begin() const; | |
| 1174 | + | const_iterator end() const; | |
| 1175 | + | ||
| 1176 | + | // Sort the collection in ascending/descending order by document order | |
| 1177 | + | void sort(bool reverse = false); | |
| 1178 | + | ||
| 1179 | + | // Get first node in the collection by document order | |
| 1180 | + | xpath_node first() const; | |
| 1181 | + | ||
| 1182 | + | // Check if collection is empty | |
| 1183 | + | bool empty() const; | |
| 1184 | + | ||
| 1185 | + | private: | |
| 1186 | + | type_t _type; | |
| 1187 | + | ||
| 1188 | + | xpath_node _storage; | |
| 1189 | + | ||
| 1190 | + | xpath_node* _begin; | |
| 1191 | + | xpath_node* _end; | |
| 1192 | + | ||
| 1193 | + | void _assign(const_iterator begin, const_iterator end); | |
| 1194 | + | }; | |
| 1195 | + | #endif | |
| 1196 | + | ||
| 1197 | + | #ifndef PUGIXML_NO_STL | |
| 1198 | + | // Convert wide string to UTF8 | |
| 1199 | + | std::basic_string<char, std::char_traits<char>, std::allocator<char> > PUGIXML_FUNCTION as_utf8(const wchar_t* str); | |
| 1200 | + | std::basic_string<char, std::char_traits<char>, std::allocator<char> > PUGIXML_FUNCTION as_utf8(const std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >& str); | |
| 1201 | + | ||
| 1202 | + | // Convert UTF8 to wide string | |
| 1203 | + | std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > PUGIXML_FUNCTION as_wide(const char* str); | |
| 1204 | + | std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > PUGIXML_FUNCTION as_wide(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >& str); | |
| 1205 | + | #endif | |
| 1206 | + | ||
| 1207 | + | // Memory allocation function interface; returns pointer to allocated memory or NULL on failure | |
| 1208 | + | typedef void* (*allocation_function)(size_t size); | |
| 1209 | + | ||
| 1210 | + | // Memory deallocation function interface | |
| 1211 | + | typedef void (*deallocation_function)(void* ptr); | |
| 1212 | + | ||
| 1213 | + | // Override default memory management functions. All subsequent allocations/deallocations will be performed via supplied functions. | |
| 1214 | + | void PUGIXML_FUNCTION set_memory_management_functions(allocation_function allocate, deallocation_function deallocate); | |
| 1215 | + | ||
| 1216 | + | // Get current memory management functions | |
| 1217 | + | allocation_function PUGIXML_FUNCTION get_memory_allocation_function(); | |
| 1218 | + | deallocation_function PUGIXML_FUNCTION get_memory_deallocation_function(); | |
| 1219 | + | } | |
| 1220 | + | ||
| 1221 | + | #if !defined(PUGIXML_NO_STL) && (defined(_MSC_VER) || defined(__ICC)) | |
| 1222 | + | namespace std | |
| 1223 | + | { | |
| 1224 | + | // Workarounds for (non-standard) iterator category detection for older versions (MSVC7/IC8 and earlier) | |
| 1225 | + | std::bidirectional_iterator_tag PUGIXML_FUNCTION _Iter_cat(const pugi::xml_node_iterator&); | |
| 1226 | + | std::bidirectional_iterator_tag PUGIXML_FUNCTION _Iter_cat(const pugi::xml_attribute_iterator&); | |
| 1227 | + | std::forward_iterator_tag PUGIXML_FUNCTION _Iter_cat(const pugi::xml_named_node_iterator&); | |
| 1228 | + | } | |
| 1229 | + | #endif | |
| 1230 | + | ||
| 1231 | + | #if !defined(PUGIXML_NO_STL) && defined(__SUNPRO_CC) | |
| 1232 | + | namespace std | |
| 1233 | + | { | |
| 1234 | + | // Workarounds for (non-standard) iterator category detection | |
| 1235 | + | std::bidirectional_iterator_tag PUGIXML_FUNCTION __iterator_category(const pugi::xml_node_iterator&); | |
| 1236 | + | std::bidirectional_iterator_tag PUGIXML_FUNCTION __iterator_category(const pugi::xml_attribute_iterator&); | |
| 1237 | + | std::forward_iterator_tag PUGIXML_FUNCTION __iterator_category(const pugi::xml_named_node_iterator&); | |
| 1238 | + | } | |
| 1239 | + | #endif | |
| 1240 | + | ||
| 1241 | + | #endif | |
| 1242 | + | ||
| 1243 | + | /** | |
| 1244 | + | * Copyright (c) 2006-2012 Arseny Kapoulkine | |
| 1245 | + | * | |
| 1246 | + | * Permission is hereby granted, free of charge, to any person | |
| 1247 | + | * obtaining a copy of this software and associated documentation | |
| 1248 | + | * files (the "Software"), to deal in the Software without | |
| 1249 | + | * restriction, including without limitation the rights to use, | |
| 1250 | + | * copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 1251 | + | * copies of the Software, and to permit persons to whom the | |
| 1252 | + | * Software is furnished to do so, subject to the following | |
| 1253 | + | * conditions: | |
| 1254 | + | * | |
| 1255 | + | * The above copyright notice and this permission notice shall be | |
| 1256 | + | * included in all copies or substantial portions of the Software. | |
| 1257 | + | * | |
| 1258 | + | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| 1259 | + | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
| 1260 | + | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| 1261 | + | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
| 1262 | + | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
| 1263 | + | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
| 1264 | + | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
| 1265 | + | * OTHER DEALINGS IN THE SOFTWARE. | |
| 1266 | + | */ |
| @@ -0,0 +1,87 @@ | |||
| 1 | + | ////////////////////////////////////////////////////////////////////// | |
| 2 | + | // This file is part of Remere's Map Editor | |
| 3 | + | ////////////////////////////////////////////////////////////////////// | |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify | |
| 5 | + | // it under the terms of the GNU General Public License as published by | |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or | |
| 7 | + | // (at your option) any later version. | |
| 8 | + | // | |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, | |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | // GNU General Public License for more details. | |
| 13 | + | // | |
| 14 | + | // You should have received a copy of the GNU General Public License | |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | + | ////////////////////////////////////////////////////////////////////// | |
| 17 | + | ||
| 18 | + | #include "main.h" | |
| 19 | + | ||
| 20 | + | #include "extension.h" | |
| 21 | + | ||
| 22 | + | MaterialsExtension::MaterialsExtension(std::string name, std::string author, std::string description) : | |
| 23 | + | name(name), | |
| 24 | + | author(author), | |
| 25 | + | description(description), | |
| 26 | + | for_all_versions(false) { | |
| 27 | + | //// | |
| 28 | + | } | |
| 29 | + | ||
| 30 | + | MaterialsExtension::~MaterialsExtension() { | |
| 31 | + | //// | |
| 32 | + | } | |
| 33 | + | ||
| 34 | + | void MaterialsExtension::addVersion(const std::string& versionString) { | |
| 35 | + | if (versionString == "all") { | |
| 36 | + | for_all_versions = true; | |
| 37 | + | } else { | |
| 38 | + | ClientVersion* client = ClientVersion::get(versionString); | |
| 39 | + | if (client) { | |
| 40 | + | ClientVersionList supported_versions = ClientVersion::getAllVersionsSupportedForClientVersion(client); | |
| 41 | + | version_list.insert(version_list.end(), supported_versions.begin(), supported_versions.end()); | |
| 42 | + | } | |
| 43 | + | } | |
| 44 | + | } | |
| 45 | + | ||
| 46 | + | bool MaterialsExtension::isForVersion(uint16_t versionId) { | |
| 47 | + | if (for_all_versions) { | |
| 48 | + | return true; | |
| 49 | + | } | |
| 50 | + | ||
| 51 | + | for (ClientVersion* version : version_list) { | |
| 52 | + | if (version->getID() == versionId) { | |
| 53 | + | return true; | |
| 54 | + | } | |
| 55 | + | } | |
| 56 | + | return false; | |
| 57 | + | } | |
| 58 | + | ||
| 59 | + | std::string MaterialsExtension::getVersionString() { | |
| 60 | + | if (for_all_versions) { | |
| 61 | + | return "All"; | |
| 62 | + | } | |
| 63 | + | ||
| 64 | + | std::string versions; | |
| 65 | + | std::string last; | |
| 66 | + | for (ClientVersion* version : version_list) { | |
| 67 | + | if (!last.empty()) { | |
| 68 | + | if (!versions.empty()) { | |
| 69 | + | versions += ", " + last; | |
| 70 | + | } else { | |
| 71 | + | versions = last; | |
| 72 | + | } | |
| 73 | + | } | |
| 74 | + | last = version->getName(); | |
| 75 | + | } | |
| 76 | + | ||
| 77 | + | if (!last.empty()) { | |
| 78 | + | if (!versions.empty()) { | |
| 79 | + | versions += " and " + last; | |
| 80 | + | } else { | |
| 81 | + | versions = last; | |
| 82 | + | } | |
| 83 | + | } else { | |
| 84 | + | return "None"; | |
| 85 | + | } | |
| 86 | + | return versions; | |
| 87 | + | } |
| @@ -0,0 +1,48 @@ | |||
| 1 | + | ////////////////////////////////////////////////////////////////////// | |
| 2 | + | // This file is part of Remere's Map Editor | |
| 3 | + | ////////////////////////////////////////////////////////////////////// | |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify | |
| 5 | + | // it under the terms of the GNU General Public License as published by | |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or | |
| 7 | + | // (at your option) any later version. | |
| 8 | + | // | |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, | |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | // GNU General Public License for more details. | |
| 13 | + | // | |
| 14 | + | // You should have received a copy of the GNU General Public License | |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | + | ////////////////////////////////////////////////////////////////////// | |
| 17 | + | ||
| 18 | + | #ifndef RME_EXTENSION_H_ | |
| 19 | + | #define RME_EXTENSION_H_ | |
| 20 | + | ||
| 21 | + | #include "tileset.h" | |
| 22 | + | #include "client_version.h" | |
| 23 | + | ||
| 24 | + | class MaterialsExtension { | |
| 25 | + | public: | |
| 26 | + | MaterialsExtension(std::string name, std::string author, std::string description); | |
| 27 | + | ~MaterialsExtension(); | |
| 28 | + | ||
| 29 | + | void addVersion(const std::string& versionString); | |
| 30 | + | bool isForVersion(uint16_t versionId); | |
| 31 | + | std::string getVersionString(); | |
| 32 | + | ||
| 33 | + | std::string name; | |
| 34 | + | std::string url; | |
| 35 | + | std::string author; | |
| 36 | + | std::string author_url; | |
| 37 | + | std::string description; | |
| 38 | + | bool for_all_versions; | |
| 39 | + | ClientVersionList version_list; | |
| 40 | + | ||
| 41 | + | private: | |
| 42 | + | MaterialsExtension(const MaterialsExtension&); | |
| 43 | + | MaterialsExtension& operator=(const MaterialsExtension&); | |
| 44 | + | }; | |
| 45 | + | ||
| 46 | + | typedef std::vector<MaterialsExtension*> MaterialsExtensionList; | |
| 47 | + | ||
| 48 | + | #endif |
| @@ -0,0 +1,135 @@ | |||
| 1 | + | ////////////////////////////////////////////////////////////////////// | |
| 2 | + | // This file is part of Remere's Map Editor | |
| 3 | + | ////////////////////////////////////////////////////////////////////// | |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify | |
| 5 | + | // it under the terms of the GNU General Public License as published by | |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or | |
| 7 | + | // (at your option) any later version. | |
| 8 | + | // | |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, | |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | // GNU General Public License for more details. | |
| 13 | + | // | |
| 14 | + | // You should have received a copy of the GNU General Public License | |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | + | ////////////////////////////////////////////////////////////////////// | |
| 17 | + | ||
| 18 | + | #include "main.h" | |
| 19 | + | ||
| 20 | + | #include "extension_window.h" | |
| 21 | + | ||
| 22 | + | #include "gui.h" | |
| 23 | + | #include "materials.h" | |
| 24 | + | ||
| 25 | + | extern Materials g_materials; | |
| 26 | + | ||
| 27 | + | BEGIN_EVENT_TABLE(ExtensionsDialog, wxDialog) | |
| 28 | + | EVT_HTML_LINK_CLICKED(wxID_ANY, ExtensionsDialog::OnClickLink) | |
| 29 | + | EVT_BUTTON(wxID_OK, ExtensionsDialog::OnClickOK) | |
| 30 | + | EVT_BUTTON(EXTENSIONS_OPEN_FOLDER_BUTTON, ExtensionsDialog::OnClickOpenFolder) | |
| 31 | + | END_EVENT_TABLE() | |
| 32 | + | ||
| 33 | + | ExtensionsDialog::ExtensionsDialog(wxWindow* parent) : | |
| 34 | + | wxDialog(parent, wxID_ANY, "Extensions", wxDefaultPosition, wxSize(600, 500), wxRESIZE_BORDER | wxCAPTION) { | |
| 35 | + | wxBoxSizer* topSizer = new wxBoxSizer(wxVERTICAL); | |
| 36 | + | ||
| 37 | + | wxHtmlWindow* htmlWindow = new wxHtmlWindow(this, wxID_ANY, wxDefaultPosition, wxSize(550, 400)); | |
| 38 | + | htmlWindow->SetPage(HTML()); | |
| 39 | + | topSizer->Add(htmlWindow, wxSizerFlags(1).DoubleBorder().Expand()); | |
| 40 | + | ||
| 41 | + | wxSizer* buttonSizer = newd wxBoxSizer(wxHORIZONTAL); | |
| 42 | + | buttonSizer->Add(newd wxButton(this, wxID_OK, "OK"), wxSizerFlags(1).Center()); | |
| 43 | + | buttonSizer->Add(newd wxButton(this, EXTENSIONS_OPEN_FOLDER_BUTTON, "Open Extensions Folder"), wxSizerFlags(1).Center()); | |
| 44 | + | topSizer->Add(buttonSizer, 0, wxCENTER | wxLEFT | wxRIGHT | wxBOTTOM, 20); | |
| 45 | + | ||
| 46 | + | SetSizerAndFit(topSizer); | |
| 47 | + | Centre(wxBOTH); | |
| 48 | + | } | |
| 49 | + | ||
| 50 | + | ExtensionsDialog::~ExtensionsDialog() { | |
| 51 | + | //// | |
| 52 | + | } | |
| 53 | + | ||
| 54 | + | void ExtensionsDialog::OnClickLink(wxHtmlLinkEvent& evt) { | |
| 55 | + | ::wxLaunchDefaultBrowser(evt.GetLinkInfo().GetHref(), wxBROWSER_NEW_WINDOW); | |
| 56 | + | } | |
| 57 | + | ||
| 58 | + | void ExtensionsDialog::OnClickOK(wxCommandEvent& evt) { | |
| 59 | + | EndModal(0); | |
| 60 | + | } | |
| 61 | + | ||
| 62 | + | void ExtensionsDialog::OnClickOpenFolder(wxCommandEvent& evt) { | |
| 63 | + | wxString cmd, extensionsDir = g_gui.GetExtensionsDirectory(); | |
| 64 | + | #if defined __WINDOWS__ | |
| 65 | + | cmd << "explorer"; | |
| 66 | + | #elif defined __APPLE__ | |
| 67 | + | cmd << "open"; | |
| 68 | + | extensionsDir.Replace(" ", "\\ "); // Escape spaces | |
| 69 | + | #elif defined __linux | |
| 70 | + | cmd << "xdg-open"; | |
| 71 | + | #else | |
| 72 | + | #error "NOT IMPLEMENTED" | |
| 73 | + | #endif | |
| 74 | + | ||
| 75 | + | cmd << " " << extensionsDir; | |
| 76 | + | ||
| 77 | + | wxExecute(cmd); | |
| 78 | + | } | |
| 79 | + | ||
| 80 | + | wxString ExtensionsDialog::HTML() const { | |
| 81 | + | wxString markup; | |
| 82 | + | for (MaterialsExtensionList::const_iterator me = g_materials.getExtensions().begin(); me != g_materials.getExtensions().end(); ++me) { | |
| 83 | + | markup << HTMLForExtension(*me); | |
| 84 | + | markup << "<hr>"; | |
| 85 | + | } | |
| 86 | + | return markup; | |
| 87 | + | } | |
| 88 | + | ||
| 89 | + | wxString ExtensionsDialog::HTMLForExtension(MaterialsExtension* me) const { | |
| 90 | + | wxString markup; | |
| 91 | + | markup | |
| 92 | + | << "<table>" | |
| 93 | + | ||
| 94 | + | << "< background='#ff0000'>" | |
| 95 | + | << "<td width='100px'><b>Extension</b></td>" | |
| 96 | + | << "<td>"; | |
| 97 | + | ||
| 98 | + | if (me->url.empty()) { | |
| 99 | + | markup << me->name; | |
| 100 | + | } else { | |
| 101 | + | markup << "<a href='" << me->url << "'>" << me->name << "</a>"; | |
| 102 | + | } | |
| 103 | + | ||
| 104 | + | markup | |
| 105 | + | << "</td>" | |
| 106 | + | << "</tr>" | |
| 107 | + | ||
| 108 | + | << "<tr>" | |
| 109 | + | << "<td width='100px'><b>Author</b></td>" | |
| 110 | + | << "<td>"; | |
| 111 | + | ||
| 112 | + | if (me->author_url.empty()) { | |
| 113 | + | markup << me->author; | |
| 114 | + | } else { | |
| 115 | + | markup << "<a href='" << me->author_url << "'>" << me->author << "</a>"; | |
| 116 | + | } | |
| 117 | + | ||
| 118 | + | markup | |
| 119 | + | << "</td>" | |
| 120 | + | << "</tr>" | |
| 121 | + | ||
| 122 | + | << "<tr>" | |
| 123 | + | << "<td width='100px'><b>Description</b></td>" | |
| 124 | + | << "<td>" << me->description << "</td>" | |
| 125 | + | << "</tr>" | |
| 126 | + | ||
| 127 | + | << "<tr>" | |
| 128 | + | << "<td width='100px'><b>Clients</b></td>" | |
| 129 | + | << "<td>" << me->getVersionString() << "</td>" | |
| 130 | + | << "</tr>" | |
| 131 | + | ||
| 132 | + | << "</table>"; | |
| 133 | + | ||
| 134 | + | return markup; | |
| 135 | + | } |
| @@ -0,0 +1,39 @@ | |||
| 1 | + | ////////////////////////////////////////////////////////////////////// | |
| 2 | + | // This file is part of Remere's Map Editor | |
| 3 | + | ////////////////////////////////////////////////////////////////////// | |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify | |
| 5 | + | // it under the terms of the GNU General Public License as published by | |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or | |
| 7 | + | // (at your option) any later version. | |
| 8 | + | // | |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, | |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | // GNU General Public License for more details. | |
| 13 | + | // | |
| 14 | + | // You should have received a copy of the GNU General Public License | |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | + | ////////////////////////////////////////////////////////////////////// | |
| 17 | + | ||
| 18 | + | #ifndef RME_EXTENSION_WINDOW_H | |
| 19 | + | #define RME_EXTENSION_WINDOW_H | |
| 20 | + | ||
| 21 | + | class MaterialsExtension; | |
| 22 | + | ||
| 23 | + | class ExtensionsDialog : public wxDialog { | |
| 24 | + | public: | |
| 25 | + | ExtensionsDialog(wxWindow* parent); | |
| 26 | + | virtual ~ExtensionsDialog(); | |
| 27 | + | ||
| 28 | + | void OnClickOK(wxCommandEvent& evt); | |
| 29 | + | void OnClickOpenFolder(wxCommandEvent& evt); | |
| 30 | + | void OnClickLink(wxHtmlLinkEvent& evt); | |
| 31 | + | ||
| 32 | + | DECLARE_EVENT_TABLE(); | |
| 33 | + | ||
| 34 | + | private: | |
| 35 | + | wxString HTML() const; | |
| 36 | + | wxString HTMLForExtension(MaterialsExtension* me) const; | |
| 37 | + | }; | |
| 38 | + | ||
| 39 | + | #endif |
| @@ -0,0 +1,704 @@ | |||
| 1 | + | ////////////////////////////////////////////////////////////////////// | |
| 2 | + | // This file is part of Remere's Map Editor | |
| 3 | + | ////////////////////////////////////////////////////////////////////// | |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify | |
| 5 | + | // it under the terms of the GNU General Public License as published by | |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or | |
| 7 | + | // (at your option) any later version. | |
| 8 | + | // | |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, | |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | // GNU General Public License for more details. | |
| 13 | + | // | |
| 14 | + | // You should have received a copy of the GNU General Public License | |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | + | ////////////////////////////////////////////////////////////////////// | |
| 17 | + | ||
| 18 | + | #include "main.h" | |
| 19 | + | ||
| 20 | + | #include "filehandle.h" | |
| 21 | + | ||
| 22 | + | #include <stdio.h> | |
| 23 | + | #include <assert.h> | |
| 24 | + | ||
| 25 | + | uint8_t NodeFileWriteHandle::NODE_START = ::NODE_START; | |
| 26 | + | uint8_t NodeFileWriteHandle::NODE_END = ::NODE_END; | |
| 27 | + | uint8_t NodeFileWriteHandle::ESCAPE_CHAR = ::ESCAPE_CHAR; | |
| 28 | + | ||
| 29 | + | void FileHandle::close() { | |
| 30 | + | if (file) { | |
| 31 | + | fclose(file); | |
| 32 | + | file = nullptr; | |
| 33 | + | } | |
| 34 | + | } | |
| 35 | + | ||
| 36 | + | std::string FileHandle::getErrorMessage() { | |
| 37 | + | switch (error_code) { | |
| 38 | + | case FILE_NO_ERROR: | |
| 39 | + | return "No error"; | |
| 40 | + | case FILE_COULD_NOT_OPEN: | |
| 41 | + | return "Could not open file"; | |
| 42 | + | case FILE_INVALID_IDENTIFIER: | |
| 43 | + | return "File magic number not recognized"; | |
| 44 | + | case FILE_STRING_TOO_LONG: | |
| 45 | + | return "Too long string encountered"; | |
| 46 | + | case FILE_READ_ERROR: | |
| 47 | + | return "Failed to read from file"; | |
| 48 | + | case FILE_WRITE_ERROR: | |
| 49 | + | return "Failed to write to file"; | |
| 50 | + | case FILE_SYNTAX_ERROR: | |
| 51 | + | return "Node file syntax error"; | |
| 52 | + | case FILE_PREMATURE_END: | |
| 53 | + | return "File end encountered unexpectedly"; | |
| 54 | + | } | |
| 55 | + | if (file == nullptr) { | |
| 56 | + | return "Could not open file (2)"; | |
| 57 | + | } | |
| 58 | + | if (ferror(file)) { | |
| 59 | + | return "Internal file error #" + i2s(ferror(file)); | |
| 60 | + | } | |
| 61 | + | return "No error"; | |
| 62 | + | } | |
| 63 | + | ||
| 64 | + | //============================================================================= | |
| 65 | + | // File read handle | |
| 66 | + | ||
| 67 | + | FileReadHandle::FileReadHandle(const std::string& name) : | |
| 68 | + | file_size(0) { | |
| 69 | + | #if defined __VISUALC__ && defined _UNICODE | |
| 70 | + | file = _wfopen(string2wstring(name).c_str(), L"rb"); | |
| 71 | + | #else | |
| 72 | + | file = fopen(name.c_str(), "rb"); | |
| 73 | + | #endif | |
| 74 | + | if (!file || ferror(file)) { | |
| 75 | + | error_code = FILE_COULD_NOT_OPEN; | |
| 76 | + | } else { | |
| 77 | + | fseek(file, 0, SEEK_END); | |
| 78 | + | file_size = ftell(file); | |
| 79 | + | fseek(file, 0, SEEK_SET); | |
| 80 | + | } | |
| 81 | + | } | |
| 82 | + | ||
| 83 | + | FileReadHandle::~FileReadHandle() { | |
| 84 | + | //// | |
| 85 | + | } | |
| 86 | + | ||
| 87 | + | void FileReadHandle::close() { | |
| 88 | + | file_size = 0; | |
| 89 | + | FileHandle::close(); | |
| 90 | + | } | |
| 91 | + | ||
| 92 | + | bool FileReadHandle::getRAW(uint8_t* ptr, size_t sz) { | |
| 93 | + | size_t o = fread(ptr, 1, sz, file); | |
| 94 | + | if (o != sz) { | |
| 95 | + | error_code = FILE_READ_ERROR; | |
| 96 | + | return false; | |
| 97 | + | } | |
| 98 | + | return true; | |
| 99 | + | } | |
| 100 | + | ||
| 101 | + | bool FileReadHandle::getRAW(std::string& str, size_t sz) { | |
| 102 | + | str.resize(sz); | |
| 103 | + | size_t o = fread(const_cast<char*>(str.data()), 1, sz, file); | |
| 104 | + | if (o != sz) { | |
| 105 | + | error_code = FILE_READ_ERROR; | |
| 106 | + | return false; | |
| 107 | + | } | |
| 108 | + | return true; | |
| 109 | + | } | |
| 110 | + | ||
| 111 | + | bool FileReadHandle::getString(std::string& str) { | |
| 112 | + | uint16_t sz; | |
| 113 | + | if (!getU16(sz)) { | |
| 114 | + | error_code = FILE_READ_ERROR; | |
| 115 | + | return false; | |
| 116 | + | } | |
| 117 | + | return getRAW(str, sz); | |
| 118 | + | } | |
| 119 | + | ||
| 120 | + | bool FileReadHandle::getLongString(std::string& str) { | |
| 121 | + | uint32_t sz; | |
| 122 | + | if (!getU32(sz)) { | |
| 123 | + | error_code = FILE_READ_ERROR; | |
| 124 | + | return false; | |
| 125 | + | } | |
| 126 | + | return getRAW(str, sz); | |
| 127 | + | } | |
| 128 | + | ||
| 129 | + | bool FileReadHandle::seek(size_t offset) { | |
| 130 | + | return fseek(file, long(offset), SEEK_SET) == 0; | |
| 131 | + | } | |
| 132 | + | ||
| 133 | + | bool FileReadHandle::seekRelative(size_t offset) { | |
| 134 | + | return fseek(file, long(offset), SEEK_CUR) == 0; | |
| 135 | + | } | |
| 136 | + | ||
| 137 | + | //============================================================================= | |
| 138 | + | // Node file read handle | |
| 139 | + | ||
| 140 | + | NodeFileReadHandle::NodeFileReadHandle() : | |
| 141 | + | last_was_start(false), | |
| 142 | + | cache(nullptr), | |
| 143 | + | cache_size(32768), | |
| 144 | + | cache_length(0), | |
| 145 | + | local_read_index(0), | |
| 146 | + | root_node(nullptr) { | |
| 147 | + | //// | |
| 148 | + | } | |
| 149 | + | ||
| 150 | + | NodeFileReadHandle::~NodeFileReadHandle() { | |
| 151 | + | while (!unused.empty()) { | |
| 152 | + | free(unused.top()); | |
| 153 | + | unused.pop(); | |
| 154 | + | } | |
| 155 | + | } | |
| 156 | + | ||
| 157 | + | BinaryNode* NodeFileReadHandle::getNode(BinaryNode* parent) { | |
| 158 | + | void* mem; | |
| 159 | + | if (unused.empty()) { | |
| 160 | + | mem = malloc(sizeof(BinaryNode)); | |
| 161 | + | } else { | |
| 162 | + | mem = unused.top(); | |
| 163 | + | unused.pop(); | |
| 164 | + | } | |
| 165 | + | return new (mem) BinaryNode(this, parent); | |
| 166 | + | } | |
| 167 | + | ||
| 168 | + | void NodeFileReadHandle::freeNode(BinaryNode* node) { | |
| 169 | + | if (node) { | |
| 170 | + | node->~BinaryNode(); | |
| 171 | + | unused.push(node); | |
| 172 | + | } | |
| 173 | + | } | |
| 174 | + | ||
| 175 | + | //============================================================================= | |
| 176 | + | // Memory based node file read handle | |
| 177 | + | ||
| 178 | + | MemoryNodeFileReadHandle::MemoryNodeFileReadHandle(const uint8_t* data, size_t size) { | |
| 179 | + | assign(data, size); | |
| 180 | + | } | |
| 181 | + | ||
| 182 | + | void MemoryNodeFileReadHandle::assign(const uint8_t* data, size_t size) { | |
| 183 | + | freeNode(root_node); | |
| 184 | + | root_node = nullptr; | |
| 185 | + | // Highly volatile, but we know we're not gonna modify | |
| 186 | + | cache = const_cast<uint8_t*>(data); | |
| 187 | + | cache_size = cache_length = size; | |
| 188 | + | local_read_index = 0; | |
| 189 | + | } | |
| 190 | + | ||
| 191 | + | MemoryNodeFileReadHandle::~MemoryNodeFileReadHandle() { | |
| 192 | + | freeNode(root_node); | |
| 193 | + | } | |
| 194 | + | ||
| 195 | + | void MemoryNodeFileReadHandle::close() { | |
| 196 | + | assign(nullptr, 0); | |
| 197 | + | } | |
| 198 | + | ||
| 199 | + | bool MemoryNodeFileReadHandle::renewCache() { | |
| 200 | + | return false; | |
| 201 | + | } | |
| 202 | + | ||
| 203 | + | BinaryNode* MemoryNodeFileReadHandle::getRootNode() { | |
| 204 | + | assert(root_node == nullptr); // You should never do this twice | |
| 205 | + | ||
| 206 | + | local_read_index++; // Skip first NODE_START | |
| 207 | + | last_was_start = true; | |
| 208 | + | root_node = getNode(nullptr); | |
| 209 | + | root_node->load(); | |
| 210 | + | return root_node; | |
| 211 | + | } | |
| 212 | + | ||
| 213 | + | //============================================================================= | |
| 214 | + | // File based node file read handle | |
| 215 | + | ||
| 216 | + | DiskNodeFileReadHandle::DiskNodeFileReadHandle(const std::string& name, const std::vector<std::string>& acceptable_identifiers) : | |
| 217 | + | file_size(0) { | |
| 218 | + | #if defined __VISUALC__ && defined _UNICODE | |
| 219 | + | file = _wfopen(string2wstring(name).c_str(), L"rb"); | |
| 220 | + | #else | |
| 221 | + | file = fopen(name.c_str(), "rb"); | |
| 222 | + | #endif | |
| 223 | + | if (!file || ferror(file)) { | |
| 224 | + | error_code = FILE_COULD_NOT_OPEN; | |
| 225 | + | } else { | |
| 226 | + | char ver[4]; | |
| 227 | + | if (fread(ver, 1, 4, file) != 4) { | |
| 228 | + | fclose(file); | |
| 229 | + | error_code = FILE_SYNTAX_ERROR; | |
| 230 | + | return; | |
| 231 | + | } | |
| 232 | + | ||
| 233 | + | // 0x00 00 00 00 is accepted as a wildcard version | |
| 234 | + | ||
| 235 | + | if (ver[0] != 0 || ver[1] != 0 || ver[2] != 0 || ver[3] != 0) { | |
| 236 | + | bool accepted = false; | |
| 237 | + | for (std::vector<std::string>::const_iterator id_iter = acceptable_identifiers.begin(); id_iter != acceptable_identifiers.end(); ++id_iter) { | |
| 238 | + | if (memcmp(ver, id_iter->c_str(), 4) == 0) { | |
| 239 | + | accepted = true; | |
| 240 | + | break; | |
| 241 | + | } | |
| 242 | + | } | |
| 243 | + | ||
| 244 | + | if (!accepted) { | |
| 245 | + | fclose(file); | |
| 246 | + | error_code = FILE_SYNTAX_ERROR; | |
| 247 | + | return; | |
| 248 | + | } | |
| 249 | + | } | |
| 250 | + | ||
| 251 | + | fseek(file, 0, SEEK_END); | |
| 252 | + | file_size = ftell(file); | |
| 253 | + | fseek(file, 4, SEEK_SET); | |
| 254 | + | } | |
| 255 | + | } | |
| 256 | + | ||
| 257 | + | DiskNodeFileReadHandle::~DiskNodeFileReadHandle() { | |
| 258 | + | close(); | |
| 259 | + | } | |
| 260 | + | ||
| 261 | + | void DiskNodeFileReadHandle::close() { | |
| 262 | + | freeNode(root_node); | |
| 263 | + | file_size = 0; | |
| 264 | + | FileHandle::close(); | |
| 265 | + | free(cache); | |
| 266 | + | } | |
| 267 | + | ||
| 268 | + | bool DiskNodeFileReadHandle::renewCache() { | |
| 269 | + | if (!cache) { | |
| 270 | + | cache = (uint8_t*)malloc(cache_size); | |
| 271 | + | } | |
| 272 | + | cache_length = fread(cache, 1, cache_size, file); | |
| 273 | + | ||
| 274 | + | if (cache_length == 0 || ferror(file)) { | |
| 275 | + | return false; | |
| 276 | + | } | |
| 277 | + | local_read_index = 0; | |
| 278 | + | return true; | |
| 279 | + | } | |
| 280 | + | ||
| 281 | + | BinaryNode* DiskNodeFileReadHandle::getRootNode() { | |
| 282 | + | assert(root_node == nullptr); // You should never do this twice | |
| 283 | + | uint8_t first; | |
| 284 | + | fread(&first, 1, 1, file); | |
| 285 | + | if (first == NODE_START) { | |
| 286 | + | root_node = getNode(nullptr); | |
| 287 | + | root_node->load(); | |
| 288 | + | return root_node; | |
| 289 | + | } else { | |
| 290 | + | error_code = FILE_SYNTAX_ERROR; | |
| 291 | + | return nullptr; | |
| 292 | + | } | |
| 293 | + | } | |
| 294 | + | ||
| 295 | + | //============================================================================= | |
| 296 | + | // Binary file node | |
| 297 | + | ||
| 298 | + | BinaryNode::BinaryNode(NodeFileReadHandle* file, BinaryNode* parent) : | |
| 299 | + | read_offset(0), | |
| 300 | + | file(file), | |
| 301 | + | parent(parent), | |
| 302 | + | child(nullptr) { | |
| 303 | + | //// | |
| 304 | + | } | |
| 305 | + | ||
| 306 | + | BinaryNode::~BinaryNode() { | |
| 307 | + | file->freeNode(child); | |
| 308 | + | } | |
| 309 | + | ||
| 310 | + | BinaryNode* BinaryNode::getChild() { | |
| 311 | + | ASSERT(file); | |
| 312 | + | ASSERT(child == nullptr); | |
| 313 | + | ||
| 314 | + | if (file->last_was_start) { | |
| 315 | + | child = file->getNode(this); | |
| 316 | + | child->load(); | |
| 317 | + | return child; | |
| 318 | + | } | |
| 319 | + | return nullptr; | |
| 320 | + | } | |
| 321 | + | ||
| 322 | + | bool BinaryNode::getRAW(uint8_t* ptr, size_t sz) { | |
| 323 | + | if (read_offset + sz > data.size()) { | |
| 324 | + | read_offset = data.size(); | |
| 325 | + | return false; | |
| 326 | + | } | |
| 327 | + | memcpy(ptr, data.data() + read_offset, sz); | |
| 328 | + | read_offset += sz; | |
| 329 | + | return true; | |
| 330 | + | } | |
| 331 | + | ||
| 332 | + | bool BinaryNode::getRAW(std::string& str, size_t sz) { | |
| 333 | + | if (read_offset + sz > data.size()) { | |
| 334 | + | read_offset = data.size(); | |
| 335 | + | return false; | |
| 336 | + | } | |
| 337 | + | str.assign(data.data() + read_offset, sz); | |
| 338 | + | read_offset += sz; | |
| 339 | + | return true; | |
| 340 | + | } | |
| 341 | + | ||
| 342 | + | bool BinaryNode::getString(std::string& str) { | |
| 343 | + | uint16_t len; | |
| 344 | + | if (!getU16(len)) { | |
| 345 | + | return false; | |
| 346 | + | } | |
| 347 | + | return getRAW(str, len); | |
| 348 | + | } | |
| 349 | + | ||
| 350 | + | bool BinaryNode::getLongString(std::string& str) { | |
| 351 | + | uint32_t len; | |
| 352 | + | if (!getU32(len)) { | |
| 353 | + | return false; | |
| 354 | + | } | |
| 355 | + | return getRAW(str, len); | |
| 356 | + | } | |
| 357 | + | ||
| 358 | + | BinaryNode* BinaryNode::advance() { | |
| 359 | + | // Advance this to the next position | |
| 360 | + | ASSERT(file); | |
| 361 | + | ||
| 362 | + | if (file->error_code != FILE_NO_ERROR) { | |
| 363 | + | return nullptr; | |
| 364 | + | } | |
| 365 | + | ||
| 366 | + | if (child == nullptr) { | |
| 367 | + | getChild(); | |
| 368 | + | } | |
| 369 | + | // We need to move the cursor to the next node, since we're still iterating our child node! | |
| 370 | + | while (child) { | |
| 371 | + | // both functions modify ourselves and sets child to nullptr, so loop will be aborted | |
| 372 | + | // possibly change to assignment ? | |
| 373 | + | child->getChild(); | |
| 374 | + | child->advance(); | |
| 375 | + | } | |
| 376 | + | ||
| 377 | + | if (file->last_was_start) { | |
| 378 | + | return nullptr; | |
| 379 | + | } else { | |
| 380 | + | // Last was end (0xff) | |
| 381 | + | // Read next byte to decide if there is another node following this | |
| 382 | + | uint8_t*& cache = file->cache; | |
| 383 | + | size_t& cache_length = file->cache_length; | |
| 384 | + | size_t& local_read_index = file->local_read_index; | |
| 385 | + | ||
| 386 | + | if (local_read_index >= cache_length) { | |
| 387 | + | if (!file->renewCache()) { | |
| 388 | + | // Failed to renew, exit | |
| 389 | + | parent->child = nullptr; | |
| 390 | + | file->freeNode(this); | |
| 391 | + | return nullptr; | |
| 392 | + | } | |
| 393 | + | } | |
| 394 | + | ||
| 395 | + | uint8_t op = cache[local_read_index]; | |
| 396 | + | ++local_read_index; | |
| 397 | + | ||
| 398 | + | if (op == NODE_START) { | |
| 399 | + | // Another node follows this. | |
| 400 | + | // Load this node as the next one | |
| 401 | + | read_offset = 0; | |
| 402 | + | data.clear(); | |
| 403 | + | load(); | |
| 404 | + | return this; | |
| 405 | + | } else if (op == NODE_END) { | |
| 406 | + | // End of this child-tree | |
| 407 | + | parent->child = nullptr; | |
| 408 | + | file->last_was_start = false; | |
| 409 | + | file->freeNode(this); | |
| 410 | + | return nullptr; | |
| 411 | + | } else { | |
| 412 | + | file->error_code = FILE_SYNTAX_ERROR; | |
| 413 | + | return nullptr; | |
| 414 | + | } | |
| 415 | + | } | |
| 416 | + | } | |
| 417 | + | ||
| 418 | + | void BinaryNode::load() { | |
| 419 | + | ASSERT(file); | |
| 420 | + | // Read until next node starts | |
| 421 | + | uint8_t*& cache = file->cache; | |
| 422 | + | size_t& cache_length = file->cache_length; | |
| 423 | + | size_t& local_read_index = file->local_read_index; | |
| 424 | + | while (true) { | |
| 425 | + | if (local_read_index >= cache_length) { | |
| 426 | + | if (!file->renewCache()) { | |
| 427 | + | // Failed to renew, exit | |
| 428 | + | file->error_code = FILE_PREMATURE_END; | |
| 429 | + | return; | |
| 430 | + | } | |
| 431 | + | } | |
| 432 | + | ||
| 433 | + | uint8_t op = cache[local_read_index]; | |
| 434 | + | ++local_read_index; | |
| 435 | + | ||
| 436 | + | switch (op) { | |
| 437 | + | case NODE_START: { | |
| 438 | + | file->last_was_start = true; | |
| 439 | + | return; | |
| 440 | + | } | |
| 441 | + | ||
| 442 | + | case NODE_END: { | |
| 443 | + | file->last_was_start = false; | |
| 444 | + | return; | |
| 445 | + | } | |
| 446 | + | ||
| 447 | + | case ESCAPE_CHAR: { | |
| 448 | + | if (local_read_index >= cache_length) { | |
| 449 | + | if (!file->renewCache()) { | |
| 450 | + | // Failed to renew, exit | |
| 451 | + | file->error_code = FILE_PREMATURE_END; | |
| 452 | + | return; | |
| 453 | + | } | |
| 454 | + | } | |
| 455 | + | ||
| 456 | + | op = cache[local_read_index]; | |
| 457 | + | ++local_read_index; | |
| 458 | + | break; | |
| 459 | + | } | |
| 460 | + | ||
| 461 | + | default: | |
| 462 | + | break; | |
| 463 | + | } | |
| 464 | + | // std::cout << "Appending..." << std::endl; | |
| 465 | + | data.append(1, op); | |
| 466 | + | } | |
| 467 | + | } | |
| 468 | + | ||
| 469 | + | //============================================================================= | |
| 470 | + | // node file binary write handle | |
| 471 | + | ||
| 472 | + | FileWriteHandle::FileWriteHandle(const std::string& name) { | |
| 473 | + | #if defined __VISUALC__ && defined _UNICODE | |
| 474 | + | file = _wfopen(string2wstring(name).c_str(), L"wb"); | |
| 475 | + | #else | |
| 476 | + | file = fopen(name.c_str(), "wb"); | |
| 477 | + | #endif | |
| 478 | + | if (file == nullptr || ferror(file)) { | |
| 479 | + | error_code = FILE_COULD_NOT_OPEN; | |
| 480 | + | } | |
| 481 | + | } | |
| 482 | + | ||
| 483 | + | FileWriteHandle::~FileWriteHandle() { | |
| 484 | + | //// | |
| 485 | + | } | |
| 486 | + | ||
| 487 | + | bool FileWriteHandle::addString(const std::string& str) { | |
| 488 | + | if (str.size() > 0xFFFF) { | |
| 489 | + | error_code = FILE_STRING_TOO_LONG; | |
| 490 | + | return false; | |
| 491 | + | } | |
| 492 | + | addU16(uint16_t(str.size())); | |
| 493 | + | addRAW(str); | |
| 494 | + | return true; | |
| 495 | + | } | |
| 496 | + | ||
| 497 | + | bool FileWriteHandle::addString(const char* str) { | |
| 498 | + | size_t len = strlen(str); | |
| 499 | + | if (len > 0xFFFF) { | |
| 500 | + | error_code = FILE_STRING_TOO_LONG; | |
| 501 | + | return false; | |
| 502 | + | } | |
| 503 | + | addU16(uint16_t(len)); | |
| 504 | + | fwrite(str, 1, len, file); | |
| 505 | + | return true; | |
| 506 | + | } | |
| 507 | + | ||
| 508 | + | bool FileWriteHandle::addLongString(const std::string& str) { | |
| 509 | + | addU32(uint32_t(str.size())); | |
| 510 | + | addRAW(str); | |
| 511 | + | return true; | |
| 512 | + | } | |
| 513 | + | ||
| 514 | + | bool FileWriteHandle::addRAW(const std::string& str) { | |
| 515 | + | fwrite(str.c_str(), 1, str.size(), file); | |
| 516 | + | return ferror(file) == 0; | |
| 517 | + | } | |
| 518 | + | ||
| 519 | + | bool FileWriteHandle::addRAW(const uint8_t* ptr, size_t sz) { | |
| 520 | + | fwrite(ptr, 1, sz, file); | |
| 521 | + | return ferror(file) == 0; | |
| 522 | + | } | |
| 523 | + | ||
| 524 | + | //============================================================================= | |
| 525 | + | // Disk based node file write handle | |
| 526 | + | ||
| 527 | + | DiskNodeFileWriteHandle::DiskNodeFileWriteHandle(const std::string& name, const std::string& identifier) { | |
| 528 | + | #if defined __VISUALC__ && defined _UNICODE | |
| 529 | + | file = _wfopen(string2wstring(name).c_str(), L"wb"); | |
| 530 | + | #else | |
| 531 | + | file = fopen(name.c_str(), "wb"); | |
| 532 | + | #endif | |
| 533 | + | if (!file || ferror(file)) { | |
| 534 | + | error_code = FILE_COULD_NOT_OPEN; | |
| 535 | + | return; | |
| 536 | + | } | |
| 537 | + | if (identifier.length() != 4) { | |
| 538 | + | error_code = FILE_INVALID_IDENTIFIER; | |
| 539 | + | return; | |
| 540 | + | } | |
| 541 | + | ||
| 542 | + | fwrite(identifier.c_str(), 1, 4, file); | |
| 543 | + | if (!cache) { | |
| 544 | + | cache = (uint8_t*)malloc(cache_size + 1); | |
| 545 | + | } | |
| 546 | + | local_write_index = 0; | |
| 547 | + | } | |
| 548 | + | ||
| 549 | + | DiskNodeFileWriteHandle::~DiskNodeFileWriteHandle() { | |
| 550 | + | close(); | |
| 551 | + | } | |
| 552 | + | ||
| 553 | + | void DiskNodeFileWriteHandle::close() { | |
| 554 | + | if (file) { | |
| 555 | + | renewCache(); | |
| 556 | + | fclose(file); | |
| 557 | + | file = nullptr; | |
| 558 | + | error_code = FILE_NO_ERROR; | |
| 559 | + | } | |
| 560 | + | } | |
| 561 | + | ||
| 562 | + | void DiskNodeFileWriteHandle::renewCache() { | |
| 563 | + | if (cache) { | |
| 564 | + | fwrite(cache, local_write_index, 1, file); | |
| 565 | + | if (ferror(file) != 0) { | |
| 566 | + | error_code = FILE_WRITE_ERROR; | |
| 567 | + | } | |
| 568 | + | } else { | |
| 569 | + | cache = (uint8_t*)malloc(cache_size + 1); | |
| 570 | + | } | |
| 571 | + | local_write_index = 0; | |
| 572 | + | } | |
| 573 | + | ||
| 574 | + | //============================================================================= | |
| 575 | + | // Memory based node file write handle | |
| 576 | + | ||
| 577 | + | MemoryNodeFileWriteHandle::MemoryNodeFileWriteHandle() { | |
| 578 | + | if (!cache) { | |
| 579 | + | cache = (uint8_t*)malloc(cache_size + 1); | |
| 580 | + | } | |
| 581 | + | local_write_index = 0; | |
| 582 | + | } | |
| 583 | + | ||
| 584 | + | MemoryNodeFileWriteHandle::~MemoryNodeFileWriteHandle() { | |
| 585 | + | close(); | |
| 586 | + | } | |
| 587 | + | ||
| 588 | + | void MemoryNodeFileWriteHandle::reset() { | |
| 589 | + | memset(cache, 0xAA, cache_size); | |
| 590 | + | local_write_index = 0; | |
| 591 | + | } | |
| 592 | + | ||
| 593 | + | void MemoryNodeFileWriteHandle::close() { | |
| 594 | + | free(cache); | |
| 595 | + | cache = nullptr; | |
| 596 | + | } | |
| 597 | + | ||
| 598 | + | uint8_t* MemoryNodeFileWriteHandle::getMemory() { | |
| 599 | + | return cache; | |
| 600 | + | } | |
| 601 | + | ||
| 602 | + | size_t MemoryNodeFileWriteHandle::getSize() { | |
| 603 | + | return local_write_index; | |
| 604 | + | } | |
| 605 | + | ||
| 606 | + | void MemoryNodeFileWriteHandle::renewCache() { | |
| 607 | + | if (cache) { | |
| 608 | + | cache_size = cache_size * 2; | |
| 609 | + | cache = (uint8_t*)realloc(cache, cache_size); | |
| 610 | + | if (!cache) { | |
| 611 | + | exit(1); | |
| 612 | + | } | |
| 613 | + | } else { | |
| 614 | + | cache = (uint8_t*)malloc(cache_size + 1); | |
| 615 | + | } | |
| 616 | + | } | |
| 617 | + | ||
| 618 | + | //============================================================================= | |
| 619 | + | // Node file write handle | |
| 620 | + | ||
| 621 | + | NodeFileWriteHandle::NodeFileWriteHandle() : | |
| 622 | + | cache(nullptr), | |
| 623 | + | cache_size(0x7FFF), | |
| 624 | + | local_write_index(0) { | |
| 625 | + | //// | |
| 626 | + | } | |
| 627 | + | ||
| 628 | + | NodeFileWriteHandle::~NodeFileWriteHandle() { | |
| 629 | + | free(cache); | |
| 630 | + | } | |
| 631 | + | ||
| 632 | + | bool NodeFileWriteHandle::addNode(uint8_t nodetype) { | |
| 633 | + | cache[local_write_index++] = NODE_START; | |
| 634 | + | if (local_write_index >= cache_size) { | |
| 635 | + | renewCache(); | |
| 636 | + | } | |
| 637 | + | ||
| 638 | + | cache[local_write_index++] = nodetype; | |
| 639 | + | if (local_write_index >= cache_size) { | |
| 640 | + | renewCache(); | |
| 641 | + | } | |
| 642 | + | ||
| 643 | + | return error_code == FILE_NO_ERROR; | |
| 644 | + | } | |
| 645 | + | ||
| 646 | + | bool NodeFileWriteHandle::endNode() { | |
| 647 | + | cache[local_write_index++] = NODE_END; | |
| 648 | + | if (local_write_index >= cache_size) { | |
| 649 | + | renewCache(); | |
| 650 | + | } | |
| 651 | + | ||
| 652 | + | return error_code == FILE_NO_ERROR; | |
| 653 | + | } | |
| 654 | + | ||
| 655 | + | bool NodeFileWriteHandle::addU8(uint8_t u8) { | |
| 656 | + | writeBytes(&u8, sizeof(u8)); | |
| 657 | + | return error_code == FILE_NO_ERROR; | |
| 658 | + | } | |
| 659 | + | ||
| 660 | + | bool NodeFileWriteHandle::addByte(uint8_t u8) { | |
| 661 | + | writeBytes(&u8, sizeof(u8)); | |
| 662 | + | return error_code == FILE_NO_ERROR; | |
| 663 | + | } | |
| 664 | + | ||
| 665 | + | bool NodeFileWriteHandle::addU16(uint16_t u16) { | |
| 666 | + | writeBytes(reinterpret_cast<uint8_t*>(&u16), sizeof(u16)); | |
| 667 | + | return error_code == FILE_NO_ERROR; | |
| 668 | + | } | |
| 669 | + | ||
| 670 | + | bool NodeFileWriteHandle::addU32(uint32_t u32) { | |
| 671 | + | writeBytes(reinterpret_cast<uint8_t*>(&u32), sizeof(u32)); | |
| 672 | + | return error_code == FILE_NO_ERROR; | |
| 673 | + | } | |
| 674 | + | ||
| 675 | + | bool NodeFileWriteHandle::addU64(uint64_t u64) { | |
| 676 | + | writeBytes(reinterpret_cast<uint8_t*>(&u64), sizeof(u64)); | |
| 677 | + | return error_code == FILE_NO_ERROR; | |
| 678 | + | } | |
| 679 | + | ||
| 680 | + | bool NodeFileWriteHandle::addString(const std::string& str) { | |
| 681 | + | if (str.size() > 0xFFFF) { | |
| 682 | + | error_code = FILE_STRING_TOO_LONG; | |
| 683 | + | return false; | |
| 684 | + | } | |
| 685 | + | addU16(uint16_t(str.size())); | |
| 686 | + | addRAW((const uint8_t*)str.c_str(), str.size()); | |
| 687 | + | return error_code == FILE_NO_ERROR; | |
| 688 | + | } | |
| 689 | + | ||
| 690 | + | bool NodeFileWriteHandle::addLongString(const std::string& str) { | |
| 691 | + | addU32(uint32_t(str.size())); | |
| 692 | + | addRAW((const uint8_t*)str.c_str(), str.size()); | |
| 693 | + | return error_code == FILE_NO_ERROR; | |
| 694 | + | } | |
| 695 | + | ||
| 696 | + | bool NodeFileWriteHandle::addRAW(std::string& str) { | |
| 697 | + | writeBytes(reinterpret_cast<uint8_t*>(const_cast<char*>(str.data())), str.size()); | |
| 698 | + | return error_code == FILE_NO_ERROR; | |
| 699 | + | } | |
| 700 | + | ||
| 701 | + | bool NodeFileWriteHandle::addRAW(const uint8_t* ptr, size_t sz) { | |
| 702 | + | writeBytes(ptr, sz); | |
| 703 | + | return error_code == FILE_NO_ERROR; | |
| 704 | + | } |
| @@ -0,0 +1,389 @@ | |||
| 1 | + | ////////////////////////////////////////////////////////////////////// | |
| 2 | + | // This file is part of Remere's Map Editor | |
| 3 | + | ////////////////////////////////////////////////////////////////////// | |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify | |
| 5 | + | // it under the terms of the GNU General Public License as published by | |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or | |
| 7 | + | // (at your option) any later version. | |
| 8 | + | // | |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, | |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | // GNU General Public License for more details. | |
| 13 | + | // | |
| 14 | + | // You should have received a copy of the GNU General Public License | |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | + | ////////////////////////////////////////////////////////////////////// | |
| 17 | + | ||
| 18 | + | #ifndef RME_FILEHANDLE_H_ | |
| 19 | + | #define RME_FILEHANDLE_H_ | |
| 20 | + | ||
| 21 | + | #include "definitions.h" | |
| 22 | + | ||
| 23 | + | #include <stdexcept> | |
| 24 | + | #include <string> | |
| 25 | + | #include <stack> | |
| 26 | + | #include <stdio.h> | |
| 27 | + | ||
| 28 | + | #ifndef FORCEINLINE | |
| 29 | + | #ifdef _MSV_VER | |
| 30 | + | #define FORCEINLINE __forceinline | |
| 31 | + | #else | |
| 32 | + | #define FORCEINLINE inline | |
| 33 | + | #endif | |
| 34 | + | #endif | |
| 35 | + | ||
| 36 | + | enum FileHandleError { | |
| 37 | + | FILE_NO_ERROR, | |
| 38 | + | FILE_COULD_NOT_OPEN, | |
| 39 | + | FILE_INVALID_IDENTIFIER, | |
| 40 | + | FILE_STRING_TOO_LONG, | |
| 41 | + | FILE_READ_ERROR, | |
| 42 | + | FILE_WRITE_ERROR, | |
| 43 | + | FILE_SYNTAX_ERROR, | |
| 44 | + | FILE_PREMATURE_END, | |
| 45 | + | }; | |
| 46 | + | ||
| 47 | + | enum NodeType { | |
| 48 | + | NODE_START = 0xfe, | |
| 49 | + | NODE_END = 0xff, | |
| 50 | + | ESCAPE_CHAR = 0xfd, | |
| 51 | + | }; | |
| 52 | + | ||
| 53 | + | class FileHandle : boost::noncopyable { | |
| 54 | + | public: | |
| 55 | + | FileHandle() : | |
| 56 | + | error_code(FILE_NO_ERROR), file(nullptr) { } | |
| 57 | + | virtual ~FileHandle() { | |
| 58 | + | close(); | |
| 59 | + | } | |
| 60 | + | ||
| 61 | + | virtual void close(); | |
| 62 | + | virtual bool isOpen() { | |
| 63 | + | return file != nullptr; | |
| 64 | + | } | |
| 65 | + | virtual bool isOk() { | |
| 66 | + | return isOpen() && error_code == FILE_NO_ERROR && ferror(file) == 0; | |
| 67 | + | } | |
| 68 | + | std::string getErrorMessage(); | |
| 69 | + | ||
| 70 | + | public: | |
| 71 | + | FileHandleError error_code; | |
| 72 | + | FILE* file; | |
| 73 | + | }; | |
| 74 | + | ||
| 75 | + | class FileReadHandle : public FileHandle { | |
| 76 | + | public: | |
| 77 | + | explicit FileReadHandle(const std::string& name); | |
| 78 | + | virtual ~FileReadHandle(); | |
| 79 | + | ||
| 80 | + | FORCEINLINE bool getU8(uint8_t& u8) { | |
| 81 | + | return getType(u8); | |
| 82 | + | } | |
| 83 | + | FORCEINLINE bool getByte(uint8_t& u8) { | |
| 84 | + | return getType(u8); | |
| 85 | + | } | |
| 86 | + | FORCEINLINE bool getSByte(int8_t& i8) { | |
| 87 | + | return getType(i8); | |
| 88 | + | } | |
| 89 | + | FORCEINLINE bool getU16(uint16_t& u16) { | |
| 90 | + | return getType(u16); | |
| 91 | + | } | |
| 92 | + | FORCEINLINE bool getU32(uint32_t& u32) { | |
| 93 | + | return getType(u32); | |
| 94 | + | } | |
| 95 | + | FORCEINLINE bool get32(int32_t& i32) { | |
| 96 | + | return getType(i32); | |
| 97 | + | } | |
| 98 | + | bool getRAW(uint8_t* ptr, size_t sz); | |
| 99 | + | bool getRAW(std::string& str, size_t sz); | |
| 100 | + | bool getString(std::string& str); | |
| 101 | + | bool getLongString(std::string& str); | |
| 102 | + | ||
| 103 | + | virtual void close(); | |
| 104 | + | bool seek(size_t offset); | |
| 105 | + | bool seekRelative(size_t offset); | |
| 106 | + | FORCEINLINE void skip(size_t offset) { | |
| 107 | + | seekRelative(offset); | |
| 108 | + | } | |
| 109 | + | size_t size() { | |
| 110 | + | return file_size; | |
| 111 | + | } | |
| 112 | + | size_t tell() { | |
| 113 | + | if (file) { | |
| 114 | + | return ftell(file); | |
| 115 | + | } | |
| 116 | + | return 0; | |
| 117 | + | } | |
| 118 | + | ||
| 119 | + | protected: | |
| 120 | + | size_t file_size; | |
| 121 | + | ||
| 122 | + | template <class T> | |
| 123 | + | bool getType(T& ref) { | |
| 124 | + | fread(&ref, sizeof(ref), 1, file); | |
| 125 | + | return ferror(file) == 0; | |
| 126 | + | } | |
| 127 | + | }; | |
| 128 | + | ||
| 129 | + | class NodeFileReadHandle; | |
| 130 | + | class DiskNodeFileReadHandle; | |
| 131 | + | class MemoryNodeFileReadHandle; | |
| 132 | + | ||
| 133 | + | class BinaryNode { | |
| 134 | + | public: | |
| 135 | + | BinaryNode(NodeFileReadHandle* file, BinaryNode* parent); | |
| 136 | + | ~BinaryNode(); | |
| 137 | + | ||
| 138 | + | FORCEINLINE bool getU8(uint8_t& u8) { | |
| 139 | + | return getType(u8); | |
| 140 | + | } | |
| 141 | + | FORCEINLINE bool getByte(uint8_t& u8) { | |
| 142 | + | return getType(u8); | |
| 143 | + | } | |
| 144 | + | FORCEINLINE bool getU16(uint16_t& u16) { | |
| 145 | + | return getType(u16); | |
| 146 | + | } | |
| 147 | + | FORCEINLINE bool getU32(uint32_t& u32) { | |
| 148 | + | return getType(u32); | |
| 149 | + | } | |
| 150 | + | FORCEINLINE bool getU64(uint64_t& u64) { | |
| 151 | + | return getType(u64); | |
| 152 | + | } | |
| 153 | + | FORCEINLINE bool skip(size_t sz) { | |
| 154 | + | if (read_offset + sz > data.size()) { | |
| 155 | + | read_offset = data.size(); | |
| 156 | + | return false; | |
| 157 | + | } | |
| 158 | + | read_offset += sz; | |
| 159 | + | return true; | |
| 160 | + | } | |
| 161 | + | bool getRAW(uint8_t* ptr, size_t sz); | |
| 162 | + | bool getRAW(std::string& str, size_t sz); | |
| 163 | + | bool getString(std::string& str); | |
| 164 | + | bool getLongString(std::string& str); | |
| 165 | + | ||
| 166 | + | BinaryNode* getChild(); | |
| 167 | + | // Returns this on success, nullptr on failure | |
| 168 | + | BinaryNode* advance(); | |
| 169 | + | ||
| 170 | + | protected: | |
| 171 | + | template <class T> | |
| 172 | + | bool getType(T& ref) { | |
| 173 | + | if (read_offset + sizeof(ref) > data.size()) { | |
| 174 | + | read_offset = data.size(); | |
| 175 | + | return false; | |
| 176 | + | } | |
| 177 | + | ref = *(T*)(data.data() + read_offset); | |
| 178 | + | ||
| 179 | + | read_offset += sizeof(ref); | |
| 180 | + | return true; | |
| 181 | + | } | |
| 182 | + | ||
| 183 | + | void load(); | |
| 184 | + | std::string data; | |
| 185 | + | size_t read_offset; | |
| 186 | + | NodeFileReadHandle* file; | |
| 187 | + | BinaryNode* parent; | |
| 188 | + | BinaryNode* child; | |
| 189 | + | ||
| 190 | + | friend class DiskNodeFileReadHandle; | |
| 191 | + | friend class MemoryNodeFileReadHandle; | |
| 192 | + | }; | |
| 193 | + | ||
| 194 | + | class NodeFileReadHandle : public FileHandle { | |
| 195 | + | public: | |
| 196 | + | NodeFileReadHandle(); | |
| 197 | + | virtual ~NodeFileReadHandle(); | |
| 198 | + | ||
| 199 | + | virtual BinaryNode* getRootNode() = 0; | |
| 200 | + | ||
| 201 | + | virtual size_t size() = 0; | |
| 202 | + | virtual size_t tell() = 0; | |
| 203 | + | ||
| 204 | + | protected: | |
| 205 | + | BinaryNode* getNode(BinaryNode* parent); | |
| 206 | + | void freeNode(BinaryNode* node); | |
| 207 | + | // Returns false when end-of-file is reached | |
| 208 | + | virtual bool renewCache() = 0; | |
| 209 | + | ||
| 210 | + | bool last_was_start; | |
| 211 | + | uint8_t* cache; | |
| 212 | + | size_t cache_size; | |
| 213 | + | size_t cache_length; | |
| 214 | + | size_t local_read_index; | |
| 215 | + | ||
| 216 | + | BinaryNode* root_node; | |
| 217 | + | ||
| 218 | + | std::stack<void*> unused; | |
| 219 | + | ||
| 220 | + | friend class BinaryNode; | |
| 221 | + | }; | |
| 222 | + | ||
| 223 | + | class DiskNodeFileReadHandle : public NodeFileReadHandle { | |
| 224 | + | public: | |
| 225 | + | DiskNodeFileReadHandle(const std::string& name, const std::vector<std::string>& acceptable_identifiers); | |
| 226 | + | virtual ~DiskNodeFileReadHandle(); | |
| 227 | + | ||
| 228 | + | virtual void close(); | |
| 229 | + | virtual BinaryNode* getRootNode(); | |
| 230 | + | ||
| 231 | + | virtual size_t size() { | |
| 232 | + | return file_size; | |
| 233 | + | } | |
| 234 | + | virtual size_t tell() { | |
| 235 | + | if (file) { | |
| 236 | + | return ftell(file); | |
| 237 | + | } | |
| 238 | + | return 0; | |
| 239 | + | } | |
| 240 | + | ||
| 241 | + | protected: | |
| 242 | + | virtual bool renewCache(); | |
| 243 | + | ||
| 244 | + | size_t file_size; | |
| 245 | + | }; | |
| 246 | + | ||
| 247 | + | class MemoryNodeFileReadHandle : public NodeFileReadHandle { | |
| 248 | + | public: | |
| 249 | + | // Does NOT claim ownership of the memory it is given. | |
| 250 | + | MemoryNodeFileReadHandle(const uint8_t* data, size_t size); | |
| 251 | + | virtual ~MemoryNodeFileReadHandle(); | |
| 252 | + | ||
| 253 | + | void assign(const uint8_t* data, size_t size); | |
| 254 | + | ||
| 255 | + | virtual void close(); | |
| 256 | + | virtual BinaryNode* getRootNode(); | |
| 257 | + | ||
| 258 | + | virtual size_t size() { | |
| 259 | + | return cache_size; | |
| 260 | + | } | |
| 261 | + | virtual size_t tell() { | |
| 262 | + | return local_read_index; | |
| 263 | + | } | |
| 264 | + | virtual bool isOk() { | |
| 265 | + | return true; | |
| 266 | + | } | |
| 267 | + | ||
| 268 | + | protected: | |
| 269 | + | virtual bool renewCache(); | |
| 270 | + | ||
| 271 | + | uint8_t* index; | |
| 272 | + | }; | |
| 273 | + | ||
| 274 | + | class FileWriteHandle : public FileHandle { | |
| 275 | + | public: | |
| 276 | + | explicit FileWriteHandle(const std::string& name); | |
| 277 | + | virtual ~FileWriteHandle(); | |
| 278 | + | ||
| 279 | + | FORCEINLINE bool addU8(uint8_t u8) { | |
| 280 | + | return addType(u8); | |
| 281 | + | } | |
| 282 | + | FORCEINLINE bool addByte(uint8_t u8) { | |
| 283 | + | return addType(u8); | |
| 284 | + | } | |
| 285 | + | FORCEINLINE bool addU16(uint16_t u16) { | |
| 286 | + | return addType(u16); | |
| 287 | + | } | |
| 288 | + | FORCEINLINE bool addU32(uint32_t u32) { | |
| 289 | + | return addType(u32); | |
| 290 | + | } | |
| 291 | + | FORCEINLINE bool addU64(uint64_t u64) { | |
| 292 | + | return addType(u64); | |
| 293 | + | } | |
| 294 | + | bool addString(const std::string& str); | |
| 295 | + | bool addString(const char* str); | |
| 296 | + | bool addLongString(const std::string& str); | |
| 297 | + | bool addRAW(const std::string& str); | |
| 298 | + | bool addRAW(const uint8_t* ptr, size_t sz); | |
| 299 | + | bool addRAW(const char* c) { | |
| 300 | + | return addRAW(reinterpret_cast<const uint8_t*>(c), strlen(c)); | |
| 301 | + | } | |
| 302 | + | ||
| 303 | + | protected: | |
| 304 | + | template <class T> | |
| 305 | + | bool addType(T ref) { | |
| 306 | + | fwrite(&ref, sizeof(ref), 1, file); | |
| 307 | + | return ferror(file) == 0; | |
| 308 | + | } | |
| 309 | + | }; | |
| 310 | + | ||
| 311 | + | class NodeFileWriteHandle : public FileHandle { | |
| 312 | + | public: | |
| 313 | + | NodeFileWriteHandle(); | |
| 314 | + | virtual ~NodeFileWriteHandle(); | |
| 315 | + | ||
| 316 | + | bool addNode(uint8_t nodetype); | |
| 317 | + | bool endNode(); | |
| 318 | + | ||
| 319 | + | bool addU8(uint8_t u8); | |
| 320 | + | bool addByte(uint8_t u8); | |
| 321 | + | bool addU16(uint16_t u16); | |
| 322 | + | bool addU32(uint32_t u32); | |
| 323 | + | bool addU64(uint64_t u64); | |
| 324 | + | bool addString(const std::string& str); | |
| 325 | + | bool addLongString(const std::string& str); | |
| 326 | + | bool addRAW(std::string& str); | |
| 327 | + | bool addRAW(const uint8_t* ptr, size_t sz); | |
| 328 | + | bool addRAW(const char* c) { | |
| 329 | + | return addRAW(reinterpret_cast<const uint8_t*>(c), strlen(c)); | |
| 330 | + | } | |
| 331 | + | ||
| 332 | + | protected: | |
| 333 | + | virtual void renewCache() = 0; | |
| 334 | + | ||
| 335 | + | static uint8_t NODE_START; | |
| 336 | + | static uint8_t NODE_END; | |
| 337 | + | static uint8_t ESCAPE_CHAR; | |
| 338 | + | ||
| 339 | + | uint8_t* cache; | |
| 340 | + | size_t cache_size; | |
| 341 | + | size_t local_write_index; | |
| 342 | + | ||
| 343 | + | FORCEINLINE void writeBytes(const uint8_t* ptr, size_t sz) { | |
| 344 | + | if (sz) { | |
| 345 | + | do { | |
| 346 | + | if (*ptr == NODE_START || *ptr == NODE_END || *ptr == ESCAPE_CHAR) { | |
| 347 | + | cache[local_write_index++] = ESCAPE_CHAR; | |
| 348 | + | if (local_write_index >= cache_size) { | |
| 349 | + | renewCache(); | |
| 350 | + | } | |
| 351 | + | } | |
| 352 | + | cache[local_write_index++] = *ptr; | |
| 353 | + | if (local_write_index >= cache_size) { | |
| 354 | + | renewCache(); | |
| 355 | + | } | |
| 356 | + | ++ptr; | |
| 357 | + | --sz; | |
| 358 | + | } while (sz != 0); | |
| 359 | + | } | |
| 360 | + | } | |
| 361 | + | }; | |
| 362 | + | ||
| 363 | + | class DiskNodeFileWriteHandle : public NodeFileWriteHandle { | |
| 364 | + | public: | |
| 365 | + | DiskNodeFileWriteHandle(const std::string& name, const std::string& identifier); | |
| 366 | + | virtual ~DiskNodeFileWriteHandle(); | |
| 367 | + | ||
| 368 | + | virtual void close(); | |
| 369 | + | ||
| 370 | + | protected: | |
| 371 | + | virtual void renewCache(); | |
| 372 | + | }; | |
| 373 | + | ||
| 374 | + | class MemoryNodeFileWriteHandle : public NodeFileWriteHandle { | |
| 375 | + | public: | |
| 376 | + | MemoryNodeFileWriteHandle(); | |
| 377 | + | virtual ~MemoryNodeFileWriteHandle(); | |
| 378 | + | ||
| 379 | + | void reset(); | |
| 380 | + | virtual void close(); | |
| 381 | + | ||
| 382 | + | uint8_t* getMemory(); | |
| 383 | + | size_t getSize(); | |
| 384 | + | ||
| 385 | + | protected: | |
| 386 | + | virtual void renewCache(); | |
| 387 | + | }; | |
| 388 | + | ||
| 389 | + | #endif |
| @@ -0,0 +1,283 @@ | |||
| 1 | + | ////////////////////////////////////////////////////////////////////// | |
| 2 | + | // This file is part of Remere's Map Editor | |
| 3 | + | ////////////////////////////////////////////////////////////////////// | |
| 4 | + | // Remere's Map Editor is free software: you can redistribute it and/or modify | |
| 5 | + | // it under the terms of the GNU General Public License as published by | |
| 6 | + | // the Free Software Foundation, either version 3 of the License, or | |
| 7 | + | // (at your option) any later version. | |
| 8 | + | // | |
| 9 | + | // Remere's Map Editor is distributed in the hope that it will be useful, | |
| 10 | + | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | + | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | + | // GNU General Public License for more details. | |
| 13 | + | // | |
| 14 | + | // You should have received a copy of the GNU General Public License | |
| 15 | + | // along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 16 | + | ////////////////////////////////////////////////////////////////////// | |
| 17 | + | ||
| 18 | + | #include "main.h" | |
| 19 | + | #include "find_creature_window.h" | |
| 20 | + | #include "common_windows.h" | |
| 21 | + | #include "gui.h" | |
| 22 | + | #include "creatures.h" | |
| 23 | + | #include "creature_brush.h" | |
| 24 | + | #include "spawn_brush.h" | |
| 25 | + | #include "spawn.h" | |
| 26 | + | #include "map.h" | |
| 27 | + | #include "editor.h" | |
| 28 | + | #include "result_window.h" | |
| 29 | + | ||
| 30 | + | // ============================================================================ | |
| 31 | + | // FindCreatureListBox | |
| 32 | + | ||
| 33 | + | class FindCreatureListBox : public wxListBox { | |
| 34 | + | public: | |
| 35 | + | FindCreatureListBox(wxWindow* parent, wxWindowID id) : wxListBox(parent, id, wxDefaultPosition, wxDefaultSize, 0, nullptr, wxLB_SINGLE | wxLB_NEEDED_SB) { | |
| 36 | + | // ... | |
| 37 | + | } | |
| 38 | + | ||
| 39 | + | ~FindCreatureListBox() { | |
| 40 | + | // ... | |
| 41 | + | } | |
| 42 | + | }; | |
| 43 | + | ||
| 44 | + | // ============================================================================ | |
| 45 | + | // FindCreatureDialog | |
| 46 | + | ||
| 47 | + | BEGIN_EVENT_TABLE(FindCreatureDialog, wxDialog) | |
| 48 | + | EVT_TEXT(wxID_ANY, FindCreatureDialog::OnText) | |
| 49 | + | EVT_TIMER(wxID_ANY, FindCreatureDialog::OnInputTimer) | |
| 50 | + | EVT_BUTTON(wxID_OK, FindCreatureDialog::OnClickOK) | |
| 51 | + | EVT_BUTTON(wxID_CANCEL, FindCreatureDialog::OnClickCancel) | |
| 52 | + | EVT_BUTTON(wxID_REFRESH, FindCreatureDialog::OnRefreshClick) | |
| 53 | + | END_EVENT_TABLE() | |
| 54 | + | ||
| 55 | + | FindCreatureDialog::FindCreatureDialog(wxWindow* parent, const wxString& title) : | |
| 56 | + | wxDialog(parent, wxID_ANY, title, wxDefaultPosition, wxSize(600, 500), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER), | |
| 57 | + | input_timer(this) { | |
| 58 | + | ||
| 59 | + | this->SetSizeHints(wxDefaultSize, wxDefaultSize); | |
| 60 | + | ||
| 61 | + | wxBoxSizer* main_sizer = new wxBoxSizer(wxHORIZONTAL); | |
| 62 | + | wxBoxSizer* options_sizer = new wxBoxSizer(wxVERTICAL); | |
| 63 | + | ||
| 64 | + | // Name search controls | |
| 65 | + | wxStaticBoxSizer* name_box_sizer = new wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, "Creature Name"), wxVERTICAL); | |
| 66 | + | name_text_input = new wxTextCtrl(name_box_sizer->GetStaticBox(), wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0); | |
| 67 | + | name_box_sizer->Add(name_text_input, 0, wxALL | wxEXPAND, 5); | |
| 68 | + | options_sizer->Add(name_box_sizer, 0, wxALL | wxEXPAND, 5); | |
| 69 | + | ||
| 70 | + | // Type selection | |
| 71 | + | wxStaticBoxSizer* type_box_sizer = new wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, "Type"), wxVERTICAL); | |
| 72 | + | search_monsters = new wxCheckBox(type_box_sizer->GetStaticBox(), wxID_ANY, "Monsters", wxDefaultPosition, wxDefaultSize, 0); | |
| 73 | + | search_monsters->SetValue(true); | |
| 74 | + | type_box_sizer->Add(search_monsters, 0, wxALL, 5); | |
| 75 | + | ||
| 76 | + | search_npcs = new wxCheckBox(type_box_sizer->GetStaticBox(), wxID_ANY, "NPCs", wxDefaultPosition, wxDefaultSize, 0); | |
| 77 | + | search_npcs->SetValue(true); | |
| 78 | + | type_box_sizer->Add(search_npcs, 0, wxALL, 5); | |
| 79 | + | ||
| 80 | + | options_sizer->Add(type_box_sizer, 0, wxALL | wxEXPAND, 5); | |
| 81 | + | ||
| 82 | + | // Auto-refresh option | |
| 83 | + | wxStaticBoxSizer* refresh_box_sizer = new wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, "Options"), wxVERTICAL); | |
| 84 | + | auto_refresh = new wxCheckBox(refresh_box_sizer->GetStaticBox(), wxID_ANY, "Auto Refresh", wxDefaultPosition, wxDefaultSize, 0); | |
| 85 | + | auto_refresh->SetValue(true); | |
| 86 | + | refresh_box_sizer->Add(auto_refresh, 0, wxALL, 5); | |
| 87 | + | ||
| 88 | + | options_sizer->Add(refresh_box_sizer, 0, wxALL | wxEXPAND, 5); | |
| 89 | + | ||
| 90 | + | // Add spacer | |
| 91 | + | options_sizer->Add(0, 0, 1, wxEXPAND, 5); | |
| 92 | + | ||
| 93 | + | // Add buttons at the bottom | |
| 94 | + | buttons_box_sizer = new wxStdDialogButtonSizer(); | |
| 95 | + | ok_button = new wxButton(this, wxID_OK); | |
| 96 | + | buttons_box_sizer->AddButton(ok_button); | |
| 97 | + | cancel_button = new wxButton(this, wxID_CANCEL); | |
| 98 | + | buttons_box_sizer->AddButton(cancel_button); | |
| 99 | + | refresh_button = new wxButton(this, wxID_REFRESH, "Refresh"); | |
| 100 | + | buttons_box_sizer->Add(refresh_button); | |
| 101 | + | buttons_box_sizer->Realize(); | |
| 102 | + | options_sizer->Add(buttons_box_sizer, 0, wxALIGN_CENTER | wxALL, 5); | |
| 103 | + | ||
| 104 | + | main_sizer->Add(options_sizer, 1, wxALL | wxEXPAND, 5); | |
| 105 | + | ||
| 106 | + | // Creature list | |
| 107 | + | wxStaticBoxSizer* creature_list_sizer = new wxStaticBoxSizer(new wxStaticBox(this, wxID_ANY, "Creatures"), wxVERTICAL); | |
| 108 | + | creatures_list = new FindCreatureListBox(this, wxID_ANY); | |
| 109 | + | creature_list_sizer->Add(creatures_list, 1, wxALL | wxEXPAND, 5); | |
| 110 | + | ||
| 111 | + | main_sizer->Add(creature_list_sizer, 1, wxALL | wxEXPAND, 5); | |
| 112 | + | ||
| 113 | + | SetSizer(main_sizer); | |
| 114 | + | Layout(); | |
| 115 | + | Centre(wxBOTH); | |
| 116 | + | ||
| 117 | + | RefreshContentsInternal(); | |
| 118 | + | } | |
| 119 | + | ||
| 120 | + | FindCreatureDialog::~FindCreatureDialog() { | |
| 121 | + | // Nothing to do | |
| 122 | + | } | |
| 123 | + | ||
| 124 | + | void FindCreatureDialog::OnText(wxCommandEvent& event) { | |
| 125 | + | input_timer.Start(300, true); | |
| 126 | + | } | |
| 127 | + | ||
| 128 | + | void FindCreatureDialog::OnInputTimer(wxTimerEvent& event) { | |
| 129 | + | if (auto_refresh->GetValue()) { | |
| 130 | + | RefreshContentsInternal(); | |
| 131 | + | } | |
| 132 | + | } | |
| 133 | + | ||
| 134 | + | void FindCreatureDialog::RefreshContentsInternal() { | |
| 135 | + | creatures_list->Clear(); | |
| 136 | + | ||
| 137 | + | bool include_monsters = search_monsters->GetValue(); | |
| 138 | + | bool include_npcs = search_npcs->GetValue(); | |
| 139 | + | wxString search_text = name_text_input->GetValue().Lower(); | |
| 140 | + | ||
| 141 | + | // Add matching creatures from the database to the list | |
| 142 | + | for (CreatureMap::iterator iter = g_creatures.begin(); iter != g_creatures.end(); ++iter) { | |
| 143 | + | CreatureType* creature_type = iter->second; | |
| 144 | + | if (!creature_type) continue; | |
| 145 | + | ||
| 146 | + | // Filter by type | |
| 147 | + | if ((creature_type->isNpc && !include_npcs) || | |
| 148 | + | (!creature_type->isNpc && !include_monsters)) { | |
| 149 | + | continue; | |
| 150 | + | } | |
| 151 | + | ||
| 152 | + | // Filter by name | |
| 153 | + | wxString creature_name = wxString(creature_type->name.c_str(), wxConvUTF8).Lower(); | |
| 154 | + | if (!search_text.IsEmpty() && creature_name.Find(search_text) == wxNOT_FOUND) { | |
| 155 | + | continue; | |
| 156 | + | } | |
| 157 | + | ||
| 158 | + | // Add to list | |
| 159 | + | int index = creatures_list->Append(wxString(creature_type->name.c_str(), wxConvUTF8)); | |
| 160 | + | creatures_list->SetClientData(index, creature_type); | |
| 161 | + | } | |
| 162 | + | ||
| 163 | + | if (creatures_list->GetCount() > 0) { | |
| 164 | + | creatures_list->SetSelection(0); | |
| 165 | + | } | |
| 166 | + | } | |
| 167 | + | ||
| 168 | + | void FindCreatureDialog::OnClickOK(wxCommandEvent& event) { | |
| 169 | + | int selection = creatures_list->GetSelection(); | |
| 170 | + | if (selection != wxNOT_FOUND) { | |
| 171 | + | CreatureType* creature_type = reinterpret_cast<CreatureType*>(creatures_list->GetClientData(selection)); | |
| 172 | + | if (creature_type) { | |
| 173 | + | result_name = wxString(creature_type->name.c_str(), wxConvUTF8); | |
| 174 | + | ||
| 175 | + | // Find all instances of this creature in spawns across the map | |
| 176 | + | Editor* editor = g_gui.GetCurrentEditor(); | |
| 177 | + | if (editor) { | |
| 178 | + | Map& map = editor->getMap(); | |
| 179 | + | // Create a results window to display the found spawns | |
| 180 | + | SearchResultWindow* result_window = g_gui.ShowSearchWindow(); | |
| 181 | + | result_window->Clear(); | |
| 182 | + | ||
| 183 | + | // Iterate through all tiles to find spawns with matching creatures | |
| 184 | + | size_t creature_found_count = 0; | |
| 185 | + | ||
| 186 | + | g_gui.CreateLoadBar("Searching for creatures..."); | |
| 187 | + | ||
| 188 | + | const std::string creature_name = creature_type->name; | |
| 189 | + | uint64_t tile_count = 0; | |
| 190 | + | for (int z = 0; z < 16; ++z) { | |
| 191 | + | for (int x = 0; x < map.getWidth(); ++x) { | |
| 192 | + | for (int y = 0; y < map.getHeight(); ++y) { | |
| 193 | + | ++tile_count; | |
| 194 | + | if (tile_count % 5000 == 0) { | |
| 195 | + | g_gui.SetLoadDone(int(tile_count * 100.0 / map.getTileCount())); | |
| 196 | + | } | |
| 197 | + | ||
| 198 | + | Tile* tile = map.getTile(x, y, z); | |
| 199 | + | if (!tile) continue; | |
| 200 | + | ||
| 201 | + | // Check for spawns | |
| 202 | + | if (tile->spawn) { | |
| 203 | + | // Found a spawn, now check for creatures nearby with matching name | |
| 204 | + | bool found_matching_creature = false; | |
| 205 | + | Position spawn_pos = tile->getPosition(); | |
| 206 | + | int spawn_radius = tile->spawn->getSize(); | |
| 207 | + | ||
| 208 | + | // Check if any creatures of this type exist within spawn radius | |
| 209 | + | for (int sx = -spawn_radius; sx <= spawn_radius && !found_matching_creature; ++sx) { | |
| 210 | + | for (int sy = -spawn_radius; sy <= spawn_radius && !found_matching_creature; ++sy) { | |
| 211 | + | Tile* creature_tile = map.getTile(spawn_pos.x + sx, spawn_pos.y + sy, spawn_pos.z); | |
| 212 | + | if (!creature_tile || !creature_tile->creature) continue; | |
| 213 | + | ||
| 214 | + | // Check if this creature matches our search | |
| 215 | + | if (creature_tile->creature && creature_tile->creature->getName() == creature_name) { | |
| 216 | + | found_matching_creature = true; | |
| 217 | + | wxString description = wxString::Format("%s at (%d,%d,%d)", | |
| 218 | + | wxString(creature_name.c_str(), wxConvUTF8), | |
| 219 | + | creature_tile->getPosition().x, | |
| 220 | + | creature_tile->getPosition().y, | |
| 221 | + | creature_tile->getPosition().z); | |
| 222 | + | result_window->AddPosition(description, creature_tile->getPosition()); | |
| 223 | + | ++creature_found_count; | |
| 224 | + | } | |
| 225 | + | } | |
| 226 | + | } | |
| 227 | + | ||
| 228 | + | // If no creature of this type found, still show the spawn position | |
| 229 | + | if (!found_matching_creature) { | |
| 230 | + | wxString description = wxString::Format("Spawn for %s at (%d,%d,%d)", | |
| 231 | + | wxString(creature_name.c_str(), wxConvUTF8), | |
| 232 | + | spawn_pos.x, spawn_pos.y, spawn_pos.z); | |
| 233 | + | result_window->AddPosition(description, spawn_pos); | |
| 234 | + | ++creature_found_count; | |
| 235 | + | } | |
| 236 | + | } | |
| 237 | + | ||
| 238 | + | // Check for loose creatures (not in spawns) | |
| 239 | + | if (tile->creature) { | |
| 240 | + | // Check if this loose creature matches our search | |
| 241 | + | if (tile->creature->getName() == creature_name) { | |
| 242 | + | wxString description = wxString::Format("%s (loose) at (%d,%d,%d)", | |
| 243 | + | wxString(creature_name.c_str(), wxConvUTF8), | |
| 244 | + | tile->getPosition().x, | |
| 245 | + | tile->getPosition().y, | |
| 246 | + | tile->getPosition().z); | |
| 247 | + | result_window->AddPosition(description, tile->getPosition()); | |
| 248 | + | ++creature_found_count; | |
| 249 | + | } | |
| 250 | + | } | |
| 251 | + | } | |
| 252 | + | } | |
| 253 | + | } | |
| 254 | + | ||
| 255 | + | g_gui.DestroyLoadBar(); | |
| 256 | + | ||
| 257 | + | wxString result_message; | |
| 258 | + | if (creature_found_count == 0) { | |
| 259 | + | result_message = wxString::Format("No %s found on the map.", wxString(creature_name.c_str(), wxConvUTF8)); | |
| 260 | + | g_gui.PopupDialog("Search completed", result_message, wxOK); | |
| 261 | + | } else { | |
| 262 | + | result_message = wxString::Format("Found %d instances of %s on the map.", | |
| 263 | + | creature_found_count, wxString(creature_name.c_str(), wxConvUTF8)); | |
| 264 | + | g_gui.SetStatusText(result_message); | |
| 265 | + | } | |
| 266 | + | } | |
| 267 | + | ||
| 268 | + | EndModal(wxID_OK); | |
| 269 | + | } | |
| 270 | + | } | |
| 271 | + | } | |
| 272 | + | ||
| 273 | + | void FindCreatureDialog::OnClickCancel(wxCommandEvent& event) { | |
| 274 | + | EndModal(wxID_CANCEL); | |
| 275 | + | } | |
| 276 | + | ||
| 277 | + | void FindCreatureDialog::OnRefreshClick(wxCommandEvent& event) { | |
| 278 | + | RefreshContentsInternal(); | |
| 279 | + | } | |
| 280 | + | ||
| 281 | + | void FindCreatureDialog::OnClose(wxCloseEvent& event) { | |
| 282 | + | EndModal(wxID_CANCEL); | |
| 283 | + | } |