Skip to content

Commit abecee5

Browse files
committed
fmk - bug fix, when one RV's name contained in another new ability for RV in list, i.e. without quotes was causing issues .. this fixes that
1 parent fc6c447 commit abecee5

1 file changed

Lines changed: 63 additions & 25 deletions

File tree

modules/performUQ/templateSub/simCenterSub.cpp

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
#include <sstream>
33
#include <iostream>
44
#include <vector>
5+
#include <deque>
56
#include <iterator>
7+
#include <string>
68

7-
using std::string;
9+
using std::deque;
810
using std::vector;
11+
using std::string;
912

1013
/* ***************** params.in example ******
1114
2
@@ -55,10 +58,12 @@ int main(int argc, char **argv)
5558
// vectors that will contain strings to search for & their replacement
5659
//
5760

58-
vector<string> original;
59-
vector<string> replace;
60-
vector<int> originalLength;
61-
vector<int> replacementLength;
61+
deque<string> rvNames;
62+
deque<string> original;
63+
deque<string> replace;
64+
deque<int> originalLength;
65+
deque<int> replacementLength;
66+
6267

6368
//
6469
// from params file, 1) read # of RV and 2) then read RV names and values
@@ -68,39 +73,72 @@ int main(int argc, char **argv)
6873
int numRVs = 0;
6974
string line;
7075
while (getline(params, line)) {
76+
7177
std::istringstream buf(line);
7278
std::istream_iterator<std::string> beg(buf), end;
7379
vector<std::string> tokens(beg, end); // done!
80+
7481
if (lineCount == 0) {
7582

7683
// first line contains #RV
7784
numRVs = std::stoi(tokens.at(0));
78-
// std::cerr << "numRV: " << numRVs << "\n";
85+
//std::cerr << "numRV: " << numRVs << "\n";
7986

8087
} else {
8188

8289
// subsequent lines contain RV and value .. add to string vectors
83-
string rvName = "\"RV." + tokens.at(0) + "\""; // add SimCenter delimiters begin="RV. & end="
84-
string rvName2 = "RV." + tokens.at(0); // add SimCenter delimiters begin="RV. & end="
85-
string rvValue = tokens.at(1);
86-
original.push_back(rvName);
87-
replace.push_back(rvValue);
88-
original.push_back(rvName2);
89-
replace.push_back(rvValue);
90-
originalLength.push_back(rvName.length());
91-
replacementLength.push_back(rvValue.length());
92-
originalLength.push_back(rvName2.length());
93-
replacementLength.push_back(rvValue.length());
90+
string rvName = tokens.at(0);
9491

95-
// std::cerr << rvName << " " << rvValue << "\n";
92+
string rvName2 = "RV." + rvName; // add SimCenter delimiters begin=RV.
93+
string rvValue = tokens.at(1);
94+
// std::cerr << "rv name: " << rvName << " value " << rvValue << "\n";
95+
96+
// check if an existing rvName starts as a substring of current
97+
bool subStringExists = false;
98+
for (size_t i = 0; i < rvNames.size(); ++i) {
99+
const std::string& s1 = rvNames[i];
100+
if (rvName.find(s1) == 0) {
101+
subStringExists = true;
102+
break; // No need to check further if found
103+
}
104+
}
105+
106+
if (subStringExists == false) {
107+
rvNames.push_back(rvName);
108+
109+
rvName = "\"RV." + rvName + "\""; // add SimCenter delimiters begin="RV. &\ end="
110+
original.push_back(rvName);
111+
replace.push_back(rvValue);
112+
original.push_back(rvName2);
113+
replace.push_back(rvValue);
114+
originalLength.push_back(rvName.length());
115+
replacementLength.push_back(rvValue.length());
116+
originalLength.push_back(rvName2.length());
117+
replacementLength.push_back(rvValue.length());
118+
119+
} else {
120+
rvNames.push_front(rvName);
121+
122+
rvName = "\"RV." + rvName + "\""; // add SimCenter delimiters begin="RV. &\ end="
123+
original.push_front(rvName2);
124+
replace.push_front(rvValue);
125+
original.push_front(rvName);
126+
replace.push_front(rvValue);
127+
128+
originalLength.push_front(rvName2.length());
129+
replacementLength.push_front(rvValue.length());
130+
originalLength.push_front(rvName.length());
131+
replacementLength.push_front(rvValue.length());
132+
}
96133
}
97134

98135
lineCount++;
99-
136+
100137
if (lineCount > numRVs)
101138
break; // don't need to do anything with additional giberish in the file
102139
}
103140

141+
104142
//
105143
// read input file line by line
106144
// - search each line for RV names, if found replace with RV value, send to output
@@ -115,33 +153,33 @@ int main(int argc, char **argv)
115153
string &oldString = original.at(i);
116154
string &newString = replace.at(i);
117155

118-
// NEW CODE: line = replaceAllOccurrences(line, oldString, newString);
119-
120-
/* ******************** OLD *************************/
121156
int oldSize = originalLength.at(i);
122157
int newSize = replacementLength.at(i);
123158

124159
// search for RV in string till end of string
125160
while (true) {
161+
126162
size_t pos = line.find(oldString);
127163

128164
// if found .. replace
129-
if (pos != string::npos)
165+
if (pos != string::npos) {
130166

131167
if( oldSize == newSize ) {
132168

133169
// if they're same size, use std::string::replace
134170
line.replace( pos, oldSize, newString );
135-
171+
136172
} else {
137173

138174
// if not same size, replace by erasing and inserting (costly)
139175
line.erase(pos, oldSize );
140176
line.insert(pos, newString );
177+
141178
}
142179

180+
143181
// end of string .. break .. onto next RV
144-
else
182+
} else
145183
break;
146184
}
147185
/******************************************************* */

0 commit comments

Comments
 (0)