+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.
{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)
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);
}
#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)
void
grub_gettext_reread_prefix (const char *val);
+const char *
+grub_get_human_size (grub_uint64_t size, int sh);
+
#endif /* ! GRUB_NORMAL_HEADER */