Proper handling of SetTextCharacterExtra.
[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 use util qw(edit_file);
32
33 if($options->progress) {
34     $output->enable_progress;
35 } else {
36     $output->disable_progress;
37 }
38
39 ########################################################################
40 # cleanup_file
41
42 sub cleanup_file {
43     local *IN = shift;
44     local *OUT = shift;
45
46     my $indent;
47     my @comments = ();
48     my $format_comments = sub {
49         local $_ = "";
50         if ($#comments == 0) {
51             my $comment = $comments[0];
52             
53             $_ = "$indent/*$comment */";           
54         } elsif ($#comments > 0) {
55             $_ = "$indent/*\n";
56             foreach my $comment (@comments) {
57                 $_ .= "$indent *$comment\n";
58             }
59             $_ .= "$indent */";
60         }
61         $indent = "";
62         @comments = ();
63
64         return $_;
65     };
66
67     my $in_comment = 0;
68     my $modified = 0;
69     while (<IN>) {
70         chomp;
71
72         if ($options->trailing_whitespace) {
73             s/(.*?)\s+$/$1/ && do { $modified = 1; };
74         } else {
75             s/(.*?)\r$/$1/ && do { $modified = 1; };
76         }
77
78         if ($options->cpp_comments) {
79             if ($in_comment) {
80                 if(/^.*?\*\//) {
81                     $in_comment = 0;
82                 }
83             } elsif (/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)\/\*(.*?)$/) {
84                 my $indent2 = $1;
85                 my $comment = $2;
86                 if($comment !~ /^.*?\*\//) {
87                     $in_comment = 1;
88                 }
89             } elsif (/^([^\"\/]*?(?:\"[^\"]*?\"[^\"]*?)*?)\/\/(.*?)\s*$/) {
90                 my $indent2 = $1;
91                 my $comment = $2;
92                 
93                 if ($indent2 =~ /^\s*$/) {
94                     if (!$indent || $indent eq $indent2) {
95                         $indent = $indent2;
96                         push @comments, $comment;
97                         next;
98                     } else {
99                         $_ .= "$indent2/*$comment */";
100                     }
101                 } else {
102                     my $comments = &$format_comments();
103                     if ($comments) {
104                         $_  = "$comments\n$indent2/*$comment */";
105                     } else {
106                         $_  = "$indent2/*$comment */";
107                     }
108
109                     $modified = 1;
110                 }
111             } else {
112                 my $comments = &$format_comments();
113                 if ($comments) {
114                     $_  = "$comments\n$_";
115                     $modified = 1;
116                 }
117             }
118         }
119
120         print OUT "$_\n";
121     }
122
123     my $comments = &$format_comments();
124     if ($comments) {
125         print OUT "$comments\n";
126         $modified = 1;
127     }
128
129     return $modified;
130 }
131
132 ########################################################################
133 # main
134
135 my @h_files = $options->h_files;
136 my @c_files = $options->c_files;
137
138 my $progress_output;
139 my $progress_current = 0;
140 my $progress_max = scalar(@h_files) + scalar(@c_files);
141
142 foreach my $file (@h_files) {
143     $progress_current++;
144     $output->progress("$file (file $progress_current of $progress_max)");
145
146     if (edit_file($file, \&cleanup_file, @_)) {
147         $output->write("$file: modified\n");
148     }
149 }
150
151 foreach my $file (@c_files) {
152     $progress_current++;
153     $output->progress("$file (file $progress_current of $progress_max)");
154
155     if (edit_file($file, \&cleanup_file, @_)) {
156         $output->write("$file: modified\n");
157     }
158 }