Bladeren bron

Added prettify function to convert size in bytes to human readable form.

Branimir Karadžić 11 jaren geleden
bovenliggende
commit
d40fec5c5c
1 gewijzigde bestanden met toevoegingen van 16 en 0 verwijderingen
  1. 16 0
      include/bx/string.h

+ 16 - 0
include/bx/string.h

@@ -311,6 +311,22 @@ namespace bx
 		return _filePath;
 	}
 
+	/// Convert size in bytes to human readable string.
+	inline void prettify(char* _out, size_t _count, uint64_t _size)
+	{
+		uint8_t idx = 0;
+		double size = double(_size);
+		while (_size != (_size&0x7ff)
+		&&     idx < 9)
+		{
+			_size >>= 10;
+			size *= 1.0/1024.0;
+			++idx;
+		}
+
+		snprintf(_out, _count, "%0.2f %c%c", size, "BkMGTPEZY"[idx], idx > 0 ? 'B' : '\0');
+	}
+
 	/*
 	 * Copyright (c) 1998 Todd C. Miller <[email protected]>
 	 *