forked from defuse/crackstation-hashdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
119 lines (94 loc) · 2.85 KB
/
util.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "util.h"
unsigned short getBytePower( std::streampos size ) {
unsigned short power;
for ( power = 0; size >= 1000; power++ )
size = size >> 10;
return power;
}
std::string getBytePowerPostfix( unsigned short power ) {
static const std::string postfixes[] = { " B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" };
static constexpr size_t numPostfixes = sizeof( postfixes ) / sizeof( std::string );
if ( power > numPostfixes ) {
return std::string( "2^" ) + std::to_string( power * 10 ) + postfixes[0];
} else {
return postfixes[power];
}
}
std::string getFormatedSize( std::streampos size, int power ) {
unsigned short formatPower = (power <= -1) ? getBytePower( size ) : (unsigned short)power;
std::stringstream ss;
if ( power == 0 ) {
ss << std::setw( 3 ) << size << " ";
} else {
ss << std::setw( 7 ) << std::fixed << std::setprecision( 3 ) << double( size ) / double( 1 << (10 * power) );
}
ss << ' ' << getBytePowerPostfix( formatPower );
return ss.str();
}
option::ArgStatus Arg::Long( const option::Option& option, bool msg ) {
try {
std::stol( option.arg );
return option::ARG_OK;
} catch ( const std::exception & ) {
if ( msg )
std::cerr << "Option '" << std::string( option.name, option.namelen ) << "' requires a numeric argument\n" << std::endl;
return option::ARG_ILLEGAL;
}
}
option::ArgStatus Arg::ULong( const option::Option& option, bool msg ) {
try {
std::stoul( option.arg );
return option::ARG_OK;
} catch ( const std::exception & ) {
if ( msg )
std::cerr << "Option '" << std::string( option.name, option.namelen ) << "' requires a non-negative numeric argument\n" << std::endl;
return option::ARG_ILLEGAL;
}
}
std::string & strToLower( std::string & s ) {
std::transform( s.begin(), s.end(), s.begin(), ::tolower );
return s;
}
std::string & strToUpper( std::string & s ) {
std::transform( s.begin(), s.end(), s.begin(), ::toupper );
return s;
}
std::string& removeChars( std::string& s, const std::string& chars ) {
s.erase( std::remove_if( s.begin(), s.end(), [&chars]( const char& c ) {
return chars.find( c ) != std::string::npos;
} ), s.end() );
return s;
}
std::string& keepChars( std::string& s, const std::string& chars ) {
s.erase( std::remove_if( s.begin(), s.end(), [&chars]( const char& c ) {
return chars.find( c ) == std::string::npos;
} ), s.end() );
return s;
}
byte hexCharToByte( char c ) {
switch ( c ) {
case '0': return 0x0;
case '1': return 0x1;
case '2': return 0x2;
case '3': return 0x3;
case '4': return 0x4;
case '5': return 0x5;
case '6': return 0x6;
case '7': return 0x7;
case '8': return 0x8;
case '9': return 0x9;
case 'a':
case 'A': return 0xA;
case 'b':
case 'B': return 0xB;
case 'c':
case 'C': return 0xC;
case 'd':
case 'D': return 0xD;
case 'e':
case 'E': return 0xE;
case 'f':
case 'F': return 0xF;
default: return -1;
}
}