1 |
################################# |
2 |
# |
3 |
# $Id: libp.pm,v 1.3 2002/07/19 18:13:50 cvsjoko Exp $ |
4 |
# |
5 |
# $Log: libp.pm,v $ |
6 |
# Revision 1.3 2002/07/19 18:13:50 cvsjoko |
7 |
# no message |
8 |
# |
9 |
# Revision 1.2 2002/06/27 02:14:22 cvsjoko |
10 |
# + stripHtml stripSpaces stripNewLines toReal |
11 |
# |
12 |
# Revision 1.1 2002/06/24 14:49:59 cvsjoko |
13 |
# + new |
14 |
# |
15 |
# |
16 |
################################# |
17 |
|
18 |
package libp; |
19 |
|
20 |
require Exporter; |
21 |
@ISA = qw( Exporter ); |
22 |
@EXPORT = qw( |
23 |
Dumper |
24 |
md5 md5_hex md5_base64 |
25 |
ParseDate UnixDate |
26 |
strftime |
27 |
stripHtml stripSpaces stripNewLines toReal trim |
28 |
croak |
29 |
array_getDifference |
30 |
getDirList |
31 |
); |
32 |
|
33 |
use strict; |
34 |
use warnings; |
35 |
|
36 |
use Data::Dumper; |
37 |
use Digest::MD5 qw(md5 md5_hex md5_base64); |
38 |
|
39 |
$main::TZ = 'GMT'; |
40 |
use Date::Manip; |
41 |
|
42 |
require LWP::UserAgent; |
43 |
use HTML::PullParser; |
44 |
|
45 |
# $now_string = strftime "%a %b %e %H:%M:%S %Y", localtime; |
46 |
# see "perldoc -f localtime" |
47 |
use POSIX qw(strftime); |
48 |
|
49 |
use Carp; |
50 |
|
51 |
use DirHandle; |
52 |
|
53 |
|
54 |
######################################## |
55 |
|
56 |
sub stripSpaces { |
57 |
my $text = shift; |
58 |
#print "text: $text", "\n"; |
59 |
#print "ord: ", ord(substr($text, 0, 1)), "\n"; |
60 |
$text =~ s/^\s*//g; |
61 |
$text =~ s/\s*$//g; |
62 |
return $text; |
63 |
} |
64 |
|
65 |
sub trim { |
66 |
my $string = shift; |
67 |
return stripSpaces($string); |
68 |
} |
69 |
|
70 |
sub stripNewLines { |
71 |
my $text = shift; |
72 |
#print "text: $text", "\n"; |
73 |
#print "ord: ", ord(substr($text, 0, 1)), "\n"; |
74 |
$text =~ s/\n//g; |
75 |
#$text =~ s/\s*$//g; |
76 |
return $text; |
77 |
} |
78 |
|
79 |
sub toReal { |
80 |
my $string = shift; |
81 |
$string =~ m/(\d+\.*\d+)/; |
82 |
my $real = $1; |
83 |
return $real; |
84 |
} |
85 |
|
86 |
sub stripHtml { |
87 |
my $html = shift; |
88 |
my $result = ''; |
89 |
#$html =~ s/<br>(.*)/ - ($1)/i; |
90 |
my $p = HTML::PullParser->new( |
91 |
doc => \$html, |
92 |
text => 'text', |
93 |
unbroken_text => 1, |
94 |
); |
95 |
while (my $token = $p->get_token()) { |
96 |
my $text = join('', @{$token}); |
97 |
$result .= $text; |
98 |
} |
99 |
#$result =~ s/ //g; |
100 |
return $result; |
101 |
} |
102 |
|
103 |
sub array_getRelations { |
104 |
my $a_ref = shift; |
105 |
my $b_ref = shift; |
106 |
my @a = @{$a_ref}; |
107 |
my @b = @{$b_ref}; |
108 |
|
109 |
my @isect = my @diff = my @union = (); |
110 |
my $e; |
111 |
my %count; |
112 |
|
113 |
foreach $e (@a, @b) { $count{$e}++ } |
114 |
|
115 |
foreach $e (keys %count) { |
116 |
push(@union, $e); |
117 |
push @{ $count{$e} == 2 ? \@isect : \@diff }, $e; |
118 |
} |
119 |
|
120 |
my $result = { |
121 |
union => \@union, |
122 |
isect => \@isect, |
123 |
diff => \@diff, |
124 |
}; |
125 |
|
126 |
} |
127 |
|
128 |
sub array_getDifference { |
129 |
my $res = array_getRelations(shift, shift); |
130 |
return $res->{diff}; |
131 |
} |
132 |
|
133 |
|
134 |
# ============================================= |
135 |
# "global" vars used in directory-recursive-parsing |
136 |
my $dirlist_buf; |
137 |
my @dirlist_path; |
138 |
my $dirlist_base; |
139 |
|
140 |
sub entry_callback { |
141 |
|
142 |
my $entry = shift; |
143 |
|
144 |
# CHECKS |
145 |
# dont't use this: |
146 |
if ($entry eq '.' || $entry eq '..') { return; } |
147 |
|
148 |
# PREPARE |
149 |
# prepare path to current entry |
150 |
my $cur_entry = join('/', @dirlist_path, $entry); |
151 |
# prepare path to current entry (absolute) |
152 |
my $cur_entry_abs = join('/', $dirlist_base, @dirlist_path, $entry); |
153 |
|
154 |
# ENTRY |
155 |
# add current entry to buffer |
156 |
$dirlist_buf .= $cur_entry . "\n"; |
157 |
|
158 |
# (SUB-)DIRECTORY |
159 |
# check if current entry is a (sub-)directory ... |
160 |
if (-d $cur_entry_abs) { |
161 |
push @dirlist_path, $cur_entry; |
162 |
# ... and parse this (recursion here!!!) |
163 |
iterate_path($cur_entry_abs); |
164 |
pop @dirlist_path; |
165 |
} |
166 |
} |
167 |
|
168 |
sub iterate_path { |
169 |
|
170 |
my $path = shift; |
171 |
|
172 |
# create new "DirHandle"-object |
173 |
my $d = new DirHandle $path; |
174 |
if (defined $d) { |
175 |
|
176 |
# iterate through all entries in $path ($d->read) and call out entry-handler on each entry |
177 |
while (defined(my $line = $d->read)) { |
178 |
entry_callback($line); |
179 |
} |
180 |
|
181 |
undef $d; |
182 |
} |
183 |
} |
184 |
|
185 |
sub getDirList { |
186 |
|
187 |
$dirlist_base = shift; |
188 |
|
189 |
# reset vars |
190 |
$dirlist_buf = ''; |
191 |
@dirlist_path = (); |
192 |
|
193 |
# start parsing file-structure |
194 |
iterate_path($dirlist_base); |
195 |
|
196 |
# return complete list of directory-content including files and subdirs |
197 |
# entries are newline (\n) - seperated |
198 |
return $dirlist_buf; |
199 |
|
200 |
} |
201 |
# ============================================= |
202 |
|
203 |
|
204 |
1; |