2 # various utilities which are exported globally
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
19 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf
20 parray parraypairs shellregex readfilestr writefilestr
21 print_all_fields cltounix iscallsign
24 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
32 # a full time for logging and other purposes
36 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
38 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
42 # get a zulu time in cluster format (2300Z)
46 my ($sec,$min,$hour) = gmtime((defined $t) ? $t : time);
48 my $buf = sprintf "%02d%02dZ", $hour, $min;
53 # get a cluster format date (23-Jun-1998)
57 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
59 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
63 # return a cluster style date time
67 my $date = cldate($t);
72 # return a unix date from a cluster date and time
77 my ($thisyear) = (gmtime)[5] + 1900;
79 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
80 return 0 if $3 > 2036;
81 return 0 unless abs($thisyear-$3) <= 1;
83 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
84 $time = "$1:$2 +0000";
85 my $r = str2time("$date $time");
87 return $r == -1 ? undef : $r;
90 # turn a latitude in degrees into a string
94 my ($deg, $min, $let);
95 $let = $n >= 0 ? 'N' : 'S';
98 $min = int ((($n - $deg) * 60) + 0.5);
99 return "$deg $min $let";
102 # turn a longitude in degrees into a string
106 my ($deg, $min, $let);
107 $let = $n >= 0 ? 'E' : 'W';
110 $min = int ((($n - $deg) * 60) + 0.5);
111 return "$deg $min $let";
114 # turn a true into 'yes' and false into 'no'
118 return $n ? $main::yes : $main::no;
121 # format a prompt with its current value and return it with its privilege
124 my ($line, $value) = @_;
125 my ($priv, $prompt, $action) = split ',', $line;
127 # if there is an action treat it as a subroutine and replace $value
129 my $q = qq{\$value = $action(\$value)};
132 $prompt = sprintf "%15s: %s", $prompt, $value;
133 return ($priv, $prompt);
136 # take an arg as an array list and print it
140 return join(', ', @{$ref});
143 # take the arg as an array reference and print as a list of pairs
150 for ($i = 0; $i < @$ref; $i += 2) {
152 my $r2 = @$ref[$i+1];
155 chop $out; # remove last space
156 chop $out; # remove last comma
160 # print all the fields for a record according to privilege
162 # The prompt record is of the format '<priv>,<prompt>[,<action>'
163 # and is expanded by promptf above
167 my $self = shift; # is a dxchan
168 my $ref = shift; # is a thingy with field_prompt and fields methods defined
170 my @fields = $ref->fields;
173 foreach $field (sort {$ref->field_prompt($a) cmp $ref->field_prompt($b)} @fields) {
174 if (defined $ref->{$field}) {
175 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
176 push @out, $ans if ($self->priv >= $priv);
182 # generate a regex from a shell type expression
183 # see 'perl cookbook' 6.9
187 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
188 return '^' . $in . "\$";
191 # start an attempt at determining whether this string might be a callsign
195 return 1 if $call =~ /^\w+\d+/;
196 return 1 if $call =~ /^\d+\w+/;
200 # read in a file into a string and return it.
201 # the filename can be split into a dir and file and the
202 # file can be in upper or lower case.
203 # there can also be a suffix
206 my ($dir, $file, $suffix) = @_;
211 $fn = "$dir/$f.$suffix";
214 $fn = "$dir/$file.$suffix";
227 my $fh = new IO::File $fn;
237 # write out a file in the format required for reading
238 # in via readfilestr, it expects the same arguments
239 # and a reference to an object
249 confess('no object to write in writefilestr') unless $obj;
250 confess('object not a reference in writefilestr') unless ref $obj;
254 $fn = "$dir/$f.$suffix";
257 $fn = "$dir/$file.$suffix";
270 my $fh = new IO::File ">$fn";
272 my $dd = new Data::Dumper([ $obj ]);
276 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
277 $fh->print($dd->Dumpxs);