-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.cpp
90 lines (78 loc) · 2.24 KB
/
parse.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
//
// Created by Linghan Xing on 10/1/18.
//
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <vector>
#include <string>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <fstream>
#include <iostream>
#include <unordered_map>
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);
}
// for( const auto& n : addr_book ) {
// std::cout << n << endl;
// }
file.close();
return addr_book;
}
std::tuple<std::vector<std::string>, std::string, int> handle_input(int argc, char **argv)
{
auto logger = spdlog::get("console");
int option;
int pflag = 0;
int hflag = 0;
int cflag = 0;
std::string portNum;
char *path;
int count = 0;
std::vector<std::string> neighbors;
while ((option = getopt(argc, argv, "p:h:c:")) != -1) {
switch (option) {
case 'p':
portNum = optarg;
pflag = 1;
break;
case 'h':
path = optarg;
neighbors = parsePath(path);
hflag = 1;
break;
case 'c':
count = atoi(optarg);
cflag = 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 (cflag == 0 || 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 count is: {}", count);
logger -> info("the selected file path is: {}", path);
return std::make_tuple(neighbors, portNum, count);
}