Skip to content

Commit

Permalink
Consistently trim ADI/ADX comments to address #13
Browse files Browse the repository at this point in the history
  • Loading branch information
flwyd committed Feb 2, 2025
1 parent 9b60e33 commit 0b0cf5c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 16 deletions.
13 changes: 10 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ input.
associations before then.
* There is some variance between amateur radio organizations on exactly where
ITU boundaries lie. This program uses the
[CQWW WAZ list](https://cqww.com/cq_waz_list.htm) for CQ Zone to entity
[CQWW WAZ list](https://cqww.com/cq_waz_list.htm) for CQ-zone-to-entity
associations, the
[AARL DXCC list](https://www.arrl.org/files/file/DXCC/2022_Current_Deleted.txt)
for ITU to entity associations, amd [zone-check.eu](https://zone-check.eu/) to
for ITU-to-entity associations, and [zone-check.eu](https://zone-check.eu/) to
determine which ITU Zones a subdivision crosses. See also the mapability
links above for some explanation of data challenges. Maritime boundaries
between zones are not well defined and this program does not attempt to
Expand All @@ -74,7 +74,14 @@ a release.

### Fixed

Franz Josef Land DXCC entity is part of Russia, Arkhangelsk Oblast.
* Consistently trim leading and trailing space from ADI and ADX comments. If a
comment is nothing but space, don’t preserve it in the output.
**Note:** record comments and file comments are preserved from input through
output, but header comments, such as the text at the top of an ADI file, are
ignored. This could change in the future, but I don’t want to preserve all
the “Generated” lines created as part of a pipeline.
[See discussion](https://github.com/flwyd/adif-multitool/discussions/13).
* Franz Josef Land DXCC entity is part of Russia, Arkhangelsk Oblast.

### Removed

Expand Down
3 changes: 1 addition & 2 deletions adif/adi.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"io"
"strconv"
"strings"
"unicode"
)

type ADIIO struct {
Expand Down Expand Up @@ -143,7 +142,7 @@ func (o *ADIIO) Read(in io.Reader) (*Logfile, error) {
}
// arbitrary text between one field or record and the next
c, err := r.ReadString('<')
c = strings.TrimFunc(strings.TrimSuffix(c, "<"), unicode.IsSpace)
c = strings.TrimSpace(strings.TrimSuffix(c, "<"))
if c != "" {
comments = append(comments, c)
}
Expand Down
18 changes: 9 additions & 9 deletions adif/adx.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,16 @@ type adxRecord struct {
func (r adxRecord) Record(header bool) *Record {
res := NewRecord()
fcs := make([]string, 0, len(r.Fields))
if r.Comment != "" {
fcs = append(fcs, r.Comment)
if c := strings.TrimSpace(r.Comment); c != "" {
fcs = append(fcs, c)
}
for _, f := range r.Fields {
if header && f.IsUserdef() {
continue
}
res.Set(f.Field())
if f.Comment != "" {
fcs = append(fcs, f.Comment)
if c := strings.TrimSpace(f.Comment); c != "" {
fcs = append(fcs, c)
}
}
res.SetComment(strings.Join(fcs, "\n"))
Expand Down Expand Up @@ -172,8 +172,8 @@ func newAdxRecord(r *Record, l *Logfile) adxRecord {
}
res.Fields = append(res.Fields, x)
}
if c := r.GetComment(); c != "" {
res.Comment = r.GetComment()
if c := strings.TrimSpace(r.GetComment()); c != "" {
res.Comment = c
}
return res
}
Expand Down Expand Up @@ -206,7 +206,7 @@ func (o *ADXIO) Read(in io.Reader) (*Logfile, error) {
if err := d.Decode(&f); err != nil {
return nil, fmt.Errorf("could not decode ADX file: %w", err)
}
l.Comment = f.Comment
l.Comment = strings.TrimSpace(f.Comment)
l.Header = f.Header.Record(true)
us, err := f.Header.UserdefFields()
if err != nil {
Expand All @@ -233,8 +233,8 @@ func (o *ADXIO) Write(l *Logfile, out io.Writer) error {
for _, r := range l.Records {
f.Records = append(f.Records, newAdxRecord(r, l))
}
if l.Comment != "" {
f.Comment = l.Comment
if c := strings.TrimSpace(l.Comment); c != "" {
f.Comment = c
}
if n, err := out.Write([]byte(xml.Header)); err != nil {
return fmt.Errorf("could not write XML header to %s: %w", out, err)
Expand Down
2 changes: 1 addition & 1 deletion adif/adx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Inverted L antenna, 70' above ground
wantComments := []string{
"",
"Field comment #1\nField comment #2",
" This is a random comment ",
"This is a random comment",
}
wantUserdef := []UserdefField{
{Name: "MY FIELD", Type: TypeString},
Expand Down
56 changes: 56 additions & 0 deletions adifmt/testdata/cat_preserve_comments.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Tests that comments in ADIF files are preserved in output.

exec adifmt cat log1.adi log2.adx --output adi
cmp stdout combined.adi
! stderr .

-- log1.adi --
Some comments in an ADI file.
Another line in an ADI file.
<ADIF_VER:5>3.1.5 <CREATED_TIMESTAMP:15>23450607 080910 <PROGRAMID:6>adifmt <PROGRAMVERSION:7>(devel) <EOH>
Just a comment about K1A!
<CALL:3>K1A Inline comment #1 <MODE:2>CW <EOR>
Comment about the N2B record.
<CALL:3>N2B <MODE:3>SSBComment at the end of record<EOR>
This is an ADI comment at the end of the file.
-- log2.adx --
<?xml version="1.0" encoding="UTF-8"?>
<ADX>
<!-- This comment is in the ADX tag.
It has two lines@ -->
<HEADER>
<!-- A header comment in an ADX file. -->
<ADIF_VER>3.1.5</ADIF_VER>
<CREATED_TIMESTAMP>23450607 080910</CREATED_TIMESTAMP>
<PROGRAMID>adifmt</PROGRAMID>
<PROGRAMVERSION>(devel)</PROGRAMVERSION>
</HEADER>
<RECORDS>
<RECORD>
<CALL>K3C</CALL>
<!-- ADX comment about K3C. -->
<MODE>FM</MODE>
</RECORD>
<RECORD>
<!-- ADX comment regarding W4D! -->
<CALL>W4D</CALL>
<MODE>RTTY</MODE>
</RECORD>
</RECORDS>
<!-- This comment is at the end of the ADX tag :-) -->
</ADX>
-- combined.adi --
Generated with 4 records by https://github.com/flwyd/adif-multitool
<ADIF_VER:5>3.1.5 <CREATED_TIMESTAMP:15>23450607 080910 <PROGRAMID:6>adifmt <PROGRAMVERSION:7>(devel) <EOH>
Just a comment about K1A!
Inline comment #1 <CALL:3>K1A <MODE:2>CW <EOR>
Comment about the N2B record.
Comment at the end of record <CALL:3>N2B <MODE:3>SSB <EOR>
ADX comment about K3C. <CALL:3>K3C <MODE:2>FM <EOR>
ADX comment regarding W4D! <CALL:3>W4D <MODE:4>RTTY <EOR>
adif-multitool: original comment (log1.adi)
This is an ADI comment at the end of the file.

adif-multitool: original comment (log2.adx)
This comment is in the ADX tag.
It has two lines@ This comment is at the end of the ADX tag :-)
2 changes: 1 addition & 1 deletion cmd/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (a *accumulator) read(filename string) (*adif.Logfile, error) {
}
}
}
if c := l.Comment; c != "" {
if c := strings.TrimSpace(l.Comment); c != "" {
prefix := "adif-multitool: original comment"
if !strings.HasPrefix(c, prefix) {
if filename != "" && filename != "-" && filename != os.Stdin.Name() {
Expand Down

0 comments on commit 0b0cf5c

Please sign in to comment.