src/generateiddfactory/GenerateIddFactoryOutFiles.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 "GenerateIddFactoryOutFiles.hpp" | ||
| 7 | #include "IddFileFactoryData.hpp" | ||
| 8 | |||
| 9 | #include "../utilities/core/Checksum.hpp" | ||
| 10 | |||
| 11 | #include <boost/regex.hpp> | ||
| 12 | |||
| 13 | #include <sstream> | ||
| 14 | |||
| 15 | namespace openstudio { | ||
| 16 | |||
| 17 | ✗ | IddFactoryOutFile::IddFactoryOutFile(const std::string& filename, const openstudio::path& outPath, const std::string& outFileHeader) | |
| 18 | ✗ | : filename(filename), finalPath(outPath / path(filename)), tempPath(outPath / path(filename + ".temp")) { | |
| 19 | ✗ | std::stringstream ss; | |
| 20 | ✗ | tempFile.open(tempPath); | |
| 21 | ✗ | if (!tempFile) { | |
| 22 | ✗ | ss << "Unable to open '" << tempPath.string() << "' for writing."; | |
| 23 | ✗ | throw std::runtime_error(ss.str().c_str()); | |
| 24 | } | ||
| 25 | ✗ | tempFile << outFileHeader << '\n'; | |
| 26 | ✗ | } | |
| 27 | |||
| 28 | ✗ | std::string IddFactoryOutFile::finalize(const std::string& /*oldChecksum*/) { | |
| 29 | ✗ | tempFile.close(); | |
| 30 | ✗ | std::string newChecksum = openstudio::checksum(tempPath); | |
| 31 | // ETH@20111122 Always copy for now. CMake/build process can't yet handle "sometimes generated" | ||
| 32 | // files. | ||
| 33 | ✗ | bool copyFile = true; // (newChecksum != oldChecksum); | |
| 34 | // cppcheck-suppress knownConditionTrueFalse | ||
| 35 | ✗ | if (copyFile) { | |
| 36 | ✗ | if (openstudio::filesystem::exists(finalPath)) { | |
| 37 | ✗ | openstudio::filesystem::remove(finalPath); | |
| 38 | } | ||
| 39 | ✗ | openstudio::filesystem::copy_file(tempPath, finalPath); | |
| 40 | } | ||
| 41 | ✗ | openstudio::filesystem::remove(tempPath); | |
| 42 | ✗ | return newChecksum; | |
| 43 | ✗ | } | |
| 44 | |||
| 45 | ✗ | GenerateIddFactoryOutFiles::GenerateIddFactoryOutFiles(const path& outPath, const std::string& outFileHeader, | |
| 46 | ✗ | const std::vector<IddFileFactoryData>& iddFiles) | |
| 47 | ✗ | : iddEnumsHxx("IddEnums.hxx", outPath, outFileHeader), | |
| 48 | ✗ | iddFieldEnumsHxx("IddFieldEnums.hxx", outPath, outFileHeader), | |
| 49 | ✗ | iddFieldEnumsIxx("IddFieldEnums.ixx", outPath, outFileHeader), | |
| 50 | ✗ | iddFactoryHxx("IddFactory.hxx", outPath, outFileHeader), | |
| 51 | ✗ | iddFactoryCxx("IddFactory.cxx", outPath, outFileHeader), | |
| 52 | ✗ | m_fileIndexPath(outPath / path("IddFactoryFileIndex.hxx")) { | |
| 53 | ✗ | for (const IddFileFactoryData& iddFile : iddFiles) { | |
| 54 | ✗ | std::shared_ptr<IddFactoryOutFile> cxxFile(new IddFactoryOutFile("IddFactory_" + iddFile.fileName() + ".cxx", outPath, outFileHeader)); | |
| 55 | ✗ | iddFactoryIddFileCxxs.push_back(cxxFile); | |
| 56 | ✗ | } | |
| 57 | |||
| 58 | ✗ | loadIddFactoryFileIndex(); | |
| 59 | ✗ | } | |
| 60 | |||
| 61 | ✗ | void GenerateIddFactoryOutFiles::finalize() { | |
| 62 | ✗ | finalizeIddFactoryOutFile(iddEnumsHxx); | |
| 63 | ✗ | finalizeIddFactoryOutFile(iddFieldEnumsHxx); | |
| 64 | ✗ | finalizeIddFactoryOutFile(iddFieldEnumsIxx); | |
| 65 | ✗ | finalizeIddFactoryOutFile(iddFactoryHxx); | |
| 66 | ✗ | finalizeIddFactoryOutFile(iddFactoryCxx); | |
| 67 | ✗ | for (std::shared_ptr<IddFactoryOutFile>& cxxFile : iddFactoryIddFileCxxs) { | |
| 68 | ✗ | finalizeIddFactoryOutFile(*cxxFile); | |
| 69 | } | ||
| 70 | |||
| 71 | ✗ | writeIddFactoryFileIndex(); | |
| 72 | ✗ | } | |
| 73 | |||
| 74 | ✗ | void GenerateIddFactoryOutFiles::finalizeIddFactoryOutFile(IddFactoryOutFile& outFile) { | |
| 75 | ✗ | std::string cs = checksumMap[outFile.filename].first; | |
| 76 | ✗ | cs = outFile.finalize(cs); | |
| 77 | ✗ | checksumMap[outFile.filename] = std::pair<std::string, bool>(cs, true); | |
| 78 | ✗ | } | |
| 79 | |||
| 80 | ✗ | void GenerateIddFactoryOutFiles::loadIddFactoryFileIndex() { | |
| 81 | ✗ | if (openstudio::filesystem::exists(m_fileIndexPath)) { | |
| 82 | ✗ | openstudio::filesystem::ifstream fileIndex(m_fileIndexPath); | |
| 83 | ✗ | std::string line; | |
| 84 | ✗ | boost::regex re("// ([^,]*),(.*)"); | |
| 85 | ✗ | boost::smatch m; | |
| 86 | ✗ | while (std::getline(fileIndex, line)) { | |
| 87 | ✗ | if (boost::regex_match(line, m, re)) { | |
| 88 | ✗ | std::string filename(m[1].first, m[1].second); | |
| 89 | ✗ | std::string cs(m[2].first, m[2].second); | |
| 90 | ✗ | checksumMap[filename] = std::pair<std::string, bool>(cs, false); | |
| 91 | ✗ | } | |
| 92 | } | ||
| 93 | ✗ | } | |
| 94 | ✗ | } | |
| 95 | |||
| 96 | ✗ | void GenerateIddFactoryOutFiles::writeIddFactoryFileIndex() { | |
| 97 | ✗ | openstudio::filesystem::ofstream fileIndex(m_fileIndexPath); | |
| 98 | ✗ | for (auto it = checksumMap.begin(), itEnd = checksumMap.end(); it != itEnd; ++it) { | |
| 99 | ✗ | if (it->second.second) { | |
| 100 | ✗ | fileIndex << "// " << it->first << "," << it->second.first << '\n'; | |
| 101 | } | ||
| 102 | } | ||
| 103 | ✗ | } | |
| 104 | |||
| 105 | } // namespace openstudio | ||
| 106 |