Monero
Loading...
Searching...
No Matches
utility.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2018-2019, tevador <tevador@gmail.com>
3
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of the copyright holder nor the
14 names of its contributors may be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#pragma once
30
31#include <cstring>
32#include <cstdlib>
33#include <iostream>
34#include <fstream>
35#include <cstdint>
36
37constexpr char hexmap[] = "0123456789abcdef";
38inline void outputHex(std::ostream& os, const char* data, int length) {
39 for (int i = 0; i < length; ++i) {
40 os << hexmap[(data[i] & 0xF0) >> 4];
41 os << hexmap[data[i] & 0x0F];
42 }
43}
44
45char parseNibble(char hex) {
46 hex &= ~0x20;
47 if (hex & 0x40) {
48 hex -= 'A' - 10;
49 }
50 else {
51 hex &= 0xf;
52 }
53 return hex;
54}
55
56void hex2bin(const char *in, int length, char *out) {
57 for (int i = 0; i < length; i += 2) {
58 char nibble1 = parseNibble(*in++);
59 char nibble2 = parseNibble(*in++);
60 *out++ = nibble1 << 4 | nibble2;
61 }
62}
63
64constexpr bool stringsEqual(char const * a, char const * b) {
65 return *a == *b && (*a == '\0' || stringsEqual(a + 1, b + 1));
66}
67
68template<size_t N>
69bool equalsHex(const void* hash, const char (&hex)[N]) {
70 char reference[N / 2];
71 hex2bin(hex, N - 1, reference);
72 return memcmp(hash, reference, sizeof(reference)) == 0;
73}
74
75inline void dump(const char* buffer, uint64_t count, const char* name) {
76 std::ofstream fout(name, std::ios::out | std::ios::binary);
77 fout.write(buffer, count);
78 fout.close();
79}
80
81inline void readOption(const char* option, int argc, char** argv, bool& out) {
82 for (int i = 0; i < argc; ++i) {
83 if (strcmp(argv[i], option) == 0) {
84 out = true;
85 return;
86 }
87 }
88 out = false;
89}
90
91inline void readIntOption(const char* option, int argc, char** argv, int& out, int defaultValue) {
92 for (int i = 0; i < argc - 1; ++i) {
93 if (strcmp(argv[i], option) == 0 && (out = atoi(argv[i + 1])) > 0) {
94 return;
95 }
96 }
97 out = defaultValue;
98}
99
100inline void readUInt64Option(const char* option, int argc, char** argv, uint64_t& out, uint64_t defaultValue) {
101 for (int i = 0; i < argc - 1; ++i) {
102 if (strcmp(argv[i], option) == 0 && (out = std::strtoull(argv[i + 1], NULL, 0)) > 0) {
103 return;
104 }
105 }
106 out = defaultValue;
107}
108
109inline void readFloatOption(const char* option, int argc, char** argv, double& out, double defaultValue) {
110 for (int i = 0; i < argc - 1; ++i) {
111 if (strcmp(argv[i], option) == 0 && (out = atof(argv[i + 1])) > 0) {
112 return;
113 }
114 }
115 out = defaultValue;
116}
117
118inline void readInt(int argc, char** argv, int& out, int defaultValue) {
119 for (int i = 0; i < argc; ++i) {
120 if (*argv[i] != '-' && (out = atoi(argv[i])) > 0) {
121 return;
122 }
123 }
124 out = defaultValue;
125}
cryptonote::block b
Definition block.cpp:40
static constexpr const char hex[]
Definition wipeable_string.cpp:36
const char * name
Definition options.c:30
const GenericPointer< typename T::ValueType > T2 defaultValue
Definition pointer.h:1124
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1124
unsigned __int64 uint64_t
Definition stdint.h:136
Definition options.h:95
Definition blockchain_usage.cpp:72
std::string data
Definition base58.cpp:37
void outputHex(std::ostream &os, const char *data, int length)
Definition utility.hpp:38
constexpr char hexmap[]
Definition utility.hpp:37
void readIntOption(const char *option, int argc, char **argv, int &out, int defaultValue)
Definition utility.hpp:91
void readUInt64Option(const char *option, int argc, char **argv, uint64_t &out, uint64_t defaultValue)
Definition utility.hpp:100
void hex2bin(const char *in, int length, char *out)
Definition utility.hpp:56
void readOption(const char *option, int argc, char **argv, bool &out)
Definition utility.hpp:81
void dump(const char *buffer, uint64_t count, const char *name)
Definition utility.hpp:75
char parseNibble(char hex)
Definition utility.hpp:45
bool equalsHex(const void *hash, const char(&hex)[N])
Definition utility.hpp:69
constexpr bool stringsEqual(char const *a, char const *b)
Definition utility.hpp:64
void readInt(int argc, char **argv, int &out, int defaultValue)
Definition utility.hpp:118
void readFloatOption(const char *option, int argc, char **argv, double &out, double defaultValue)
Definition utility.hpp:109