static void append_environments_to_replacement(pqrs::string::replacement& r) { if (r.find("ENV_HOME") == r.end()) { const char* p = std::getenv("HOME"); if (!p) { p = ""; } r["ENV_HOME"] = p; } }
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); } }
void xml_compiler::append_environments_to_replacement_(pqrs::string::replacement& r) const { if (r.find("ENV_HOME") == r.end()) { const char* p = std::getenv("HOME"); if (!p) { p = ""; } r["ENV_HOME"] = p; } r.emplace("ENV_Karabiner_Resources", system_xml_directory_); if (r.find("ENV_Select_the_previous_input_source_shortcut") == r.end()) { std::string shortcut = get_select_the_previous_input_source_shortcut(); r.emplace("ENV_Select_the_previous_input_source_shortcut", shortcut); } if (r.find("ENV_Select_next_source_in_input_menu_shortcut") == r.end()) { std::string shortcut = get_select_next_source_in_input_menu_shortcut(); r.emplace("ENV_Select_next_source_in_input_menu_shortcut", shortcut); } }
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); } }