μHAL (v2.6.5)
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 char* aAttrName , std::string& aTarget )
117  {
118  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
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 char* aAttrName , std::string& aTarget );
137  template bool GetXMLattribute<false>( const pugi::xml_node& aNode , const char* aAttrName , std::string& aTarget );
138 
139 
140  template < bool DebugInfo >
141  bool GetXMLattribute ( const pugi::xml_node& aNode , const char* aAttrName , const char* aTarget )
142  {
143  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
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 char* aAttrName , const char* aTarget );
162  template bool GetXMLattribute<false>( const pugi::xml_node& aNode , const char* aAttrName , const char* aTarget );
163 
164 
165  template < bool DebugInfo >
166  bool GetXMLattribute ( const pugi::xml_node& aNode , const char* aAttrName , int32_t& aTarget )
167  {
168  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
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 char* aAttrName , int32_t& aTarget );
238  template bool GetXMLattribute<false>( const pugi::xml_node& aNode , const char* aAttrName , int32_t& aTarget );
239 
240 
241  template < bool DebugInfo >
242  bool GetXMLattribute ( const pugi::xml_node& aNode , const char* aAttrName , uint32_t& aTarget )
243  {
244  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
245 
246  if ( ! lAttr.empty() )
247  {
248  std::string lAttrStr ( lAttr.value() );
249  std::stringstream ss;
250 
251  //if string is of the form "x89abcdef" , "X89abcdef" , "0x89abcdef" , "0X89abcdef"
252  if ( lAttrStr.size() > 2 )
253  {
254  if ( ( lAttrStr[1] == 'x' ) || ( lAttrStr[1] == 'X' ) )
255  {
256  ss << std::hex << lAttrStr.substr ( 2 );
257  }
258  else
259  {
260  ss << lAttrStr;
261  }
262  }
263  else if ( lAttrStr.size() > 1 )
264  {
265  if ( ( lAttrStr[0] == 'x' ) || ( lAttrStr[0] == 'X' ) )
266  {
267  ss << std::hex << lAttrStr.substr ( 1 );
268  }
269  else
270  {
271  ss << lAttrStr;
272  }
273  }
274  else
275  {
276  ss << lAttrStr;
277  }
278 
279  // ss >> aTarget;
280  // aTarget = lAttr.as_uint();
281 
282  if ( ss.str().size() > 10 )
283  {
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" );
286  throw lExc;
287  }
288 
289  uint64_t lTarget;
290  ss >> lTarget;
291 
292  if ( lTarget>>32 )
293  {
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" );
296  throw lExc;
297  }
298 
299  aTarget = ( uint32_t ) ( lTarget );
300  return true;
301  }
302  else
303  {
304  if ( DebugInfo )
305  {
306  log ( Error() , "Failed to get attribute " , Quote ( aAttrName ) , " from XML node." );
307  }
308 
309  return false;
310  }
311  }
312 
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 );
315 
316 
317  template < bool DebugInfo >
318  bool GetXMLattribute ( const pugi::xml_node& aNode , const char* aAttrName , double& aTarget )
319  {
320  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
321 
322  if ( ! lAttr.empty() )
323  {
324  aTarget = lAttr.as_double();
325  return true;
326  }
327  else
328  {
329  if ( DebugInfo )
330  {
331  log ( Error() , "Failed to get attribute " , Quote ( aAttrName ) , " from XML node." );
332  }
333 
334  return false;
335  }
336  }
337 
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 );
340 
341 
342  template < bool DebugInfo >
343  bool GetXMLattribute ( const pugi::xml_node& aNode , const char* aAttrName , float& aTarget )
344  {
345  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
346 
347  if ( ! lAttr.empty() )
348  {
349  aTarget = lAttr.as_float();
350  return true;
351  }
352  else
353  {
354  if ( DebugInfo )
355  {
356  log ( Error() , "Failed to get attribute " , Quote ( aAttrName ) , " from XML node." );
357  }
358 
359  return false;
360  }
361  }
362 
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 );
365 
366 
367  template < bool DebugInfo >
368  bool GetXMLattribute ( const pugi::xml_node& aNode , const char* aAttrName , bool& aTarget )
369  {
370  pugi::xml_attribute lAttr = aNode.attribute ( aAttrName );
371 
372  if ( ! lAttr.empty() )
373  {
374  aTarget = lAttr.as_bool();
375  return true;
376  }
377  else
378  {
379  if ( DebugInfo )
380  {
381  log ( Error() , "Failed to get attribute " , Quote ( aAttrName ) , " from XML node." );
382  }
383 
384  return false;
385  }
386  }
387 
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 );
390  }
391 }
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...
Definition: xml.cpp:116
template bool GetXMLattribute< true >(const pugi::xml_node &aNode, const char *aAttrName, std::string &aTarget)
ErrorLevel Error
Definition: LogLevels.cpp:61
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
_Quote< T > Quote(const T &aT)
Hexadecimal.
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...