1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <string>
#include <fstream>
#include <sstream>
#include <rapidxml.hpp>
#include <rapidxml_print.hpp>
#include <rapidxml_utils.hpp>
#include "fileManager.hpp"
void filesystem::exportItem(item itemData, int itemID){
std::ostringstream convert;
//convert the itemID into a string as a filename, for example, 1.item
std::string filename;
convert << itemID;
filename = convert.str();
filename.append(".item");
//convert each the data from the item into strings
std::string equipmentSlot;
std::string armorRating;
std::string minDmg;
std::string maxDmg;
convert << itemData.equipment_slot;
equipmentSlot = convert.str();
convert << itemData.armor_rating;
armorRating = convert.str();
convert << itemData.min_damage;
minDmg = convert.str();
convert << itemData.max_damage;
maxDmg = convert.str();
//open the XML document to write to
rapidxml::xml_document<> doc;
//write the basic information to the document
rapidxml::xml_node<>* decl = doc.allocate_node(rapidxml::node_declaration);
decl->append_attribute(doc.allocate_attribute("version", "1.0"));
decl->append_attribute(doc.allocate_attribute("encoding", "utf-8"));
doc.append_node(decl);
//write the item's attributes to the rootnode
rapidxml::xml_node<>* root = doc.allocate_node(rapidxml::node_element, "rootnode");
root->append_attribute(doc.allocate_attribute("itemName", itemData.name.c_str()));
root->append_attribute(doc.allocate_attribute("itemDescription", itemData.description.c_str()));
root->append_attribute(doc.allocate_attribute("icon", itemData.icon.c_str()));
root->append_attribute(doc.allocate_attribute("equipmentSlot", equipmentSlot.c_str()));
root->append_attribute(doc.allocate_attribute("armorRating", armorRating.c_str()));
root->append_attribute(doc.allocate_attribute("minDMG", minDmg.c_str()));
root->append_attribute(doc.allocate_attribute("maxDMG", maxDmg.c_str()));
rapidxml::xml_node<>* child = doc.allocate_node(rapidxml::node_element, "childnode");
root->append_node(child);
//write the information to the file, for example, 1.item
std::string xml_as_string;
rapidxml::print(std::back_inserter(xml_as_string), doc);
std::ofstream file(filename.c_str());
file << doc;
file.close();
doc.clear();
}
void filesystem::importItem(item itemData, int itemID){
std::ostringstream convert;
//convert the itemID into a string as a filename, for example, 1.item
std::string filename;
convert << itemID;
filename = convert.str();
filename.append(".item");
//open the XML document
rapidxml::xml_document<> doc;
rapidxml::file<> file(filename.c_str());
doc.parse<0> (file.data());
std::string name;
std::string description;
std::string icon;
std::string equipmentSlot;
std::string armorRating;
std::string minDMG;
std::string maxDMG;
//open the rootnode where all the information is saved
rapidxml::xml_node<> *n = doc.first_node("itemAttributes")->first_node();
//read each individual attribute from the XML document
convert << n->first_attribute("itemName")->value();
name = convert.str();
itemData.name = name;
convert << n->next_sibling()->value();
description = convert.str();
itemData.description = description;
convert << n->next_sibling()->value();
icon = convert.str();
itemData.icon = icon;
convert << n->next_sibling()->value();
equipmentSlot = convert.str();
int v_equipmentSlot = atoi(equipmentSlot.c_str());
itemData.equipment_slot = v_equipmentSlot;
convert << n->next_sibling()->value();
armorRating = convert.str();
int v_armorRating = atoi(armorRating.c_str());
itemData.armor_rating = v_armorRating;
convert << n->next_sibling()->value();
minDMG = convert.str();
int v_minDMG = atoi(minDMG.c_str());
itemData.min_damage = v_minDMG;
convert << n->last_attribute()->value();
maxDMG = convert.str();
int v_maxDMG = atoi(maxDMG.c_str());
itemData.max_damage = v_maxDMG;
}
|