Client-server through DBus, cmake support
[qtrapids] / src / include / qtrapids / format.hpp
diff --git a/src/include/qtrapids/format.hpp b/src/include/qtrapids/format.hpp
new file mode 100644 (file)
index 0000000..8094101
--- /dev/null
@@ -0,0 +1,52 @@
+#ifndef _QTRAPIDS_FORMAT_HPP_
+#define _QTRAPIDS_FORMAT_HPP_
+
+#include <qtrapids/info.hpp>
+#include <QtCore/QString>
+
+
+
+namespace qtrapids
+{
+
+   
+    static inline QString formatProgress(uint progress)
+    {
+        return QString::number(((double)progress) / torrent_progress_max * 100);
+    }
+
+    namespace 
+    {
+
+        static const qulonglong size_KB = 1024;
+        static const qulonglong size_MB = size_KB << 10;
+        static const qulonglong size_GB = size_MB << 10;
+
+        static char const* size_names[] = {
+            "GB",
+            "MB",
+            "KB",
+            "B"
+        };
+    }
+
+    static inline QString formatSize(qulonglong size)
+    {
+        qulonglong unit = size_GB;
+        char const ** unit_name = &size_names[0];
+        QString ret("");
+        for (unit = size_GB; unit > 0; unit >>= 10, ++unit_name) {
+            if (size & (~(unit - 1))) {
+                ret += (QString::number(size / unit) + *unit_name);
+                return ret;
+            }
+        }
+        ret += (QString::number(size / unit) + "B");
+        return ret;
+    }
+
+
+} // namespace qtrapids
+
+#endif // _QTRAPIDS_FORMAT_HPP_
+