/[cvs]/nfo/perl/libs/libp.pm
ViewVC logotype

Contents of /nfo/perl/libs/libp.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations)
Sun Nov 17 07:18:59 2002 UTC (21 years, 11 months ago) by joko
Branch: MAIN
Changes since 1.5: +18 -1 lines
+ sub deep_copy

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed