torc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DeviceInfoHelper.cpp
Go to the documentation of this file.
1 // Torc - Copyright 2011-2013 University of Southern California. All Rights Reserved.
2 // $HeadURL$
3 // $Id$
4 
5 // This program is free software: you can redistribute it and/or modify it under the terms of the
6 // GNU General Public License as published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
11 // the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program. If
14 // not, see <http://www.gnu.org/licenses/>.
15 
16 /// \file
17 /// \brief Source for the DeviceInfoHelper class.
18 
22 #include <boost/filesystem.hpp>
23 #include <boost/regex.hpp>
24 #include <iostream>
25 #include <sstream>
26 #include <fstream>
27 
28 namespace torc {
29 namespace bitstream {
30 
32  const std::string& inTemplateName, const std::string& inOutputName,
33  const torc::common::DeviceVector& inDeviceNames, Bitstream& inBitstream) {
34 
35  // preflight and open the output file
36  boost::filesystem::path outputPath
37  = torc::common::DirectoryTree::getWorkingPath() / "torc" / "bitstream" / inOutputName;
38 /// \todo Reinstate check for existing static device information file.
39 // if(boost::filesystem::exists(outputPath)) {
40 // std::cerr << "Existing static device information file will not be replaced: "
41 // << outputPath.string() << std::endl;
42 // return;
43 // }
44  std::ofstream outputStream(outputPath.string().c_str(), std::ios::out | std::ios::binary);
45  if(!outputStream.is_open()) {
46  std::cerr << "Output static device information file could not be opened: "
47  << outputPath.string() << std::endl;
48  return;
49  }
50 
51  // read the device info template
53  / "torc" / "bitstream" / "build" / inTemplateName;
54  std::ifstream templateStream(templatePath.string().c_str(),
55  std::ios::in | std::ios::binary | std::ios::ate);
56  if(!templateStream.is_open()) {
57  std::cerr << "Input static device information template could not be opened: "
58  << outputPath.string() << std::endl;
59  return;
60  }
61  std::string templateString;
62  // resize the string according to the file length
63  std::ifstream::pos_type size = templateStream.tellg();
64  templateString.resize(size);
65  // rewind to the beginning and read the entire file directly into the string
66  templateStream.seekg(0, std::ios::beg);
67  templateStream.read(const_cast<char*>(templateString.data()), size);
68  // close close file
69  templateStream.close();
70 
71  // iterate over all devices
72  std::stringstream allDeviceInfo;
73  torc::common::DeviceVector::const_iterator dp = inDeviceNames.begin();
74  torc::common::DeviceVector::const_iterator de = inDeviceNames.end();
75  while(dp < de) {
76  const std::string& device = *dp++;
77  if(device.empty()) break;
78  inBitstream.setDevice(device);
79  inBitstream.initializeDeviceInfo(device);
80  inBitstream.writeDeviceInfo(allDeviceInfo, device);
81  if(dp < de) allDeviceInfo << std::endl;
82  }
83 
84  // define the substitutions
85  typedef std::map<std::string, std::string> SubstitutionMap;
86  SubstitutionMap substitutions;
87  substitutions["%%ARCHITECTURE%%"] = inFamilyName;
88  substitutions["%%DEVICES%%"] = allDeviceInfo.str();
89  substitutions["%%GENERATED%%"] = __FILE__;
90 
91  // replace all substitution strings in the template
92  SubstitutionMap::iterator p = substitutions.begin();
93  SubstitutionMap::iterator e = substitutions.end();
94  while(p != e) {
95  // create the regular expression, and prepare to replace
96  boost::regex re(p->first);
97  std::stringstream stringStream;
98  std::ostream_iterator<char, char> stringIterator(stringStream);
99  // replace all occurrences of the current substitution pattern
100  boost::regex_replace(stringIterator, templateString.begin(), templateString.end(), re,
101  p->second, boost::match_default);
102  // copy the results back into place
103  templateString = stringStream.str();
104  p++;
105  }
106 
107  // write the output file
108  outputStream << templateString;
109  outputStream.close();
110 
111  }
112 
113 } // namespace bitstream
114 } // namespace torc
Header for the DeviceInfoHelper class.
Header for the DirectoryTree class.
std::string string
Xilinx bitstream base class.
virtual void initializeDeviceInfo(const std::string &inDeviceName)
Initialize the maps between frame indexes and frame addresses. This is generally only useful for int...
boost::filesystem::path path
void setDevice(const std::string &inDeviceName)
Assign the device enumeration constant for the given device name.
static const boost::filesystem::path & getWorkingPath(void)
Returns the absolute path to the working directory.
std::vector< std::string > DeviceVector
Vector of device names.
Definition: Devices.hpp:119
virtual void writeDeviceInfo(std::ostream &inStream, const std::string &inDeviceName)
Output static device information to a stream.
Definition: Bitstream.cpp:74
static void buildFamilyDeviceInfo(const std::string &inFamilyName, const std::string &inTemplateName, const std::string &inOutputName, const torc::common::DeviceVector &inDeviceNames, Bitstream &inBitstream)
Build the static device information for the specified family.
Header for the DeviceInfo class.