convtbl.c revision 164673
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 164673 2006-11-27 16:23:09Z 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
45struct convtbl {
46	uintmax_t	 mul;
47	uintmax_t	 scale;
48	const char	*str;
49	const char	*name;
50};
51
52static struct convtbl convtbl[] = {
53	/* mul, scale, str, name */
54	[SC_BYTE] =	{ BYTE, BYTES, "B",  "byte"  },
55	[SC_KILOBYTE] =	{ BYTE, KILO,  "KB", "kbyte" },
56	[SC_MEGABYTE] =	{ BYTE, MEGA,  "MB", "mbyte" },
57	[SC_GIGABYTE] =	{ BYTE, GIGA,  "GB", "gbyte" },
58
59	[SC_BIT] =	{ BIT, BITS, "b",  "bit"  },
60	[SC_KILOBIT] =	{ BIT, KILO, "Kb", "kbit" },
61	[SC_MEGABIT] =	{ BIT, MEGA, "Mb", "mbit" },
62	[SC_GIGABIT] =	{ BIT, GIGA, "Gb", "gbit" },
63
64	[SC_AUTO] =	{ 0, 0, "", "auto" }
65};
66
67static
68struct convtbl *
69get_tbl_ptr(const uintmax_t size, const int scale)
70{
71	uintmax_t	 tmp;
72	int		 idx;
73
74	/* If our index is out of range, default to auto-scaling. */
75	idx = scale < SC_AUTO ? scale : SC_AUTO;
76
77	if (idx == SC_AUTO)
78		/*
79		 * Simple but elegant algorithm.  Count how many times
80		 * we can shift our size value right by a factor of ten,
81		 * incrementing an index each time.  We then use the
82		 * index as the array index into the conversion table.
83		 */
84		for (tmp = size, idx = SC_KILOBYTE;
85		     tmp >= MEGA && idx < SC_GIGABYTE;
86		     tmp >>= 10, idx++);
87
88	return (&convtbl[idx]);
89}
90
91double
92convert(const uintmax_t size, const int scale)
93{
94	struct convtbl	*tp;
95
96	tp = get_tbl_ptr(size, scale);
97	return ((double)size * tp->mul / tp->scale);
98
99}
100
101const char *
102get_string(const uintmax_t size, const int scale)
103{
104	struct convtbl	*tp;
105
106	tp = get_tbl_ptr(size, scale);
107	return (tp->str);
108}
109
110int
111get_scale(const char *name)
112{
113	int i;
114
115	for (i = 0; i <= SC_AUTO; i++)
116		if (strcmp(convtbl[i].name, name) == 0)
117			return (i);
118	return (-1);
119}
120
121const char *
122get_helplist()
123{
124	int i;
125	size_t len;
126	static char *buf;
127
128	if (buf == NULL) {
129		len = 0;
130		for (i = 0; i <= SC_AUTO; i++)
131			len += strlen(convtbl[i].name) + 2;
132		if ((buf = malloc(len)) != NULL) {
133			buf[0] = '\0';
134			for (i = 0; i <= SC_AUTO; i++) {
135				strcat(buf, convtbl[i].name);
136				if (i < SC_AUTO)
137					strcat(buf, ", ");
138			}
139		} else
140			return ("");
141	}
142	return (buf);
143}
144