ptree_ptr ptree_proxy::merge(ptree_ptr other) const { if (other->merkle() != m_merkle) { // If there is a mismatch, fail hard return ptree_ptr(); } // Otherwise, anything is better than me return other; }
ptree_ptr ptree_leaf::merge(ptree_ptr other) const { if (m_merkle != other->merkle()) { // If there is a mismatch, fail hard return ptree_ptr(); } // Otherwise, return me return shared_from_this(); }
void xml_compiler::read_xml_(ptree_ptr& out, const std::string& file_path, const pqrs::string::replacement& replacement) const { try { out.reset(new boost::property_tree::ptree()); std::string xml; if (replacement.empty()) { pqrs::string::string_from_file(xml, file_path.c_str()); } else { pqrs::string::string_by_replacing_double_curly_braces_from_file(xml, replacement_warnings_, file_path.c_str(), replacement); } if (xml.empty()) { // Show warning message when we failed to read file. // // If private.xml includes files in network file system, it might fail to read. // For that case, we continue reading with ignoring missing files. xml += "<?xml version=\"1.0\"?>\n" "<root>" " <item>" " <name style=\"caution\">Caution:</name>" " <appendix><![CDATA["; xml += file_path + " is not found."; xml += " ]]></appendix>" " </item>" "</root>"; } std::stringstream istream(xml, std::stringstream::in); int flags = boost::property_tree::xml_parser::no_comments; boost::property_tree::read_xml(istream, *out, flags); } catch (std::exception& e) { std::string what = e.what(); // Hack: // boost::property_tree::read_xml throw exception with filename. // But, when we call read_xml with stream, the filename becomes "unspecified file" as follow. // // <unspecified file>(4): expected element name // // So, we change "unspecified file" to file name by ourself. boost::replace_first(what, "<unspecified file>", std::string("<") + file_path + ">"); error_information_.set(what); } }
ptree_ptr ptree_branch::merge(ptree_ptr other) const { if (m_merkle != other->merkle()) { // If there is a mismatch, fail hard return ptree_ptr(); } if (complete() || other->is_proxy()) { // If I am complete, or the other side is a proxy // pick this entry return shared_from_this(); } // At this point, othe side should be a branch shared_ptr<const ptree_branch> obranch = std::dynamic_pointer_cast<const ptree_branch>(other); if (!obranch) { // If not, fail return ptree_ptr(); } // Merge both children return make_shared<ptree_branch>(m_split_pos, m_branches[0]->merge(obranch->m_branches[0]), m_branches[1]->merge(obranch->m_branches[1])); }
void xml_compiler::read_xml_(ptree_ptr& out, const std::string& file_path, const pqrs::string::replacement& replacement) const { try { out.reset(new boost::property_tree::ptree()); std::string xml; if (replacement.empty()) { pqrs::string::string_from_file(xml, file_path.c_str()); } else { pqrs::string::string_by_replacing_double_curly_braces_from_file(xml, replacement_warnings_, file_path.c_str(), replacement); } if (xml.empty()) { error_information_.set(file_path + " is not found."); return; } std::stringstream istream(xml, std::stringstream::in); int flags = boost::property_tree::xml_parser::no_comments; boost::property_tree::read_xml(istream, *out, flags); } catch (std::exception& e) { std::string what = e.what(); // Hack: // boost::property_tree::read_xml throw exception with filename. // But, when we call read_xml with stream, the filename becomes "unspecified file" as follow. // // <unspecified file>(4): expected element name // // So, we change "unspecified file" to file name by ourself. boost::replace_first(what, "<unspecified file>", std::string("<") + file_path + ">"); error_information_.set(what); } }
static void write_proxy(writable& out, const ptree_ptr& p) { out.write(pid_proxy); out.write(p->node_count()); out.write(p->merkle()); }
ptree_branch::ptree_branch(uint8_t split_pos, const ptree_ptr& p1, const ptree_ptr& p2) : m_branches{ p1, p2 } , m_node_count(p1->node_count() + p2->node_count()) , m_split_pos(split_pos) , m_complete(p1->complete() && p2->complete()) { if (p1->is_proxy()) { // p1 is proxy, p2 must be valid, and branches are in order m_prefix = p2->prefix(); } else if (p2->is_proxy()) { // p2 is proxy, p1 must be valid, and branches are in order m_prefix = p1->prefix(); } else { // Both are valid, but might be out of order m_prefix = p1->prefix(); if (p1->prefix() > p2->prefix()) { swap(m_branches[0], m_branches[1]); } } m_prefix.zero_above(split_pos); m_merkle = make_digest(m_prefix, m_node_count, m_branches[0]->merkle(), m_branches[1]->merkle()); }