Skip to content
Snippets Groups Projects
pugixml.cpp 266 KiB
Newer Older
  • Learn to ignore specific revisions
  • Radim Vavřík's avatar
    Radim Vavřík committed
    4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000
    		return (_root >= r._root);
    	}
    
    	PUGI__FN bool xml_node::empty() const
    	{
    		return !_root;
    	}
    	
    	PUGI__FN const char_t* xml_node::name() const
    	{
    		return (_root && _root->name) ? _root->name : PUGIXML_TEXT("");
    	}
    
    	PUGI__FN xml_node_type xml_node::type() const
    	{
    		return _root ? static_cast<xml_node_type>((_root->header & impl::xml_memory_page_type_mask) + 1) : node_null;
    	}
    	
    	PUGI__FN const char_t* xml_node::value() const
    	{
    		return (_root && _root->value) ? _root->value : PUGIXML_TEXT("");
    	}
    	
    	PUGI__FN xml_node xml_node::child(const char_t* name_) const
    	{
    		if (!_root) return xml_node();
    
    		for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
    			if (i->name && impl::strequal(name_, i->name)) return xml_node(i);
    
    		return xml_node();
    	}
    
    	PUGI__FN xml_attribute xml_node::attribute(const char_t* name_) const
    	{
    		if (!_root) return xml_attribute();
    
    		for (xml_attribute_struct* i = _root->first_attribute; i; i = i->next_attribute)
    			if (i->name && impl::strequal(name_, i->name))
    				return xml_attribute(i);
    		
    		return xml_attribute();
    	}
    	
    	PUGI__FN xml_node xml_node::next_sibling(const char_t* name_) const
    	{
    		if (!_root) return xml_node();
    		
    		for (xml_node_struct* i = _root->next_sibling; i; i = i->next_sibling)
    			if (i->name && impl::strequal(name_, i->name)) return xml_node(i);
    
    		return xml_node();
    	}
    
    	PUGI__FN xml_node xml_node::next_sibling() const
    	{
    		if (!_root) return xml_node();
    		
    		if (_root->next_sibling) return xml_node(_root->next_sibling);
    		else return xml_node();
    	}
    
    	PUGI__FN xml_node xml_node::previous_sibling(const char_t* name_) const
    	{
    		if (!_root) return xml_node();
    		
    		for (xml_node_struct* i = _root->prev_sibling_c; i->next_sibling; i = i->prev_sibling_c)
    			if (i->name && impl::strequal(name_, i->name)) return xml_node(i);
    
    		return xml_node();
    	}
    
    	PUGI__FN xml_node xml_node::previous_sibling() const
    	{
    		if (!_root) return xml_node();
    		
    		if (_root->prev_sibling_c->next_sibling) return xml_node(_root->prev_sibling_c);
    		else return xml_node();
    	}
    
    	PUGI__FN xml_node xml_node::parent() const
    	{
    		return _root ? xml_node(_root->parent) : xml_node();
    	}
    
    	PUGI__FN xml_node xml_node::root() const
    	{
    		if (!_root) return xml_node();
    
    		impl::xml_memory_page* page = reinterpret_cast<impl::xml_memory_page*>(_root->header & impl::xml_memory_page_pointer_mask);
    
    		return xml_node(static_cast<impl::xml_document_struct*>(page->allocator));
    	}
    
    	PUGI__FN xml_text xml_node::text() const
    	{
    		return xml_text(_root);
    	}
    
    	PUGI__FN const char_t* xml_node::child_value() const
    	{
    		if (!_root) return PUGIXML_TEXT("");
    		
    		for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
    			if (i->value && impl::is_text_node(i))
    				return i->value;
    
    		return PUGIXML_TEXT("");
    	}
    
    	PUGI__FN const char_t* xml_node::child_value(const char_t* name_) const
    	{
    		return child(name_).child_value();
    	}
    
    	PUGI__FN xml_attribute xml_node::first_attribute() const
    	{
    		return _root ? xml_attribute(_root->first_attribute) : xml_attribute();
    	}
    
    	PUGI__FN xml_attribute xml_node::last_attribute() const
    	{
    		return _root && _root->first_attribute ? xml_attribute(_root->first_attribute->prev_attribute_c) : xml_attribute();
    	}
    
    	PUGI__FN xml_node xml_node::first_child() const
    	{
    		return _root ? xml_node(_root->first_child) : xml_node();
    	}
    
    	PUGI__FN xml_node xml_node::last_child() const
    	{
    		return _root && _root->first_child ? xml_node(_root->first_child->prev_sibling_c) : xml_node();
    	}
    
    	PUGI__FN bool xml_node::set_name(const char_t* rhs)
    	{
    		switch (type())
    		{
    		case node_pi:
    		case node_declaration:
    		case node_element:
    			return impl::strcpy_insitu(_root->name, _root->header, impl::xml_memory_page_name_allocated_mask, rhs);
    
    		default:
    			return false;
    		}
    	}
    		
    	PUGI__FN bool xml_node::set_value(const char_t* rhs)
    	{
    		switch (type())
    		{
    		case node_pi:
    		case node_cdata:
    		case node_pcdata:
    		case node_comment:
    		case node_doctype:
    			return impl::strcpy_insitu(_root->value, _root->header, impl::xml_memory_page_value_allocated_mask, rhs);
    
    		default:
    			return false;
    		}
    	}
    
    	PUGI__FN xml_attribute xml_node::append_attribute(const char_t* name_)
    	{
    		if (type() != node_element && type() != node_declaration) return xml_attribute();
    		
    		xml_attribute a(impl::append_attribute_ll(_root, impl::get_allocator(_root)));
    		a.set_name(name_);
    		
    		return a;
    	}
    
    	PUGI__FN xml_attribute xml_node::prepend_attribute(const char_t* name_)
    	{
    		if (type() != node_element && type() != node_declaration) return xml_attribute();
    		
    		xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root)));
    		if (!a) return xml_attribute();
    
    		a.set_name(name_);
    		
    		xml_attribute_struct* head = _root->first_attribute;
    
    		if (head)
    		{
    			a._attr->prev_attribute_c = head->prev_attribute_c;
    			head->prev_attribute_c = a._attr;
    		}
    		else
    			a._attr->prev_attribute_c = a._attr;
    		
    		a._attr->next_attribute = head;
    		_root->first_attribute = a._attr;
    				
    		return a;
    	}
    
    	PUGI__FN xml_attribute xml_node::insert_attribute_before(const char_t* name_, const xml_attribute& attr)
    	{
    		if ((type() != node_element && type() != node_declaration) || attr.empty()) return xml_attribute();
    		
    		// check that attribute belongs to *this
    		xml_attribute_struct* cur = attr._attr;
    
    		while (cur->prev_attribute_c->next_attribute) cur = cur->prev_attribute_c;
    
    		if (cur != _root->first_attribute) return xml_attribute();
    
    		xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root)));
    		if (!a) return xml_attribute();
    
    		a.set_name(name_);
    
    		if (attr._attr->prev_attribute_c->next_attribute)
    			attr._attr->prev_attribute_c->next_attribute = a._attr;
    		else
    			_root->first_attribute = a._attr;
    		
    		a._attr->prev_attribute_c = attr._attr->prev_attribute_c;
    		a._attr->next_attribute = attr._attr;
    		attr._attr->prev_attribute_c = a._attr;
    				
    		return a;
    	}
    
    	PUGI__FN xml_attribute xml_node::insert_attribute_after(const char_t* name_, const xml_attribute& attr)
    	{
    		if ((type() != node_element && type() != node_declaration) || attr.empty()) return xml_attribute();
    		
    		// check that attribute belongs to *this
    		xml_attribute_struct* cur = attr._attr;
    
    		while (cur->prev_attribute_c->next_attribute) cur = cur->prev_attribute_c;
    
    		if (cur != _root->first_attribute) return xml_attribute();
    
    		xml_attribute a(impl::allocate_attribute(impl::get_allocator(_root)));
    		if (!a) return xml_attribute();
    
    		a.set_name(name_);
    
    		if (attr._attr->next_attribute)
    			attr._attr->next_attribute->prev_attribute_c = a._attr;
    		else
    			_root->first_attribute->prev_attribute_c = a._attr;
    		
    		a._attr->next_attribute = attr._attr->next_attribute;
    		a._attr->prev_attribute_c = attr._attr;
    		attr._attr->next_attribute = a._attr;
    
    		return a;
    	}
    
    	PUGI__FN xml_attribute xml_node::append_copy(const xml_attribute& proto)
    	{
    		if (!proto) return xml_attribute();
    
    		xml_attribute result = append_attribute(proto.name());
    		result.set_value(proto.value());
    
    		return result;
    	}
    
    	PUGI__FN xml_attribute xml_node::prepend_copy(const xml_attribute& proto)
    	{
    		if (!proto) return xml_attribute();
    
    		xml_attribute result = prepend_attribute(proto.name());
    		result.set_value(proto.value());
    
    		return result;
    	}
    
    	PUGI__FN xml_attribute xml_node::insert_copy_after(const xml_attribute& proto, const xml_attribute& attr)
    	{
    		if (!proto) return xml_attribute();
    
    		xml_attribute result = insert_attribute_after(proto.name(), attr);
    		result.set_value(proto.value());
    
    		return result;
    	}
    
    	PUGI__FN xml_attribute xml_node::insert_copy_before(const xml_attribute& proto, const xml_attribute& attr)
    	{
    		if (!proto) return xml_attribute();
    
    		xml_attribute result = insert_attribute_before(proto.name(), attr);
    		result.set_value(proto.value());
    
    		return result;
    	}
    
    	PUGI__FN xml_node xml_node::append_child(xml_node_type type_)
    	{
    		if (!impl::allow_insert_child(this->type(), type_)) return xml_node();
    		
    		xml_node n(impl::append_node(_root, impl::get_allocator(_root), type_));
    
    		if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml"));
    
    		return n;
    	}
    
    	PUGI__FN xml_node xml_node::prepend_child(xml_node_type type_)
    	{
    		if (!impl::allow_insert_child(this->type(), type_)) return xml_node();
    		
    		xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));
    		if (!n) return xml_node();
    
    		n._root->parent = _root;
    
    		xml_node_struct* head = _root->first_child;
    
    		if (head)
    		{
    			n._root->prev_sibling_c = head->prev_sibling_c;
    			head->prev_sibling_c = n._root;
    		}
    		else
    			n._root->prev_sibling_c = n._root;
    		
    		n._root->next_sibling = head;
    		_root->first_child = n._root;
    				
    		if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml"));
    
    		return n;
    	}
    
    	PUGI__FN xml_node xml_node::insert_child_before(xml_node_type type_, const xml_node& node)
    	{
    		if (!impl::allow_insert_child(this->type(), type_)) return xml_node();
    		if (!node._root || node._root->parent != _root) return xml_node();
    	
    		xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));
    		if (!n) return xml_node();
    
    		n._root->parent = _root;
    		
    		if (node._root->prev_sibling_c->next_sibling)
    			node._root->prev_sibling_c->next_sibling = n._root;
    		else
    			_root->first_child = n._root;
    		
    		n._root->prev_sibling_c = node._root->prev_sibling_c;
    		n._root->next_sibling = node._root;
    		node._root->prev_sibling_c = n._root;
    
    		if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml"));
    
    		return n;
    	}
    
    	PUGI__FN xml_node xml_node::insert_child_after(xml_node_type type_, const xml_node& node)
    	{
    		if (!impl::allow_insert_child(this->type(), type_)) return xml_node();
    		if (!node._root || node._root->parent != _root) return xml_node();
    	
    		xml_node n(impl::allocate_node(impl::get_allocator(_root), type_));
    		if (!n) return xml_node();
    
    		n._root->parent = _root;
    	
    		if (node._root->next_sibling)
    			node._root->next_sibling->prev_sibling_c = n._root;
    		else
    			_root->first_child->prev_sibling_c = n._root;
    		
    		n._root->next_sibling = node._root->next_sibling;
    		n._root->prev_sibling_c = node._root;
    		node._root->next_sibling = n._root;
    
    		if (type_ == node_declaration) n.set_name(PUGIXML_TEXT("xml"));
    
    		return n;
    	}
    
    	PUGI__FN xml_node xml_node::append_child(const char_t* name_)
    	{
    		xml_node result = append_child(node_element);
    
    		result.set_name(name_);
    
    		return result;
    	}
    
    	PUGI__FN xml_node xml_node::prepend_child(const char_t* name_)
    	{
    		xml_node result = prepend_child(node_element);
    
    		result.set_name(name_);
    
    		return result;
    	}
    
    	PUGI__FN xml_node xml_node::insert_child_after(const char_t* name_, const xml_node& node)
    	{
    		xml_node result = insert_child_after(node_element, node);
    
    		result.set_name(name_);
    
    		return result;
    	}
    
    	PUGI__FN xml_node xml_node::insert_child_before(const char_t* name_, const xml_node& node)
    	{
    		xml_node result = insert_child_before(node_element, node);
    
    		result.set_name(name_);
    
    		return result;
    	}
    
    	PUGI__FN xml_node xml_node::append_copy(const xml_node& proto)
    	{
    		xml_node result = append_child(proto.type());
    
    		if (result) impl::recursive_copy_skip(result, proto, result);
    
    		return result;
    	}
    
    	PUGI__FN xml_node xml_node::prepend_copy(const xml_node& proto)
    	{
    		xml_node result = prepend_child(proto.type());
    
    		if (result) impl::recursive_copy_skip(result, proto, result);
    
    		return result;
    	}
    
    	PUGI__FN xml_node xml_node::insert_copy_after(const xml_node& proto, const xml_node& node)
    	{
    		xml_node result = insert_child_after(proto.type(), node);
    
    		if (result) impl::recursive_copy_skip(result, proto, result);
    
    		return result;
    	}
    
    	PUGI__FN xml_node xml_node::insert_copy_before(const xml_node& proto, const xml_node& node)
    	{
    		xml_node result = insert_child_before(proto.type(), node);
    
    		if (result) impl::recursive_copy_skip(result, proto, result);
    
    		return result;
    	}
    
    	PUGI__FN bool xml_node::remove_attribute(const char_t* name_)
    	{
    		return remove_attribute(attribute(name_));
    	}
    
    	PUGI__FN bool xml_node::remove_attribute(const xml_attribute& a)
    	{
    		if (!_root || !a._attr) return false;
    
    		// check that attribute belongs to *this
    		xml_attribute_struct* attr = a._attr;
    
    		while (attr->prev_attribute_c->next_attribute) attr = attr->prev_attribute_c;
    
    		if (attr != _root->first_attribute) return false;
    
    		if (a._attr->next_attribute) a._attr->next_attribute->prev_attribute_c = a._attr->prev_attribute_c;
    		else if (_root->first_attribute) _root->first_attribute->prev_attribute_c = a._attr->prev_attribute_c;
    		
    		if (a._attr->prev_attribute_c->next_attribute) a._attr->prev_attribute_c->next_attribute = a._attr->next_attribute;
    		else _root->first_attribute = a._attr->next_attribute;
    
    		impl::destroy_attribute(a._attr, impl::get_allocator(_root));
    
    		return true;
    	}
    
    	PUGI__FN bool xml_node::remove_child(const char_t* name_)
    	{
    		return remove_child(child(name_));
    	}
    
    	PUGI__FN bool xml_node::remove_child(const xml_node& n)
    	{
    		if (!_root || !n._root || n._root->parent != _root) return false;
    
    		if (n._root->next_sibling) n._root->next_sibling->prev_sibling_c = n._root->prev_sibling_c;
    		else if (_root->first_child) _root->first_child->prev_sibling_c = n._root->prev_sibling_c;
    		
    		if (n._root->prev_sibling_c->next_sibling) n._root->prev_sibling_c->next_sibling = n._root->next_sibling;
    		else _root->first_child = n._root->next_sibling;
    		
    		impl::destroy_node(n._root, impl::get_allocator(_root));
    
    		return true;
    	}
    
    	PUGI__FN xml_node xml_node::find_child_by_attribute(const char_t* name_, const char_t* attr_name, const char_t* attr_value) const
    	{
    		if (!_root) return xml_node();
    		
    		for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
    			if (i->name && impl::strequal(name_, i->name))
    			{
    				for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute)
    					if (impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value))
    						return xml_node(i);
    			}
    
    		return xml_node();
    	}
    
    	PUGI__FN xml_node xml_node::find_child_by_attribute(const char_t* attr_name, const char_t* attr_value) const
    	{
    		if (!_root) return xml_node();
    		
    		for (xml_node_struct* i = _root->first_child; i; i = i->next_sibling)
    			for (xml_attribute_struct* a = i->first_attribute; a; a = a->next_attribute)
    				if (impl::strequal(attr_name, a->name) && impl::strequal(attr_value, a->value))
    					return xml_node(i);
    
    		return xml_node();
    	}
    
    #ifndef PUGIXML_NO_STL
    	PUGI__FN string_t xml_node::path(char_t delimiter) const
    	{
    		xml_node cursor = *this; // Make a copy.
    		
    		string_t result = cursor.name();
    
    		while (cursor.parent())
    		{
    			cursor = cursor.parent();
    			
    			string_t temp = cursor.name();
    			temp += delimiter;
    			temp += result;
    			result.swap(temp);
    		}
    
    		return result;
    	}
    #endif
    
    	PUGI__FN xml_node xml_node::first_element_by_path(const char_t* path_, char_t delimiter) const
    	{
    		xml_node found = *this; // Current search context.
    
    		if (!_root || !path_ || !path_[0]) return found;
    
    		if (path_[0] == delimiter)
    		{
    			// Absolute path; e.g. '/foo/bar'
    			found = found.root();
    			++path_;
    		}
    
    		const char_t* path_segment = path_;
    
    		while (*path_segment == delimiter) ++path_segment;
    
    		const char_t* path_segment_end = path_segment;
    
    		while (*path_segment_end && *path_segment_end != delimiter) ++path_segment_end;
    
    		if (path_segment == path_segment_end) return found;
    
    		const char_t* next_segment = path_segment_end;
    
    		while (*next_segment == delimiter) ++next_segment;
    
    		if (*path_segment == '.' && path_segment + 1 == path_segment_end)
    			return found.first_element_by_path(next_segment, delimiter);
    		else if (*path_segment == '.' && *(path_segment+1) == '.' && path_segment + 2 == path_segment_end)
    			return found.parent().first_element_by_path(next_segment, delimiter);
    		else
    		{
    			for (xml_node_struct* j = found._root->first_child; j; j = j->next_sibling)
    			{
    				if (j->name && impl::strequalrange(j->name, path_segment, static_cast<size_t>(path_segment_end - path_segment)))
    				{
    					xml_node subsearch = xml_node(j).first_element_by_path(next_segment, delimiter);
    
    					if (subsearch) return subsearch;
    				}
    			}
    
    			return xml_node();
    		}
    	}
    
    	PUGI__FN bool xml_node::traverse(xml_tree_walker& walker)
    	{
    		walker._depth = -1;
    		
    		xml_node arg_begin = *this;
    		if (!walker.begin(arg_begin)) return false;
    
    		xml_node cur = first_child();
    				
    		if (cur)
    		{
    			++walker._depth;
    
    			do 
    			{
    				xml_node arg_for_each = cur;
    				if (!walker.for_each(arg_for_each))
    					return false;
    						
    				if (cur.first_child())
    				{
    					++walker._depth;
    					cur = cur.first_child();
    				}
    				else if (cur.next_sibling())
    					cur = cur.next_sibling();
    				else
    				{
    					// Borland C++ workaround
    					while (!cur.next_sibling() && cur != *this && !cur.parent().empty())
    					{
    						--walker._depth;
    						cur = cur.parent();
    					}
    						
    					if (cur != *this)
    						cur = cur.next_sibling();
    				}
    			}
    			while (cur && cur != *this);
    		}
    
    		assert(walker._depth == -1);
    
    		xml_node arg_end = *this;
    		return walker.end(arg_end);
    	}
    
    	PUGI__FN size_t xml_node::hash_value() const
    	{
    		return static_cast<size_t>(reinterpret_cast<uintptr_t>(_root) / sizeof(xml_node_struct));
    	}
    
    	PUGI__FN xml_node_struct* xml_node::internal_object() const
    	{
    		return _root;
    	}
    
    	PUGI__FN void xml_node::print(xml_writer& writer, const char_t* indent, unsigned int flags, xml_encoding encoding, unsigned int depth) const
    	{
    		if (!_root) return;
    
    		impl::xml_buffered_writer buffered_writer(writer, encoding);
    
    		impl::node_output(buffered_writer, *this, indent, flags, depth);
    	}
    
    #ifndef PUGIXML_NO_STL
    	PUGI__FN void xml_node::print(std::basic_ostream<char, std::char_traits<char> >& stream, const char_t* indent, unsigned int flags, xml_encoding encoding, unsigned int depth) const
    	{
    		xml_writer_stream writer(stream);
    
    		print(writer, indent, flags, encoding, depth);
    	}
    
    	PUGI__FN void xml_node::print(std::basic_ostream<wchar_t, std::char_traits<wchar_t> >& stream, const char_t* indent, unsigned int flags, unsigned int depth) const
    	{
    		xml_writer_stream writer(stream);
    
    		print(writer, indent, flags, encoding_wchar, depth);
    	}
    #endif
    
    	PUGI__FN ptrdiff_t xml_node::offset_debug() const
    	{
    		xml_node_struct* r = root()._root;
    
    		if (!r) return -1;
    
    		const char_t* buffer = static_cast<impl::xml_document_struct*>(r)->buffer;
    
    		if (!buffer) return -1;
    
    		switch (type())
    		{
    		case node_document:
    			return 0;
    
    		case node_element:
    		case node_declaration:
    		case node_pi:
    			return (_root->header & impl::xml_memory_page_name_allocated_mask) ? -1 : _root->name - buffer;
    
    		case node_pcdata:
    		case node_cdata:
    		case node_comment:
    		case node_doctype:
    			return (_root->header & impl::xml_memory_page_value_allocated_mask) ? -1 : _root->value - buffer;
    
    		default:
    			return -1;
    		}
    	}
    
    #ifdef __BORLANDC__
    	PUGI__FN bool operator&&(const xml_node& lhs, bool rhs)
    	{
    		return (bool)lhs && rhs;
    	}
    
    	PUGI__FN bool operator||(const xml_node& lhs, bool rhs)
    	{
    		return (bool)lhs || rhs;
    	}
    #endif
    
    	PUGI__FN xml_text::xml_text(xml_node_struct* root): _root(root)
    	{
    	}
    
    	PUGI__FN xml_node_struct* xml_text::_data() const
    	{
    		if (!_root || impl::is_text_node(_root)) return _root;
    
    		for (xml_node_struct* node = _root->first_child; node; node = node->next_sibling)
    			if (impl::is_text_node(node))
    				return node;
    
    		return 0;
    	}
    
    	PUGI__FN xml_node_struct* xml_text::_data_new()
    	{
    		xml_node_struct* d = _data();
    		if (d) return d;
    
    		return xml_node(_root).append_child(node_pcdata).internal_object();
    	}
    
    	PUGI__FN xml_text::xml_text(): _root(0)
    	{
    	}
    
    	PUGI__FN static void unspecified_bool_xml_text(xml_text***)
    	{
    	}
    
    	PUGI__FN xml_text::operator xml_text::unspecified_bool_type() const
    	{
    		return _data() ? unspecified_bool_xml_text : 0;
    	}
    
    	PUGI__FN bool xml_text::operator!() const
    	{
    		return !_data();
    	}
    
    	PUGI__FN bool xml_text::empty() const
    	{
    		return _data() == 0;
    	}
    
    	PUGI__FN const char_t* xml_text::get() const
    	{
    		xml_node_struct* d = _data();
    
    		return (d && d->value) ? d->value : PUGIXML_TEXT("");
    	}
    
    	PUGI__FN const char_t* xml_text::as_string(const char_t* def) const
    	{
    		xml_node_struct* d = _data();
    
    		return (d && d->value) ? d->value : def;
    	}
    
    	PUGI__FN int xml_text::as_int(int def) const
    	{
    		xml_node_struct* d = _data();
    
    		return impl::get_value_int(d ? d->value : 0, def);
    	}
    
    	PUGI__FN unsigned int xml_text::as_uint(unsigned int def) const
    	{
    		xml_node_struct* d = _data();
    
    		return impl::get_value_uint(d ? d->value : 0, def);
    	}
    
    	PUGI__FN double xml_text::as_double(double def) const
    	{
    		xml_node_struct* d = _data();
    
    		return impl::get_value_double(d ? d->value : 0, def);
    	}
    
    	PUGI__FN float xml_text::as_float(float def) const
    	{
    		xml_node_struct* d = _data();
    
    		return impl::get_value_float(d ? d->value : 0, def);
    	}
    
    	PUGI__FN bool xml_text::as_bool(bool def) const
    	{
    		xml_node_struct* d = _data();
    
    		return impl::get_value_bool(d ? d->value : 0, def);
    	}
    
    	PUGI__FN bool xml_text::set(const char_t* rhs)
    	{
    		xml_node_struct* dn = _data_new();
    
    		return dn ? impl::strcpy_insitu(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
    	}
    
    	PUGI__FN bool xml_text::set(int rhs)
    	{
    		xml_node_struct* dn = _data_new();
    
    		return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
    	}
    
    	PUGI__FN bool xml_text::set(unsigned int rhs)
    	{
    		xml_node_struct* dn = _data_new();
    
    		return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
    	}
    
    	PUGI__FN bool xml_text::set(double rhs)
    	{
    		xml_node_struct* dn = _data_new();
    
    		return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
    	}
    
    	PUGI__FN bool xml_text::set(bool rhs)
    	{
    		xml_node_struct* dn = _data_new();
    
    		return dn ? impl::set_value_convert(dn->value, dn->header, impl::xml_memory_page_value_allocated_mask, rhs) : false;
    	}
    
    	PUGI__FN xml_text& xml_text::operator=(const char_t* rhs)
    	{
    		set(rhs);
    		return *this;
    	}
    
    	PUGI__FN xml_text& xml_text::operator=(int rhs)
    	{
    		set(rhs);
    		return *this;
    	}
    
    	PUGI__FN xml_text& xml_text::operator=(unsigned int rhs)
    	{
    		set(rhs);
    		return *this;
    	}
    
    	PUGI__FN xml_text& xml_text::operator=(double rhs)
    	{
    		set(rhs);
    		return *this;
    	}
    
    	PUGI__FN xml_text& xml_text::operator=(bool rhs)
    	{
    		set(rhs);
    		return *this;
    	}
    
    	PUGI__FN xml_node xml_text::data() const
    	{
    		return xml_node(_data());
    	}
    
    #ifdef __BORLANDC__
    	PUGI__FN bool operator&&(const xml_text& lhs, bool rhs)
    	{
    		return (bool)lhs && rhs;
    	}
    
    	PUGI__FN bool operator||(const xml_text& lhs, bool rhs)
    	{
    		return (bool)lhs || rhs;
    	}
    #endif
    
    	PUGI__FN xml_node_iterator::xml_node_iterator()
    	{
    	}
    
    	PUGI__FN xml_node_iterator::xml_node_iterator(const xml_node& node): _wrap(node), _parent(node.parent())
    	{
    	}
    
    	PUGI__FN xml_node_iterator::xml_node_iterator(xml_node_struct* ref, xml_node_struct* parent): _wrap(ref), _parent(parent)
    	{
    	}
    
    	PUGI__FN bool xml_node_iterator::operator==(const xml_node_iterator& rhs) const
    	{
    		return _wrap._root == rhs._wrap._root && _parent._root == rhs._parent._root;
    	}
    	
    	PUGI__FN bool xml_node_iterator::operator!=(const xml_node_iterator& rhs) const
    	{
    		return _wrap._root != rhs._wrap._root || _parent._root != rhs._parent._root;
    	}
    
    	PUGI__FN xml_node& xml_node_iterator::operator*() const
    	{
    		assert(_wrap._root);
    		return _wrap;
    	}
    
    	PUGI__FN xml_node* xml_node_iterator::operator->() const
    	{
    		assert(_wrap._root);
    		return const_cast<xml_node*>(&_wrap); // BCC32 workaround
    	}
    
    	PUGI__FN const xml_node_iterator& xml_node_iterator::operator++()
    	{
    		assert(_wrap._root);
    		_wrap._root = _wrap._root->next_sibling;
    		return *this;
    	}
    
    	PUGI__FN xml_node_iterator xml_node_iterator::operator++(int)
    	{
    		xml_node_iterator temp = *this;
    		++*this;
    		return temp;
    	}
    
    	PUGI__FN const xml_node_iterator& xml_node_iterator::operator--()
    	{
    		_wrap = _wrap._root ? _wrap.previous_sibling() : _parent.last_child();
    		return *this;
    	}
    
    	PUGI__FN xml_node_iterator xml_node_iterator::operator--(int)
    	{
    		xml_node_iterator temp = *this;
    		--*this;
    		return temp;
    	}
    
    	PUGI__FN xml_attribute_iterator::xml_attribute_iterator()
    	{
    	}
    
    	PUGI__FN xml_attribute_iterator::xml_attribute_iterator(const xml_attribute& attr, const xml_node& parent): _wrap(attr), _parent(parent)
    	{
    	}
    
    	PUGI__FN xml_attribute_iterator::xml_attribute_iterator(xml_attribute_struct* ref, xml_node_struct* parent): _wrap(ref), _parent(parent)
    	{
    	}
    
    	PUGI__FN bool xml_attribute_iterator::operator==(const xml_attribute_iterator& rhs) const
    	{
    		return _wrap._attr == rhs._wrap._attr && _parent._root == rhs._parent._root;
    	}
    	
    	PUGI__FN bool xml_attribute_iterator::operator!=(const xml_attribute_iterator& rhs) const
    	{
    		return _wrap._attr != rhs._wrap._attr || _parent._root != rhs._parent._root;
    	}
    
    	PUGI__FN xml_attribute& xml_attribute_iterator::operator*() const
    	{
    		assert(_wrap._attr);
    		return _wrap;
    	}
    
    	PUGI__FN xml_attribute* xml_attribute_iterator::operator->() const
    	{
    		assert(_wrap._attr);
    		return const_cast<xml_attribute*>(&_wrap); // BCC32 workaround
    	}
    
    	PUGI__FN const xml_attribute_iterator& xml_attribute_iterator::operator++()
    	{
    		assert(_wrap._attr);
    		_wrap._attr = _wrap._attr->next_attribute;
    		return *this;
    	}