1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
#
# 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
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#
package util;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(
append_file edit_file read_file replace_file
normalize_set is_subset
);
@EXPORT_OK = qw();
%EXPORT_TAGS = ();
########################################################################
# _compare_files
sub _compare_files($$) {
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;
}
########################################################################
# append_file
sub append_file($$@) {
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
sub edit_file($$@) {
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
sub read_file($$@) {
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
sub replace_file($$@) {
my $filename = shift;
my $function = shift;
open(OUT, "> $filename.tmp") || die "Can't open file '$filename.tmp'";
my $result = &$function(\*OUT, @_);
close(OUT);
if($result && _compare_files($filename, "$filename.tmp")) {
unlink("$filename");
rename("$filename.tmp", $filename);
} else {
unlink("$filename.tmp");
}
return $result;
}
########################################################################
# normalize_set
sub normalize_set($) {
local $_ = shift;
if(!defined($_)) {
return undef;
}
my %hash = ();
foreach my $key (split(/\s*&\s*/)) {
$hash{$key}++;
}
return join(" & ", sort(keys(%hash)));
}
########################################################################
# is_subset
sub is_subset($$) {
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;
}
1;
|