utility.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright (c) 2019 tevador
  3. This file is part of RandomX.
  4. RandomX 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. RandomX is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with RandomX. If not, see<http://www.gnu.org/licenses/>.
  14. */
  15. #pragma once
  16. #include <cstring>
  17. #include <cstdlib>
  18. #include <iostream>
  19. #include <fstream>
  20. constexpr char hexmap[] = "0123456789abcdef";
  21. inline void outputHex(std::ostream& os, const char* data, int length) {
  22. for (int i = 0; i < length; ++i) {
  23. os << hexmap[(data[i] & 0xF0) >> 4];
  24. os << hexmap[data[i] & 0x0F];
  25. }
  26. }
  27. inline void dump(const char* buffer, uint64_t count, const char* name) {
  28. std::ofstream fout(name, std::ios::out | std::ios::binary);
  29. fout.write(buffer, count);
  30. fout.close();
  31. }
  32. inline void readOption(const char* option, int argc, char** argv, bool& out) {
  33. for (int i = 0; i < argc; ++i) {
  34. if (strcmp(argv[i], option) == 0) {
  35. out = true;
  36. return;
  37. }
  38. }
  39. out = false;
  40. }
  41. inline void readIntOption(const char* option, int argc, char** argv, int& out, int defaultValue) {
  42. for (int i = 0; i < argc - 1; ++i) {
  43. if (strcmp(argv[i], option) == 0 && (out = atoi(argv[i + 1])) > 0) {
  44. return;
  45. }
  46. }
  47. out = defaultValue;
  48. }
  49. inline void readInt(int argc, char** argv, int& out, int defaultValue) {
  50. for (int i = 0; i < argc; ++i) {
  51. if (*argv[i] != '-' && (out = atoi(argv[i])) > 0) {
  52. return;
  53. }
  54. }
  55. out = defaultValue;
  56. }