1
0
Fork 0
mirror of https://github.com/on4kjm/FLEcli.git synced 2025-02-17 04:22:36 +01:00
FLEcli/cmd/adif_write.go

137 lines
4 KiB
Go
Raw Normal View History

2020-06-27 22:53:23 +02:00
package cmd
/*
Copyright © 2020 Jean-Marc Meessen, ON4KJM <on4kjm@gmail.com>
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.
*/
2020-07-13 22:40:49 +02:00
import (
"bufio"
"fmt"
"os"
"strings"
"time"
)
2020-06-30 22:20:56 +02:00
// outputAdif generates and writes data in ADIF format
2020-07-12 22:33:01 +02:00
func outputAdif(outputFile string, fullLog []LogLine, isWWFF bool, isSOTA bool) {
2020-06-30 22:20:56 +02:00
//convert the log data to an in-memory ADIF file
2020-07-12 22:33:01 +02:00
adifData := buildAdif(fullLog, isWWFF, isSOTA)
2020-06-30 22:20:56 +02:00
//write to a file
2020-07-13 22:40:49 +02:00
writeFile(outputFile, adifData)
2020-06-29 22:29:42 +02:00
}
2020-06-30 22:20:56 +02:00
// buildAdif creates the adif file in memory ready to be printed
2020-07-12 22:33:01 +02:00
func buildAdif(fullLog []LogLine, isWWFF bool, isSOTA bool) (adifList []string) {
2020-06-29 22:29:42 +02:00
//Print the fixed header
adifList = append(adifList, "ADIF Export for Fast Log Entry by DF3CB")
adifList = append(adifList, "<PROGRAMID:3>FLE")
2020-07-12 22:33:01 +02:00
adifList = append(adifList, "<ADIF_VER:5>3.1.0")
2020-06-29 22:29:42 +02:00
adifList = append(adifList, "<EOH>")
for _, logLine := range fullLog {
2020-07-12 22:33:01 +02:00
var adifLine strings.Builder
adifLine.WriteString(adifElement("STATION_CALLSIGN", logLine.MyCall))
adifLine.WriteString(adifElement("CALL", logLine.Call))
adifLine.WriteString(adifElement("QSO_DATE", adifDate(logLine.Date)))
adifLine.WriteString(adifElement("TIME_ON", logLine.Time))
adifLine.WriteString(adifElement("BAND", logLine.Band))
adifLine.WriteString(adifElement("MODE", logLine.Mode))
2020-06-29 22:29:42 +02:00
if logLine.Frequency != "" {
2020-07-12 22:33:01 +02:00
adifLine.WriteString(adifElement("FREQ", logLine.Frequency))
}
adifLine.WriteString(adifElement("RST_SENT", logLine.RSTsent))
adifLine.WriteString(adifElement("RST_RCVD", logLine.RSTrcvd))
2020-07-16 20:14:52 +02:00
if logLine.Comment != "" {
adifLine.WriteString(adifElement("COMMENT", logLine.Comment))
}
if logLine.OMname != "" {
adifLine.WriteString(adifElement("NAME", logLine.OMname))
}
2020-07-12 22:33:01 +02:00
if logLine.QSLmsg != "" {
2020-07-16 20:14:52 +02:00
adifLine.WriteString(adifElement("QSLMSG", logLine.QSLmsg))
2020-07-12 22:33:01 +02:00
}
if isWWFF {
adifLine.WriteString(adifElement("MY_SIG", "WWFF"))
adifLine.WriteString(adifElement("MY_SIG_INFO", logLine.MyWWFF))
}
if isSOTA {
adifLine.WriteString(adifElement("MY_SOTA_REF", logLine.MySOTA))
if logLine.SOTA != "" {
adifLine.WriteString(adifElement("SOTA_REF", logLine.SOTA))
}
2020-06-29 22:29:42 +02:00
}
2020-07-12 22:33:01 +02:00
adifLine.WriteString(adifElement("OPERATOR", logLine.Operator))
2020-06-30 13:44:30 +02:00
if logLine.Nickname != "" {
2020-07-12 22:33:01 +02:00
adifLine.WriteString(adifElement("APP_EQSL_QTH_NICKNAME", logLine.Nickname))
2020-06-30 13:44:30 +02:00
}
2020-07-12 22:33:01 +02:00
adifLine.WriteString("<EOR>")
2020-06-30 13:44:30 +02:00
2020-07-12 22:33:01 +02:00
adifList = append(adifList, adifLine.String())
2020-06-29 22:29:42 +02:00
}
return adifList
}
2020-07-13 22:40:49 +02:00
// writeFile writes the in-memory data (lines) to a file
func writeFile(outputFile string, adifData []string) {
2020-06-30 22:20:56 +02:00
//TODO: check access rights
f, err := os.Create(outputFile)
checkFileError(err)
defer f.Close()
w := bufio.NewWriter(f)
lineCount := 0
for _, adifLine := range adifData {
_, err := w.WriteString(adifLine + "\n")
checkFileError(err)
w.Flush()
checkFileError(err)
lineCount++
}
2020-07-16 20:14:52 +02:00
fmt.Printf("\nSuccessfully wrote %d lines to file \"%s\"\n", lineCount, outputFile)
2020-06-30 22:20:56 +02:00
}
// adifElement generated the ADIF sub-element
2020-06-29 22:29:42 +02:00
func adifElement(elementName, elementValue string) (element string) {
return fmt.Sprintf("<%s:%d>%s ", strings.ToUpper(elementName), len(elementValue), elementValue)
2020-06-29 13:55:11 +02:00
}
2020-06-30 22:20:56 +02:00
// checkFileError handles file related errors
func checkFileError(e error) {
if e != nil {
panic(e)
}
}
2020-07-01 13:53:53 +02:00
//adifDate converts a date in YYYY-MM-DD format to YYYYMMDD
func adifDate(inputDate string) (outputDate string) {
2020-07-12 22:33:01 +02:00
const FLEdateFormat = "2006-01-02"
date, err := time.Parse(FLEdateFormat, inputDate)
2020-07-01 13:53:53 +02:00
//error should never happen
if err != nil {
panic(err)
}
2020-07-12 22:33:01 +02:00
const ADIFdateFormat = "20060102"
return date.Format(ADIFdateFormat)
2020-07-01 13:53:53 +02:00
}