diff --git a/cmd/parse_line.go b/cmd/parse_line.go index 06d2a01..4933f40 100644 --- a/cmd/parse_line.go +++ b/cmd/parse_line.go @@ -36,6 +36,8 @@ type LogLine struct { Frequency string Time string Call string + Comment string + QSLmsg string } @@ -46,6 +48,14 @@ func ParseLine(inputStr string, previousLine LogLine) (logLine LogLine, errorMsg 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) for _, element := range elements { @@ -89,10 +99,41 @@ func SprintLogRecord(logLine LogLine) (output string){ output = output + "Frequency " + logLine.Frequency + "\n" output = output + "Time " + logLine.Time + "\n" output = output + "Call " + logLine.Call + "\n" + output = output + "Comment " + logLine.Comment + "\n" + output = output + "QSLmsg " + logLine.QSLmsg + "\n" 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 { switch lookup { case diff --git a/cmd/parse_line_test.go b/cmd/parse_line_test.go index ad73e9e..1b60bbd 100644 --- a/cmd/parse_line_test.go +++ b/cmd/parse_line_test.go @@ -22,6 +22,11 @@ func TestParseLine(t *testing.T) { args{ inputStr: "40m cw", previousLine: LogLine{ Mode: "SSB", }}, LogLine{ Band: "40m", Mode: "CW",}, "", }, + { + "Parse for comment", + args{ inputStr: "4 g3noh ", previousLine: LogLine{ Mode: "SSB", }}, + LogLine{ Comment: "PSE QSL Direct", Call: "G3NOH", Time: "2"}, "", + }, { "Wrong mode", args{ inputStr: "cww", previousLine: LogLine{ Mode: "SSB", }},