Bitstream Interpretation Library (BIL)  0.1
bit2xml.cpp
Go to the documentation of this file.
1 
9 #include <cstdlib>
10 #include <fstream>
11 #include <iostream>
15 #include <bitfile/BitFileData.hpp>
22 #include <util/XMLWriter.hpp>
23 
24 
25 int main(int argc, char** argv)
26 {
27  using namespace bil;
28 
29  try
30  {
31  // process command line
32  std::cout << SPLASH_MSG;
33  parseCommandLine(argc, argv);
34 
35  // load bitfile
36  std::cout << LOADING_BITFILE_MSG;
37  BitFileData bfd;
38  std::ifstream bitfileStream(bitFileName.c_str(), std::ios::binary);
39  readBitfile(bfd, bitfileStream);
40 
41  // construct bitstream from bitfile raw data
42  std::cout << DECODING_BITSTREAM_MSG;
43  Bitstream bs;
45  bfd.bitstreamWordCount(0);
46 
47  // get bitfile name without path
48  size_t pos = bitFileName.find_last_of("/\\") + 1;
49  std::string bitFileNameWithoutPath(bitFileName.substr(pos));
50 
51  // write XML file containing bitfile meta data and bitstream contents
52  std::cout << WRITING_XML_MSG;
53  std::ofstream ofXml(xmlFileName.c_str());
54  XMLWriter xmlWriter(ofXml);
55  xmlWriter.addProcessingInstruction(XML_HEADER, XML_HEADER_DATA, false);
56  if (addStylesheet) xmlWriter.addProcessingInstruction(XML_STYLESHEET_HEADER, XML_STYLESHEET_HEADER_DATA);
57  xmlWriter.beginElement(XML_ELEMENT_BITFILE);
58  xmlWriter.addAttribute(XML_ELEMENT_FILENAME, bitFileNameWithoutPath);
59  xmlWriter.beginElement(XML_ELEMENT_META);
60  writeBitFileMetaXML(bfd, xmlWriter);
61  xmlWriter.endElement();
62  xmlWriter.beginElement(XML_ELEMENT_PACKETS);
63  writeV5BitstreamXML(xmlWriter, bs);
64  xmlWriter.endElement();
65  xmlWriter.endElement();
66 
67  // create XSL and CSS style sheets for XML display
68  if (addStylesheet)
69  {
70  std::cout << WRITING_STYLESHEETS_MSG;
71 
72  pos = xmlFileName.find_last_of("/\\") + 1;
73  std::string xmlFilePath(xmlFileName.substr(0, pos));
74 
75  std::string xslFileName(xmlFilePath);
76  xslFileName.append(XSL_FILENAME);
77  std::ofstream ofXSL(xslFileName.c_str(), std::ios::binary);
78  ofXSL.write(reinterpret_cast<const char*>(DISASSEMBLY_XSL), DISASSEMBLY_XSL_SIZE);
79  if (!ofXSL) throw IOException();
80 
81  std::string cssFileName(xmlFilePath);
82  cssFileName.append(CSS_FILENAME);
83  std::ofstream ofCSS(cssFileName.c_str(), std::ios::binary);
84  ofCSS.write(reinterpret_cast<const char*>(DISASSEMBLY_CSS), DISASSEMBLY_CSS_SIZE);
85  if (!ofCSS) throw IOException();
86  }
87 
88  // finished
89  std::cout << FINISHED_MSG;
90  }
91  catch (const CommandLineException& e)
92  {
93  std::cout << ERROR_MSG << e.what() << INFO_MSG;
94  return EXIT_FAILURE;
95  }
96  catch (...)
97  {
98  std::cout << ERROR_UNKNOWN_MSG;
99  return EXIT_FAILURE;
100  }
101  return EXIT_SUCCESS;
102 }