replaced dump_file with dump_ostream

git-svn-id: svn://136.177.114.72/svn_GW/phreeqcpp/branches/ErrorHandling@5867 1feff8c3-07ed-0310-ac33-dd36852eb9cd
This commit is contained in:
Scott R Charlton 2011-12-06 07:30:12 +00:00
parent 1f7e4e16e9
commit b260023bbf
3 changed files with 54 additions and 41 deletions

View File

@ -14,7 +14,7 @@ PHRQ_io(void)
log_file = NULL;
punch_file = NULL;
error_file = NULL;
dump_file = NULL;
dump_ostream = NULL;
io_error_count = 0;
output_file_on = true;
@ -68,10 +68,14 @@ PHRQ_io::Set_punch_file(FILE * out)
this->punch_file = out;
}
void
PHRQ_io::Set_dump_file(FILE * out)
PHRQ_io::Set_dump_ostream(std::ostream *os)
{
safe_close(&this->dump_file);
this->dump_file = out;
this->dump_ostream = os;
}
std::ostream *
PHRQ_io::Get_dump_ostream(void)
{
return this->dump_ostream;
}
// ---------------------------------------------------------------------- */
@ -371,11 +375,10 @@ error_msg(const char *err_str, bool stop)
/* ---------------------------------------------------------------------- */
bool PHRQ_io::
dump_open(const char *file_name)
dump_open(const char *file_name, std::ios_base::openmode mode)
/* ---------------------------------------------------------------------- */
{
safe_close(&dump_file);
if ((dump_file = fopen(file_name, "w")) == NULL)
if ((dump_ostream = new std::ofstream(file_name, mode)) == NULL)
{
return false; // error
}
@ -386,9 +389,9 @@ void PHRQ_io::
dump_fflush(void)
/* ---------------------------------------------------------------------- */
{
if (dump_file && dump_file_on)
if (dump_ostream && dump_file_on)
{
fflush(dump_file);
dump_ostream->flush();
}
}
/* ---------------------------------------------------------------------- */
@ -396,16 +399,17 @@ void PHRQ_io::
dump_close(void)
/* ---------------------------------------------------------------------- */
{
safe_close(&dump_file);
delete dump_ostream;
dump_ostream = NULL;
}
/* ---------------------------------------------------------------------- */
void PHRQ_io::
dump_rewind(void)
/* ---------------------------------------------------------------------- */
{
if (dump_file && dump_file_on)
if (dump_ostream && dump_file_on)
{
rewind(dump_file);
dump_ostream->seekp(0, std::ios_base::beg);
}
}
/* ---------------------------------------------------------------------- */
@ -413,8 +417,14 @@ bool PHRQ_io::
dump_isopen(void)
/* ---------------------------------------------------------------------- */
{
if (dump_file)
return true; // open
if (dump_ostream)
{
if (std::ofstream *ofs = dynamic_cast<std::ofstream*>(dump_ostream))
{
return ofs->is_open();
}
return true;
}
return false;
}
/* ---------------------------------------------------------------------- */
@ -422,9 +432,9 @@ void PHRQ_io::
dump_msg(const char * str)
/* ---------------------------------------------------------------------- */
{
if (dump_file != NULL && dump_file_on)
if (dump_ostream != NULL && dump_file_on)
{
fprintf(dump_file, "%s", str);
(*dump_ostream) << str;
}
}
@ -547,12 +557,13 @@ close_output_files(void)
ret |= fclose(log_file);
if (punch_file != NULL)
ret |= fclose(punch_file);
if (dump_file != NULL)
ret |= fclose(dump_file);
if (dump_ostream != NULL)
delete dump_ostream;
if (error_file != NULL)
ret |= fclose(error_file);
error_file = NULL;
output_file = log_file = punch_file = dump_file = NULL;
output_file = log_file = punch_file = NULL;
dump_ostream = NULL;
return ret;
}
//safe_close is static method

View File

@ -7,7 +7,10 @@
#define IPQ_DLL_EXPORT
#endif
#include <sstream>
#include <ios>
#include <iosfwd>
#include <exception>
class PhreeqcStop : std::exception
{
};
@ -60,7 +63,7 @@ public:
void warning_msg(const char *err_str);
// dump_file
bool dump_open(const char *file_name);
bool dump_open(const char *file_name, std::ios_base::openmode mode = std::ios_base::out);
void dump_fflush(void);
void dump_close(void);
void dump_rewind(void);
@ -80,7 +83,8 @@ public:
void Set_error_file(FILE * out);
void Set_log_file(FILE * out);
void Set_punch_file(FILE * out);
void Set_dump_file(FILE * out);
void Set_dump_ostream(std::ostream *dump_ostream);
std::ostream *Get_dump_ostream(void);
// close input file
void close_input(void);
@ -121,7 +125,7 @@ protected:
FILE *log_file;
FILE *punch_file;
FILE *error_file;
FILE *dump_file;
std::ostream *dump_ostream;
int io_error_count;
bool output_file_on;

View File

@ -1943,31 +1943,29 @@ int Phreeqc::
dump_entities(void)
/* ---------------------------------------------------------------------- */
{
int return_value;
return_value = OK;
if (!dump_info.Get_bool_any())
{
return(OK);
}
std::ofstream dump_stream;
if (dump_info.Get_append())
if (this->phrq_io)
{
//dump_stream.open(dump_info.get_file_name(), std::ios_base::app);
dump_stream.open(dump_info.Get_file_name().c_str(), std::ios_base::app);
std::ios_base::openmode mode = std::ios_base::out;
if (dump_info.Get_append())
{
mode = std::ios_base::app;
}
if (this->phrq_io->dump_open(dump_info.Get_file_name().c_str(), mode))
{
dump_ostream(*this->phrq_io->Get_dump_ostream());
this->phrq_io->dump_close();
}
else
{
sprintf(error_string, "Unable to open dump file \"%s\"", dump_info.Get_file_name().c_str());
error_msg(error_string, STOP);
}
}
else
{
dump_stream.open(dump_info.Get_file_name().c_str());
}
if (!dump_stream.is_open())
{
sprintf(error_string, "Unable to open dump file \"%s\"", dump_info.Get_file_name().c_str());
error_msg(error_string, STOP);
}
dump_ostream(dump_stream);
return (OK);
}
/* ---------------------------------------------------------------------- */