2 # various utilities which are exported globally
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
18 use vars qw($VERSION $BRANCH);
19 use vars qw(@month %patmap @ISA @EXPORT);
23 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf
24 parray parraypairs phex shellregex readfilestr writefilestr
26 print_all_fields cltounix unpad is_callsign is_latlong
27 is_qra is_freq is_digits is_pctext is_pcflag insertitem deleteitem
32 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
40 # a full time for logging and other purposes
44 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
46 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
50 # get a zulu time in cluster format (2300Z)
54 $t = defined $t ? $t : time;
56 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
57 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
61 # get a cluster format date (23-Jun-1998)
65 $t = defined $t ? $t : time;
67 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
69 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
73 # return a cluster style date time
78 my $date = cldate($t, $dst);
79 my $time = ztime($t, $dst);
83 # return a unix date from a cluster date and time
88 my ($thisyear) = (gmtime)[5] + 1900;
90 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
91 return 0 if $3 > 2036;
92 return 0 unless abs($thisyear-$3) <= 1;
94 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
95 $time = "$1:$2 +0000";
96 my $r = str2time("$date $time");
98 return $r == -1 ? undef : $r;
101 # turn a latitude in degrees into a string
105 my ($deg, $min, $let);
106 $let = $n >= 0 ? 'N' : 'S';
109 $min = int ((($n - $deg) * 60) + 0.5);
110 return "$deg $min $let";
113 # turn a longitude in degrees into a string
117 my ($deg, $min, $let);
118 $let = $n >= 0 ? 'E' : 'W';
121 $min = int ((($n - $deg) * 60) + 0.5);
122 return "$deg $min $let";
125 # turn a true into 'yes' and false into 'no'
129 return $n ? $main::yes : $main::no;
132 # provide a data dumpered version of the object passed
136 my $dd = new Data::Dumper([$value]);
139 $dd->Quotekeys($] < 5.005 ? 1 : 0);
140 $value = $dd->Dumpxs;
141 $value =~ s/([\r\n\t])/sprintf("%%%02X", ord($1))/eg;
142 $value =~ s/^\s*\[//;
143 $value =~ s/\]\s*$//;
148 # format a prompt with its current value and return it with its privilege
151 my ($line, $value) = @_;
152 my ($priv, $prompt, $action) = split ',', $line;
154 # if there is an action treat it as a subroutine and replace $value
156 my $q = qq{\$value = $action(\$value)};
158 } elsif (ref $value) {
161 $prompt = sprintf "%15s: %s", $prompt, $value;
162 return ($priv, $prompt);
165 # turn a hex field into printed hex
169 return sprintf '%X', $val;
172 # take an arg as a hash of call=>time pairs and print it
177 for (sort keys %$ref) {
178 $out .= "$_=$ref->{$_}, ";
185 # take an arg as an array list and print it
189 return ref $ref ? join(', ', @{$ref}) : $ref;
192 # take the arg as an array reference and print as a list of pairs
199 for ($i = 0; $i < @$ref; $i += 2) {
201 my $r2 = @$ref[$i+1];
204 chop $out; # remove last space
205 chop $out; # remove last comma
212 my @a = split /,/, $ref->field_prompt(shift);
213 my @b = split /,/, $ref->field_prompt(shift);
214 return lc $a[1] cmp lc $b[1];
217 # print all the fields for a record according to privilege
219 # The prompt record is of the format '<priv>,<prompt>[,<action>'
220 # and is expanded by promptf above
224 my $self = shift; # is a dxchan
225 my $ref = shift; # is a thingy with field_prompt and fields methods defined
227 my @fields = $ref->fields;
229 my $width = $self->width - 1;
232 foreach $field (sort {_sort_fields($ref, $a, $b)} @fields) {
233 if (defined $ref->{$field}) {
234 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
236 if (length $ans > $width) {
237 my ($p, $a) = split /: /, $ans, 2;
238 my $l = (length $p) + 2;
239 my $al = ($width - 1) - $l;
241 while (length $a > $al ) {
242 ($bit, $a) = unpack "A$al A*", $a;
243 push @tmp, "$p: $bit";
246 push @tmp, "$p: $a" if length $a;
250 push @out, @tmp if ($self->priv >= $priv);
256 # generate a regex from a shell type expression
257 # see 'perl cookbook' 6.9
261 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
262 return '^' . $in . "\$";
265 # read in a file into a string and return it.
266 # the filename can be split into a dir and file and the
267 # file can be in upper or lower case.
268 # there can also be a suffix
271 my ($dir, $file, $suffix) = @_;
276 $fn = "$dir/$f.$suffix";
279 $fn = "$dir/$file.$suffix";
292 my $fh = new IO::File $fn;
302 # write out a file in the format required for reading
303 # in via readfilestr, it expects the same arguments
304 # and a reference to an object
314 confess('no object to write in writefilestr') unless $obj;
315 confess('object not a reference in writefilestr') unless ref $obj;
319 $fn = "$dir/$f.$suffix";
322 $fn = "$dir/$file.$suffix";
335 my $fh = new IO::File ">$fn";
337 my $dd = new Data::Dumper([ $obj ]);
341 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
342 $fh->print($dd->Dumpxs);
349 copy(@_) or return $!;
352 # remove leading and trailing spaces from an input string
361 # check that a field only has callsign characters in it
364 return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+) # basic prefix
365 (?:/(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+))? # / another one (possibly)
366 [A-Z]{1,3} # callsign letters
367 (?:/(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+))? # / another prefix possibly
368 (?:/[0-9A-Z]{1,2})? # /0-9A-Z+ possibly
369 (?:-\d{1,2})? # - nn possibly
375 return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+)!x # basic prefix
379 # check that a PC protocol field is valid text
382 return undef unless length $_[0];
383 return undef if $_[0] =~ /[\x00-\x08\x0a-\x1f\x80-\x9f]/;
387 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
390 return $_[0] =~ /^[01\*\-]+$/;
393 # check that a thing is a frequency
396 return $_[0] =~ /^\d+(?:\.\d+)?$/;
399 # check that a thing is just digits
402 return $_[0] =~ /^[\d]+$/;
405 # does it look like a qra locator?
408 return $_[0] =~ /^[A-Ra-r][A-Ra-r]\d\d[A-Xa-x][A-Xa-x]$/;
411 # does it look like a valid lat/long
414 return $_[0] =~ /^\s*\d{1,2}\s+\d{1,2}\s*[NnSs]\s+1?\d{1,2}\s+\d{1,2}\s*[EeWw]\s*$/;
417 # insert an item into a list if it isn't already there returns 1 if there 0 if not
423 return 1 if grep {$_ eq $item } @$list;
428 # delete an item from a list if it is there returns no deleted
435 @$list = grep {$_ ne $item } @$list;
442 my ($a, $b, $c, $d) = $s =~ /(\d+)\.(\d+)\.(?:(\d+)\.(\d+))?/;
444 my $v = sprintf( "%d.%03d", $a, $b) || 0;
445 my $br = sprintf( "%d.%03d", $c, $d) if defined $c;
449 $main::branch += $br;
454 ($VERSION, $BRANCH) = dxver(q$Revision$);