26 #include <boost/property_tree/xml_parser.hpp>
28 #include <SimoxUtility/algorithm/string/string_tools.h>
33 #define EDITORFILEOPENER_CONFIGFILE "ArmarXGui/EditorFileOpenerConfig.xml"
34 #define FILE_VARIABLE "$file"
35 #define LINE_VARIABLE "$line"
36 #define DEFAULT_EDITOR "qtcreator"
42 setTag(
"EditorFileOpener");
43 using namespace boost::property_tree;
48 if (configFile.empty())
61 xml_parser::read_xml(configFile, pt);
63 catch (xml_parser::xml_parser_error& e)
66 ARMARX_WARNING <<
"Could not load state-configfile '" << configFile
67 <<
"'. Required parameters may be unset.\nReason at " << e.filename()
68 <<
":" << e.line() <<
":\n"
69 << e.message() <<
flush;
73 for (ptree::value_type
const&
v : pt.get_child(
"editors"))
75 if (
v.first ==
"editor")
77 std::string editorName =
v.second.get<std::string>(
"name");
78 std::string openCommandLine =
v.second.get<std::string>(
"opencommandline");
79 editors[editorName] = openCommandLine;
86 const std::string& filepath,
89 std::string openCommandLine = editors[editorName];
91 if (openCommandLine.empty())
93 ARMARX_WARNING <<
"Cannot find editor named '" << editorName <<
"'";
97 std::string::size_type lenFile = std::string(
FILE_VARIABLE).size();
98 std::string::size_type posFile = openCommandLine.find(
FILE_VARIABLE);
100 if (posFile != std::string::npos)
102 openCommandLine.replace(posFile, lenFile, filepath);
105 std::string::size_type lenLine = std::string(
LINE_VARIABLE).size();
106 std::string::size_type posLine = openCommandLine.find(
LINE_VARIABLE);
107 std::stringstream lineStr;
108 lineStr << lineNumber;
110 if (posLine != std::string::npos)
112 openCommandLine.replace(posLine, lenLine, lineStr.str());
115 simox::alg::trim(openCommandLine);
117 if (*openCommandLine.rbegin() !=
'&')
119 openCommandLine +=
" &";
124 if (std::system(openCommandLine.c_str()))
132 const char* envEditor = std::getenv(
"ARMARX_EDITOR");
134 const std::string editorName = [&]() -> std::string
136 if (envEditor ==
nullptr)
141 const std::string editor = std::string(envEditor);
142 if (editors.count(editor) > 0)
147 ARMARX_WARNING <<
"The editor '" << editor <<
"' is not registered. "
153 openFile(editorName, filepath, lineNumber);