From 18607a5ee8873ada88c6ecc2eb24e9ee9c6e6248 Mon Sep 17 00:00:00 2001 From: dm5wk Date: Tue, 24 Sep 2024 21:00:53 +0200 Subject: [PATCH 1/3] add pod help and my_ref with multiple output files --- adif-pota2sig.pl | 232 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 186 insertions(+), 46 deletions(-) diff --git a/adif-pota2sig.pl b/adif-pota2sig.pl index c27baae..641ecdb 100755 --- a/adif-pota2sig.pl +++ b/adif-pota2sig.pl @@ -8,58 +8,198 @@ # modified from perlmonks - TedPride - http://www.perlmonks.org/?node_id=559222 # modified from https://scruss.com/blog/2011/05/23/parsing-adif-with-perl/ # -# Description: -# Parse adif file, if a contact has one or more POTA_REF entries (delimited by -# a comma), add SIG POTA and SIG_INFO with reference. Multiply contact such, -# that every POTA_REF entry is a separate contact with own SIG_INFO. -# Extendet adif file will be printed to stdout. -# -# Usage: -# adif-pota2sig.pl logfile.adi > new-logfile.adi +# Description and Usage: +# adif-pota2sig.pl -h + +=pod + +=head1 NAME + +adif-pota2sig - Process adif file for POTA uploads + +=head1 SYNOPSIS + +adif-pota2sig.pl -i in.adi -o out.adi + +adif-pota2sig.pl < in.adi > out.adi + +adif-pota2sig.pl -r K-TEST -t out_REF.adi -i in.adi + +adif-pota2sig.pl -r K-TEST -t out_REF.adi < in.adi + +adif-pota2sig.pl -h + +=cut use strict; use warnings; -# use filename.adi as parameter -open(my $FH, '<', $ARGV[0]) or die "$ARGV[0]: $!"; +use Getopt::Long qw(:config gnu_compat); +use Pod::Usage; -while (<$FH>) { - print; - # fast forward past header - last if m/\s+$/i; +my $input_file = ""; +my $output_file = ""; +my $my_ref = ""; +my $output_template = ""; +my $force_output = 0; + +GetOptions ( + "i|input=s" => \$input_file, + "o|output=s" => \$output_file, + "r|reference=s" => \$my_ref, + "f|force" => \$force_output, + "t|template=s" => \$output_template, + "h|help" => sub {pod2usage(-verbose => 2)} +) or die("Error in command line arguments\n"); + +=pod + +=head1 OPTIONS + +=over + +=item B<-h|--help> + +Print a brief help message and exits. + +=item B<-i F|--input=F> + +Use F as input (default: - for stdin). + +=item B<-o F|--output=F> + +Use F as output (default: - for stdout). + +=item B<-r MY_POTA_REF|--reference=MY_POTA_REF> + +Use MY_POTA_REF as own POTA reference. If there are more than one (for a +n-fer) create a list with commas, e. g. S K-TEST1,K-TEST2,K-TEST3> + +B<-t> is mandatory for this option. + +=item B<-t F|--template=F> + +Use out_REF.adi as output filename template for multiple references. REF will +be replaced by a POTA reference. Multiple filenames will be created. + +B<-o> is not allowed for this option. + +B<-r> is mandatory for this option. + +=item B<-f|--force> + +By default filenames selected with B<-o> and B<-t> won't be overwritten and the +program will stop. Using B<-f> will overwrite output files. + +=back + +=cut + +if (($my_ref ne "") && ($output_template eq "")) { + die "-t is mandatory for -r"; } -my @entry = (); -my @pota_refs = (); -while (<$FH>) { - push @entry, $_; - if (m/\s+$/i) { - # entry complete - if (scalar(@pota_refs) > 0) { - # add sig_info to each pota reference - foreach my $ref (@pota_refs) { - # print all except last - print @entry[0 .. $#entry - 1]; - # insert POTA SIG - print "POTA\n"; - print "" . $ref . "\n"; - # print last - print $entry[-1]; - } - } else { - # no park reference in this entry - print @entry; - } - # clear entry and pota refs for next entry - @entry = (); - @pota_refs = (); - } elsif (m/^ - my $val = (split(/>/))[-1]; - chomp($val); - # fill pota_refs with references - @pota_refs = split(/,/, $val); - chomp @pota_refs; +if (($output_template ne "") && ($my_ref eq "")) { + die "-r is mandatory for -t"; +} + +if (($output_template ne "") && ($output_file ne "")) { + die "-o is not allowed with -t"; +} + +if (($output_template ne "") && ($output_template !~ /REF/)) { + die "-t needs REF in output filename template"; +} + +my $FH_IN; +if (($input_file eq "-") || ($input_file eq "")) { + $FH_IN = "STDIN"; +} else { + open($FH_IN, '<', $input_file) or die "$input_file: $!"; +} + +my @output_filenames; + +if ($my_ref ne "") { + foreach (split(/,/, $my_ref)) { + push(@output_filenames, $output_template =~ s/REF/$_/r); } +} else { + push(@output_filenames, $output_file); } -close($FH); + +foreach my $output_filename (@output_filenames) { + seek $FH_IN, 0, 0; + + my $FH_OUT; + + if (($output_filename eq "-") || ($output_filename eq "")) { + $FH_OUT = "STDOUT"; + } else { + if ((-e $output_filename) && (! $force_output)) { + die "File $output_filename exists. Use --force to overwrite"; + } else { + open($FH_OUT, '>', $output_filename) or die "$output_filename: $!"; + } + } + + # select filehandle for print + select($FH_OUT); + + while (<$FH_IN>) { + print; + # fast forward past header + last if m/\s+$/i; + } + + my @entry = (); + my @pota_refs = (); + while (<$FH_IN>) { + push @entry, $_; + if (m/\s+$/i) { + # entry complete + if (scalar(@pota_refs) > 0) { + # add sig_info to each pota reference + foreach my $ref (@pota_refs) { + # print all except last + print @entry[0 .. $#entry - 1]; + # insert POTA SIG + print "POTA\n"; + print "" . $ref . "\n"; + # print last + print $entry[-1]; + } + } else { + # no park reference in this entry + print @entry; + } + # clear entry and pota refs for next entry + @entry = (); + @pota_refs = (); + } elsif (m/^ + my $val = (split(/>/))[-1]; + chomp($val); + # fill pota_refs with references + @pota_refs = split(/,/, $val); + chomp @pota_refs; + } + } + close($FH_OUT); +} +close($FH_IN); + +__END__ + +=head1 DESCRIPTION + +B will read an adif file, if a contact has one or more POTA_REF +entries (delimited by a comma), add SIG POTA and SIG_INFO with reference. +Multiply contact such, that every POTA_REF entry is a separate contact with own +SIG_INFO. Extendet adif file will be printed to stdout or a file. + +If used with B<-r> and B<-t> options a number of files with contents described +above will be created. On file for each POTA reference. For output filenames +see B<-t>. + +=cut From 0ebec3bb20384edb15518fb6f718f66217bb44e4 Mon Sep 17 00:00:00 2001 From: dm5wk Date: Wed, 25 Sep 2024 18:24:29 +0200 Subject: [PATCH 2/3] set MY_SIG and MY_SIG_INFO --- adif-pota2sig.pl | 49 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/adif-pota2sig.pl b/adif-pota2sig.pl index 641ecdb..7505094 100755 --- a/adif-pota2sig.pl +++ b/adif-pota2sig.pl @@ -39,18 +39,18 @@ use Pod::Usage; my $input_file = ""; my $output_file = ""; -my $my_ref = ""; +my $my_refs = ""; my $output_template = ""; my $force_output = 0; GetOptions ( "i|input=s" => \$input_file, "o|output=s" => \$output_file, - "r|reference=s" => \$my_ref, - "f|force" => \$force_output, + "r|reference=s" => \$my_refs, "t|template=s" => \$output_template, + "f|force" => \$force_output, "h|help" => sub {pod2usage(-verbose => 2)} -) or die("Error in command line arguments\n"); +) or die("Error in command line arguments"); =pod @@ -95,11 +95,11 @@ program will stop. Using B<-f> will overwrite output files. =cut -if (($my_ref ne "") && ($output_template eq "")) { +if (($my_refs ne "") && ($output_template eq "")) { die "-t is mandatory for -r"; } -if (($output_template ne "") && ($my_ref eq "")) { +if (($output_template ne "") && ($my_refs eq "")) { die "-r is mandatory for -t"; } @@ -118,17 +118,17 @@ if (($input_file eq "-") || ($input_file eq "")) { open($FH_IN, '<', $input_file) or die "$input_file: $!"; } -my @output_filenames; +my %filenames_ref = (); -if ($my_ref ne "") { - foreach (split(/,/, $my_ref)) { - push(@output_filenames, $output_template =~ s/REF/$_/r); +if ($my_refs ne "") { + foreach (split(/,/, $my_refs)) { + $filenames_ref{$output_template =~ s/REF/$_/r} = $_; } } else { - push(@output_filenames, $output_file); + $filenames_ref{$output_file} = ""; } -foreach my $output_filename (@output_filenames) { +foreach my $output_filename (keys %filenames_ref) { seek $FH_IN, 0, 0; my $FH_OUT; @@ -158,21 +158,35 @@ foreach my $output_filename (@output_filenames) { push @entry, $_; if (m/\s+$/i) { # entry complete + + # maybe add MY_SIG + my $my_ref = $filenames_ref{$output_filename}; + if ($my_ref ne "") { + my $last = pop(@entry); + push(@entry, "POTA\r\n"); + push(@entry, "" . $my_ref . "\r\n"); + push(@entry, $last); + } + + # print whole entry if (scalar(@pota_refs) > 0) { + # maybe print entry multiple times # add sig_info to each pota reference foreach my $ref (@pota_refs) { # print all except last print @entry[0 .. $#entry - 1]; # insert POTA SIG - print "POTA\n"; - print "" . $ref . "\n"; + print "POTA\r\n"; + print "" . $ref . "\r\n"; # print last print $entry[-1]; } } else { # no park reference in this entry + # print entry only once print @entry; } + # clear entry and pota refs for next entry @entry = (); @pota_refs = (); @@ -183,6 +197,13 @@ foreach my $output_filename (@output_filenames) { # fill pota_refs with references @pota_refs = split(/,/, $val); chomp @pota_refs; + } elsif (m/^ Date: Wed, 25 Sep 2024 18:27:07 +0200 Subject: [PATCH 3/3] add more documentation --- README.md | 74 ++++++++++++++++++++++++++++++++++++++++++------ adif-pota2sig.pl | 48 +++++++++++++++++-------------- 2 files changed, 91 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index d419f6d..37f6c64 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,69 @@ # adif-pota2sig -Convert POTA fields in adif files to SIG and SIG_INFO fields +Process adif file for POTA uploads -Parse adif file, if a record has one or more POTA_REF entries (delimited by a -comma), add SIG POTA and SIG_INFO with reference. Multiply record such, that -every POTA_REF entry is a separate record with own SIG_INFO. -Extended adif file will be printed to stdout. +## SYNOPSIS -## Usage: -```sh -adif-pota2sig.pl logfile.adi > extended-logfile.adi -``` +adif-pota2sig.pl -i `in.adi` -o `out.adi` + +adif-pota2sig.pl < `in.adi` > `out.adi` + +adif-pota2sig.pl -r K-TEST -t `out_REF.adi` -i `in.adi` + +adif-pota2sig.pl -r K-TEST -t `out_REF.adi` < `in.adi` + +adif-pota2sig.pl -h + +## DESCRIPTION + +**adif-pota2sig** will read an adif file, if a contact has one or more POTA\_REF +entries (delimited by a comma), add SIG POTA and SIG\_INFO with reference. +Multiply contact such, that every POTA\_REF entry is a separate contact with own +SIG\_INFO. Extendet adif file will be printed to stdout or a file. + +If used with **-r** and **-t** options a number of files with contents described +above will be created. One file for each POTA reference. For output filenames +see **-t**. + +With **-r** you can also include the location in the format REFERENCE@LOCATION, +which is handy for trails. E. g. -r DE-0622@DE-HH + +## OPTIONS + +- **-h|--help** + + Print a brief help message and exits. + +- **-i `in.adi`|--input=`in.adi`** + + Use `in.adi` as input (default: - for stdin). + +- **-o `out.adi`|--output=`out.adi`** + + Use `out.adi` as output (default: - for stdout). + +- **-r MY\_POTA\_REF|--reference=MY\_POTA\_REF** + + Use MY\_POTA\_REF as own POTA reference. If there are more than one (for a + n-fer) create a list with commas, e. g. **-r** K-TEST1,K-TEST2,K-TEST3. + + Also set the entries MY\_SIG and MY\_SIG\_INFO matching the reference in the + output files. By default already existing entries with MY\_SIG and MY\_SIG\_INFO + won't be overwritten and the program will stop. Using **-f** will overwrite the + entries. + + **-t** is mandatory for this option. + +- **-t `out_REF.adi`|--template=`out_REF.adi`** + + Use `out_REF.adi` as output filename template for multiple references. REF + will be replaced by a POTA reference. Multiple filenames will be created. + + **-o** is not allowed for this option. + + **-r** is mandatory for this option. + +- **-f|--force** + + By default filenames selected with **-o** and **-t** won't be overwritten and the + program will stop. Using **-f** will overwrite output files. diff --git a/adif-pota2sig.pl b/adif-pota2sig.pl index 7505094..1c00c3d 100755 --- a/adif-pota2sig.pl +++ b/adif-pota2sig.pl @@ -19,13 +19,13 @@ adif-pota2sig - Process adif file for POTA uploads =head1 SYNOPSIS -adif-pota2sig.pl -i in.adi -o out.adi +adif-pota2sig.pl -i F -o F -adif-pota2sig.pl < in.adi > out.adi +adif-pota2sig.pl < F > F -adif-pota2sig.pl -r K-TEST -t out_REF.adi -i in.adi +adif-pota2sig.pl -r K-TEST -t F -i F -adif-pota2sig.pl -r K-TEST -t out_REF.adi < in.adi +adif-pota2sig.pl -r K-TEST -t F < F adif-pota2sig.pl -h @@ -54,6 +54,20 @@ GetOptions ( =pod +=head1 DESCRIPTION + +B will read an adif file, if a contact has one or more POTA_REF +entries (delimited by a comma), add SIG POTA and SIG_INFO with reference. +Multiply contact such, that every POTA_REF entry is a separate contact with own +SIG_INFO. Extendet adif file will be printed to stdout or a file. + +If used with B<-r> and B<-t> options a number of files with contents described +above will be created. One file for each POTA reference. For output filenames +see B<-t>. + +With B<-r> you can also include the location in the format REFERENCE@LOCATION, +which is handy for trails. E. g. -r DE-0622@DE-HH + =head1 OPTIONS =over @@ -73,14 +87,19 @@ Use F as output (default: - for stdout). =item B<-r MY_POTA_REF|--reference=MY_POTA_REF> Use MY_POTA_REF as own POTA reference. If there are more than one (for a -n-fer) create a list with commas, e. g. S K-TEST1,K-TEST2,K-TEST3> +n-fer) create a list with commas, e. g. S K-TEST1,K-TEST2,K-TEST3>. + +Also set the entries MY_SIG and MY_SIG_INFO matching the reference in the +output files. By default already existing entries with MY_SIG and MY_SIG_INFO +won't be overwritten and the program will stop. Using B<-f> will overwrite the +entries. B<-t> is mandatory for this option. =item B<-t F|--template=F> -Use out_REF.adi as output filename template for multiple references. REF will -be replaced by a POTA reference. Multiple filenames will be created. +Use F as output filename template for multiple references. REF +will be replaced by a POTA reference. Multiple filenames will be created. B<-o> is not allowed for this option. @@ -209,18 +228,3 @@ foreach my $output_filename (keys %filenames_ref) { close($FH_OUT); } close($FH_IN); - -__END__ - -=head1 DESCRIPTION - -B will read an adif file, if a contact has one or more POTA_REF -entries (delimited by a comma), add SIG POTA and SIG_INFO with reference. -Multiply contact such, that every POTA_REF entry is a separate contact with own -SIG_INFO. Extendet adif file will be printed to stdout or a file. - -If used with B<-r> and B<-t> options a number of files with contents described -above will be created. On file for each POTA reference. For output filenames -see B<-t>. - -=cut