OSDN Git Service

Some Makefile improvements.
[lamexp/LameXP.git] / etc / Patches / AC3Filter-tools-FlushProgress+STDOUT.V4.diff
1  valdec.cpp | 102 +++++++++++++++++++++++++++++++++----------------------------
2  1 file changed, 56 insertions(+), 46 deletions(-)
3
4 diff --git a/valdec.cpp b/valdec.cpp
5 index 2530a56..589068c 100644
6 --- a/valdec.cpp
7 +++ b/valdec.cpp
8 @@ -3,6 +3,10 @@
9  #include <math.h>
10  #include <string>
11  
12 +// CRT
13 +#include <fcntl.h>
14 +#include <io.h>
15 +
16  // parsers
17  #include "source/file_parser.h"
18  
19 @@ -131,7 +135,7 @@ int valdec(const arg_list_t &args)
20    using std::string;
21    if (args.size() < 2)
22    {
23 -    printf(usage);
24 +    fprintf(stderr, usage);
25      return 1;
26    }
27  
28 @@ -218,7 +222,7 @@ int valdec(const arg_list_t &args)
29      {
30        if (parser)
31        {
32 -        printf("-ac3 : ambiguous parser\n");
33 +        fprintf(stderr, "-ac3 : ambiguous parser\n");
34          return 1;
35        }
36  
37 @@ -231,7 +235,7 @@ int valdec(const arg_list_t &args)
38      {
39        if (parser)
40        {
41 -        printf("-dts : ambiguous parser\n");
42 +        fprintf(stderr, "-dts : ambiguous parser\n");
43          return 1;
44        }
45  
46 @@ -244,7 +248,7 @@ int valdec(const arg_list_t &args)
47      {
48        if (parser)
49        {
50 -        printf("-mpa : ambiguous parser\n");
51 +        fprintf(stderr, "-mpa : ambiguous parser\n");
52          return 1;
53        }
54  
55 @@ -288,7 +292,7 @@ int valdec(const arg_list_t &args)
56      {
57        if (sink)
58        {
59 -        printf("-decode : ambiguous output mode\n");
60 +        fprintf(stderr, "-decode : ambiguous output mode\n");
61          return 1;
62        }
63  
64 @@ -304,7 +308,7 @@ int valdec(const arg_list_t &args)
65      {
66        if (sink)
67        {
68 -        printf("-play : ambiguous output mode\n");
69 +        fprintf(stderr, "-play : ambiguous output mode\n");
70          return 1;
71        }
72  
73 @@ -320,12 +324,12 @@ int valdec(const arg_list_t &args)
74      {
75        if (sink)
76        {
77 -        printf("-raw : ambiguous output mode\n");
78 +        fprintf(stderr, "-raw : ambiguous output mode\n");
79          return 1;
80        }
81        if (args.size() - iarg < 1)
82        {
83 -        printf("-raw : specify a file name\n");
84 +        fprintf(stderr, "-raw : specify a file name\n");
85          return 1;
86        }
87  
88 @@ -342,12 +346,12 @@ int valdec(const arg_list_t &args)
89      {
90        if (sink)
91        {
92 -        printf("-wav : ambiguous output mode\n");
93 +        fprintf(stderr, "-wav : ambiguous output mode\n");
94          return 1;
95        }
96        if (args.size() - iarg < 1)
97        {
98 -        printf("-wav : specify a file name\n");
99 +        fprintf(stderr, "-wav : specify a file name\n");
100          return 1;
101        }
102  
103 @@ -364,7 +368,7 @@ int valdec(const arg_list_t &args)
104      {
105        if (sink)
106        {
107 -        printf("-nothing : ambiguous output mode\n");
108 +        fprintf(stderr, "-nothing : ambiguous output mode\n");
109          return 1;
110        }
111  
112 @@ -404,7 +408,7 @@ int valdec(const arg_list_t &args)
113      {
114        if (args.size() - iarg < 1)
115        {
116 -        printf("-log : specify a file name\n");
117 +        fprintf(stderr, "-log : specify a file name\n");
118          return 1;
119        }
120  
121 @@ -587,7 +591,7 @@ int valdec(const arg_list_t &args)
122      }
123      if (have_delay) continue;
124  
125 -    printf("Error: unknown option: %s\n", arg.raw.c_str());
126 +    fprintf(stderr, "Error: unknown option: %s\n", arg.raw.c_str());
127      return 1;
128    }
129  
130 @@ -600,13 +604,13 @@ int valdec(const arg_list_t &args)
131  
132    if (!file.open(input_filename, parser, 1000000))
133    {
134 -    printf("Error: Cannot open file '%s'\n", input_filename);
135 +    fprintf(stderr, "Error: Cannot open file '%s'\n", input_filename);
136      return 1;
137    }
138  
139    if (!file.stats() || !file.probe())
140    {
141 -    printf("Error: Cannot detect input file format\n", input_filename);
142 +    fprintf(stderr, "Error: Cannot detect input file format\n", input_filename);
143      return 1;
144    }
145  
146 @@ -616,8 +620,8 @@ int valdec(const arg_list_t &args)
147  
148    if (print_info)
149    {
150 -    printf("%s\n", file.file_info().c_str());
151 -    printf("%s", file.stream_info().c_str());
152 +    fprintf(stderr, "%s\n", file.file_info().c_str());
153 +    fprintf(stderr, "%s", file.stream_info().c_str());
154    }
155  
156    if (mode == mode_nothing)
157 @@ -638,14 +642,14 @@ int valdec(const arg_list_t &args)
158    Speakers user_spk(format, mask, sample_rate);
159    if (!dvd_graph.set_user(user_spk))
160    {
161 -    printf("Error: unsupported user format %s\n", user_spk.print());
162 +    fprintf(stderr, "Error: unsupported user format %s\n", user_spk.print());
163      return 1;
164    }
165  
166    Speakers in_spk = file.get_output();
167    if (!dvd_graph.open(in_spk))
168    {
169 -    printf("Error: unsupported file format %s\n", in_spk.print());
170 +    fprintf(stderr, "Error: unsupported file format %s\n", in_spk.print());
171      return 1;
172    }
173  
174 @@ -679,7 +683,7 @@ int valdec(const arg_list_t &args)
175      case mode_raw:
176        if (!out_filename || !raw.open_file(out_filename))
177        {
178 -        printf("Error: failed to open output file '%s'\n", out_filename);
179 +        fprintf(stderr, "Error: failed to open output file '%s'\n", out_filename);
180          return 1;
181        }
182        break;
183 @@ -687,7 +691,7 @@ int valdec(const arg_list_t &args)
184      case mode_wav:
185        if (!out_filename || !wav.open_file(out_filename))
186        {
187 -        printf("Error: failed to open output file '%s'\n", out_filename);
188 +        fprintf(stderr, "Error: failed to open output file '%s'\n", out_filename);
189          return 1;
190        }
191        break;
192 @@ -695,7 +699,7 @@ int valdec(const arg_list_t &args)
193      case mode_play:
194        if (!dsound.open_dsound(0))
195        {
196 -        printf("Error: failed to init DirectSound\n");
197 +        fprintf(stderr, "Error: failed to init DirectSound\n");
198          return 1;
199        }
200        break;
201 @@ -744,10 +748,14 @@ int valdec(const arg_list_t &args)
202        int(value2db(level)),                                                                                    \
203        int(file.get_frames() / time),                                                                           \
204        cpu_current.usage() * 100);                                                                              \
205 +    fflush(stderr);                                                                                            \
206    }
207  
208    #define DROP_STAT \
209 -    fprintf(stderr, "                                                                             \r");
210 +  { \
211 +    fprintf(stderr, "                                                                             \r"); \
212 +    fflush(stderr); \
213 +  }
214  
215    while (file.get_chunk(chunk))
216    {
217 @@ -760,7 +768,7 @@ int valdec(const arg_list_t &args)
218          PRINT_STAT;
219  
220        if (streams > 0 && print_info)
221 -        printf("\n\n%s", file.stream_info().c_str());
222 +        fprintf(stderr, "\n\n%s", file.stream_info().c_str());
223  
224        streams++;
225        if (mode == mode_nothing)
226 @@ -775,11 +783,11 @@ int valdec(const arg_list_t &args)
227          if (sink->open(new_spk))
228          {
229            DROP_STAT;
230 -          printf("Opening audio output %s...\n", new_spk.print().c_str());
231 +          fprintf(stderr, "Opening audio output %s...\n", new_spk.print().c_str());
232          }
233          else
234          {
235 -          printf("\nOutput format %s is unsupported\n", new_spk.print().c_str());
236 +          fprintf(stderr, "\nOutput format %s is unsupported\n", new_spk.print().c_str());
237            return 1;
238          }
239        }
240 @@ -806,11 +814,11 @@ int valdec(const arg_list_t &args)
241        if (sink->open(new_spk))
242        {
243          DROP_STAT;
244 -        printf("Opening audio output %s...\n", new_spk.print().c_str());
245 +        fprintf(stderr, "Opening audio output %s...\n", new_spk.print().c_str());
246        }
247        else
248        {
249 -        printf("\nOutput format %s is unsupported\n", new_spk.print().c_str());
250 +        fprintf(stderr, "\nOutput format %s is unsupported\n", new_spk.print().c_str());
251          return 1;
252        }
253      }
254 @@ -829,13 +837,13 @@ int valdec(const arg_list_t &args)
255    // Final statistics
256  
257    PRINT_STAT;
258 -  printf("\n---------------------------------------\n");
259 +  fprintf(stderr, "\n---------------------------------------\n");
260    if (streams > 1)
261 -    printf("Streams found: %i\n", streams);
262 -  printf("Frames: %i\n", file.get_frames());
263 -  printf("System time: %ims\n", int(cpu_total.get_system_time() * 1000));
264 -  printf("Process time: %ims\n", int(cpu_total.get_thread_time() * 1000 ));
265 -  printf("Approx. %.2f%% realtime CPU usage\n", double(cpu_total.get_thread_time() * 100) / file.get_size(file.time));
266 +    fprintf(stderr, "Streams found: %i\n", streams);
267 +  fprintf(stderr, "Frames: %i\n", file.get_frames());
268 +  fprintf(stderr, "System time: %ims\n", int(cpu_total.get_system_time() * 1000));
269 +  fprintf(stderr, "Process time: %ims\n", int(cpu_total.get_thread_time() * 1000 ));
270 +  fprintf(stderr, "Approx. %.2f%% realtime CPU usage\n", double(cpu_total.get_thread_time() * 100) / file.get_size(file.time));
271  
272    /////////////////////////////////////////////////////////
273    // Print levels histogram
274 @@ -852,21 +860,21 @@ int valdec(const arg_list_t &args)
275      max_level = dvd_graph.proc.get_max_level();
276      dbpb = dvd_graph.proc.get_dbpb();
277  
278 -    printf("\nHistogram:\n");
279 -    printf("------------------------------------------------------------------------------\n");
280 +    fprintf(stderr, "\nHistogram:\n");
281 +    fprintf(stderr, "------------------------------------------------------------------------------\n");
282      for (i = 0; i*dbpb < 100 && i < MAX_HISTOGRAM; i++)
283      {
284 -      printf("%2idB: %4.1f ", i * dbpb, hist[i] * 100);
285 +      fprintf(stderr, "%2idB: %4.1f ", i * dbpb, hist[i] * 100);
286        for (j = 0; j < 67 && j < hist[i] * 67; j++)
287 -        printf("*");
288 -      printf("\n");
289 +        fprintf(stderr, "*");
290 +      fprintf(stderr, "\n");
291      }
292 -    printf("------------------------------------------------------------------------------\n");
293 -    printf("max_level;%f\ndbpb;%i\nhistogram;", max_level, dbpb);
294 +    fprintf(stderr, "------------------------------------------------------------------------------\n");
295 +    fprintf(stderr, "max_level;%f\ndbpb;%i\nhistogram;", max_level, dbpb);
296      for (i = 0; i < MAX_HISTOGRAM; i++)
297 -      printf("%.4f;", hist[i]);
298 -    printf("\n");
299 -    printf("------------------------------------------------------------------------------\n");
300 +      fprintf(stderr, "%.4f;", hist[i]);
301 +    fprintf(stderr, "\n");
302 +    fprintf(stderr, "------------------------------------------------------------------------------\n");
303    }
304  
305    return 0;
306 @@ -876,16 +884,18 @@ int main(int argc, const char *argv[])
307  {
308    try
309    {
310 +    _setmode(_fileno(stdout), _O_BINARY);
311 +    _setmode(_fileno(stdin), _O_BINARY);
312      return valdec(args_utf8(argc, argv));
313    }
314    catch (ValibException &e)
315    {
316 -    printf("Processing error: %s\n", boost::diagnostic_information(e).c_str());
317 +    fprintf(stderr, "Processing error: %s\n", boost::diagnostic_information(e).c_str());
318      return -1;
319    }
320    catch (arg_t::bad_value_e &e)
321    {
322 -    printf("Bad argument value: %s", e.arg.c_str());
323 +    fprintf(stderr, "Bad argument value: %s", e.arg.c_str());
324      return -1;
325    }
326    return 0;