00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "torc/bitstream/build/DeviceInfoHelper.hpp"
00020 #include "torc/bitstream/DeviceInfo.hpp"
00021 #include "torc/common/DirectoryTree.hpp"
00022 #include <boost/filesystem.hpp>
00023 #include <boost/regex.hpp>
00024 #include <iostream>
00025 #include <sstream>
00026 #include <fstream>
00027
00028 namespace torc {
00029 namespace bitstream {
00030
00031 void DeviceInfoHelper::buildFamilyDeviceInfo(const std::string& inFamilyName,
00032 const std::string& inTemplateName, const std::string& inOutputName,
00033 const torc::common::DeviceVector& inDeviceNames, Bitstream& inBitstream) {
00034
00035
00036 boost::filesystem::path outputPath
00037 = torc::common::DirectoryTree::getWorkingPath() / "torc" / "bitstream" / inOutputName;
00038
00039
00040
00041
00042
00043
00044 std::ofstream outputStream(outputPath.string().c_str(), std::ios::out | std::ios::binary);
00045 if(!outputStream.is_open()) {
00046 std::cerr << "Output static device information file could not be opened: "
00047 << outputPath.string() << std::endl;
00048 return;
00049 }
00050
00051
00052 boost::filesystem::path templatePath = torc::common::DirectoryTree::getWorkingPath()
00053 / "torc" / "bitstream" / "build" / inTemplateName;
00054 std::ifstream templateStream(templatePath.string().c_str(),
00055 std::ios::in | std::ios::binary | std::ios::ate);
00056 if(!templateStream.is_open()) {
00057 std::cerr << "Input static device information template could not be opened: "
00058 << outputPath.string() << std::endl;
00059 return;
00060 }
00061 std::string templateString;
00062
00063 std::ifstream::pos_type size = templateStream.tellg();
00064 templateString.resize(size);
00065
00066 templateStream.seekg(0, std::ios::beg);
00067 templateStream.read(const_cast<char*>(templateString.data()), size);
00068
00069 templateStream.close();
00070
00071
00072 std::stringstream allDeviceInfo;
00073 torc::common::DeviceVector::const_iterator dp = inDeviceNames.begin();
00074 torc::common::DeviceVector::const_iterator de = inDeviceNames.end();
00075 while(dp < de) {
00076 const std::string& device = *dp++;
00077 if(device.empty()) break;
00078 inBitstream.setDevice(device);
00079 inBitstream.initializeDeviceInfo(device);
00080 inBitstream.writeDeviceInfo(allDeviceInfo, device);
00081 if(dp < de) allDeviceInfo << std::endl;
00082 }
00083
00084
00085 typedef std::map<std::string, std::string> SubstitutionMap;
00086 SubstitutionMap substitutions;
00087 substitutions["%%ARCHITECTURE%%"] = inFamilyName;
00088 substitutions["%%DEVICES%%"] = allDeviceInfo.str();
00089 substitutions["%%GENERATED%%"] = __FILE__;
00090
00091
00092 SubstitutionMap::iterator p = substitutions.begin();
00093 SubstitutionMap::iterator e = substitutions.end();
00094 while(p != e) {
00095
00096 boost::regex re(p->first);
00097 std::stringstream stringStream;
00098 std::ostream_iterator<char, char> stringIterator(stringStream);
00099
00100 boost::regex_replace(stringIterator, templateString.begin(), templateString.end(), re,
00101 p->second, boost::match_default);
00102
00103 templateString = stringStream.str();
00104 p++;
00105 }
00106
00107
00108 outputStream << templateString;
00109 outputStream.close();
00110
00111 }
00112
00113 }
00114 }