2 # various utilities which are exported globally
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
18 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf
19 parray parraypairs shellregex readfilestr
20 print_all_fields cltounix iscallsign
23 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
31 # a full time for logging and other purposes
35 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
37 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
41 # get a zulu time in cluster format (2300Z)
45 my ($sec,$min,$hour) = gmtime((defined $t) ? $t : time);
47 my $buf = sprintf "%02d%02dZ", $hour, $min;
52 # get a cluster format date (23-Jun-1998)
56 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
58 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
62 # return a cluster style date time
66 my $date = cldate($t);
71 # return a unix date from a cluster date and time
76 my ($thisyear) = (gmtime)[5] + 1900;
78 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
79 return 0 if $3 > 2036;
80 return 0 unless abs($thisyear-$3) <= 1;
82 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
83 $time = "$1:$2 +0000";
84 my $r = str2time("$date $time");
86 return $r == -1 ? undef : $r;
89 # turn a latitude in degrees into a string
93 my ($deg, $min, $let);
94 $let = $n >= 0 ? 'N' : 'S';
97 $min = int ((($n - $deg) * 60) + 0.5);
98 return "$deg $min $let";
101 # turn a longitude in degrees into a string
105 my ($deg, $min, $let);
106 $let = $n >= 0 ? 'E' : 'W';
109 $min = int ((($n - $deg) * 60) + 0.5);
110 return "$deg $min $let";
113 # turn a true into 'yes' and false into 'no'
117 return $n ? $main::yes : $main::no;
120 # format a prompt with its current value and return it with its privilege
123 my ($line, $value) = @_;
124 my ($priv, $prompt, $action) = split ',', $line;
126 # if there is an action treat it as a subroutine and replace $value
128 my $q = qq{\$value = $action(\$value)};
131 $prompt = sprintf "%15s: %s", $prompt, $value;
132 return ($priv, $prompt);
135 # take an arg as an array list and print it
139 return join(', ', @{$ref});
142 # take the arg as an array reference and print as a list of pairs
149 for ($i = 0; $i < @$ref; $i += 2) {
151 my $r2 = @$ref[$i+1];
154 chop $out; # remove last space
155 chop $out; # remove last comma
159 # print all the fields for a record according to privilege
161 # The prompt record is of the format '<priv>,<prompt>[,<action>'
162 # and is expanded by promptf above
166 my $self = shift; # is a dxchan
167 my $ref = shift; # is a thingy with field_prompt and fields methods defined
169 my @fields = $ref->fields;
172 foreach $field (sort {$ref->field_prompt($a) cmp $ref->field_prompt($b)} @fields) {
173 if (defined $ref->{$field}) {
174 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
175 push @out, $ans if ($self->priv >= $priv);
181 # generate a regex from a shell type expression
182 # see 'perl cookbook' 6.9
186 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
187 return '^' . $in . "\$";
190 # start an attempt at determining whether this string might be a callsign
194 return 1 if $call =~ /^\w+\d+/;
195 return 1 if $call =~ /^\d+\w+/;
199 # read in a file into a string and return it.
200 # the filename can be split into a dir and file and the
201 # file can be in upper or lower case.
202 # there can also be a suffix
205 my ($dir, $file, $suffix) = @_;
209 $fn = "$dir/$file.$suffix";
212 $fn = "$dir/$file.$suffix";
223 my $fh = new IO::File $fn;