-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSVMap.cpp
88 lines (69 loc) · 1.98 KB
/
CSVMap.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
#include "CSVMap.h"
CSVMap::CSVMap(const string &file_path, bool& header)
{
this->file_path_ = file_path;
this->header_ = header;
ConstructMap();
}
CSVMap::~CSVMap(){}
void CSVMap::ConstructMap()
{
ifstream csvFile;
csvFile.open(file_path_.c_str());
if(csvFile.is_open())
{
string line;
istringstream s(line);
if(this->header_)
{
string header;
//Record and skip header
if(getline(csvFile, line))
{
istringstream ss(line);
while(getline(ss, header, ','))
this->headers_.push_back(header);
}
}
while(getline(csvFile, line))
{
istringstream ss(line);
string column;
Date* key_date = NULL;
while(getline(ss, column, ','))
{
if(key_date == NULL)
{
int string_date[3];
string splitdate;
int count = 0;
istringstream ss2(column);
while(getline(ss2, splitdate, '/'))
{
string_date[count] = atoi(splitdate.c_str());
count++;
}
key_date = new Date(string_date[0], string_date[1], string_date[2]);
}
else
this->csvmap_[*key_date].push_back(column);
}
delete key_date;
}
}
else
cout << "File not found.\n";
csvFile.close();
}
void CSVMap::DisplayDataFromDate(int month, int day, int year)
{
Date temp_date (month, day, year);
cout << endl;
for(int i = 0; i < this->headers_.size(); i++)
cout << this->headers_[i] << ' ';
cout << endl;
cout << temp_date << ' ';
for(int i = 0; i < this->csvmap_[temp_date].size(); i++)
cout << this->csvmap_[temp_date][i] << ' ';
cout << endl;
}