Skip to content

Commit a0e6dab

Browse files
authored
Merge pull request #40 from SPauly/iss35
Closes #35
2 parents b770a5c + 1f00615 commit a0e6dab

File tree

5 files changed

+28
-11
lines changed

5 files changed

+28
-11
lines changed

GetIntoCPPAgain.exe

2.05 KB
Binary file not shown.

src/CSVParser.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,25 @@ namespace csv
6464
throw Error("Row: Item not found");
6565
}
6666

67+
Row& Row::set_headerptr( Row* _headerptr){
68+
mptr_header = _headerptr;
69+
return *mptr_header;
70+
}
71+
6772
std::string_view Row::operator[] (_HEADER_TYPE &_header) const {
6873
return this->getvalue(_header);
6974
};
7075

7176
std::string_view Row::operator[] (std::string_view _header) const {
72-
_HEADER_TYPE pos = mptr_header->get_item_position(_header);
73-
return m_data.at(pos);
77+
if (mptr_header)
78+
{
79+
_HEADER_TYPE pos = mptr_header->get_item_position(_header);
80+
return m_data.at(pos);
81+
}
82+
else
83+
{
84+
throw Error("Row: not linked to valid header");
85+
}
7486
};
7587
//end class Row
7688

@@ -90,18 +102,18 @@ namespace csv
90102
//end class Header
91103

92104
//class CSVParser
93-
CSVParser::CSVParser(const std::string *PATH_ptr, Header& _header_structure)
105+
CSVParser::CSVParser(const std::string &PATH_ref, Header& _header_structure)
94106
{
95107
//initialize important references before exceptions
96-
m_CURRENT_FILE = *PATH_ptr;
108+
m_CURRENT_FILE = PATH_ref;
97109
_ptr_header = &_header_structure;
98110
m_DATABASE.exceptions(std::fstream::failbit);
99111

100112
try
101113
{
102114

103115
//open csv file
104-
m_DATABASE.open(*PATH_ptr, std::ios::in | std::ios::out | std::ios::binary);
116+
m_DATABASE.open(PATH_ref, std::ios::in | std::ios::out | std::ios::binary);
105117

106118
//init header of file
107119
std::string *tmp_line = new std::string;
@@ -170,6 +182,10 @@ namespace csv
170182
}
171183

172184
bool CSVParser::addRow(Row& _row) {
185+
if(_ptr_header)
186+
_row.set_headerptr(_ptr_header);
187+
else
188+
throw Error("CSV: Cannot add row without linking parser to valid header element first");
173189
try
174190
{
175191
if (m_DATABASE.is_open())

src/CSVParser.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ namespace csv
3434
void add_value(std::string_view);
3535
std::string_view getvalue(_HEADER_TYPE &) const;
3636
_HEADER_TYPE& get_item_position(std::string_view);
37+
Row& set_headerptr(Row*);
3738

3839

3940
std::string_view operator[] (_HEADER_TYPE &) const;
@@ -42,7 +43,7 @@ namespace csv
4243
protected:
4344
std::vector<std::string> m_data;
4445
private:
45-
Row* mptr_header;
46+
Row* mptr_header = nullptr;
4647
_HEADER_TYPE m_item_pos = 0;
4748
};
4849

@@ -65,7 +66,7 @@ namespace csv
6566
{
6667
public:
6768
CSVParser() = delete;
68-
CSVParser(const std::string *, Header&);
69+
CSVParser(const std::string &, Header&);
6970
~CSVParser();
7071

7172
const unsigned int size();
@@ -77,8 +78,8 @@ namespace csv
7778
#endif
7879

7980
public:
80-
Header *_ptr_header;
81-
bool _csvgood;
81+
Header *_ptr_header = nullptr;
82+
bool _csvgood = false;
8283

8384
private:
8485
std::string m_CURRENT_FILE;

src/Library.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Library::Library(User* _ptr_user){
44
mptr_user = _ptr_user;
55
try{
6-
mptr_csv_parser = new csv::CSVParser(&m_inventory_path, m_inventory_structure);
6+
mptr_csv_parser = new csv::CSVParser(m_inventory_path, m_inventory_structure);
77
}
88
catch(csv::Error &e){
99
log(e.what());

src/User.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ User::User(){
172172
//longer allocation
173173
try
174174
{
175-
mptr_csv_parser = new csv::CSVParser(&m_path_userfile, m_userfile_header);
175+
mptr_csv_parser = new csv::CSVParser(m_path_userfile, m_userfile_header);
176176
}
177177
catch (csv::Error &e)
178178
{

0 commit comments

Comments
 (0)