-
Notifications
You must be signed in to change notification settings - Fork 32
/
log_utils.c
194 lines (149 loc) · 4.55 KB
/
log_utils.c
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
/*
* Tlf - contest logging program for amateur radio operators
* Copyright (C) 2019-22 Thomas Beierlein <dl1jbe@darc.de>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* ------------------------------------------------------------------------
* util functions to work with lines from log
*
---------------------------------------------------------------------------*/
#include <ctype.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "bands.h"
#include "get_time.h"
#include "setcontest.h"
#include "tlf.h"
/* for the following code we assume that we have well formatted log lines,
* which has to be checked separately if needed */
/** check if logline is only a comment */
bool log_is_comment(const char *buffer) {
if (buffer[0] != ';')
return 0;
else
return 1;
}
/** read bandindex from logline */
int log_get_band(const char *logline) {
int nr = 0;
char band[4];
g_strlcpy(band, logline, 4);
nr = atoi(band);
return bandnr2index(nr);
}
/** read mode from logline
* -1 if no recognized mode */
int log_get_mode(const char *logline) {
if (strncasecmp("CW ", logline + 3, 3) == 0) {
return CWMODE;
}
if (strncasecmp("SSB", logline + 3, 3) == 0) {
return SSBMODE;
}
if (strncasecmp("DIG", logline + 3, 3) == 0) {
return DIGIMODE;
}
return -1;
}
/** read points from logline */
int log_get_points(const char *logline) {
char tmpbuf[3];
g_strlcpy(tmpbuf, logline + 76, 3);
return atoi(tmpbuf);
}
struct qso_t *parse_qso(char *buffer) {
char *tmp;
char *sp;
struct qso_t *ptr;
struct tm date_n_time;
ptr = g_malloc0(sizeof(struct qso_t));
/* remember whole line */
ptr->logline = g_strdup(buffer);
ptr->qsots = 0;
ptr->is_comment = log_is_comment(buffer);
if (ptr->is_comment) {
return ptr;
}
/* split buffer into parts for linedata_t record and parse
* them accordingly */
tmp = strtok_r(buffer, " \t", &sp);
/* band */
ptr->band = atoi(tmp);
ptr->bandindex = bandnr2index(ptr->band);
/* mode */
if (strcasestr(tmp, "CW"))
ptr->mode = CWMODE;
else if (strcasestr(tmp, "SSB"))
ptr->mode = SSBMODE;
else
ptr->mode = DIGIMODE;
/* date & time */
memset(&date_n_time, 0, sizeof(struct tm));
strptime(strtok_r(NULL, " \t", &sp), DATE_FORMAT, &date_n_time);
strptime(strtok_r(NULL, " \t", &sp), TIME_FORMAT, &date_n_time);
ptr->timestamp = timegm(&date_n_time);
ptr->year = date_n_time.tm_year + 1900; /* convert to
1968..2067 */
ptr->month = date_n_time.tm_mon + 1; /* tm_mon = 0..11 */
ptr->day = date_n_time.tm_mday;
ptr->hour = date_n_time.tm_hour;
ptr->min = date_n_time.tm_min;
/* qso number */
ptr->qso_nr = atoi(strtok_r(NULL, " \t", &sp));
/* his call */
ptr->call = g_strdup(strtok_r(NULL, " \t", &sp));
/* RST send and received */
ptr->rst_s = atoi(strtok_r(NULL, " \t", &sp));
ptr->rst_r = atoi(strtok_r(NULL, " \t", &sp));
/* comment (exchange) */
ptr->comment = g_strndup(buffer + 54, contest->exchange_width);
/* tx */
ptr->tx = (buffer[79] == '*') ? 1 : 0;
/* frequency (kHz) */
ptr->freq = atof(buffer + 80) * 1000.0;
if (freq2bandindex(ptr->freq) == BANDINDEX_OOB) {
ptr->freq = 0.;
}
return ptr;
}
/** free qso record pointed to by ptr */
void free_qso(struct qso_t *ptr) {
if (ptr != NULL) {
g_free(ptr->comment);
g_free(ptr->logline);
g_free(ptr->call);
g_free(ptr->mult1_value);
g_free(ptr->callupdate);
g_free(ptr->normalized_comment);
g_free(ptr->section);
g_free(ptr);
}
}
static void qso_free(gpointer data) {
free_qso((struct qso_t *) data);
}
void free_qso_array() {
if (qso_array != NULL) {
g_ptr_array_free(qso_array, TRUE);
qso_array = NULL;
}
}
void init_qso_array() {
free_qso_array();
qso_array = g_ptr_array_new_with_free_func(qso_free);
}