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
186 # print all the fields for a record according to privilege
188 # The prompt record is of the format '<priv>,<prompt>[,<action>'
189 # and is expanded by promptf above
193 my $self = shift; # is a dxchan
194 my $ref = shift; # is a thingy with field_prompt and fields methods defined
196 my @fields = $ref->fields;
199 foreach $field (sort {$ref->field_prompt($a) cmp $ref->field_prompt($b)} @fields) {
200 if (defined $ref->{$field}) {
201 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
203 if (length $ans > 79) {
204 my ($p, $a) = split /: /, $ans, 2;
205 my $l = (length $p) + 2;
208 while (length $a > $al ) {
209 ($bit, $a) = unpack "A$al A*", $a;
210 push @tmp, "$p: $bit";
213 push @tmp, "$p: $a" if length $a;
217 push @out, @tmp if ($self->priv >= $priv);
223 # generate a regex from a shell type expression
224 # see 'perl cookbook' 6.9
228 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
229 return '^' . $in . "\$";
232 # read in a file into a string and return it.
233 # the filename can be split into a dir and file and the
234 # file can be in upper or lower case.
235 # there can also be a suffix
238 my ($dir, $file, $suffix) = @_;
243 $fn = "$dir/$f.$suffix";
246 $fn = "$dir/$file.$suffix";
259 my $fh = new IO::File $fn;
269 # write out a file in the format required for reading
270 # in via readfilestr, it expects the same arguments
271 # and a reference to an object
281 confess('no object to write in writefilestr') unless $obj;
282 confess('object not a reference in writefilestr') unless ref $obj;
286 $fn = "$dir/$f.$suffix";
289 $fn = "$dir/$file.$suffix";
302 my $fh = new IO::File ">$fn";
304 my $dd = new Data::Dumper([ $obj ]);
308 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
309 $fh->print($dd->Dumpxs);
314 # remove leading and trailing spaces from an input string
323 # check that a field only has callsign characters in it
326 return $_[0] =~ /^(?:[A-Z]{1,2}\d+|\d[A-Z]\d+)[A-Z]+(?:-\d{1,2}|\/[A-Z0-9]+)?$/;
329 # check that a PC protocol field is valid text
332 return $_[0] =~ /^[\x09\x20-\xFF]+$/;
335 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
338 return $_[0] =~ /^[01\*\-]+$/;
341 # check that a thing is a frequency
344 return $_[0] =~ /^[\d\.]+$/;
347 # check that a thing is just digits
350 return $_[0] =~ /^[\d]+$/;
353 # does it look like a qra locator?
356 return $_[0] =~ /^[A-Za-z][A-Za-z]\d\d[A-Za-z][A-Za-z]$/o;
359 # does it look like a valid lat/long
362 return $_[0] =~ /^\s*\d{1,2}\s+\d{1,2}\s*[NnSs]\s+\d{1,2}\s+\d{1,2}\s*[EeWw]\s*$/;
365 # insert an item into a list if it isn't already there returns 1 if there 0 if not
371 return 1 if grep {$_ eq $item } @$list;
376 # delete an item from a list if it is there returns no deleted
383 @$list = grep {$_ ne $item } @$list;