μHAL (v2.7.9)
Part of the IPbus software repository
xml.cpp
Go to the documentation of this file.
1 /*
2 ---------------------------------------------------------------------------
3 
4  This file is part of uHAL.
5 
6  uHAL is a hardware access library and programming framework
7  originally developed for upgrades of the Level-1 trigger of the CMS
8  experiment at CERN.
9 
10  uHAL is free software: you can redistribute it and/or modify
11  it under the terms of the GNU General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  uHAL is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU General Public License for more details.
19 
20  You should have received a copy of the GNU General Public License
21  along with uHAL. If not, see <http://www.gnu.org/licenses/>.
22 
23 
24  Andrew Rose, Imperial College, London
25  email: awr01 <AT> imperial.ac.uk
26 
27  Marc Magrans de Abril, CERN
28  email: marc.magrans.de.abril <AT> cern.ch
29 
30  Tom Williams, Rutherford Appleton Laboratory, Oxfordshire
31  email: tom.williams <AT> cern.ch
32 
33 ---------------------------------------------------------------------------
34 */
35 
36 #include "uhal/utilities/xml.hpp"
37 
38 
39 #include <ctype.h>
40 #include <sstream>
41 
42 #include "pugixml.hpp"
43 
44 #include "uhal/log/LogLevels.hpp"
45 #include "uhal/log/log.hpp"
48 
49 
50 
51 // ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
52 namespace uhal
53 {
54  namespace utilities
55  {
56 
57  void PugiXMLParseResultPrettifier ( const pugi::xml_parse_result& aLoadResult , const boost::filesystem::path& aPath , const std::vector<uint8_t>& aFile )
58  {
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 );
64 
65  for ( ; lIt0!=lIt1 ; ++lIt0 )
66  {
67  if ( *lIt0 == '\n' )
68  {
69  lLineCounter++;
70  }
71  }
72 
73  for ( ; lIt1 != aFile.begin() ; --lIt1 )
74  {
75  if ( *lIt1 == '\n' )
76  {
77  ++lIt1;
78  break;
79  }
80  }
81 
82  for ( ; lIt2 != aFile.end() ; ++lIt2 )
83  {
84  if ( *lIt2 == '\n' )
85  {
86  --lIt2;
87  break;
88  }
89  }
90 
91  std::size_t lDist0 ( lIt0 - lIt1 );
92  std::size_t lDist1 ( lIt2 - lIt0 );
93  std::string lLine;
94  lLine.reserve ( lIt2 - lIt1 );
95 
96  for ( ; lIt1 != lIt2 ; ++lIt1 )
97  {
98  if ( isprint ( *lIt1 ) || *lIt1==10 )
99  {
100  lLine += *lIt1;
101  }
102  else
103  {
104  lLine += '#';
105  }
106  }
107 
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 , '_' ) );
112  }
113 
114 
115  template < bool DebugInfo >
116  bool GetXMLattribute ( const pugi::xml_node& aNode , const std::string& aAttrName , std::string& aTarget )
117  {
118  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName.c_str() );
119 
120  if ( ! lAttr.empty() )
121  {
122  aTarget = lAttr.value();
123  return true;
124  }
125  else
126  {
127  if ( DebugInfo )
128  {
129  log ( Error() , "Failed to get attribute " , Quote ( aAttrName ) , " from XML node." );
130  }
131 
132  return false;
133  }
134  }
135 
136  template bool GetXMLattribute<true>( const pugi::xml_node& aNode , const std::string& aAttrName , std::string& aTarget );
137  template bool GetXMLattribute<false>( const pugi::xml_node& aNode , const std::string& aAttrName , std::string& aTarget );
138 
139 
140  template < bool DebugInfo >
141  bool GetXMLattribute ( const pugi::xml_node& aNode , const std::string& aAttrName , const char* aTarget )
142  {
143  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName.c_str() );
144 
145  if ( ! lAttr.empty() )
146  {
147  aTarget = lAttr.value();
148  return true;
149  }
150  else
151  {
152  if ( DebugInfo )
153  {
154  log ( Error() , "Failed to get attribute " , Quote ( aAttrName ) , " from XML node." );
155  }
156 
157  return false;
158  }
159  }
160 
161  template bool GetXMLattribute<true>( const pugi::xml_node& aNode , const std::string& aAttrName , const char* aTarget );
162  template bool GetXMLattribute<false>( const pugi::xml_node& aNode , const std::string& aAttrName , const char* aTarget );
163 
164 
165  template < bool DebugInfo >
166  bool GetXMLattribute ( const pugi::xml_node& aNode , const std::string& aAttrName , int32_t& aTarget )
167  {
168  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName.c_str() );
169 
170  if ( ! lAttr.empty() )
171  {
172  std::string lAttrStr ( lAttr.value() );
173  std::stringstream ss;
174 
175  //if string is of the form "x89abcdef" , "X89abcdef" , "0x89abcdef" , "0X89abcdef"
176  if ( lAttrStr.size() > 2 )
177  {
178  if ( ( lAttrStr[1] == 'x' ) || ( lAttrStr[1] == 'X' ) )
179  {
180  ss << std::hex << lAttrStr.substr ( 2 );
181  }
182  else
183  {
184  ss << lAttrStr;
185  }
186  }
187  else if ( lAttrStr.size() > 1 )
188  {
189  if ( ( lAttrStr[0] == 'x' ) || ( lAttrStr[0] == 'X' ) )
190  {
191  ss << std::hex << lAttrStr.substr ( 1 );
192  }
193  else
194  {
195  ss << lAttrStr;
196  }
197  }
198  else
199  {
200  ss << lAttrStr;
201  }
202 
203  // ss >> aTarget;
204  // aTarget = lAttr.as_int();
205 
206  if ( ss.str().size() > 10 )
207  {
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" );
210  throw lExc;
211  }
212 
213  int64_t lTarget;
214  ss >> lTarget;
215 
216  if ( lTarget>>32 )
217  {
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" );
220  throw lExc;
221  }
222 
223  aTarget = ( int32_t ) ( lTarget );
224  return true;
225  }
226  else
227  {
228  if ( DebugInfo )
229  {
230  log ( Error() , "Failed to get attribute " , Quote ( aAttrName ) , " from XML node." );
231  }
232 
233  return false;
234  }
235  }
236 
237  template bool GetXMLattribute<true>( const pugi::xml_node& aNode , const std::string& aAttrName , int32_t& aTarget );
238  template bool GetXMLattribute<false>( const pugi::xml_node& aNode , const std::string& aAttrName , int32_t& aTarget );
239 
240 
241  template < bool DebugInfo >
242  bool GetXMLattribute ( const pugi::xml_node& aNode , const std::string& aAttrName , uint32_t& aTarget )
243  {
244  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName.c_str() );
245 
246  if ( ! lAttr.empty() )
247  {
248  std::string lAttrStr ( lAttr.value() );
249  std::stringstream ss;
250 
251  size_t lBasePrefixLength = 0;
252 
253  if ( lAttrStr.empty() )
254  throw exception::NodeAttributeIncorrectValue("XML attribute '" + aAttrName + "' is empty, so cannot be converted to uint32_t");
255 
256  //if string is of the form "x89abcdef" , "X89abcdef" , "0x89abcdef" , "0X89abcdef"
257  if ( lAttrStr.size() > 2 )
258  {
259  if ( ( lAttrStr[0] == '0' ) and ( ( lAttrStr[1] == 'x' ) or ( lAttrStr[1] == 'X' ) ) )
260  {
261  lBasePrefixLength = 2;
262  ss << std::hex;
263  }
264  }
265 
266  if ( lAttrStr.size() > 1 )
267  {
268  if ( ( lAttrStr[0] == 'x' ) || ( lAttrStr[0] == 'X' ) )
269  {
270  lBasePrefixLength = 1;
271  ss << std::hex;
272  }
273  }
274 
275  if ( lAttrStr.find_first_not_of(lBasePrefixLength ? "0123456789abcdefABCDEF" : "0123456789", lBasePrefixLength) != std::string::npos )
276  throw exception::NodeAttributeIncorrectValue("XML attribute '" + aAttrName + "' has value '" + lAttrStr + "' that cannot be converted to uint32_t");
277 
278  ss << lAttrStr.substr(lBasePrefixLength);
279 
280 
281  if ( ss.str().size() > 10 )
282  {
283  exception::StringNumberWillNotFitInto32BitNumber lExc;
284  log ( lExc , "XML attribute " , Quote ( aAttrName ) , " has value " , Quote ( ss.str() ) , " which is too big to fit into 32-bit number" );
285  throw lExc;
286  }
287 
288  uint64_t lTarget;
289  ss >> lTarget;
290 
291  if ( lTarget>>32 )
292  {
293  exception::StringNumberWillNotFitInto32BitNumber lExc;
294  log ( lExc , "XML attribute " , Quote ( aAttrName ) , " has value " , Quote ( ss.str() ) , " which is too big to fit into 32-bit number" );
295  throw lExc;
296  }
297 
298  aTarget = ( uint32_t ) ( lTarget );
299  return true;
300  }
301  else
302  {
303  if ( DebugInfo )
304  {
305  log ( Error() , "Failed to get attribute " , Quote ( aAttrName ) , " from XML node." );
306  }
307 
308  return false;
309  }
310  }
311 
312  template bool GetXMLattribute<true>( const pugi::xml_node& aNode , const std::string& aAttrName , uint32_t& aTarget );
313  template bool GetXMLattribute<false>( const pugi::xml_node& aNode , const std::string& aAttrName , uint32_t& aTarget );
314 
315 
316  template < bool DebugInfo >
317  bool GetXMLattribute ( const pugi::xml_node& aNode , const std::string& aAttrName , double& aTarget )
318  {
319  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName.c_str() );
320 
321  if ( ! lAttr.empty() )
322  {
323  aTarget = lAttr.as_double();
324  return true;
325  }
326  else
327  {
328  if ( DebugInfo )
329  {
330  log ( Error() , "Failed to get attribute " , Quote ( aAttrName ) , " from XML node." );
331  }
332 
333  return false;
334  }
335  }
336 
337  template bool GetXMLattribute<true>( const pugi::xml_node& aNode , const std::string& aAttrName , double& aTarget );
338  template bool GetXMLattribute<false>( const pugi::xml_node& aNode , const std::string& aAttrName , double& aTarget );
339 
340 
341  template < bool DebugInfo >
342  bool GetXMLattribute ( const pugi::xml_node& aNode , const std::string& aAttrName , float& aTarget )
343  {
344  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName.c_str() );
345 
346  if ( ! lAttr.empty() )
347  {
348  aTarget = lAttr.as_float();
349  return true;
350  }
351  else
352  {
353  if ( DebugInfo )
354  {
355  log ( Error() , "Failed to get attribute " , Quote ( aAttrName ) , " from XML node." );
356  }
357 
358  return false;
359  }
360  }
361 
362  template bool GetXMLattribute<true>( const pugi::xml_node& aNode , const std::string& aAttrName , float& aTarget );
363  template bool GetXMLattribute<false>( const pugi::xml_node& aNode , const std::string& aAttrName , float& aTarget );
364 
365 
366  template < bool DebugInfo >
367  bool GetXMLattribute ( const pugi::xml_node& aNode , const std::string& aAttrName , bool& aTarget )
368  {
369  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName.c_str() );
370 
371  if ( ! lAttr.empty() )
372  {
373  aTarget = lAttr.as_bool();
374  return true;
375  }
376  else
377  {
378  if ( DebugInfo )
379  {
380  log ( Error() , "Failed to get attribute " , Quote ( aAttrName ) , " from XML node." );
381  }
382 
383  return false;
384  }
385  }
386 
387  template bool GetXMLattribute<true>( const pugi::xml_node& aNode , const std::string& aAttrName, bool& aTarget );
388  template bool GetXMLattribute<false>( const pugi::xml_node& aNode , const std::string& aAttrName, bool& aTarget );
389  }
390 }
pugi::xml_attribute
Definition: pugixml.hpp:349
pugi::xml_attribute::as_float
float as_float(float def=0) const
Definition: pugixml.cpp:5190
xml.hpp
pugixml.hpp
pugi::xml_attribute::as_bool
bool as_bool(bool def=false) const
Definition: pugixml.cpp:5195
uhal
Definition: HttpResponseGrammar.hpp:49
pugi::xml_attribute::as_double
double as_double(double def=0) const
Definition: pugixml.cpp:5185
log_inserters.quote.hpp
pugi::xml_parse_result::description
const char * description() const
Definition: pugixml.cpp:6819
uhal::log
void log(FatalLevel &aFatal, const T0 &aArg0)
Function to add a log entry at Fatal level.
Definition: log.hxx:20
log_inserters.integer.hpp
uhal::Integer
_Integer< T, IntFmt<> > Integer(const T &aT)
Forward declare a function which creates an instance of the ultra-lightweight wrapper from an integer...
Definition: log_inserters.integer.hxx:43
pugi::xml_node
Definition: pugixml.hpp:455
pugi::xml_attribute::value
const char_t * value() const
Definition: pugixml.cpp:5222
uhal::hex
@ hex
Hexadecimal.
Definition: log_inserters.integer.hpp:51
pugi::xml_attribute::empty
bool empty() const
Definition: pugixml.cpp:5212
uhal::Error
ErrorLevel Error
Definition: LogLevels.cpp:61
uhal::utilities::GetXMLattribute
bool GetXMLattribute(const pugi::xml_node &aNode, const std::string &aAttrName, std::string &aTarget)
Helper function to retrieve a named attribute from a PugiXML node and cast it to the correct type.
Definition: xml.cpp:116
uhal::Quote
_Quote< T > Quote(const T &aT)
Definition: log_inserters.quote.hxx:49
uhal::utilities::GetXMLattribute< true >
template bool GetXMLattribute< true >(const pugi::xml_node &aNode, const std::string &aAttrName, std::string &aTarget)
pugi::xml_parse_result::offset
ptrdiff_t offset
Definition: pugixml.hpp:984
uhal::utilities::GetXMLattribute< false >
template bool GetXMLattribute< false >(const pugi::xml_node &aNode, const std::string &aAttrName, std::string &aTarget)
uhal::utilities::PugiXMLParseResultPrettifier
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.
Definition: xml.cpp:57
pugi::xml_node::attribute
xml_attribute attribute(const char_t *name) const
Definition: pugixml.cpp:5507
log.hpp
LogLevels.hpp
pugi::xml_parse_result
Definition: pugixml.hpp:979