@@ -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 {
0 commit comments