From 74bb0bc5d05a221d7115702c829d76022a887e88 Mon Sep 17 00:00:00 2001 From: David L Parkhurst Date: Thu, 22 Oct 2009 19:26:38 +0000 Subject: [PATCH] For dump and delete, added StorageBinList git-svn-id: svn://136.177.114.72/svn_GW/phreeqcpp/trunk@3718 1feff8c3-07ed-0310-ac33-dd36852eb9cd --- StorageBinList.cpp | 243 +++++++++++++++++++++++++++++++++++++++++++++ StorageBinList.h | 55 ++++++++++ 2 files changed, 298 insertions(+) create mode 100644 StorageBinList.cpp create mode 100644 StorageBinList.h diff --git a/StorageBinList.cpp b/StorageBinList.cpp new file mode 100644 index 00000000..617d7197 --- /dev/null +++ b/StorageBinList.cpp @@ -0,0 +1,243 @@ +#include "StorageBinList.h" +#include // std::replace +StorageBinListItem::StorageBinListItem(void) +{ + this->defined = false; +}; + +StorageBinListItem::~StorageBinListItem(void) +{ +}; +void StorageBinListItem::Augment(std::string token) +{ + this->defined = true; + if (token.size() == 0) return; + + // split string accounting for possible negative numbers + int pos; + if ((pos = token.find("--")) != std::string::npos) + { + token.replace(pos,2," &"); + } + std::replace(token.begin() + 1, token.end(), '-', ' '); + std::replace(token.begin() + 1, token.end(), '&', '-'); + + // parse string into 1 or 2 numbers + std::istringstream iss(token); + std::set < int > temp_set; + int i; + if (iss >> i) + { + // add first + temp_set.insert(i); + if (iss >> i) + { + // add second if defined + temp_set.insert(i); + } + } + + // add single number or range to StorageBinListItem + if (temp_set.size() == 1) + { + this->numbers.insert(*(temp_set.begin())); + } + else if (temp_set.size() == 2) + { + int i1, i2; + std::set ::iterator it; + it = temp_set.begin(); + i1 = *it; + it++; + i2 = *it; + for (i = i1; i <= i2; i++) + { + this->numbers.insert(i); + } + } +}; +StorageBinList::StorageBinList(void) +{ +} +StorageBinList::StorageBinList(CParser & parser) +{ + this->Read(parser); +} +StorageBinList::~StorageBinList(void) +{ +} + +void StorageBinList::SetAll(bool tf) +{ + this->solution.Clear(); + this->pp_assemblage.Clear(); + this->exchange.Clear(); + this->surface.Clear(); + this->s_s_assemblage.Clear(); + this->gas_phase.Clear(); + this->kinetics.Clear(); + this->mix.Clear(); + this->reaction.Clear(); + this->temperature.Clear(); + + this->solution.Set_defined(tf); + this->pp_assemblage.Set_defined(tf); + this->exchange.Set_defined(tf); + this->surface.Set_defined(tf); + this->s_s_assemblage.Set_defined(tf); + this->gas_phase.Set_defined(tf); + this->kinetics.Set_defined(tf); + this->mix.Set_defined(tf); + this->reaction.Set_defined(tf); + this->temperature.Set_defined(tf); +} + +bool StorageBinList::Read(CParser & parser) +{ + bool return_value(true); + static std::vector < std::string > vopts; + if (vopts.empty()) + { + vopts.reserve(15); + vopts.push_back("solution"); + vopts.push_back("pp_assemblage"); + vopts.push_back("equilibrium_phases"); + vopts.push_back("exchange"); + vopts.push_back("surface"); + vopts.push_back("s_s_assemblage"); + vopts.push_back("solid_solution"); + vopts.push_back("solid_solutions"); + vopts.push_back("gas_phase"); + vopts.push_back("kinetics"); + vopts.push_back("mix"); + vopts.push_back("reaction"); + vopts.push_back("temperature"); + vopts.push_back("all"); + } + + std::istream::pos_type next_char; + std::string token; + int opt_save; + + bool useLastLine(false); + opt_save = CParser::OPT_DEFAULT; + + for (;;) + { + int opt; + opt = parser.get_option(vopts, next_char); + if (opt == CParser::OPT_DEFAULT) + { + opt = opt_save; + } + else + { + opt_save = opt; + } + + // Select StorageBinListItem + StorageBinListItem *item; + switch (opt) + { + case 0: + item = &(this->Get_solution()); + break; + case 1: + case 2: + item = &(this->Get_pp_assemblage()); + break; + case 3: + item = &(this->Get_exchange()); + break; + case 4: + item = &(this->Get_surface()); + break; + case 5: + case 6: + case 7: + item = &(this->Get_s_s_assemblage()); + break; + case 8: + item = &(this->Get_gas_phase()); + break; + case 9: + item = &(this->Get_kinetics()); + break; + case 10: + item = &(this->Get_mix()); + break; + case 11: + item = &(this->Get_reaction()); + break; + case 12: + item = &(this->Get_temperature()); + break; + default: + break; + } + + // Read dump entity list of numbers or number ranges for line, store in item + if (opt >= 0 && opt <= 12) + { + for (;;) + { + CParser::TOKEN_TYPE j = parser.copy_token(token, next_char); + if (j == CParser::TT_DIGIT) + { + item->Augment(token); + } + else if (j == CParser::TT_EMPTY) + { + item->Augment(token); + break; + } + else + { + parser.error_msg("Expected single number or range of numbers.", + CParser::OT_CONTINUE); + break; + } + } + } + + // Process other identifiers + switch (opt) + { + case CParser::OPT_EOF: + break; + case CParser::OPT_KEYWORD: + break; + + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + case 6: + case 7: + case 8: + case 9: + case 10: + case 11: + case 12: + break; + case 13: //all + this->SetAll(true); + break; + default: + case CParser::OPT_DEFAULT: + case CParser::OPT_ERROR: + opt = CParser::OPT_EOF; + parser.error_msg("Unknown input reading DELETE definition.", + CParser::OT_CONTINUE); + parser.error_msg(parser.line().c_str(), CParser::OT_CONTINUE); + useLastLine = false; + return_value = false; + break; + } + if (opt == CParser::OPT_EOF || opt == CParser::OPT_KEYWORD) + break; + } + return(return_value); +} \ No newline at end of file diff --git a/StorageBinList.h b/StorageBinList.h new file mode 100644 index 00000000..66fc52e7 --- /dev/null +++ b/StorageBinList.h @@ -0,0 +1,55 @@ +#if !defined(STORAGEBINLIST_H_INCLUDED) +#define STORAGEBINLIST_H_INCLUDED +#include // std::set +#include // std::string +#include // std::list +#include // std::vector +#include "Parser.h" +class StorageBinListItem +{ +public: + StorageBinListItem(void); + ~StorageBinListItem(void); + void Set_defined(bool tf) { this->defined = tf; }; + bool Get_defined(void) { return(this->defined); }; + void Augment(std::string token); + std::set < int > &Get_numbers(void) { return(this->numbers); }; + void Clear(void) { this->numbers.clear(); }; +protected: + std::set < int > numbers; + bool defined; +}; +class StorageBinList +{ +public: + StorageBinList(void); + StorageBinList(CParser & parser); + ~StorageBinList(void); + bool Read(CParser & parser); + void SetAll(bool tf); + + StorageBinListItem & Get_solution(void) { return(this->solution); }; + StorageBinListItem & Get_pp_assemblage(void) { return(this->pp_assemblage); }; + StorageBinListItem & Get_exchange(void) { return(this->exchange); }; + StorageBinListItem & Get_surface(void) { return(this->surface); }; + StorageBinListItem & Get_s_s_assemblage(void) { return(this->s_s_assemblage); }; + StorageBinListItem & Get_gas_phase(void) { return(this->gas_phase); }; + StorageBinListItem & Get_kinetics(void) { return(this->kinetics); }; + StorageBinListItem & Get_mix(void) { return(this->mix); }; + StorageBinListItem & Get_reaction(void) { return(this->reaction); }; + StorageBinListItem & Get_temperature(void) { return(this->temperature); }; +protected: + StorageBinListItem solution; + StorageBinListItem pp_assemblage; + StorageBinListItem exchange; + StorageBinListItem surface; + StorageBinListItem s_s_assemblage; + StorageBinListItem gas_phase; + StorageBinListItem kinetics; + StorageBinListItem mix; + StorageBinListItem reaction; + StorageBinListItem temperature; +}; + + +#endif // !defined(STORAGEBINLIST_H_INCLUDED) \ No newline at end of file