HPCToolkit
xml.cpp
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 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 //************************* System Include Files ****************************
61 
62 #include <fstream>
63 
64 #include <string>
65 using std::string;
66 
67 #include <cstring>
68 
69 //*************************** User Include Files ****************************
70 
71 #include "xml.hpp"
72 
74 
75 //*************************** Forward Declarations ***************************
76 
77 using namespace xml;
78 
79 const string xml::SPC = " "; // space
80 const string xml::eleB = "<"; // element begin, initial
81 const string xml::eleBf = "</"; // element begin, final
82 const string xml::eleE = ">"; // element end, normal
83 const string xml::eleEc = "/>"; // element end, compact: <.../>
84 const string xml::attB = "=\""; // attribute value begin
85 const string xml::attE = "\""; // attribute value end
86 
87 //****************************************************************************
88 // Read
89 //****************************************************************************
90 
91 // Reads from 'attB' to and including 'attE'.
92 bool
93 xml::ReadAttrStr(std::istream& is, string& s, int flags)
94 {
95  bool STATE = true; // false indicates an error
96  is >> std::ws;
97  STATE &= IOUtil::Skip(is, "="); is >> std::ws;
98  STATE &= IOUtil::Skip(is, "\""); is >> std::ws;
99  s = IOUtil::Get(is, '"');
100  if (flags & UNESC_TRUE) { s = UnEscapeStr(s); }
101  STATE &= IOUtil::Skip(is, "\""); is >> std::ws;
102  return STATE;
103 }
104 
105 
106 //****************************************************************************
107 // Write
108 //****************************************************************************
109 
110 // Writes attribute value, beginning with 'attB' and ending with 'attE'.
111 bool
112 xml::WriteAttrStr(std::ostream& os, const char* s, int flags)
113 {
114  string str = ((flags & ESC_TRUE) ? EscapeStr(s) : s);
115  os << attB << str << attE;
116  return (!os.fail());
117 }
118 
119 
120 //****************************************************************************
121 //
122 //****************************************************************************
123 
124 // 'EscapeStr' and 'UnEscapeStr': Returns the string with all
125 // necessary characters (un)escaped; will not modify 'str'
126 
127 namespace xml {
128  static string
129  substitute(const char* str, const string* fromStrs, const string* toStrs);
130 
131  static const int numSubs = 4; // number of substitutes
132  static const string RegStrs[] = {"<", ">", "&", "\""};
133  static const string EscStrs[] = {"&lt;", "&gt;", "&amp;", "&quot;"};
134 }
135 
136 
137 string
138 xml::EscapeStr(const char* str)
139 {
140  return substitute(str, RegStrs, EscStrs);
141 }
142 
143 
144 string
145 xml::UnEscapeStr(const char* str)
146 {
147  return substitute(str, EscStrs, RegStrs);
148 }
149 
150 
151 static string
152 xml::substitute(const char* str, const string* fromStrs, const string* toStrs)
153 {
154  static string newStr = string("", 512);
155 
156  string retStr = str;
157  if (!str) { return retStr; }
158 
159  // Iterate over 'str' and substitute patterns
160  newStr = "";
161  int strLn = strlen(str);
162  for (int i = 0; str[i] != '\0'; /* */) {
163 
164  // Attempt to find a pattern for substitution at this position
165  int curSub = 0, curSubLn = 0;
166  for (/*curSub = 0*/; curSub < numSubs; curSub++) {
167  curSubLn = fromStrs[curSub].length();
168  if ((strLn-i >= curSubLn) &&
169  (strncmp(str+i, fromStrs[curSub].c_str(), curSubLn) == 0)) {
170  break; // only one substitution possible per position
171  }
172  }
173 
174  // Substitute or copy current position; Adjust iteration to
175  // inspect next character. (resizes if necessary)
176  if (curSub < numSubs) { // we found a string to substitute
177  newStr += toStrs[curSub]; i += curSubLn;
178  } else {
179  newStr += str[i]; i++;
180  }
181  }
182  retStr = newStr;
183 
184  return retStr;
185 }
186 
187 //****************************************************************************
188 //
189 //****************************************************************************
static const string RegStrs[]
Definition: xml.cpp:132
const std::string SPC
Definition: xml.cpp:79
static const string EscStrs[]
Definition: xml.cpp:133
bool WriteAttrStr(std::ostream &os, const char *s, int flags=ESC_TRUE)
Definition: xml.cpp:112
const std::string eleB
Definition: xml.cpp:80
Definition: xml.cpp:127
const std::string eleEc
Definition: xml.cpp:83
bool Skip(std::istream &is, const char *s)
Definition: IOUtil.cpp:210
static const int numSubs
Definition: xml.cpp:131
std::string Get(std::istream &is, char end)
Definition: IOUtil.cpp:185
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
const std::string eleE
Definition: xml.cpp:82
static string substitute(const char *str, const string *fromStrs, const string *toStrs)
Definition: xml.cpp:152
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