From b6a5a7a6a2b3d97cbff01ebb37ed91c343b401c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20L=C3=BCbke?= Date: Tue, 19 Mar 2024 13:11:42 +0000 Subject: [PATCH] Add file reading functionality and update script and database handling --- ext/iphreeqc | 2 +- src/Init/GridInit.cpp | 58 +++++++++++++++++++++++++++++++------------ 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/ext/iphreeqc b/ext/iphreeqc index b34c32737..d6e713074 160000 --- a/ext/iphreeqc +++ b/ext/iphreeqc @@ -1 +1 @@ -Subproject commit b34c327377e2e06fb1d79b546996b9b401e850bc +Subproject commit d6e7130746f9823e0bc5f711179e676e27ba3737 diff --git a/src/Init/GridInit.cpp b/src/Init/GridInit.cpp index dcd28db75..eead62881 100644 --- a/src/Init/GridInit.cpp +++ b/src/Init/GridInit.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -73,24 +74,49 @@ getModuleSizes(IPhreeqcPOET &phreeqc, const Rcpp::List &initial_grid) { return phreeqc.getModuleSizes(row_ids); } -void InitialList::initGrid(const Rcpp::List &grid_input) { - // parse input values - const std::string script = Rcpp::as( - grid_input[getGridMemberString(GridMembers::PQC_SCRIPT_FILE)]); - const std::string database = Rcpp::as( - grid_input[getGridMemberString(GridMembers::PQC_DB_FILE)]); +static std::string readFile(const std::string &path) { + std::string string_rpath(PATH_MAX, '\0'); - std::string script_rp(PATH_MAX, '\0'); - std::string database_rp(PATH_MAX, '\0'); - - if (realpath(script.c_str(), script_rp.data()) == nullptr) { - throw std::runtime_error("Failed to resolve the path of the Phreeqc input" + - script); + if (realpath(path.c_str(), string_rpath.data()) == nullptr) { + throw std::runtime_error("Failed to resolve the realpath to file " + path); } - if (realpath(database.c_str(), database_rp.data()) == nullptr) { - throw std::runtime_error("Failed to resolve the path of the database" + - database); + std::ifstream file(string_rpath); + + if (!file.is_open()) { + throw std::runtime_error("Failed to open file: " + path); + } + + std::stringstream buffer; + buffer << file.rdbuf(); + + return buffer.str(); +} + +void InitialList::initGrid(const Rcpp::List &grid_input) { + // parse input values + + std::string script; + std::string database; + + if (grid_input.containsElementNamed( + getGridMemberString(GridMembers::PQC_SCRIPT_FILE))) { + + script = readFile(Rcpp::as( + grid_input[getGridMemberString(GridMembers::PQC_SCRIPT_FILE)])); + } else { + script = Rcpp::as( + grid_input[getGridMemberString(GridMembers::PQC_SCRIPT_STRING)]); + } + + if (grid_input.containsElementNamed( + getGridMemberString(GridMembers::PQC_DB_FILE))) { + + database = readFile(Rcpp::as( + grid_input[getGridMemberString(GridMembers::PQC_DB_FILE)])); + } else { + database = Rcpp::as( + grid_input[getGridMemberString(GridMembers::PQC_DB_STRING)]); } Rcpp::NumericMatrix grid_def = @@ -122,7 +148,7 @@ void InitialList::initGrid(const Rcpp::List &grid_input) { throw std::runtime_error("Grid size must be positive."); } - IPhreeqcPOET phreeqc(database_rp, script_rp); + IPhreeqcPOET phreeqc(database, script); this->phreeqc_mat = pqcScriptToGrid(phreeqc, R); this->initial_grid = matToGrid(R, this->phreeqc_mat, grid_def);