-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
195 lines (178 loc) · 7.15 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright (c) 2022-2025 plc-user
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
//
// QET_ElementScaler is a commandline-tool to scale
// QElectroTech-Graphics and/or converts them to
// Scalable Vector-Graphics.
//
// usage:
// see README.md
//
// +----------------------------------------------------------+
// | This software uses pugixml library (https://pugixml.org) |
// | pugixml is Copyright (C) 2006-2025 Arseny Kapoulkine. |
// +----------------------------------------------------------+
//
// own header(s):
#include "main.h"
// =============================================================================
// implementation
// =============================================================================
int main(int argc, char **argv)
{
// read and check the Commandline-Parameters
int iRetVal = parseCommandline(argc, argv);
if (_DEBUG_) std::cerr << "iRetVal: " << iRetVal << std::endl;
if (xPrintHelp == true) {
PrintHelp(argv[0], sVersion);
return 0;
}
if (xStopWithError == true) {
std::cerr << "check options and try again!" << std::endl;
return -1;
}
// load Element from stdin or XML-File
pugi::xml_document doc;
pugi::xml_parse_result result;
if (xReadFromStdIn == true){
result = doc.load(std::cin);
} else {
if ((ElementFile != "") && (std::filesystem::exists(ElementFile))) {
//ElementFile = argv[1];
result = doc.load_file(ElementFile.c_str());
} else
if ((argc>1)&&(std::filesystem::exists(argv[argc-1]))) {
ElementFile = argv[argc-1];
result = doc.load_file(ElementFile.c_str());
} else
{
PrintHelp(argv[0], sVersion);
return -1;
}
}
// check the result of "doc.load"-Function
if (!result){
// try to read the corrupt part and output to stderr
if (xReadFromStdIn == true) {
std::cerr << "Data could not be loaded: " << result.description() << std::endl;
std::cerr << "Check data up to byte-offset: " << result.offset << "\n";
} else {
std::cerr << "File \"" << ElementFile << "\" could not be loaded: " << result.description() << std::endl;
std::cerr << "Check file up to byte-offset: " << result.offset << " -- content partly shown here:\n";
std::cerr << "(...)" << ReadPieceOfFile(ElementFile, result.offset, (get_terminal_width()-10)) << "(...)\n\n";
}
return -1;
} else {
if (_DEBUG_) std::cerr << "Element-Data loaded successfully.\n";
}
// xml-file was successfully loaded, let's check, what kind of data we have...
if (doc.child("definition").child("description")) {
xIsElmtFile = true;
} else if (doc.child("qet-directory").child("names")) {
xIsDirFile = true;
xCreateSVG = false; // cannot create SVG from directory-file
xCreateELMT = true; // use the same xml-output-function for "element" and "qet_directory"
} else if (doc.child("titleblocktemplate")) {
std::cerr << "QElectroTech - titleblock-templates are not supported by QET_ElementScaler!\n";
return -2;
} else if (doc.child("project").child("diagram") && doc.child("project").child("collection")) {
std::cerr << "QElectroTech - project-files are not supported by QET_ElementScaler!\n";
return -2;
} else {
// no file-format we can handle here -> QUIT with message
std::cerr << "cannot handle \"" << ElementFile << "\": wrong file-content! " << std::endl;
return -2;
}
// build the filename for the scaled element:
ElementFileScaled = ElementFile;
if (xOverwriteOriginal == true){
std::cerr << "will overwrite original file!" << std::endl;
} else {
if (xPrintToStdOut == false) {
if (xIsElmtFile == true) {
ElementFileScaled.insert(ElementFileScaled.length()-5, ".SCALED");
} else {
ElementFileScaled += ".SCALED";
}
}
}
if (_DEBUG_) std::cerr << ElementFileScaled << std::endl;
// Process "qet_directory"
if (xIsDirFile) {
ProcessDirFile(doc);
}
// Process the Element-file: scale, flip, etc...
if (xIsElmtFile) {
ProcessElement(doc);
}
if (xCreateSVG == true) {
// SVG-Daten erstellen
std::string s = ToSVG(doc);
if (xPrintToStdOut == true) {
// zur Standard-Ausgabe:
std::cout << s << "\n\n";
} else {
// Dateinamen erstellen und SVG speichern
SVGFile = ElementFile+".svg";
std::ofstream out;
out.open(SVGFile, std::ios::out);
out << s << "\n";
out.close();
}
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;
std::cout << sXML << "\n";
if (_DEBUG_) std::cerr << "XML auf stdout ------------------------------------------------------" << std::endl;
} else {
// 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 {
if (_DEBUG_) std::cerr << "file \"" << ElementFileScaled << "\" saved successfully!" << std::endl;
}
outFile.close();
}
}
return 0;
}