commit:     a235b67877128a5ab23388cabb0a31bf3502094e
Author:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
AuthorDate: Sun Mar  9 21:07:24 2014 +0000
Commit:     Mike Frysinger <vapier <AT> gentoo <DOT> org>
CommitDate: Sun Mar  9 21:07:24 2014 +0000
URL:        
http://git.overlays.gentoo.org/gitweb/?p=proj/portage-utils.git;a=commit;h=a235b678

use localized number formats

NLS becomes a proper compile time option and we use that to print numbers
in a more natural format.

If people want raw format for scripts, you can set LC_ALL=C.

URL: https://bugs.gentoo.org/503646

---
 Makefile              |  2 ++
 libq/human_readable.c |  6 +++---
 libq/i18n.h           |  4 +++-
 main.c                |  3 +--
 qmerge.c              |  2 +-
 qsize.c               | 18 ++++++++++--------
 6 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/Makefile b/Makefile
index 2889fe2..d202f54 100644
--- a/Makefile
+++ b/Makefile
@@ -4,6 +4,7 @@
 
 check_gcc=$(shell if $(CC) $(1) -S -o /dev/null -xc /dev/null > /dev/null 
2>&1; \
        then echo "$(1)"; else echo "$(2)"; fi)
+istrue = $(if $(filter 1 yes true on,$(strip $1)),1,0)
 
 ####################################################
 WFLAGS    := -Wall -Wunused -Wimplicit -Wshadow -Wformat=2 \
@@ -18,6 +19,7 @@ WFLAGS    := -Wall -Wunused -Wimplicit -Wshadow -Wformat=2 \
 CFLAGS    ?= -O2 -g -pipe
 CFLAGS    += -std=gnu99
 CPPFLAGS  ?=
+CPPFLAGS  += -DENABLE_NLS=$(call istrue,$(NLS))
 #CFLAGS   += -DEBUG -g
 #CFLAGS   += -Os -DOPTIMIZE_FOR_SIZE=2 -falign-functions=2 -falign-jumps=2 
-falign-labels=2 -falign-loops=2
 #LDFLAGS  := -pie

diff --git a/libq/human_readable.c b/libq/human_readable.c
index bdef428..3a8aa34 100644
--- a/libq/human_readable.c
+++ b/libq/human_readable.c
@@ -38,8 +38,8 @@ const char *make_human_readable_str(unsigned long long size,
 {
        /* The code will adjust for additional (appended) units. */
        static const char zero_and_units[] = { '0', 0, 'k', 'M', 'G', 'T' };
-       static const char fmt[] = "%Lu";
-       static const char fmt_tenths[] = "%Lu.%d%c";
+       static const char fmt[] = "%'Lu";
+       static const char fmt_tenths[] = "%'Lu%s%d%c";
 
        static char str[21];            /* Sufficient for 64 bit unsigned 
integers. */
 
@@ -85,7 +85,7 @@ const char *make_human_readable_str(unsigned long long size,
        }
 
        /* If f==fmt then 'frac' and 'u' are ignored. */
-       snprintf(str, sizeof(str), f, val, frac, *u);
+       snprintf(str, sizeof(str), f, val, decimal_point, frac, *u);
 
        return str;
 }

diff --git a/libq/i18n.h b/libq/i18n.h
index d26713c..50f36ea 100644
--- a/libq/i18n.h
+++ b/libq/i18n.h
@@ -1,15 +1,17 @@
 #ifndef _I18N_H
 #define _I18N_H
 
-#ifdef ENABLE_NLS
+#if ENABLE_NLS
 # include <locale.h>
 # include <libintl.h>
 # define _(String) gettext (String)
+# define decimal_point localeconv()->decimal_point
 #else
 # define _(String) (String)
 # define setlocale(x,y)
 # define bindtextdomain(x,y)
 # define textdomain(x)
+# define decimal_point "."
 #endif
 
 #endif

diff --git a/main.c b/main.c
index 2fd242f..bb510a8 100644
--- a/main.c
+++ b/main.c
@@ -1293,11 +1293,10 @@ int main(int argc, char **argv)
        IF_DEBUG(init_coredumps());
        argv0 = argv[0];
 
-#ifdef ENABLE_NLS      /* never tested */
        setlocale(LC_ALL, "");
        bindtextdomain(argv0, CONFIG_EPREFIX "usr/share/locale");
        textdomain(argv0);
-#endif
+
 #if 1
        if (fstat(fileno(stdout), &st) != -1)
                if (!isatty(fileno(stdout)))

diff --git a/qmerge.c b/qmerge.c
index 3aab0ce..572365e 100644
--- a/qmerge.c
+++ b/qmerge.c
@@ -1425,7 +1425,7 @@ print_Pkg(int full, struct pkg_t *pkg)
        printf("%s%s/%s%s%s%s%s%s\n", BOLD, pkg->CATEGORY, BLUE, pkg->PF, NORM,
                !quiet ? " [" : "",
                !quiet ? make_human_readable_str(pkg->SIZE, 1, KILOBYTE) : "",
-               !quiet ? "KB]" : "");
+               !quiet ? "KiB]" : "");
 
        if (full == 0)
                return;

diff --git a/qsize.c b/qsize.c
index 3cecccb..83ad8b1 100644
--- a/qsize.c
+++ b/qsize.c
@@ -62,8 +62,8 @@ int qsize_main(int argc, char **argv)
                case 'a': search_all = 1; break;
                case 's': summary = 1; break;
                case 'S': summary = summary_only = 1; break;
-               case 'm': disp_units = MEGABYTE; str_disp_units = "MB"; break;
-               case 'k': disp_units = KILOBYTE; str_disp_units = "KB"; break;
+               case 'm': disp_units = MEGABYTE; str_disp_units = "MiB"; break;
+               case 'k': disp_units = KILOBYTE; str_disp_units = "KiB"; break;
                case 'b': disp_units = 1; str_disp_units = "bytes"; break;
                case 'i': ignore_regexp = add_set(optarg, optarg, 
ignore_regexp); break;
                }
@@ -139,19 +139,20 @@ int qsize_main(int argc, char **argv)
                        num_all_ignored += num_ignored;
 
                        if (!summary_only) {
-                               printf("%s%s/%s%s%s: %lu files, %lu non-files, 
", BOLD,
+                               printf("%s%s/%s%s%s: %'lu files, %'lu 
non-files, ", BOLD,
                                       catname, BLUE, pkgname, NORM,
                                       (unsigned long)num_files,
                                       (unsigned long)num_nonfiles);
                                if (num_ignored)
-                                       printf("%lu names-ignored, ", (unsigned 
long)num_ignored);
+                                       printf("%'lu names-ignored, ", 
(unsigned long)num_ignored);
                                if (disp_units)
                                        printf("%s %s\n",
                                               
make_human_readable_str(num_bytes, 1, disp_units),
                                               str_disp_units);
                                else
-                                       printf("%lu.%lu KB\n",
+                                       printf("%'lu%s%lu KiB\n",
                                               (unsigned long)(num_bytes / 
KILOBYTE),
+                                              decimal_point,
                                               (unsigned 
long)(((num_bytes%KILOBYTE)*1000)/KILOBYTE));
                        }
 
@@ -161,18 +162,19 @@ int qsize_main(int argc, char **argv)
        }
 
        if (summary) {
-               printf(" %sTotals%s: %lu files, %lu non-files, ", BOLD, NORM,
+               printf(" %sTotals%s: %'lu files, %'lu non-files, ", BOLD, NORM,
                       (unsigned long)num_all_files,
                       (unsigned long)num_all_nonfiles);
                if (num_all_ignored)
-                       printf("%lu names-ignored, ", (unsigned 
long)num_all_ignored);
+                       printf("%'lu names-ignored, ", (unsigned 
long)num_all_ignored);
                if (disp_units)
                        printf("%s %s\n",
                               make_human_readable_str(num_all_bytes, 1, 
disp_units),
                               str_disp_units);
                else
-                       printf("%lu.%lu MB\n",
+                       printf("%'lu%s%lu MiB\n",
                               (unsigned long)(num_all_bytes / MEGABYTE),
+                              decimal_point,
                               (unsigned 
long)(((num_all_bytes%MEGABYTE)*1000)/MEGABYTE));
        }
        free_sets(ignore_regexp);

Reply via email to