-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.cpp
78 lines (69 loc) · 1.78 KB
/
field.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
#include <iostream>
#include <cstring>
#include "field.h"
#include "ip.h"
#include "port.h"
#include "Include_string/string.h"///////DONT FORGET TO CHANGE!!!!!!!!!!!
Field::Field(String pattern, field_type type) {
this->type = type;
this->pattern = pattern;
}
Field::Field(String pattern) {
this->type = GENERIC;
this->pattern = pattern;
}
Field::~Field() {
// empty
}
field_type Field::get_type() const {
return type;
}
bool Field::set_value(String val) {
field_type our_type = get_type();
bool is_success;
if (our_type == IP) {
Field *field_ip = new Ip(pattern);
Ip *ip = (Ip*)field_ip;
is_success = ip->set_value(val);
} else if (our_type == PORT) {
Field *field_port = new Port(pattern);
Port *port = (Port*)field_port;
is_success = port->set_value(val);
} else {
is_success = false;
}
//who should delete Ip and port?
return is_success;
}
bool Field::match_value(String val) const {
field_type our_type = get_type();
bool is_success;
if (our_type == IP) {
Field *field_ip = new Ip(pattern);
Ip *ip = (Ip*)field_ip;
is_success = ip->match_value(val);
} else if (our_type == PORT) {
Field *field_port = new Port(pattern);
Port *port = (Port*)field_port;
is_success = port->match_value(val);
} else {
is_success = false;
}
//who should delete Ip and port?
return is_success;
}
bool Field::match(String packet) {
String *packet_parts;
size_t size_of_arr;
packet.split(",=", &packet_parts, &size_of_arr);
bool is_equal = false;
bool are_matching =false;
for (size_t i = 0; i < (size_of_arr/2); ++i) {
is_equal = pattern.equals(packet_parts[2*i].trim());
if (is_equal) {
are_matching = match_value(packet_parts[2 * i + 1].trim());
}
}
delete[] packet_parts;
return are_matching;
}