File: utils.c

package info (click to toggle)
exfatprogs 1.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,184 kB
  • sloc: ansic: 7,887; sh: 4,505; makefile: 60
file content (66 lines) | stat: -rw-r--r-- 1,384 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *   Copyright (C) 2026 Hyunchul Lee <hyc.lee@gmail.com>
 *
 *   Portions of the progress bar code derived from ntfsprogs-plus and modified for exfatprogs.
 */

#include <stdio.h>
#include <inttypes.h>

#include "utils.h"

void progress_init(struct progress_bar *p, uint32_t start, uint32_t stop, uint32_t res)
{
	uint64_t total;

	p->start = start;
	p->stop = stop;
	p->current = start;

	total = stop - start + 1;
	if (total <= 0)
		total = 1;

#ifdef PROG_CALC_FLOAT
	p->unit = 100.0 / total;

	if (((res * 100 * 100) / total) == 0)
		p->resolution = (int)(total / (100 * 100.0));	// 0.01 resolution
	else
		p->resolution = res;
#else
	p->total = total;

	if (((res * 100) / p->total) == 0) {
		p->resolution = p->total / 100;
		if (!p->resolution)
			p->resolution = 1;
	} else
		p->resolution = res;
#endif
}

void progress_update(struct progress_bar *p, uint32_t update)
{
#ifdef PROG_CALC_FLOAT
	float percent;
#else
	uint32_t percent;
#endif

	p->current += update;
	if (p->current != p->stop) {
		if ((p->current - p->start) % p->resolution)
			return;
#ifdef PROG_CALC_FLOAT
		percent = p->unit * p->current;
		printf("%6.2f percent completed\r", percent);
#else
		percent = (p->current * 100) / p->total;
		printf("%3u percent completed\r", percent);
#endif
	} else
		printf("100.00 percent completed\n");
	fflush(stdout);
}