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 phex shellregex readfilestr writefilestr
19 print_all_fields cltounix unpad is_callsign
20 is_freq is_digits is_pctext is_pcflag insertitem deleteitem
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 $t = defined $t ? $t : time;
47 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
48 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
52 # get a cluster format date (23-Jun-1998)
56 $t = defined $t ? $t : time;
58 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
60 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
64 # return a cluster style date time
69 my $date = cldate($t, $dst);
70 my $time = ztime($t, $dst);
74 # return a unix date from a cluster date and time
79 my ($thisyear) = (gmtime)[5] + 1900;
81 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
82 return 0 if $3 > 2036;
83 return 0 unless abs($thisyear-$3) <= 1;
85 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
86 $time = "$1:$2 +0000";
87 my $r = str2time("$date $time");
89 return $r == -1 ? undef : $r;
92 # turn a latitude in degrees into a string
96 my ($deg, $min, $let);
97 $let = $n >= 0 ? 'N' : 'S';
100 $min = int ((($n - $deg) * 60) + 0.5);
101 return "$deg $min $let";
104 # turn a longitude in degrees into a string
108 my ($deg, $min, $let);
109 $let = $n >= 0 ? 'E' : 'W';
112 $min = int ((($n - $deg) * 60) + 0.5);
113 return "$deg $min $let";
116 # turn a true into 'yes' and false into 'no'
120 return $n ? $main::yes : $main::no;
123 # format a prompt with its current value and return it with its privilege
126 my ($line, $value) = @_;
127 my ($priv, $prompt, $action) = split ',', $line;
129 # if there is an action treat it as a subroutine and replace $value
131 my $q = qq{\$value = $action(\$value)};
133 } elsif (ref $value) {
134 my $dd = new Data::Dumper([$value]);
138 $value = $dd->Dumpxs;
140 $prompt = sprintf "%15s: %s", $prompt, $value;
141 return ($priv, $prompt);
144 # turn a hex field into printed hex
148 return sprintf '%X', $val;
151 # take an arg as an array list and print it
155 return join(', ', @{$ref});
158 # take the arg as an array reference and print as a list of pairs
165 for ($i = 0; $i < @$ref; $i += 2) {
167 my $r2 = @$ref[$i+1];
170 chop $out; # remove last space
171 chop $out; # remove last comma
175 # print all the fields for a record according to privilege
177 # The prompt record is of the format '<priv>,<prompt>[,<action>'
178 # and is expanded by promptf above
182 my $self = shift; # is a dxchan
183 my $ref = shift; # is a thingy with field_prompt and fields methods defined
185 my @fields = $ref->fields;
188 foreach $field (sort {$ref->field_prompt($a) cmp $ref->field_prompt($b)} @fields) {
189 if (defined $ref->{$field}) {
190 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
192 if (length $ans > 79) {
193 my ($p, $a) = split /: /, $ans, 2;
194 my $l = (length $p) + 2;
197 while (length $a > $al ) {
198 ($bit, $a) = unpack "A$al A*", $a;
199 push @tmp, "$p: $bit";
202 push @tmp, "$p: $a" if length $a;
206 push @out, @tmp if ($self->priv >= $priv);
212 # generate a regex from a shell type expression
213 # see 'perl cookbook' 6.9
217 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
218 return '^' . $in . "\$";
221 # read in a file into a string and return it.
222 # the filename can be split into a dir and file and the
223 # file can be in upper or lower case.
224 # there can also be a suffix
227 my ($dir, $file, $suffix) = @_;
232 $fn = "$dir/$f.$suffix";
235 $fn = "$dir/$file.$suffix";
248 my $fh = new IO::File $fn;
258 # write out a file in the format required for reading
259 # in via readfilestr, it expects the same arguments
260 # and a reference to an object
270 confess('no object to write in writefilestr') unless $obj;
271 confess('object not a reference in writefilestr') unless ref $obj;
275 $fn = "$dir/$f.$suffix";
278 $fn = "$dir/$file.$suffix";
291 my $fh = new IO::File ">$fn";
293 my $dd = new Data::Dumper([ $obj ]);
297 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
298 $fh->print($dd->Dumpxs);
303 # remove leading and trailing spaces from an input string
312 # check that a field only has callsign characters in it
315 return $_[0] =~ /^(?:[A-Z]{1,2}\d+|\d[A-Z]\d+)[A-Z0-9\/\-]+$/;
318 # check that a PC protocol field is valid text
321 return $_[0] =~ /^[\x09\x20-\xFF]+$/;
324 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
327 return $_[0] =~ /^[01\*\-]+$/;
330 # check that a thing is a frequency
333 return $_[0] =~ /^[\d\.]+$/;
336 # check that a thing is just digits
339 return $_[0] =~ /^[\d]+$/;
342 # insert an item into a list if it isn't already there returns 1 if there 0 if not
348 return 1 if grep {$_ eq $item } @$list;
353 # delete an item from a list if it is there returns no deleted
360 @$list = grep {$_ ne $item } @$list;