Squashed 'src/' changes from 6e248c34..81f180a0

81f180a0 Fixed bugs in inverse
6d575967 strcpy_safe and strcat_safe
6d98c4e1 trying different header files
72796f15 added <cstring>
e8481607 warnings, strcat, strcpy
988bdee0 Try using goto(s)
9b10ce3f Try updated logical expression
812061be Turn off optimizing on k_temp

git-subtree-dir: src
git-subtree-split: 81f180a069285bcb4d180c860664b4b2a193854d
This commit is contained in:
Darth Vader 2023-11-16 05:04:39 +00:00
parent 10bcb271d7
commit 1b2eb6ca0d
23 changed files with 210 additions and 278 deletions

2
Var.c
View File

@ -74,7 +74,7 @@ char* VarAllocString(const char* pSrc)
char* psz;
if (!pSrc) return NULL;
psz = (char*) malloc(strlen(pSrc) + 1);
strcpy(psz, pSrc);
if(psz != NULL) strcpy(psz, pSrc);
return psz;
}

View File

@ -406,7 +406,7 @@ cxxNameDouble::add(const char *token, LDBLE total)
//
{
char key[MAX_LENGTH];
strcpy(key, token);
Utilities::strcpy_safe(key, MAX_LENGTH, token);
cxxNameDouble::iterator current = (*this).find(key);
if (current != (*this).end())

View File

@ -550,7 +550,8 @@ numtostr(char * Result, LDBLE n)
l_s[i] = '\0';
* p2c: basic.p, line 248:
* Note: Modification of string length may translate incorrectly [146] *
return strcpy(Result, strltrim(l_s));
Utilities::strcpy_safe(Result, MAX_LENGTH, strltrim(l_s));
return Result;
} */
}
@ -1746,16 +1747,16 @@ void PBasic::
snerr(const char * l_s)
{
char str[MAX_LENGTH] = {0};
strcpy(str, "Syntax_error ");
Utilities::strcpy_safe(str, MAX_LENGTH, "Syntax_error ");
if (phreeqci_gui)
{
_ASSERTE(nIDErrPrompt == 0);
nIDErrPrompt = IDS_ERR_SYNTAX;
}
strcat(str, l_s);
strcat(str, " in line: ");
Utilities::strcat_safe(str, MAX_LENGTH, l_s);
Utilities::strcat_safe(str, MAX_LENGTH, " in line: ");
if (strcmp(inbuf, "run"))
strcat(str, inbuf);
Utilities::strcat_safe(str, MAX_LENGTH, inbuf);
errormsg(str);
}
@ -1763,16 +1764,16 @@ void PBasic::
tmerr(const char * l_s)
{
char str[MAX_LENGTH] = {0};
strcpy(str, "Type mismatch error");
Utilities::strcpy_safe(str, MAX_LENGTH, "Type mismatch error");
if (phreeqci_gui)
{
_ASSERTE(nIDErrPrompt == 0);
nIDErrPrompt = IDS_ERR_MISMATCH;
}
strcat(str, l_s);
strcat(str, " in line: ");
Utilities::strcat_safe(str, MAX_LENGTH, l_s);
Utilities::strcat_safe(str, MAX_LENGTH, " in line: ");
if (strcmp(inbuf, "run"))
strcat(str, inbuf);
Utilities::strcat_safe(str, MAX_LENGTH, inbuf);
errormsg(str);
}
@ -1901,8 +1902,9 @@ require(int k, struct LOC_exec *LINK)
if (item == command_tokens.end())
snerr(": missing unknown command");
else {
strcpy(str, ": missing ");
snerr(strcat(str, item->first.c_str()));
Utilities::strcpy_safe(str, MAX_LENGTH, ": missing ");
Utilities::strcat_safe(str, MAX_LENGTH, item->first.c_str());
snerr(str);
}
#if !defined(R_SO)
exit(4);
@ -2544,7 +2546,7 @@ factor(struct LOC_exec * LINK)
size_t l = elt_name.size();
l = l < 256 ? 256 : l + 1;
char* token = (char*)PhreeqcPtr->PHRQ_malloc(l * sizeof(char));
strcpy(token, elt_name.c_str());
Utilities::strcpy_safe(token, l, elt_name.c_str());
*elt_varrec->UU.U1.sval = token;
}
break;
@ -6240,9 +6242,9 @@ exec(void)
_ASSERTE(nIDErrPrompt == 0);
nIDErrPrompt = IDS_ERR_ILLEGAL;
}
strcat(STR1, "Illegal command in line: ");
Utilities::strcat_safe(STR1, MAX_LENGTH, "Illegal command in line: ");
if (strcmp(inbuf, "run"))
strcat(STR1, inbuf);
Utilities::strcat_safe(STR1, MAX_LENGTH, inbuf);
errormsg(STR1);
break;
}
@ -6392,7 +6394,7 @@ cmdplot_xy(struct LOC_exec *LINK)
n[i] = expr(LINK);
if (n[i].stringval)
{
strcpy(STR[i], n[i].UU.sval);
Utilities::strcpy_safe(STR[i], MAX_LENGTH, n[i].UU.sval);
PhreeqcPtr->PHRQ_free(n[i].UU.sval);
}
else

View File

@ -14,6 +14,7 @@
#include "PBasic.h"
#include "Temperature.h"
#include "SSassemblage.h"
#include "Utils.h"
#if defined(PHREEQCI_GUI)
#ifdef _DEBUG
@ -173,7 +174,7 @@ size_t Phreeqc::list_components(std::list<std::string> &list_c)
{
if (it->first == "Charge") continue;
char string[MAX_LENGTH];
strcpy(string, it->first.c_str());
Utilities::strcpy_safe(string, MAX_LENGTH, it->first.c_str());
class master *master_ptr = master_bsearch_primary(string);
if (master_ptr == NULL) continue;
if (master_ptr->type != AQ) continue;

View File

@ -1036,7 +1036,6 @@ public:
int get_token(const char** eqnaddr, std::string& string, LDBLE* z, int* l);
int islegit(const char c);
void malloc_error(void);
int parse_couple(char* token);
int print_centered(const char* string);
static int replace(const char* str1, const char* str2, char* str);
static void replace(std::string &stds, const char* str1, const char* str2);

View File

@ -14,7 +14,7 @@
#include "cxxMix.h"
#include "Reaction.h"
#include "Temperature.h"
#include "Utils.h"
#if defined(PHREEQCI_GUI)
#ifdef _DEBUG
#define new DEBUG_NEW
@ -66,11 +66,11 @@ cxxSystem::totalize(Phreeqc * phreeqc_ptr)
if (this->solution != NULL)
{
char token[MAX_LENGTH];
strcpy(token, "O");
Utilities::strcpy_safe(token, MAX_LENGTH, "O");
this->totals[token] = this->solution->Get_total_o();
strcpy(token, "H");
Utilities::strcpy_safe(token, MAX_LENGTH, "H");
this->totals[token] = this->solution->Get_total_h();
strcpy(token, "Charge");
Utilities::strcpy_safe(token, MAX_LENGTH, "Charge");
this->totals[token] = this->solution->Get_cb();
this->totals.add_extensive(this->solution->Get_totals(), 1.0);
}

View File

@ -705,7 +705,7 @@ calc_logk_n(const char* name)
{
l_logk[i] = 0.0;
}
strcpy(token, name);
Utilities::strcpy_safe(token, MAX_LENGTH, name);
logk_ptr = logk_search(token);
if (logk_ptr != NULL)
{
@ -730,7 +730,7 @@ calc_logk_p(const char* name)
LDBLE lk = -999.9;
LDBLE l_logk[MAX_LOG_K_INDICES];
strcpy(token, name);
Utilities::strcpy_safe(token, MAX_LENGTH, name);
phase_ptr = phase_bsearch(token, &j, FALSE);
if (phase_ptr != NULL)
@ -769,7 +769,7 @@ calc_logk_s(const char* name)
class species* s_ptr;
LDBLE lk, l_logk[MAX_LOG_K_INDICES];
strcpy(token, name);
Utilities::strcpy_safe(token, MAX_LENGTH, name);
s_ptr = s_search(token);
if (s_ptr != NULL)
{
@ -797,7 +797,7 @@ dh_a0(const char* name)
class species* s_ptr;
double a = -999.99;
strcpy(token, name);
Utilities::strcpy_safe(token, MAX_LENGTH, name);
s_ptr = s_search(token);
if (s_ptr != NULL)
{
@ -819,7 +819,7 @@ dh_bdot(const char* name)
}
else
{
strcpy(token, name);
Utilities::strcpy_safe(token, MAX_LENGTH, name);
s_ptr = s_search(token);
if (s_ptr != NULL)
{
@ -839,7 +839,7 @@ calc_deltah_p(const char* name)
LDBLE lkm, lkp;
LDBLE l_logk[MAX_LOG_K_INDICES];
double dh = -999.99;
strcpy(token, name);
Utilities::strcpy_safe(token, MAX_LENGTH, name);
phase_ptr = phase_bsearch(token, &j, FALSE);
if (phase_ptr != NULL)
@ -879,7 +879,7 @@ calc_deltah_s(const char* name)
class species* s_ptr;
LDBLE lkm, lkp, l_logk[MAX_LOG_K_INDICES];
double dh = -999.99;
strcpy(token, name);
Utilities::strcpy_safe(token, MAX_LENGTH, name);
s_ptr = s_search(token);
if (s_ptr != NULL)
{
@ -929,7 +929,7 @@ calc_surface_charge(const char* surface_name)
if (token_ptr->s->type != SURF)
continue;
master_ptr = trxn.token[i].s->primary;
strcpy(token, master_ptr->elt->name);
Utilities::strcpy_safe(token, MAX_LENGTH, master_ptr->elt->name);
replace("_", " ", token);
cptr = token;
copy_token(token1, &cptr, &j);
@ -1237,7 +1237,7 @@ calc_t_sc(const char* name)
char token[MAX_LENGTH];
class species* s_ptr;
strcpy(token, name);
Utilities::strcpy_safe(token, MAX_LENGTH, name);
s_ptr = s_search(token);
if (s_ptr != NULL && s_ptr->in)
{
@ -1262,7 +1262,7 @@ calc_f_visc(const char* name)
if (print_viscosity)
{
strcpy(token, name);
Utilities::strcpy_safe(token, MAX_LENGTH, name);
s_ptr = s_search(token);
if (s_ptr != NULL && s_ptr->in)
return s_ptr->dw_t_visc;
@ -2111,7 +2111,7 @@ match_elts_in_species(const char* name, const char* mytemplate)
char token1[MAX_LENGTH], template1[MAX_LENGTH], equal_list1[MAX_LENGTH];
char str[2];
strcpy(token, name);
Utilities::strcpy_safe(token, MAX_LENGTH, name);
squeeze_white(token);
replace("(+", "(", token);
if (strstr("token", "++") != NULL)
@ -2168,7 +2168,7 @@ match_elts_in_species(const char* name, const char* mytemplate)
/*
* Replace elements with first of equivalent elements
*/
strcpy(template1, mytemplate);
Utilities::strcpy_safe(template1, MAX_LENGTH, mytemplate);
squeeze_white(template1);
cptr = template1;
while (extract_bracket(&cptr, equal_list) == TRUE)
@ -2229,22 +2229,22 @@ match_elts_in_species(const char* name, const char* mytemplate)
token[0] = '\0';
for (i = 0; i < count_match_tokens; i++)
{
strcat(token, match_vector[i].first.c_str());
Utilities::strcat_safe(token, MAX_LENGTH, match_vector[i].first.c_str());
if (match_vector[i].second != 1.0)
{
snprintf(token1, sizeof(token1), "%g", (double)match_vector[i].second);
strcat(token, token1);
Utilities::strcat_safe(token, MAX_LENGTH, token1);
}
}
/*
* Write a template name using first of equivalent elements
*/
strcpy(template1, mytemplate);
Utilities::strcpy_safe(template1, MAX_LENGTH, mytemplate);
squeeze_white(template1);
cptr = template1;
while (extract_bracket(&cptr, equal_list) == TRUE)
{
strcpy(equal_list1, equal_list);
Utilities::strcpy_safe(equal_list1, MAX_LENGTH, equal_list);
replace("{", "", equal_list);
replace("}", "", equal_list);
while (replace(",", " ", equal_list) == TRUE);
@ -2410,10 +2410,6 @@ surf_total(const char* total_name, const char* surface_name)
{
if (s_x[j]->next_elt[i].elt->master->type != SURF) continue;
//strcpy(token, s_x[j]->next_elt[i].elt->name);
//replace("_", " ", token);
//cptr = token;
//copy_token(name, &cptr, &k);
token = s_x[j]->next_elt[i].elt->name;
replace("_", " ", token);
std::string::iterator b = token.begin();
@ -2512,7 +2508,7 @@ surf_total_no_redox(const char* total_name, const char* surface_name)
{
if (x[j]->type != SURFACE)
continue;
strcpy(token, x[j]->master[0]->elt->name);
Utilities::strcpy_safe(token, MAX_LENGTH, x[j]->master[0]->elt->name);
replace("_", " ", token);
cptr = token;
copy_token(name, &cptr, &k);
@ -2528,7 +2524,7 @@ surf_total_no_redox(const char* total_name, const char* surface_name)
}
if (j >= count_unknowns)
return (0);
strcpy(surface_name_local, name);
Utilities::strcpy_safe(surface_name_local, MAX_LENGTH, name);
/*
* find total moles of each element in diffuse layer...
*/
@ -2542,7 +2538,7 @@ surf_total_no_redox(const char* total_name, const char* surface_name)
{
if (s_x[j]->next_elt[i].elt->master->type != SURF) continue;
strcpy(token, s_x[j]->next_elt[i].elt->name);
Utilities::strcpy_safe(token, MAX_LENGTH, s_x[j]->next_elt[i].elt->name);
replace("_", " ", token);
cptr = token;
copy_token(name, &cptr, &k);
@ -2792,7 +2788,7 @@ edl_species(const char* surf_name, LDBLE * count, char*** names, LDBLE * *moles,
if (names == NULL)
malloc_error();
*moles = (LDBLE*)PHRQ_malloc((sys.size() + 1) * sizeof(LDBLE));
if (moles == NULL)
if (*moles == NULL)
malloc_error();
(*names)[0] = NULL;
@ -2886,13 +2882,13 @@ system_total(const char* total_name, LDBLE * count, char*** names,
*/
size_t count_sys = sys.size();
*names = (char**)PHRQ_malloc((count_sys + 1) * sizeof(char*));
if (names == NULL)
if (*names == NULL)
malloc_error();
*types = (char**)PHRQ_malloc((count_sys + 1) * sizeof(char*));
if (types == NULL)
if (*types == NULL)
malloc_error();
*moles = (LDBLE*)PHRQ_malloc((count_sys + 1) * sizeof(LDBLE));
if (moles == NULL)
if (*moles == NULL)
malloc_error();
(*names)[0] = NULL;
@ -3126,7 +3122,7 @@ system_total_elements(void)
{
t = master_ptr->total;
}
strcpy(name, master[i]->elt->name);
Utilities::strcpy_safe(name, MAX_LENGTH, master[i]->elt->name);
count_sys = sys.size();
sys.resize(count_sys + 1);
sys[count_sys].name = string_duplicate(name);
@ -3173,7 +3169,7 @@ system_total_si(void)
iap += rxn_ptr->s->la * rxn_ptr->coef;
}
si = -phases[i]->lk + iap;
strcpy(name, phases[i]->name);
Utilities::strcpy_safe(name, MAX_LENGTH, phases[i]->name);
size_t count_sys = sys.size();
sys.resize(count_sys + 1);
sys[count_sys].name = string_duplicate(name);
@ -3491,7 +3487,7 @@ system_total_elt(const char* total_name)
{
size_t count_sys = sys.size();
sys.resize(count_sys + 1);
strcpy(name, x[k]->master[0]->elt->name);
Utilities::strcpy_safe(name, MAX_LENGTH, x[k]->master[0]->elt->name);
replace("_psi", "", name);
sys[count_sys].name = string_duplicate(name);
sys[count_sys].moles = elt_list[j].coef;
@ -3736,7 +3732,7 @@ system_total_elt_secondary(const char* total_name)
}
if (l >= count_elts)
continue;
strcpy(name, x[k]->master[0]->elt->name);
Utilities::strcpy_safe(name, MAX_LENGTH, x[k]->master[0]->elt->name);
replace("_psi", "", name);
size_t count_sys = sys.size();
sys.resize(count_sys + 1);
@ -4051,8 +4047,8 @@ iso_value(const char* total_name)
int j;
char token[MAX_LENGTH];
char my_total_name[MAX_LENGTH];
strcpy(token, "");
strcpy(my_total_name, total_name);
Utilities::strcpy_safe(token, MAX_LENGTH, "");
Utilities::strcpy_safe(my_total_name, MAX_LENGTH, total_name);
while (replace(" ", "_", my_total_name));
for (j = 0; j < (int)isotope_ratio.size(); j++)
{
@ -4062,12 +4058,12 @@ iso_value(const char* total_name)
continue;
return (isotope_ratio[j]->converted_ratio);
}
strcpy(my_total_name, total_name);
Utilities::strcpy_safe(my_total_name, MAX_LENGTH, total_name);
while (replace("[", "", my_total_name));
while (replace("]", "", my_total_name));
strcat(token, "R(");
strcat(token, my_total_name);
strcat(token, ")");
Utilities::strcat_safe(token, MAX_LENGTH, "R(");
Utilities::strcat_safe(token, MAX_LENGTH, my_total_name);
Utilities::strcat_safe(token, MAX_LENGTH, ")");
for (j = 0; j < (int)isotope_ratio.size(); j++)
{
if (isotope_ratio[j]->ratio == MISSING)
@ -4086,10 +4082,10 @@ iso_unit(const char* total_name)
char token[MAX_LENGTH], unit[MAX_LENGTH];
class master_isotope* master_isotope_ptr;
char my_total_name[MAX_LENGTH];
strcpy(token, "");
strcpy(my_total_name, total_name);
Utilities::strcpy_safe(token, MAX_LENGTH, "");
Utilities::strcpy_safe(my_total_name, MAX_LENGTH, total_name);
while (replace(" ", "_", my_total_name));
strcpy(unit, "unknown");
Utilities::strcpy_safe(unit, MAX_LENGTH, "unknown");
for (j = 0; j < (int)isotope_ratio.size(); j++)
{
if (isotope_ratio[j]->ratio == MISSING)
@ -4099,16 +4095,16 @@ iso_unit(const char* total_name)
master_isotope_ptr = master_isotope_search(isotope_ratio[j]->isotope_name);
if (master_isotope_ptr != NULL)
{
strcpy(unit, master_isotope_ptr->units);
Utilities::strcpy_safe(unit, MAX_LENGTH, master_isotope_ptr->units);
}
return string_duplicate(unit);
}
strcpy(my_total_name, total_name);
Utilities::strcpy_safe(my_total_name, MAX_LENGTH, total_name);
while (replace("[", "", my_total_name));
while (replace("]", "", my_total_name));
strcat(token, "R(");
strcat(token, my_total_name);
strcat(token, ")");
Utilities::strcat_safe(token, MAX_LENGTH, "R(");
Utilities::strcat_safe(token, MAX_LENGTH, my_total_name);
Utilities::strcat_safe(token, MAX_LENGTH, ")");
for (j = 0; j < (int)isotope_ratio.size(); j++)
{
if (isotope_ratio[j]->ratio == MISSING)
@ -4118,7 +4114,7 @@ iso_unit(const char* total_name)
master_isotope_ptr = master_isotope_search(isotope_ratio[j]->isotope_name);
if (master_isotope_ptr != NULL)
{
strcpy(unit, master_isotope_ptr->units);
Utilities::strcpy_safe(unit, MAX_LENGTH, master_isotope_ptr->units);
}
return string_duplicate(unit);
}

View File

@ -181,6 +181,57 @@ Utilities::safe_exp(LDBLE t)
}
return exp(t);
}
size_t Utilities::
strcpy_safe(char* dest, size_t max, const char* src)
{
size_t lsrc = 0;
try
{
if (dest == nullptr || src == nullptr)
{
std::cerr << "nullptr in Utilities::strcpy_safe." << std::endl;
throw;
}
lsrc = strlen(src);
if (lsrc + 1 > max)
{
std::cerr << "Buffer overrun in Utilities::strcpy_safe." << std::endl;
throw;
}
memcpy(dest, src, (lsrc + 1) * sizeof(char));
}
catch (...)
{
throw;
}
return lsrc;
}
size_t Utilities::
strcat_safe(char* dest, size_t max, const char* src)
{
size_t ldest = 0, lsrc = 0;
try
{
if (dest == nullptr || src == nullptr)
{
std::cerr << "nullptr in Utilities::strcat_safe." << std::endl;
throw;
}
lsrc = strlen(src);
ldest = strlen(dest);
if (ldest + lsrc + 1 > max)
{
std::cerr << "Buffer overrun in Utilities::strcat_safe." << std::endl;
throw;
}
memcpy(&dest[ldest], src, (lsrc + 1) * sizeof(char));
}
catch (...)
{
throw;
}
return ldest + lsrc;
}
//+NAN LDBLE: 7ff8000000000000
//-NAN LDBLE: fff8000000000000
/*

View File

@ -20,7 +20,8 @@ namespace Utilities
void str_toupper(std::string & str);
std::string pad_right(const std::string & str, size_t l);
bool replace(const char *str1, const char *str2, std::string & str);
size_t strcat_safe(char* dest, size_t max, const char* src);
size_t strcpy_safe(char* dest, size_t max, const char* src);
void squeeze_white(std::string & s_l);
double convert_time(double t, std::string in, std::string out);
LDBLE safe_exp(LDBLE t);

View File

@ -4,6 +4,9 @@
#include <istream>
#include <fstream>
#include "phqalloc.h"
#include "string.h"
#include <stdio.h>
#include <stdlib.h>
#if defined(PHREEQCI_GUI)
#ifdef _DEBUG
@ -124,7 +127,7 @@ get_line(void)
if (line == NULL)
malloc_error();
}
strcpy(line, phrq_io->Get_m_line().c_str());
strcpy(line_save, phrq_io->Get_m_line_save().c_str());
Utilities::strcpy_safe(line, max_line, phrq_io->Get_m_line().c_str());
Utilities::strcpy_safe(line_save, max_line, phrq_io->Get_m_line_save().c_str());
return j;
}

View File

@ -31,7 +31,7 @@ inverse_models(void)
* for any marked "new".
*/
int n/*, print1*/;
char string[MAX_LENGTH];
char string[MAX_LENGTH] = "";
if (count_inverse <= 0) return OK;
// Revert to previous headings after inverse modeling
std::vector<std::string> old_headings;
@ -54,10 +54,10 @@ inverse_models(void)
*/
if (inverse[n].pat != NULL)
{
strcpy(string, inverse[n].pat);
Utilities::strcpy_safe(string, MAX_LENGTH, inverse[n].pat);
if (replace(".pat", ".pat", string) != TRUE)
{
strcat(string, ".pat");
Utilities::strcat_safe(string, MAX_LENGTH, ".pat");
}
netpath_file = fopen(string, "w");
if (netpath_file == NULL)
@ -4174,11 +4174,11 @@ print_total_multi(FILE * l_netpath_file, cxxSolution *solution_ptr,
LDBLE sum;
int i, found;
strcpy(elts[0], elt0);
strcpy(elts[1], elt1);
strcpy(elts[2], elt2);
strcpy(elts[3], elt3);
strcpy(elts[4], elt4);
Utilities::strcpy_safe(elts[0], MAX_LENGTH, elt0);
Utilities::strcpy_safe(elts[1], MAX_LENGTH, elt1);
Utilities::strcpy_safe(elts[2], MAX_LENGTH, elt2);
Utilities::strcpy_safe(elts[3], MAX_LENGTH, elt3);
Utilities::strcpy_safe(elts[4], MAX_LENGTH, elt4);
sum = 0;

View File

@ -1,6 +1,7 @@
#include "Phreeqc.h"
#include "phqalloc.h"
#include "Solution.h"
#include "Utils.h"
#if defined(PHREEQCI_GUI)
#ifdef _DEBUG
@ -982,7 +983,7 @@ print_isotope_ratios(void)
/*
* Print isotope ratio
*/
strcpy(token, isotope_ratio[j]->name);
Utilities::strcpy_safe(token, MAX_LENGTH, isotope_ratio[j]->name);
while (replace("_", " ", token) == TRUE);
output_msg(sformatf( " %-20s\t%12.5e\t%15.5g %-10s\n",
token, (double) isotope_ratio[j]->ratio,
@ -1045,7 +1046,7 @@ print_isotope_alphas(void)
/*
* Print isotope ratio
*/
strcpy(token, isotope_alpha[j]->name);
Utilities::strcpy_safe(token, MAX_LENGTH, isotope_alpha[j]->name);
while (replace("_", " ", token) == TRUE);
if (isotope_alpha[j]->named_logk != NULL)
{

View File

@ -884,7 +884,6 @@ int Phreeqc::gammas_a_f(int i1)
{
if (s_x[i]->rxn_x.token[j].s->type == EX)
{
//strcpy(name, s_x[i]->rxn_x.token[j].s->name);
name = s_x[i]->rxn_x.token[j].s->name;
//m_ptr = s_x[i]->rxn_x.token[j].s->primary->elt->master; // appt debug
break;

View File

@ -67,6 +67,7 @@
#include "nvector_serial.h"
#include "sundialstypes.h"
#include "sundialsmath.h"
#include "Utils.h"
/* WARNING don`t include any headers below here */
@ -173,7 +174,7 @@ M_EnvInit_Serial(integertype vec_length)
me->ops->nvprint = N_VPrint_Serial;
/* Attach ID tag */
strcpy(me->tag, ID_TAG_S);
Utilities::strcpy_safe(me->tag, 8, ID_TAG_S);
return (me);

View File

@ -1,5 +1,6 @@
#include "Phreeqc.h"
#include "phqalloc.h"
#include "Utils.h"
#if defined(PHREEQCI_GUI)
#ifdef _DEBUG
@ -133,7 +134,7 @@ parse_eq(char* eqn, std::vector<class elt_list>& new_elt_list, int association)
* Get elements in species or mineral formula
*/
count_elts = 0;
strcpy(token, trxn.token[0].name);
Utilities::strcpy_safe(token, MAX_LENGTH, trxn.token[0].name);
replace("(s)", "", token);
replace("(S)", "", token);
replace("(g)", "", token);

View File

@ -5413,8 +5413,15 @@ k_temp(LDBLE tc, LDBLE pa) /* pa - pressure in atm */
* Calculates log k's for all species and pure_phases
*/
if (tc == current_tc && pa == current_pa && ((fabs(mu_x - current_mu) < 1e-3 * mu_x) || !mu_terms_in_logk))
return OK;
// if (tc == current_tc && pa == current_pa && ((fabs(mu_x - current_mu) < 1e-3 * mu_x) || !mu_terms_in_logk))
// return OK;
if (tc != current_tc) goto proceed;
if (pa != current_pa) goto proceed;
if (fabs(mu_x - current_mu) > 1e-3 * mu_x) goto proceed;
if (mu_terms_in_logk) goto proceed;
return OK;
proceed:
int i;
LDBLE tempk = tc + 273.15;

View File

@ -321,7 +321,7 @@ print_diffuse_layer(cxxSurfaceCharge *charge_ptr)
add_elt_list(s_x[j]->next_elt, moles_surface);
}
/*
strcpy(token, s_h2o->name);
Utilities::strcpy_safe(token, MAX_LENGTH, s_h2o->name);
ptr = &(token[0]);
get_elts_in_species (&ptr, mass_water_surface / gfw_water);
*/
@ -427,9 +427,9 @@ print_eh(void)
/*
* Print result
*/
strcpy(token, master[i]->elt->name);
strcat(token, "/");
strcat(token, master[k]->elt->name);
Utilities::strcpy_safe(token, MAX_LENGTH, master[i]->elt->name);
Utilities::strcat_safe(token, MAX_LENGTH, "/");
Utilities::strcat_safe(token, MAX_LENGTH, master[k]->elt->name);
output_msg(sformatf("\t%-15s%12.4f%12.4f\n", token,
(double) pe, (double) eh));
}
@ -2904,34 +2904,34 @@ punch_identifiers(void)
switch (state)
{
case 0:
strcpy(token, "init");
Utilities::strcpy_safe(token, MAX_LENGTH, "init");
break;
case 1:
strcpy(token, "i_soln");
Utilities::strcpy_safe(token, MAX_LENGTH, "i_soln");
break;
case 2:
strcpy(token, "i_exch");
Utilities::strcpy_safe(token, MAX_LENGTH, "i_exch");
break;
case 3:
strcpy(token, "i_surf");
Utilities::strcpy_safe(token, MAX_LENGTH, "i_surf");
break;
case 4:
strcpy(token, "i_gas");
Utilities::strcpy_safe(token, MAX_LENGTH, "i_gas");
break;
case 5:
strcpy(token, "react");
Utilities::strcpy_safe(token, MAX_LENGTH, "react");
break;
case 6:
strcpy(token, "inverse");
Utilities::strcpy_safe(token, MAX_LENGTH, "inverse");
break;
case 7:
strcpy(token, "advect");
Utilities::strcpy_safe(token, MAX_LENGTH, "advect");
break;
case 8:
strcpy(token, "transp");
Utilities::strcpy_safe(token, MAX_LENGTH, "transp");
break;
default:
strcpy(token, "unknown");
Utilities::strcpy_safe(token, MAX_LENGTH, "unknown");
break;
}
fpunchf(PHAST_NULL("state"), sformat, token);

View File

@ -1097,7 +1097,7 @@ read_exchange_master_species(void)
if (token[0] == '[') {
cptr1 = token;
get_elt(&cptr, element, &l);
strcpy(token, element);
Utilities::strcpy_safe(token, MAX_LENGTH, element);
}
*/
replace("(+", "(", token);
@ -1752,7 +1752,7 @@ read_inv_phases(class inverse *inverse_ptr, const char* cptr)
j = copy_token(token, &cptr, &l);
if (j == EMPTY)
break;
strcpy(token1, token);
Utilities::strcpy_safe(token1, MAX_LENGTH, token);
str_tolower(token1);
if (token1[0] == 'p')
{
@ -3107,7 +3107,7 @@ read_master_species(void)
if (token[0] == '[') {
cptr1 = token;
get_elt(&cptr, element, &l);
strcpy(token, element);
Utilities::strcpy_safe(token, MAX_LENGTH, element);
}
*/
replace("(+", "(", token);
@ -3726,7 +3726,7 @@ read_phases(void)
/*
* Get pointer to each species in the reaction, store new species if necessary
*/
strcpy(token1, trxn.token[0].name);
Utilities::strcpy_safe(token1, MAX_LENGTH, trxn.token[0].name);
replace("(g)", "", token1);
replace("(s)", "", token1);
replace("(G)", "", token1);
@ -3739,7 +3739,7 @@ read_phases(void)
(strstr(trxn.token[i].name, "(S)") == NULL) &&
(strstr(trxn.token[i].name, "(G)") == NULL))
{
strcpy(token1, trxn.token[i].name);
Utilities::strcpy_safe(token1, MAX_LENGTH, trxn.token[i].name);
replace("(aq)", "", token1);
replace("(AQ)", "", token1);
replace("H2O(l)", "H2O", token1);
@ -5737,7 +5737,7 @@ read_use(void)
/*
* Read number
*/
strcpy(token1, token);
Utilities::strcpy_safe(token1, MAX_LENGTH, token);
for (;;)
{
i = copy_token(token, &cptr, &l);
@ -7013,16 +7013,16 @@ read_surface_master_species(void)
master[count_master]->s = s_store(token1.c_str(), l_z, FALSE);
}
master[count_master]->primary = TRUE;
strcpy(token, master[count_master]->elt->name);
Utilities::strcpy_safe(token, MAX_LENGTH, master[count_master]->elt->name);
count_master++;
/*
* Save values in master and species structure for surface psi
*/
strcpy(token1, token);
Utilities::strcpy_safe(token1, MAX_LENGTH, token);
replace("_", " ", token1);
cptr1 = token1;
copy_token(token, &cptr1, &l);
strcat(token, "_psi");
Utilities::strcat_safe(token, MAX_LENGTH, "_psi");
add_psi_master_species(token);
opt_save = OPTION_DEFAULT;
break;
@ -7041,10 +7041,9 @@ add_psi_master_species(char *token)
class species *s_ptr;
class master *master_ptr;
const char* cptr;
char token1[MAX_LENGTH];
char token1[MAX_LENGTH] = "";
int i, n, plane;
strcpy(token1, token);
Utilities::strcpy_safe(token1, MAX_LENGTH, token);
for (plane = SURF_PSI; plane <= SURF_PSI2; plane++)
{
strcpy(token, token1);
@ -9481,7 +9480,7 @@ read_copy(void)
switch (next_keyword)
{
case Keywords::KEY_NONE: /* Have not read line with keyword */
strcpy(nonkeyword, token);
Utilities::strcpy_safe(nonkeyword, MAX_LENGTH, token);
break;
case Keywords::KEY_SOLUTION: /* Solution */
case Keywords::KEY_EQUILIBRIUM_PHASES: /* Pure phases */
@ -9508,7 +9507,7 @@ read_copy(void)
/*
* Read source index
*/
strcpy(token1, token);
Utilities::strcpy_safe(token1, MAX_LENGTH, token);
i = copy_token(token, &cptr, &l);
if (i == DIGIT)
{
@ -9728,8 +9727,8 @@ cleanup_after_parser(CParser &parser)
// check_key sets next_keyword
if (parser.get_m_line_type() == PHRQ_io::LT_EOF)
{
strcpy(line, "");
strcpy(line_save, "");
Utilities::strcpy_safe(line, max_line, "");
Utilities::strcpy_safe(line_save, max_line, "");
next_keyword = Keywords::KEY_END;
return(TRUE);
}
@ -9751,8 +9750,8 @@ cleanup_after_parser(CParser &parser)
if (line == NULL)
malloc_error();
}
strcpy(line, parser.line().c_str());
strcpy(line_save, parser.line_save().c_str());
Utilities::strcpy_safe(line, max_line, parser.line().c_str());
Utilities::strcpy_safe(line_save, max_line, parser.line_save().c_str());
return return_value;
}
/* ---------------------------------------------------------------------- */

View File

@ -707,13 +707,13 @@ add_pp_assemblage(cxxPPassemblage *pp_assemblage_ptr)
comp_ptr->Set_delta(0.0);
if (comp_ptr->Get_add_formula().size() > 0)
{
strcpy(token, comp_ptr->Get_add_formula().c_str());
Utilities::strcpy_safe(token, MAX_LENGTH, comp_ptr->Get_add_formula().c_str());
cptr = &(token[0]);
get_elts_in_species(&cptr, 1.0);
}
else
{
strcpy(token, phase_ptr->formula);
Utilities::strcpy_safe(token, MAX_LENGTH, phase_ptr->formula);
add_elt_list(phase_ptr->next_elt, 1.0);
}
if (comp_ptr->Get_moles() > 0.0)

View File

@ -914,13 +914,13 @@ build_tally_table(void)
paren_count = 0;
if (comp_ptr->Get_add_formula().size() > 0)
{
strcpy(token, comp_ptr->Get_add_formula().c_str());
Utilities::strcpy_safe(token, MAX_LENGTH, comp_ptr->Get_add_formula().c_str());
cptr = &(token[0]);
get_elts_in_species(&cptr, 1.0);
}
else
{
strcpy(token, phase_ptr->formula);
Utilities::strcpy_safe(token, MAX_LENGTH, phase_ptr->formula);
add_elt_list(phase_ptr->next_elt, 1.0);
}
elt_list_combine();
@ -971,7 +971,7 @@ build_tally_table(void)
tally_table[n].type = Ss_phase;
count_elts = 0;
paren_count = 0;
strcpy(token, phase_ptr->formula);
Utilities::strcpy_safe(token, MAX_LENGTH, phase_ptr->formula);
add_elt_list(phase_ptr->next_elt, 1.0);
elt_list_combine();
tally_table[n].formula = elt_list_vsave();
@ -1019,7 +1019,7 @@ build_tally_table(void)
phase_ptr = NULL;
if (kinetics_comp_ptr->Get_namecoef().size() == 1)
{
strcpy(token, kinetics_comp_ptr->Get_namecoef().begin()->first.c_str());
Utilities::strcpy_safe(token, MAX_LENGTH, kinetics_comp_ptr->Get_namecoef().begin()->first.c_str());
phase_ptr = phase_bsearch(token, &p, FALSE);
}
if (phase_ptr != NULL)

View File

@ -819,7 +819,7 @@ replace_solids_gases(void)
/* try phase name without (g) or (s) */
if (phase_ptr == NULL)
{
strcpy(token, token_ptr->name);
Utilities::strcpy_safe(token, MAX_LENGTH, token_ptr->name);
replace("(g)", "", token);
replace("(s)", "", token);
replace("(G)", "", token);
@ -2013,8 +2013,8 @@ tidy_punch(void)
" %s.", pair_ref.first.c_str());
warning_msg(error_string);
}
//strcpy(token, "m_");
//strcat(token, punch.molalities[i].name);
// Utilities::strcpy_safe(token, MAX_LENGTH, "m_");
//Utilities::strcat_safe(token, punch.molalities[i].name);
//fpunchf_heading(sformatf("%*s\t", l, token));
//if (punch.molalities[i].s == NULL)
//{
@ -2039,8 +2039,8 @@ tidy_punch(void)
" %s.", pair_ref.first.c_str());
warning_msg(error_string);
}
//strcpy(token, "la_");
//strcat(token, punch.activities[i].name);
// Utilities::strcpy_safe(token, MAX_LENGTH, "la_");
//Utilities::strcat_safe(token, punch.activities[i].name);
//fpunchf_heading(sformatf("%*s\t", l, token));
//if (punch.activities[i].s == NULL)
//{
@ -2066,8 +2066,8 @@ tidy_punch(void)
" %s.", pair_ref.first.c_str());
warning_msg(error_string);
}
//strcpy(token, "d_");
//strcat(token, punch.pure_phases[i].name);
// Utilities::strcpy_safe(token, MAX_LENGTH, "d_");
//Utilities::strcat_safe(token, punch.pure_phases[i].name);
//fpunchf_heading(sformatf("%*s\t", l, punch.pure_phases[i].name));
//fpunchf_heading(sformatf("%*s\t", l, token));
//if (punch.pure_phases[i].phase == NULL)
@ -2093,8 +2093,8 @@ tidy_punch(void)
" %s.", pair_ref.first.c_str());
warning_msg(error_string);
}
//strcpy(token, "si_");
//strcat(token, punch.si[i].name);
// Utilities::strcpy_safe(token, MAX_LENGTH, "si_");
//Utilities::strcat_safe(token, punch.si[i].name);
//fpunchf_heading(sformatf("%*s\t", l, token));
//if (punch.si[i].phase == NULL)
//{
@ -2126,8 +2126,8 @@ tidy_punch(void)
" %s.", pair_ref.first.c_str());
warning_msg(error_string);
}
//strcpy(token, "g_");
//strcat(token, punch.gases[i].name);
// Utilities::strcpy_safe(token, MAX_LENGTH, "g_");
//Utilities::strcat_safe(token, punch.gases[i].name);
//fpunchf_heading(sformatf("%*s\t", l, token));
//if (punch.gases[i].phase == NULL)
//{
@ -2149,11 +2149,11 @@ tidy_punch(void)
name = "dk_";
name.append(pair_ref.first);
fpunchf_heading(sformatf("%*s\t", l, name.c_str()));
//strcpy(token, "k_");
//strcat(token, punch.kinetics[i].name);
// Utilities::strcpy_safe(token, MAX_LENGTH, "k_");
//Utilities::strcat_safe(token, punch.kinetics[i].name);
//fpunchf_heading(sformatf("%*s\t", l, token));
//strcpy(token, "dk_");
//strcat(token, punch.kinetics[i].name);
// Utilities::strcpy_safe(token, MAX_LENGTH, "dk_");
//Utilities::strcat_safe(token, punch.kinetics[i].name);
//fpunchf_heading(sformatf("%*s\t", l, token));
}
@ -2166,8 +2166,8 @@ tidy_punch(void)
std::string name = "s_";
name.append(pair_ref.first);
fpunchf_heading(sformatf("%*s\t", l, name.c_str()));
//strcpy(token, "s_");
//strcat(token, punch.s_s[i].name);
// Utilities::strcpy_safe(token, MAX_LENGTH, "s_");
//Utilities::strcat_safe(token, punch.s_s[i].name);
//fpunchf_heading(sformatf("%*s\t", l, token));
}
@ -2196,8 +2196,8 @@ tidy_punch(void)
// punch.isotopes[i].name, punch.isotopes[i].name);
// warning_msg(error_string);
//}
//strcpy(token, "I_");
//strcat(token, punch.isotopes[i].name);
// Utilities::strcpy_safe(token, MAX_LENGTH, "I_");
//Utilities::strcat_safe(token, punch.isotopes[i].name);
//fpunchf_heading(sformatf("%*s\t", l, token));
}
@ -2227,8 +2227,8 @@ tidy_punch(void)
// punch.calculate_values[i].name);
// warning_msg(error_string);
//}
//strcpy(token, "V_");
//strcat(token, punch.calculate_values[i].name);
// Utilities::strcpy_safe(token, MAX_LENGTH, "V_");
//Utilities::strcat_safe(token, punch.calculate_values[i].name);
//fpunchf_heading(sformatf("%*s\t", l, token));
}

View File

@ -1784,7 +1784,7 @@ set_initial_moles(int i)
cxxExchComp comp;
count_elts = 0;
paren_count = 0;
strcpy(token, "X");
Utilities::strcpy_safe(token, MAX_LENGTH, "X");
cptr = token;
get_elts_in_species(&cptr, 2e-10);
cptr = token;

View File

@ -233,7 +233,7 @@ compute_gfw(const char *string, LDBLE * gfw)
count_elts = 0;
paren_count = 0;
strcpy(token, string);
Utilities::strcpy_safe(token, MAX_LENGTH, string);
cptr = token;
if (get_elts_in_species(&cptr, 1.0) == ERROR)
{
@ -640,135 +640,6 @@ malloc_error(void)
return;
}
/* ---------------------------------------------------------------------- */
int Phreeqc::
parse_couple(char *token)
/* ---------------------------------------------------------------------- */
{
/*
* Parse couple puts redox couples in standard form
* "+" is removed and couples are rewritten in sort
* order.
*/
int e1, e2, p1, p2;
const char* cptr;
std::string elt1, elt2;
char paren1[MAX_LENGTH], paren2[MAX_LENGTH];
if (strcmp_nocase_arg1(token, "pe") == 0)
{
str_tolower(token);
return (OK);
}
while (replace("(+", "(", token) == TRUE);
cptr = token;
get_elt(&cptr, elt1, &e1);
if (*cptr != '(')
{
error_string = sformatf( "Element name must be followed by "
"parentheses in redox couple, %s.", token);
error_msg(error_string, CONTINUE);
parse_error++;
return (ERROR);
}
paren_count = 1;
paren1[0] = '(';
p1 = 1;
while (*cptr != '\0')
{
cptr++;
if (*cptr == '/' || *cptr == '\0')
{
error_string = sformatf(
"End of line or " "/"
" encountered before end of parentheses, %s.", token);
error_msg(error_string, CONTINUE);
return (ERROR);
}
paren1[p1++] = *cptr;
if (*cptr == '(')
paren_count++;
if (*cptr == ')')
paren_count--;
if (paren_count == 0)
break;
}
paren1[p1] = '\0';
cptr++;
if (*cptr != '/')
{
error_string = sformatf( " " "/" " must follow parentheses "
"ending first half of redox couple, %s.", token);
error_msg(error_string, CONTINUE);
parse_error++;
return (ERROR);
}
cptr++;
get_elt(&cptr, elt2, &e2);
if (strcmp(elt1.c_str(), elt2.c_str()) != 0)
{
error_string = sformatf( "Redox couple must be two redox states "
"of the same element, %s.", token);
error_msg(error_string, CONTINUE);
return (ERROR);
}
if (*cptr != '(')
{
error_string = sformatf( "Element name must be followed by "
"parentheses in redox couple, %s.", token);
error_msg(error_string, CONTINUE);
parse_error++;
return (ERROR);
}
paren2[0] = '(';
paren_count = 1;
p2 = 1;
while (*cptr != '\0')
{
cptr++;
if (*cptr == '/' || *cptr == '\0')
{
error_string = sformatf( "End of line or " "/" " encountered"
" before end of parentheses, %s.", token);
error_msg(error_string, CONTINUE);
return (ERROR);
}
paren2[p2++] = *cptr;
if (*cptr == '(')
paren_count++;
if (*cptr == ')')
paren_count--;
if (paren_count == 0)
break;
}
paren2[p2] = '\0';
if (strcmp(paren1, paren2) < 0)
{
strcpy(token, elt1.c_str());
strcat(token, paren1);
strcat(token, "/");
strcat(token, elt2.c_str());
strcat(token, paren2);
}
else if (strcmp(paren1, paren2) > 0)
{
strcpy(token, elt2.c_str());
strcat(token, paren2);
strcat(token, "/");
strcat(token, elt1.c_str());
strcat(token, paren1);
}
else
{
error_string = sformatf( "Both parts of redox couple are the same, %s.",
token);
error_msg(error_string, CONTINUE);
return (ERROR);
}
return (OK);
}
/* ---------------------------------------------------------------------- */
int Phreeqc::
print_centered(const char *string)
@ -783,7 +654,7 @@ print_centered(const char *string)
for (i = 0; i < l1; i++)
token[i] = '-';
token[i] = '\0';
strcat(token, string);
Utilities::strcat_safe(token, MAX_LENGTH, string);
for (i = 0; i < l2; i++)
token[i + l1 + l] = '-';
token[79] = '\0';