2 # various utilities which are exported globally
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
17 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf
18 parray parraypairs shellregex readfilestr writefilestr
19 print_all_fields cltounix iscallsign unpad
22 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
30 # a full time for logging and other purposes
34 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
36 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
40 # get a zulu time in cluster format (2300Z)
44 $t = defined $t ? $t : time;
46 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
47 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
51 # get a cluster format date (23-Jun-1998)
55 $t = defined $t ? $t : time;
57 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
59 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
63 # return a cluster style date time
68 my $date = cldate($t, $dst);
69 my $time = ztime($t, $dst);
73 # return a unix date from a cluster date and time
78 my ($thisyear) = (gmtime)[5] + 1900;
80 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
81 return 0 if $3 > 2036;
82 return 0 unless abs($thisyear-$3) <= 1;
84 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
85 $time = "$1:$2 +0000";
86 my $r = str2time("$date $time");
88 return $r == -1 ? undef : $r;
91 # turn a latitude in degrees into a string
95 my ($deg, $min, $let);
96 $let = $n >= 0 ? 'N' : 'S';
99 $min = int ((($n - $deg) * 60) + 0.5);
100 return "$deg $min $let";
103 # turn a longitude in degrees into a string
107 my ($deg, $min, $let);
108 $let = $n >= 0 ? 'E' : 'W';
111 $min = int ((($n - $deg) * 60) + 0.5);
112 return "$deg $min $let";
115 # turn a true into 'yes' and false into 'no'
119 return $n ? $main::yes : $main::no;
122 # format a prompt with its current value and return it with its privilege
125 my ($line, $value) = @_;
126 my ($priv, $prompt, $action) = split ',', $line;
128 # if there is an action treat it as a subroutine and replace $value
130 my $q = qq{\$value = $action(\$value)};
133 $prompt = sprintf "%15s: %s", $prompt, $value;
134 return ($priv, $prompt);
137 # take an arg as an array list and print it
141 return join(', ', @{$ref});
144 # take the arg as an array reference and print as a list of pairs
151 for ($i = 0; $i < @$ref; $i += 2) {
153 my $r2 = @$ref[$i+1];
156 chop $out; # remove last space
157 chop $out; # remove last comma
161 # print all the fields for a record according to privilege
163 # The prompt record is of the format '<priv>,<prompt>[,<action>'
164 # and is expanded by promptf above
168 my $self = shift; # is a dxchan
169 my $ref = shift; # is a thingy with field_prompt and fields methods defined
171 my @fields = $ref->fields;
174 foreach $field (sort {$ref->field_prompt($a) cmp $ref->field_prompt($b)} @fields) {
175 if (defined $ref->{$field}) {
176 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
177 push @out, $ans if ($self->priv >= $priv);
183 # generate a regex from a shell type expression
184 # see 'perl cookbook' 6.9
188 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
189 return '^' . $in . "\$";
192 # start an attempt at determining whether this string might be a callsign
196 return 1 if $call =~ /^[A-Z]+\d+[A-Z]+/;
197 return 1 if $call =~ /^\d+[A-Z]\d+[A-Z]+/;
201 # read in a file into a string and return it.
202 # the filename can be split into a dir and file and the
203 # file can be in upper or lower case.
204 # there can also be a suffix
207 my ($dir, $file, $suffix) = @_;
212 $fn = "$dir/$f.$suffix";
215 $fn = "$dir/$file.$suffix";
228 my $fh = new IO::File $fn;
238 # write out a file in the format required for reading
239 # in via readfilestr, it expects the same arguments
240 # and a reference to an object
250 confess('no object to write in writefilestr') unless $obj;
251 confess('object not a reference in writefilestr') unless ref $obj;
255 $fn = "$dir/$f.$suffix";
258 $fn = "$dir/$file.$suffix";
271 my $fh = new IO::File ">$fn";
273 my $dd = new Data::Dumper([ $obj ]);
277 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
278 $fh->print($dd->Dumpxs);
283 # remove leading and trailing spaces from an input string