Enigma  3.2.0
A Simple, Reliable and Efficient Encryption Tool
SizeUtils.hpp
Go to the documentation of this file.
1 #pragma once
2 #ifndef ENIGMA_SIZE_UTILS_H
3 #define ENIGMA_SIZE_UTILS_H
4 
5 #include <Core/Core.hpp>
6 #include <cmath> // std::log, std::floor
7 #include <iomanip> // std::setprecision
8 #include <sstream> // std::ostringstream
9 
11 
12 class SizeUtils final {
13  ENIGMA_STATIC_CLASS(SizeUtils);
14 
15  public:
16  /*
17  * Converts bytes to human friendly readable format, e.g (100 KB, 30 Bytes, 5.23 MB...)
18  */
19  static std::string FriendlySize(const std::size_t bytes) noexcept {
20  constexpr const std::size_t KB = 1024;
21 
22  if (bytes < KB)
23  return std::to_string(bytes) + " Bytes";
24 
25  constexpr const char *sizes[]{"Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
26  const std::size_t i = static_cast<std::size_t>(std::floor(static_cast<double>(std::log(bytes) / std::log(KB))));
27 
28  std::ostringstream oss{};
29  oss << std::fixed
30  << std::setprecision(2)
31  << static_cast<float>(bytes / std::pow(KB, i))
32  << ' '
33  << sizes[i];
34 
35  return oss.str();
36  }
37 };
38 
40 #define ENIGMA_BYTES_TO_KB(bytes) (static_cast<float>(bytes) / 1024.0f)
41 #define ENIGMA_BYTES_TO_MB(bytes) (static_cast<float>(bytes) / 1024.0f / 1024.0f)
42 #define ENIGMA_BYTES_TO_GB(bytes) (static_cast<float>(bytes) / 1024.0f / 1024.0f / 1024.0f)
43 #define ENIGMA_BYTES_TO_TB(bytes) (static_cast<float>(bytes) / 1024.0f / 1024.0f / 1024.0f / 1024.0f)
44 
46 #define ENIGMA_MB_TO_BYTES(mb) (static_cast<std::size_t>(mb) * 1024 * 1024)
47 #define ENIGMA_MB_TO_KB(mb) (static_cast<std::size_t>(mb) * 1024)
48 #define ENIGMA_MB_TO_GB(mb) (static_cast<float>(mb) / 1024.0f)
49 #define ENIGMA_MB_TO_TB(mb) (static_cast<float>(mb) / 1024.0f / 1024.0f)
50 
51 #define ENIGMA_KB_TO_BYTES(kb) (static_cast<std::size_t>(kb) * 1024)
52 
54 
55 #endif // !ENIGMA_SIZE_UTILS_H
#define NS_ENIGMA_BEGIN
Enable/Disable Assertions.
Definition: Macros.hpp:13
#define NS_ENIGMA_END
Definition: Macros.hpp:14
static std::string FriendlySize(const std::size_t bytes) noexcept
Definition: SizeUtils.hpp:19