iphreeqc/dumper.cpp
Scott R Charlton 7c3f0b5baf changed implementation of trim_left trim_right and trim.
see http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring

Note: some previous uses of trim were erroneous (see tokiso_unit) since it had to be used like this:
 s = trim(s);
can now be used like
 trim(s)


git-svn-id: svn://136.177.114.72/svn_GW/phreeqc3/trunk@6330 1feff8c3-07ed-0310-ac33-dd36852eb9cd
2012-03-30 23:44:01 +00:00

274 lines
5.5 KiB
C++

#include <algorithm> // std::replace
#include "dumper.h"
#include "Parser.h"
#include "PHRQ_io.h"
dumper::dumper(PHRQ_io *io)
:
PHRQ_base(io)
{
this->file_name = "dump.out";
this->append = false;
this->on = false;
}
dumper::dumper(CParser & parser, PHRQ_io *io)
:
PHRQ_base(io)
{
this->file_name = "dump.out";
this->append = false;
this->Read(parser);
}
dumper::~dumper(void)
{
}
void dumper::SetAll(bool tf)
{
this->binList.SetAll(tf);
}
bool dumper::Read(CParser & parser)
{
bool return_value(true);
static std::vector < std::string > vopts;
if (vopts.empty())
{
vopts.reserve(20);
vopts.push_back("file"); // 0
vopts.push_back("append"); // 1
vopts.push_back("all"); // 2
vopts.push_back("cell"); // 3
vopts.push_back("cells"); // 4
vopts.push_back("solution"); // 5
vopts.push_back("solutions"); // 6
vopts.push_back("pp_assemblage");
vopts.push_back("pp_assemblages");
vopts.push_back("equilibrium_phase");
vopts.push_back("equilibrium_phases"); // 10
vopts.push_back("exchange");
vopts.push_back("surface");
vopts.push_back("ss_assemblage");
vopts.push_back("solid_solution");
vopts.push_back("solid_solutions"); // 15
vopts.push_back("gas_phase");
vopts.push_back("gas_phases");
vopts.push_back("kinetics"); // 18
vopts.push_back("mix"); // 19
vopts.push_back("reaction"); // 20
vopts.push_back("reactions"); // 21
vopts.push_back("temperature"); // 22
vopts.push_back("reaction_temperature"); // 23
vopts.push_back("reaction_temperatures"); // 24
vopts.push_back("pressure"); // 25
vopts.push_back("reaction_pressure"); // 26
vopts.push_back("reaction_pressures"); // 27
}
std::istream::pos_type ptr;
std::istream::pos_type next_char;
std::string token;
int opt_save;
bool useLastLine(false);
opt_save = CParser::OPT_DEFAULT;
bool cleared_once = false;
this->on = true;
for (;;)
{
int opt;
StorageBinListItem cells;
opt = parser.get_option(vopts, next_char);
if (opt == CParser::OPT_DEFAULT)
{
opt = opt_save;
}
else
{
opt_save = opt;
}
if (opt > 1 && !cleared_once)
{
binList.SetAll(false);
cleared_once = true;
}
// Select StorageBinListItem
StorageBinListItem *item = NULL;
switch (opt)
{
case 3: // cell
case 4: // cells
item = &cells;
break;
case 5:
case 6:
item = &(this->binList.Get_solution());
break;
case 7:
case 8:
case 9:
case 10:
item = &(this->binList.Get_pp_assemblage());
break;
case 11:
item = &(this->binList.Get_exchange());
break;
case 12:
item = &(this->binList.Get_surface());
break;
case 13:
case 14:
case 15:
item = &(this->binList.Get_ss_assemblage());
break;
case 16:
case 17:
item = &(this->binList.Get_gas_phase());
break;
case 18:
item = &(this->binList.Get_kinetics());
break;
case 19: // mix
item = &(this->binList.Get_mix());
break;
case 20: // reaction
case 21: // reactions
item = &(this->binList.Get_reaction());
break;
case 22: // temperature
case 23: // reaction_temperature
case 24: // reaction_temperatures
item = &(this->binList.Get_temperature());
break;
case 25: // pressure
case 26: // reaction_pressure
case 27: // reaction_pressures
item = &(this->binList.Get_pressure());
break;
default:
break;
}
// Read dump entity list of numbers or number ranges for line, store in item
if ((opt > 2))
{
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.",
PHRQ_io::OT_CONTINUE);
}
}
}
if (opt == 3 || opt == 4)
{
this->binList.TransferAll(cells);
}
// Process other identifiers
std::set < int >::iterator it;
switch (opt)
{
case CParser::OPT_EOF:
break;
case CParser::OPT_KEYWORD:
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
case 13:
case 14:
case 15:
case 16:
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
case 23:
case 24:
case 25:
case 26:
case 27:
break;
case 0: //file
std::getline(parser.get_iss(), this->file_name);
trim(this->file_name);
if (this->file_name.size() == 0)
{
this->file_name = "dump.out";
}
break;
case 1: //append
{
parser.copy_token(token, next_char);
//if (!(parser.get_iss() >> this->append))
this->append = true;
if (token.c_str()[0] == 'f' || token.c_str()[0] == 'F')
{
this->append = false;
}
}
break;
case 2: //all
this->SetAll(true);
break;
default:
case CParser::OPT_DEFAULT:
case CParser::OPT_ERROR:
opt = CParser::OPT_EOF;
parser.error_msg("Unknown input reading DUMP definition.",
PHRQ_io::OT_CONTINUE);
parser.error_msg(parser.line().c_str(), PHRQ_io::OT_CONTINUE);
useLastLine = false;
return_value = false;
break;
}
if (opt == CParser::OPT_EOF || opt == CParser::OPT_KEYWORD)
break;
}
return(return_value);
}
bool dumper::Get_bool_any(void)
{
return (
Get_bool_solution() ||
Get_bool_pp_assemblage() ||
Get_bool_exchange() ||
Get_bool_surface() ||
Get_bool_ss_assemblage() ||
Get_bool_gas_phase() ||
Get_bool_kinetics() ||
Get_bool_mix() ||
Get_bool_reaction() ||
Get_bool_temperature() ||
Get_bool_pressure()
);
}