COPASI API  4.16.103
Public Member Functions | Private Member Functions | Private Attributes | List of all members
CWriteConfig Class Reference

#include <CWriteConfig.h>

Collaboration diagram for CWriteConfig:
Collaboration graph
[legend]

Public Member Functions

 CWriteConfig ()
 
 CWriteConfig (const std::string &name, std::ios_base::openmode mode=std::ios_base::out)
 
C_INT32 fail ()
 
C_INT32 flush ()
 
void setDefaults ()
 
C_INT32 setVariable (const std::string &name, const std::string &type, const void *pout)
 
C_INT32 setVariable (const std::string &name, const std::string &type, const void *pout1, const void *pout2)
 
 ~CWriteConfig ()
 

Private Member Functions

C_INT32 commit ()
 
void writeVersion (void)
 

Private Attributes

std::stringstream mBuffer
 
C_INT32 mFail
 
std::string mFileName
 
unsigned C_INT32 mLineNumber
 
std::ios_base::openmode mOpenMode
 

Detailed Description

CWriteConfig class. A more elaborate class description.

New Class based on pmutils read functionality (C) Stefan Hoops 2001

Definition at line 25 of file CWriteConfig.h.

Constructor & Destructor Documentation

CWriteConfig::CWriteConfig ( void  )

Default consructor. This creates a configuration buffer without assigning a filename. It is currently useless.

Definition at line 30 of file CWriteConfig.cpp.

References mBuffer, mFail, mFileName, mLineNumber, and mOpenMode.

31 {
32  // initialize everything
33  mFileName = "";
34  mOpenMode = std::ios_base::out;
35  mLineNumber = 0;
36  mFail = 0;
37 
38  mBuffer.setf(std::ios_base::scientific);
39  mBuffer.precision(16);
40 }
std::string mFileName
Definition: CWriteConfig.h:103
std::ios_base::openmode mOpenMode
Definition: CWriteConfig.h:118
C_INT32 mFail
Definition: CWriteConfig.h:125
unsigned C_INT32 mLineNumber
Definition: CWriteConfig.h:113
std::stringstream mBuffer
Definition: CWriteConfig.h:108
CWriteConfig::CWriteConfig ( const std::string &  name,
std::ios_base::openmode  mode = std::ios_base::out 
)

Specified consructor. This opens the configuration file using the filename specified as the argument.

Parameters
namename of the confguration file.
modeoutput mode, defaults to creating a new file.

Definition at line 42 of file CWriteConfig.cpp.

References mBuffer, mFail, mFileName, mLineNumber, mOpenMode, and writeVersion().

43 {
44  // initialize everything
45  mFileName = name;
46  mOpenMode = mode;
47  mLineNumber = 0;
48  mFail = 0;
49 
50  mBuffer.setf(std::ios_base::scientific);
51  mBuffer.precision(16);
52 
53  if (mOpenMode & std::ios_base::out)
54  writeVersion();
55 }
std::string mFileName
Definition: CWriteConfig.h:103
void writeVersion(void)
std::ios_base::openmode mOpenMode
Definition: CWriteConfig.h:118
C_INT32 mFail
Definition: CWriteConfig.h:125
unsigned C_INT32 mLineNumber
Definition: CWriteConfig.h:113
std::stringstream mBuffer
Definition: CWriteConfig.h:108
CWriteConfig::~CWriteConfig ( void  )

Destructor. The destructor calls the method flush().

Definition at line 57 of file CWriteConfig.cpp.

References commit().

58 {
59  commit();
60 }
C_INT32 commit()

Member Function Documentation

C_INT32 CWriteConfig::commit ( void  )
private

Commits all information to the configuration file. This method is called by the destructor.

Definition at line 75 of file CWriteConfig.cpp.

References fatalError, mBuffer, mFail, mFileName, and mOpenMode.

Referenced by flush(), and ~CWriteConfig().

76 {
77 #ifdef WIN32
78  mOpenMode |= std::ios_base::binary;
79 #endif
80 
81  std::ofstream ConfigFile(utf8ToLocale(mFileName).c_str(), mOpenMode);
82 
83  if (ConfigFile.fail())
84  {
85  fatalError();
86  return mFail = 1;
87  }
88 
89  ConfigFile << mBuffer.str();
90 
91  if (ConfigFile.fail())
92  {
93  fatalError();
94  return mFail = 1;
95  }
96 
97  mOpenMode |= std::ios_base::app;
98  return mFail;
99 }
std::string mFileName
Definition: CWriteConfig.h:103
#define fatalError()
std::ios_base::openmode mOpenMode
Definition: CWriteConfig.h:118
C_INT32 mFail
Definition: CWriteConfig.h:125
std::stringstream mBuffer
Definition: CWriteConfig.h:108
C_INT32 CWriteConfig::fail ( )

Returns the failure status.

Returns
mFail
See Also
mFail

Definition at line 104 of file CWriteConfig.cpp.

References mFail.

105 {
106  // return the failure state
107  return mFail;
108 }
C_INT32 mFail
Definition: CWriteConfig.h:125
C_INT32 CWriteConfig::flush ( void  )

Flush the output buffer to the configuration file.

Returns
mFail
See Also
mFail

Definition at line 62 of file CWriteConfig.cpp.

References commit(), fatalError, mBuffer, and mFail.

63 {
64  if (commit())
65  {
66  fatalError();
67  return mFail;
68  }
69 
70  mBuffer.str(""); // We need to reset the string buffer.
71 
72  return mFail;
73 }
#define fatalError()
C_INT32 mFail
Definition: CWriteConfig.h:125
C_INT32 commit()
std::stringstream mBuffer
Definition: CWriteConfig.h:108
void CWriteConfig::setDefaults ( )

Save various default values

Definition at line 101 of file CWriteConfig.cpp.

102 {}
C_INT32 CWriteConfig::setVariable ( const std::string &  name,
const std::string &  type,
const void pout 
)

Writes a variable to the output file.

Parameters
namename of the variable to be written.
typetype of the variable to be written.
*poutpointer to the location where the variable is stored.
Returns
mFail
See Also
mFail

Definition at line 110 of file CWriteConfig.cpp.

References C_FLOAT64, C_INT16, C_INT32, fatalError, mBuffer, mFail, and mLineNumber.

Referenced by writeVersion().

113 {
114  mBuffer << name;
115 
116  if (pout)
117  {
118  if (type != "multiline")
119  mBuffer << "=";
120 
121  // Return the value depending on the type
122  if (type == "string")
123  {
124  mBuffer << *(std::string *) pout;
125  }
126  else if (type == "C_FLOAT64")
127  {
128  mBuffer << *(C_FLOAT64 *) pout;
129  }
130  else if (type == "C_INT32")
131  {
132  mBuffer << *(C_INT32 *) pout;
133  }
134  else if (type == "C_INT16")
135  {
136  mBuffer << *(C_INT16 *) pout;
137  }
138  else if (type == "bool")
139  {
140  mBuffer << (*(bool *) pout) ? 1 : 0;
141  }
142  else if (type == "multiline")
143  {
144  mBuffer << std::endl;
145  mBuffer << *(std::string *) pout << std::endl;
146  mBuffer << "End" << name;
147  }
148  else
149  {
150  fatalError();
151  mFail = 1; //Error
152  }
153  }
154 
155  mBuffer << std::endl;
156  mLineNumber++;
157 
158  return mFail;
159 }
#define fatalError()
#define C_INT32
Definition: copasi.h:90
C_INT32 mFail
Definition: CWriteConfig.h:125
#define C_INT16
Definition: copasi.h:91
unsigned C_INT32 mLineNumber
Definition: CWriteConfig.h:113
std::stringstream mBuffer
Definition: CWriteConfig.h:108
#define C_FLOAT64
Definition: copasi.h:92
C_INT32 CWriteConfig::setVariable ( const std::string &  name,
const std::string &  type,
const void pout1,
const void pout2 
)

Definition at line 161 of file CWriteConfig.cpp.

References C_INT32, fatalError, mBuffer, mFail, and mLineNumber.

165 {
166  mBuffer << name << "=";
167 
168  // Return the value depending on the type
169 
170  if (type == "node")
171  {
172  // pout1 and pout2 point to chars mType and mSubtype.
173  // Therfore, we need this complicated cast.
174  mBuffer << (C_INT32) (*(char *) pout1) << ","
175  << (C_INT32) (*(char *) pout2)
176  // This will help to debug
177  // << "\t" << *(char *) pout1 << "," << *(char *) pout2
178 ;
179  }
180  else
181  {
182  fatalError();
183  mFail = 1; //Error
184  }
185 
186  mBuffer << std::endl;
187  mLineNumber++;
188 
189  return mFail;
190 }
#define fatalError()
#define C_INT32
Definition: copasi.h:90
C_INT32 mFail
Definition: CWriteConfig.h:125
unsigned C_INT32 mLineNumber
Definition: CWriteConfig.h:113
std::stringstream mBuffer
Definition: CWriteConfig.h:108
void CWriteConfig::writeVersion ( void  )
private

Writes a version number to the file. This method is called by one of the constructors.

Definition at line 192 of file CWriteConfig.cpp.

References setVariable().

Referenced by CWriteConfig().

193 {
194  if (Copasi)
195  setVariable("Version", "string", &Copasi->ProgramVersion.getVersion());
196  else
197  {
198  std::string Version("4.0.0");
199  setVariable("Version", "string", &Version);
200  }
201 }
C_INT32 setVariable(const std::string &name, const std::string &type, const void *pout)

Member Data Documentation

std::stringstream CWriteConfig::mBuffer
private

Output file

Definition at line 108 of file CWriteConfig.h.

Referenced by commit(), CWriteConfig(), flush(), and setVariable().

C_INT32 CWriteConfig::mFail
private

Failure status: 0 = no error !0 = error

Definition at line 125 of file CWriteConfig.h.

Referenced by commit(), CWriteConfig(), fail(), flush(), and setVariable().

std::string CWriteConfig::mFileName
private

Name of the configuration file.

Definition at line 103 of file CWriteConfig.h.

Referenced by commit(), and CWriteConfig().

unsigned C_INT32 CWriteConfig::mLineNumber
private

Current line number in the configuration file

Definition at line 113 of file CWriteConfig.h.

Referenced by CWriteConfig(), and setVariable().

std::ios_base::openmode CWriteConfig::mOpenMode
private

Open mode

Definition at line 118 of file CWriteConfig.h.

Referenced by commit(), and CWriteConfig().


The documentation for this class was generated from the following files: