diff --git a/src/csg/csg.cpp b/src/csg/csg.cpp index 7de1dde17764f7320fee2353388e1f75c7c75bc3..72db8ad49fe8e42ebe60d96875e5fd94dc4cc67a 100644 --- a/src/csg/csg.cpp +++ b/src/csg/csg.cpp @@ -3,8 +3,8 @@ #include #include "csg.hpp" - #include "csg_types.hpp" +#include "csg_exception.hpp" namespace { @@ -14,16 +14,22 @@ bool has_ext(std::string filename, std::string extension) { return filename.rfind(extension) == (filename.size() - extension.size()); } -std::optional read_csg(std::string geom_file) { +std::string read_csg(std::string geom_file) { if (!has_ext(geom_file, csg_ext)) { - std::cout << "Filename: " << geom_file << " must has extension .csg" - << std::endl; - return std::nullopt; + std::string err_msg = "ERROR: Filename " + geom_file + " must have extension .csg"; + std::cout << err_msg << std::endl; + throw csg::Exception("read_csg", err_msg); } std::ifstream in_stream; in_stream.open(geom_file.c_str()); + if (!in_stream.good()) { + std::string err_msg = "ERROR: Cannot open file " + geom_file; + std::cout << err_msg << std::endl; + throw csg::Exception("read_csg", err_msg); + } + std::stringstream str_stream; str_stream << in_stream.rdbuf(); return str_stream.str(); @@ -62,21 +68,12 @@ double CsgIF::operator()(double xx, double yy, double zz) const noexcept { std::shared_ptr get_csgtree_from_filename(std::string geom_file) { auto csg_str = read_csg(geom_file); - if (!csg_str) { - std::cout << "Unable to read .csg file: " << geom_file << std::endl; - return nullptr; - } - return csg::parse_csg(csg_str.value()); + return csg::parse_csg(csg_str); } std::unique_ptr get_csgif_from_filename(std::string geom_file) { auto csg_obj = get_csgtree_from_filename(geom_file); - if (!csg_obj) { - std::cout << "Failed to parse .csg file: " << geom_file << std::endl; - return nullptr; - } - csg::CsgIF csg_if(csg_obj); return std::make_unique(csg_if); } @@ -85,11 +82,6 @@ std::unique_ptr get_csgif(const std::string &geom_file, bool is_internal_flow) { auto csg_obj = get_csgtree_from_filename(geom_file); - if (!csg_obj) { - std::cout << "Failed to parse .csg file: " << geom_file << std::endl; - return nullptr; - } - csg::CsgIF csg_if(csg_obj, is_internal_flow); return std::make_unique(csg_if); }