-
Notifications
You must be signed in to change notification settings - Fork 9
/
parse_line.go
409 lines (368 loc) · 10.4 KB
/
parse_line.go
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package fleprocess
/*
Copyright © 2020 Jean-Marc Meessen, ON4KJM <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// LogLine is used to store all the data of a single log line
type LogLine struct {
Date string
MyCall string
Operator string
MyWWFF string
MyPOTA string
MySOTA string
MyPota string
MySota string
MyGrid string
QslMsgFromHeader string
Nickname string
Mode string
ModeType string
Band string
BandLowerLimit float64
BandUpperLimit float64
Frequency string
Time string
ActualTime string //time actually recorded in FLE
Call string
Comment string
QSLmsg string
OMname string
GridLoc string
RSTsent string
RSTrcvd string
WWFF string
POTA string
SOTA string
}
var regexpIsFullTime = regexp.MustCompile(`^[0-2]{1}[0-9]{3}$`)
var regexpIsTimePart = regexp.MustCompile(`^[0-5]{1}[0-9]{1}$|^[1-9]{1}$`)
var regexpIsOMname = regexp.MustCompile(`^@`)
var regexpIsGridLoc = regexp.MustCompile(`^#`)
var regexpIsRst = regexp.MustCompile(`^[\d]{1,3}$`)
var regexpIsFreq = regexp.MustCompile(`^[\d]+\.[\d]+$`)
var regexpIsSotaKeyWord = regexp.MustCompile(`(?i)^sota$`)
var regexpIsWwffKeyWord = regexp.MustCompile(`(?i)^wwff$`)
var regexpIsPotaKeyWord = regexp.MustCompile(`(?i)^pota$`)
var regexpDatePattern = regexp.MustCompile(`^(\d{2}|\d{4})[-/ .]\d{1,2}[-/ .]\d{1,2}$`)
var regexpIsDateKeyWord = regexp.MustCompile(`(?i)^date$`)
var regexpDayIncrementPattern = regexp.MustCompile(`^\+*$`)
var regexpIsDayKeyword = regexp.MustCompile(`(?i)^day$`)
var regexpKhzPartOfQrg = regexp.MustCompile(`\.\d+`)
// ParseLine cuts a FLE line into useful bits
func ParseLine(inputStr string, previousLine LogLine) (logLine LogLine, errorMsg string) {
//TODO: input null protection?
//Flag telling that we are processing data to the right of the callsign
isRightOfCall := false
//Flag used to know if we are parsing the Sent RST (first) or received RST (second)
haveSentRST := false
//TODO: Make something more intelligent
//TODO: What happens if we have partial lines
previousLine.Call = ""
previousLine.RSTsent = ""
previousLine.RSTrcvd = ""
previousLine.SOTA = ""
previousLine.POTA = ""
previousLine.WWFF = ""
previousLine.OMname = ""
previousLine.GridLoc = ""
previousLine.Comment = ""
previousLine.ActualTime = ""
logLine = previousLine
//TODO: what happens when we have <> or when there are multiple comments
//TODO: Refactor this! it is ugly
comment, inputStr := getBraketedData(inputStr, COMMENT)
if comment != "" {
logLine.Comment = comment
}
QSLmsg, inputStr := getBraketedData(inputStr, QSL)
if QSLmsg != "" {
logLine.QSLmsg = QSLmsg
}
elements := strings.Fields(inputStr)
for _, element := range elements {
// Is it a mode?
if lookupMode(strings.ToUpper(element)) {
logLine.Mode = strings.ToUpper(element)
//TODO: improve this: what if the band is at the end of the line
// Set the default RST depending of the mode
if (logLine.RSTsent == "") || (logLine.RSTrcvd == "") {
// get default RST and Mode category
modeType, defaultReport := getDefaultReport(logLine.Mode)
logLine.ModeType = modeType
logLine.RSTsent = defaultReport
logLine.RSTrcvd = defaultReport
} else {
errorMsg = errorMsg + "Double definitiion of RST"
}
continue
}
//Date?
if regexpDatePattern.MatchString(element) {
//We probably have a date, let's normalize it
errorTxt := ""
normalizedDate := ""
normalizedDate, errorTxt = NormalizeDate(element)
if len(errorTxt) != 0 {
logLine.Date = normalizedDate
errorMsg = errorMsg + fmt.Sprintf("Invalid Date: %s (%s)", element, errorTxt)
} else {
logLine.Date, errorTxt = ValidateDate(normalizedDate)
if len(errorTxt) != 0 {
errorMsg = errorMsg + fmt.Sprintf("Error %s", errorTxt)
}
}
continue
}
// The date keyword is not really useful, skip it
if regexpIsDateKeyWord.MatchString(element) {
continue
}
//Skip the "day" keyword
if regexpIsDayKeyword.MatchString(element) {
continue
}
//Scan the + part
if regexpDayIncrementPattern.MatchString(element) {
increment := len(element)
newDate, dateError := IncrementDate(logLine.Date, increment)
if dateError != "" {
errorMsg = errorMsg + dateError
}
logLine.Date = newDate
continue
}
// Is it a band?
isBandElement, bandLowerLimit, bandUpperLimit, _ := IsBand(element)
if isBandElement {
logLine.Band = strings.ToLower(element)
logLine.BandLowerLimit = bandLowerLimit
logLine.BandUpperLimit = bandUpperLimit
//As a new band is defined, we reset the stored frequency (from previous lines)
// This assumes that the band is defined before frequency
logLine.Frequency = ""
continue
}
// Is it a Frequency?
if regexpIsFreq.MatchString(element) {
khzPart := regexpKhzPartOfQrg.FindStringSubmatch(element)
var qrg float64
qrg, _ = strconv.ParseFloat(element, 32)
if (logLine.BandLowerLimit != 0.0) && (logLine.BandUpperLimit != 0.0) {
if (qrg >= logLine.BandLowerLimit) && (qrg <= logLine.BandUpperLimit) {
//Increase precision to half Khz if data is available
if len(khzPart[0]) > 4 {
//The "." is part of the returned string
logLine.Frequency = fmt.Sprintf("%.4f", qrg)
} else {
logLine.Frequency = fmt.Sprintf("%.3f", qrg)
}
} else {
logLine.Frequency = ""
errorMsg = errorMsg + "Frequency [" + element + "] is invalid for " + logLine.Band + " band."
}
} else {
errorMsg = errorMsg + "Unable to load frequency [" + element + "]: no band defined for that frequency."
}
continue
}
// Is it a call sign ?
//FIXME: (don't remember what)
if validCallRegexp.MatchString(strings.ToUpper(element)) {
//If it starts with "#",it is a grid definition and not a call
if element[0] != '#' {
callErrorMsg := ""
logLine.Call, callErrorMsg = ValidateCall(element)
errorMsg = errorMsg + callErrorMsg
isRightOfCall = true
continue
}
}
// Is it a "full" time ?
if !isRightOfCall {
if regexpIsFullTime.MatchString(element) {
logLine.Time = element
logLine.ActualTime = element
continue
}
// Is it a partial time ?
if regexpIsTimePart.MatchString(element) {
if logLine.Time == "" {
logLine.Time = element
logLine.ActualTime = element
} else {
goodPart := logLine.Time[:len(logLine.Time)-len(element)]
logLine.Time = goodPart + element
logLine.ActualTime = goodPart + element
}
continue
}
}
// Is it the OM's name (starting with "@")
if regexpIsOMname.MatchString(element) {
logLine.OMname = strings.TrimLeft(element, "@")
continue
}
// Is it the Grid Locator (starting with "#")
if regexpIsGridLoc.MatchString(element) {
grid := strings.TrimLeft(element, "#")
cleanGrid, callErrorMsg := ValidateGridLocator(grid)
logLine.GridLoc = cleanGrid
errorMsg = errorMsg + callErrorMsg
continue
}
if isRightOfCall {
//This is probably a RST
if regexpIsRst.MatchString(element) {
workRST := ""
switch len(element) {
case 1:
if logLine.ModeType == "CW" {
workRST = "5" + element + "9"
} else {
if logLine.ModeType == "PHONE" {
workRST = "5" + element
}
}
case 2:
if logLine.ModeType == "CW" {
workRST = element + "9"
} else {
if logLine.ModeType == "PHONE" {
workRST = element
}
}
case 3:
if logLine.ModeType == "CW" {
workRST = element
} else {
workRST = "*" + element
errorMsg = errorMsg + "Invalid report [" + element + "] for " + logLine.ModeType + " mode."
}
}
if haveSentRST {
logLine.RSTrcvd = workRST
} else {
logLine.RSTsent = workRST
haveSentRST = true
}
continue
}
// If the "wwff" keyword is used, skip it
if regexpIsWwffKeyWord.MatchString(element) {
// this keyword is not requiered anymore with FLE 3 and doesn't add any value
continue
}
// Is it a "WWFF to WWFF" reference?
workRef, wwffErr := ValidateWwff(element)
if wwffErr == "" {
logLine.WWFF = workRef
continue
}
// If the "pota" keyword is used, skip it
if regexpIsPotaKeyWord.MatchString(element) {
// this keyword is not requiered anymore with FLE 3 and doesn't add any value
continue
}
// Is it a "POTA to POTA" reference?
workRef, potaErr := ValidatePota(element)
if potaErr == "" {
logLine.POTA = workRef
continue
}
// If the "sota" keyword is used, skip it
if regexpIsSotaKeyWord.MatchString(element) {
// this keyword is not requiered anymore with FLE 3 and doesn't add any value
continue
}
// Is it a Summit to Summit (sota) reference?
workRef, sotaErr := ValidateSota(element)
if sotaErr == "" {
logLine.SOTA = workRef
continue
}
}
//If we come here, we could not make sense of what we found
errorMsg = errorMsg + "Unable to make sense of [" + element + "]. "
}
//If no report is present, let's fill it with mode default
if logLine.RSTsent == "" {
_, logLine.RSTsent = getDefaultReport(logLine.Mode)
}
if logLine.RSTrcvd == "" {
_, logLine.RSTrcvd = getDefaultReport(logLine.Mode)
}
//For debug purposes
//fmt.Println("\n", SprintLogRecord(logLine))
return logLine, errorMsg
}
func lookupMode(lookup string) bool {
switch lookup {
case
"CW",
"SSB",
"AM",
"FM",
"RTTY",
"FT8",
"PSK",
"JT65",
"JT9",
"FT4",
"JS8",
"ARDOP",
"ATV",
"C4FM",
"CHIP",
"CLO",
"CONTESTI",
"DIGITALVOICE",
"DOMINO",
"DSTAR",
"FAX",
"FSK441",
"HELL",
"ISCAT",
"JT4",
"JT6M",
"JT44",
"MFSK",
"MSK144",
"MT63",
"OLIVIA",
"OPERA",
"PAC",
"PAX",
"PKT",
"PSK2K",
"Q15",
"QRA64",
"ROS",
"RTTYM",
"SSTV",
"T10",
"THOR",
"THRB",
"TOR",
"V4",
"VOI",
"WINMOR",
"WSPR":
return true
}
return false
}