2 # various utilities which are exported globally
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
17 @EXPORT = qw(atime ztime cldate cldatetime slat slong yesno promptf
18 parray parraypairs shellregex readfilestr writefilestr
19 print_all_fields cltounix iscallsign unpad is_callsign
20 is_freq is_digits is_pctext is_pcflag insertitem deleteitem
23 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
31 # a full time for logging and other purposes
35 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
37 my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
41 # get a zulu time in cluster format (2300Z)
45 $t = defined $t ? $t : time;
47 my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
48 my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
52 # get a cluster format date (23-Jun-1998)
56 $t = defined $t ? $t : time;
58 my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
60 my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
64 # return a cluster style date time
69 my $date = cldate($t, $dst);
70 my $time = ztime($t, $dst);
74 # return a unix date from a cluster date and time
79 my ($thisyear) = (gmtime)[5] + 1900;
81 return 0 unless $date =~ /^\s*(\d+)-(\w\w\w)-([12][90]\d\d)$/;
82 return 0 if $3 > 2036;
83 return 0 unless abs($thisyear-$3) <= 1;
85 return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
86 $time = "$1:$2 +0000";
87 my $r = str2time("$date $time");
89 return $r == -1 ? undef : $r;
92 # turn a latitude in degrees into a string
96 my ($deg, $min, $let);
97 $let = $n >= 0 ? 'N' : 'S';
100 $min = int ((($n - $deg) * 60) + 0.5);
101 return "$deg $min $let";
104 # turn a longitude in degrees into a string
108 my ($deg, $min, $let);
109 $let = $n >= 0 ? 'E' : 'W';
112 $min = int ((($n - $deg) * 60) + 0.5);
113 return "$deg $min $let";
116 # turn a true into 'yes' and false into 'no'
120 return $n ? $main::yes : $main::no;
123 # format a prompt with its current value and return it with its privilege
126 my ($line, $value) = @_;
127 my ($priv, $prompt, $action) = split ',', $line;
129 # if there is an action treat it as a subroutine and replace $value
131 my $q = qq{\$value = $action(\$value)};
133 } elsif (ref $value) {
134 my $dd = new Data::Dumper([$value]);
138 $value = $dd->Dumpxs;
140 $prompt = sprintf "%15s: %s", $prompt, $value;
141 return ($priv, $prompt);
144 # take an arg as an array list and print it
148 return join(', ', @{$ref});
151 # take the arg as an array reference and print as a list of pairs
158 for ($i = 0; $i < @$ref; $i += 2) {
160 my $r2 = @$ref[$i+1];
163 chop $out; # remove last space
164 chop $out; # remove last comma
168 # print all the fields for a record according to privilege
170 # The prompt record is of the format '<priv>,<prompt>[,<action>'
171 # and is expanded by promptf above
175 my $self = shift; # is a dxchan
176 my $ref = shift; # is a thingy with field_prompt and fields methods defined
178 my @fields = $ref->fields;
181 foreach $field (sort {$ref->field_prompt($a) cmp $ref->field_prompt($b)} @fields) {
182 if (defined $ref->{$field}) {
183 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
185 if (length $ans > 79) {
186 my ($p, $a) = split /: /, $ans;
187 my $l = (length $p) + 2;
189 while (length $a > $al ) {
194 push @tmp, "$p: $a" if length $a;
198 push @out, @tmp if ($self->priv >= $priv);
204 # generate a regex from a shell type expression
205 # see 'perl cookbook' 6.9
209 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
210 return '^' . $in . "\$";
213 # start an attempt at determining whether this string might be a callsign
217 return 1 if $call =~ /^[A-Z]+\d+[A-Z]+/;
218 return 1 if $call =~ /^\d+[A-Z]\d+[A-Z]+/;
222 # read in a file into a string and return it.
223 # the filename can be split into a dir and file and the
224 # file can be in upper or lower case.
225 # there can also be a suffix
228 my ($dir, $file, $suffix) = @_;
233 $fn = "$dir/$f.$suffix";
236 $fn = "$dir/$file.$suffix";
249 my $fh = new IO::File $fn;
259 # write out a file in the format required for reading
260 # in via readfilestr, it expects the same arguments
261 # and a reference to an object
271 confess('no object to write in writefilestr') unless $obj;
272 confess('object not a reference in writefilestr') unless ref $obj;
276 $fn = "$dir/$f.$suffix";
279 $fn = "$dir/$file.$suffix";
292 my $fh = new IO::File ">$fn";
294 my $dd = new Data::Dumper([ $obj ]);
298 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
299 $fh->print($dd->Dumpxs);
304 # remove leading and trailing spaces from an input string
313 # check that a field only has callsign characters in it
316 return $_[0] =~ /^(?:[A-Z]{1,2}\d+|\d[A-Z]\d+)[A-Z0-9\/\-]+$/;
319 # check that a PC protocol field is valid text
322 return $_[0] =~ /^[\x09\x20-\xFF]+$/;
325 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
328 return $_[0] =~ /^[01\*\-]+$/;
331 # check that a thing is a frequency
334 return $_[0] =~ /^[\d\.]+$/;
337 # check that a thing is just digits
340 return $_[0] =~ /^[\d]+$/;
343 # insert an item into a list if it isn't already there returns 1 if there 0 if not
349 return 1 if grep {$_ eq $item } @$list;
354 # delete an item from a list if it is there returns no deleted
361 @$list = grep {$_ ne $item } @$list;