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