2 # various utilities which are exported globally
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
18 use vars qw($VERSION $BRANCH);
20 main::mkver($VERSION = q$Revision$);
22 use vars qw(@month %patmap @ISA @EXPORT);
26 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf
27 parray parraypairs phex shellregex readfilestr writefilestr
29 print_all_fields cltounix unpad is_callsign is_latlong
30 is_qra is_freq is_digits is_pctext is_pcflag insertitem deleteitem
35 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
43 # a full time for logging and other purposes
47 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
49 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
53 # get a zulu time in cluster format (2300Z)
57 $t = defined $t ? $t : time;
59 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
60 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
64 # get a cluster format date (23-Jun-1998)
68 $t = defined $t ? $t : time;
70 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
72 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
76 # return a cluster style date time
81 my $date = cldate($t, $dst);
82 my $time = ztime($t, $dst);
86 # return a unix date from a cluster date and time
91 my ($thisyear) = (gmtime)[5] + 1900;
93 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
94 return 0 if $3 > 2036;
95 return 0 unless abs($thisyear-$3) <= 1;
97 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
98 $time = "$1:$2 +0000";
99 my $r = str2time("$date $time");
101 return $r == -1 ? undef : $r;
104 # turn a latitude in degrees into a string
108 my ($deg, $min, $let);
109 $let = $n >= 0 ? 'N' : 'S';
112 $min = int ((($n - $deg) * 60) + 0.5);
113 return "$deg $min $let";
116 # turn a longitude in degrees into a string
120 my ($deg, $min, $let);
121 $let = $n >= 0 ? 'E' : 'W';
124 $min = int ((($n - $deg) * 60) + 0.5);
125 return "$deg $min $let";
128 # turn a true into 'yes' and false into 'no'
132 return $n ? $main::yes : $main::no;
135 # format a prompt with its current value and return it with its privilege
138 my ($line, $value) = @_;
139 my ($priv, $prompt, $action) = split ',', $line;
141 # if there is an action treat it as a subroutine and replace $value
143 my $q = qq{\$value = $action(\$value)};
145 } elsif (ref $value) {
146 my $dd = new Data::Dumper([$value]);
150 $value = $dd->Dumpxs;
151 $value =~ s/([\r\n\t])/sprintf("%%%02X", ord($1))/eg;
153 $prompt = sprintf "%15s: %s", $prompt, $value;
154 return ($priv, $prompt);
157 # turn a hex field into printed hex
161 return sprintf '%X', $val;
164 # take an arg as a hash of call=>time pairs and print it
169 for (sort keys %$ref) {
170 $out .= "$_=$ref->{$_}, ";
177 # take an arg as an array list and print it
181 return ref $ref ? join(', ', @{$ref}) : $ref;
184 # take the arg as an array reference and print as a list of pairs
191 for ($i = 0; $i < @$ref; $i += 2) {
193 my $r2 = @$ref[$i+1];
196 chop $out; # remove last space
197 chop $out; # remove last comma
204 my @a = split /,/, $ref->field_prompt(shift);
205 my @b = split /,/, $ref->field_prompt(shift);
206 return lc $a[1] cmp lc $b[1];
209 # print all the fields for a record according to privilege
211 # The prompt record is of the format '<priv>,<prompt>[,<action>'
212 # and is expanded by promptf above
216 my $self = shift; # is a dxchan
217 my $ref = shift; # is a thingy with field_prompt and fields methods defined
219 my @fields = $ref->fields;
221 my $width = $self->width - 1;
224 foreach $field (sort {_sort_fields($ref, $a, $b)} @fields) {
225 if (defined $ref->{$field}) {
226 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
228 if (length $ans > $width) {
229 my ($p, $a) = split /: /, $ans, 2;
230 my $l = (length $p) + 2;
231 my $al = ($width - 1) - $l;
233 while (length $a > $al ) {
234 ($bit, $a) = unpack "A$al A*", $a;
235 push @tmp, "$p: $bit";
238 push @tmp, "$p: $a" if length $a;
242 push @out, @tmp if ($self->priv >= $priv);
248 # generate a regex from a shell type expression
249 # see 'perl cookbook' 6.9
253 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
254 return '^' . $in . "\$";
257 # read in a file into a string and return it.
258 # the filename can be split into a dir and file and the
259 # file can be in upper or lower case.
260 # there can also be a suffix
263 my ($dir, $file, $suffix) = @_;
268 $fn = "$dir/$f.$suffix";
271 $fn = "$dir/$file.$suffix";
284 my $fh = new IO::File $fn;
294 # write out a file in the format required for reading
295 # in via readfilestr, it expects the same arguments
296 # and a reference to an object
306 confess('no object to write in writefilestr') unless $obj;
307 confess('object not a reference in writefilestr') unless ref $obj;
311 $fn = "$dir/$f.$suffix";
314 $fn = "$dir/$file.$suffix";
327 my $fh = new IO::File ">$fn";
329 my $dd = new Data::Dumper([ $obj ]);
333 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
334 $fh->print($dd->Dumpxs);
341 copy(@_) or return $!;
344 # remove leading and trailing spaces from an input string
353 # check that a field only has callsign characters in it
356 return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+) # basic prefix
357 (?:/(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+))? # / another one (possibly)
358 [A-Z]{1,3} # callsign letters
359 (?:/(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+))? # / another prefix possibly
360 (?:/[0-9A-Z]{1,2})? # /0-9A-Z+ possibly
361 (?:-\d{1,2})? # - nn possibly
367 return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+)!x # basic prefix
370 # check that a PC protocol field is valid text
373 return undef unless length $_[0];
374 return undef if $_[0] =~ /[\x00-\x08\x0a-\x1f\x80-\x9f]/;
378 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
381 return $_[0] =~ /^[01\*\-]+$/;
384 # check that a thing is a frequency
387 return $_[0] =~ /^\d+(?:\.\d+)?$/;
390 # check that a thing is just digits
393 return $_[0] =~ /^[\d]+$/;
396 # does it look like a qra locator?
399 return $_[0] =~ /^[A-Ra-r][A-Ra-r]\d\d[A-Xa-x][A-Xa-x]$/;
402 # does it look like a valid lat/long
405 return $_[0] =~ /^\s*\d{1,2}\s+\d{1,2}\s*[NnSs]\s+1?\d{1,2}\s+\d{1,2}\s*[EeWw]\s*$/;
408 # insert an item into a list if it isn't already there returns 1 if there 0 if not
414 return 1 if grep {$_ eq $item } @$list;
419 # delete an item from a list if it is there returns no deleted
426 @$list = grep {$_ ne $item } @$list;