API files update.
[wine] / tools / winapi / util.pm
1 #
2 # Copyright 1999, 2000, 2001 Patrik Stridvall
3 #
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.
8 #
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.
13 #
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 #
18
19 package util;
20
21 use strict;
22
23 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
24 require Exporter;
25
26 @ISA = qw(Exporter);
27 @EXPORT = qw(
28     &append_file &edit_file &read_file &replace_file
29     &normalize_set &is_subset
30 );
31 @EXPORT_OK = qw();
32 %EXPORT_TAGS = ();
33
34 ########################################################################
35 # append_file
36
37 sub append_file {
38     my $filename = shift;
39     my $function = shift;
40
41     open(OUT, ">> $filename") || die "Can't open file '$filename'";
42     my $result = &$function(\*OUT, @_);
43     close(OUT);
44
45     return $result;
46 }
47
48 ########################################################################
49 # edit_file
50
51 sub edit_file {
52     my $filename = shift;
53     my $function = shift;
54
55     open(IN, "< $filename") || die "Can't open file '$filename'";
56     open(OUT, "> $filename.tmp") || die "Can't open file '$filename.tmp'";
57
58     my $result = &$function(\*IN, \*OUT, @_);
59
60     close(IN);
61     close(OUT);
62
63     if($result) {
64         unlink("$filename");
65         rename("$filename.tmp", "$filename");
66     } else {
67         unlink("$filename.tmp");
68     }
69
70     return $result;
71 }
72
73 ########################################################################
74 # read_file
75
76 sub read_file {
77     my $filename = shift;
78     my $function = shift;
79
80     open(IN, "< $filename") || die "Can't open file '$filename'";
81     my $result = &$function(\*IN, @_);
82     close(IN);
83
84     return $result;
85 }
86
87 ########################################################################
88 # replace_file
89
90 sub replace_file {
91     my $filename = shift;
92     my $function = shift;
93
94     open(OUT, "> $filename.tmp") || die "Can't open file '$filename.tmp'";
95
96     my $result = &$function(\*OUT, @_);
97
98     close(OUT);
99
100     if($result) {
101         unlink("$filename");
102         rename("$filename.tmp", "$filename");
103     } else {
104         unlink("$filename.tmp");
105     }
106
107     return $result;
108 }
109
110 ########################################################################
111 # normalize_set
112
113 sub normalize_set {
114     local $_ = shift;
115
116     if(!defined($_)) {
117         return undef;
118     }
119
120     my %hash = ();
121     foreach my $key (split(/\s*&\s*/)) {
122         $hash{$key}++;
123     }
124
125     return join(" & ", sort(keys(%hash)));
126 }
127
128 ########################################################################
129 # is_subset
130
131 sub is_subset {
132     my $subset = shift;
133     my $set = shift;
134
135     foreach my $subitem (split(/ & /, $subset)) {
136         my $match = 0;
137         foreach my $item (split(/ & /, $set)) {
138             if($subitem eq $item) {
139                 $match = 1;
140                 last;
141             }
142         }
143         if(!$match) {
144             return 0;
145         }
146     }
147     return 1;
148 }
149
150 1;