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
36 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
44 # a full time for logging and other purposes
48 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
50 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
54 # get a zulu time in cluster format (2300Z)
58 $t = defined $t ? $t : time;
60 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
61 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
65 # get a cluster format date (23-Jun-1998)
69 $t = defined $t ? $t : time;
71 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
73 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
77 # return a cluster style date time
82 my $date = cldate($t, $dst);
83 my $time = ztime($t, $dst);
87 # return a unix date from a cluster date and time
92 my ($thisyear) = (gmtime)[5] + 1900;
94 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
95 return 0 if $3 > 2036;
96 return 0 unless abs($thisyear-$3) <= 1;
98 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
99 $time = "$1:$2 +0000";
100 my $r = str2time("$date $time");
102 return $r == -1 ? undef : $r;
105 # turn a latitude in degrees into a string
109 my ($deg, $min, $let);
110 $let = $n >= 0 ? 'N' : 'S';
113 $min = int ((($n - $deg) * 60) + 0.5);
114 return "$deg $min $let";
117 # turn a longitude in degrees into a string
121 my ($deg, $min, $let);
122 $let = $n >= 0 ? 'E' : 'W';
125 $min = int ((($n - $deg) * 60) + 0.5);
126 return "$deg $min $let";
129 # turn a true into 'yes' and false into 'no'
133 return $n ? $main::yes : $main::no;
136 # format a prompt with its current value and return it with its privilege
139 my ($line, $value) = @_;
140 my ($priv, $prompt, $action) = split ',', $line;
142 # if there is an action treat it as a subroutine and replace $value
144 my $q = qq{\$value = $action(\$value)};
146 } elsif (ref $value) {
147 my $dd = new Data::Dumper([$value]);
151 $value = $dd->Dumpxs;
152 $value =~ s/([\r\n\t])/sprintf("%%%02X", ord($1))/eg;
154 $prompt = sprintf "%15s: %s", $prompt, $value;
155 return ($priv, $prompt);
158 # turn a hex field into printed hex
162 return sprintf '%X', $val;
165 # take an arg as an array list and print it
169 return ref $ref ? join(', ', @{$ref}) : $ref;
172 # take the arg as an array reference and print as a list of pairs
179 for ($i = 0; $i < @$ref; $i += 2) {
181 my $r2 = @$ref[$i+1];
184 chop $out; # remove last space
185 chop $out; # remove last comma
192 my @a = split /,/, $ref->field_prompt(shift);
193 my @b = split /,/, $ref->field_prompt(shift);
194 return lc $a[1] cmp lc $b[1];
197 # print all the fields for a record according to privilege
199 # The prompt record is of the format '<priv>,<prompt>[,<action>'
200 # and is expanded by promptf above
204 my $self = shift; # is a dxchan
205 my $ref = shift; # is a thingy with field_prompt and fields methods defined
207 my @fields = $ref->fields;
209 my $width = $self->width - 1;
212 foreach $field (sort {_sort_fields($ref, $a, $b)} @fields) {
213 if (defined $ref->{$field}) {
214 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
216 if (length $ans > $width) {
217 my ($p, $a) = split /: /, $ans, 2;
218 my $l = (length $p) + 2;
219 my $al = ($width - 1) - $l;
221 while (length $a > $al ) {
222 ($bit, $a) = unpack "A$al A*", $a;
223 push @tmp, "$p: $bit";
226 push @tmp, "$p: $a" if length $a;
230 push @out, @tmp if ($self->priv >= $priv);
236 # generate a regex from a shell type expression
237 # see 'perl cookbook' 6.9
241 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
242 return '^' . $in . "\$";
245 # read in a file into a string and return it.
246 # the filename can be split into a dir and file and the
247 # file can be in upper or lower case.
248 # there can also be a suffix
251 my ($dir, $file, $suffix) = @_;
256 $fn = "$dir/$f.$suffix";
259 $fn = "$dir/$file.$suffix";
272 my $fh = new IO::File $fn;
282 # write out a file in the format required for reading
283 # in via readfilestr, it expects the same arguments
284 # and a reference to an object
294 confess('no object to write in writefilestr') unless $obj;
295 confess('object not a reference in writefilestr') unless ref $obj;
299 $fn = "$dir/$f.$suffix";
302 $fn = "$dir/$file.$suffix";
315 my $fh = new IO::File ">$fn";
317 my $dd = new Data::Dumper([ $obj ]);
321 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
322 $fh->print($dd->Dumpxs);
329 copy(@_) or return $!;
332 # remove leading and trailing spaces from an input string
341 # check that a field only has callsign characters in it
344 return $_[0] =~ /^(?:[A-Z]{1,2}\d+|\d[A-Z]{1,2}\d+)[A-Z]{1,3}(?:-\d{1,2}|\/(?:[A-Z]{1,2}\d{0,2}|\d[A-Z]\d{0,2}))?$/;
347 # check that a PC protocol field is valid text
350 return undef unless length $_[0];
351 return undef if $_[0] =~ /[\x00-\x08\x0a-\x1f\x80-\x9f]/;
355 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
358 return $_[0] =~ /^[01\*\-]+$/;
361 # check that a thing is a frequency
364 return $_[0] =~ /^\d+(?:\.\d+)?$/;
367 # check that a thing is just digits
370 return $_[0] =~ /^[\d]+$/;
373 # does it look like a qra locator?
376 return $_[0] =~ /^[A-Ra-r][A-Ra-r]\d\d[A-Xa-x][A-Xa-x]$/;
379 # does it look like a valid lat/long
382 return $_[0] =~ /^\s*\d{1,2}\s+\d{1,2}\s*[NnSs]\s+1?\d{1,2}\s+\d{1,2}\s*[EeWw]\s*$/;
385 # insert an item into a list if it isn't already there returns 1 if there 0 if not
391 return 1 if grep {$_ eq $item } @$list;
396 # delete an item from a list if it is there returns no deleted
403 @$list = grep {$_ ne $item } @$list;