2 # Copyright 1999, 2000, 2001 Patrik Stridvall
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 use vars
qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28 append_file edit_file read_file replace_file
29 normalize_set is_subset
34 ########################################################################
37 sub _compare_files
($$) {
43 return -1 if !open(IN
, "< $file1");
47 return 1 if !open(IN
, "< $file2");
54 ########################################################################
57 sub append_file
($$@
) {
61 open(OUT
, ">> $filename") || die "Can't open file '$filename'";
62 my $result = &$function(\
*OUT
, @_);
68 ########################################################################
75 open(IN
, "< $filename") || die "Can't open file '$filename'";
76 open(OUT
, "> $filename.tmp") || die "Can't open file '$filename.tmp'";
78 my $result = &$function(\
*IN
, \
*OUT
, @_);
85 rename("$filename.tmp", "$filename");
87 unlink("$filename.tmp");
93 ########################################################################
100 open(IN
, "< $filename") || die "Can't open file '$filename'";
101 my $result = &$function(\
*IN
, @_);
107 ########################################################################
110 sub replace_file
($$@
) {
111 my $filename = shift;
112 my $function = shift;
114 open(OUT
, "> $filename.tmp") || die "Can't open file '$filename.tmp'";
116 my $result = &$function(\
*OUT
, @_);
120 if($result && _compare_files
($filename, "$filename.tmp")) {
122 rename("$filename.tmp", $filename);
124 unlink("$filename.tmp");
130 ########################################################################
133 sub normalize_set
($) {
141 foreach my $key (split(/\s*&\s*/)) {
145 return join(" & ", sort(keys(%hash)));
148 ########################################################################
155 foreach my $subitem (split(/ & /, $subset)) {
157 foreach my $item (split(/ & /, $set)) {
158 if($subitem eq $item) {