checking now works a bit better
[spider.git] / perl / DXUtil.pm
1 #
2 # various utilities which are exported globally
3 #
4 # Copyright (c) 1998 - Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 package DXUtil;
10
11 use Date::Parse;
12 use IO::File;
13 use Data::Dumper;
14
15 require Exporter;
16 @ISA = qw(Exporter);
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
21             );
22
23 @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
24 %patmap = (
25                    '*' => '.*',
26                    '?' => '.',
27                    '[' => '[',
28                    ']' => ']'
29 );
30
31 # a full time for logging and other purposes
32 sub atime
33 {
34         my $t = shift;
35         my ($sec,$min,$hour,$mday,$mon,$year) = gmtime((defined $t) ? $t : time);
36         $year += 1900;
37         my $buf = sprintf "%02d%s%04d\@%02d:%02d:%02d", $mday, $month[$mon], $year, $hour, $min, $sec;
38         return $buf;
39 }
40
41 # get a zulu time in cluster format (2300Z)
42 sub ztime
43 {
44         my $t = shift;
45         $t = defined $t ? $t : time;
46         my $dst = shift;
47         my ($sec,$min,$hour) = $dst ? localtime($t): gmtime($t);
48         my $buf = sprintf "%02d%02d%s", $hour, $min, ($dst) ? '' : 'Z';
49         return $buf;
50 }
51
52 # get a cluster format date (23-Jun-1998)
53 sub cldate
54 {
55         my $t = shift;
56         $t = defined $t ? $t : time;
57         my $dst = shift;
58         my ($sec,$min,$hour,$mday,$mon,$year) = $dst ? localtime($t) : gmtime($t);
59         $year += 1900;
60         my $buf = sprintf "%2d-%s-%04d", $mday, $month[$mon], $year;
61         return $buf;
62 }
63
64 # return a cluster style date time
65 sub cldatetime
66 {
67         my $t = shift;
68         my $dst = shift;
69         my $date = cldate($t, $dst);
70         my $time = ztime($t, $dst);
71         return "$date $time";
72 }
73
74 # return a unix date from a cluster date and time
75 sub cltounix
76 {
77         my $date = shift;
78         my $time = shift;
79         my ($thisyear) = (gmtime)[5] + 1900;
80
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;
84         $date = "$1 $2 $3";
85         return 0 unless $time =~ /^([012]\d)([012345]\d)Z$/;
86         $time = "$1:$2 +0000";
87         my $r = str2time("$date $time");
88         return $r unless $r;
89         return $r == -1 ? undef : $r;
90 }
91
92 # turn a latitude in degrees into a string
93 sub slat
94 {
95         my $n = shift;
96         my ($deg, $min, $let);
97         $let = $n >= 0 ? 'N' : 'S';
98         $n = abs $n;
99         $deg = int $n;
100         $min = int ((($n - $deg) * 60) + 0.5);
101         return "$deg $min $let";
102 }
103
104 # turn a longitude in degrees into a string
105 sub slong
106 {
107         my $n = shift;
108         my ($deg, $min, $let);
109         $let = $n >= 0 ? 'E' : 'W';
110         $n = abs $n;
111         $deg = int $n;
112         $min = int ((($n - $deg) * 60) + 0.5);
113         return "$deg $min $let";
114 }
115
116 # turn a true into 'yes' and false into 'no'
117 sub yesno
118 {
119         my $n = shift;
120         return $n ? $main::yes : $main::no;
121 }
122
123 # format a prompt with its current value and return it with its privilege
124 sub promptf
125 {
126         my ($line, $value) = @_;
127         my ($priv, $prompt, $action) = split ',', $line;
128
129         # if there is an action treat it as a subroutine and replace $value
130         if ($action) {
131                 my $q = qq{\$value = $action(\$value)};
132                 eval $q;
133         }
134         $prompt = sprintf "%15s: %s", $prompt, $value;
135         return ($priv, $prompt);
136 }
137
138 # take an arg as an array list and print it
139 sub parray
140 {
141         my $ref = shift;
142         return join(', ', @{$ref});
143 }
144
145 # take the arg as an array reference and print as a list of pairs
146 sub parraypairs
147 {
148         my $ref = shift;
149         my $i;
150         my $out;
151   
152         for ($i = 0; $i < @$ref; $i += 2) {
153                 my $r1 = @$ref[$i];
154                 my $r2 = @$ref[$i+1];
155                 $out .= "$r1-$r2, ";
156         }
157         chop $out;                                      # remove last space
158         chop $out;                                      # remove last comma
159         return $out;
160 }
161
162 # print all the fields for a record according to privilege
163 #
164 # The prompt record is of the format '<priv>,<prompt>[,<action>'
165 # and is expanded by promptf above
166 #
167 sub print_all_fields
168 {
169         my $self = shift;                       # is a dxchan
170         my $ref = shift;                        # is a thingy with field_prompt and fields methods defined
171         my @out;
172         my @fields = $ref->fields;
173         my $field;
174
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);
179                 }
180         }
181         return @out;
182 }
183
184 # generate a regex from a shell type expression 
185 # see 'perl cookbook' 6.9
186 sub shellregex
187 {
188         my $in = shift;
189         $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
190         return '^' . $in . "\$";
191 }
192
193 # start an attempt at determining whether this string might be a callsign
194 sub iscallsign
195 {
196         my $call = uc shift;
197         return 1 if $call =~ /^[A-Z]+\d+[A-Z]+/;
198         return 1 if $call =~ /^\d+[A-Z]\d+[A-Z]+/;
199         return undef;
200 }
201
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
206 sub readfilestr
207 {
208         my ($dir, $file, $suffix) = @_;
209         my $fn;
210         my $f;
211         if ($suffix) {
212                 $f = uc $file;
213                 $fn = "$dir/$f.$suffix";
214                 unless (-e $fn) {
215                         $f = lc $file;
216                         $fn = "$dir/$file.$suffix";
217                 }
218         } elsif ($file) {
219                 $f = uc $file;
220                 $fn = "$dir/$file";
221                 unless (-e $fn) {
222                         $f = lc $file;
223                         $fn = "$dir/$file";
224                 }
225         } else {
226                 $fn = $dir;
227         }
228
229         my $fh = new IO::File $fn;
230         my $s = undef;
231         if ($fh) {
232                 local $/ = undef;
233                 $s = <$fh>;
234                 $fh->close;
235         }
236         return $s;
237 }
238
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
242 sub writefilestr
243 {
244         my $dir = shift;
245         my $file = shift;
246         my $suffix = shift;
247         my $obj = shift;
248         my $fn;
249         my $f;
250         
251         confess('no object to write in writefilestr') unless $obj;
252         confess('object not a reference in writefilestr') unless ref $obj;
253         
254         if ($suffix) {
255                 $f = uc $file;
256                 $fn = "$dir/$f.$suffix";
257                 unless (-e $fn) {
258                         $f = lc $file;
259                         $fn = "$dir/$file.$suffix";
260                 }
261         } elsif ($file) {
262                 $f = uc $file;
263                 $fn = "$dir/$file";
264                 unless (-e $fn) {
265                         $f = lc $file;
266                         $fn = "$dir/$file";
267                 }
268         } else {
269                 $fn = $dir;
270         }
271
272         my $fh = new IO::File ">$fn";
273         if ($fh) {
274                 my $dd = new Data::Dumper([ $obj ]);
275                 $dd->Indent(1);
276                 $dd->Terse(1);
277                 $dd->Quotekeys(0);
278                 #       $fh->print(@_) if @_ > 0;     # any header comments, lines etc
279                 $fh->print($dd->Dumpxs);
280                 $fh->close;
281         }
282 }
283
284 # remove leading and trailing spaces from an input string
285 sub unpad
286 {
287         my $s = shift;
288         $s =~ s/\s+$//;
289         $s =~ s/^\s+//;
290         return $s;
291 }
292
293 # check that a field only has callsign characters in it
294 sub is_callsign
295 {
296         return $_[0] =~ /^[A-Z0-9\-]+$/;
297 }
298
299 # check that a PC protocol field is valid text
300 sub is_pctext
301 {
302         return $_[0] =~ /^[\x09\x20-\xA8\xE0-\xEF]+$/;
303 }
304
305 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
306 sub is_pcflag
307 {
308         return $_[0] =~ /^[01\*\-]+$/;
309 }
310
311 # check that a thing is a frequency
312 sub is_freq
313 {
314         return $_[0] =~ /^[\d\.]+$/;
315 }
316
317 # check that a thing is just digits
318 sub is_digits
319 {
320         return $_[0] =~ /^[\d]+$/;
321 }