-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
378 lines (341 loc) · 8.8 KB
/
Copy pathmain.cpp
File metadata and controls
378 lines (341 loc) · 8.8 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// #include "file.h"
// #include "record.h"
// #include "logdb.h"
#include <iostream>
// #include <sqlite3.h>
#include <fstream>
#include <QtWidgets/QApplication>
#include "GLogApplication.h"
using namespace std;
// using namespace adif;
/*
enum MENU_ITEM {
LOAD = 1,
LIST,
SEARCHDATE,
SEARCHCALL,
INPUT,
EXPORT,
QUIT
};
*/
static const string db_name = "alllog.adi";
/*
// LogDB theDB;
adif::File theDB;
int menu(void);
void load_db(void);
void save_db(void);
void load_adif(void);
void list(void);
void searchDate(void);
void searchCall(void);
void input(void);
void export_csv(void);
*/
int main(int argc, char *argv[]) { // NOLINT
#ifdef HAS_WAYLAND_SUPPORT
const char *sessionType = std::getenv("XDG_SESSION_TYPE");
if (sessionType && std::string(sessionType) == "wayland") {
qDebug() << "Wayland session detected, setting platform to wayland;xcb";
qputenv("QT_QPA_PLATFORM", "wayland;xcb");
} else {
qDebug() << "X11 or unknown session detected, using default platform.";
}
#else
qDebug() << "Application built without Wayland support.";
#endif
QApplication app(argc, argv);
QApplication::setApplicationName("GLog");
GLogApplication window(true);
window.enableBackup();
window.show();
return QApplication::exec();
}
/*
void load_db(void)
{
ifstream in(db_name);
if ( in ) {
theDB.load(in);
}
in.close();
}
void save_db(void)
{
ofstream out(db_name);
theDB.save(out);
out.close();
}
int menu(void)
{
int ret = 0;
do {
cout << "1. Load (merge) an ADIF file" << endl;
cout << "2. List all records" << endl;
cout << "3. Search records by date" << endl;
cout << "4. Search records by call" << endl;
cout << "5. Input a record" << endl;
cout << "6. Export all to a CSV file" << endl;
cout << "7. Quit" << endl;
cout << "Please enter your choice: ";
if ( cin >> ret ) {
if ( ret < 1 || ret > 7 ) {
cout << "Invalid choice, please try again." << endl;
ret = 0;
}
} else {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input, please try again." << endl;
}
} while ( ret ==0 );
return ret;
}
void load_adif(void)
{
string filename;
cout << "Please enter the filename: ";
cin >> filename;
ifstream in(filename);
if ( !in ) {
cout << "Failed to open the file." << endl;
return;
}
File f;
f.load(in);
cout << "Number of records loaded: " << f.size() << endl;
// save records into the db
int cnt = theDB.merge(f, [](const Record& orig, const Record& cur) {
cout << "Record already exists." << endl;
cout << "Original: " << orig.to_string() << endl;
cout << "New : " << cur.to_string() << endl;
cout << "(S)kip, (R)eplace, (A)dd, (M)erge, (Q)uit:";
char choice;
cin >> choice;
switch (choice) {
case 'A':
case 'a':
return MERGE_ADD;
case 'S':
case 's':
return MERGE_SKIP;
case 'R':
case 'r':
return MERGE_REPLACE;
case 'M':
case 'm':
return MERGE_MERGE;
case 'Q':
case 'q':
return MERGE_QUIT;
default:
return MERGE_SKIP;
}
});
save_db();
cout << cnt << " record(s) has been added." << endl;
in.close();
}
void list(void)
{
theDB.print(cout);
cout << "Number of records in database: " << theDB.size() << endl;
}
#include <ctime>
void input(void)
{
static Record lastRecord;
Record rec;
char buffer[80];
cin.ignore(numeric_limits<streamsize>::max(), '\n');
string date;
time_t rawtime;
struct tm * timeinfo;
time (&rawtime);
timeinfo = gmtime(&rawtime);
strftime(buffer,sizeof(buffer),"%Y%m%d",timeinfo);
std::string str(buffer);
std::cout << "date(" <<str<< "):";
// cin >> date;
getline(cin, date);
if ( date.empty() ) {
date = str;
}
rec.set_field("qso_date", date);
string time;
strftime(buffer,sizeof(buffer),"%H%M%S",timeinfo);
str = buffer;
std::cout << "time(" <<str<< "):";
// cin >> time;
getline(cin, time);
if ( time.empty() ) {
time = str;
}
rec.set_field("time_on", time);
string freq;
while ( freq.empty() ) {
cout << "freq(" << lastRecord.get_field("freq") << ")";
// cin >> freq;
getline(cin, freq);
if ( freq.empty() ) {
freq = lastRecord.get_field("freq");
}
}
rec.set_field("freq", freq);
string mode;
while ( mode.empty() ) {
cout << "mode(" << lastRecord.get_field("mode") << ")";
// cin >> mode;
getline(cin, mode);
if ( mode.empty() ) {
mode = lastRecord.get_field("mode");
}
}
rec.set_field("mode", mode);
string call;
cout << "call:";
getline(cin, call);
rec.set_field("call", call);
string defrst = "59";
if ( mode == "CW" || mode == "cw" ) {
defrst = "599";
}
string rst_sent;
cout << "rst_sent(" << defrst << "):";
getline(cin, rst_sent);
if ( rst_sent.empty() ) {
rst_sent = defrst;
}
rec.set_field("rst_sent", rst_sent);
string rst_rcvd;
cout << "rst_rcvd(" << defrst << "):";
getline(cin, rst_rcvd);
if ( rst_rcvd.empty() ) {
rst_rcvd = defrst;
}
rec.set_field("rst_rcvd", rst_rcvd);
string name;
cout << "name:";
getline(cin, name);
if ( !name.empty() ) {
rec.set_field("name", name);
}
string qth;
cout << "qth:";
getline(cin, qth);
if ( !qth.empty() ) {
rec.set_field("qth", qth);
}
string comment;
cout << "comment:";
getline(cin, comment);
if ( !comment.empty() ) {
rec.set_field("comment", comment);
}
lastRecord = rec;
theDB.add(rec);
cout << rec.to_string() << endl;
cout << "one record added." << endl;
save_db();
}
void afterSearch(const vector<Record>& records);
void modify_record(Record& rec);
void searchDate(void)
{
string sdate;
cout << "The begin date(yyyymmdd):";
cin >> sdate;
string edate;
cout << "The end date(yyyymmdd):";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
getline(cin, edate);
if ( edate.empty() ) {
edate = sdate;
}
vector<Record> records = theDB.search_records_by_date(sdate, edate);
afterSearch(records);
}
void searchCall(void)
{
string call;
cout << "Please enter the callsign: ";
cin >> call;
vector<Record> records = theDB.search_records_by_call(call);
afterSearch(records);
}
void export_csv(void)
{
string filename;
cout << "Please enter the csv filename: ";
cin >> filename;
ofstream out(filename);
theDB.export_csv(out);
out.close();
}
void afterSearch(const vector<Record>& records)
{
int cnt = 1;
for ( auto rec : records ) {
cout << cnt << ": " << rec.to_string() << endl;
cnt += 1;
}
cout << "Number of records found: " << records.size() << endl;
if ( !records.empty() ) {
char choice='Q';
int num=1;
while ( true) {
cout << "(M)odify, (D)elete, (Q)uit:";
cin >> choice;
if ( choice == 'Q' || choice == 'q' ) {
break;
} else if ( choice != 'M' && choice != 'm' && choice != 'D' && choice != 'd' ) {
continue;
}
cout << "Enter the number of the record:";
cin >> num;
if ( num < 1 || num > records.size() ) {
cout << "Invalid number, please try again." << endl;
continue;
}
break;
}
Record r = records[num-1];
switch (choice) {
case 'M':
case 'm':
modify_record(r);
theDB.update(records[num-1], r);
save_db();
break;
case 'D':
case 'd':
theDB.remove(r);
save_db();
break;
}
}
}
void modify_record(Record& rec)
{
cout << "qso_date,time_on,freq,mode,call,rst_sent,rst_rcvd" << endl;
cout << rec.to_string() << endl;
cout << "qso_date and time_on can not be modified.\n";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
while (true) {
string key;
string value;
cout << "key:" << endl;
getline(cin, key);
if ( key.empty() ) {
break;
}
cout << "value:" << endl;
getline(cin, value);
if ( key != "qso_date" && key != "time_on" && !value.empty() ) {
rec.set_field(key, value);
}
}
cout << "Modified: " << rec.to_string() << endl;
}
*/