Browse Source

Add ADIF export stuff

master
Dashie der otter 4 years ago
parent
commit
8a5979355f
7 changed files with 214 additions and 5 deletions
  1. 116
    0
      adif_export.cpp
  2. 55
    0
      adif_export.h
  3. 1
    1
      adif_parse.h
  4. 1
    1
      adif_parser.cpp
  5. 6
    0
      common.h
  6. 2
    1
      crApDIF.pro
  7. 33
    2
      main.cpp

+ 116
- 0
adif_export.cpp View File

@@ -0,0 +1,116 @@
#include "adif_export.h"

/**
* @brief AdifExporter::AdifExporter
* @param fname Filename
*/
AdifExporter::AdifExporter(QString fname) {
// default values
fileValid = false;
filename = fname;
}

/**
* @brief AdifExporter constructor
*/
AdifExporter::~AdifExporter() {
// Cleaning stuff
if (debug)
qDebug() << "Closing exporter";
file->close();
}

/**
* @brief Return file validity, like the file have been successfully opened
* @return True if file is valid, else False
*/
bool AdifExporter::fileValidity() {
return fileValid;
}

/**
* @brief If error occured, here it is
* @return Last error in text format
*/
QString AdifExporter::lastError() {
return error;
}

/**
* @brief Open file and create stream handler
* @return True if ok, else False
*/
bool AdifExporter::validateFile() {
file = new QFile(filename);

if (file->open(QFile::WriteOnly | QFile::Truncate)) {
fileStream = new QTextStream(file);
fileValid = true;
} else {
fileValid = false;
error = "Cannot open file";
return false;
}

if (debug)
qDebug() << "Opened file";

return true;
}

void AdifExporter::addHeader(QString const name, QString const value) {
if (debug)
qDebug() << "Adding header" << name << value;
headers.insert(name, value);
}

void AdifExporter::addQso(QMap<QString, QString> values) {
qso.insert(qso.size() + 1, values);
}

QString AdifExporter::formatPseudoXml(QString const name, QString const value) {
QString a("<%1:%2>%3");
return a.arg(name).arg(value.size()).arg(value);
}

bool AdifExporter::exportToFile() {
if (debug)
qDebug() << "Exporting file";
// Add comments
*fileStream << "ADIF Export made with crApDIF\n";
*fileStream << "\n";

// Add headers
QMapIterator<QString, QString> iHeaders(headers);
while (iHeaders.hasNext()) {
iHeaders.next();
QString px = formatPseudoXml(iHeaders.key(), iHeaders.value());
*fileStream << px << "\n";
}
*fileStream << "<eoh>\n\n"; // End Of Header

// Add QSO
QMapIterator<int, QMap<QString, QString> > iQso(qso);
while (iQso.hasNext()) {
iQso.next();
QMapIterator<QString, QString> iQsoValues(iQso.value());
while (iQsoValues.hasNext()) {
iQsoValues.next();
QString px = formatPseudoXml(iQsoValues.key(), iQsoValues.value());
*fileStream << px << "\n";
}
*fileStream << "<eor>\n\n"; // End Of Record
}



return true;
}

/**
* @brief AdifExporter::version
* @return QString version
*/
QString AdifExporter::version() {
return QString("Exporter version %1, currently working with ADIF 3.0.4 only").arg(LIB_VERSION);
}

+ 55
- 0
adif_export.h View File

@@ -0,0 +1,55 @@
#ifndef ADIFEXPORT_H
#define ADIFEXPORT_H

#include <QObject>
#include <QString>
#include <QDebug>
#include <QStringList>
#include <QMap>
#include <QFile>
#include <QHash>
#include <QPair>
#include <QTextStream>

#include "common.h"

/**
* @brief The Adif class
*/
class AdifExporter
{
public:
bool debug;
AdifExporter(QString fname); // our constructor
~AdifExporter(); // bye bye

bool validateFile();
bool fileValidity();

void addHeader(QString const name, QString const value);
void addQso(QMap<QString, QString> values);

bool exportToFile();

// Want some error ?
QString lastError();

QString version();


private:
QString filename;
bool fileValid;

QMap<QString, QString> headers;
QMap<int, QMap<QString, QString> > qso;

QString error;
QFile *file;
QTextStream *fileStream;

QString formatPseudoXml(QString const name, QString const value);

};

#endif // ADIFEXPORT_H

+ 1
- 1
adif_parse.h View File

@@ -11,7 +11,7 @@
#include <QPair>
#include <QTextStream>

#define VERSION "0.1"
#include "common.h"

/**
* @brief The Adif class

+ 1
- 1
adif_parser.cpp View File

@@ -353,5 +353,5 @@ QMap<int, QMap<QString, QString> > AdifParser::getParsedQso() {
* @return QString version
*/
QString AdifParser::version() {
return QString("Parser version %1, currently working with ADIF 3.0.4 only").arg(VERSION);
return QString("Parser version %1, currently working with ADIF 3.0.4 only").arg(LIB_VERSION);
}

+ 6
- 0
common.h View File

@@ -0,0 +1,6 @@
#ifndef UNICORNS_RULES_THE_WORLD_H
#define UNICORNS_RULES_THE_WORLD_H

#define LIB_VERSION "0.1"

#endif // UNICORNS_RULES_THE_WORLD_H

+ 2
- 1
crApDIF.pro View File

@@ -13,5 +13,6 @@ SOURCES += main.cpp \

HEADERS += \
adif_parse.h \
adif_export.h
adif_export.h \
common.h


+ 33
- 2
main.cpp View File

@@ -6,13 +6,13 @@
#include <QMap>

#include "adif_parse.h"
#include "adif_export.h"

/**
* @brief parser
* @param filename
*/
void parser(QString const filename) {

int qerr;

AdifParser parser(filename);
@@ -84,7 +84,38 @@ void parser(QString const filename) {
* @param filename
*/
void exporter(QString const filename) {
(void)filename;
int qerr;

AdifExporter exp(filename);
exp.debug = true;

qDebug() << "lib version" << exp.version();

// Validate if file is OK, open it and create streamHandler
qerr = exp.validateFile();
if (!qerr) {
qDebug() << "File validation" << exp.lastError();
return;
}

// Add some headers
exp.addHeader("my_name", "kevin");
exp.addHeader("operator", "K3VIN");

// Create one QSO
QMap<QString, QString> qso;
qso.insert("call", "LZ3ZZ");
qso.insert("qso_date", "20151024");
qso.insert("time_on", "143500");
qso.insert("band", "15m");
qso.insert("freq", "21.299");
qso.insert("mode", "SSB");

// Add QSO
exp.addQso(qso);

// Export to file
exp.exportToFile();
}

/**

Loading…
Cancel
Save