2 # various utilities which are exported globally
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
17 use vars qw($VERSION $BRANCH);
18 $VERSION = sprintf( "%d.%03d", q$Revision$ =~ /(\d+)\.(\d+)/ );
19 $BRANCH = sprintf( "%d.%03d", q$Revision$ =~ /\d+\.\d+\.(\d+)\.(\d+)/ ) || 0;
20 $main::build += $VERSION;
21 $main::branch += $BRANCH;
23 use vars qw(@month %patmap @ISA @EXPORT);
27 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf
28 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
34 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
42 # a full time for logging and other purposes
46 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
48 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
52 # get a zulu time in cluster format (2300Z)
56 $t = defined $t ? $t : time;
58 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
59 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
63 # get a cluster format date (23-Jun-1998)
67 $t = defined $t ? $t : time;
69 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
71 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
75 # return a cluster style date time
80 my $date = cldate($t, $dst);
81 my $time = ztime($t, $dst);
85 # return a unix date from a cluster date and time
90 my ($thisyear) = (gmtime)[5] + 1900;
92 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
93 return 0 if $3 > 2036;
94 return 0 unless abs($thisyear-$3) <= 1;
96 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
97 $time = "$1:$2 +0000";
98 my $r = str2time("$date $time");
100 return $r == -1 ? undef : $r;
103 # turn a latitude in degrees into a string
107 my ($deg, $min, $let);
108 $let = $n >= 0 ? 'N' : 'S';
111 $min = int ((($n - $deg) * 60) + 0.5);
112 return "$deg $min $let";
115 # turn a longitude in degrees into a string
119 my ($deg, $min, $let);
120 $let = $n >= 0 ? 'E' : 'W';
123 $min = int ((($n - $deg) * 60) + 0.5);
124 return "$deg $min $let";
127 # turn a true into 'yes' and false into 'no'
131 return $n ? $main::yes : $main::no;
134 # format a prompt with its current value and return it with its privilege
137 my ($line, $value) = @_;
138 my ($priv, $prompt, $action) = split ',', $line;
140 # if there is an action treat it as a subroutine and replace $value
142 my $q = qq{\$value = $action(\$value)};
144 } elsif (ref $value) {
145 my $dd = new Data::Dumper([$value]);
149 $value = $dd->Dumpxs;
151 $prompt = sprintf "%15s: %s", $prompt, $value;
152 return ($priv, $prompt);
155 # turn a hex field into printed hex
159 return sprintf '%X', $val;
162 # take an arg as an array list and print it
166 return join(', ', @{$ref});
169 # take the arg as an array reference and print as a list of pairs
176 for ($i = 0; $i < @$ref; $i += 2) {
178 my $r2 = @$ref[$i+1];
181 chop $out; # remove last space
182 chop $out; # remove last comma
189 my @a = split /,/, $ref->field_prompt(shift);
190 my @b = split /,/, $ref->field_prompt(shift);
191 return lc $a[1] cmp lc $b[1];
194 # print all the fields for a record according to privilege
196 # The prompt record is of the format '<priv>,<prompt>[,<action>'
197 # and is expanded by promptf above
201 my $self = shift; # is a dxchan
202 my $ref = shift; # is a thingy with field_prompt and fields methods defined
204 my @fields = $ref->fields;
206 my $width = $self->width - 1;
209 foreach $field (sort {_sort_fields($ref, $a, $b)} @fields) {
210 if (defined $ref->{$field}) {
211 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
213 if (length $ans > $width) {
214 my ($p, $a) = split /: /, $ans, 2;
215 my $l = (length $p) + 2;
216 my $al = ($width - 1) - $l;
218 while (length $a > $al ) {
219 ($bit, $a) = unpack "A$al A*", $a;
220 push @tmp, "$p: $bit";
223 push @tmp, "$p: $a" if length $a;
227 push @out, @tmp if ($self->priv >= $priv);
233 # generate a regex from a shell type expression
234 # see 'perl cookbook' 6.9
238 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
239 return '^' . $in . "\$";
242 # read in a file into a string and return it.
243 # the filename can be split into a dir and file and the
244 # file can be in upper or lower case.
245 # there can also be a suffix
248 my ($dir, $file, $suffix) = @_;
253 $fn = "$dir/$f.$suffix";
256 $fn = "$dir/$file.$suffix";
269 my $fh = new IO::File $fn;
279 # write out a file in the format required for reading
280 # in via readfilestr, it expects the same arguments
281 # and a reference to an object
291 confess('no object to write in writefilestr') unless $obj;
292 confess('object not a reference in writefilestr') unless ref $obj;
296 $fn = "$dir/$f.$suffix";
299 $fn = "$dir/$file.$suffix";
312 my $fh = new IO::File ">$fn";
314 my $dd = new Data::Dumper([ $obj ]);
318 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
319 $fh->print($dd->Dumpxs);
324 # remove leading and trailing spaces from an input string
333 # check that a field only has callsign characters in it
336 return $_[0] =~ /^(?:[A-Z]{1,2}\d+|\d[A-Z]\d+)[A-Z]+(?:-\d{1,2}|\/[A-Z0-9]+)?$/;
339 # check that a PC protocol field is valid text
342 return $_[0] =~ /^[\x09\x20-\xFF]+$/;
345 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
348 return $_[0] =~ /^[01\*\-]+$/;
351 # check that a thing is a frequency
354 return $_[0] =~ /^\d+(?:\.\d+)?$/;
357 # check that a thing is just digits
360 return $_[0] =~ /^[\d]+$/;
363 # does it look like a qra locator?
366 return $_[0] =~ /^[A-Za-z][A-Za-z]\d\d[A-Za-z][A-Za-z]$/o;
369 # does it look like a valid lat/long
372 return $_[0] =~ /^\s*\d{1,2}\s+\d{1,2}\s*[NnSs]\s+\d{1,2}\s+\d{1,2}\s*[EeWw]\s*$/;
375 # insert an item into a list if it isn't already there returns 1 if there 0 if not
381 return 1 if grep {$_ eq $item } @$list;
386 # delete an item from a list if it is there returns no deleted
393 @$list = grep {$_ ne $item } @$list;