From 12c9ec507c5145ed9475a855bfffb3bc35d778da Mon Sep 17 00:00:00 2001 From: conrad Date: Mon, 24 May 2021 16:26:53 +1000 Subject: [PATCH] better handling of bad_alloc situations --- include/armadillo_bits/diskio_meat.hpp | 96 +++++++++++++++----------- 1 file changed, 55 insertions(+), 41 deletions(-) diff --git a/include/armadillo_bits/diskio_meat.hpp b/include/armadillo_bits/diskio_meat.hpp index a923a361..8616cf86 100644 --- a/include/armadillo_bits/diskio_meat.hpp +++ b/include/armadillo_bits/diskio_meat.hpp @@ -2993,7 +2993,7 @@ diskio::load_csv_ascii(SpMat& x, std::istream& f, std::string& err_msg) // TODO: replace with more efficient implementation - bool load_okay = f.good(); + if(f.good() == false) { return false; } f.clear(); const std::fstream::pos_type pos1 = f.tellg(); @@ -3009,7 +3009,7 @@ diskio::load_csv_ascii(SpMat& x, std::istream& f, std::string& err_msg) std::string token; - while( f.good() && load_okay ) + while(f.good()) { std::getline(f, line_string); @@ -3031,41 +3031,51 @@ diskio::load_csv_ascii(SpMat& x, std::istream& f, std::string& err_msg) ++f_n_rows; } - f.clear(); - f.seekg(pos1); - - try { x.zeros(f_n_rows, f_n_cols); } catch(...) { err_msg = "not enough memory"; return false; } - - uword row = 0; - - while(f.good()) + try { - std::getline(f, line_string); + f.clear(); + f.seekg(pos1); - if(line_string.size() == 0) { break; } + MapMat tmp(f_n_rows, f_n_cols); - line_stream.clear(); - line_stream.str(line_string); + uword row = 0; - uword col = 0; - - while(line_stream.good()) + while(f.good()) { - std::getline(line_stream, token, ','); + std::getline(f, line_string); - eT val = eT(0); + if(line_string.size() == 0) { break; } - diskio::convert_token( val, token ); + line_stream.clear(); + line_stream.str(line_string); - if(val != eT(0)) { x(row,col) = val; } + uword col = 0; - ++col; + while(line_stream.good()) + { + std::getline(line_stream, token, ','); + + eT val = eT(0); + + diskio::convert_token( val, token ); + + if(val != eT(0)) { tmp(row,col) = val; } + + ++col; + } + + ++row; } - ++row; + x = tmp; + } + catch(...) + { + err_msg = "not enough memory"; + return false; } - return load_okay; + return true; } @@ -3118,7 +3128,7 @@ diskio::load_coord_ascii(SpMat& x, std::istream& f, std::string& err_msg) { arma_extra_debug_sigprint(); - bool load_okay = f.good(); + if(f.good() == false) { return false; } f.clear(); const std::fstream::pos_type pos1 = f.tellg(); @@ -3135,7 +3145,7 @@ diskio::load_coord_ascii(SpMat& x, std::istream& f, std::string& err_msg) std::string token; - while( f.good() && load_okay ) + while(f.good()) { std::getline(f, line_string); @@ -3151,7 +3161,7 @@ diskio::load_coord_ascii(SpMat& x, std::istream& f, std::string& err_msg) line_stream >> line_row; - if(line_stream.good() == false) { load_okay = false; break; } + if(line_stream.good() == false) { err_msg = "incorrect format"; return false; } line_stream >> line_col; @@ -3161,19 +3171,15 @@ diskio::load_coord_ascii(SpMat& x, std::istream& f, std::string& err_msg) if(f_n_cols < line_col) { f_n_cols = line_col; } } - // take into account that indices start at 0 if(size_found) { ++f_n_rows; ++f_n_cols; } - - if(load_okay) + try { f.clear(); f.seekg(pos1); - MapMat tmp; - - try { tmp.set_size(f_n_rows, f_n_cols); } catch(...) { err_msg = "not enough memory"; return false; } + MapMat tmp(f_n_rows, f_n_cols); while(f.good()) { @@ -3204,8 +3210,13 @@ diskio::load_coord_ascii(SpMat& x, std::istream& f, std::string& err_msg) x = tmp; } + catch(...) + { + err_msg = "not enough memory"; + return false; + } - return load_okay; + return true; } @@ -3217,7 +3228,7 @@ diskio::load_coord_ascii(SpMat< std::complex >& x, std::istream& f, std::stri { arma_extra_debug_sigprint(); - bool load_okay = f.good(); + if(f.good() == false) { return false; } f.clear(); const std::fstream::pos_type pos1 = f.tellg(); @@ -3235,7 +3246,7 @@ diskio::load_coord_ascii(SpMat< std::complex >& x, std::istream& f, std::stri std::string token_real; std::string token_imag; - while( f.good() && load_okay ) + while(f.good()) { std::getline(f, line_string); @@ -3251,7 +3262,7 @@ diskio::load_coord_ascii(SpMat< std::complex >& x, std::istream& f, std::stri line_stream >> line_row; - if(line_stream.good() == false) { load_okay = false; break; } + if(line_stream.good() == false) { err_msg = "incorrect format"; return false; } line_stream >> line_col; @@ -3264,15 +3275,13 @@ diskio::load_coord_ascii(SpMat< std::complex >& x, std::istream& f, std::stri // take into account that indices start at 0 if(size_found) { ++f_n_rows; ++f_n_cols; } - if(load_okay) + try { f.clear(); f.seekg(pos1); MapMat< std::complex > tmp(f_n_rows, f_n_cols); - try { tmp.set_size(f_n_rows, f_n_cols); } catch(...) { err_msg = "not enough memory"; return false; } - while(f.good()) { std::getline(f, line_string); @@ -3314,8 +3323,13 @@ diskio::load_coord_ascii(SpMat< std::complex >& x, std::istream& f, std::stri x = tmp; } + catch(...) + { + err_msg = "not enough memory"; + return false; + } - return load_okay; + return true; }