Home | History | Annotate | Line # | Download | only in gen
humanize_number.c revision 1.4
      1  1.4   thorpej /*	$NetBSD: humanize_number.c,v 1.4 2002/11/11 18:00:43 thorpej 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.1       abs #ifndef __lint
     42  1.1       abs __COPYRIGHT("@(#) Copyright (c) 2002\n\
     43  1.1       abs 	The NetBSD Foundation, inc. All rights reserved.\n");
     44  1.4   thorpej __RCSID("$NetBSD: humanize_number.c,v 1.4 2002/11/11 18:00:43 thorpej Exp $");
     45  1.1       abs #endif /* !__lint */
     46  1.1       abs 
     47  1.1       abs #include <assert.h>
     48  1.1       abs #include <stdio.h>
     49  1.1       abs #include <stdlib.h>
     50  1.1       abs #include <string.h>
     51  1.1       abs #include <locale.h>
     52  1.1       abs #include <util.h>
     53  1.1       abs 
     54  1.1       abs int
     55  1.1       abs humanize_number(char *buf, size_t len, int64_t bytes,
     56  1.1       abs     const char *suffix, int scale, int flags)
     57  1.1       abs {
     58  1.3  drochner 	const char *prefixes;
     59  1.1       abs 	int	i, r;
     60  1.1       abs 	int64_t	divisor, max, s1, s2, sign;
     61  1.1       abs 	size_t	baselen, suffixlen;
     62  1.1       abs 
     63  1.1       abs 	_DIAGASSERT(buf != NULL);
     64  1.1       abs 	_DIAGASSERT(suffix != NULL);
     65  1.4   thorpej 	_DIAGASSERT(scale >= 0);
     66  1.1       abs 
     67  1.3  drochner 	if (flags & HN_DIVISOR_1000) {
     68  1.3  drochner 		/* SI for decimal multiplies */
     69  1.3  drochner 		divisor = 1000;
     70  1.3  drochner 		prefixes = " kMGTPE";
     71  1.3  drochner 	} else {
     72  1.3  drochner 		/*
     73  1.3  drochner 		 * binary multiplies
     74  1.3  drochner 		 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
     75  1.3  drochner 		 */
     76  1.3  drochner 		divisor = 1024;
     77  1.3  drochner 		prefixes = " KMGTPE";
     78  1.3  drochner 	}
     79  1.3  drochner 
     80  1.4   thorpej 	if ((size_t) scale >= strlen(prefixes) && scale != HN_AUTOSCALE &&
     81  1.1       abs 	    scale != HN_GETSCALE)
     82  1.1       abs 		return (-1);
     83  1.1       abs 
     84  1.1       abs 	if (buf == NULL || suffix == NULL)
     85  1.1       abs 		return (-1);
     86  1.1       abs 
     87  1.1       abs 	if (len > 0)
     88  1.1       abs 		buf[0] = '\0';
     89  1.1       abs 	if (bytes < 0) {
     90  1.1       abs 		sign = -1;
     91  1.1       abs 		bytes *= -100;
     92  1.1       abs 		baselen = 4;
     93  1.1       abs 	} else {
     94  1.1       abs 		sign = 1;
     95  1.1       abs 		bytes *= 100;
     96  1.1       abs 		baselen = 3;
     97  1.1       abs 	}
     98  1.1       abs 
     99  1.1       abs 	suffixlen = strlen(suffix);
    100  1.1       abs 
    101  1.1       abs 	/* check if enough room for `x y' + suffix + `\0' */
    102  1.1       abs 	if (len < baselen + suffixlen + 1)
    103  1.1       abs 		return (-1);
    104  1.1       abs 
    105  1.1       abs 	if (flags & HN_DIVISOR_1000)
    106  1.1       abs 		divisor = 1000;
    107  1.1       abs 	else
    108  1.1       abs 		divisor = 1024;
    109  1.1       abs 
    110  1.1       abs 	max = 100;
    111  1.4   thorpej 	for (i = 0;
    112  1.4   thorpej 	     (size_t) i < len - suffixlen - baselen + ((flags & HN_NOSPACE) ?
    113  1.4   thorpej 	     1 : 0); i++)
    114  1.1       abs 		max *= 10;
    115  1.1       abs 
    116  1.1       abs 	if ((scale & HN_AUTOSCALE) || (scale & HN_GETSCALE)) {
    117  1.3  drochner 		for (i = 0; bytes >= max && prefixes[i + 1]; i++)
    118  1.1       abs 			bytes /= divisor;
    119  1.1       abs 	} else {
    120  1.3  drochner 		for (i = 0; i < scale && prefixes[i + 1]; i++)
    121  1.1       abs 			bytes /= divisor;
    122  1.1       abs 	}
    123  1.1       abs 
    124  1.1       abs 	if (scale & HN_GETSCALE)
    125  1.1       abs 		return (i);
    126  1.1       abs 
    127  1.1       abs 	if (bytes < 1000 && flags & HN_DECIMAL) {
    128  1.1       abs 		if (len < (baselen + 2 + ((flags & HN_NOSPACE) || (i == 0 &&
    129  1.1       abs 		    !(flags & HN_B)) ? 0 : 1)))
    130  1.1       abs 			return (-1);
    131  1.1       abs 		s1 = bytes / 100;
    132  1.1       abs 		if ((s2 = (((bytes % 100) + 5) / 10)) == 10) {
    133  1.1       abs 			s1++;
    134  1.1       abs 			s2 = 0;
    135  1.1       abs 		}
    136  1.1       abs 		r = snprintf(buf, len, "%lld%s%lld%s%c%s",
    137  1.1       abs 		    /* LONGLONG */
    138  1.1       abs 		    (long long)(sign * s1),
    139  1.1       abs 		    localeconv()->decimal_point,
    140  1.1       abs 		    /* LONGLONG */
    141  1.1       abs 		    (long long)s2,
    142  1.1       abs 		    (i == 0 && !(flags & HN_B)) || flags & HN_NOSPACE ?
    143  1.1       abs 		    "" : " ", (i == 0 && (flags & HN_B)) ? 'B' :
    144  1.1       abs 		    prefixes[i], suffix);
    145  1.1       abs 
    146  1.1       abs 	} else
    147  1.1       abs 		r = snprintf(buf, len, "%lld%s%c%s",
    148  1.1       abs 		    /* LONGLONG */
    149  1.1       abs 		    (long long)(sign * ((bytes + 50) / 100)),
    150  1.1       abs 		    i == 0 || flags & HN_NOSPACE ? "" : " ", (i == 0 &&
    151  1.1       abs 		    (flags & HN_B)) ? 'B' : prefixes[i], suffix);
    152  1.1       abs 
    153  1.1       abs 	return (r);
    154  1.1       abs }
    155