| 1 | # Copyright (C) 2008 Wes Hardaker | ||
| 2 | # License: Same as perl. See the LICENSE file for details. | ||
| 3 | package Ham::Callsign::Display::Format; | ||
| 4 | |||
| 5 | use Ham::Callsign::Base; | ||
| 6 | use Ham::Callsign; | ||
| 7 | use Data::Dumper; | ||
| 8 | our @ISA = qw(Ham::Callsign::Base); | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | |||
| 12 | sub sprint { | ||
| 13 | my ($self, $callsigns, $format) = @_; | ||
| 14 | $format .= "\n" if ($format); | ||
| 15 | $format = $self->{'format'} . "\n" if (!$format && $self->{'format'}); | ||
| 16 | if (!$format) { | ||
| 17 | $format = "%{3.3:FromDB}: %{1:operator_class} %{-8.8:thecallsign} %{first_name} %{last_name} => %{qth}\n" | ||
| 18 | } | ||
| 19 | |||
| 20 | my $output = ""; | ||
| 21 | |||
| 22 | # this allows array refs to be given with multiple calls... | ||
| 23 | foreach my $callsign (@$callsigns) { | ||
| 24 | my $localformat = $self->{$callsign->{'FromDB'} . "format"} . "\n" | ||
| 25 | if (exists($self->{$callsign->{'FromDB'} . "format"})); | ||
| 26 | $output .= format_to_string($localformat || $format, $callsign); | ||
| 27 | } | ||
| 28 | return $output; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub display { | ||
| 32 | my $self = shift; | ||
| 33 | print $self->sprint(@_); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub format_to_string { | ||
| 37 | my ($format, $callsign) = @_; | ||
| 38 | my @args; | ||
| 39 | |||
| 40 | # changes strings like %{NUM:STRING} into sprintf statement arguments | ||
| 41 | while ($format =~ s/\%{(?:(-?[\.\d]+):|)([^\%]+)}/"%" . ($1 || "") . "s"/e) { | ||
| 42 | my $arg; | ||
| 43 | if (defined($callsign->{$2})) { | ||
| 44 | $arg = $callsign->{$2}; | ||
| 45 | } else { | ||
| 46 | $arg = ""; | ||
| 47 | } | ||
| 48 | push @args, "$arg"; | ||
| 49 | } | ||
| 50 | return sprintf($format, @args); | ||
| 51 | } |