OSDN Git Service

Added facilities for printing bytes values in human-readable format (i.e. with SI...
authorresver <resver@60bc1c72-a15a-11de-b98f-4500b42dc123>
Tue, 9 Nov 2010 18:13:41 +0000 (18:13 +0000)
committerresver <resver@60bc1c72-a15a-11de-b98f-4500b42dc123>
Tue, 9 Nov 2010 18:13:41 +0000 (18:13 +0000)
git-svn-id: http://exfat.googlecode.com/svn/trunk@174 60bc1c72-a15a-11de-b98f-4500b42dc123

libexfat/exfat.h
libexfat/utils.c

index a5614b2..035afa1 100644 (file)
@@ -100,6 +100,12 @@ struct exfat_iterator
        struct exfat_node* current;
 };
 
+struct exfat_human_bytes
+{
+       uint64_t value;
+       const char* unit;
+};
+
 extern int exfat_errors;
 
 void exfat_bug(const char* format, ...)
@@ -147,6 +153,7 @@ uint16_t exfat_add_checksum(const void* entry, uint16_t sum);
 le16_t exfat_calc_checksum(const struct exfat_entry_meta1* meta1,
                const struct exfat_entry_meta2* meta2, const le16_t* name);
 le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name);
+void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb);
 
 int utf16_to_utf8(char* output, const le16_t* input, size_t outsize,
                size_t insize);
index 53d4e86..edb45aa 100644 (file)
@@ -228,3 +228,19 @@ le16_t exfat_calc_name_hash(const struct exfat* ef, const le16_t* name)
        }
        return cpu_to_le16(hash);
 }
+
+void exfat_humanize_bytes(uint64_t value, struct exfat_human_bytes* hb)
+{
+       size_t i;
+       const char* units[] = {"bytes", "KB", "MB", "GB", "TB", "PB"};
+       uint64_t divisor = 1;
+
+       for (i = 0; i < sizeof(units) / sizeof(units[0]) - 1; i++)
+       {
+               if ((value + divisor / 2) / divisor < 1024)
+                       break;
+               divisor *= 1024;
+       }
+       hb->value = (value + divisor / 2) / divisor;
+       hb->unit = units[i];
+}