]> git.ipfire.org Git - thirdparty/libnl.git/commitdiff
New function nl_size2str()
authorThomas Graf <tgraf@suug.ch>
Wed, 17 Nov 2010 14:13:33 +0000 (15:13 +0100)
committerThomas Graf <tgraf@suug.ch>
Wed, 17 Nov 2010 14:13:33 +0000 (15:13 +0100)
include/netlink/utils.h
lib/utils.c

index 9f38f21f7d22ea6a6871803a82e5e0010243b904..309e02f75b742a3e20d62894e542022c693cda37 100644 (file)
@@ -45,6 +45,7 @@ extern double nl_cancel_down_us(uint32_t, char **);
 
 /* generic unit translations */
 extern long    nl_size2int(const char *);
+extern char *  nl_size2str(const size_t, char *, const size_t);
 extern long    nl_prob2int(const char *);
 
 /* time translations */
index 1168293aba581306159afee6a2f196156d65eb9f..015cd6a7ec66249ae7e34886b949c534025f1281 100644 (file)
@@ -224,6 +224,56 @@ long nl_size2int(const char *str)
        return l;
 }
 
+static const struct {
+       double limit;
+       const char *unit;
+} size_units[] = {
+       { 1024. * 1024. * 1024. * 1024. * 1024., "EiB" },
+       { 1024. * 1024. * 1024. * 1024., "TiB" },
+       { 1024. * 1024. * 1024., "GiB" },
+       { 1024. * 1024., "MiB" },
+       { 1024., "KiB" },
+       { 0., "B" },
+};
+
+/**
+ * Convert a size toa character string
+ * @arg size           Size in number of bytes
+ * @arg buf            Buffer to write character string to
+ * @arg len            Size of buf
+ *
+ * This function converts a value in bytes to a human readable representation
+ * of it. The function uses IEC prefixes:
+ *
+ * @code
+ * 1024 bytes => 1 KiB
+ * 1048576 bytes => 1 MiB
+ * @endcode
+ *
+ * The highest prefix is used which ensures a result of >= 1.0, the result
+ * is provided as floating point number with a maximum precision of 2 digits:
+ * @code
+ * 965176 bytes => 942.55 KiB
+ * @endcode
+ *
+ * @return pointer to buf
+ */
+char *nl_size2str(const size_t size, char *buf, const size_t len)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(size_units); i++) {
+               if (size >= size_units[i].limit) {
+                       snprintf(buf, len, "%.2g%s",
+                               (double) size / size_units[i].limit,
+                               size_units[i].unit);
+                       return buf;
+               }
+       }
+
+       BUG();
+}
+
 /**
  * Convert a character string to a probability
  * @arg str            probability encoded as character string