-
Notifications
You must be signed in to change notification settings - Fork 0
/
param_parser.cpp
74 lines (67 loc) · 1.82 KB
/
param_parser.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
#include "param_parser.h"
#include "help.h"
#include <iostream>
using namespace std;
#include <sys/stat.h>
input_param parse_options(unsigned char argc, char *argv[])
{
input_param options;
if (argc == 1)
{ //invalid
return options;
}
else
{
for (unsigned i = 1; i <= argc; ++i)
{
if (argv[i] == string("-o") || argv[i] == string("--output"))
{
if ((i + 1) < argc) //make sure array bounds not exceeded
options.output_file = argv[++i];
else //invalid
return options;
}
else if (argv[i] == string("-d") || argv[i] == string("--decode"))
options.encode = false;
else if (argv[i] == string("-v") || argv[i] == string("--verbose"))
options.verbose = true;
else
{
options.input_file = argv[i];
break;
}
}
//check if input file exists
if (options.input_file == "")
{
return options;
}
struct stat file_info;
if (stat(options.input_file.c_str(), &file_info) == 0)
{
options.input_file_size = file_info.st_size;
}
else
return options;
//return the parsed options
options.invalid = false;
if (!options.encode && options.output_file == "a.huff")
options.output_file = "a.dhuff";
return options;
}
}
bool show_help_if_option_invalid(input_param options)
{
if (options.invalid)
{
print_help();
return true;
}
if (options.input_file_size == 0)
{
print_help("Input file doesn't exist.");
print_help();
return true;
}
return false;
}