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 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 # read in a file into a string and return it.
214 # the filename can be split into a dir and file and the
215 # file can be in upper or lower case.
216 # there can also be a suffix
219 my ($dir, $file, $suffix) = @_;
224 $fn = "$dir/$f.$suffix";
227 $fn = "$dir/$file.$suffix";
240 my $fh = new IO::File $fn;
250 # write out a file in the format required for reading
251 # in via readfilestr, it expects the same arguments
252 # and a reference to an object
262 confess('no object to write in writefilestr') unless $obj;
263 confess('object not a reference in writefilestr') unless ref $obj;
267 $fn = "$dir/$f.$suffix";
270 $fn = "$dir/$file.$suffix";
283 my $fh = new IO::File ">$fn";
285 my $dd = new Data::Dumper([ $obj ]);
289 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
290 $fh->print($dd->Dumpxs);
295 # remove leading and trailing spaces from an input string
304 # check that a field only has callsign characters in it
307 return $_[0] =~ /^(?:[A-Z]{1,2}\d+|\d[A-Z]\d+)[A-Z0-9\/\-]+$/;
310 # check that a PC protocol field is valid text
313 return $_[0] =~ /^[\x09\x20-\xFF]+$/;
316 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
319 return $_[0] =~ /^[01\*\-]+$/;
322 # check that a thing is a frequency
325 return $_[0] =~ /^[\d\.]+$/;
328 # check that a thing is just digits
331 return $_[0] =~ /^[\d]+$/;
334 # insert an item into a list if it isn't already there returns 1 if there 0 if not
340 return 1 if grep {$_ eq $item } @$list;
345 # delete an item from a list if it is there returns no deleted
352 @$list = grep {$_ ne $item } @$list;