convtbl.c revision 164637
1108684Sphk/* 2108684Sphk * Copyright (c) 2003, Trent Nelson, <trent@arpa.com>. 3108684Sphk * All rights reserved. 4108684Sphk * 5108684Sphk * Redistribution and use in source and binary forms, with or without 6108684Sphk * modification, are permitted provided that the following conditions 7108684Sphk * are met: 8108684Sphk * 1. Redistributions of source code must retain the above copyright 9108684Sphk * notice, this list of conditions and the following disclaimer. 10108684Sphk * 2. Redistributions in binary form must reproduce the above copyright 11108684Sphk * notice, this list of conditions and the following disclaimer in the 12108684Sphk * documentation and/or other materials provided with the distribution. 13108684Sphk * 3. The name of the author may not be used to endorse or promote products 14108684Sphk * derived from this software without specific prior written permission. 15108684Sphk * 16108684Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17108684Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18108684Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19108684Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20108684Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21108684Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22108684Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23108684Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24108684Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25108684Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26108684Sphk * SUCH DAMAGE. 27108684Sphk * 28108684Sphk * $FreeBSD: head/usr.bin/systat/convtbl.c 164637 2006-11-26 20:14:47Z yar $ 29108684Sphk */ 30108684Sphk 31108684Sphk#include <sys/types.h> 32108684Sphk#include <unistd.h> 33108684Sphk#include "convtbl.h" 34108684Sphk 35108684Sphkstruct convtbl convtbl[] = { 36108684Sphk /* mul, scale, str */ 37108684Sphk { BYTE, BYTES, "bytes" }, /* SC_BYTE (0) */ 38108684Sphk { BYTE, KILO, "KB" }, /* SC_KILOBYTE (1) */ 39108684Sphk { BYTE, MEGA, "MB" }, /* SC_MEGABYTE (2) */ 40108684Sphk { BYTE, GIGA, "GB" }, /* SC_GIGABYTE (3) */ 41108684Sphk 42108684Sphk { BIT, BITS, "b" }, /* SC_BITS (4) */ 43108684Sphk { BIT, KILO, "Kb" }, /* SC_KILOBITS (5) */ 44108684Sphk { BIT, MEGA, "Mb" }, /* SC_MEGABITS (6) */ 45108684Sphk { BIT, GIGA, "Gb" }, /* SC_GIGABITS (7) */ 46108684Sphk 47108684Sphk { 0, 0, "" } /* SC_AUTO (8) */ 48108684Sphk 49108684Sphk}; 50108684Sphk 51108684Sphk 52164637Syarstatic 53108684Sphkstruct convtbl * 54108684Sphkget_tbl_ptr(const u_long size, const u_int scale) 55108684Sphk{ 56108684Sphk struct convtbl *tbl_ptr = NULL; 57108684Sphk u_long tmp = 0; 58126775Sdwmalone u_int idx = scale; 59108684Sphk 60108684Sphk /* If our index is out of range, default to auto-scaling. */ 61126775Sdwmalone if (idx > SC_AUTO) 62126775Sdwmalone idx = SC_AUTO; 63108684Sphk 64126775Sdwmalone if (idx == SC_AUTO) 65108684Sphk /* 66108684Sphk * Simple but elegant algorithm. Count how many times 67108684Sphk * we can shift our size value right by a factor of ten, 68108684Sphk * incrementing an index each time. We then use the 69108684Sphk * index as the array index into the conversion table. 70108684Sphk */ 71126775Sdwmalone for (tmp = size, idx = SC_KILOBYTE; 72164636Syar tmp >= MEGA && idx < SC_GIGABYTE; 73126775Sdwmalone tmp >>= 10, idx++); 74108684Sphk 75126775Sdwmalone tbl_ptr = &convtbl[idx]; 76108684Sphk return tbl_ptr; 77108684Sphk} 78108684Sphk 79108684Sphkdouble 80108684Sphkconvert(const u_long size, const u_int scale) 81108684Sphk{ 82108684Sphk struct convtbl *tp = NULL; 83108684Sphk 84108684Sphk tp = get_tbl_ptr(size, scale); 85108684Sphk 86108684Sphk return ((double)size * tp->mul / tp->scale); 87108684Sphk 88108684Sphk} 89108684Sphk 90126775Sdwmaloneconst char * 91108684Sphkget_string(const u_long size, const u_int scale) 92108684Sphk{ 93108684Sphk struct convtbl *tp = NULL; 94108684Sphk 95108684Sphk tp = get_tbl_ptr(size, scale); 96108684Sphk 97108684Sphk return tp->str; 98108684Sphk} 99