-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsvTest.cpp
102 lines (86 loc) · 2.03 KB
/
CsvTest.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
91
92
93
94
95
96
97
98
99
100
101
102
void getCsvAttributes(std::string& inputStr, const std::string& separator,
std::map<std::string, unsigned int>& result,
const std::list<std::string>& filterList = {})
{
std::string::size_type sz{};
std::string word{};
unsigned int counter{ 0 };
bool useFilter{ false };
if (filterList.size() != 0)
{
useFilter = true;
}
result.clear();
while (inputStr.size() > 0)
{
word = "";
sz = inputStr.find_first_of(separator);
if (sz != 0)
{
word = inputStr.substr(0, sz);
inputStr.erase(0, sz + 1);
}
else
{
inputStr.erase(0, 1);
}
if (useFilter)
{
auto it = std::find(filterList.begin(), filterList.end(), word);
if (it != filterList.end())
{
result.insert({ word, counter });
}
}
else
{
result.insert({ word, counter });
}
counter++;
}
}
void getCsvValues(std::string& inputStr, const std::string& separator, std::map<unsigned int, std::string>& result)
{
std::string::size_type sz{};
std::string word{};
unsigned int counter{ 0 };
result.clear();
while (inputStr.size() > 0)
{
word = "";
sz = inputStr.find_first_of(separator);
if (sz != 0)
{
word = inputStr.substr(0, sz);
inputStr.erase(0, sz + 1);
}
else
{
inputStr.erase(0, 1);
}
result.insert({ counter, word });
counter++;
}
}
void testCsv()
{
std::list<std::map< unsigned int, std::string>> content{};
std::string line{};
std::map<std::string, unsigned int> attributes_list{};
std::map< unsigned int, std::string> row_list{};
std::list<std::string> filterList = { "MachineCore.Origin.Location", "MachineCore.Reject.NoLength", "Sorting.SortingProduct.FirstReject.Name" };
std::ifstream file("C:\\Users\\Strutu\\Desktop\\MPR_TOP2000 VALENCE_1645178161_CUSTOMER_SORT_22-02-18T09-56-01Z.csv", std::ios::in);
if (file.is_open())
{
std::getline(file, line);
getCsvAttributes(line, ";", attributes_list, filterList);
while (std::getline(file, line))
{
getCsvValues(line, ";", row_list);
content.push_back(row_list);
}
}
else
std::cout << "Could not open the file\n";
file.close();
}