File: util.c

package info (click to toggle)
lcdproc 0.5.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 5,064 kB
  • sloc: ansic: 59,645; sh: 1,740; perl: 681; makefile: 417
file content (100 lines) | stat: -rw-r--r-- 2,761 bytes parent folder | download | duplicates (3)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** \file clients/lcdproc/util.c
 * Utility functions to print some numerical values in a nice fashion
 */

/*-
 *  Copyright (C) 2005  Peter Marschall
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301.
 */

#include "util.h"


/** Print a memory value with the correct unit to a given string.
 * \param *dst        String to print the value to.
 * \param value       Value to print.
 * \param roundlimit  Set < 1.0 if precision of original input value is not sufficient.
 * \return  dst
 */
char *
sprintf_memory(char *dst, double value, double roundlimit)
{
	if (dst != NULL) {
		char *format = "%.1f%s";

		char *unit = convert_double(&value, 1024, roundlimit);

		if (value < 100)
			format = "%.2f%s";
		if (value < 10)
			format = "%.3f%s";

		sprintf(dst, format, value, unit);
	}
	return dst;
}


/** Print a percentage value to a given string.
 * \param *dst     String to print the percentage value to.
 * \param percent  Value to print.
 * \return  dst
 */
char *
sprintf_percent(char *dst, double percent)
{
	if (dst != NULL) {
		if (percent > 99.9)
			strcpy(dst, "100%");
		else
			sprintf(dst, "%.1f%%", (percent >= 0) ? percent : 0);
	}
	return dst;
}


/** Convert a value with unit value.
 * does not do formatting
 * \param *value       Pointer to the value to be scaled
 * \param base         Base used for scaling (e.g. 1000 for decimal, 1024 for binary units)
 * \param roundlimit   Set < 1.0 if precision of original input value is not sufficient
 * \return       Unit string to be used when displaying value
 */
char *
convert_double(double *value, int base, double roundlimit)
{
	static char *units[] = {"", "k", "M", "G", "T", "P", "E", "Z", "Y", NULL};
	int off = 0;

	if ((roundlimit <= 0.0) || (roundlimit > 1.0))
		roundlimit = 0.5;

	/* Get value's order of magnitude */
	while (units[off] != NULL) {
		/*
		 * Note: the check for 1000 is to idenfity the number of
		 * characters in the output needed, not to decide if to scale
		 * value.
		 */
		if (*value < 1000 * roundlimit)
			break;
		off++;
		*value /= base;
	}
	return units[off];
}

/* EOF */