/
main.cpp
111 lines (94 loc) · 2.65 KB
/
main.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
/*
* (C) 2014 Mike Tzou
*
* This file is part of "Mask calculator".
*
* "Mask calculator" is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* "Mask calculator" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <cstdio>
#include <cstring>
#include "greedy_merge.hpp"
#include "qmm.hpp"
void printUsage( const std::string &a );
void printArg( const int argc , char *argv[] );
int main( const int argc , char *argv[] )
{
// input handling
if( argc < 2 )
{
printUsage( argv[0] );
exit( EXIT_FAILURE );
}
// GreedyMerge my_calc;
QMM my_calc;
// get/sort numbers
for( auto i = 1 ; i < argc ; ++i )
{
// check if is range of numbers
const char *if_range = strchr( argv[i] , '-' );
if( if_range != NULL )
{
// check for invalid input
if( ( if_range == argv[i] ) || // first char is delimiter
( *( if_range + 1 ) == '\0' ) || // last char is delimiter
( *( if_range + 1 ) == '-' ) ) // 2 delimiters
{
printUsage( argv[0] );
fprintf( stderr, "input invalid: %s\n" , argv[i] );
exit( EXIT_FAILURE );
}
const uint32_t a = strtoul( argv[i] , NULL , 0 );
const uint32_t b = strtoul( if_range + 1 , NULL , 0 );
my_calc.addNum( a, b );
}
else
{
// single number
const uint32_t a = strtoul( argv[i] , NULL , 0 );
my_calc.addNum( a );
}
}
my_calc.doCalc();
// pretty output
size_t counter = 1;
auto *results = my_calc.getOutput();
printf( "\nvalue(dec) / mask(hex)\n" );
for( const auto &i : ( *results ) )
{
for( const auto &j : i.second )
printf( "%zu: %u / %x\n" , counter++ , j , i.first );
}
// system( "pause" );
return 0;
}
void printUsage( const std::string &a )
{
fprintf( stderr , "\n" );
fprintf( stderr , "Mask calculator\n\n" );
fprintf( stderr , "Usage: %s value1 [value2 ...]\n" , a.c_str() );
fprintf( stderr , "Example1: %s 50 100\n" , a.c_str() );
fprintf( stderr , "Example2: %s 50-100\n" , a.c_str() );
fprintf( stderr , "Example3: %s 50 100 1000-1020\n\n" , a.c_str() );
return;
}
void printArg( const int argc , char *argv[] )
{
printf( "argc: %d\n" , argc );
for( auto i = 1 ; i < argc ; ++i )
{
printf( "argv[%d]: %s\n" , i , argv[i] );
}
return;
}