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 $t = defined $t ? $t : time;
48 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
49 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
53 # get a cluster format date (23-Jun-1998)
57 $t = defined $t ? $t : time;
59 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
61 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
65 # return a cluster style date time
70 my $date = cldate($t, $dst);
71 my $time = ztime($t, $dst);
75 # return a unix date from a cluster date and time
80 my ($thisyear) = (gmtime)[5] + 1900;
82 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
83 return 0 if $3 > 2036;
84 return 0 unless abs($thisyear-$3) <= 1;
86 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
87 $time = "$1:$2 +0000";
88 my $r = str2time("$date $time");
90 return $r == -1 ? undef : $r;
93 # turn a latitude in degrees into a string
97 my ($deg, $min, $let);
98 $let = $n >= 0 ? 'N' : 'S';
101 $min = int ((($n - $deg) * 60) + 0.5);
102 return "$deg $min $let";
105 # turn a longitude in degrees into a string
109 my ($deg, $min, $let);
110 $let = $n >= 0 ? 'E' : 'W';
113 $min = int ((($n - $deg) * 60) + 0.5);
114 return "$deg $min $let";
117 # turn a true into 'yes' and false into 'no'
121 return $n ? $main::yes : $main::no;
124 # format a prompt with its current value and return it with its privilege
127 my ($line, $value) = @_;
128 my ($priv, $prompt, $action) = split ',', $line;
130 # if there is an action treat it as a subroutine and replace $value
132 my $q = qq{\$value = $action(\$value)};
135 $prompt = sprintf "%15s: %s", $prompt, $value;
136 return ($priv, $prompt);
139 # take an arg as an array list and print it
143 return join(', ', @{$ref});
146 # take the arg as an array reference and print as a list of pairs
153 for ($i = 0; $i < @$ref; $i += 2) {
155 my $r2 = @$ref[$i+1];
158 chop $out; # remove last space
159 chop $out; # remove last comma
163 # print all the fields for a record according to privilege
165 # The prompt record is of the format '<priv>,<prompt>[,<action>'
166 # and is expanded by promptf above
170 my $self = shift; # is a dxchan
171 my $ref = shift; # is a thingy with field_prompt and fields methods defined
173 my @fields = $ref->fields;
176 foreach $field (sort {$ref->field_prompt($a) cmp $ref->field_prompt($b)} @fields) {
177 if (defined $ref->{$field}) {
178 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
179 push @out, $ans if ($self->priv >= $priv);
185 # generate a regex from a shell type expression
186 # see 'perl cookbook' 6.9
190 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
191 return '^' . $in . "\$";
194 # start an attempt at determining whether this string might be a callsign
198 return 1 if $call =~ /^[A-Z]+\d+[A-Z]+/;
199 return 1 if $call =~ /^\d+[A-Z]\d+[A-Z]+/;
203 # read in a file into a string and return it.
204 # the filename can be split into a dir and file and the
205 # file can be in upper or lower case.
206 # there can also be a suffix
209 my ($dir, $file, $suffix) = @_;
214 $fn = "$dir/$f.$suffix";
217 $fn = "$dir/$file.$suffix";
230 my $fh = new IO::File $fn;
240 # write out a file in the format required for reading
241 # in via readfilestr, it expects the same arguments
242 # and a reference to an object
252 confess('no object to write in writefilestr') unless $obj;
253 confess('object not a reference in writefilestr') unless ref $obj;
257 $fn = "$dir/$f.$suffix";
260 $fn = "$dir/$file.$suffix";
273 my $fh = new IO::File ">$fn";
275 my $dd = new Data::Dumper([ $obj ]);
279 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
280 $fh->print($dd->Dumpxs);