1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2012 Torsten Rahn <tackat@kde.org>
4 //
5 
6 
7 #include <QApplication>
8 #include <QDataStream>
9 #include <QFile>
10 #include <QDebug>
11 #include <QXmlInputSource>
12 #include <QXmlSimpleReader>
13 #include "svgxmlhandler.h"
14 
parseSvg(const QString & svgFilename,QDataStream * out,const QString & path,int header)15 void parseSvg( const QString & svgFilename, QDataStream * out, const QString & path, int header ) {
16 
17     SVGXmlHandler     handler( out, path, header );
18     QFile             xmlFile( svgFilename );
19     QXmlInputSource   inputSource(&xmlFile);
20     QXmlSimpleReader  reader;
21 
22     reader.setContentHandler(&handler);
23     reader.parse( inputSource );
24 }
25 
main(int argc,char * argv[])26 int main(int argc, char *argv[])
27 {
28     QApplication  app(argc, argv);
29 
30     qDebug( " Syntax: pntreplace [-i pnt-sourcefile -o pnt-targetfile -s svg-replacementfile -id pntidNumber -path svgpathid] " );
31 
32     QString inputFilename("PDIFFBORDERS.PNT");
33     int inputIndex = app.arguments().indexOf("-i");
34     if (inputIndex > 0 && inputIndex + 1 < argc )
35         inputFilename = app.arguments().at( inputIndex + 1 );
36 
37     QString outputFilename("NEW.PNT");
38     int outputIndex = app.arguments().indexOf("-o");
39     if (outputIndex > 0 && outputIndex + 1 < argc )
40         outputFilename = app.arguments().at( outputIndex + 1 );
41 
42     QString svgFilename("output.svg");
43     int svgIndex = app.arguments().indexOf("-s");
44     if (svgIndex > 0 && svgIndex + 1 < argc )
45         svgFilename = app.arguments().at( svgIndex + 1 );
46 
47     QString path("id_path");
48     int pathIndex = app.arguments().indexOf("-path");
49     if (pathIndex > 0 && pathIndex + 1 < argc )
50         path = app.arguments().at( pathIndex + 1 );
51 
52     int delIndex = -1;
53     int idIndex = app.arguments().indexOf("-id");
54     if (idIndex > 0 && idIndex + 1 < argc )
55         delIndex = app.arguments().at( idIndex + 1 ).toInt();
56 
57 
58     qDebug() << "input filename:" << inputFilename;
59     qDebug() << "output filename:" << outputFilename;
60     qDebug() << "svg replacement filename:" << svgFilename;
61     qDebug() << "replace index:" << delIndex;
62     qDebug() << "replacement:" << path;
63 
64     // INPUT
65     QFile  file( inputFilename );
66 
67     if ( file.open( QIODevice::ReadOnly ) ) {
68         QDataStream stream( &file );  // read the data serialized from the file
69         stream.setByteOrder( QDataStream::LittleEndian );
70 
71         // OUTPUT
72         QFile data(outputFilename);
73 
74         if (data.open(QFile::WriteOnly | QFile::Truncate)) {
75             QDataStream out(&data);
76             out.setByteOrder( QDataStream::LittleEndian );
77 
78             short  header;
79             short  iLat;
80             short  iLon;
81 
82             bool skip = false;
83 
84             while( !stream.atEnd() ){
85                 stream >> header >> iLat >> iLon;
86                 if ( header == delIndex ) {
87                     parseSvg( svgFilename, &out, path, delIndex );
88                     skip = true;
89                 }
90                 else if ( header > 5 )
91                     skip = false;
92 
93                 if ( !skip )
94                     out << header << iLat << iLon;
95             }
96             data.close();
97         }
98         else {
99             qDebug() << "ERROR: Couldn't write output file to disc!";
100         }
101         file.close();
102     }
103     else {
104         qDebug() << "ERROR: Source file not found!";
105     }
106 
107     app.exit();
108 }
109