|
@@ -0,0 +1,127 @@
|
|
1
|
+package helper;
|
|
2
|
+
|
|
3
|
+## Author: Alois Mahdal at zxcvb cz
|
|
4
|
+# my private development helpers package
|
|
5
|
+
|
|
6
|
+# This program is free software: you can redistribute it and/or modify
|
|
7
|
+# it under the terms of the GNU General Public License as published by
|
|
8
|
+# the Free Software Foundation, either version 3 of the License, or
|
|
9
|
+# (at your option) any later version.
|
|
10
|
+
|
|
11
|
+# This program is distributed in the hope that it will be useful,
|
|
12
|
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+# GNU General Public License for more details.
|
|
15
|
+
|
|
16
|
+# You should have received a copy of the GNU General Public License
|
|
17
|
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
18
|
+
|
|
19
|
+use Carp;
|
|
20
|
+use YAML;
|
|
21
|
+use Data::Dumper;
|
|
22
|
+
|
|
23
|
+###
|
|
24
|
+# `find . -type f` plus a Windows version. And a front-end
|
|
25
|
+our $PLATFORM;
|
|
26
|
+$PLATFORM = (-d "c:\\windows" ? 'windows' : 'unix' );
|
|
27
|
+sub find_files {
|
|
28
|
+ my $path = shift;
|
|
29
|
+ if ($PLATFORM eq 'windows') {
|
|
30
|
+ return &helper::_find_files_windows($path);
|
|
31
|
+ } elsif ($PLATFORM eq 'unix') {
|
|
32
|
+ return &helper::_find_files_unix($path);
|
|
33
|
+ } else {
|
|
34
|
+ croak "unsupported platform!";
|
|
35
|
+ }
|
|
36
|
+}
|
|
37
|
+sub _find_files_windows {
|
|
38
|
+ my $path = shift;
|
|
39
|
+ $path =~ s|/|\\|g;
|
|
40
|
+ @files = `dir /b /s /a-d $path`;
|
|
41
|
+ chomp @files;
|
|
42
|
+ return \@files;
|
|
43
|
+}
|
|
44
|
+sub _find_files_unix {
|
|
45
|
+ my $path = shift;
|
|
46
|
+ @files = `find $path -type f`;
|
|
47
|
+ chomp @files;
|
|
48
|
+ return \@files;
|
|
49
|
+}
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+###
|
|
53
|
+# load fields from my favorite simple CSV-like format
|
|
54
|
+sub load_fields($) {
|
|
55
|
+ my $file = shift;
|
|
56
|
+ unless ($file) { carp "nothing to load"; return; }
|
|
57
|
+ $file =~ m|\.conf$| or carp "conf files should end with '.conf'";
|
|
58
|
+ open $fh, "<", $file or croak "cannot open $file: $!";
|
|
59
|
+
|
|
60
|
+ my @lines;
|
|
61
|
+ LINE: while (<$fh>) {
|
|
62
|
+ chomp;
|
|
63
|
+ next LINE if m|^\s*#|;
|
|
64
|
+ next LINE if m|^$|;
|
|
65
|
+ s|^\s+||; s|\s+$||;
|
|
66
|
+ s|\s*;\s*|;|g;
|
|
67
|
+ my @fields = split ';';
|
|
68
|
+ next LINE unless @fields;
|
|
69
|
+ push @lines, \@fields;
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ close $fh or carp "cannot close $file: $!";
|
|
73
|
+ return \@lines;
|
|
74
|
+}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+###
|
|
78
|
+# simple yet effective shufffle
|
|
79
|
+sub fisher_yates_shuffle {
|
|
80
|
+ my $array = shift;
|
|
81
|
+ my $i;
|
|
82
|
+ for ($i = @$array; --$i; ) {
|
|
83
|
+ my $j = int rand ($i+1);
|
|
84
|
+ next if $i == $j;
|
|
85
|
+ @$array[$i,$j] = @$array[$j,$i];
|
|
86
|
+ }
|
|
87
|
+}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+###
|
|
91
|
+# dmup anything (by reference)
|
|
92
|
+
|
|
93
|
+my $DUMPS = "./dumps";
|
|
94
|
+my $DEFAULT_FORM = "yaml";
|
|
95
|
+
|
|
96
|
+sub dmupp($@) { _dmup("perl", @_) }
|
|
97
|
+sub dmupy($@) { _dmup("yaml", @_) }
|
|
98
|
+sub dmup($@) { _dmup($DEFAULT_FORM, @_) }
|
|
99
|
+
|
|
100
|
+sub _dmup($$@) {
|
|
101
|
+ my $form = shift;
|
|
102
|
+ my $name = ( $_[1] ? shift : "" );
|
|
103
|
+ mkdir $DUMPS unless -d $DUMPS;
|
|
104
|
+ open my $df, ">", "$DUMPS/$name.dmp";
|
|
105
|
+ if ($form eq "yaml") {
|
|
106
|
+ print $df YAML::Dump(@_);
|
|
107
|
+ } elsif ($form eq "perl") {
|
|
108
|
+ print $df Dumper(@_);
|
|
109
|
+ }
|
|
110
|
+ close $df;
|
|
111
|
+}
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+###
|
|
115
|
+# is that @ARRAY shuffled?
|
|
116
|
+sub is_shuffled {
|
|
117
|
+ my $array = shift;
|
|
118
|
+ my $sorted = [ sort @{$array} ];
|
|
119
|
+ my ($n, $i) = (0, 0);
|
|
120
|
+ foreach (@{$array}) {
|
|
121
|
+ $n++ unless ${$array}[$i] eq ${$sorted}[$i];
|
|
122
|
+ $i++;
|
|
123
|
+ }
|
|
124
|
+ return $n;
|
|
125
|
+}
|
|
126
|
+
|
|
127
|
+1;
|