Skip to content

Commit 4e80524

Browse files
committed
Unified progress implementation.
1 parent 7e8fad0 commit 4e80524

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

src/6d6copy.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "i18n.h"
1212
#include "i18n_error.h"
1313
#include "monotonic-time.h"
14+
#include "progress.h"
1415

1516
const char *program = "6d6copy";
1617
static void help(const char *arg)
@@ -153,9 +154,7 @@ int main(int argc, char **argv)
153154
if (m != l) io_error(3);
154155
n += m;
155156
if (progress == 1) {
156-
fprintf(stderr, "%2$3d%% %1$6.1f MB \r",
157-
(double) n / 1000000,
158-
(int) (100 * n / end));
157+
progress_update(n, end);
159158
} else if (progress == 2) {
160159
t = monotonic_time();
161160
if (t - t1 >= _50ms) {
@@ -169,7 +168,7 @@ int main(int argc, char **argv)
169168
}
170169

171170
if (progress == 1) {
172-
fprintf(stderr, "%2$3d%% %1$6.1f MB \n", (double) end / 1000000, 100);
171+
progress_complete(end);
173172
} else if (progress == 2) {
174173
t = monotonic_time();
175174
fprintf(stdout,

src/6d6mseed.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "wmseed.h"
2626
#define RESAMPLER_IMPLEMENTATION
2727
#include "resampler.h"
28+
#include "progress.h"
2829

2930
static FILE *_logfile = 0;
3031
static void log_entry(FILE *f, const char *format, ...)
@@ -483,8 +484,7 @@ int main(int argc, char **argv)
483484
}
484485
}
485486
if (progress == 1 && i % 1024 == 0) {
486-
fprintf(stderr, "%5.1f%% %8.1fMB \r", i * 100.0 / h_end.address, i * 512.0 / 1000000);
487-
fflush(stderr);
487+
progress_update(i * 512ll, h_end.address * 512ll);
488488
} else if (progress == 2) {
489489
t2 = monotonic_time();
490490
if (t2 - t1 >= _50ms) {
@@ -501,8 +501,7 @@ int main(int argc, char **argv)
501501
wmseed_destroy(channels[c]);
502502
}
503503
if (progress == 1) {
504-
log_entry(stderr, "%5.1f%% %8.1fMB \n", 100.0, h_end.address * 512.0 / 1000000);
505-
fflush(stderr);
504+
progress_complete(h_end.address * 512ll);
506505
} else if (progress == 2) {
507506
t2 = monotonic_time();
508507
fprintf(stdout,

src/6d6read.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "i18n_error.h"
1818
#include "monotonic-time.h"
1919
#include "tai.h"
20+
#include "progress.h"
2021

2122
#define SAMPLE_TRACKER_IMPLEMENTATION
2223
#include "sample-tracker.h"
@@ -333,8 +334,7 @@ int main(int argc, char **argv)
333334
}
334335
}
335336
if (progress == 1 && i % 1024 == 0) {
336-
fprintf(stderr, "%3d%% %6.1fMB \r", (int) (i * 100 / h_end.address), (double) i * 512 / 1000000l);
337-
fflush(stderr);
337+
progress_update(i * 512ll, h_end.address * 512ll);
338338
} else if (progress == 2) {
339339
t2 = monotonic_time();
340340
if (t2 - t1 >= _50ms) {
@@ -358,8 +358,7 @@ int main(int argc, char **argv)
358358
return 1;
359359
}
360360
if (progress == 1) {
361-
fprintf(stderr, "%3d%% %6.1fMB \n", 100, (double) h_end.address * 512 / 1000000l);
362-
fflush(stderr);
361+
progress_complete(h_end.address * 512ll);
363362
} else if (progress == 2) {
364363
t2 = monotonic_time();
365364
fprintf(stderr,

src/6d6strip.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "tai.h"
1616
#include "i18n.h"
1717
#include "i18n_error.h"
18+
#include "progress.h"
1819

1920
static const char *program = "6d6strip";
2021
static void help(const char *arg)
@@ -205,8 +206,7 @@ int main(int argc, char **argv)
205206
}
206207
}
207208
if (progress && i % 1024 == 0) {
208-
fprintf(stderr, "%3d%% %6.1fMB \r", (int) (i * 100 / h_end.address), (double) i * 512 / 1000000l);
209-
fflush(stderr);
209+
progress_update(i * 512ll, h_end.address * 512ll);
210210
}
211211
}
212212
done:
@@ -216,8 +216,7 @@ int main(int argc, char **argv)
216216
frame_counter = 0;
217217
}
218218
if (progress) {
219-
fprintf(stderr, "%3d%% %6.1fMB \n", 100, (double) h_end.address * 512 / 1000000l);
220-
fflush(stderr);
219+
progress_complete(h_end.address * 512ll);
221220
}
222221
return 0;
223222
}

src/progress.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef PROGRESS_H
2+
#define PROGRESS_H
3+
4+
#include <stdio.h>
5+
6+
static void progress_update(long long done, long long total) {
7+
fprintf(stderr, "%5.1f%% %8.1fMB \r", 100.0 * done / total, done * 1e-6);
8+
fflush(stderr);
9+
}
10+
11+
static void progress_complete(long long total) {
12+
fprintf(stderr, "%5.1f%% %8.1fMB \n", 100.0, total * 1e-6);
13+
fflush(stderr);
14+
}
15+
16+
#endif

0 commit comments

Comments
 (0)