convtbl.c revision 164675
1/*
2 * Copyright (c) 2003, Trent Nelson, <trent@arpa.com>.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: head/usr.bin/systat/convtbl.c 164675 2006-11-27 16:33:44Z yar $
29 */
30
31#include <sys/types.h>
32#include <stdlib.h>
33#include <string.h>
34#include "convtbl.h"
35
36#define BIT	(8)
37#define BYTE	(1)
38
39#define BITS	(1)
40#define BYTES	(1)
41#define KILO	(1024LL)
42#define MEGA	(KILO * 1024)
43#define GIGA	(MEGA * 1024)
44#define TERA	(GIGA * 1024)
45
46struct convtbl {
47	uintmax_t	 mul;
48	uintmax_t	 scale;
49	const char	*str;
50	const char	*name;
51};
52
53static struct convtbl convtbl[] = {
54	/* mul, scale, str, name */
55	[SC_BYTE] =	{ BYTE, BYTES, "B",  "byte"  },
56	[SC_KILOBYTE] =	{ BYTE, KILO,  "KB", "kbyte" },
57	[SC_MEGABYTE] =	{ BYTE, MEGA,  "MB", "mbyte" },
58	[SC_GIGABYTE] =	{ BYTE, GIGA,  "GB", "gbyte" },
59	[SC_TERABYTE] =	{ BYTE, TERA,  "TB", "tbyte" },
60
61	[SC_BIT] =	{ BIT, BITS, "b",  "bit"  },
62	[SC_KILOBIT] =	{ BIT, KILO, "Kb", "kbit" },
63	[SC_MEGABIT] =	{ BIT, MEGA, "Mb", "mbit" },
64	[SC_GIGABIT] =	{ BIT, GIGA, "Gb", "gbit" },
65	[SC_TERABIT] =	{ BIT, TERA, "Tb", "tbit" },
66
67	[SC_AUTO] =	{ 0, 0, "", "auto" }
68};
69
70static
71struct convtbl *
72get_tbl_ptr(const uintmax_t size, const int scale)
73{
74	uintmax_t	 tmp;
75	int		 idx;
76
77	/* If our index is out of range, default to auto-scaling. */
78	idx = scale < SC_AUTO ? scale : SC_AUTO;
79
80	if (idx == SC_AUTO)
81		/*
82		 * Simple but elegant algorithm.  Count how many times
83		 * we can shift our size value right by a factor of ten,
84		 * incrementing an index each time.  We then use the
85		 * index as the array index into the conversion table.
86		 */
87		for (tmp = size, idx = SC_KILOBYTE;
88		     tmp >= MEGA && idx < SC_BIT - 1;
89		     tmp >>= 10, idx++);
90
91	return (&convtbl[idx]);
92}
93
94double
95convert(const uintmax_t size, const int scale)
96{
97	struct convtbl	*tp;
98
99	tp = get_tbl_ptr(size, scale);
100	return ((double)size * tp->mul / tp->scale);
101
102}
103
104const char *
105get_string(const uintmax_t size, const int scale)
106{
107	struct convtbl	*tp;
108
109	tp = get_tbl_ptr(size, scale);
110	return (tp->str);
111}
112
113int
114get_scale(const char *name)
115{
116	int i;
117
118	for (i = 0; i <= SC_AUTO; i++)
119		if (strcmp(convtbl[i].name, name) == 0)
120			return (i);
121	return (-1);
122}
123
124const char *
125get_helplist()
126{
127	int i;
128	size_t len;
129	static char *buf;
130
131	if (buf == NULL) {
132		len = 0;
133		for (i = 0; i <= SC_AUTO; i++)
134			len += strlen(convtbl[i].name) + 2;
135		if ((buf = malloc(len)) != NULL) {
136			buf[0] = '\0';
137			for (i = 0; i <= SC_AUTO; i++) {
138				strcat(buf, convtbl[i].name);
139				if (i < SC_AUTO)
140					strcat(buf, ", ");
141			}
142		} else
143			return ("");
144	}
145	return (buf);
146}
147