HPCToolkit
xml.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // Some useful and simple routines for using XML. The xerces
54 // library is much much more complete and powerful, but for simple
55 // apps, it can be overkill.
56 //
57 // Description:
58 // [The set of functions, macros, etc. defined in the file]
59 //
60 //***************************************************************************
61 
62 #ifndef xml_xml_hpp
63 #define xml_xml_hpp
64 
65 //************************* System Include Files ****************************
66 
67 #include <iostream>
68 #include <string>
69 
70 #include <inttypes.h>
71 
72 //*************************** User Include Files ****************************
73 
74 #include <include/uint.h>
75 
76 #include <lib/support/StrUtil.hpp>
77 #include <lib/support/IOUtil.hpp>
78 
79 //*************************** Forward Declarations ***************************
80 
81 //****************************************************************************
82 
83 namespace xml {
84 
85  extern const std::string SPC; // space
86  extern const std::string eleB; // element begin, initial
87  extern const std::string eleBf; // element begin, final
88  extern const std::string eleE; // element end, normal
89  extern const std::string eleEc; // element end, compact: <.../>
90  extern const std::string attB; // attribute value begin
91  extern const std::string attE; // attribute value end
92 
93  enum XMLElementI {
94  TOKEN = 0,
95  ATT1 = 1,
96  ATT2 = 2,
97  ATT3 = 3,
98  ATT4 = 4,
99  ATT5 = 5,
100  ATT6 = 6,
101  ATT7 = 7,
102  ATT8 = 8,
103  ATT9 = 9
104  };
105 
106  class RWError { };
107 
108  enum {
109  ESC_FALSE = (0 << 0), /* Do not escape reserved XML chars */
110  ESC_TRUE = (1 << 0), /* Escape reserved XML chars */
111  UNESC_FALSE = (0 << 0),
112  UNESC_TRUE = (1 << 0)
113  };
114 
115  // Returns the string with all necessary characters (un)escaped; will
116  // not modify 'str'
117  std::string EscapeStr(const char* str);
118 
119  inline std::string
120  EscapeStr(const std::string& str)
121  {
122  return EscapeStr(str.c_str());
123  }
124 
125  std::string UnEscapeStr(const char* str);
126 
127  inline std::string
128  UnEscapeStr(const std::string& str)
129  {
130  return UnEscapeStr(str.c_str());
131  }
132 
133  // -------------------------------------------------------
134  // Reads from 'attB' to and including 'attE'. Eats up whitespace
135  // before and after the attibute.
136  // -------------------------------------------------------
137  bool ReadAttrStr(std::istream& is, std::string& s, int flags = UNESC_TRUE);
138 
139  // declaration to remove Intel compiler warning
140  template <class T> bool
141  ReadAttrNum(std::istream& is, T& n);
142 
143  // Read a number into a C/C++ numerical type
144  template <class T> bool
145  ReadAttrNum(std::istream& is, T& n)
146  {
147  bool STATE = true; // false indicates an error
148  is >> std::ws;
149  STATE &= IOUtil::Skip(is, "="); is >> std::ws;
150  STATE &= IOUtil::Skip(is, "\""); is >> std::ws;
151  is >> n;
152  STATE &= IOUtil::Skip(is, "\""); is >> std::ws;
153  return STATE;
154  }
155 
156  // -------------------------------------------------------
157  // Writes attribute value, beginning with 'attB' and ending with 'attE'
158  // -------------------------------------------------------
159 
160  // FIXME: replace the WriteAttr* with this; replace instances of
161  // MakeAttr that go to ostreams with Write.
162 #if 0
163  struct WriteMetricInfo_ {
164  const SampledMetricDesc* mdesc;
166  };
167 
168  static inline WriteMetricInfo_
169  writeMetric(const SampledMetricDesc* mdesc, hpcrun_metricVal_t x)
170  {
171  WriteMetricInfo_ info;
172  info.mdesc = mdesc;
173  info.x = x;
174  return info;
175  }
176 
177  inline std::ostream&
178  operator<<(std::ostream& os, const ADynNode::WriteMetricInfo_& info)
179  {
180  if (hpcrun_metricFlags_isFlag(info.mdesc->flags(), HPCRUN_MetricFlag_Real)) {
181  os << xml::MakeAttrNum(info.x.r);
182  }
183  else {
184  os << xml::MakeAttrNum(info.x.i);
185  }
186  return os;
187  }
188 #endif
189 
190  bool
191  WriteAttrStr(std::ostream& os, const char* s, int flags = ESC_TRUE);
192 
193  inline bool
194  WriteAttrStr(std::ostream& os, const std::string& s, int flags = ESC_TRUE)
195  {
196  return WriteAttrStr(os, s.c_str(), flags);
197  }
198 
199  // declaration to remove Intel compiler warning
200  template <class T> bool
201  WriteAttrNum(std::ostream& os, T n);
202 
203  // Write a C/C++ numerical type
204  template <class T> bool
205  WriteAttrNum(std::ostream& os, T n)
206  {
207  os << attB << n << attE;
208  return (!os.fail());
209  }
210 
211  // -------------------------------------------------------
212  // Creates an attribute string, beginning with 'attB' and ending with 'attE'
213  // -------------------------------------------------------
214 
215  inline std::string
216  MakeAttrStr(const char* x, int flags = ESC_TRUE) {
217  std::string str = ((flags & ESC_TRUE) ? EscapeStr(x) : x);
218  return (attB + str + attE);
219  }
220 
221  inline std::string
222  MakeAttrStr(const std::string& x, int flags = ESC_TRUE) {
223  return MakeAttrStr(x.c_str(), flags);
224  }
225 
226 
227  inline std::string
228  MakeAttrNum(int x) {
229  return (attB + StrUtil::toStr(x) + attE);
230  }
231 
232  inline std::string
233  MakeAttrNum(unsigned int x, int base = 10) {
234  return (attB + StrUtil::toStr(x, base) + attE);
235  }
236 
237  inline std::string
238  MakeAttrNum(int64_t x) {
239  return (attB + StrUtil::toStr(x) + attE);
240  }
241 
242  inline std::string
243  MakeAttrNum(uint64_t x, int base = 10) {
244  return (attB + StrUtil::toStr(x, base) + attE);
245  }
246 
247  inline std::string
248  MakeAttrNum(double x, const char* format = "%g" /*"%.15f"*/) {
249  return (attB + StrUtil::toStr(x, format) + attE);
250  }
251 
252 }
253 
254 #endif /* xml_xml_hpp */
const std::string SPC
Definition: xml.cpp:79
string toStr(const int x, int base)
Definition: StrUtil.cpp:243
bool WriteAttrStr(std::ostream &os, const char *s, int flags=ESC_TRUE)
Definition: xml.cpp:112
const std::string eleB
Definition: xml.cpp:80
XMLElementI
Definition: xml.hpp:93
std::ostream & operator<<(std::ostream &os, const hpcrun_metricVal_t x)
Definition: CCT-Tree.hpp:109
std::string MakeAttrStr(const char *x, int flags=ESC_TRUE)
Definition: xml.hpp:216
Definition: xml.cpp:127
void(* T)(int code, va_list_box *box, int put(int c, void *cl), void *cl, unsigned char flags[256], int width, int precision)
Definition: fmt.h:62
const std::string eleEc
Definition: xml.cpp:83
bool WriteAttrNum(std::ostream &os, T n)
Definition: xml.hpp:205
bool Skip(std::istream &is, const char *s)
Definition: IOUtil.cpp:210
bool ReadAttrNum(std::istream &is, T &n)
Definition: xml.hpp:145
const std::string eleBf
Definition: xml.cpp:81
const std::string attE
Definition: xml.cpp:85
std::string EscapeStr(const char *str)
Definition: xml.cpp:138
std::string MakeAttrNum(int x)
Definition: xml.hpp:228
const std::string eleE
Definition: xml.cpp:82
<!-- ********************************************************************--> n<!-- HPCToolkit Experiment DTD --> n<!-- Version 2.1 --> n<!-- ********************************************************************--> n<!ELEMENT HPCToolkitExperiment(Header,(SecCallPathProfile|SecFlatProfile) *)> n<!ATTLIST HPCToolkitExperiment\n version CDATA #REQUIRED > n n<!-- ******************************************************************--> n n<!-- Info/NV:flexible name-value pairs:(n) ame;(t) ype;(v) alue --> n<!ELEMENT Info(NV *)> n<!ATTLIST Info\n n CDATA #IMPLIED > n<!ELEMENT NV EMPTY > n<!ATTLIST NV\n n CDATA #REQUIRED\n t CDATA #IMPLIED\n v CDATA #REQUIRED > n n<!-- ******************************************************************--> n<!-- Header --> n<!-- ******************************************************************--> n<!ELEMENT Header(Info *)> n<!ATTLIST Header\n n CDATA #REQUIRED > n n<!-- ******************************************************************--> n<!-- Section Header --> n<!-- ******************************************************************--> n<!ELEMENT SecHeader(MetricTable?, MetricDBTable?, TraceDBTable?, LoadModuleTable?, FileTable?, ProcedureTable?, Info *)> n n<!-- MetricTable:--> n<!ELEMENT MetricTable(Metric) * > n n<!-- Metric:(i) d;(n) ame --> n<!--(v) alue-type:transient type of values --> n<!--(t) ype:persistent type of metric --> n<!-- fmt:format;show;--> n<!ELEMENT Metric(MetricFormula *, Info?)> n<!ATTLIST Metric\n i CDATA #REQUIRED\n n CDATA #REQUIRED\n es CDATA #IMPLIED\n em CDATA #IMPLIED\n ep CDATA #IMPLIED\n v(raw|final|derived-incr|derived) \"raw\\ t (inclusive|exclusive|nil) \nil\\ partner CDATA #IMPLIED\ fmt CDATA #IMPLIED\ show (1|0) \1\\ show-percent (1|0) \1> n n<!-- MetricFormula represents derived metrics: (t)ype; (frm): formula --> n<!ELEMENT MetricFormula (Info?)> n<!ATTLIST MetricFormula\ t (combine|finalize) \finalize\\ i CDATA #IMPLIED\ frm CDATA #REQUIRED> n n<!-- Metric data, used in sections: (n)ame [from Metric]; (v)alue --> n<!ELEMENT M EMPTY> n<!ATTLIST M\ n CDATA #REQUIRED\ v CDATA #REQUIRED> n n<!-- MetricDBTable: --> n<!ELEMENT MetricDBTable (MetricDB)*> n n<!-- MetricDB: (i)d; (n)ame --> n<!-- (t)ype: persistent type of metric --> n<!-- db-glob: file glob describing files in metric db --> n<!-- db-id: id within metric db --> n<!-- db-num-metrics: number of metrics in db --> n<!-- db-header-sz: size (in bytes) of a db file header --> n<!ELEMENT MetricDB EMPTY> n<!ATTLIST MetricDB\ i CDATA #REQUIRED\ n CDATA #REQUIRED\ t (inclusive|exclusive|nil) \nil\\ partner CDATA #IMPLIED\ db-glob CDATA #IMPLIED\ db-id CDATA #IMPLIED\ db-num-metrics CDATA #IMPLIED\ db-header-sz CDATA #IMPLIED> n n<!-- TraceDBTable: --> n<!ELEMENT TraceDBTable (TraceDB)> n n<!-- TraceDB: (i)d --> n<!-- db-min-time: min beginning time stamp (global) --> n<!-- db-max-time: max ending time stamp (global) --> n<!ELEMENT TraceDB EMPTY> n<!ATTLIST TraceDB\ i CDATA #REQUIRED\ db-glob CDATA #IMPLIED\ db-min-time CDATA #IMPLIED\ db-max-time CDATA #IMPLIED\ db-header-sz CDATA #IMPLIED> n n<!-- LoadModuleTable assigns a short name to a load module --> n<!ELEMENT LoadModuleTable (LoadModule)*> n n<!ELEMENT LoadModule (Info?)> n<!ATTLIST LoadModule\ i CDATA #REQUIRED\ n CDATA #REQUIRED> n n<!-- FileTable assigns a short name to a file --> n<!ELEMENT FileTable (File)*> n n<!ELEMENT File (Info?)> n<!ATTLIST File\ i CDATA #REQUIRED\ n CDATA #REQUIRED> n n<!-- ProcedureTable assigns a short name to a procedure --> n<!ELEMENT ProcedureTable (Procedure)*> n n<!-- Info/NV: flexible name-value pairs: (n)ame; (t)ype; (v)alue --> n<!-- f: family of the procedure (fake, root, ...)--> n<!ELEMENT Procedure (Info?)> n<!ATTLIST Procedure\ i CDATA #REQUIRED\ n CDATA #REQUIRED\ f CDATA #IMPLIED> n n<!-- ****************************************************************** --> n<!-- Section: Call path profile --> n<!-- ****************************************************************** --> n<!ELEMENT SecCallPathProfile (SecHeader, SecCallPathProfileData)> n<!ATTLIST SecCallPathProfile\ i CDATA #REQUIRED\ n CDATA #REQUIRED> n n<!ELEMENT SecCallPathProfileData (PF|M)*> n<!-- Procedure frame --> n<!-- (i)d: unique identifier for cross referencing --> n<!-- (s)tatic scope id --> n<!-- (n)ame: a string or an id in ProcedureTable --> n<!-- (lm) load module: a string or an id in LoadModuleTable --> n<!-- (f)ile name: a string or an id in LoadModuleTable --> n<!-- (l)ine range: \beg-end\ (inclusive range) --> n<!-- (a)lien: whether frame is alien to enclosing P --> n<!-- (str)uct: hpcstruct node id --> n<!-- (t)ype: hpcrun node type: memory access, variable declaration, ... --> n<!-- (v)ma-range-set: \{[beg-end), [beg-end)...}\ --> n<!ELEMENT PF (PF|Pr|L|C|S|M)*> n<!ATTLIST PF\ i CDATA #IMPLIED\ s CDATA #IMPLIED\ n CDATA #REQUIRED\ lm CDATA #IMPLIED\ f CDATA #IMPLIED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Procedure (static): GOAL: replace with 'P' --> n<!ELEMENT Pr (Pr|L|C|S|M)*> n<!ATTLIST Pr\ i CDATA #IMPLIED\ s CDATA #IMPLIED\ n CDATA #REQUIRED\ lm CDATA #IMPLIED\ f CDATA #IMPLIED\ l CDATA #IMPLIED\ a (1|0) \0\\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Callsite (a special StatementRange) --> n<!ELEMENT C (PF|M)*> n<!ATTLIST C\ i CDATA #IMPLIED\ s CDATA #IMPLIED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n n<!-- ****************************************************************** --> n<!-- Section: Flat profile --> n<!-- ****************************************************************** --> n<!ELEMENT SecFlatProfile (SecHeader, SecFlatProfileData)> n<!ATTLIST SecFlatProfile\ i CDATA #REQUIRED\ n CDATA #REQUIRED> n n<!ELEMENT SecFlatProfileData (LM|M)*> n<!-- Load module: (i)d; (n)ame; (v)ma-range-set --> n<!ELEMENT LM (F|P|M)*> n<!ATTLIST LM\ i CDATA #IMPLIED\ n CDATA #REQUIRED\ v CDATA #IMPLIED> n<!-- File --> n<!ELEMENT F (P|L|S|M)*> n<!ATTLIST F\ i CDATA #IMPLIED\ n CDATA #REQUIRED> n<!-- Procedure (Note 1) --> n<!ELEMENT P (P|A|L|S|C|M)*> n<!ATTLIST P\ i CDATA #IMPLIED\ n CDATA #REQUIRED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Alien (Note 1) --> n<!ELEMENT A (A|L|S|C|M)*> n<!ATTLIST A\ i CDATA #IMPLIED\ f CDATA #IMPLIED\ n CDATA #IMPLIED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Loop (Note 1,2) --> n<!ELEMENT L (A|Pr|L|S|C|M)*> n<!ATTLIST L\ i CDATA #IMPLIED\ s CDATA #IMPLIED\ l CDATA #IMPLIED\ f CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Statement (Note 2) --> n<!-- (it): trace record identifier --> n<!ELEMENT S (S|M)*> n<!ATTLIST S\ i CDATA #IMPLIED\ it CDATA #IMPLIED\ s CDATA #IMPLIED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Note 1: Contained Cs may not contain PFs --> n<!-- Note 2: The 's' attribute is not used for flat profiles --> n
bool ReadAttrStr(std::istream &is, std::string &s, int flags=UNESC_TRUE)
const std::string attB
Definition: xml.cpp:84
std::string UnEscapeStr(const char *str)
Definition: xml.cpp:145