You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
2.7 KiB

  1. /*
  2. Copyright (c) 2016, Antonio SJ Musumeci <trapexit@spawn.link>
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted, provided that the above
  5. copyright notice and this permission notice appear in all copies.
  6. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  7. WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  8. MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  9. ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  10. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  11. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  12. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  13. */
  14. #include "ef.hpp"
  15. #include <inttypes.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <time.h>
  19. #include <cstdint>
  20. #include <string>
  21. #define KB (1024ULL)
  22. #define MB (KB * 1024ULL)
  23. #define GB (MB * 1024ULL)
  24. #define TB (GB * 1024ULL)
  25. namespace num
  26. {
  27. int
  28. to_uint64_t(const std::string &str,
  29. uint64_t &value)
  30. {
  31. char *endptr;
  32. uint64_t tmp;
  33. tmp = strtoll(str.c_str(),&endptr,10);
  34. switch(*endptr)
  35. {
  36. case 'k':
  37. case 'K':
  38. tmp *= 1024;
  39. break;
  40. case 'm':
  41. case 'M':
  42. tmp *= (1024 * 1024);
  43. break;
  44. case 'g':
  45. case 'G':
  46. tmp *= (1024 * 1024 * 1024);
  47. break;
  48. case 't':
  49. case 'T':
  50. tmp *= (1024ULL * 1024 * 1024 * 1024);
  51. break;
  52. case '\0':
  53. break;
  54. default:
  55. return -1;
  56. }
  57. value = tmp;
  58. return 0;
  59. }
  60. int
  61. to_double(const std::string &str_,
  62. double *d_)
  63. {
  64. double tmp;
  65. char *endptr;
  66. tmp = strtod(str_.c_str(),&endptr);
  67. if(*endptr != '\0')
  68. return -1;
  69. *d_ = tmp;
  70. return 0;
  71. }
  72. int
  73. to_time_t(const std::string &str,
  74. time_t &value)
  75. {
  76. time_t tmp;
  77. char *endptr;
  78. tmp = strtoll(str.c_str(),&endptr,10);
  79. if(*endptr != '\0')
  80. return -1;
  81. if(tmp < 0)
  82. return -1;
  83. value = tmp;
  84. return 0;
  85. }
  86. }
  87. namespace num
  88. {
  89. std::string
  90. humanize(const uint64_t bytes_)
  91. {
  92. char buf[64];
  93. if(bytes_ < KB)
  94. sprintf(buf,"%" PRIu64 "",bytes_);
  95. ef(((bytes_ / TB) * TB) == bytes_)
  96. sprintf(buf,"%" PRIu64 "T",bytes_ / TB);
  97. ef(((bytes_ / GB) * GB) == bytes_)
  98. sprintf(buf,"%" PRIu64 "G",bytes_ / GB);
  99. ef(((bytes_ / MB) * MB) == bytes_)
  100. sprintf(buf,"%" PRIu64 "M",bytes_ / MB);
  101. ef(((bytes_ / KB) * KB) == bytes_)
  102. sprintf(buf,"%" PRIu64 "K",bytes_ / KB);
  103. else
  104. sprintf(buf,"%" PRIu64 "",bytes_);
  105. return std::string(buf);
  106. }
  107. }