42 #include "pugixml.hpp" 45 #include "uhal/log/log.hpp" 57 void PugiXMLParseResultPrettifier (
const pugi::xml_parse_result& aLoadResult ,
const boost::filesystem::path& aPath ,
const std::vector<uint8_t>& aFile )
59 log (
Error() ,
"Failed to parse file " , aPath.c_str() ,
". PugiXML returned the following description " ,
Quote ( aLoadResult.description() ) ,
"." );
60 std::size_t lLineCounter ( 1 );
61 std::vector<uint8_t>::const_iterator lIt0 ( aFile.begin() );
62 std::vector<uint8_t>::const_iterator lIt1 ( aFile.begin() + aLoadResult.offset );
63 std::vector<uint8_t>::const_iterator lIt2 ( lIt1 );
65 for ( ; lIt0!=lIt1 ; ++lIt0 )
73 for ( ; lIt1 != aFile.begin() ; --lIt1 )
82 for ( ; lIt2 != aFile.end() ; ++lIt2 )
91 std::size_t lDist0 ( lIt0 - lIt1 );
92 std::size_t lDist1 ( lIt2 - lIt0 );
94 lLine.reserve ( lIt2 - lIt1 );
96 for ( ; lIt1 != lIt2 ; ++lIt1 )
98 if ( isprint ( *lIt1 ) || *lIt1==10 )
108 log (
Error() ,
"Error occured at line number " ,
Integer ( lLineCounter ) ,
109 ", character " ,
Integer ( lDist0+1 ) ,
"\n" 110 "LINE : " , lLine ,
"\n" 111 "ERROR LOCATION : " , std::string ( lDist0 ,
'_' ) ,
"^" , std::string ( lDist1 ,
'_' ) );
115 template <
bool DebugInfo >
116 bool GetXMLattribute (
const pugi::xml_node& aNode ,
const char* aAttrName , std::string& aTarget )
118 pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
120 if ( ! lAttr.empty() )
122 aTarget = lAttr.value();
129 log (
Error() ,
"Failed to get attribute " ,
Quote ( aAttrName ) ,
" from XML node." );
136 template bool GetXMLattribute<true>(
const pugi::xml_node& aNode ,
const char* aAttrName , std::string& aTarget );
137 template bool GetXMLattribute<false>(
const pugi::xml_node& aNode ,
const char* aAttrName , std::string& aTarget );
140 template <
bool DebugInfo >
141 bool GetXMLattribute (
const pugi::xml_node& aNode ,
const char* aAttrName ,
const char* aTarget )
143 pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
145 if ( ! lAttr.empty() )
147 aTarget = lAttr.value();
154 log (
Error() ,
"Failed to get attribute " ,
Quote ( aAttrName ) ,
" from XML node." );
161 template bool GetXMLattribute<true>(
const pugi::xml_node& aNode ,
const char* aAttrName ,
const char* aTarget );
162 template bool GetXMLattribute<false>(
const pugi::xml_node& aNode ,
const char* aAttrName ,
const char* aTarget );
165 template <
bool DebugInfo >
166 bool GetXMLattribute (
const pugi::xml_node& aNode ,
const char* aAttrName , int32_t& aTarget )
168 pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
170 if ( ! lAttr.empty() )
172 std::string lAttrStr ( lAttr.value() );
173 std::stringstream ss;
176 if ( lAttrStr.size() > 2 )
178 if ( ( lAttrStr[1] ==
'x' ) || ( lAttrStr[1] ==
'X' ) )
180 ss <<
std::hex << lAttrStr.substr ( 2 );
187 else if ( lAttrStr.size() > 1 )
189 if ( ( lAttrStr[0] ==
'x' ) || ( lAttrStr[0] ==
'X' ) )
191 ss <<
std::hex << lAttrStr.substr ( 1 );
206 if ( ss.str().size() > 10 )
208 exception::StringNumberWillNotFitInto32BitNumber lExc;
209 log ( lExc ,
"XML attribute " ,
Quote ( aAttrName ) ,
" has value " ,
Quote ( ss.str() ) ,
" which is too big to fit into 32-bit number" );
218 exception::StringNumberWillNotFitInto32BitNumber lExc;
219 log ( lExc ,
"XML attribute " ,
Quote ( aAttrName ) ,
" has value " ,
Quote ( ss.str() ) ,
" which is too big to fit into 32-bit number" );
223 aTarget = ( int32_t ) ( lTarget );
230 log (
Error() ,
"Failed to get attribute " ,
Quote ( aAttrName ) ,
" from XML node." );
237 template bool GetXMLattribute<true>(
const pugi::xml_node& aNode ,
const char* aAttrName , int32_t& aTarget );
238 template bool GetXMLattribute<false>(
const pugi::xml_node& aNode ,
const char* aAttrName , int32_t& aTarget );
241 template <
bool DebugInfo >
242 bool GetXMLattribute (
const pugi::xml_node& aNode ,
const char* aAttrName , uint32_t& aTarget )
244 pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
246 if ( ! lAttr.empty() )
248 std::string lAttrStr ( lAttr.value() );
249 std::stringstream ss;
252 if ( lAttrStr.size() > 2 )
254 if ( ( lAttrStr[1] ==
'x' ) || ( lAttrStr[1] ==
'X' ) )
256 ss <<
std::hex << lAttrStr.substr ( 2 );
263 else if ( lAttrStr.size() > 1 )
265 if ( ( lAttrStr[0] ==
'x' ) || ( lAttrStr[0] ==
'X' ) )
267 ss <<
std::hex << lAttrStr.substr ( 1 );
282 if ( ss.str().size() > 10 )
284 exception::StringNumberWillNotFitInto32BitNumber lExc;
285 log ( lExc ,
"XML attribute " ,
Quote ( aAttrName ) ,
" has value " ,
Quote ( ss.str() ) ,
" which is too big to fit into 32-bit number" );
294 exception::StringNumberWillNotFitInto32BitNumber lExc;
295 log ( lExc ,
"XML attribute " ,
Quote ( aAttrName ) ,
" has value " ,
Quote ( ss.str() ) ,
" which is too big to fit into 32-bit number" );
299 aTarget = ( uint32_t ) ( lTarget );
306 log (
Error() ,
"Failed to get attribute " ,
Quote ( aAttrName ) ,
" from XML node." );
313 template bool GetXMLattribute<true>(
const pugi::xml_node& aNode ,
const char* aAttrName , uint32_t& aTarget );
314 template bool GetXMLattribute<false>(
const pugi::xml_node& aNode ,
const char* aAttrName , uint32_t& aTarget );
317 template <
bool DebugInfo >
318 bool GetXMLattribute (
const pugi::xml_node& aNode ,
const char* aAttrName ,
double& aTarget )
320 pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
322 if ( ! lAttr.empty() )
324 aTarget = lAttr.as_double();
331 log (
Error() ,
"Failed to get attribute " ,
Quote ( aAttrName ) ,
" from XML node." );
338 template bool GetXMLattribute<true>(
const pugi::xml_node& aNode ,
const char* aAttrName ,
double& aTarget );
339 template bool GetXMLattribute<false>(
const pugi::xml_node& aNode ,
const char* aAttrName ,
double& aTarget );
342 template <
bool DebugInfo >
343 bool GetXMLattribute (
const pugi::xml_node& aNode ,
const char* aAttrName ,
float& aTarget )
345 pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
347 if ( ! lAttr.empty() )
349 aTarget = lAttr.as_float();
356 log (
Error() ,
"Failed to get attribute " ,
Quote ( aAttrName ) ,
" from XML node." );
363 template bool GetXMLattribute<true>(
const pugi::xml_node& aNode ,
const char* aAttrName ,
float& aTarget );
364 template bool GetXMLattribute<false>(
const pugi::xml_node& aNode ,
const char* aAttrName ,
float& aTarget );
367 template <
bool DebugInfo >
368 bool GetXMLattribute (
const pugi::xml_node& aNode ,
const char* aAttrName ,
bool& aTarget )
370 pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
372 if ( ! lAttr.empty() )
374 aTarget = lAttr.as_bool();
381 log (
Error() ,
"Failed to get attribute " ,
Quote ( aAttrName ) ,
" from XML node." );
388 template bool GetXMLattribute<true>(
const pugi::xml_node& aNode ,
const char* aAttrName,
bool& aTarget );
389 template bool GetXMLattribute<false>(
const pugi::xml_node& aNode ,
const char* aAttrName,
bool& aTarget );
bool GetXMLattribute(const pugi::xml_node &aNode, const char *aAttrName, std::string &aTarget)
Helper function to retrieve a named attribute from a PugiXML node and cast it to the correct type...
template bool GetXMLattribute< true >(const pugi::xml_node &aNode, const char *aAttrName, std::string &aTarget)
void PugiXMLParseResultPrettifier(const pugi::xml_parse_result &aLoadResult, const boost::filesystem::path &aPath, const std::vector< uint8_t > &aFile)
Helper function to make debugging failures when parsing XML files easier.
_Quote< T > Quote(const T &aT)
template bool GetXMLattribute< false >(const pugi::xml_node &aNode, const char *aAttrName, std::string &aTarget)
_Integer< T, IntFmt<> > Integer(const T &aT)
Forward declare a function which creates an instance of the ultra-lightweight wrapper from an integer...