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