-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLReader.cpp
More file actions
203 lines (187 loc) · 7.35 KB
/
XMLReader.cpp
File metadata and controls
203 lines (187 loc) · 7.35 KB
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
196
197
198
199
200
201
202
203
/***************************************************************************
* Copyright (C) 2024 by VikMorroHun *
* luke2135@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 3 of the License. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses>. *
***************************************************************************/
#include <XMLReader.h>
#include <QDebug>
#include <QObject>
/****************************************************************************************************
* An Element is something that does or could have attributes of its own or contain other elements. *
* An Attribute is something that is self-contained, i.e., a color, an ID, a name. *
* An element can contain: text, attributes, other elements, or a mix of the above. *
* Metadata (data about data) should be stored as attributes, and the data itself should be stored *
* as elements. https://www.w3schools.com/xml/xml_attributes.asp *
****************************************************************************************************/
XMLReader::XMLReader()
{
/*if ( document == NULL )
{
str = "XMLReader constructor without document pointer. Exiting.";
//emit XMLSendStringBasic( str ); // emit inside constructor has no effect
return;
} */
//pWorkerShared = &Shared;
}
// https://stackoverflow.com/questions/49188597/how-to-parse-an-unknown-xml-in-qt-and-get-all-the-attributes-in-it
XMLReader::~XMLReader()
{
}
//+------------------------------------------------------------------+
//| Read XML file into QStringList - test mode |
//| INPUT: fileName, shared data pointer |
//| OUTPUT: QStringList |
//| REMARK: none |
//+------------------------------------------------------------------+
void XMLReader::ReadXMLSLTest( QString fileName, strucShared * pShared )
{
qint32 i;
QString elementName, elementText, strIN, temp;
QFile file(fileName);
//QMessageBox msgBox;
if ( !file.open( QIODevice::ReadOnly ) )
{
str = QObject::tr("Opening file %1 failed.").arg(fileName);
emit XMLSendStringBasicSignal( str );
//msgBox.setText( QObject::tr("Opening file %1 failed.").arg(fileName) );
//msgBox.exec();
return;
}
str = "ReadXMLSL test - file opened. Element names:\n";
emit XMLSendStringBasicSignal( str );
pShared->sList.clear();str.clear();
pTsXML = new QTextStream( &file );
while ( !pTsXML->atEnd() )
{
strIN = pTsXML->readLine();//strIN = strIN.trimmed(); // breaks multiple line string
if ( strIN.endsWith( ">" ) )
{
str += strIN;
pShared->sList.append( str );str.clear();
}
else
{
str += strIN;str += "\n";
continue;
}
}
file.close();delete pTsXML;
if ( pShared->sList.size() > 0 )
{
for ( i = 0; i < pShared->sList.size(); i++ )
{
temp = pShared->sList.at( i );
elementName = this->getsElementID( temp );
elementText = this->getsElementText( temp );
if ( elementName.size() && elementText.size() ) // elementName != "", elementText == "" -> node
{
str = "element name: " + elementName + ", element text: " + elementText;
emit XMLSendStringBasicSignal( str ); //pShared->sList.at(i)
}
}
}
}
//+------------------------------------------------------------------+
//| Read XML file into QStringList |
//| INPUT: fileName, shared data pointer, emit string? |
//| OUTPUT: QStringList |
//| REMARK: none |
//+------------------------------------------------------------------+
void XMLReader::ReadXMLSL( QString fileName, strucShared * pShared, bool bEchoMode = true )
{
QString strIN;
QFile file(fileName);
pShared->sList.clear();
if ( !file.open( QIODevice::ReadOnly ) )
{
str = QObject::tr("Opening file %1 failed.").arg(fileName);
emit XMLSendStringBasicSignal( str );
return;
}
str.clear();
pTsXML = new QTextStream( &file );
while ( !pTsXML->atEnd() )
{
strIN = pTsXML->readLine();//strIN = strIN.trimmed(); // breaks multiple line string
if ( strIN.endsWith( ">" ) )
{
str += strIN;
pShared->sList.append( str );str.clear();
}
else
{
str += strIN;str += "\n";
continue;
}
}
file.close();delete pTsXML;
str = QObject::tr("File containing Steam depot information %1 loaded.").arg(fileName);
if ( bEchoMode )
emit XMLSendStringBasicSignal( str );
}
void XMLReader::TestMsgSender()
{
emit XMLSendStringBasicSignal( "KUKUCS" );
}
//+------------------------------------------------------------------+
//| Get element ID |
//| INPUT: string |
//| OUTPUT: element ID, or empty string if error occured |
//| REMARK: none |
//+------------------------------------------------------------------+
QString XMLReader::getsElementID( QString s )
{
qint32 i, j;
i = s.indexOf( "<" );
j = s.indexOf( ">", i + 1 );
if ( i == -1 || j == -1 )
return "";
s.truncate( j );
i = s.indexOf( "<" );
s.remove( 0, i + 1 );
if ( s.endsWith("/") ) // empty element
s.truncate( s.size() - 1 );
return s;
}
//+------------------------------------------------------------------+
//| Check if empty element |
//| INPUT: string |
//| OUTPUT: true - element is empty, false - element contains data |
//| REMARK: none |
//+------------------------------------------------------------------+
bool XMLReader::getbIsEmptyElement( QString * s )
{
bool bRet = false;
if ( s->endsWith("/>") )
bRet = true;
return bRet;
}
//+------------------------------------------------------------------+
//| Get element text |
//| INPUT: string |
//| OUTPUT: element text (or empty string if empty) |
//| REMARK: none |
//+------------------------------------------------------------------+
QString XMLReader::getsElementText( QString s )
{
qint32 i, j;
if ( this->getbIsEmptyElement( &s ) )
return "";
i = s.indexOf( "<" );
j = s.indexOf( ">", i + 1 );
s.remove(0, j + 1 );
i = s.indexOf( "<" );
s.truncate( i );
return s;
}