-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.cpp
More file actions
71 lines (56 loc) · 1.71 KB
/
parse.cpp
File metadata and controls
71 lines (56 loc) · 1.71 KB
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
//
// Created by Linghan Xing on 10/27/18.
//
#include "parse.h"
std::vector<std::string> parsePath(std::string paths) {
std::vector<std::string> addr_book;
std::ifstream file(paths);
std::string str;
int counter = 1;
while (std::getline(file, str))
{ addr_book.push_back(str); }
file.close();
return addr_book;
}
std::tuple<std::vector<std::string>, std::string> handle_input(int argc, char **argv)
{
auto logger = spdlog::get("console");
int option;
int pflag = 0;
int hflag = 0;
std::string portNum;
char *path;
std::vector<std::string> neighbors;
while ((option = getopt(argc, argv, "p:h:")) != -1) {
switch (option) {
case 'p':
portNum = optarg;
pflag = 1;
break;
case 'h':
path = optarg;
neighbors = parsePath(path);
hflag = 1;
break;
case '?':
if (optopt == 'p')
fprintf(stderr, "Option -%c needs argument\n", optopt);
else fprintf(stderr, "Unknown option -%c. \n", optopt);
break;
default:
fprintf(stderr, "error");
}
}
if (hflag == 0 || pflag == 0) {
logger -> error("please input all required options");
exit(1);
}
if (stoi(portNum) < 1024 || stoi(portNum) > 65535) {
logger -> error("PortNumber out of range", portNum);
exit(1);
}
logger -> info("finished processing input");
logger -> info("the selected portal is: {}", portNum);
logger -> info("the selected file path is: {}", path);
return std::make_tuple(neighbors, portNum);
}