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
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)};
134 $prompt = sprintf "%15s: %s", $prompt, $value;
135 return ($priv, $prompt);
138 # take an arg as an array list and print it
142 return join(', ', @{$ref});
145 # take the arg as an array reference and print as a list of pairs
152 for ($i = 0; $i < @$ref; $i += 2) {
154 my $r2 = @$ref[$i+1];
157 chop $out; # remove last space
158 chop $out; # remove last comma
162 # print all the fields for a record according to privilege
164 # The prompt record is of the format '<priv>,<prompt>[,<action>'
165 # and is expanded by promptf above
169 my $self = shift; # is a dxchan
170 my $ref = shift; # is a thingy with field_prompt and fields methods defined
172 my @fields = $ref->fields;
175 foreach $field (sort {$ref->field_prompt($a) cmp $ref->field_prompt($b)} @fields) {
176 if (defined $ref->{$field}) {
177 my ($priv, $ans) = promptf($ref->field_prompt($field), $ref->{$field});
178 push @out, $ans if ($self->priv >= $priv);
184 # generate a regex from a shell type expression
185 # see 'perl cookbook' 6.9
189 $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
190 return '^' . $in . "\$";
193 # start an attempt at determining whether this string might be a callsign
197 return 1 if $call =~ /^[A-Z]+\d+[A-Z]+/;
198 return 1 if $call =~ /^\d+[A-Z]\d+[A-Z]+/;
202 # read in a file into a string and return it.
203 # the filename can be split into a dir and file and the
204 # file can be in upper or lower case.
205 # there can also be a suffix
208 my ($dir, $file, $suffix) = @_;
213 $fn = "$dir/$f.$suffix";
216 $fn = "$dir/$file.$suffix";
229 my $fh = new IO::File $fn;
239 # write out a file in the format required for reading
240 # in via readfilestr, it expects the same arguments
241 # and a reference to an object
251 confess('no object to write in writefilestr') unless $obj;
252 confess('object not a reference in writefilestr') unless ref $obj;
256 $fn = "$dir/$f.$suffix";
259 $fn = "$dir/$file.$suffix";
272 my $fh = new IO::File ">$fn";
274 my $dd = new Data::Dumper([ $obj ]);
278 # $fh->print(@_) if @_ > 0; # any header comments, lines etc
279 $fh->print($dd->Dumpxs);
284 # remove leading and trailing spaces from an input string
293 # check that a field only has callsign characters in it
296 return $_[0] =~ /^[A-Z0-9\-]+$/;
299 # check that a PC protocol field is valid text
302 return $_[0] =~ /^[\x09\x20-\xA8\xE0-\xEF]+$/;
305 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
308 return $_[0] =~ /^[01\*\-]+$/;
311 # check that a thing is a frequency
314 return $_[0] =~ /^[\d\.]+$/;
317 # check that a thing is just digits
320 return $_[0] =~ /^[\d]+$/;