Home | History | Annotate | Line # | Download | only in gen
humanize_number.c revision 1.10
      1  1.10  christos /*	$NetBSD: humanize_number.c,v 1.10 2005/09/13 01:44:09 christos Exp $	*/
      2   1.1       abs 
      3   1.1       abs /*
      4   1.1       abs  * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
      5   1.1       abs  * All rights reserved.
      6   1.1       abs  *
      7   1.1       abs  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1       abs  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9   1.1       abs  * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
     10   1.1       abs  *
     11   1.1       abs  * Redistribution and use in source and binary forms, with or without
     12   1.1       abs  * modification, are permitted provided that the following conditions
     13   1.1       abs  * are met:
     14   1.1       abs  * 1. Redistributions of source code must retain the above copyright
     15   1.1       abs  *    notice, this list of conditions and the following disclaimer.
     16   1.1       abs  * 2. Redistributions in binary form must reproduce the above copyright
     17   1.1       abs  *    notice, this list of conditions and the following disclaimer in the
     18   1.1       abs  *    documentation and/or other materials provided with the distribution.
     19   1.1       abs  * 3. All advertising materials mentioning features or use of this software
     20   1.1       abs  *    must display the following acknowledgement:
     21   1.1       abs  *      This product includes software developed by the NetBSD
     22   1.1       abs  *      Foundation, Inc. and its contributors.
     23   1.1       abs  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24   1.1       abs  *    contributors may be used to endorse or promote products derived
     25   1.1       abs  *    from this software without specific prior written permission.
     26   1.1       abs  *
     27   1.1       abs  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28   1.1       abs  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29   1.1       abs  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30   1.1       abs  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31   1.1       abs  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32   1.1       abs  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33   1.1       abs  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34   1.1       abs  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35   1.1       abs  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36   1.1       abs  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37   1.1       abs  * POSSIBILITY OF SUCH DAMAGE.
     38   1.1       abs  */
     39   1.1       abs 
     40   1.1       abs #include <sys/cdefs.h>
     41  1.10  christos #if defined(LIBC_SCCS) && !defined(lint)
     42  1.10  christos __RCSID("$NetBSD: humanize_number.c,v 1.10 2005/09/13 01:44:09 christos Exp $");
     43  1.10  christos #endif /* LIBC_SCCS and not lint */
     44   1.1       abs 
     45   1.9    kleink #include "namespace.h"
     46   1.1       abs #include <assert.h>
     47   1.1       abs #include <stdio.h>
     48   1.1       abs #include <stdlib.h>
     49   1.1       abs #include <string.h>
     50   1.1       abs #include <locale.h>
     51   1.1       abs #include <util.h>
     52   1.1       abs 
     53   1.1       abs int
     54   1.1       abs humanize_number(char *buf, size_t len, int64_t bytes,
     55   1.1       abs     const char *suffix, int scale, int flags)
     56   1.1       abs {
     57   1.8     enami 	const char *prefixes, *sep;
     58   1.8     enami 	int	b, i, r, maxscale, s1, s2, sign;
     59   1.8     enami 	int64_t	divisor, max;
     60   1.8     enami 	size_t	baselen;
     61   1.1       abs 
     62   1.1       abs 	_DIAGASSERT(buf != NULL);
     63   1.1       abs 	_DIAGASSERT(suffix != NULL);
     64   1.4   thorpej 	_DIAGASSERT(scale >= 0);
     65   1.1       abs 
     66   1.3  drochner 	if (flags & HN_DIVISOR_1000) {
     67   1.3  drochner 		/* SI for decimal multiplies */
     68   1.3  drochner 		divisor = 1000;
     69   1.8     enami 		if (flags & HN_B)
     70   1.8     enami 			prefixes = "B\0k\0M\0G\0T\0P\0E";
     71   1.8     enami 		else
     72   1.8     enami 			prefixes = "\0\0k\0M\0G\0T\0P\0E";
     73   1.3  drochner 	} else {
     74   1.3  drochner 		/*
     75   1.3  drochner 		 * binary multiplies
     76   1.3  drochner 		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
     77   1.3  drochner 		 */
     78   1.3  drochner 		divisor = 1024;
     79   1.8     enami 		if (flags & HN_B)
     80   1.8     enami 			prefixes = "B\0K\0M\0G\0T\0P\0E";
     81   1.8     enami 		else
     82   1.8     enami 			prefixes = "\0\0K\0M\0G\0T\0P\0E";
     83   1.3  drochner 	}
     84   1.3  drochner 
     85   1.8     enami #define	SCALE2PREFIX(scale)	(&prefixes[(scale) << 1])
     86   1.8     enami 	maxscale = 7;
     87   1.8     enami 
     88   1.8     enami 	if (scale >= maxscale &&
     89   1.8     enami 	    (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
     90   1.1       abs 		return (-1);
     91   1.1       abs 
     92   1.1       abs 	if (buf == NULL || suffix == NULL)
     93   1.1       abs 		return (-1);
     94   1.1       abs 
     95   1.1       abs 	if (len > 0)
     96   1.1       abs 		buf[0] = '\0';
     97   1.1       abs 	if (bytes < 0) {
     98   1.1       abs 		sign = -1;
     99   1.1       abs 		bytes *= -100;
    100   1.8     enami 		baselen = 3;		/* sign, digit, prefix */
    101   1.1       abs 	} else {
    102   1.1       abs 		sign = 1;
    103   1.1       abs 		bytes *= 100;
    104   1.8     enami 		baselen = 2;		/* digit, prefix */
    105   1.1       abs 	}
    106   1.8     enami 	if (flags & HN_NOSPACE)
    107   1.8     enami 		sep = "";
    108   1.8     enami 	else {
    109   1.8     enami 		sep = " ";
    110   1.8     enami 		baselen++;
    111   1.8     enami 	}
    112   1.8     enami 	baselen += strlen(suffix);
    113   1.1       abs 
    114   1.8     enami 	/* Check if enough room for `x y' + suffix + `\0' */
    115   1.8     enami 	if (len < baselen + 1)
    116   1.1       abs 		return (-1);
    117   1.1       abs 
    118   1.8     enami 	if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
    119   1.8     enami 		/* See if there is additional columns can be used. */
    120   1.8     enami 		for (max = 100, i = len - baselen; i-- > 0;)
    121   1.8     enami 			max *= 10;
    122   1.1       abs 
    123   1.8     enami 		for (i = 0; bytes >= max && i < maxscale; i++)
    124   1.1       abs 			bytes /= divisor;
    125   1.8     enami 
    126   1.8     enami 		if (scale & HN_GETSCALE)
    127   1.8     enami 			return (i);
    128   1.8     enami 	} else
    129   1.8     enami 		for (i = 0; i < scale && i < maxscale; i++)
    130   1.1       abs 			bytes /= divisor;
    131   1.1       abs 
    132   1.8     enami 	/* If a value <= 9.9 after rounding and ... */
    133   1.8     enami 	if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
    134   1.8     enami 		/* baselen + \0 + .N */
    135   1.8     enami 		if (len < baselen + 1 + 2)
    136   1.1       abs 			return (-1);
    137   1.8     enami 		b = ((int)bytes + 5) / 10;
    138   1.8     enami 		s1 = b / 10;
    139   1.8     enami 		s2 = b % 10;
    140   1.8     enami 		r = snprintf(buf, len, "%d%s%d%s%s%s",
    141   1.8     enami 		    sign * s1, localeconv()->decimal_point, s2,
    142   1.8     enami 		    sep, SCALE2PREFIX(i), suffix);
    143   1.1       abs 	} else
    144   1.8     enami 		r = snprintf(buf, len, "%lld%s%s%s",
    145   1.1       abs 		    /* LONGLONG */
    146   1.1       abs 		    (long long)(sign * ((bytes + 50) / 100)),
    147   1.8     enami 		    sep, SCALE2PREFIX(i), suffix);
    148   1.1       abs 
    149   1.1       abs 	return (r);
    150   1.1       abs }
    151