Parsing comments

pull/2/head
Jean-Marc MEESSEN 4 years ago
parent 3656daff9e
commit d4a9ff89b1

@ -36,6 +36,8 @@ type LogLine struct {
Frequency string Frequency string
Time string Time string
Call string Call string
Comment string
QSLmsg string
} }
@ -46,6 +48,14 @@ func ParseLine(inputStr string, previousLine LogLine) (logLine LogLine, errorMsg
logLine = previousLine logLine = previousLine
//TODO: what happens when we have <> or when there are multiple comments
comment := getBraketedData(inputStr, "COMMENT")
if comment != "" {
logLine.Comment = comment
inputStr = strings.Replace(inputStr, "<" + comment + ">", "",1)
fmt.Println("Cleaned input string: ", inputStr)
}
elements := strings.Fields(inputStr) elements := strings.Fields(inputStr)
for _, element := range elements { for _, element := range elements {
@ -89,10 +99,41 @@ func SprintLogRecord(logLine LogLine) (output string){
output = output + "Frequency " + logLine.Frequency + "\n" output = output + "Frequency " + logLine.Frequency + "\n"
output = output + "Time " + logLine.Time + "\n" output = output + "Time " + logLine.Time + "\n"
output = output + "Call " + logLine.Call + "\n" output = output + "Call " + logLine.Call + "\n"
output = output + "Comment " + logLine.Comment + "\n"
output = output + "QSLmsg " + logLine.QSLmsg + "\n"
return output return output
} }
func getBraketedData(value, braketType string) (text string) {
// Get substring between two strings.
a := ""
b := ""
if braketType == "COMMENT" {
a = "<"
b = ">"
}
if braketType == "QSL" {
a = "["
b = "]"
}
posFirst := strings.Index(value, a)
if posFirst == -1 {
return ""
}
posLast := strings.Index(value, b)
if posLast == -1 {
return ""
}
posFirstAdjusted := posFirst + 1
if posFirstAdjusted >= posLast {
return ""
}
return value[posFirstAdjusted:posLast]
}
func lookupMode(lookup string) bool { func lookupMode(lookup string) bool {
switch lookup { switch lookup {
case case

@ -22,6 +22,11 @@ func TestParseLine(t *testing.T) {
args{ inputStr: "40m cw", previousLine: LogLine{ Mode: "SSB", }}, args{ inputStr: "40m cw", previousLine: LogLine{ Mode: "SSB", }},
LogLine{ Band: "40m", Mode: "CW",}, "", LogLine{ Band: "40m", Mode: "CW",}, "",
}, },
{
"Parse for comment",
args{ inputStr: "4 g3noh <PSE QSL Direct>", previousLine: LogLine{ Mode: "SSB", }},
LogLine{ Comment: "PSE QSL Direct", Call: "G3NOH", Time: "2"}, "",
},
{ {
"Wrong mode", "Wrong mode",
args{ inputStr: "cww", previousLine: LogLine{ Mode: "SSB", }}, args{ inputStr: "cww", previousLine: LogLine{ Mode: "SSB", }},

Loading…
Cancel
Save