1 /***************************************************************************
2 converter.cpp - c++ example program for signing a log
3 -------------------
4 begin : Sun Dec 15 2002
5 copyright : (C) 2002 by ARRL
6 author : Jon Bloom
7 email : jbloom@arrl.org
8 revision : $Id$
9 ***************************************************************************/
10
11 #ifdef HAVE_CONFIG_H
12 #include "sysconfig.h"
13 #endif
14
15 #include <iostream>
16 #include <fstream>
17 #include <cstdlib>
18 #include <unistd.h>
19 #ifdef HAVE_GETOPT_H
20 #include <getopt.h>
21 #endif
22 #include "tqsllib.h"
23 #include "tqslerrno.h"
24 #include "tqslconvert.h"
25 #include "tqslexc.h"
26
27 using std::cerr;
28 using std::endl;
29 using std::ofstream;
30
usage()31 int usage() {
32 cerr << "Usage: converter [-ac] station-location infile [outfile]\n";
33 exit(EXIT_FAILURE);
34 }
35
36 int
main(int argc,char * argv[])37 main(int argc, char *argv[]) {
38 enum { UNKNOWN, CABRILLO, ADIF } type = UNKNOWN;
39 int opt;
40
41 while ((opt = getopt(argc, argv, "ca")) != -1) {
42 switch (opt) {
43 case 'c':
44 type = CABRILLO;
45 break;
46 case 'a':
47 type = ADIF;
48 break;
49 default:
50 usage();
51 }
52 }
53 if (argc - optind < 2)
54 usage();
55 tQSL_Converter conv = 0;
56 try {
57 if (tqsl_init())
58 throw tqslexc();
59 // Get the specified station location data
60 tQSL_Location loc;
61 if (tqsl_getStationLocation(&loc, argv[optind++]))
62 throw tqslexc();
63 // Get the callsign and DXCC entity to use
64 char call[256];
65 int dxcc;
66 if (tqsl_getLocationCallSign(loc, call, sizeof call))
67 throw tqslexc();
68 if (tqsl_getLocationDXCCEntity(loc, &dxcc))
69 throw tqslexc();
70 // Get a list of available signing certificates
71 tQSL_Cert *certs;
72 int ncerts;
73 if (tqsl_selectCertificates(&certs, &ncerts, call, dxcc, 0, 0, 1))
74 throw tqslexc();
75 if (ncerts < 1)
76 throw myexc(string("No certificates available for ") + call);
77 int stat = 1;
78 if (type == UNKNOWN || type == CABRILLO) {
79 if ((stat = tqsl_beginCabrilloConverter(&conv, argv[optind], certs, ncerts, loc)) != 0
80 && type == CABRILLO)
81 throw tqslexc();
82 }
83 if (stat) {
84 if (tqsl_beginADIFConverter(&conv, argv[optind], certs, ncerts, loc))
85 throw tqslexc();
86 }
87 tqsl_setConverterAllowDuplicates(conv, false);
88 optind++;
89 const char *ofile = (optind < argc) ? argv[optind] : "converted.tq7";
90 ofstream out;
91 out.open(ofile, std::ios::out|std::ios::trunc|std::ios::binary);
92 if (!out.is_open())
93 throw myexc(string("Unable to open ") + ofile);
94 bool haveout = false;
95 do {
96 const char *gabbi = tqsl_getConverterGABBI(conv);
97 if (gabbi) {
98 haveout = true;
99 out << gabbi;
100 continue;
101 }
102 if (tQSL_Error == TQSL_SIGNINIT_ERROR) {
103 tQSL_Cert cert;
104 if (tqsl_getConverterCert(conv, &cert))
105 throw tqslexc();
106 if (tqsl_beginSigning(cert, 0, 0, 0))
107 throw tqslexc();
108 continue;
109 }
110 if (tQSL_Error == TQSL_DUPLICATE_QSO)
111 continue;
112 break;
113 } while (1);
114 out.close();
115 if (tQSL_Error != TQSL_NO_ERROR)
116 throw tqslexc();
117 else if (!haveout)
118 cerr << "Empty log file" << endl;
119 } catch(exception& x) {
120 char buf[40] = "";
121 int lineno;
122 if (conv && !tqsl_getConverterLine(conv, &lineno)) // && lineno > 0)
123 snprintf(buf, sizeof buf, " on line %d", lineno);
124 cerr << "Aborted: " << x.what() << buf << endl;
125 tqsl_converterRollBack(conv);
126 return EXIT_FAILURE;
127 }
128 tqsl_converterCommit(conv);
129 return EXIT_SUCCESS;
130 }
131