File: converter.cpp

package info (click to toggle)
tqsllib 2.0-8
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,460 kB
  • ctags: 1,081
  • sloc: sh: 8,309; cpp: 7,980; xml: 4,068; makefile: 102
file content (123 lines) | stat: -rw-r--r-- 3,266 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
                          converter.cpp  -  description
                             -------------------
    begin                : Sun Dec 15 2002
    copyright            : (C) 2002 by ARRL
    author               : Jon Bloom
    email                : jbloom@arrl.org
    revision             : $Id: converter.cpp,v 1.2 2003/08/18 17:06:33 ke3z Exp $
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "sysconfig.h"
#endif

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <unistd.h>
#ifdef HAVE_GETOPT_H
	#include <getopt.h>
#endif
#include "tqsllib.h"
#include "tqslerrno.h"
#include "tqslconvert.h"
#include "tqslexc.h"

using namespace std;

int usage() {
	cerr << "Usage: converter [-ac] station-location infile [outfile]\n";
	exit(EXIT_FAILURE);
}

int
main(int argc, char *argv[]) {
	enum { UNKNOWN, CABRILLO, ADIF } type = UNKNOWN;
	int opt;

	while ((opt = getopt(argc, argv, "ca")) != -1) {
		switch (opt) {
			case 'c':
				type = CABRILLO;
				break;
			case 'a':
				type = ADIF;
				break;
			default:
				usage();
		}
	}
	if (argc - optind < 2)
		usage();
	tQSL_Converter conv = 0;
	try {
		if (tqsl_init())
			throw tqslexc();
		// Get the specified station location data
		tQSL_Location loc;
		if (tqsl_getStationLocation(&loc, argv[optind++]))
			throw tqslexc();
		// Get the callsign and DXCC entity to use
		char call[256];
		int dxcc;
		if (tqsl_getLocationCallSign(loc, call, sizeof call))
			throw tqslexc();
		if (tqsl_getLocationDXCCEntity(loc, &dxcc))
			throw tqslexc();
		// Get a list of available signing certificates
		tQSL_Cert *certs;
		int ncerts;
		if (tqsl_selectCertificates(&certs, &ncerts, call, dxcc, 0, 0, 1))
			throw tqslexc();
		if (ncerts < 1)
			throw myexc(string("No certificates available for ") + call);
		int stat = 1;
		if (type == UNKNOWN || type == CABRILLO) {
			if ((stat = tqsl_beginCabrilloConverter(&conv, argv[optind], certs, ncerts, loc)) != 0
				&& type == CABRILLO)
				throw tqslexc();
		}
		if (stat) {
			if (tqsl_beginADIFConverter(&conv, argv[optind], certs, ncerts, loc))
				throw tqslexc();
		}
		optind++;
		const char *ofile = (optind < argc) ? argv[optind] : "converted.tq7";
		ofstream out;
		out.open(ofile, ios::out|ios::trunc|ios::binary);
		if (!out.is_open())
			throw myexc(string("Unable to open ") + ofile);
		bool haveout = false;
		do {
			const char *gabbi = tqsl_getConverterGABBI(conv);
			if (gabbi) {
				haveout = true;
				out << gabbi;
				continue;
			}
   			if (tQSL_Error == TQSL_SIGNINIT_ERROR) {
   				tQSL_Cert cert;
   				if (tqsl_getConverterCert(conv, &cert))
					throw tqslexc();
   				if (tqsl_beginSigning(cert, 0, 0, 0))
					throw tqslexc();
   				continue;
   			}
   			break;
		} while (1);
		out.close();
		if (tQSL_Error != TQSL_NO_ERROR)
			throw tqslexc();
		else if (!haveout)
			cerr << "Empty log file" << endl;
	} catch (exception& x) {
		char buf[40] = "";
		int lineno;
		if (conv && !tqsl_getConverterLine(conv, &lineno)) // && lineno > 0)
			sprintf(buf, " on line %d", lineno);
		cerr << "Aborted: " << x.what() << buf << endl;
		return EXIT_FAILURE;
	}
	return EXIT_SUCCESS;
}