Skip to content

Commit

Permalink
some changes to "clean" the element-file a bit
Browse files Browse the repository at this point in the history
- add trim-function to remove trailing and ending whitespace from strings
- trim localized names of element
- manage element-information:
  for some time now and for whatever reason, element-editor adds
  element-information without content –> remove empty info-lines
- use trim-function for element-information
- sort element-information alphabetically by name
- add self to author-information
- add default terminal-size, if it can't be detected
- adjust min-length for polygon-sections
- changed saving-routine for saving xml
- adjust version-number
  • Loading branch information
plc-user committed Feb 5, 2025
1 parent 478e1a5 commit 06edd3a
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 17 deletions.
69 changes: 66 additions & 3 deletions inc/elements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <iomanip> // for IO-Operations
#include <string> // for string-handling
#include <sstream> // for String-Streams

#include <algorithm> // for std::sort

//
//--- implementation of class "DefinitionLine" ---------------------------------
Expand Down Expand Up @@ -95,8 +95,8 @@ void NamesList::ReadFromPugiNode(pugi::xml_node node)
{
node = node.first_child();
for (; node; node = node.next_sibling()){
std::string lang = node.attribute("lang").as_string();
std::string name = node.child_value();
std::string lang = trim(node.attribute("lang").as_string());
std::string name = trim(node.child_value());
AddName(lang, name);
// keine sort-Funktion: Das macht die std::map automatisch!
}
Expand All @@ -117,6 +117,69 @@ void NamesList::WriteToPugiNode(pugi::xml_node node)



//
//--- implementation of class "ElementInfo" --------------------------------------
//
void ElementInfo::ReadFromPugiNode(pugi::xml_node node)
{
node = node.first_child();
for (; node; node = node.next_sibling()){
std::string name = node.attribute("name").as_string();
std::string text = trim(node.child_value());
bool show = node.attribute("show").as_bool();
AddInfo(EInfo{name, text, show});
}
// abschließend die Liste sortieren nach "name":
std::sort(info.begin(), info.end(),
[](const EInfo& v1, const EInfo& v2) {
return v1.name < v2.name;
}
);
}
void ElementInfo::WriteToPugiNode(pugi::xml_node node)
{
const std::string nodeChild = "elementInformation";
// erstmal alle informationen löschen, wenn's sie gibt:
if (node.child(nodeChild))
while(node.remove_child(nodeChild));
// und jetzt kommen die Namen sortiert wieder rein:
for (const auto& [name, text, show] : info) {
if (text.empty()) continue; // do not save empty lines!
node.append_child(nodeChild).append_attribute("show").set_value((int)show);
node.last_child().append_attribute("name").set_value(name);
node.last_child().text().set(trim(text));
}
}
//
//--- END - implementation of class "ElementInfo" --------------------------------
//



//
//--- implementation of class Author-Information -------------------------------
//
void AuthorInfo::ReadFromPugiNode(pugi::xml_node node)
{
author = trim(node.child_value());
}
void AuthorInfo::UpdatePugiNode(pugi::xml_node node)
{
ReadFromPugiNode(node);
if (author.find("QET_ElementScaler") == std::string::npos)
author += "\nedited with \"QET_ElementScaler\"";
WriteToPugiNode(node);
}
void AuthorInfo::WriteToPugiNode(pugi::xml_node node)
{
node.text().set(trim(author));
}
//
//--- END - implementation of Author-Information -------------------------------
//



//
//--- implementation of class "BaseStyle" --------------------------------------
//
Expand Down
56 changes: 56 additions & 0 deletions inc/elements.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ struct PolyPoint



struct EInfo
{ // a single entry for elementInformations
std::string name = "";
std::string text = "";
bool show = false;
// functions:
EInfo();
EInfo(const std::string n, const std::string t, const bool s): name(n), text(t), show(s) {}
};



//
//--- START - definition of definition-line of Elements ------------------------
//
Expand Down Expand Up @@ -125,6 +137,50 @@ class NamesList {



//
//--- START - definition of informations-list of Elements ----------------------
//
class ElementInfo {
private:
std::vector<EInfo> info;
//
public:
void ReadFromPugiNode(pugi::xml_node);
void WriteToPugiNode(pugi::xml_node);
void AddInfo(const EInfo& ei) { info.push_back(ei); }
void AddInfo(const std::string& name, const std::string& text, const bool& show) { info.push_back(EInfo{name, text, show}); }
void Clear(void) { info.clear(); };
//
protected:
//
};
//
//--- END - definition of informations-list of Elements ------------------------
//



//
//--- START - definition of Author-Information ---------------------------------
//
class AuthorInfo {
private:
std::string author = "";
//
public:
void ReadFromPugiNode(pugi::xml_node);
void UpdatePugiNode(pugi::xml_node);
void WriteToPugiNode(pugi::xml_node);
//
protected:
//
};
//
//--- END - definition of Author-Information -----------------------------------
//



//
//--- START - definition of Base-Classes of Elements ---------------------------
//
Expand Down
35 changes: 34 additions & 1 deletion inc/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ void CheckForDoubleString(std::string& sArg){
// ###############################################################
//
void get_terminal_size(size_t& width, size_t& height) {
// Quelle:
// Quelle (mit eigenen Ergänzungen):
// https://stackoverflow.com/questions/23369503/get-size-of-terminal-window-rows-columns
#if defined(_WIN32)
CONSOLE_SCREEN_BUFFER_INFO csbi;
Expand All @@ -270,6 +270,9 @@ void get_terminal_size(size_t& width, size_t& height) {
ioctl(fileno(stdout), TIOCGWINSZ, &w);
width = (size_t)(w.ws_col);
height = (size_t)(w.ws_row);
#else
width = (size_t)(80);
height = (size_t)(24);
#endif // Windows/Linux
}
// ---
Expand All @@ -283,6 +286,8 @@ size_t get_terminal_height(void) {
struct winsize w;
ioctl(fileno(stdout), TIOCGWINSZ, &w);
return (size_t)(w.ws_row);
#else
return (size_t)(24);
#endif // Windows/Linux
}
// ---
Expand All @@ -296,6 +301,8 @@ size_t get_terminal_width(void) {
struct winsize w;
ioctl(fileno(stdout), TIOCGWINSZ, &w);
return (size_t)(w.ws_col);
#else
return (size_t)(80);
#endif // Windows/Linux
}
//
Expand Down Expand Up @@ -336,3 +343,29 @@ std::string ReadPieceOfFile(const std::string file, size_t pos, const size_t len
// ### END: read a piece from a text-file ###
// ###############################################################
//


//
// ###############################################################
// ### remove whitespace from string ###
// ###############################################################
//
std::string trim(const std::string& source) {
std::string s(source);
if (source.empty())
return s;
// delete leading whitespace
while (std::isspace(s[0])){
s.erase(0,1);
}
// delete trailing whitespace
while (std::isspace(s[s.length()-1])){
s.erase(s.length()-1,1);
}
return s;
}
//
// ###############################################################
// ### END: remove whitespace from string ###
// ###############################################################
//
7 changes: 7 additions & 0 deletions inc/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ size_t get_terminal_width(void);
std::string ReadPieceOfFile(const std::string file, size_t pos, const size_t length);


//
// --- "trim" remove leading and trailing whitespace from a std::string --------
//
std::string trim(const std::string& source);



//
// Funktionen, die vorher auch schon da waren
//
Expand Down
33 changes: 24 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,37 @@ int main(int argc, char **argv)
return 0;
}


// save XML to a string, to be able to "edit" raw data
std::string sXML;
std::stringstream ssxml;
doc.save(ssxml, " ", pugi::format_default | pugi::format_no_declaration);
sXML = ssxml.str();
ssxml.clear();
// delete blanks at end of tags
std::string sOld = " />\n";
std::string sNew = "/>\n";
while (sXML.find(sOld) != std::string::npos) {
sXML.replace(sXML.find(sOld), sOld.size(), sNew);
}

if (xCreateELMT == true) {
if (xPrintToStdOut==true) {
if (_DEBUG_) std::cerr << "XML auf stdout ------------------------------------------------------" << std::endl;
doc.save(std::cout, " ", pugi::format_default | pugi::format_no_declaration);
// no result from "doc.save" when sending XML to cout ...
std::cout << sXML << "\n";
if (_DEBUG_) std::cerr << "XML auf stdout ------------------------------------------------------" << std::endl;
return 0;
} else {
// we try to save the new XML:
if (doc.save_file(ElementFileScaled.c_str(), " ", pugi::format_default | pugi::format_no_declaration) == true) {
if (_DEBUG_) std::cerr << "file \"" << ElementFileScaled << "\" saved successfully!" << std::endl;
return 0;
// save string to file:
std::ofstream outFile(ElementFileScaled);
outFile << sXML;
if ((outFile.rdstate() & std::ofstream::badbit) != 0) {
std::cerr << "file \"" << ElementFileScaled << "\" could not be saved!\n";
} else if ((outFile.rdstate() & std::ofstream::failbit) != 0) {
std::cerr << "saving \"" << ElementFileScaled << "\" failed!\n";
} else {
std::cerr << "file \"" << ElementFileScaled << "\" could not be saved!" << std::endl;
return -1;
if (_DEBUG_) std::cerr << "file \"" << ElementFileScaled << "\" saved successfully!" << std::endl;
}
outFile.close();
}
}

Expand Down
15 changes: 11 additions & 4 deletions main.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
// global variables
// =============================================================================

const std::string sVersion = "v0.5.3beta1";
const std::string sVersion = "v0.5.3";

// the element-file to process:
static std::string ElementFile = "";
Expand Down Expand Up @@ -78,7 +78,7 @@ static bool xDynTextsUUIDsUnique = true;

// max. Number of decimals:
static size_t decimals = 2; // number of decimals for floating-point values
static double MinLineLength = 0.015; //
static double MinLineLength = 0.025; //

static double scaleX = 1.0;
static double scaleY = 1.0;
Expand Down Expand Up @@ -323,7 +323,7 @@ int parseCommandline(int argc, char *argv[]) {
/******************************************************************************/
void PrintHelp(const std::string &s, const std::string &v){
std::stringstream ss;
ss << std::filesystem::path(s).filename();
ss << std::filesystem::path(s).filename().stem(); // without extension
std::string sExeName = ss.str();
std::cout << std::endl
<< sExeName << " version " << v << " needs arguments!" << std::endl
Expand Down Expand Up @@ -428,7 +428,14 @@ void ProcessElement(pugi::xml_node doc) {
NamesList Namen;
Namen.ReadFromPugiNode(doc.child("definition").child("names"));
Namen.WriteToPugiNode(doc.child("definition").child("names"));
// in einer Schleife die Elemente bearbeiten:
// wir lesen die Bauteil-Informationen, um nach Namen zu sortieren
ElementInfo EiList;
EiList.ReadFromPugiNode(doc.child("definition").child("elementInformations"));
EiList.WriteToPugiNode(doc.child("definition").child("elementInformations"));
// ein wenig Werbung in eigener Sache: Programmnamen hinzufügen:
AuthorInfo AInfo;
AInfo.UpdatePugiNode(doc.child("definition").child("informations"));
// in einer Schleife die Elemente bearbeiten - wir starten hier:
pugi::xml_node node = doc.child("definition").child("description").first_child();
// ... in a loop all parts
for (; node; node = node.next_sibling())
Expand Down

0 comments on commit 06edd3a

Please sign in to comment.