find problem in split in print_all_fields
[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, 2;
187                                 my $l = (length $p) + 2;
188                                 my $al = 79 - $l;
189                                 my $bit;
190                                 while (length $a > $al ) {
191                                         ($bit, $a) = unpack "A$al A*", $a;
192                                         push @tmp, "$p: $bit";
193                                         $p = ' ' x ($l - 2);
194                                 }
195                                 push @tmp, "$p: $a" if length $a;
196                         } else {
197                                 push @tmp, $ans;
198                         }
199                         push @out, @tmp if ($self->priv >= $priv);
200                 }
201         }
202         return @out;
203 }
204
205 # generate a regex from a shell type expression 
206 # see 'perl cookbook' 6.9
207 sub shellregex
208 {
209         my $in = shift;
210         $in =~ s{(.)} { $patmap{$1} || "\Q$1" }ge;
211         return '^' . $in . "\$";
212 }
213
214 # read in a file into a string and return it. 
215 # the filename can be split into a dir and file and the 
216 # file can be in upper or lower case.
217 # there can also be a suffix
218 sub readfilestr
219 {
220         my ($dir, $file, $suffix) = @_;
221         my $fn;
222         my $f;
223         if ($suffix) {
224                 $f = uc $file;
225                 $fn = "$dir/$f.$suffix";
226                 unless (-e $fn) {
227                         $f = lc $file;
228                         $fn = "$dir/$file.$suffix";
229                 }
230         } elsif ($file) {
231                 $f = uc $file;
232                 $fn = "$dir/$file";
233                 unless (-e $fn) {
234                         $f = lc $file;
235                         $fn = "$dir/$file";
236                 }
237         } else {
238                 $fn = $dir;
239         }
240
241         my $fh = new IO::File $fn;
242         my $s = undef;
243         if ($fh) {
244                 local $/ = undef;
245                 $s = <$fh>;
246                 $fh->close;
247         }
248         return $s;
249 }
250
251 # write out a file in the format required for reading
252 # in via readfilestr, it expects the same arguments 
253 # and a reference to an object
254 sub writefilestr
255 {
256         my $dir = shift;
257         my $file = shift;
258         my $suffix = shift;
259         my $obj = shift;
260         my $fn;
261         my $f;
262         
263         confess('no object to write in writefilestr') unless $obj;
264         confess('object not a reference in writefilestr') unless ref $obj;
265         
266         if ($suffix) {
267                 $f = uc $file;
268                 $fn = "$dir/$f.$suffix";
269                 unless (-e $fn) {
270                         $f = lc $file;
271                         $fn = "$dir/$file.$suffix";
272                 }
273         } elsif ($file) {
274                 $f = uc $file;
275                 $fn = "$dir/$file";
276                 unless (-e $fn) {
277                         $f = lc $file;
278                         $fn = "$dir/$file";
279                 }
280         } else {
281                 $fn = $dir;
282         }
283
284         my $fh = new IO::File ">$fn";
285         if ($fh) {
286                 my $dd = new Data::Dumper([ $obj ]);
287                 $dd->Indent(1);
288                 $dd->Terse(1);
289                 $dd->Quotekeys(0);
290                 #       $fh->print(@_) if @_ > 0;     # any header comments, lines etc
291                 $fh->print($dd->Dumpxs);
292                 $fh->close;
293         }
294 }
295
296 # remove leading and trailing spaces from an input string
297 sub unpad
298 {
299         my $s = shift;
300         $s =~ s/\s+$//;
301         $s =~ s/^\s+//;
302         return $s;
303 }
304
305 # check that a field only has callsign characters in it
306 sub is_callsign
307 {
308         return $_[0] =~ /^(?:[A-Z]{1,2}\d+|\d[A-Z]\d+)[A-Z0-9\/\-]+$/;
309 }
310
311 # check that a PC protocol field is valid text
312 sub is_pctext
313 {
314         return $_[0] =~ /^[\x09\x20-\xFF]+$/;
315 }
316
317 # check that a PC prot flag is fairly valid (doesn't check the difference between 1/0 and */-)
318 sub is_pcflag
319 {
320         return $_[0] =~ /^[01\*\-]+$/;
321 }
322
323 # check that a thing is a frequency
324 sub is_freq
325 {
326         return $_[0] =~ /^[\d\.]+$/;
327 }
328
329 # check that a thing is just digits
330 sub is_digits
331 {
332         return $_[0] =~ /^[\d]+$/;
333 }
334
335 # insert an item into a list if it isn't already there returns 1 if there 0 if not
336 sub insertitem
337 {
338         my $list = shift;
339         my $item = shift;
340         
341         return 1 if grep {$_ eq $item } @$list;
342         push @$list, $item;
343         return 0;
344 }
345
346 # delete an item from a list if it is there returns no deleted 
347 sub deleteitem
348 {
349         my $list = shift;
350         my $item = shift;
351         my $n = @$list;
352         
353         @$list = grep {$_ ne $item } @$list;
354         return $n - @$list;
355 }