]> git.ipfire.org Git - thirdparty/grub.git/commitdiff
Factor-out human-size printing.
authorVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sun, 5 May 2013 09:31:53 +0000 (11:31 +0200)
committerVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sun, 5 May 2013 09:31:53 +0000 (11:31 +0200)
ChangeLog
grub-core/commands/ls.c
grub-core/normal/misc.c
include/grub/normal.h

index 57bce500814b8bb29cbea24e43598414c109d551..c9e6f06a534bb6d0982907f19968545ff7d85d92 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2013-05-05  Vladimir Serbinenko  <phcoder@gmail.com>
+
+       Factor-out human-size printing.
+
 2013-05-04  Vladimir Serbinenko  <phcoder@gmail.com>
 
        Agglomerate more mallocs to speed-up gfxterm.
index 83930aabf4a735998bcf19dbfe769a6c248fbcd7..6c608f6da87808fe08aea22c4736b338cd2060f7 100644 (file)
@@ -43,8 +43,6 @@ static const struct grub_arg_option options[] =
     {0, 0, 0, 0, 0, 0}
   };
 
-static const char grub_human_sizes[] = {' ', 'K', 'M', 'G', 'T'};
-
 /* Helper for grub_ls_list_devices.  */
 static int
 grub_ls_print_devices (const char *name, void *data)
@@ -143,34 +141,7 @@ print_files_long (const char *filename, const struct grub_dirhook_info *info,
       if (! ctx->human)
        grub_printf ("%-12llu", (unsigned long long) file->size);
       else
-       {
-         grub_uint64_t fsize = file->size * 100ULL;
-         grub_uint64_t fsz = file->size;
-         int units = 0;
-         char buf[20];
-
-         while (fsz / 1024)
-           {
-             fsize = (fsize + 512) / 1024;
-             fsz /= 1024;
-             units++;
-           }
-
-         if (units)
-           {
-             grub_uint64_t whole, fraction;
-
-             whole = grub_divmod64 (fsize, 100, &fraction);
-             grub_snprintf (buf, sizeof (buf),
-                            "%" PRIuGRUB_UINT64_T
-                            ".%02" PRIuGRUB_UINT64_T "%c", whole, fraction,
-                            grub_human_sizes[units]);
-             grub_printf ("%-12s", buf);
-           }
-         else
-           grub_printf ("%-12llu", (unsigned long long) file->size);
-
-       }
+       grub_printf ("%-12s", grub_get_human_size (file->size, 1));
       grub_file_close (file);
       grub_free (pathname);
     }
index d23de62e6a34ac9032e3c7fd48ec78e56dce8565..1a86e0f8c8fb4a93033c1b40cc80a170125735f6 100644 (file)
 #include <grub/i18n.h>
 #include <grub/partition.h>
 
+static const char *grub_human_sizes[] = {N_("B"), N_("KiB"), N_("MiB"), N_("GiB"), N_("TiB")};
+static const char *grub_human_short_sizes[] = {"", "K", "M", "G", "T"};
+
+const char *
+grub_get_human_size (grub_uint64_t size, int sh)
+{
+  grub_uint64_t fsize = size * 100ULL;
+  grub_uint64_t fsz = size;
+  int units = 0;
+  static char buf[20];
+
+  while (fsz / 1024)
+    {
+      fsize = (fsize + 512) / 1024;
+      fsz /= 1024;
+      units++;
+    }
+
+  if (units)
+    {
+      grub_uint64_t whole, fraction;
+
+      whole = grub_divmod64 (fsize, 100, &fraction);
+      grub_snprintf (buf, sizeof (buf),
+                    "%" PRIuGRUB_UINT64_T
+                    ".%02" PRIuGRUB_UINT64_T "%s", whole, fraction,
+                    sh ? grub_human_short_sizes[units] : _(grub_human_sizes[units]));
+    }
+  else
+    grub_snprintf (buf, sizeof (buf), "%llu%s", (unsigned long long) size,
+                  sh ? grub_human_short_sizes[units] : _(grub_human_sizes[units]));
+  return buf;
+}
+
 /* Print the information on the device NAME.  */
 grub_err_t
 grub_normal_print_device_info (const char *name)
index 4fcc3da66cabfbff30617c6408d3b05421eee5b9..930b3b93e23d87f66afc0d26f5188ff6bfb0e970 100644 (file)
@@ -150,4 +150,7 @@ grub_dyncmd_get_cmd (grub_command_t cmd);
 void
 grub_gettext_reread_prefix (const char *val);
 
+const char *
+grub_get_human_size (grub_uint64_t size, int sh);
+
 #endif /* ! GRUB_NORMAL_HEADER */