Skip to content

Commit

Permalink
Merge pull request #176 from AliHaider93/main
Browse files Browse the repository at this point in the history
readded formats mistakenly deleted in pull request #144
these changes were deleted by mistake, and they handle various format forms of a .k file
  • Loading branch information
drossberg authored Mar 3, 2025
2 parents ab19546 + 3961763 commit 6212069
Showing 1 changed file with 265 additions and 4 deletions.
269 changes: 265 additions & 4 deletions src/conv/k-g/k_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ enum class Options {
};


enum class ReadFormat {
Standard,
Long,
I10
};


static std::string read_line
(
std::istream& is
Expand Down Expand Up @@ -132,12 +139,94 @@ static std::vector<std::string> read_line_node_standard
const char* line
) {
std::vector<std::string> 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<std::string> read_line_node_long
(
const char* line
) {
std::vector<std::string> 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();
Expand All @@ -146,6 +235,9 @@ static std::vector<std::string> read_line_node_standard
temp += line[i];
}
}
else if (temp.size() == 0) {
ret.push_back("0");
}
else
continue;
}
Expand All @@ -163,6 +255,107 @@ static std::vector<std::string> 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<std::string> read_line_node_i10
(
const char* line
) {
std::vector<std::string> 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<std::string, std::string> split_key
(
const char* key
) {
std::pair<std::string, std::string> 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;
}

Expand All @@ -177,7 +370,7 @@ static std::vector<std::string> 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();
Expand Down Expand Up @@ -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<std::string> nodeLines;//in old .k files a node with Long format is split in two lines.
std::vector<std::string> tokens;
const size_t FirstNode = 2;
std::vector<std::string> optionsContainer;
Expand Down Expand Up @@ -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")) {
Expand Down Expand Up @@ -495,7 +710,7 @@ bool parse_k
std::cout << "Unexpected command " << tokens[0] << " in k-file " << fileName << "\n";
}
else if (command[0] == "PART") {
if ((command.size() == 1) || (command[1] == "INERTIA")) {
if ((command.size() == 1) || (command[1] == "INERTIA") || (command[1] == "AVERAGED")) {
state = KState::Part;
partLinesRead = 0;
partTitle = "";
Expand Down Expand Up @@ -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<std::string, std::string> 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;
Expand All @@ -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<std::string> 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";
Expand Down

0 comments on commit 6212069

Please sign in to comment.