GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 0.0% 0 / 0 / 215
Functions: 0.0% 0 / 0 / 10
Branches: 0.0% 0 / 0 / 483

src/generateiddfactory/IddFileFactoryData.cpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #include "IddFileFactoryData.hpp"
7 #include "WriteEnums.hpp"
8
9 #include "../utilities/idd/IddRegex.hpp"
10
11 #include <boost/regex.hpp>
12 #include <boost/algorithm/string.hpp>
13
14 #include <iostream>
15 #include <sstream>
16 #include <exception>
17
18 namespace openstudio {
19
20 ✗ IddFileFactoryData::IddFileFactoryData(const std::string& fileNameAndPathPair) {
21 ✗ std::cout << "Creating new IddFileFactoryData object from input argument '" << fileNameAndPathPair << "'." << '\n';
22 ✗ std::stringstream ss;
23
24 // decompose based on comma
25 ✗ boost::regex re("([^,]+),?([^,]*)");
26 ✗ boost::smatch match;
27 ✗ bool ok = boost::regex_match(fileNameAndPathPair, match, re);
28 ✗ if (!ok) {
29 ✗ ss << "Unable to decompose " << fileNameAndPathPair << " into name and path. Type 'GenerateIddFactory --help' if usage is unclear.";
30 ✗ throw std::runtime_error(ss.str().c_str());
31 }
32 ✗ std::string firstStr = std::string(match[1].first, match[1].second);
33 ✗ std::string secondStr = std::string(match[2].first, match[2].second);
34 ✗ boost::trim(firstStr);
35 ✗ boost::trim(secondStr);
36 ✗ if (secondStr.empty()) {
37 ✗ secondStr = firstStr;
38 ✗ firstStr.clear();
39 }
40 ✗ m_fileName = firstStr;
41 ✗ m_filePath = path(secondStr);
42
43 // validate filePath
44 ✗ m_filePath = openstudio::filesystem::system_complete(m_filePath);
45 ✗ if (!openstudio::filesystem::is_regular_file(m_filePath)) {
46 ss << "Unable to locate intended idd file from input argument '" << fileNameAndPathPair << "'. User-supplied path resolved to '"
47 ✗ << m_filePath.string() << "'.";
48 ✗ throw std::runtime_error(ss.str().c_str());
49 }
50
51 // validate fileName
52 ✗ if (m_fileName.empty()) {
53 ✗ m_fileName = m_filePath.stem().string();
54 }
55 ✗ re = boost::regex("[a-zA-Z]\\w*");
56 ✗ ok = boost::regex_match(m_fileName, match, re);
57 ✗ if (!ok) {
58 ✗ ss << "'" << m_fileName << "' is an invalid Idd file name. " << "Please use Perl regex word characters, and start the name with a letter.";
59 ✗ throw std::runtime_error(ss.str().c_str());
60 }
61
62 std::cout << "IddFileFactoryData object created: " << '\n'
63 ✗ << " fileName = " << m_fileName << '\n'
64 ✗ << " filePath = " << m_filePath.string() << '\n'
65 ✗ << '\n';
66 ✗ }
67
68 ✗ void IddFileFactoryData::parseFile(const path& outPath, const std::string& outFileHeader, GenerateIddFactoryOutFiles& outFiles, int iddFileIndex) {
69 ✗ std::stringstream ss;
70
71 ✗ openstudio::filesystem::ifstream iddFile(m_filePath);
72 ✗ if (!iddFile) {
73 ✗ ss << "Unable to open Idd file " << m_fileName << " located at " << m_filePath.string() << ".";
74 ✗ throw std::runtime_error(ss.str().c_str());
75 }
76
77 ✗ std::stringstream header;
78 ✗ std::string line;
79 ✗ std::string trimLine;
80 ✗ boost::smatch matches;
81 ✗ int lineNum = 1;
82 ✗ const std::shared_ptr<IddFactoryOutFile>& cxxFile = outFiles.iddFactoryIddFileCxxs[iddFileIndex];
83
84 // get version
85 ✗ std::getline(iddFile, line);
86 ✗ trimLine = line;
87 ✗ boost::trim(trimLine);
88 ✗ bool ok = boost::regex_search(trimLine, matches, iddRegex::version());
89 ✗ if (!ok) {
90 ✗ ss << "Idd file " << m_fileName << " located at " << m_filePath.string() << " does not list its version on the first line, which is: " << '\n'
91 ✗ << line;
92 ✗ throw std::runtime_error(ss.str().c_str());
93 }
94 ✗ m_version = std::string(matches[1].first, matches[1].second);
95 ✗ boost::trim(m_version);
96 ✗ header << m_readyLineForOutput(line) << '\n';
97
98 // get rest of header
99 ✗ while (std::getline(iddFile, line)) {
100 ✗ ++lineNum;
101 ✗ trimLine = line;
102 ✗ boost::trim(trimLine);
103 ✗ if (trimLine.empty()) {
104 ✗ break;
105 }
106 ✗ if (!boost::regex_match(trimLine, iddRegex::commentOnlyLine())) {
107 ✗ ss << "Idd file " << m_fileName << "'s header is not separated from other data by a blank line.";
108 ✗ throw std::runtime_error(ss.str().c_str());
109 }
110 ✗ header << m_readyLineForOutput(line) << '\n';
111 }
112 ✗ m_header = header.str();
113
114 // parse body of file
115 ✗ std::string group;
116 ✗ std::string includeFile;
117 ✗ while (std::getline(iddFile, line)) {
118 ✗ ++lineNum;
119 ✗ trimLine = line;
120 ✗ boost::trim(trimLine);
121
122 ✗ if (trimLine.empty()) {
123 ✗ continue;
124 }
125 ✗ if (boost::regex_match(trimLine, iddRegex::commentOnlyLine())) {
126 ✗ continue;
127 }
128
129 // look for group identifier
130 ✗ if (boost::regex_search(trimLine, matches, iddRegex::group())) {
131 ✗ group = std::string(matches[1].first, matches[1].second);
132 ✗ boost::trim(group);
133 ✗ continue;
134 }
135
136 // look for include file
137 ✗ if (boost::regex_search(trimLine, matches, iddRegex::includeFile())) {
138 ✗ includeFile = std::string(matches[1].first, matches[1].second);
139 ✗ boost::trim(includeFile);
140 ✗ m_includedFiles.push_back(FileNameRemovedObjectsPair(includeFile, std::vector<std::string>()));
141 ✗ continue;
142 }
143
144 // look for object to remove from include file
145 ✗ if (boost::regex_search(trimLine, matches, iddRegex::removeObject())) {
146 ✗ if (includeFile.empty()) {
147 ss << "Cannot process \\remove-object identifier: " << '\n'
148 << line << '\n'
149 ✗ << "found on line " << lineNum << " of Idd file '" << m_fileName << "', because it " << '\n'
150 ✗ << "is not preceded by a non-empty \\include-file identifier.";
151 ✗ throw std::runtime_error(ss.str().c_str());
152 }
153 ✗ std::string objectToRemove = std::string(matches[1].first, matches[1].second);
154 ✗ objectToRemove = m_convertName(objectToRemove);
155 ✗ m_includedFiles.back().second.push_back(objectToRemove);
156 ✗ continue;
157 ✗ }
158
159 // look for IddObject
160 ✗ StringPair objectName;
161 ✗ ok = boost::regex_search(trimLine, matches, iddRegex::line());
162 ✗ if (!ok) {
163 ✗ ss << "Unable to extract object name from line " << lineNum << "," << '\n' << line << '\n' << ", of Idd file '" << m_fileName << "'.";
164 ✗ throw std::runtime_error(ss.str().c_str());
165 }
166 ✗ objectName.second = std::string(matches[1].first, matches[1].second);
167 ✗ boost::trim(objectName.second);
168 ✗ objectName.first = m_convertName(objectName.second);
169 ✗ m_objectNames.push_back(objectName);
170
171 // start writing create function
172 ✗ cxxFile->tempFile << '\n'
173 << "IddObject create" << objectName.first << "IddObject() {" << '\n'
174 << '\n'
175 << " static const IddObject object = []{" << '\n'
176 << '\n'
177 << " // Rely on C++11 static initialization and Initialize on First Use Idiom" << '\n'
178 << " // to make sure all statics are initialized properly, thread safely" << '\n'
179 << " std::stringstream ss;" << '\n'
180 ✗ << " ss << \"" << m_readyLineForOutput(line) << "\\n\";";
181
182 // start collecting field names
183 // (requires \field tag, which is expected to occur one per line)
184 ✗ std::vector<std::string> fieldNames;
185 ✗ std::vector<std::string> extensibleFieldNames;
186
187 // get rest of object
188 ✗ while (std::getline(iddFile, line)) {
189 ✗ ++lineNum;
190 ✗ trimLine = line;
191 ✗ boost::trim(trimLine);
192 ✗ if (trimLine.empty()) {
193 // finish writing create function
194 ✗ cxxFile->tempFile << '\n'
195 << '\n'
196 << " IddObjectType objType(IddObjectType::" << objectName.first << ");" << '\n'
197 << " OptionalIddObject oObj = IddObject::load(\"" << objectName.second << "\"," << '\n'
198 << " \"" << group << "\"," << '\n'
199 << " ss.str()," << '\n'
200 << " objType);" << '\n'
201 << " OS_ASSERT(oObj);" << '\n'
202 << " return *oObj;" << '\n'
203 << " }(); // immediately invoked lambda" << '\n'
204 << '\n'
205 << " OS_ASSERT(object.type() == IddObjectType::" << objectName.first << ");" << '\n'
206 << " return object;" << '\n'
207 ✗ << "}" << '\n';
208
209 // write field enums
210 ✗ if (!fieldNames.empty() || !extensibleFieldNames.empty()) {
211 // file for this object
212 ✗ ss << objectName.first << "_FieldEnums.hxx";
213 ✗ std::string filename = ss.str();
214 ✗ ss.str("");
215
216 ✗ IddFactoryOutFile fieldEnumHxx(filename, outPath, outFileHeader);
217 ✗ std::string upperObjectName(objectName.first);
218 ✗ boost::algorithm::to_upper(upperObjectName);
219
220 ✗ std::stringstream tempSS;
221
222 fieldEnumHxx.tempFile << "#ifndef UTILITIES_IDD_" << upperObjectName << "_FIELDENUMS_HXX" << '\n'
223 << "#define UTILITIES_IDD_" << upperObjectName << "_FIELDENUMS_HXX" << '\n'
224 << '\n'
225 << "#include <utilities/UtilitiesAPI.hpp>" << '\n'
226 << "#include <utilities/core/Enum.hpp>" << '\n'
227 << '\n'
228 ✗ << "namespace openstudio {";
229
230 ✗ if (!fieldNames.empty()) {
231 ✗ std::list<std::pair<std::string, std::string>> fieldNamePairs;
232 ✗ for (const std::string& name : fieldNames) {
233 ✗ fieldNamePairs.emplace_back(m_convertName(name), name);
234 }
235 ✗ writeEnumFast(tempSS, objectName.first + "Fields", fieldNamePairs);
236
237 fieldEnumHxx.tempFile << '\n'
238 << "/** \\class " << objectName.first << "Fields" << '\n'
239 << " * \\brief Enumeration of " << objectName.second << "'s Idd and Idf non-extensible fields. " << '\n'
240 << " * \\details This enumeration provides readable, and semi-robust tags to use instead" << '\n'
241 << " * of bare unsigned integer field indices. (The enumeration value names are more likely" << '\n'
242 << " * to be valid across EnergyPlus and OpenStudio releases, and will break if they become" << '\n'
243 << " * invalid.) See the OPENSTUDIO_ENUM documentation in utilities/core/Enum.hpp. The actual macro" << '\n'
244 << " * call is:" << '\n'
245 << " *" << '\n'
246 << " * \\code"
247 << '\n'
248 // << tempSS.str() << '\n'
249 << " * \\endcode */" << '\n'
250 ✗ << tempSS.str() << '\n';
251
252 ✗ tempSS.str("");
253 ✗ }
254
255 ✗ if (!extensibleFieldNames.empty()) {
256 ✗ std::list<std::pair<std::string, std::string>> extensibleFieldNamePairs;
257 ✗ for (const std::string& name : extensibleFieldNames) {
258 ✗ extensibleFieldNamePairs.emplace_back(m_convertName(name), name);
259 }
260 ✗ writeEnumFast(tempSS, objectName.first + "ExtensibleFields", extensibleFieldNamePairs);
261
262 fieldEnumHxx.tempFile << '\n'
263 << "/** \\class " << objectName.first << "ExtensibleFields" << '\n'
264 << " * \\brief Enumeration of " << objectName.second << "'s Idd and Idf extensible fields" << '\n'
265 << " * to be used in conjunction with ExtensibleIndex and ExtensibleGroup." << '\n'
266 << " * \\details This enumeration provides readable, and semi-robust tags to use instead" << '\n'
267 << " * of bare unsigned integer ExtensibleIndex.field indices. (The enumeration value names" << '\n'
268 << " * are more likely to be valid across EnergyPlus and OpenStudio releases, and will break" << '\n'
269 << " * if they become invalid.)See the OPENSTUDIO_ENUM documentation in utilities/core/Enum.hpp. The actual macro"
270 << '\n'
271 << " * call is:" << '\n'
272 << " *" << '\n'
273 << " * \\code"
274 << '\n'
275 // << tempSS.str() << '\n'
276 << " * \\endcode */" << '\n'
277 ✗ << tempSS.str() << '\n';
278
279 ✗ tempSS.str("");
280 ✗ }
281
282 fieldEnumHxx.tempFile << '\n'
283 << "} // openstudio" << '\n'
284 << '\n'
285 ✗ << "#endif // UTILITIES_IDD_" << upperObjectName << "_FIELDENUMS_HXX" << '\n';
286 ✗ outFiles.finalizeIddFactoryOutFile(fieldEnumHxx);
287
288 // add to aggregate headers
289 ✗ outFiles.iddFieldEnumsHxx.tempFile << "#include <utilities/idd/" << filename << ">" << '\n';
290 ✗ outFiles.iddFieldEnumsIxx.tempFile << "%include <utilities/idd/" << filename << ">" << '\n';
291 ✗ }
292
293 ✗ break;
294 }
295
296 // continue writing create function
297 ✗ cxxFile->tempFile << '\n' << " ss << \"" << m_readyLineForOutput(line) << "\\n\";";
298
299 // look for field name
300 ✗ std::string fieldName;
301 ✗ if (boost::regex_search(trimLine, matches, iddRegex::name())) {
302 ✗ fieldName = std::string(matches[1].first, matches[1].second);
303 ✗ fieldName = boost::regex_replace(fieldName, boost::regex(","), "");
304 ✗ boost::trim(fieldName);
305 ✗ if (extensibleFieldNames.empty()) {
306 ✗ fieldNames.push_back(fieldName);
307 } else {
308 // strip out numbers
309 ✗ fieldName = boost::regex_replace(fieldName, iddRegex::numberAndPrecedingSpace(), "", boost::format_first_only);
310 ✗ boost::trim(fieldName);
311 ✗ extensibleFieldNames.push_back(fieldName);
312 }
313 }
314
315 // look for extensible identifier
316 ✗ if (boost::regex_search(trimLine, iddRegex::beginExtensible())) {
317 ✗ if (fieldNames.empty()) {
318 std::cerr << "Extensible processing unable to proceed because " << objectName.second
319 << " is extensible but is missing \\begin-extensible flag." << '\n'
320 ✗ << '\n';
321 }
322 assert(!fieldNames.empty());
323 // cppcheck 1.x chokes on this, 2.3 doesn't, but ignore it in case someone has an older version
324 // cppcheck-suppress containerOutOfBounds
325 ✗ fieldName = fieldNames.back();
326 // strip out numbers and transfer from fieldNames to extensibleFieldNames
327 ✗ fieldName = boost::regex_replace(fieldName, iddRegex::numberAndPrecedingSpace(), "", boost::format_first_only);
328 ✗ boost::trim(fieldName);
329 ✗ extensibleFieldNames.push_back(fieldName);
330 ✗ fieldNames.pop_back();
331 }
332
333 ✗ } // while -- IddObject
334
335 ✗ } // while -- IddFile
336
337 ✗ iddFile.close();
338 ✗ std::cout << "Parsed Idd file " << m_fileName << " located at " << m_filePath.string() << "," << '\n'
339 ✗ << "which contains " << m_objectNames.size() << " objects." << '\n'
340 ✗ << '\n';
341 ✗ if (!m_includedFiles.empty()) {
342 ✗ for (const FileNameRemovedObjectsPair& p : m_includedFiles) {
343 ✗ std::cout << "Idd file '" << m_fileName << "' includes all but " << p.second.size() << " objects of Idd file '" << p.first << "'." << '\n'
344 ✗ << '\n';
345 }
346 }
347
348 // register objects with CallbackMap
349 ✗ cxxFile->tempFile << "void IddFactory::register" << fileName() << "ObjectsInCallbackMap() {" << '\n';
350 ✗ for (const StringPair& objectName : objectNames()) {
351 ✗ cxxFile->tempFile << " m_callbackMap.insert(IddObjectCallbackMap::value_type(IddObjectType::" << objectName.first << ",create"
352 ✗ << objectName.first << "IddObject));" << '\n';
353 ✗ }
354 ✗ cxxFile->tempFile << "}" << '\n' << '\n';
355 ✗ }
356
357 ✗ std::string IddFileFactoryData::fileName() const {
358 ✗ return m_fileName;
359 }
360
361 ✗ std::string IddFileFactoryData::version() const {
362 ✗ return m_version;
363 }
364
365 ✗ std::string IddFileFactoryData::header() const {
366 ✗ return m_header;
367 }
368
369 ✗ std::vector<std::pair<std::string, std::string>> IddFileFactoryData::objectNames() const {
370 ✗ return m_objectNames;
371 }
372
373 ✗ unsigned IddFileFactoryData::numIncludedFiles() const {
374 ✗ return m_includedFiles.size();
375 }
376
377 ✗ IddFileFactoryData::FileNameRemovedObjectsPair IddFileFactoryData::includedFile(unsigned index) const {
378 assert(index < numIncludedFiles());
379 ✗ return m_includedFiles[index];
380 }
381
382 ✗ std::string IddFileFactoryData::m_convertName(const std::string& originalName) {
383 ✗ std::string result(originalName);
384 ✗ boost::trim(result);
385 ✗ result = boost::regex_replace(result, boost::regex("^100 ?%"), "All");
386 ✗ result = boost::regex_replace(result, boost::regex("[ \\-]+"), "\\u");
387 ✗ result = boost::regex_replace(result, boost::regex("[:/,\\(\\)%]+"), "_");
388 ✗ result = boost::regex_replace(result, boost::regex("[\\.\\?\\]\\[]"), "");
389 ✗ result = boost::regex_replace(result, boost::regex("="), "_EQUAL_");
390 ✗ result = boost::regex_replace(result, boost::regex("\\*\\*"), "_POW_");
391 ✗ result = boost::regex_replace(result, boost::regex("\\+"), "_PLUS_");
392 ✗ result = boost::regex_replace(result, boost::regex("\\*"), "_TIMES_");
393 ✗ return result;
394 ✗ }
395
396 ✗ std::string IddFileFactoryData::m_readyLineForOutput(const std::string& line) {
397 ✗ std::string result(line);
398 ✗ result = boost::regex_replace(result, boost::regex("\\\\"), "\\\\\\\\");
399 ✗ result = boost::regex_replace(result, boost::regex("\""), "\\\\\"");
400 ✗ boost::trim(result);
401 ✗ return result;
402 ✗ }
403
404 } // namespace openstudio
405