-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataFilter.cpp
67 lines (49 loc) · 2.09 KB
/
DataFilter.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
//
// Created by n on 2022-01-14.
//
#include "DataFilter.h"
std::vector<std::string> DataFilter::filter_by_delimiter() const {
return filter_by_delimiter(input_file_name_, delim_after_);
}
std::vector<std::string>
DataFilter::filter_by_delimiter(const std::string &input_file_name, const std::string &delim_after) const {
std::ifstream infile(input_file_name);
std::vector<std::string> names{};
if (!infile.is_open()) {
std::cout << "Failed to open file." << std::endl;
} else {
std::cout << "Successfully opened file " << input_file_name << std::endl;
for (std::string line{}, delim{}; std::getline(infile, line), std::getline(infile, delim);) {
if (delim_after == delim) {
names.push_back(line);
// std::cout << line << std::endl; //DEBUG
}
}
}
// std::cout << names.size() << std::endl; //DEBUG
return names;
}
long DataFilter::count_occurences(const std::string &input_file_name, const std::string &delim_after) const {
std::ifstream infile(input_file_name);
long count {};
if (!infile.is_open()) {
std::cout << "Failed to open file." << std::endl;
} else {
std::cout << "Successfully opened file " << input_file_name << std::endl;
for (std::string delim{}; std::getline(infile, delim);) {
if (delim_after == delim) {
++count;
// std::cout << line << std::endl; //DEBUG
}
}
}
// std::cout << names.size() << std::endl; //DEBUG
return count;
}
std::vector<std::string> DataFilter::filter_by_delimiter_verbose() const {
long expected_count = count_occurences(input_file_name_, delim_after_);
std::vector<std::string> names = filter_by_delimiter(input_file_name_, delim_after_);
std::cout << "Expected count by delimiter: `" << delim_after_ << "`: " << expected_count << std::endl;
std::cout << "Found relevant: " << names.size() << " lines. " << std::endl;
std::cout << "Count is equal: " << std::boolalpha << (expected_count == names.size()) << std::endl;
}