File indexing completed on 2021-02-14 13:31:11
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include "L1TriggerConfig/L1GtConfigProducers/interface/L1GtVhdlTemplateFile.h"
0017
0018
0019 #include <iostream>
0020 #include <fstream>
0021 #include <sstream>
0022 #include <map>
0023 #include <string>
0024 #include <vector>
0025
0026
0027
0028
0029 L1GtVhdlTemplateFile::L1GtVhdlTemplateFile() { intern_ = false; }
0030
0031
0032 L1GtVhdlTemplateFile::L1GtVhdlTemplateFile(const std::string &filename) {
0033 if (!open(filename, false))
0034 std::cout << "Error while opening file: " << filename << std::endl;
0035 }
0036
0037
0038 L1GtVhdlTemplateFile::L1GtVhdlTemplateFile(const L1GtVhdlTemplateFile &rhs) {
0039 lines_ = rhs.lines_;
0040 intern_ = rhs.intern_;
0041 parameterMap_ = rhs.parameterMap_;
0042 }
0043
0044
0045 L1GtVhdlTemplateFile::~L1GtVhdlTemplateFile() {
0046
0047 }
0048
0049 const bool L1GtVhdlTemplateFile::findAndReplaceString(std::string ¶mString,
0050 const std::string &searchString,
0051 const std::string &replaceString) {
0052 size_t position;
0053 position = paramString.find(searchString);
0054 if (position == std::string::npos)
0055 return false;
0056 paramString.replace(position, searchString.length(), replaceString);
0057 return true;
0058 }
0059
0060 bool L1GtVhdlTemplateFile::open(const std::string &fileName, bool internal) {
0061 const char paramIndicator = '#';
0062 const char commentIndicator = '%';
0063 char buffer[2000];
0064 std::string stringBuffer;
0065
0066 std::fstream inputFile(fileName.c_str(), std::ios::in);
0067
0068 if (!inputFile.is_open())
0069 return false;
0070
0071
0072 while (!inputFile.eof()) {
0073 inputFile.getline(buffer, 2000);
0074 stringBuffer = buffer;
0075
0076 if (stringBuffer[stringBuffer.length() - 1] == 13) {
0077 stringBuffer.replace(stringBuffer.length() - 1, 1, "");
0078 }
0079
0080 lines_.push_back(stringBuffer );
0081 }
0082
0083 inputFile.close();
0084
0085 if (internal) {
0086
0087 std::vector<std::string>::iterator iter = lines_.begin();
0088 while (iter != lines_.end()) {
0089 while ((*iter)[0] == commentIndicator && (*iter)[1] == commentIndicator)
0090 lines_.erase(iter);
0091
0092 if ((*iter)[0] == paramIndicator) {
0093 std::vector<std::string>::iterator iter2 = iter;
0094
0095
0096 iter2++;
0097
0098 while (iter2 != lines_.end()) {
0099 if ((*iter2)[0] == paramIndicator && (*iter2)[1] == paramIndicator) {
0100 iter2++;
0101 break;
0102 }
0103
0104 parameterMap_[(*iter).substr(1)] += (*iter2);
0105
0106
0107 std::vector<std::string>::iterator tmpIter = iter2;
0108 tmpIter++;
0109
0110
0111 if (!((*tmpIter)[0] == paramIndicator && (*tmpIter)[1] == paramIndicator))
0112 parameterMap_[(*iter).substr(1)] += "\n";
0113
0114 iter2++;
0115 }
0116
0117
0118
0119 if (iter2 == lines_.end())
0120 return false;
0121
0122
0123
0124 lines_.erase(iter, iter2);
0125 }
0126
0127
0128 if (iter != lines_.end())
0129 iter++;
0130 }
0131
0132
0133 iter = lines_.begin();
0134 while (iter != lines_.end()) {
0135 if ((*iter).empty() || (*iter).length() == 0 || (*iter) == " ")
0136 lines_.erase(iter);
0137 else
0138 iter++;
0139 }
0140 }
0141
0142 return true;
0143 }
0144
0145 bool L1GtVhdlTemplateFile::save(const std::string &fileName) {
0146 std::ofstream outputFile(fileName.c_str());
0147 std::vector<std::string>::iterator iter = lines_.begin();
0148
0149
0150 while (iter != lines_.end()) {
0151
0152 outputFile << *iter << std::endl;
0153 iter++;
0154 }
0155
0156 outputFile.close();
0157
0158 return true;
0159 }
0160
0161 bool L1GtVhdlTemplateFile::substitute(const std::string &searchString, const std::string &replaceString) {
0162 bool success = false;
0163
0164 std::vector<std::string>::iterator iter = lines_.begin();
0165 while (iter != lines_.end()) {
0166
0167 while (findAndReplaceString(*iter, ("$(" + searchString + ")"), replaceString)) {
0168 findAndReplaceString(*iter, ("$(" + searchString + ")"), replaceString);
0169 success = true;
0170 }
0171 iter++;
0172 }
0173
0174 return success;
0175 }
0176
0177 bool L1GtVhdlTemplateFile::insert(const std::string &atLine, const std::vector<std::string> &content) {
0178 bool success = false;
0179 std::vector<std::string>::iterator iter = lines_.begin();
0180
0181
0182 while (iter != lines_.end()) {
0183
0184 if ((*iter).find(atLine) != std::string::npos) {
0185
0186 iter = lines_.erase(iter);
0187
0188 lines_.insert(iter, content.begin(), content.end());
0189
0190 success = true;
0191 break;
0192 }
0193
0194 iter++;
0195 }
0196
0197 return success;
0198 }
0199
0200 bool L1GtVhdlTemplateFile::insert(const std::string atLine, const L1GtVhdlTemplateFile &_file) {
0201 std::vector<std::string> temp = _file.returnLines();
0202
0203 if (insert(atLine, temp))
0204 return true;
0205
0206 return false;
0207 }
0208
0209 bool L1GtVhdlTemplateFile::close() {
0210
0211 return true;
0212 }
0213
0214 void L1GtVhdlTemplateFile::print() const {
0215 std::vector<std::string>::const_iterator iter = lines_.begin();
0216 while (iter != lines_.end()) {
0217 std::cout << *iter << std::endl;
0218 iter++;
0219 }
0220 }
0221
0222 std::vector<std::string> L1GtVhdlTemplateFile::returnLines() const { return lines_; }
0223
0224 void L1GtVhdlTemplateFile::printParameterMap() const {
0225 std::cout << "Enter parametermap" << std::endl;
0226
0227 std::map<std::string, std::string>::const_iterator iter = parameterMap_.begin();
0228
0229 while (iter != parameterMap_.end()) {
0230 std::cout << (*iter).first << ": " << (*iter).second << std::endl;
0231 iter++;
0232 ;
0233 }
0234 }
0235
0236 std::map<std::string, std::string> L1GtVhdlTemplateFile::returnParameterMap() const { return parameterMap_; }
0237
0238 bool L1GtVhdlTemplateFile::extractParametersFromString(const std::string &str,
0239 std::vector<std::string> ¶meters) const {
0240
0241
0242
0243 if (int pos1 = str.find("$(") != std::string::npos && str.substr(0, 2) != "--") {
0244 int pos2 = str.find(')');
0245
0246 std::string tempStr = (str.substr(pos1 + 1, (pos2 - pos1 - 1)));
0247
0248
0249
0250
0251
0252
0253 parameters.push_back(tempStr);
0254
0255 while (extractParametersFromString(str.substr(pos2), parameters))
0256 extractParametersFromString(str.substr(pos2), parameters);
0257
0258 return true;
0259 } else {
0260 return false;
0261 }
0262
0263 return true;
0264 }
0265
0266 std::vector<std::string> L1GtVhdlTemplateFile::getSubstitutionParametersFromTemplate() const {
0267 std::vector<std::string> temp;
0268 std::vector<std::string>::const_iterator iter = lines_.begin();
0269
0270
0271 while (iter != lines_.end()) {
0272 extractParametersFromString((*iter), temp);
0273 iter++;
0274 }
0275
0276 return temp;
0277 }
0278
0279 void L1GtVhdlTemplateFile::append(const std::string &str) { lines_.push_back(str); }
0280
0281 void L1GtVhdlTemplateFile::append(const L1GtVhdlTemplateFile &file) {
0282 for (unsigned int i = 0; i < file.lines_.size(); i++) {
0283 lines_.push_back(file.lines_.at(i));
0284 }
0285 }
0286
0287 bool L1GtVhdlTemplateFile::removeLineWithContent(const std::string &str) {
0288 bool success = false;
0289
0290 std::vector<std::string>::iterator iter = lines_.begin();
0291 while (iter != lines_.end()) {
0292 size_t position;
0293 position = (*iter).find(str);
0294
0295 if (position != std::string::npos) {
0296 lines_.erase(iter);
0297 success = true;
0298 } else
0299 iter++;
0300 }
0301 return success;
0302 }
0303
0304 bool L1GtVhdlTemplateFile::removeEmptyLines() {
0305 std::vector<std::string>::iterator iter = lines_.begin();
0306
0307 while (iter != lines_.end()) {
0308 if ((*iter).empty() || (*iter).length() == 0 || (*iter) == " ")
0309 lines_.erase(iter);
0310 else
0311 iter++;
0312 }
0313
0314 return true;
0315 }
0316
0317 bool L1GtVhdlTemplateFile::isBlank(const char &chr) const {
0318 if (chr == ' ')
0319 return true;
0320 return false;
0321 }
0322
0323 bool L1GtVhdlTemplateFile::split(const std::string ¶m, std::vector<std::string> &result) const {
0324 unsigned int i = 0;
0325 while (isBlank(param[i])) {
0326 i++;
0327 }
0328
0329 std::string temp = param.substr(i);
0330 std::size_t pos = temp.find(' ');
0331
0332 if (pos != std::string::npos) {
0333 std::string temp2 = temp.substr(0, pos);
0334 result.push_back(temp2);
0335 while (split(temp.substr(pos), result))
0336 split(temp.substr(pos), result);
0337
0338 } else if (!isBlank(temp[pos + 1])) {
0339 result.push_back(temp);
0340 return false;
0341 } else
0342 return false;
0343
0344 return false;
0345 }
0346
0347 void L1GtVhdlTemplateFile::getConditionsFromAlgo(std::string condString, std::vector<std::string> &result) const {
0348 std::vector<std::string> operators;
0349
0350 operators.push_back("AND");
0351 operators.push_back("OR");
0352 operators.push_back("NOT");
0353 operators.push_back("(");
0354 operators.push_back(")");
0355
0356 for (unsigned int i = 0; i < operators.size(); i++) {
0357 while (findAndReplaceString(condString, operators.at(i), ""))
0358 findAndReplaceString(condString, operators.at(i), "");
0359 }
0360
0361 split(condString, result);
0362 }
0363
0364 std::string L1GtVhdlTemplateFile::lines2String() const {
0365 std::vector<std::string>::const_iterator iter = lines_.begin();
0366 std::ostringstream buffer;
0367
0368 while (iter != lines_.end()) {
0369 buffer << (*iter) << std::endl;
0370 iter++;
0371 }
0372
0373 return buffer.str();
0374 }
0375
0376 std::string L1GtVhdlTemplateFile::getInternalParameter(const std::string &indentifier) {
0377 return parameterMap_[indentifier];
0378 }