All of lore.kernel.org
 help / color / mirror / Atom feed
From: Goffredo Baroncelli <kreijack@gmail.com>
To: kreijack@gmail.com
Cc: Hugo Mills <hugo@carfax.org.uk>,
	Chris Mason <chris.mason@fusionio.com>,
	linux-btrfs@vger.kernel.org, David Sterba <dave@jikos.cz>,
	Martin Steigerwald <Martin@lichtvoll.de>,
	Goffredo Baroncelli <kreijack@inwind.it>
Subject: [PATCH 1/3] Add support for different unit.
Date: Sat, 13 Oct 2012 21:27:14 +0200	[thread overview]
Message-ID: <1350156436-14439-2-git-send-email-kreijack@gmail.com> (raw)
In-Reply-To: <1350156436-14439-1-git-send-email-kreijack@gmail.com>

From: Goffredo Baroncelli <kreijack@inwind.it>

The function pretty_sizes() returns a string containing the passed
number. It add a suffix depending by the number: eg KiB, MiB.
This change replace the old SI suffix (KB, MB..) by the IEC
ones (KiB, MiB..). Moreover a space is added between the suffix
and the number.

Setting opprtunately the enviroment variable BTRFS_UNIT, it is
possible to:
BTRFS_UNIT=SI the suffix is KB for 1000bytes, MB for 10^6 bytes...
BTRFS_UNIT=IEC the suffix is KiB for 1024bytes, MiB for 1024 KiB ...
BTRFS_UNIT=COMPACT the suffix is KB for 1024 bytes, MB for 1024 KiB;
                   no space between the number and the suffix.

See http://en.wikipedia.org/wiki/Byte for further information about
the different suffix.
---
 utils.c |   70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 61 insertions(+), 9 deletions(-)

diff --git a/utils.c b/utils.c
index 205e667..8528cc1 100644
--- a/utils.c
+++ b/utils.c
@@ -1085,33 +1085,85 @@ out:
 	return ret;
 }
 
-static char *size_strs[] = { "", "KB", "MB", "GB", "TB",
+static char *size_strs_inf[] = { "", "KiB", "MiB", "GiB", "TiB",
+			    "PiB", "EiB", "ZiB", "YiB"};
+static char *size_strs_dec[] = { "", "KB", "MB", "GB", "TB",
 			    "PB", "EB", "ZB", "YB"};
+
+static int which_unit( ){
+	static int unit=-1;
+	char	*u;
+
+	if( unit != -1 )
+		return unit;
+
+	unit = 0;
+
+	u =getenv("BTRFS_UNIT");
+	if(!u) return 0;
+
+	if( !strcmp(u,"SI") )
+		unit = 1;
+	else if( !strcmp(u, "COMPACT") )
+		unit = 2;
+	else if( !strcmp(u, "IEC") )
+		unit = 0;
+	/* else
+		Shall we raise an error ? 
+	*/
+		
+	return unit;
+
+}
+
+
 char *pretty_sizes(u64 size)
 {
 	int num_divs = 0;
-        int pretty_len = 16;
+        int pretty_len = 20;
 	float fraction;
-	char *pretty;
+	char *pretty, *space;
+	int  shift = 1024;
+	char **size_strs;
+
+	if( which_unit() == 1 ){		/* SI */
+		shift = 1000;
+		size_strs = size_strs_dec;
+		space = " ";
+	} else if( which_unit() == 2 ){		/* Old method:
+						   SI suffix, but 
+						   multiply of 1024 */
+		shift = 1024;
+		size_strs = size_strs_dec;
+		space = "";
+	}else{
+		shift = 1024;			/* IEC */
+		size_strs = size_strs_inf;
+		space = " ";
+	}
 
-	if( size < 1024 ){
+	if( size < shift ){
 		fraction = size;
 		num_divs = 0;
 	} else {
 		u64 last_size = size;
 		num_divs = 0;
-		while(size >= 1024){
+		while(size >= shift){
 			last_size = size;
-			size /= 1024;
+			size /= shift;
 			num_divs ++;
 		}
 
-		if (num_divs > ARRAY_SIZE(size_strs))
+		if (num_divs > ARRAY_SIZE(size_strs_inf))
 			return NULL;
-		fraction = (float)last_size / 1024;
+		fraction = (float)last_size / shift;
 	}
 	pretty = malloc(pretty_len);
-	snprintf(pretty, pretty_len, "%.2f%s", fraction, size_strs[num_divs]);
+
+	snprintf(pretty, pretty_len, "%.2f%s%s", 
+		fraction, 
+		space,
+		size_strs[num_divs]);
 	return pretty;
 }
 
-- 
1.7.10.4


  reply	other threads:[~2012-10-13 19:27 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-13 19:27 [PATCH][V1][BTRFS-PROGS] Replace the units from KB to KiB Goffredo Baroncelli
2012-10-13 19:27 ` Goffredo Baroncelli [this message]
2012-10-15 11:47   ` [PATCH 1/3] Add support for different unit Martin Steigerwald
2012-10-13 19:27 ` [PATCH 2/3] Deleted the byte prefix with pretty_sizes() Goffredo Baroncelli
2012-10-13 19:27 ` [PATCH 3/3] Document the use of BTRFS_UNIT in man page Goffredo Baroncelli
2012-10-15 11:58 ` [PATCH][V1][BTRFS-PROGS] Replace the units from KB to KiB Martin Steigerwald
2012-10-15 17:02   ` Goffredo Baroncelli

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1350156436-14439-2-git-send-email-kreijack@gmail.com \
    --to=kreijack@gmail.com \
    --cc=Martin@lichtvoll.de \
    --cc=chris.mason@fusionio.com \
    --cc=dave@jikos.cz \
    --cc=hugo@carfax.org.uk \
    --cc=kreijack@inwind.it \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.