utils.hh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2018-2020 Stefan Westerfeld
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef AUDIOWMARK_UTILS_HH
  18. #define AUDIOWMARK_UTILS_HH
  19. #include <vector>
  20. #include <string>
  21. std::vector<int> bit_str_to_vec (const std::string& bits);
  22. std::string bit_vec_to_str (const std::vector<int>& bit_vec);
  23. std::vector<unsigned char> hex_str_to_vec (const std::string& str);
  24. std::string vec_to_hex_str (const std::vector<unsigned char>& vec);
  25. double get_time();
  26. template<typename T>
  27. inline const T&
  28. bound (const T& min_value, const T& value, const T& max_value)
  29. {
  30. return std::min (std::max (value, min_value), max_value);
  31. }
  32. // detect compiler
  33. #if __clang__
  34. #define AUDIOWMARK_COMP_CLANG
  35. #elif __GNUC__ > 2
  36. #define AUDIOWMARK_COMP_GCC
  37. #else
  38. #error "unsupported compiler"
  39. #endif
  40. #ifdef AUDIOWMARK_COMP_GCC
  41. #define AUDIOWMARK_PRINTF(format_idx, arg_idx) __attribute__ ((__format__ (gnu_printf, format_idx, arg_idx)))
  42. #else
  43. #define AUDIOWMARK_PRINTF(format_idx, arg_idx) __attribute__ ((__format__ (__printf__, format_idx, arg_idx)))
  44. #endif
  45. void error (const char *format, ...) AUDIOWMARK_PRINTF (1, 2);
  46. void warning (const char *format, ...) AUDIOWMARK_PRINTF (1, 2);
  47. void info (const char *format, ...) AUDIOWMARK_PRINTF (1, 2);
  48. void debug (const char *format, ...) AUDIOWMARK_PRINTF (1, 2);
  49. enum class Log { ERROR = 3, WARNING = 2, INFO = 1, DEBUG = 0 };
  50. void set_log_level (Log level);
  51. std::string string_printf (const char *fmt, ...) AUDIOWMARK_PRINTF (1, 2);
  52. class Error
  53. {
  54. public:
  55. enum class Code {
  56. NONE,
  57. STR
  58. };
  59. Error (Code code = Code::NONE) :
  60. m_code (code)
  61. {
  62. switch (code)
  63. {
  64. case Code::NONE:
  65. m_message = "OK";
  66. break;
  67. default:
  68. m_message = "Unknown error";
  69. }
  70. }
  71. explicit
  72. Error (const std::string& message) :
  73. m_code (Code::STR),
  74. m_message (message)
  75. {
  76. }
  77. Code
  78. code()
  79. {
  80. return m_code;
  81. }
  82. const char *
  83. message()
  84. {
  85. return m_message.c_str();
  86. }
  87. operator bool()
  88. {
  89. return m_code != Code::NONE;
  90. }
  91. private:
  92. Code m_code;
  93. std::string m_message;
  94. };
  95. class ScopedFile
  96. {
  97. FILE *m_file;
  98. public:
  99. ScopedFile (FILE *f) :
  100. m_file (f)
  101. {
  102. }
  103. ~ScopedFile()
  104. {
  105. if (m_file)
  106. fclose (m_file);
  107. }
  108. };
  109. #endif /* AUDIOWMARK_UTILS_HH */