Commit c5a7f11e authored by Deepak Rangarajan's avatar Deepak Rangarajan
Browse files

throw csg::exceptions for erros when opening the file

parent 8d9cb3db
Loading
Loading
Loading
Loading
Loading
+12 −20
Original line number Diff line number Diff line
@@ -3,8 +3,8 @@
#include <sstream>

#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<std::string> 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<csg::Tree> 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<CsgIF> 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<CsgIF>(csg_if);
}
@@ -85,11 +82,6 @@ std::unique_ptr<CsgIF> 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<CsgIF>(csg_if);
}