remove references to iscallsign
[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 unpad is_callsign
20                          is_freq is_digits is_pctext is_pcflag insertitem deleteitem
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         } elsif (ref $value) {
134                 my $dd = new Data::Dumper([$value]);
135                 $dd->Indent(0);
136                 $dd->Terse(1);
137                 $dd->Quotekeys(0);
138                 $value = $dd->Dumpxs;
139         }
140         $prompt = sprintf "%15s: %s", $prompt, $value;
141         return ($priv, $prompt);
142 }
143
144 # take an arg as an array list and print it
145 sub parray
146 {
147         my $ref = shift;
148         return join(', ', @{$ref});
149 }
150
151 # take the arg as an array reference and print as a list of pairs
152 sub parraypairs
153 {
154         my $ref = shift;
155         my $i;
156         my $out;
157   
158         for ($i = 0; $i < @$ref; $i += 2) {
159                 my $r1 = @$ref[$i];
160                 my $r2 = @$ref[$i+1];
161                 $out .= "$r1-$r2, ";
162         }
163         chop $out;                                      # remove last space
164         chop $out;                                      # remove last comma
165         return $out;
166 }
167
168 # print all the fields for a record according to privilege
169 #
170 # The prompt record is of the format '<priv>,<prompt>[,<action>'
171 # and is expanded by promptf above
172 #
173 sub print_all_fields
174 {
175         my $self = shift;                       # is a dxchan
176         my $ref = shift;                        # is a thingy with field_prompt and fields methods defined
177         my @out;
178         my @fields = $ref->fields;
179         my $field;
180
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});
184                         my @tmp;
185                         if (length $ans > 79) {
186                                 my ($p, $a) = split /: /, $ans;
187                                 my $l = (length $p) + 2;
188                                 my $al = 79 - $l;
189                                 while (length $a > $al ) {
190                                         $a =~ s/^(.{$al})//;
191                                         push @tmp, "$p: $1";
192                                         $p = ' ' x ($l - 2);
193                                 }
194                                 push @tmp, "$p: $a" if length $a;
195                         } else {
196                                 push @tmp, $ans;
197                         }
198                         push @out, @tmp if ($self->priv >= $priv);
199                 }
200         }
201         return @out;
202 }
203
204 # generate a regex from a shell type expression 
205 # see 'perl cookbook' 6.9
206 sub shellregex
207 {
208         my $in = shift;
209         $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
210         return '^' . $in . "\$";
211 }
212
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
217 sub readfilestr
218 {
219         my ($dir, $file, $suffix) = @_;
220         my $fn;
221         my $f;
222         if ($suffix) {
223                 $f = uc $file;
224                 $fn = "$dir/$f.$suffix";
225                 unless (-e $fn) {
226                         $f = lc $file;
227                         $fn = "$dir/$file.$suffix";
228                 }
229         } elsif ($file) {
230                 $f = uc $file;
231                 $fn = "$dir/$file";
232                 unless (-e $fn) {
233                         $f = lc $file;
234                         $fn = "$dir/$file";
235                 }
236         } else {
237                 $fn = $dir;
238         }
239
240         my $fh = new IO::File $fn;
241         my $s = undef;
242         if ($fh) {
243                 local $/ = undef;
244                 $s = <$fh>;
245                 $fh->close;
246         }
247         return $s;
248 }
249
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
253 sub writefilestr
254 {
255         my $dir = shift;
256         my $file = shift;
257         my $suffix = shift;
258         my $obj = shift;
259         my $fn;
260         my $f;
261         
262         confess('no object to write in writefilestr') unless $obj;
263         confess('object not a reference in writefilestr') unless ref $obj;
264         
265         if ($suffix) {
266                 $f = uc $file;
267                 $fn = "$dir/$f.$suffix";
268                 unless (-e $fn) {
269                         $f = lc $file;
270                         $fn = "$dir/$file.$suffix";
271                 }
272         } elsif ($file) {
273                 $f = uc $file;
274                 $fn = "$dir/$file";
275                 unless (-e $fn) {
276                         $f = lc $file;
277                         $fn = "$dir/$file";
278                 }
279         } else {
280                 $fn = $dir;
281         }
282
283         my $fh = new IO::File ">$fn";
284         if ($fh) {
285                 my $dd = new Data::Dumper([ $obj ]);
286                 $dd->Indent(1);
287                 $dd->Terse(1);
288                 $dd->Quotekeys(0);
289                 #       $fh->print(@_) if @_ > 0;     # any header comments, lines etc
290                 $fh->print($dd->Dumpxs);
291                 $fh->close;
292         }
293 }
294
295 # remove leading and trailing spaces from an input string
296 sub unpad
297 {
298         my $s = shift;
299         $s =~ s/\s+$//;
300         $s =~ s/^\s+//;
301         return $s;
302 }
303
304 # check that a field only has callsign characters in it
305 sub is_callsign
306 {
307         return $_[0] =~ /^(?:[A-Z]{1,2}\d+|\d[A-Z]\d+)[A-Z0-9\/\-]+$/;
308 }
309
310 # check that a PC protocol field is valid text
311 sub is_pctext
312 {
313         return $_[0] =~ /^[\x09\x20-\xFF]+$/;
314 }
315
316 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
317 sub is_pcflag
318 {
319         return $_[0] =~ /^[01\*\-]+$/;
320 }
321
322 # check that a thing is a frequency
323 sub is_freq
324 {
325         return $_[0] =~ /^[\d\.]+$/;
326 }
327
328 # check that a thing is just digits
329 sub is_digits
330 {
331         return $_[0] =~ /^[\d]+$/;
332 }
333
334 # insert an item into a list if it isn't already there returns 1 if there 0 if not
335 sub insertitem
336 {
337         my $list = shift;
338         my $item = shift;
339         
340         return 1 if grep {$_ eq $item } @$list;
341         push @$list, $item;
342         return 0;
343 }
344
345 # delete an item from a list if it is there returns no deleted 
346 sub deleteitem
347 {
348         my $list = shift;
349         my $item = shift;
350         my $n = @$list;
351         
352         @$list = grep {$_ ne $item } @$list;
353         return $n - @$list;
354 }