HPCToolkit
PGMReader.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 // XML adaptor for the program structure file (PGM)
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 //************************ System Include Files ******************************
61 
62 #include <sys/types.h>
63 #include <sys/stat.h>
64 #include <errno.h>
65 #include <fcntl.h>
66 #include <string.h>
67 #include <unistd.h>
68 
69 #include <iostream>
70 using std::cerr;
71 using std::endl;
72 
73 #include <string>
74 using std::string;
75 
76 //************************* User Include Files *******************************
77 
78 #include "PGMReader.hpp"
79 #include "XercesUtil.hpp"
80 
81 //*********************** Xerces Include Files *******************************
82 
83 #include <xercesc/util/XMLString.hpp>
84 using XERCES_CPP_NAMESPACE::XMLString;
85 
86 //************************ Forward Declarations ******************************
87 
88 //****************************************************************************
89 
90 namespace Prof {
91 
92 namespace Struct {
93 
94 // Simple sanity check for struct file that we can open it and it
95 // begins with '<?xml'. Otherwise, the xerces error messages are
96 // rather unhelpful.
97 //
98 // FIXME: better to check for DOCTYPE tag:
99 // <!DOCTYPE HPCToolkitStructure
100 //
101 #define BUF_SIZE 10
102 static void
103 xmlSanityCheck(const char *filenm, string & docType)
104 {
105  char buf[BUF_SIZE];
106 
107  int fd = open(filenm, O_RDONLY);
108  if (fd < 0) {
109  cerr << "unable to open " << docType << " file: '" << filenm << "': "
110  << strerror(errno) << endl;
111  exit(1);
112  }
113 
114  memset(buf, 0, BUF_SIZE);
115  ssize_t ret = read(fd, buf, 5);
116  if (ret < 0) {
117  cerr << "unable to read " << docType << " file: '" << filenm << "': "
118  << strerror(errno) << endl;
119  exit(1);
120  }
121 
122  if (strncasecmp(buf, "<?xml", 5) != 0) {
123  cerr << "unable to parse " << docType << " file: '" << filenm << "': "
124  << "not an xml file" << endl;
125  exit(1);
126  }
127 
128  close(fd);
129 }
130 
131 
132 void
134  const std::vector<string>& structureFiles,
135  PGMDocHandler::Doc_t docty,
136  DocHandlerArgs& docargs)
137 {
138  if (structureFiles.empty()) { return; }
139 
140  InitXerces();
141 
142  for (uint i = 0; i < structureFiles.size(); ++i) {
143  const string& fnm = structureFiles[i];
144  read_PGM(structure, fnm.c_str(), docty, docargs);
145  }
146 
147  FiniXerces();
148 }
149 
150 
151 void
153  const char* filenm,
154  PGMDocHandler::Doc_t docty,
155  DocHandlerArgs& docHandlerArgs)
156 {
157  if (!filenm || filenm[0] == '\0') {
158  return;
159  }
160 
161  string fpath = filenm;
162  string docType = PGMDocHandler::ToString(docty);
163 
164  xmlSanityCheck(filenm, docType);
165 
166  if (!fpath.empty()) {
167  try {
168  SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
169 
170  parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
171  parser->setFeature(XMLUni::fgXercesDynamic, true);
172  parser->setFeature(XMLUni::fgXercesValidationErrorAsFatal, true);
173 
174  PGMDocHandler* handler = new PGMDocHandler(docty, &structure,
175  docHandlerArgs);
176  parser->setContentHandler(handler);
177  parser->setErrorHandler(handler);
178 
179  parser->parse(fpath.c_str());
180 
181  if (parser->getErrorCount() > 0) {
182  DIAG_Throw("ignoring " << fpath << " because of previously reported parse errors.");
183  }
184  delete handler;
185  delete parser;
186  }
187  catch (const SAXException& x) {
188  DIAG_Throw("parsing '" << fpath << "'" <<
189  XMLString::transcode(x.getMessage()));
190  }
191  catch (const PGMException& x) {
192  DIAG_Throw("reading '" << fpath << "'" << x.message());
193  }
194  catch (...) {
195  DIAG_EMsg("While processing '" << fpath << "'...");
196  throw;
197  };
198  }
199  else {
200  DIAG_Throw("Could not open " << PGMDocHandler::ToString(docty)
201  << " file '" << filenm << "'.");
202  }
203 }
204 
205 
206 } // namespace Util
207 
208 } // namespace Analysis
#define DIAG_EMsg(...)
Definition: diagnostics.h:251
static const char * ToString(Doc_t docty)
Definition: fmt.c:108
#define BUF_SIZE
Definition: PGMReader.cpp:101
virtual std::string message() const
void InitXerces()
Definition: XercesUtil.cpp:78
exit
Definition: names.cpp:1
unsigned int uint
Definition: uint.h:124
void read_PGM(Struct::Tree &structure, const char *filenm, PGMDocHandler::Doc_t docty, DocHandlerArgs &docHandlerArgs)
Definition: PGMReader.cpp:152
static void xmlSanityCheck(const char *filenm, string &docType)
Definition: PGMReader.cpp:103
ssize_t MONITOR_EXT_WRAP_NAME() read(int fd, void *buf, size_t count)
Definition: io-over.c:152
void FiniXerces()
Definition: XercesUtil.cpp:92
void readStructure(Struct::Tree &structure, const std::vector< string > &structureFiles, PGMDocHandler::Doc_t docty, DocHandlerArgs &docargs)
Definition: PGMReader.cpp:133