2 # various utilities which are exported globally
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
18 use vars qw($VERSION $BRANCH);
19 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
20 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ || (0,0));
21 $main::build += $VERSION;
22 $main::branch += $BRANCH;
24 use vars qw(@month %patmap @ISA @EXPORT);
28 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf
29 parray parraypairs phex shellregex readfilestr writefilestr
31 print_all_fields cltounix unpad is_callsign is_latlong
32 is_qra is_freq is_digits is_pctext is_pcflag insertitem deleteitem
37 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
45 # a full time for logging and other purposes
49 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
51 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
55 # get a zulu time in cluster format (2300Z)
59 $t = defined $t ? $t : time;
61 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
62 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
66 # get a cluster format date (23-Jun-1998)
70 $t = defined $t ? $t : time;
72 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
74 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
78 # return a cluster style date time
83 my $date = cldate($t, $dst);
84 my $time = ztime($t, $dst);
88 # return a unix date from a cluster date and time
93 my ($thisyear) = (gmtime)[5] + 1900;
95 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
96 return 0 if $3 > 2036;
97 return 0 unless abs($thisyear-$3) <= 1;
99 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
100 $time = "$1:$2 +0000";
101 my $r = str2time("$date $time");
103 return $r == -1 ? undef : $r;
106 # turn a latitude in degrees into a string
110 my ($deg, $min, $let);
111 $let = $n >= 0 ? 'N' : 'S';
114 $min = int ((($n - $deg) * 60) + 0.5);
115 return "$deg $min $let";
118 # turn a longitude in degrees into a string
122 my ($deg, $min, $let);
123 $let = $n >= 0 ? 'E' : 'W';
126 $min = int ((($n - $deg) * 60) + 0.5);
127 return "$deg $min $let";
130 # turn a true into 'yes' and false into 'no'
134 return $n ? $main::yes : $main::no;
137 # format a prompt with its current value and return it with its privilege
140 my ($line, $value) = @_;
141 my ($priv, $prompt, $action) = split ',', $line;
143 # if there is an action treat it as a subroutine and replace $value
145 my $q = qq{\$value = $action(\$value)};
147 } elsif (ref $value) {
148 my $dd = new Data::Dumper([$value]);
152 $value = $dd->Dumpxs;
153 $value =~ s/([\r\n\t])/sprintf("%%%02X", ord($1))/eg;
155 $prompt = sprintf "%15s: %s", $prompt, $value;
156 return ($priv, $prompt);
159 # turn a hex field into printed hex
163 return sprintf '%X', $val;
166 # take an arg as a hash of call=>time pairs and print it
171 for (sort keys %$ref) {
172 $out .= "$_=$ref->{$_}, ";
179 # take an arg as an array list and print it
183 return ref $ref ? join(', ', @{$ref}) : $ref;
186 # take the arg as an array reference and print as a list of pairs
193 for ($i = 0; $i < @$ref; $i += 2) {
195 my $r2 = @$ref[$i+1];
198 chop $out; # remove last space
199 chop $out; # remove last comma
206 my @a = split /,/, $ref->field_prompt(shift);
207 my @b = split /,/, $ref->field_prompt(shift);
208 return lc $a[1] cmp lc $b[1];
211 # print all the fields for a record according to privilege
213 # The prompt record is of the format '<priv>,<prompt>[,<action>'
214 # and is expanded by promptf above
218 my $self = shift; # is a dxchan
219 my $ref = shift; # is a thingy with field_prompt and fields methods defined
221 my @fields = $ref->fields;
223 my $width = $self->width - 1;
226 foreach $field (sort {_sort_fields($ref, $a, $b)} @fields) {
227 if (defined $ref->{$field}) {
228 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
230 if (length $ans > $width) {
231 my ($p, $a) = split /: /, $ans, 2;
232 my $l = (length $p) + 2;
233 my $al = ($width - 1) - $l;
235 while (length $a > $al ) {
236 ($bit, $a) = unpack "A$al A*", $a;
237 push @tmp, "$p: $bit";
240 push @tmp, "$p: $a" if length $a;
244 push @out, @tmp if ($self->priv >= $priv);
250 # generate a regex from a shell type expression
251 # see 'perl cookbook' 6.9
255 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
256 return '^' . $in . "\$";
259 # read in a file into a string and return it.
260 # the filename can be split into a dir and file and the
261 # file can be in upper or lower case.
262 # there can also be a suffix
265 my ($dir, $file, $suffix) = @_;
270 $fn = "$dir/$f.$suffix";
273 $fn = "$dir/$file.$suffix";
286 my $fh = new IO::File $fn;
296 # write out a file in the format required for reading
297 # in via readfilestr, it expects the same arguments
298 # and a reference to an object
308 confess('no object to write in writefilestr') unless $obj;
309 confess('object not a reference in writefilestr') unless ref $obj;
313 $fn = "$dir/$f.$suffix";
316 $fn = "$dir/$file.$suffix";
329 my $fh = new IO::File ">$fn";
331 my $dd = new Data::Dumper([ $obj ]);
335 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
336 $fh->print($dd->Dumpxs);
343 copy(@_) or return $!;
346 # remove leading and trailing spaces from an input string
355 # check that a field only has callsign characters in it
358 return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+) # basic prefix
359 (?:/(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+))? # / another one (possibly)
360 [A-Z]{1,3} # callsign letters
361 (?:/(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+))? # / another prefix possibly
362 (?:/[0-9A-Z]{1,2})? # /0-9A-Z+ possibly
363 (?:-\d{1,2})? # - nn possibly
369 return $_[0] =~ m!^(?:[A-Z]{1,2}\d+ | \d[A-Z]{1,2}\d+)!x # basic prefix
373 # check that a PC protocol field is valid text
376 return undef unless length $_[0];
377 return undef if $_[0] =~ /[\x00-\x08\x0a-\x1f\x80-\x9f]/;
381 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
384 return $_[0] =~ /^[01\*\-]+$/;
387 # check that a thing is a frequency
390 return $_[0] =~ /^\d+(?:\.\d+)?$/;
393 # check that a thing is just digits
396 return $_[0] =~ /^[\d]+$/;
399 # does it look like a qra locator?
402 return $_[0] =~ /^[A-Ra-r][A-Ra-r]\d\d[A-Xa-x][A-Xa-x]$/;
405 # does it look like a valid lat/long
408 return $_[0] =~ /^\s*\d{1,2}\s+\d{1,2}\s*[NnSs]\s+1?\d{1,2}\s+\d{1,2}\s*[EeWw]\s*$/;
411 # insert an item into a list if it isn't already there returns 1 if there 0 if not
417 return 1 if grep {$_ eq $item } @$list;
422 # delete an item from a list if it is there returns no deleted
429 @$list = grep {$_ ne $item } @$list;