Fixed some issues found by winapi_check.
[wine] / tools / winapi / output.pm
1 package output;
2
3 use strict;
4
5 my $_output;
6
7 sub new {
8     my $self = shift;
9     $_output = _output->new(@_);
10     return $_output;
11 }
12
13 sub AUTOLOAD {
14     my $self = shift;
15
16     my $name = $output::AUTOLOAD;
17     $name =~ s/^.*::(.[^:]*)$/$1/;
18
19     return $_output->$name(@_);
20 }
21
22 package _output;
23
24 use strict;
25
26 sub new {
27     my $proto = shift;
28     my $class = ref($proto) || $proto;
29     my $self  = {};
30     bless ($self, $class);
31
32     my $progress = \${$self->{PROGRESS}};
33     my $last_progress = \${$self->{LAST_PROGRESS}};
34     my $progress_count = \${$self->{PROGRESS_COUNT}};
35     my $prefix = \${$self->{PREFIX}};
36
37     $$progress = "";
38     $$last_progress = "";
39     $$progress_count = 0;
40     $$prefix = "";
41
42     return $self;
43 }
44
45
46 sub show_progress {
47     my $self = shift;
48     my $progress = \${$self->{PROGRESS}};
49     my $last_progress = \${$self->{LAST_PROGRESS}};
50     my $progress_count = \${$self->{PROGRESS_COUNT}};
51
52     $$progress_count++;
53
54     if($$progress_count > 0 && $$progress) {
55         print STDERR $$progress;
56         $$last_progress = $$progress;
57     }
58 }
59
60 sub hide_progress  {
61     my $self = shift;
62     my $progress = \${$self->{PROGRESS}};
63     my $last_progress = \${$self->{LAST_PROGRESS}};
64     my $progress_count = \${$self->{PROGRESS_COUNT}};
65
66     $$progress_count--;
67
68     if($$last_progress) {
69         my $message;
70         for (1..length($$last_progress)) {
71             $message .= "\b \b";
72         }
73         print STDERR $message;
74         undef $$last_progress;
75     }
76 }
77
78 sub update_progress {
79     my $self = shift;
80     my $progress = \${$self->{PROGRESS}};
81     my $last_progress = \${$self->{LAST_PROGRESS}};
82     
83     my $prefix = "";
84     my $suffix = "";
85     if($$last_progress) {
86         for (1..length($$last_progress)) {
87             $prefix .= "\b";
88         }
89         
90         my $diff = length($$last_progress)-length($$progress);
91         if($diff > 0) {
92             for (1..$diff) {
93                 $suffix .= " ";
94             }
95             for (1..$diff) {
96                 $suffix .= "\b";
97             }
98         }
99     }
100     print STDERR $prefix . $$progress . $suffix;
101     $$last_progress = $$progress;
102 }
103
104 sub progress {
105     my $self = shift;
106     my $progress = \${$self->{PROGRESS}};
107
108     $$progress = shift;
109
110     $self->update_progress;
111 }
112
113 sub prefix {
114     my $self = shift;
115     my $prefix = \${$self->{PREFIX}};
116
117     $$prefix = shift;
118 }
119
120 sub write {
121     my $self = shift;
122
123     my $message = shift;
124
125     my $prefix = \${$self->{PREFIX}};
126
127     $self->hide_progress;
128     print $$prefix . $message;
129     $self->show_progress;
130 }
131
132 1;