2002-03-10 00:29:33 +01:00
|
|
|
#
|
|
|
|
# Copyright 1999, 2000, 2001 Patrik Stridvall
|
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2002-03-10 00:29:33 +01:00
|
|
|
#
|
|
|
|
|
2001-06-13 21:38:29 +02:00
|
|
|
package util;
|
|
|
|
|
|
|
|
use strict;
|
2020-07-22 00:10:10 +02:00
|
|
|
use warnings 'all';
|
2001-06-13 21:38:29 +02:00
|
|
|
|
|
|
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
|
|
|
|
require Exporter;
|
|
|
|
|
|
|
|
@ISA = qw(Exporter);
|
2001-07-11 19:27:45 +02:00
|
|
|
@EXPORT = qw(
|
2004-10-26 02:12:21 +02:00
|
|
|
append_file edit_file read_file replace_file
|
|
|
|
normalize_set is_subset
|
2001-07-11 19:27:45 +02:00
|
|
|
);
|
2001-06-13 21:38:29 +02:00
|
|
|
@EXPORT_OK = qw();
|
|
|
|
%EXPORT_TAGS = ();
|
|
|
|
|
2002-10-29 22:25:09 +01:00
|
|
|
########################################################################
|
|
|
|
# _compare_files
|
|
|
|
|
2004-10-26 02:12:21 +02:00
|
|
|
sub _compare_files($$) {
|
2002-10-29 22:25:09 +01:00
|
|
|
my $file1 = shift;
|
|
|
|
my $file2 = shift;
|
|
|
|
|
|
|
|
local $/ = undef;
|
|
|
|
|
|
|
|
return -1 if !open(IN, "< $file1");
|
|
|
|
my $s1 = <IN>;
|
|
|
|
close(IN);
|
|
|
|
|
|
|
|
return 1 if !open(IN, "< $file2");
|
|
|
|
my $s2 = <IN>;
|
|
|
|
close(IN);
|
|
|
|
|
|
|
|
return $s1 cmp $s2;
|
|
|
|
}
|
|
|
|
|
2001-06-13 21:38:29 +02:00
|
|
|
########################################################################
|
|
|
|
# append_file
|
|
|
|
|
2004-10-26 02:12:21 +02:00
|
|
|
sub append_file($$@) {
|
2001-06-13 21:38:29 +02:00
|
|
|
my $filename = shift;
|
|
|
|
my $function = shift;
|
|
|
|
|
|
|
|
open(OUT, ">> $filename") || die "Can't open file '$filename'";
|
|
|
|
my $result = &$function(\*OUT, @_);
|
|
|
|
close(OUT);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
# edit_file
|
|
|
|
|
2004-10-26 02:12:21 +02:00
|
|
|
sub edit_file($$@) {
|
2001-06-13 21:38:29 +02:00
|
|
|
my $filename = shift;
|
|
|
|
my $function = shift;
|
|
|
|
|
|
|
|
open(IN, "< $filename") || die "Can't open file '$filename'";
|
|
|
|
open(OUT, "> $filename.tmp") || die "Can't open file '$filename.tmp'";
|
|
|
|
|
|
|
|
my $result = &$function(\*IN, \*OUT, @_);
|
|
|
|
|
|
|
|
close(IN);
|
|
|
|
close(OUT);
|
|
|
|
|
|
|
|
if($result) {
|
|
|
|
unlink("$filename");
|
|
|
|
rename("$filename.tmp", "$filename");
|
|
|
|
} else {
|
|
|
|
unlink("$filename.tmp");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
# read_file
|
|
|
|
|
2004-10-26 02:12:21 +02:00
|
|
|
sub read_file($$@) {
|
2001-06-13 21:38:29 +02:00
|
|
|
my $filename = shift;
|
|
|
|
my $function = shift;
|
|
|
|
|
|
|
|
open(IN, "< $filename") || die "Can't open file '$filename'";
|
|
|
|
my $result = &$function(\*IN, @_);
|
|
|
|
close(IN);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
# replace_file
|
|
|
|
|
2004-10-26 02:12:21 +02:00
|
|
|
sub replace_file($$@) {
|
2001-06-13 21:38:29 +02:00
|
|
|
my $filename = shift;
|
|
|
|
my $function = shift;
|
|
|
|
|
|
|
|
open(OUT, "> $filename.tmp") || die "Can't open file '$filename.tmp'";
|
|
|
|
|
|
|
|
my $result = &$function(\*OUT, @_);
|
|
|
|
|
|
|
|
close(OUT);
|
|
|
|
|
2002-10-29 22:25:09 +01:00
|
|
|
if($result && _compare_files($filename, "$filename.tmp")) {
|
2001-06-13 21:38:29 +02:00
|
|
|
unlink("$filename");
|
2002-10-29 22:25:09 +01:00
|
|
|
rename("$filename.tmp", $filename);
|
2001-06-13 21:38:29 +02:00
|
|
|
} else {
|
|
|
|
unlink("$filename.tmp");
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2002-06-01 04:55:48 +02:00
|
|
|
}
|
2001-06-13 21:38:29 +02:00
|
|
|
|
2001-07-11 19:27:45 +02:00
|
|
|
########################################################################
|
|
|
|
# normalize_set
|
|
|
|
|
2004-10-26 02:12:21 +02:00
|
|
|
sub normalize_set($) {
|
2001-07-11 19:27:45 +02:00
|
|
|
local $_ = shift;
|
|
|
|
|
|
|
|
if(!defined($_)) {
|
|
|
|
return undef;
|
|
|
|
}
|
2002-06-01 04:55:48 +02:00
|
|
|
|
2001-07-11 19:27:45 +02:00
|
|
|
my %hash = ();
|
|
|
|
foreach my $key (split(/\s*&\s*/)) {
|
|
|
|
$hash{$key}++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return join(" & ", sort(keys(%hash)));
|
|
|
|
}
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
# is_subset
|
|
|
|
|
2004-10-26 02:12:21 +02:00
|
|
|
sub is_subset($$) {
|
2001-07-11 19:27:45 +02:00
|
|
|
my $subset = shift;
|
|
|
|
my $set = shift;
|
|
|
|
|
|
|
|
foreach my $subitem (split(/ & /, $subset)) {
|
|
|
|
my $match = 0;
|
|
|
|
foreach my $item (split(/ & /, $set)) {
|
|
|
|
if($subitem eq $item) {
|
|
|
|
$match = 1;
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!$match) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2001-06-13 21:38:29 +02:00
|
|
|
1;
|