| 1 |
package util ; |
| 2 |
|
| 3 |
use strict; |
| 4 |
|
| 5 |
|
| 6 |
package main ; |
| 7 |
|
| 8 |
use strict ; |
| 9 |
use BerkeleyDB ; |
| 10 |
use File::Path qw(rmtree); |
| 11 |
use vars qw(%DB_errors $FA) ; |
| 12 |
|
| 13 |
use vars qw( @StdErrFile ); |
| 14 |
|
| 15 |
@StdErrFile = ( -ErrFile => *STDERR, -ErrPrefix => "\n# " ) ; |
| 16 |
|
| 17 |
$| = 1; |
| 18 |
|
| 19 |
%DB_errors = ( |
| 20 |
'DB_INCOMPLETE' => "DB_INCOMPLETE: Sync was unable to complete", |
| 21 |
'DB_KEYEMPTY' => "DB_KEYEMPTY: Non-existent key/data pair", |
| 22 |
'DB_KEYEXIST' => "DB_KEYEXIST: Key/data pair already exists", |
| 23 |
'DB_LOCK_DEADLOCK' => "DB_LOCK_DEADLOCK: Locker killed to resolve a deadlock", |
| 24 |
'DB_LOCK_NOTGRANTED' => "DB_LOCK_NOTGRANTED: Lock not granted", |
| 25 |
'DB_NOTFOUND' => "DB_NOTFOUND: No matching key/data pair found", |
| 26 |
'DB_OLD_VERSION' => "DB_OLDVERSION: Database requires a version upgrade", |
| 27 |
'DB_RUNRECOVERY' => "DB_RUNRECOVERY: Fatal error, run database recovery", |
| 28 |
) ; |
| 29 |
|
| 30 |
# full tied array support started in Perl 5.004_57 |
| 31 |
# just double check. |
| 32 |
$FA = 0 ; |
| 33 |
{ |
| 34 |
sub try::TIEARRAY { bless [], "try" } |
| 35 |
sub try::FETCHSIZE { $FA = 1 } |
| 36 |
my @a ; |
| 37 |
tie @a, 'try' ; |
| 38 |
my $a = @a ; |
| 39 |
} |
| 40 |
|
| 41 |
{ |
| 42 |
package LexFile ; |
| 43 |
|
| 44 |
use vars qw( $basename @files ) ; |
| 45 |
$basename = "db0000" ; |
| 46 |
|
| 47 |
sub new |
| 48 |
{ |
| 49 |
my $self = shift ; |
| 50 |
#my @files = () ; |
| 51 |
foreach (@_) |
| 52 |
{ |
| 53 |
$_ = $basename ; |
| 54 |
1 while unlink $basename ; |
| 55 |
push @files, $basename ; |
| 56 |
++ $basename ; |
| 57 |
} |
| 58 |
bless [ @files ], $self ; |
| 59 |
} |
| 60 |
|
| 61 |
sub DESTROY |
| 62 |
{ |
| 63 |
my $self = shift ; |
| 64 |
chmod 0777, @{ $self } ; |
| 65 |
for (@$self) { 1 while unlink $_ } ; |
| 66 |
} |
| 67 |
|
| 68 |
END |
| 69 |
{ |
| 70 |
foreach (@files) { unlink $_ } |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
{ |
| 76 |
package LexDir ; |
| 77 |
|
| 78 |
use File::Path qw(rmtree); |
| 79 |
|
| 80 |
use vars qw( $basename %dirs ) ; |
| 81 |
|
| 82 |
sub new |
| 83 |
{ |
| 84 |
my $self = shift ; |
| 85 |
my $dir = shift ; |
| 86 |
|
| 87 |
rmtree $dir if -e $dir ; |
| 88 |
|
| 89 |
mkdir $dir, 0777 or return undef ; |
| 90 |
|
| 91 |
return bless [ $dir ], $self ; |
| 92 |
} |
| 93 |
|
| 94 |
sub DESTROY |
| 95 |
{ |
| 96 |
my $self = shift ; |
| 97 |
my $dir = $self->[0]; |
| 98 |
#rmtree $dir; |
| 99 |
$dirs{$dir} ++ ; |
| 100 |
} |
| 101 |
|
| 102 |
END |
| 103 |
{ |
| 104 |
foreach (keys %dirs) { |
| 105 |
rmtree $_ if -d $_ ; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
{ |
| 112 |
package Redirect ; |
| 113 |
use Symbol ; |
| 114 |
|
| 115 |
sub new |
| 116 |
{ |
| 117 |
my $class = shift ; |
| 118 |
my $filename = shift ; |
| 119 |
my $fh = gensym ; |
| 120 |
open ($fh, ">$filename") || die "Cannot open $filename: $!" ; |
| 121 |
my $real_stdout = select($fh) ; |
| 122 |
return bless [$fh, $real_stdout ] ; |
| 123 |
|
| 124 |
} |
| 125 |
sub DESTROY |
| 126 |
{ |
| 127 |
my $self = shift ; |
| 128 |
close $self->[0] ; |
| 129 |
select($self->[1]) ; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
sub normalise |
| 134 |
{ |
| 135 |
my $data = shift ; |
| 136 |
$data =~ s#\r\n#\n#g |
| 137 |
if $^O eq 'cygwin' ; |
| 138 |
|
| 139 |
return $data ; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
sub docat |
| 144 |
{ |
| 145 |
my $file = shift; |
| 146 |
local $/ = undef; |
| 147 |
open(CAT,$file) || die "Cannot open $file:$!"; |
| 148 |
my $result = <CAT>; |
| 149 |
close(CAT); |
| 150 |
$result = normalise($result); |
| 151 |
return $result; |
| 152 |
} |
| 153 |
|
| 154 |
sub docat_del |
| 155 |
{ |
| 156 |
my $file = shift; |
| 157 |
local $/ = undef; |
| 158 |
open(CAT,$file) || die "Cannot open $file: $!"; |
| 159 |
my $result = <CAT> || "" ; |
| 160 |
close(CAT); |
| 161 |
unlink $file ; |
| 162 |
$result = normalise($result); |
| 163 |
return $result; |
| 164 |
} |
| 165 |
|
| 166 |
sub docat_del_sort |
| 167 |
{ |
| 168 |
my $file = shift; |
| 169 |
open(CAT,$file) || die "Cannot open $file: $!"; |
| 170 |
my @got = <CAT>; |
| 171 |
@got = sort @got; |
| 172 |
|
| 173 |
my $result = join('', @got) || "" ; |
| 174 |
close(CAT); |
| 175 |
unlink $file ; |
| 176 |
$result = normalise($result); |
| 177 |
return $result; |
| 178 |
} |
| 179 |
|
| 180 |
sub readFile |
| 181 |
{ |
| 182 |
my $file = shift; |
| 183 |
local $/ = undef; |
| 184 |
open(RD,$file) || die "Cannot open $file:$!"; |
| 185 |
my $result = <RD>; |
| 186 |
close(RD); |
| 187 |
return $result; |
| 188 |
} |
| 189 |
|
| 190 |
sub writeFile |
| 191 |
{ |
| 192 |
my $name = shift ; |
| 193 |
open(FH, ">$name") or return 0 ; |
| 194 |
print FH @_ ; |
| 195 |
close FH ; |
| 196 |
return 1 ; |
| 197 |
} |
| 198 |
|
| 199 |
sub touch |
| 200 |
{ |
| 201 |
my $file = shift ; |
| 202 |
open(CAT,">$file") || die "Cannot open $file:$!"; |
| 203 |
close(CAT); |
| 204 |
} |
| 205 |
|
| 206 |
sub joiner |
| 207 |
{ |
| 208 |
my $db = shift ; |
| 209 |
my $sep = shift ; |
| 210 |
my ($k, $v) = (0, "") ; |
| 211 |
my @data = () ; |
| 212 |
|
| 213 |
my $cursor = $db->db_cursor() or return () ; |
| 214 |
for ( my $status = $cursor->c_get($k, $v, DB_FIRST) ; |
| 215 |
$status == 0 ; |
| 216 |
$status = $cursor->c_get($k, $v, DB_NEXT)) { |
| 217 |
push @data, $v ; |
| 218 |
} |
| 219 |
|
| 220 |
(scalar(@data), join($sep, @data)) ; |
| 221 |
} |
| 222 |
|
| 223 |
sub joinkeys |
| 224 |
{ |
| 225 |
my $db = shift ; |
| 226 |
my $sep = shift || " " ; |
| 227 |
my ($k, $v) = (0, "") ; |
| 228 |
my @data = () ; |
| 229 |
|
| 230 |
my $cursor = $db->db_cursor() or return () ; |
| 231 |
for ( my $status = $cursor->c_get($k, $v, DB_FIRST) ; |
| 232 |
$status == 0 ; |
| 233 |
$status = $cursor->c_get($k, $v, DB_NEXT)) { |
| 234 |
push @data, $k ; |
| 235 |
} |
| 236 |
|
| 237 |
return join($sep, @data) ; |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
sub dumpdb |
| 242 |
{ |
| 243 |
my $db = shift ; |
| 244 |
my $sep = shift || " " ; |
| 245 |
my ($k, $v) = (0, "") ; |
| 246 |
my @data = () ; |
| 247 |
|
| 248 |
my $cursor = $db->db_cursor() or return () ; |
| 249 |
for ( my $status = $cursor->c_get($k, $v, DB_FIRST) ; |
| 250 |
$status == 0 ; |
| 251 |
$status = $cursor->c_get($k, $v, DB_NEXT)) { |
| 252 |
print " [$k][$v]\n" ; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
sub countRecords |
| 259 |
{ |
| 260 |
my $db = shift ; |
| 261 |
my ($k, $v) = (0,0) ; |
| 262 |
my ($count) = 0 ; |
| 263 |
my ($cursor) = $db->db_cursor() ; |
| 264 |
#for ($status = $cursor->c_get($k, $v, DB_FIRST) ; |
| 265 |
# $status == 0 ; |
| 266 |
# $status = $cursor->c_get($k, $v, DB_NEXT) ) |
| 267 |
while ($cursor->c_get($k, $v, DB_NEXT) == 0) |
| 268 |
{ ++ $count } |
| 269 |
|
| 270 |
return $count ; |
| 271 |
} |
| 272 |
|
| 273 |
sub addData |
| 274 |
{ |
| 275 |
my $db = shift ; |
| 276 |
my @data = @_ ; |
| 277 |
die "addData odd data\n" if @data % 2 != 0 ; |
| 278 |
my ($k, $v) ; |
| 279 |
my $ret = 0 ; |
| 280 |
while (@data) { |
| 281 |
$k = shift @data ; |
| 282 |
$v = shift @data ; |
| 283 |
$ret += $db->db_put($k, $v) ; |
| 284 |
} |
| 285 |
|
| 286 |
return ($ret == 0) ; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
# These two subs lifted directly from MLDBM.pm |
| 292 |
# |
| 293 |
sub _compare { |
| 294 |
use vars qw(%compared); |
| 295 |
local %compared; |
| 296 |
return _cmp(@_); |
| 297 |
} |
| 298 |
|
| 299 |
sub _cmp { |
| 300 |
my($a, $b) = @_; |
| 301 |
|
| 302 |
# catch circular loops |
| 303 |
return(1) if $compared{$a.'&*&*&*&*&*'.$b}++; |
| 304 |
# print "$a $b\n"; |
| 305 |
# print &Data::Dumper::Dumper($a, $b); |
| 306 |
|
| 307 |
if(ref($a) and ref($a) eq ref($b)) { |
| 308 |
if(eval { @$a }) { |
| 309 |
# print "HERE ".@$a." ".@$b."\n"; |
| 310 |
@$a == @$b or return 0; |
| 311 |
# print @$a, ' ', @$b, "\n"; |
| 312 |
# print "HERE2\n"; |
| 313 |
|
| 314 |
for(0..@$a-1) { |
| 315 |
&_cmp($a->[$_], $b->[$_]) or return 0; |
| 316 |
} |
| 317 |
} elsif(eval { %$a }) { |
| 318 |
keys %$a == keys %$b or return 0; |
| 319 |
for (keys %$a) { |
| 320 |
&_cmp($a->{$_}, $b->{$_}) or return 0; |
| 321 |
} |
| 322 |
} elsif(eval { $$a }) { |
| 323 |
&_cmp($$a, $$b) or return 0; |
| 324 |
} else { |
| 325 |
die("data $a $b not handled"); |
| 326 |
} |
| 327 |
return 1; |
| 328 |
} elsif(! ref($a) and ! ref($b)) { |
| 329 |
return ($a eq $b); |
| 330 |
} else { |
| 331 |
return 0; |
| 332 |
} |
| 333 |
|
| 334 |
} |
| 335 |
|
| 336 |
sub fillout |
| 337 |
{ |
| 338 |
my $var = shift ; |
| 339 |
my $length = shift ; |
| 340 |
my $pad = shift || " " ; |
| 341 |
my $template = $pad x $length ; |
| 342 |
substr($template, 0, length($var)) = $var ; |
| 343 |
return $template ; |
| 344 |
} |
| 345 |
|
| 346 |
sub title |
| 347 |
{ |
| 348 |
#diag "" ; |
| 349 |
ok(1, $_[0]) ; |
| 350 |
#diag "" ; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
1; |