class_main

This commit is contained in:
David Parkhurst 2021-03-26 21:21:20 -06:00
parent c748922b5e
commit 51fec19379
7 changed files with 75 additions and 412 deletions

View File

@ -863,8 +863,6 @@ void Phreeqc::init(void)
phreeqc_mpi_myself = 0;
first_read_input = TRUE;
user_database = NULL;
//have_punch_name = FALSE;
print_density = 0;
print_viscosity = 0;
cell_pore_volume = 0;
@ -2016,7 +2014,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc)
phreeqc_mpi_myself = 0;
first_read_input = TRUE;
user_database = string_duplicate(pSrc->user_database);
user_database = pSrc->user_database;
//have_punch_name = pSrc->have_punch_name;
print_density = pSrc->print_density;
print_viscosity = pSrc->print_viscosity;

View File

@ -408,8 +408,8 @@ public:
int store_get_equi_reactants(int k, int kin_end);
// mainsubs.cpp -------------------------------
std::ifstream* open_input_stream(char* query, char* default_name, std::ios_base::openmode mode, bool batch);
std::ofstream* open_output_stream(char* query, char* default_name, std::ios_base::openmode mode, bool batch);
std::ifstream* open_input_stream(std::string query, std::string& default_name, std::ios_base::openmode mode, bool batch);
std::ofstream* open_output_stream(std::string query, std::string& default_name, std::ios_base::openmode mode, bool batch);
int copy_entities(void);
void do_mixes(void);
void initialize(void);
@ -1049,11 +1049,8 @@ public:
LDBLE calc_rho_0(LDBLE tc, LDBLE pa);
LDBLE calc_dielectrics(LDBLE tc, LDBLE pa);
int compute_gfw(const char* string, LDBLE* gfw);
#if defined PHREEQ98
int copy_title(char* token_ptr, char** ptr, int* length);
#endif
int copy_token(char* token_ptr, const char** ptr, int* length);
int copy_token(std::string& token, const char** ptr);
static int copy_token(char* token_ptr, const char** ptr, int* length);
static int copy_token(std::string& token, const char** ptr);
int dup_print(const char* cptr, int emphasis);
int equal(LDBLE a, LDBLE b, LDBLE eps);
public:
@ -1630,7 +1627,7 @@ protected:
std::map<std::string, struct isotope_alpha*> isotope_alpha_map;
int phreeqc_mpi_myself;
int first_read_input;
char* user_database;
std::string user_database;
//int have_punch_name;
/* VP: Density Start */
@ -1748,7 +1745,7 @@ protected:
int forward_output_to_log;
/* phreeqc_files.cpp ------------------------------- */
char* default_data_base;
std::string default_data_base;
/* Pitzer */
int pitzer_model, sit_model, pitzer_pe;
int full_pitzer, always_full_pitzer, ICON, IC;

View File

@ -2821,10 +2821,8 @@ kinetics_formula(std::string kin_name, cxxNameDouble &stoichiometry)
// add formula
std::string name = it->first;
LDBLE coef = it->second;
char * temp_name = string_duplicate(name.c_str());
const char* cptr = temp_name;
const char* cptr = &name[0];
get_elts_in_species(&cptr, coef);
free_check_null(temp_name);
}
}
formula.append(kin_name);

View File

@ -320,17 +320,15 @@ write_banner(void)
return 0;
}
#ifdef ERROR_OSTREAM
/* ---------------------------------------------------------------------- */
int Phreeqc::
process_file_names(int argc, char *argv[], std::istream **db_cookie,
std::istream **input_cookie, int log)
/* ---------------------------------------------------------------------- */
{
int l;
char token[2 * MAX_LENGTH], default_name[2 * MAX_LENGTH];
char query[2 * MAX_LENGTH];
char in_file[2 * MAX_LENGTH], out_file[2 * MAX_LENGTH], db_file[2 * MAX_LENGTH];
std::string token, default_name;
std::string query;
std::string in_file, out_file, db_file;
char *env_ptr;
const char* cptr;
/*
@ -365,28 +363,28 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie,
/*
* Open user-input file
*/
strcpy(query, "Name of input file?");
query = "Name of input file?";
std::ifstream * local_input_stream = NULL;
if (argc <= 1)
{
default_name[0] = '\0';
default_name.clear();
local_input_stream = open_input_stream(query, default_name, std::ios_base::in, false);
}
else
{
strcpy(default_name, argv[1]);
default_name = argv[1];
local_input_stream = open_input_stream(query, default_name, std::ios_base::in, true);
}
screen_msg(sformatf("Input file: %s\n\n", default_name));
strcpy(in_file, default_name);
screen_msg(sformatf("Input file: %s\n\n", default_name.c_str()));
in_file = default_name;
/*
* Open file for output
*/
strcpy(query, "Name of output file?");
cptr = default_name;
copy_token(token, &cptr, &l);
strcpy(token, default_name);
strcat(token, ".out");
query = "Name of output file?";
cptr = default_name.c_str();
copy_token(token, &cptr);
token = default_name;
token.append(".out");
std::ofstream * local_output_stream = NULL;
if (argc <= 1)
{
@ -398,11 +396,11 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie,
}
else if (argc >= 3)
{
strcpy(token, argv[2]);
token = argv[2];
local_output_stream = open_output_stream(query, token, std::ios_base::out, true);
}
screen_msg(sformatf("Output file: %s\n\n", token));
strcpy(out_file, token);
screen_msg(sformatf("Output file: %s\n\n", token.c_str()));
out_file = token;
phrq_io->Set_output_ostream(local_output_stream);
/*
* Open log file
@ -423,15 +421,14 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie,
if (get_line() == KEYWORD)
{
cptr = line;
copy_token(token, &cptr, &l);
if (strcmp_nocase(token, "database") == 0)
copy_token(token, &cptr);
if (strcmp_nocase(token.c_str(), "database") == 0)
{
user_database = (char *) free_check_null(user_database);
user_database = string_duplicate(cptr);
if (string_trim(user_database) == EMPTY)
user_database = cptr;
string_trim(user_database);
if (user_database.size() == 0)
{
warning_msg("DATABASE file name is missing; default database will be used.");
user_database = (char *) free_check_null(user_database);
}
}
}
@ -440,26 +437,26 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie,
else
{
delete local_input_stream;
error_string = sformatf( "Error opening file, %s.", in_file);
error_string = sformatf( "Error opening file, %s.", in_file.c_str());
error_msg(error_string, STOP);
}
/*
* Open data base
*/
strcpy(query, "Name of database file?");
query = "Name of database file?";
env_ptr = getenv("PHREEQC_DATABASE");
if (user_database != NULL)
if (user_database.size() > 0)
{
strcpy(token, user_database);
token = user_database;
}
else if (env_ptr != NULL)
{
strcpy(token, env_ptr);
token = env_ptr;
}
else
{
strcpy(token, default_data_base);
token = default_data_base;
}
std::ifstream * local_database_file = NULL;
@ -473,9 +470,9 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie,
}
else if (argc >= 4)
{
if (user_database == NULL)
if (user_database.size() == 0)
{
strcpy(token, argv[3]);
token = argv[3];
}
else
{
@ -487,24 +484,21 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie,
}
local_database_file->close();
delete local_database_file;
user_database = (char *) free_check_null(user_database);
user_database = string_duplicate(token);
screen_msg(sformatf("Database file: %s\n\n", token));
strcpy(db_file, token);
output_msg(sformatf(" Input file: %s\n", in_file));
output_msg(sformatf(" Output file: %s\n", out_file));
user_database = token;
screen_msg(sformatf("Database file: %s\n\n", token.c_str()));
db_file = token;
output_msg(sformatf(" Input file: %s\n", in_file.c_str()));
output_msg(sformatf(" Output file: %s\n", out_file.c_str()));
#ifdef NPP
output_msg(sformatf("Using PHREEQC: version 3.6.5, compiled February 24, 2021\n"));
#endif
output_msg(sformatf("Database file: %s\n\n", token));
output_msg(sformatf("Database file: %s\n\n", token.c_str()));
#ifdef NPP
output_flush();
#endif
/*
* local cleanup
*/
user_database = (char *) free_check_null(user_database);
line = (char *) free_check_null(line);
line_save = (char *) free_check_null(line_save);
@ -517,239 +511,37 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie,
}
return 0;
}
#else
/* ---------------------------------------------------------------------- */
int Phreeqc::
process_file_names(int argc, char *argv[], std::istream **db_cookie,
std::istream **input_cookie, int log)
/* ---------------------------------------------------------------------- */
{
int l;
char token[2 * MAX_LENGTH], default_name[2 * MAX_LENGTH];
char query[2 * MAX_LENGTH];
char in_file[2 * MAX_LENGTH], out_file[2 * MAX_LENGTH], db_file[2 * MAX_LENGTH];
char *env_ptr;
const char* cptr;
/*
* Prepare error handling
*/
try {
if (phrq_io == NULL)
{
std::cerr << "No PHRQ_io output handler defined in process_file_names" << "\n";
}
/*
* Prep for get_line
*/
max_line = MAX_LINE;
space((void **) ((void *) &line), INIT, &max_line, sizeof(char));
space((void **) ((void *) &line_save), INIT, &max_line, sizeof(char));
/*
* Open error ostream
*/
if (argc > 4)
{
if (!phrq_io->error_open(argv[4]))
{
error_string = sformatf( "Error opening file, %s.", argv[4]);
warning_msg(error_string);
}
}
else
{
phrq_io->error_open(NULL);
}
/*
* Open user-input file
*/
strcpy(query, "Name of input file?");
std::ifstream * local_input_stream = NULL;
if (argc <= 1)
{
default_name[0] = '\0';
local_input_stream = open_input_stream(query, default_name, std::ios_base::in, false);
}
else
{
strcpy(default_name, argv[1]);
local_input_stream = open_input_stream(query, default_name, std::ios_base::in, true);
}
screen_msg(sformatf("Input file: %s\n\n", default_name));
strcpy(in_file, default_name);
/*
* Open file for output
*/
strcpy(query, "Name of output file?");
cptr = default_name;
copy_token(token, &cptr, &l);
strcat(token, ".out");
std::ofstream * local_output_stream;
if (argc <= 1)
{
local_output_stream = open_output_stream(query, token, std::ios_base::out, false);
}
else if (argc == 2)
{
local_output_stream = open_output_stream(query, token, std::ios_base::out, true);
}
else if (argc >= 3)
{
strcpy(token, argv[2]);
local_output_stream = open_output_stream(query, token, std::ios_base::out, true);
}
screen_msg(sformatf("Output file: %s\n\n", token));
strcpy(out_file, token);
phrq_io->Set_output_ostream(local_output_stream);
/*
* Open log file
*/
if (log == TRUE)
{
if (!phrq_io->log_open("phreeqc.log"))
{
error_msg("Cannot open log file, phreeqc.log.", STOP);
}
}
/*
* Read input file for DATABASE keyword
*/
if (local_input_stream->is_open())
{
phrq_io->push_istream(local_input_stream);
if (get_line() == KEYWORD)
{
cptr = line;
copy_token(token, &cptr, &l);
if (strcmp_nocase(token, "database") == 0)
{
user_database = (char *) free_check_null(user_database);
user_database = string_duplicate(cptr);
if (string_trim(user_database) == EMPTY)
{
warning_msg("DATABASE file name is missing; default database will be used.");
user_database = (char *) free_check_null(user_database);
}
}
}
phrq_io->pop_istream();
}
else
{
delete local_input_stream;
error_string = sformatf( "Error opening file, %s.", in_file);
error_msg(error_string, STOP);
}
/*
* Open data base
*/
strcpy(query, "Name of database file?");
env_ptr = getenv("PHREEQC_DATABASE");
if (user_database != NULL)
{
strcpy(token, user_database);
}
else if (env_ptr != NULL)
{
strcpy(token, env_ptr);
}
else
{
strcpy(token, default_data_base);
}
std::ifstream * local_database_file;
if (argc <= 1)
{
local_database_file = open_input_stream(query, token, std::ios_base::in, false);
}
else if (argc < 4)
{
local_database_file = open_input_stream(query, token, std::ios_base::in, true);
}
else if (argc >= 4)
{
if (user_database == NULL)
{
strcpy(token, argv[3]);
}
else
{
#ifndef PHREEQCI_GUI
warning_msg ("Database file from DATABASE keyword is used; command line argument ignored.");
#endif
}
local_database_file = open_input_stream(query, token, std::ios_base::in, true);
}
local_database_file->close();
delete local_database_file;
screen_msg(sformatf("Database file: %s\n\n", token));
strcpy(db_file, token);
output_msg(sformatf(" Input file: %s\n", in_file));
output_msg(sformatf(" Output file: %s\n", out_file));
output_msg(sformatf("Database file: %s\n\n", token));
/*
* local cleanup
*/
user_database = (char *) free_check_null(user_database);
user_database = string_duplicate(token);
line = (char *) free_check_null(line);
line_save = (char *) free_check_null(line_save);
*db_cookie = new std::ifstream(db_file, std::ios_base::in);
*input_cookie = new std::ifstream(in_file, std::ios_base::in);
}
catch (const PhreeqcStop& e)
{
return get_input_errors();
}
return 0;
}
#endif
/* ---------------------------------------------------------------------- */
std::ifstream * Phreeqc::
open_input_stream(char *query, char *default_name, std::ios_base::openmode mode, bool batch)
open_input_stream(std::string query, std::string& default_name, std::ios_base::openmode mode, bool batch)
/* ---------------------------------------------------------------------- */
{
char name[MAX_LENGTH];
std::string name;
std::ifstream *new_stream;
int l;
#ifdef ERROR_OSTREAM
std::ostream * error_ostream_save = phrq_io->Get_error_ostream();
#else
FILE * error_file_save = phrq_io->Get_error_file();
#endif
for (;;)
{
/*
* Get file name
*/
strcpy(name, default_name);
name = default_name;
if (!batch )
{
#ifdef ERROR_OSTREAM
phrq_io->Set_error_ostream(&std::cerr);
#else
phrq_io->Set_error_file(stderr);
#endif
screen_msg(sformatf("%s\n", query));
if (default_name[0] != '\0')
screen_msg(sformatf("%s\n", query.c_str()));
if (default_name.size() > 0)
{
screen_msg(sformatf("Default: %s\n", default_name));
screen_msg(sformatf("Default: %s\n", default_name.c_str()));
}
char *s_ptr = fgets(name, MAX_LENGTH, stdin);
if (s_ptr == NULL)
std::getline(std::cin, name);
if (name.size() == 0)
{
std::cerr << "Failed defining name." << std::endl;
}
l = (int) strlen(name);
name[l - 1] = '\0';
if (name[0] == '\0')
if (name.size() == 0)
{
strcpy(name, default_name);
name = default_name;
}
}
/*
@ -758,15 +550,11 @@ open_input_stream(char *query, char *default_name, std::ios_base::openmode mode,
new_stream = new std::ifstream(name, mode);
if (new_stream == NULL || !new_stream->is_open())
{
#ifdef ERROR_OSTREAM
phrq_io->Set_error_ostream(&std::cerr);
#else
phrq_io->Set_error_file(stderr);
#endif
error_string = sformatf( "\nERROR: Cannot open file, %s.\n", name);
error_string = sformatf( "\nERROR: Cannot open file, %s.\n", name.c_str());
screen_msg(error_string);
#ifdef NPP
error_msg(sformatf( "\nERROR: Cannot open file, %s.\n Please check, and give the correct, full path + name.\n", name), STOP);
error_msg(sformatf( "\nERROR: Cannot open file, %s.\n Please check, and give the correct, full path + name.\n", name.c_str()), STOP);
break;
#endif
error_flush();
@ -775,62 +563,44 @@ open_input_stream(char *query, char *default_name, std::ios_base::openmode mode,
}
break;
}
strncpy(default_name, name, MAX_LENGTH);
default_name = name;
if (!batch )
{
//phrq_io->Set_error_ostream(error_file_save);
#ifdef ERROR_OSTREAM
phrq_io->Set_error_ostream(error_ostream_save);
#else
phrq_io->Set_error_file(error_file_save);
#endif
}
return (new_stream);
}
/* ---------------------------------------------------------------------- */
std::ofstream * Phreeqc::
open_output_stream(char *query, char *default_name, std::ios_base::openmode mode, bool batch)
open_output_stream(std::string query, std::string& default_name, std::ios_base::openmode mode, bool batch)
/* ---------------------------------------------------------------------- */
{
char name[MAX_LENGTH];
std::string name;
std::ofstream *new_stream;
int l;
#ifdef ERROR_OSTREAM
std::ostream * error_ostream_save = phrq_io->Get_error_ostream();
#else
FILE * error_file_save = phrq_io->Get_error_file();
#endif
for (;;)
{
/*
* Get file name
*/
strcpy(name, default_name);
name = default_name;
if (!batch )
{
#ifdef ERROR_OSTREAM
phrq_io->Set_error_ostream(&std::cerr);
#else
phrq_io->Set_error_file(stderr);
#endif
screen_msg(sformatf("%s\n", query));
screen_msg(sformatf("%s\n", query.c_str()));
if (default_name[0] != '\0')
{
screen_msg(sformatf("Default: %s\n", default_name));
screen_msg(sformatf("Default: %s\n", default_name.c_str()));
}
char *s_ptr = fgets(name, MAX_LENGTH, stdin);
if (s_ptr == NULL)
std::getline(std::cin, name);
if (name.size() == 0)
{
std::cerr << "Failed defining name." << std::endl;
}
l = (int) strlen(name);
name[l - 1] = '\0';
if (name[0] == '\0')
if (name.size() == 0)
{
strcpy(name, default_name);
name = default_name;
}
}
/*
@ -839,12 +609,8 @@ open_output_stream(char *query, char *default_name, std::ios_base::openmode mode
new_stream = new std::ofstream(name, mode);
if (new_stream == NULL || !new_stream->is_open())
{
#ifdef ERROR_OSTREAM
phrq_io->Set_error_ostream(&std::cerr);
#else
phrq_io->Set_error_file(stderr);
#endif
error_string = sformatf( "\nERROR: Cannot open file, %s.\n", name);
error_string = sformatf( "\nERROR: Cannot open file, %s.\n", name.c_str());
screen_msg(error_string);
error_flush();
batch = FALSE;
@ -852,92 +618,10 @@ open_output_stream(char *query, char *default_name, std::ios_base::openmode mode
}
break;
}
strncpy(default_name, name, MAX_LENGTH);
default_name = name;
if (!batch )
{
#ifdef ERROR_OSTREAM
phrq_io->Set_error_ostream(error_ostream_save);
#else
phrq_io->Set_error_file(error_file_save);
#endif
}
return (new_stream);
}
#ifdef SKIP
/* ---------------------------------------------------------------------- */
std::ofstream * Phreeqc::
open_output_file(char *query, char *default_name, std::ios_base::openmode mode, bool batch)
/* ---------------------------------------------------------------------- */
{
char name[MAX_LENGTH];
std::ofstream *new_stream;
int l;
#ifdef ERROR_OSTREAM
std::ostream * error_ostream_save = phrq_io->Get_error_ostream();
#else
FILE * error_file_save = phrq_io->Get_error_file();
#endif
for (;;)
{
/*
* Get file name
*/
strcpy(name, default_name);
if (!batch )
{
#ifdef ERROR_OSTREAM
phrq_io->Set_error_ostream(&std::cerr);
#else
phrq_io->Set_error_file(stderr);
#endif
screen_msg(sformatf("%s\n", query));
if (default_name[0] != '\0')
{
screen_msg(sformatf("Default: %s\n", default_name));
}
char *s_ptr = fgets(name, MAX_LENGTH, stdin);
if (s_ptr == NULL)
{
std::cerr << "Failed defining name." << std::endl;
}
l = (int) strlen(name);
name[l - 1] = '\0';
if (name[0] == '\0')
{
strcpy(name, default_name);
}
}
/*
* Open existing file to read
*/
new_stream = new std::ofstream(name, mode);
if (new_stream == NULL || !new_stream->is_open())
{
#ifdef ERROR_OSTREAM
phrq_io->Set_error_ostream(&std::cerr);
#else
phrq_io->Set_error_file(stderr);
#endif
error_string = sformatf( "\nERROR: Cannot open file, %s.\n", name);
screen_msg(error_string);
error_flush();
batch = FALSE;
continue;
}
break;
}
strncpy(default_name, name, MAX_LENGTH);
if (!batch )
{
#ifdef ERROR_OSTREAM
phrq_io->Set_error_ostream(error_ostream_save);
#else
phrq_io->Set_error_file(error_file_save);
#endif
}
return (new_stream);
}
#endif

View File

@ -4042,9 +4042,8 @@ void Phreeqc::
dump_netpath(struct inverse *inverse_ptr)
/* ---------------------------------------------------------------------- */
{
int j;
std::string string;
const char* cptr;
//const char* cptr;
if (inverse_ptr->netpath == NULL)
return;
@ -4076,21 +4075,16 @@ dump_netpath(struct inverse *inverse_ptr)
{
if (it->second.Get_n_user() < 0)
continue;
/* flags and description */
char * description = string_duplicate(it->second.Get_description().c_str());
cptr = description;
j = copy_token(string, &cptr);
if (j != EMPTY)
if (it->second.Get_description().size() > 0)
{
string = sformatf("%s", description);
string = it->second.Get_description();
}
else
{
string = sformatf("Solution %d", it->second.Get_n_user());
}
fprintf(netpath_file, "4020%s\n", string.c_str());
description = (char *) free_check_null(description);
//description = (char *) free_check_null(description);
/* lat/lon */
fprintf(netpath_file,
" # Lat/lon\n");
@ -4397,7 +4391,6 @@ dump_netpath_pat(struct inverse *inv_ptr)
cxxSolution *solution_ptr, *solution_ptr_orig;
struct master *master_ptr;
LDBLE d1, d2, d3;
const char* cptr;
LDBLE sum, sum1, sum_iso, d;
std::vector<double> array_save, l_delta_save;
int count_unknowns_save, max_row_count_save, max_column_count_save, temp,
@ -4516,10 +4509,8 @@ dump_netpath_pat(struct inverse *inv_ptr)
solution_ptr = Utilities::Rxn_find(Rxn_solution_map, -7);
/* Header */
char * description = string_duplicate(solution_ptr_orig->Get_description().c_str());
cptr = description;
std::string string;
if (copy_token(string, &cptr) != EMPTY)
if (solution_ptr_orig->Get_description().size() > 0)
{
fprintf(netpath_file, "%d. %s\n", count_inverse_models,
solution_ptr_orig->Get_description().c_str());
@ -4529,7 +4520,6 @@ dump_netpath_pat(struct inverse *inv_ptr)
fprintf(netpath_file, "%d. Solution %d\n", count_inverse_models,
solution_ptr_orig->Get_n_user());
}
description = (char *) free_check_null(description);
/* bookkeeping */
count_pat_solutions++;

View File

@ -257,14 +257,12 @@ read_input(void)
#if defined(SWIG_SHARED_OBJ)
warning_msg("DATABASE keyword is ignored by IPhreeqc.");
#else
user_database = (char *) free_check_null(user_database);
user_database = string_duplicate(cptr);
if (string_trim(user_database) == EMPTY)
user_database = cptr;
string_trim(user_database);
if (user_database.size() == 0)
{
error_msg("DATABASE file name is missing.", CONTINUE);
input_error++;
user_database = (char *) free_check_null(user_database);
}
first_read_input = FALSE;
#endif

View File

@ -221,7 +221,6 @@ clean_up(void)
line = (char *) free_check_null(line);
line_save = (char *) free_check_null(line_save);
/* free user database name if defined */
user_database = (char *) free_check_null(user_database);
dump_file_name = (char *) free_check_null(dump_file_name);
#ifdef PHREEQCI_GUI
free_spread();
@ -230,7 +229,6 @@ clean_up(void)
last_title_x.clear();
count_inverse = 0;
default_data_base = (char *) free_check_null(default_data_base);
sformatf_buffer = (char *) free_check_null(sformatf_buffer);
return (OK);
}