Skip to content

Commit f75ae56

Browse files
committed
fix: add Windows compatibility for progress bar timing
Replace POSIX clock_gettime() with cross-platform timing using QueryPerformanceCounter on Windows and clock_gettime on POSIX. Introduce progress_time_t typedef to abstract the platform-specific time storage type. Signed-off-by: Vincent Jardin <vjardin@free.fr>
1 parent f296605 commit f75ae56

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/progress.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
* Minimal terminal progress bar with optional callback for library integration
77
*/
88

9+
#ifndef _WIN32
910
#define _DEFAULT_SOURCE /* clock_gettime, CLOCK_MONOTONIC */
11+
#endif
1012

1113
#include "progress.h"
1214
#include <stdio.h>
@@ -17,15 +19,42 @@
1719
/* Global quiet mode */
1820
int progress_global_quiet = 0;
1921

20-
/* Get elapsed time in seconds */
22+
#ifdef _WIN32
23+
/* Windows: use QueryPerformanceCounter for high-resolution timing */
24+
static LARGE_INTEGER perf_freq;
25+
static int perf_freq_init = 0;
26+
27+
static void
28+
get_current_time(progress_time_t *t) {
29+
QueryPerformanceCounter(t);
30+
}
31+
32+
static double
33+
get_elapsed_secs(const progress_time_t *start) {
34+
LARGE_INTEGER now;
35+
if (!perf_freq_init) {
36+
QueryPerformanceFrequency(&perf_freq);
37+
perf_freq_init = 1;
38+
}
39+
QueryPerformanceCounter(&now);
40+
return (double)(now.QuadPart - start->QuadPart) / (double)perf_freq.QuadPart;
41+
}
42+
#else
43+
/* POSIX: use clock_gettime with CLOCK_MONOTONIC */
44+
static void
45+
get_current_time(progress_time_t *t) {
46+
clock_gettime(CLOCK_MONOTONIC, t);
47+
}
48+
2149
static double
22-
get_elapsed_secs(const struct timespec *start) {
50+
get_elapsed_secs(const progress_time_t *start) {
2351
struct timespec now;
2452
clock_gettime(CLOCK_MONOTONIC, &now);
2553
double secs = (double)(now.tv_sec - start->tv_sec);
2654
secs += (double)(now.tv_nsec - start->tv_nsec) / 1e9;
2755
return secs;
2856
}
57+
#endif
2958

3059
void
3160
progress_init(progress_t *p, size_t total, const char *desc) {
@@ -36,7 +65,7 @@ progress_init(progress_t *p, size_t total, const char *desc) {
3665
p->callback = NULL;
3766
p->user_data = NULL;
3867
p->quiet = progress_global_quiet;
39-
clock_gettime(CLOCK_MONOTONIC, &p->start_time);
68+
get_current_time(&p->start_time);
4069
progress_update(p, 0);
4170
}
4271

src/progress.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111

1212
#include <stddef.h>
1313
#include <stdint.h>
14+
15+
#ifdef _WIN32
16+
#include <windows.h>
17+
typedef LARGE_INTEGER progress_time_t;
18+
#else
1419
#include <time.h>
20+
typedef struct timespec progress_time_t;
21+
#endif
1522

1623
/*
1724
* Progress callback function type
@@ -27,7 +34,7 @@ typedef struct {
2734
const char *desc;
2835
progress_cb_t callback;
2936
void *user_data;
30-
struct timespec start_time; /* Start time for speed/ETA calculation */
37+
progress_time_t start_time; /* Start time for speed/ETA calculation */
3138
int quiet; /* Suppress output if set */
3239
} progress_t;
3340

0 commit comments

Comments
 (0)