File indexing completed on 2024-04-21 14:52:05

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2012 Torsten Rahn <tackat@kde.org>
0004 //
0005 
0006 
0007 #include <QApplication>
0008 #include <QDataStream>
0009 #include <QFile>
0010 #include <QDebug>
0011 #include <QXmlInputSource>
0012 #include <QXmlSimpleReader>
0013 #include "svgxmlhandler.h"
0014 
0015 void parseSvg( const QString & svgFilename, QDataStream * out, const QString & path, int header ) {
0016 
0017     SVGXmlHandler     handler( out, path, header );
0018     QFile             xmlFile( svgFilename );
0019     QXmlInputSource   inputSource(&xmlFile);
0020     QXmlSimpleReader  reader;
0021 
0022     reader.setContentHandler(&handler);
0023     reader.parse( inputSource );
0024 }
0025 
0026 int main(int argc, char *argv[])
0027 {
0028     QApplication  app(argc, argv);
0029 
0030     qDebug( " Syntax: pntreplace [-i pnt-sourcefile -o pnt-targetfile -s svg-replacementfile -id pntidNumber -path svgpathid] " );
0031 
0032     QString inputFilename("PDIFFBORDERS.PNT");
0033     int inputIndex = app.arguments().indexOf("-i");
0034     if (inputIndex > 0 && inputIndex + 1 < argc )
0035         inputFilename = app.arguments().at( inputIndex + 1 );
0036 
0037     QString outputFilename("NEW.PNT");
0038     int outputIndex = app.arguments().indexOf("-o");
0039     if (outputIndex > 0 && outputIndex + 1 < argc )
0040         outputFilename = app.arguments().at( outputIndex + 1 );
0041 
0042     QString svgFilename("output.svg");
0043     int svgIndex = app.arguments().indexOf("-s");
0044     if (svgIndex > 0 && svgIndex + 1 < argc )
0045         svgFilename = app.arguments().at( svgIndex + 1 );
0046 
0047     QString path("id_path");
0048     int pathIndex = app.arguments().indexOf("-path");
0049     if (pathIndex > 0 && pathIndex + 1 < argc )
0050         path = app.arguments().at( pathIndex + 1 );
0051 
0052     int delIndex = -1;
0053     int idIndex = app.arguments().indexOf("-id");
0054     if (idIndex > 0 && idIndex + 1 < argc )
0055         delIndex = app.arguments().at( idIndex + 1 ).toInt();
0056 
0057 
0058     qDebug() << "input filename:" << inputFilename;
0059     qDebug() << "output filename:" << outputFilename;
0060     qDebug() << "svg replacement filename:" << svgFilename;
0061     qDebug() << "replace index:" << delIndex;
0062     qDebug() << "replacement:" << path;
0063 
0064     // INPUT
0065     QFile  file( inputFilename );
0066 
0067     if ( file.open( QIODevice::ReadOnly ) ) {
0068         QDataStream stream( &file );  // read the data serialized from the file
0069         stream.setByteOrder( QDataStream::LittleEndian );
0070 
0071         // OUTPUT
0072         QFile data(outputFilename);
0073 
0074         if (data.open(QFile::WriteOnly | QFile::Truncate)) {
0075             QDataStream out(&data);
0076             out.setByteOrder( QDataStream::LittleEndian );
0077 
0078             short  header;
0079             short  iLat;
0080             short  iLon;
0081 
0082             bool skip = false;
0083 
0084             while( !stream.atEnd() ){
0085                 stream >> header >> iLat >> iLon;
0086                 if ( header == delIndex ) {
0087                     parseSvg( svgFilename, &out, path, delIndex );
0088                     skip = true;
0089                 }
0090                 else if ( header > 5 )
0091                     skip = false;
0092 
0093                 if ( !skip )
0094                     out << header << iLat << iLon;
0095             }
0096             data.close();
0097         }
0098         else {
0099             qDebug() << "ERROR: Couldn't write output file to disc!";
0100         }
0101         file.close();
0102     }
0103     else {
0104         qDebug() << "ERROR: Source file not found!";
0105     }
0106 
0107     app.exit();
0108 }