From 7be772dff6df392bf33c8e12486cfe78f7034184 Mon Sep 17 00:00:00 2001 From: louisgoogl Date: Sat, 1 Feb 2025 17:26:56 +0100 Subject: [PATCH] Format mistakenly deleted changes Pull request 144 --- src/conv/k-g/k_parser.cpp | 267 +++++++++++++++++++++++++++++++++++++- 1 file changed, 264 insertions(+), 3 deletions(-) diff --git a/src/conv/k-g/k_parser.cpp b/src/conv/k-g/k_parser.cpp index 3111416163..809806fa93 100644 --- a/src/conv/k-g/k_parser.cpp +++ b/src/conv/k-g/k_parser.cpp @@ -103,6 +103,13 @@ enum class Options { }; +enum class ReadFormat { + Standard, + Long, + I10 +}; + + static std::string read_line ( std::istream& is @@ -132,12 +139,94 @@ static std::vector read_line_node_standard const char* line ) { std::vector ret; + bool commaSeparated = false; + + if (line != nullptr) { + std::string temp; + + if (strchr(line, ',') != nullptr) { + commaSeparated = true; + } + + for (size_t i = 0; i < strlen(line); ++i) { + if (line[i] == ',') { + if (temp.size() > 0) { + ret.push_back(temp); + temp.clear(); + } + else + ret.push_back("0"); + + continue; + } + + if (((i == 8) || (i == 24) || (i == 40) || (i == 56) || (i == 64) || (i == 72)) && !commaSeparated) { + if (temp.size() > 0) { + ret.push_back(temp); + temp.clear(); + + if (!(line[i] == ' ')) { + temp += line[i]; + } + } + else if (temp.size() == 0) { + ret.push_back("0"); + } + else + continue; + } + else if ((line[i] == ' ')) { + continue; + } + else if ((line[i] == '\t')) { + break; + } + else + temp += line[i]; + } + + if (temp.size() > 0) + ret.push_back(temp); + } + + if (ret.size() < 6) { + for (size_t i_0 = ret.size(); i_0 < 6; ++i_0) { + ret.push_back("0"); + } + } + + return ret; +} + + +// Long node line format is I20,3E20.0,2F20.0 (20 digit integer, 3x 20 digit doubles, 2x 20 digit floats). +static std::vector read_line_node_long +( + const char* line +) { + std::vector ret; + bool commaSeparated = false; if (line != nullptr) { std::string temp; + if (strchr(line, ',') != nullptr) { + commaSeparated = true; + } + for (size_t i = 0; i < strlen(line); ++i) { - if ((i == 8) || (i == 24) || (i == 40) || (i == 56) || (i == 64) || (i == 72)) { + if (line[i] == ',') { + if (temp.size() > 0) { + ret.push_back(temp); + temp.clear(); + } + else + ret.push_back("0"); + + continue; + } + + if (((i == 20) || (i == 40) || (i == 60) || (i == 80) || (i == 100) || (i == 120)) && !commaSeparated) { if (temp.size() > 0) { ret.push_back(temp); temp.clear(); @@ -146,6 +235,9 @@ static std::vector read_line_node_standard temp += line[i]; } } + else if (temp.size() == 0) { + ret.push_back("0"); + } else continue; } @@ -163,6 +255,107 @@ static std::vector read_line_node_standard ret.push_back(temp); } + if (ret.size() < 6) { + for (size_t i_0 = ret.size(); i_0 < 6; ++i_0) { + ret.push_back("0"); + } + } + + return ret; +} + +// I10 node line format is I10,3E16.0,2F10.0 (10 digit integer, 3x 16 digit doubles, 2x 10 digit floats). +static std::vector read_line_node_i10 +( + const char* line +) { + std::vector ret; + bool commaSeparated = false; + + if (line != nullptr) { + std::string temp; + + if (strchr(line, ',') != nullptr) { + commaSeparated = true; + } + + for (size_t i = 0; i < strlen(line); ++i) { + if (line[i] == ',') { + if (temp.size() > 0) { + ret.push_back(temp); + temp.clear(); + } + else + ret.push_back("0"); + + continue; + } + + if (((i == 10) || (i == 26) || (i == 42) || (i == 58) || (i == 68) || (i == 78)) && !commaSeparated) { + if (temp.size() > 0) { + ret.push_back(temp); + temp.clear(); + + if (!(line[i] == ' ')) { + temp += line[i]; + } + } + else if (temp.size() == 0) { + ret.push_back("0"); + } + else + continue; + } + else if ((line[i] == ' ')) { + continue; + } + else if ((line[i] == '\t')) { + break; + } + else + temp += line[i]; + } + + if (temp.size() > 0) + ret.push_back(temp); + } + + if (ret.size() < 6) { + for (size_t i_0 = ret.size(); i_0 < 6; ++i_0) { + ret.push_back("0"); + } + } + + return ret; +} + + +std::pair split_key +( + const char* key +) { + std::pair ret; + + if (key != nullptr) { + std::string temp; + + for (size_t i = 0; i < strlen(key); ++i) { + if (key[i] == '=') { + if (temp.size() > 0) { + ret.first = temp; + temp.clear(); + } + } + else if (key[i] != '=') { + temp += key[i]; + } + } + + if (temp.size() > 0) { + ret.second = temp; + } + } + return ret; } @@ -177,7 +370,7 @@ static std::vector parse_line std::string temp; for (size_t i = 0; i < strlen(line); ++i) { - if ((line[i] == ' ') || (line[i] == '\t')) { + if ((line[i] == ' ') || (line[i] == '\t') || (line[i] == ',')) { if (temp.size() > 0) { ret.push_back(temp); temp.clear(); @@ -249,6 +442,9 @@ bool parse_k int sectionElForm = 0; //int CST = 0; std::string line = read_line(is); + ReadFormat fileFormat = ReadFormat::Standard; + ReadFormat nodeFormat = ReadFormat::Standard; + std::vector nodeLines;//in old .k files a node with Long format is split in two lines. std::vector tokens; const size_t FirstNode = 2; std::vector optionsContainer; @@ -285,6 +481,25 @@ bool parse_k state = KState::Node; else std::cout << "Unexpected command " << tokens[0] << " in k-file " << fileName << "\n"; + + if (tokens.size() == 1) { + nodeFormat = fileFormat; + } + else if (tokens.size() == 2) { + if (tokens[1][0] == '-') { + nodeFormat = ReadFormat::Standard; + } + else if (tokens[1][0] == '+') { + nodeFormat = ReadFormat::Long; + } + else if (tokens[1][0] == '%') { + nodeFormat = ReadFormat::I10; + } + else + std::cout << "Unhandeled format" << tokens[1] << "in k-file" << fileName << "\n"; + } + else + std::cout << "Unhandeled node format in k-file" << fileName << "\n"; } else if (command[0] == "ELEMENT") { if ((command.size() > 1) && (command[1] == "BEAM")) { @@ -578,6 +793,23 @@ bool parse_k else std::cout << "Unexpected command " << tokens[0] << " in k-file " << fileName << "\n"; } + else if (command[0] == "KEYWORD") { + if (tokens.size() > 1) { + for (size_t i = 1; i < tokens.size(); ++i) { + std::pair format = split_key((tokens[i]).c_str()); + + if (format.first == "LONG") { + fileFormat = ReadFormat::Long; + } + else if (format.first == "I10") { + fileFormat = ReadFormat::I10; + break; + } + } + } + else + fileFormat = ReadFormat::Standard; + } } handledLine = true; @@ -591,7 +823,36 @@ bool parse_k break; case KState::Node: { - tokens = read_line_node_standard(line.c_str()); + if (nodeFormat == ReadFormat::Standard) { + tokens = read_line_node_standard(line.c_str()); + } + else if (nodeFormat == ReadFormat::Long) { + std::vector tempLine = read_line_node_long(line.c_str()); + + if (nodeLines.size() == 0) { + if (tempLine.size() == 4) { + nodeLines = tempLine; + break; + } + else if (tempLine.size() == 6) { + tokens = tempLine; + } + else + std::cout << "Error a node with Long format can be written in one line of 6 variables or split in two lines, the first line should contain 4 variables "; + } + else { + if (tempLine.size() == 2) { + nodeLines.insert(nodeLines.end(), tempLine.begin(), tempLine.end()); + tokens = nodeLines; + nodeLines.clear(); + } + else + std::cout << "Error a node with Long format should be split in two lines, the second line should contain 2 variables"; + } + } + else if (nodeFormat == ReadFormat::I10) { + tokens = read_line_node_i10(line.c_str()); + } if (tokens.size() < 4) { std::cout << "Too short NODE in k-file " << fileName << "\n";