Skip to content

Commit b770a5c

Browse files
authored
Merge pull request #38 from SPauly/iss34
Closes #34 #37
2 parents 773b4e8 + 4fb93d1 commit b770a5c

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

GetIntoCPPAgain.exe

5.93 KB
Binary file not shown.

src/CSVParser.cpp

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,40 @@ namespace csv
8080
{
8181
_header_size = this->size();
8282
_header_ptr = m_data.data();
83+
m_header = _row;
8384
};
8485

86+
std::string_view Header::string(){
87+
return m_header;
88+
}
89+
8590
//end class Header
8691

8792
//class CSVParser
8893
CSVParser::CSVParser(const std::string *PATH_ptr, Header& _header_structure)
8994
{
95+
//initialize important references before exceptions
96+
m_CURRENT_FILE = *PATH_ptr;
97+
_ptr_header = &_header_structure;
9098
m_DATABASE.exceptions(std::fstream::failbit);
9199

92100
try
93101
{
94-
m_CURRENT_FILE = *PATH_ptr;
102+
95103
//open csv file
96104
m_DATABASE.open(*PATH_ptr, std::ios::in | std::ios::out | std::ios::binary);
97105

98106
//init header of file
99107
std::string *tmp_line = new std::string;
100108
tmp_line->clear();
101109
fm::_getline(m_DATABASE, *tmp_line);
102-
_ptr_header = new Header(*tmp_line);
110+
if(*tmp_line != _header_structure.string()){
111+
m_DATABASE.seekp(0, std::ios::beg);
112+
m_DATABASE << _header_structure.string() << "\r\n"; //temporary solution!!! Use insert in file function here instead
113+
m_DATABASE.flush();
114+
}
103115
tmp_line->clear();
116+
104117
//check m_DATABASE for consistency
105118

106119
//init m_content
@@ -118,17 +131,13 @@ namespace csv
118131
delete tmp_line;
119132
m_DATABASE.clear();
120133
m_DATABASE.flush();
134+
121135
_csvgood = true;
122136
}
123137
catch (const std::fstream::failure &e)
124138
{
125139
try{
126-
m_DATABASE.open(*PATH_ptr, std::ios::in | std::ios::out | std::ios::binary | std::ios::app);
127-
addRow(_header_structure);
128-
_ptr_header = &_header_structure;
129-
130-
m_DATABASE.clear();
131-
m_DATABASE.flush();
140+
m_create_database();
132141
_csvgood = true;
133142
}
134143
catch (const std::fstream::failure &e){
@@ -147,7 +156,6 @@ namespace csv
147156
_csvgood = false;
148157
m_DATABASE.close();
149158
m_content.clear();
150-
delete _ptr_header;
151159
}
152160

153161
const unsigned int CSVParser::size() {
@@ -210,6 +218,16 @@ namespace csv
210218
return false;
211219
}
212220

221+
std::fstream &CSVParser::m_create_database()
222+
{
223+
m_DATABASE.open(m_CURRENT_FILE, std::ios::in | std::ios::out | std::ios::binary | std::ios::app);
224+
addRow(*_ptr_header);
225+
226+
m_DATABASE.clear();
227+
m_DATABASE.flush();
228+
return m_DATABASE;
229+
}
230+
213231
#ifdef _DEBUG_CSV
214232
void CSVParser::print_csv()
215233
{

src/CSVParser.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,14 @@ namespace csv
4848

4949
class Header : public Row
5050
{
51+
private:
52+
std::string m_header;
5153
public:
5254
Header() = delete;
5355
Header(std::string_view);
5456
Header(std::string_view, Row*) = delete;
5557

58+
std::string_view string();
5659
public:
5760
_HEADER_TYPE _header_size;
5861
std::string *_header_ptr;
@@ -76,12 +79,13 @@ namespace csv
7679
public:
7780
Header *_ptr_header;
7881
bool _csvgood;
79-
private:
80-
//void m_check_consistency();
82+
8183
private:
8284
std::string m_CURRENT_FILE;
8385
std::fstream m_DATABASE;
8486
std::vector<Row> m_content;
87+
private:
88+
std::fstream& m_create_database();
8589
};
8690

8791
} // namespace csv

src/main.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ int main() {
1818
lib.run_library();
1919
//allow the user to do something with the application
2020
//user->log_activity();
21-
//user->logout();
2221
//user->get_activity();
2322
std::cin.get();
2423
delete user;

0 commit comments

Comments
 (0)