Moved ts_xlib.c into x11drv and removed libwine_tsx11.
[wine] / tools / winapi / winapi_cleanup
1 #! /usr/bin/perl -w
2
3 # Copyright 2002 Patrik Stridvall
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 #
19
20 use strict;
21
22 BEGIN {
23     $0 =~ m%^(.*?/?tools)/winapi/winapi_cleanup$%;
24     require "$1/winapi/setup.pm";
25 }
26
27 use config qw($current_dir $wine_dir);
28 use output qw($output);
29 use winapi_cleanup_options qw($options);
30
31 if($options->progress) {
32     $output->enable_progress;
33 } else {
34     $output->disable_progress;
35 }
36
37 ########################################################################
38 # edit_file
39
40 sub edit_file {
41     my $filename = shift;
42     my $function = shift;
43
44     open(IN, "< $filename") || die "Can't open file '$filename'";
45     open(OUT, "> $filename.tmp") || die "Can't open file '$filename.tmp'";
46
47     my $result = &$function(\*IN, \*OUT, @_);
48
49     close(IN);
50     close(OUT);
51
52     if($result) {
53         unlink($filename);
54         rename("$filename.tmp", $filename);
55     } else {
56         unlink("$filename.tmp");
57     }
58
59     return $result;
60 }
61
62 ########################################################################
63 # cleanup_file
64
65 sub cleanup_file {
66     local *IN = shift;
67     local *OUT = shift;
68
69     my $indent;
70     my @comments = ();
71     my $format_comments = sub {
72         local $_ = "";
73         if ($#comments == 0) {
74             my $comment = $comments[0];
75             
76             $_ = "$indent/*$comment */";           
77         } elsif ($#comments > 0) {
78             $_ = "$indent/*\n";
79             foreach my $comment (@comments) {
80                 $_ .= "$indent *$comment\n";
81             }
82             $_ .= "$indent */";
83         }
84         $indent = "";
85         @comments = ();
86
87         return $_;
88     };
89
90     my $in_comment = 0;
91     my $modified = 0;
92     while (<IN>) {
93         chomp;
94
95         if ($options->trailing_whitespace) {
96             s/(.*?)\s+$/$1/ && do { $modified = 1; };
97         } else {
98             s/(.*?)\r$/$1/ && do { $modified = 1; };
99         }
100
101         if ($options->cpp_comments) {
102             if ($in_comment) {
103                 if(/^.*?\*\//) {
104                     $in_comment = 0;
105                 }
106             } elsif (/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)\/\*(.*?)$/) {
107                 my $indent2 = $1;
108                 my $comment = $2;
109                 if($comment !~ /^.*?\*\//) {
110                     $in_comment = 1;
111                 }
112             } elsif (/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)\/\/(.*?)\s*$/) {
113                 my $indent2 = $1;
114                 my $comment = $2;
115                 
116                 if ($indent2 =~ /^\s*$/) {
117                     if (!$indent || $indent eq $indent2) {
118                         $indent = $indent2;
119                         push @comments, $comment;
120                         next;
121                     } else {
122                         $_ .= "$indent2/*$comment */";
123                     }
124                 } else {
125                     my $comments = &$format_comments();
126                     if ($comments) {
127                         $_  = "$comments\n$indent2/*$comment */";
128                     } else {
129                         $_  = "$indent2/*$comment */";
130                     }
131
132                     $modified = 1;
133                 }
134             } else {
135                 my $comments = &$format_comments();
136                 if ($comments) {
137                     $_  = "$comments\n$_";
138                     $modified = 1;
139                 }
140             }
141         }
142
143         print OUT "$_\n";
144     }
145
146     my $comments = &$format_comments();
147     if ($comments) {
148         print OUT "$comments\n";
149         $modified = 1;
150     }
151
152     return $modified;
153 }
154
155 ########################################################################
156 # main
157
158 my @h_files = $options->h_files;
159 my @c_files = $options->c_files;
160
161 my $progress_output;
162 my $progress_current = 0;
163 my $progress_max = scalar(@h_files) + scalar(@c_files);
164
165 foreach my $file (@h_files) {
166     $progress_current++;
167     $output->progress("$file (file $progress_current of $progress_max)");
168
169     if (edit_file($file, \&cleanup_file, @_)) {
170         $output->write("$file: modified\n");
171     }
172 }
173
174 foreach my $file (@c_files) {
175     $progress_current++;
176     $output->progress("$file (file $progress_current of $progress_max)");
177
178     if (edit_file($file, \&cleanup_file, @_)) {
179         $output->write("$file: modified\n");
180     }
181 }