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

#include <CReadConfig.h>

Collaboration diagram for CReadConfig:
Collaboration graph
[legend]

Public Types

enum  Mode { NEXT = 0, SEARCH, LOOP, ALL }
 

Public Member Functions

 CReadConfig (const std::string &name)
 
 CReadConfig (std::istream &in)
 
C_INT32 fail ()
 
void getDefaults ()
 
C_INT32 getVariable (const std::string &name, const std::string &type, void *pout, CReadConfig::Mode mode=CReadConfig::NEXT)
 
C_INT32 getVariable (const std::string &name, const std::string &type, void *pout1, void *pout2, CReadConfig::Mode mode=CReadConfig::NEXT)
 
std::string getVersion ()
 
void rewind ()
 
 ~CReadConfig ()
 

Private Member Functions

 CReadConfig ()
 
C_INT32 initFileBuffer ()
 
std::string lookAhead ()
 

Private Attributes

C_INT32 mFail
 
std::stringstream mFileBuffer
 
std::string mFilename
 
C_INT32 mLineNumber
 
CReadConfig::Mode mMode
 
std::istream * mpBuffer
 
std::string mVersion
 

Detailed Description

CReadConfig class. A more elaborate class description. New Class based on pmutils read functionality

(C) Stefan Hoops 2001

Definition at line 34 of file CReadConfig.h.

Member Enumeration Documentation

Enumerator
NEXT 
SEARCH 
LOOP 
ALL 

Definition at line 39 of file CReadConfig.h.

Constructor & Destructor Documentation

CReadConfig::CReadConfig ( void  )
private

Default constructor. This creates a configuration buffer for input assigning a filename. It is currently useless.

Definition at line 34 of file CReadConfig.cpp.

34  :
35  mpBuffer(NULL),
36  mLineNumber(-1),
38  mFail(0),
39  mVersion(),
40  mFilename(),
41  mFileBuffer()
42 {}
std::string mFilename
Definition: CReadConfig.h:97
CReadConfig::Mode mMode
Definition: CReadConfig.h:80
C_INT32 mFail
Definition: CReadConfig.h:87
C_INT32 mLineNumber
Definition: CReadConfig.h:75
std::string mVersion
Definition: CReadConfig.h:92
std::stringstream mFileBuffer
Definition: CReadConfig.h:102
std::istream * mpBuffer
Definition: CReadConfig.h:70
CReadConfig::CReadConfig ( const std::string &  name)

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

Parameters
namename of the configuration file.

Definition at line 44 of file CReadConfig.cpp.

References getVariable(), initFileBuffer(), and mVersion.

44  :
45  mpBuffer(NULL),
46  mLineNumber(-1),
48  mFail(0),
49  mVersion(),
50  mFilename(name),
51  mFileBuffer()
52 {
54 
55  getVariable("Version", "string", &mVersion);
56 }
C_INT32 initFileBuffer()
std::string mFilename
Definition: CReadConfig.h:97
CReadConfig::Mode mMode
Definition: CReadConfig.h:80
C_INT32 mFail
Definition: CReadConfig.h:87
C_INT32 mLineNumber
Definition: CReadConfig.h:75
std::string mVersion
Definition: CReadConfig.h:92
std::stringstream mFileBuffer
Definition: CReadConfig.h:102
C_INT32 getVariable(const std::string &name, const std::string &type, void *pout, CReadConfig::Mode mode=CReadConfig::NEXT)
Definition: CReadConfig.cpp:81
std::istream * mpBuffer
Definition: CReadConfig.h:70
CReadConfig::CReadConfig ( std::istream &  in)

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

Parameters
std::istream& in

Definition at line 58 of file CReadConfig.cpp.

58  :
59  mpBuffer(&in),
60  mLineNumber(-1),
62  mFail(0),
63  mVersion(),
64  mFilename(),
65  mFileBuffer()
66 {}
std::string mFilename
Definition: CReadConfig.h:97
CReadConfig::Mode mMode
Definition: CReadConfig.h:80
C_INT32 mFail
Definition: CReadConfig.h:87
C_INT32 mLineNumber
Definition: CReadConfig.h:75
std::string mVersion
Definition: CReadConfig.h:92
std::stringstream mFileBuffer
Definition: CReadConfig.h:102
std::istream * mpBuffer
Definition: CReadConfig.h:70
CReadConfig::~CReadConfig ( void  )

Destructor. The destructor calls the method free().

Definition at line 68 of file CReadConfig.cpp.

69 {}

Member Function Documentation

C_INT32 CReadConfig::fail ( )

Returns the failure status.

Returns
mFail
See Also
mFail

Definition at line 71 of file CReadConfig.cpp.

References mFail.

72 {
73  // return the failure state
74  return mFail;
75 }
C_INT32 mFail
Definition: CReadConfig.h:87
void CReadConfig::getDefaults ( )

Load various system wide default values

Definition at line 78 of file CReadConfig.cpp.

79 {}
C_INT32 CReadConfig::getVariable ( const std::string &  name,
const std::string &  type,
void pout,
CReadConfig::Mode  mode = CReadConfig::NEXT 
)

Retrieves a variable from the input file.

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

Definition at line 81 of file CReadConfig.cpp.

References ALL, C_FLOAT64, C_INT16, C_INT32, CCopasiMessage::ERROR, CCopasiMessage::EXCEPTION, lookAhead(), LOOP, MCReadConfig, mFail, mFilename, mLineNumber, mpBuffer, mVersion, rewind(), SEARCH, and strToDouble().

Referenced by CKinFunction::CKinFunction(), CReadConfig(), getVariable(), CFunctionDB::load(), CCompartment::load(), CReaction::load(), CSteadyStateProblem::load(), CMCAProblem::load(), CLNAProblem::load(), CNewtonMethod::load(), CLNATask::load(), CMCATask::load(), CFunctionParameter::load(), CMCAMethod::load(), CTrajectoryProblem::load(), CModel::load(), CFunction::load(), CTrajectoryTask::load(), CSteadyStateTask::load(), CNodeK::load(), CMetab::load(), CMetabOld::load(), CReaction::loadOld(), and CReaction::loadOneRole().

85 {
86  char c[] = " ";
87  size_t equal = 0;
88  std::string Line;
89  std::string Name;
90  std::string Value;
91 
92  mode = (mode & CReadConfig::LOOP) ? CReadConfig::ALL : mode;
93 
94  // Get the current line
95 
96  while (true)
97  {
98  Line.erase();
99 
100  // Read a line form the input buffer
101  mLineNumber++;
102 
103  while (true)
104  {
105  mpBuffer->read(c, 1);
106 
107  if (*c == '\n' || mpBuffer->eof())
108  break;
109 
110  //YH: here we need to delete ^M carriage return, it is \r
111  if (*c == '\r')
112  continue;
113 
114  Line += c;
115  }
116 
117  equal = Line.find('=');
118  Name = Line.substr(0, equal);
119 
120  Value = Line.substr(equal + 1);
121 
122  // The Compartment keyword is used twice. So we must determine by
123  // the context if we have found the correct one in the case the mode
124  // is SEARCH.
125 
126  if ((mode & CReadConfig::SEARCH) &&
127  name == "Compartment" &&
128  Name == "Compartment")
129  {
130  if (lookAhead() != "Volume")
131  {
132  Name = "<CONTINUE>";
133  }
134  }
135 
136  // The Title keyword is used twice. So we must determine by
137  // the context if we have found the correct one in the case the mode
138  // is SEARCH.
139  if ((mode & CReadConfig::SEARCH) &&
140  name == "Title" &&
141  Name == "Title")
142  {
143  if ((mVersion < "4" &&
144  lookAhead() != "TotalMetabolites") ||
145  (mVersion >= "4" &&
146  lookAhead() != "Comments"))
147  {
148  Name = "<CONTINUE>";
149  }
150  }
151 
152  // We found what we are looking for
153  if (name == Name)
154  break;
155 
156  if (mode & CReadConfig::SEARCH)
157  {
158  if (mpBuffer->eof())
159  {
160  if (!(mode & CReadConfig::LOOP))
162  name.c_str(), mFilename.c_str(),
163  mLineNumber);
164 
165  // Rewind the buffer
166  mode = CReadConfig::SEARCH;
167  rewind();
168  }
169 
170  continue;
171  }
172 
173  // We should never reach this line!!!
175  mFilename.c_str(), mLineNumber);
176  }
177 
178  // Return the value depending on the type
179  if (type == "string")
180  {
181  *(std::string *) pout = Value;
182  }
183  else if (type == "C_FLOAT64")
184  {
185  // may be we should check if Value is really a C_FLOAT64
186  *(C_FLOAT64 *) pout = strToDouble(Value.c_str(), NULL);
187  }
188  else if (type == "C_INT32")
189  {
190  // may be we should check if Value is really a integer
191  *(C_INT32 *) pout = atoi(Value.c_str());
192  }
193  else if (type == "C_INT16")
194  {
195  // may be we should check if Value is really a integer
196  *(C_INT16 *) pout = atoi(Value.c_str());
197  }
198  else if (type == "bool")
199  {
200  // may be we should check if Value is really a integer
201  *(bool *) pout = (atoi(Value.c_str()) != 0);
202  }
203  else if (type == "multiline")
204  {
205  Value.erase();
206 
207  while (true)
208  {
209  Line.erase();
210 
211  // Read a line form the input buffer
212  mLineNumber++;
213 
214  while (true)
215  {
216  mpBuffer->read(c, 1);
217 
218  if (*c == '\n' || mpBuffer->eof())
219  break;
220 
221  //YH: here we need to delete ^M carriage return, it is \r
222  if (*c == '\r')
223  continue;
224 
225  Line += c;
226  }
227 
228  if (Line == "End" + name)
229  break;
230 
231  if (Value.length())
232  Value += '\n';
233 
234  Value += Line;
235  }
236 
237  *(std::string *) pout = Value;
238  }
239  else
240  {
242  name.c_str());
243  mFail = 1; //Error
244  }
245 
246  return mFail;
247 }
std::string lookAhead()
std::string mFilename
Definition: CReadConfig.h:97
#define C_INT32
Definition: copasi.h:90
C_INT32 mFail
Definition: CReadConfig.h:87
C_INT32 mLineNumber
Definition: CReadConfig.h:75
std::string mVersion
Definition: CReadConfig.h:92
#define MCReadConfig
#define C_INT16
Definition: copasi.h:91
double strToDouble(const char *str, char const **pTail)
Definition: utility.cpp:325
#define C_FLOAT64
Definition: copasi.h:92
std::istream * mpBuffer
Definition: CReadConfig.h:70
C_INT32 CReadConfig::getVariable ( const std::string &  name,
const std::string &  type,
void pout1,
void pout2,
CReadConfig::Mode  mode = CReadConfig::NEXT 
)

Retrieves a variable from the input file.

Parameters
namename of the variable to be retrieved.
typetype of the variable to be retrieved.
*pout1pointer to the location where the first part of the retrieved variable is stored.
*pout2pointer to the location where the second part of the retrieved variable is stored.
Returns
mFail
See Also
mFail

Definition at line 249 of file CReadConfig.cpp.

References CCopasiMessage::ERROR, getVariable(), MCReadConfig, and mFail.

254 {
255  std::string Value;
256 
257  if ((mFail = getVariable(name, "string", &Value, mode)))
258  return mFail;
259 
260  if (type == "node")
261  {
262  size_t komma = 0;
263 
264  komma = Value.find(",");
265 
266  std::string Type = Value.substr(0, komma);
267  *(char*) pout1 = (char) atoi(Type.c_str());
268 
269  std::string Subtype = Value.substr(komma + 1);
270  *(char*) pout2 = (char) atoi(Subtype.c_str());
271  }
272  else
273  {
275  name.c_str());
276  mFail = 1; //Error
277  }
278 
279  return mFail;
280 }
C_INT32 mFail
Definition: CReadConfig.h:87
#define MCReadConfig
C_INT32 getVariable(const std::string &name, const std::string &type, void *pout, CReadConfig::Mode mode=CReadConfig::NEXT)
Definition: CReadConfig.cpp:81
std::string CReadConfig::getVersion ( )

Retrieves the version string of the configbuffer

Definition at line 76 of file CReadConfig.cpp.

References mVersion.

Referenced by CSteadyStateProblem::load(), CLNAProblem::load(), CMCAProblem::load(), CNewtonMethod::load(), CTrajectoryProblem::load(), and CCopasiDataModel::loadModel().

76 {return mVersion;}
std::string mVersion
Definition: CReadConfig.h:92
C_INT32 CReadConfig::initFileBuffer ( )
private

Initializes the input buffer for reading.

Returns
mFail
See Also
mFail

Definition at line 282 of file CReadConfig.cpp.

References CCopasiMessage::ERROR, CLocaleString::fromUtf8(), MCReadConfig, mFail, mFileBuffer, mFilename, and mpBuffer.

Referenced by CReadConfig().

283 {
284  char c[] = " ";
285 
286  mpBuffer = & mFileBuffer;
287 
288  // read the configuration file into the configuration buffer
289 #ifdef WIN32
290  std::ifstream File(CLocaleString::fromUtf8(mFilename).c_str(), std::ios_base::binary);
291 #else
292  std::ifstream File(CLocaleString::fromUtf8(mFilename).c_str());
293 #endif
294 
295  if (File.fail())
297  mFilename.c_str());
298 
299  while (true)
300  {
301  File.read(c, 1);
302 
303  if (File.eof())
304  break;
305 
306  if (File.fail())
308  mFilename.c_str());
309 
310  mFileBuffer << c;
311  }
312 
313  File.clear();
314 
315  File.close();
316 
317  if (File.fail())
319  mFilename.c_str());
320 
321  return mFail;
322 }
std::string mFilename
Definition: CReadConfig.h:97
C_INT32 mFail
Definition: CReadConfig.h:87
#define MCReadConfig
std::stringstream mFileBuffer
Definition: CReadConfig.h:102
static CLocaleString fromUtf8(const std::string &utf8)
std::istream * mpBuffer
Definition: CReadConfig.h:70
std::string CReadConfig::lookAhead ( )
private

Look ahead to find the next variable name

Definition at line 324 of file CReadConfig.cpp.

References mpBuffer.

Referenced by getVariable().

325 {
326  std::streampos pos = mpBuffer->tellg();
327 
328  std::string Line;
329  *mpBuffer >> Line;
330 
331  mpBuffer->seekg(pos - mpBuffer->tellg(), std::ios_base::cur);
332 
333  return Line.substr(0, Line.find("="));
334 }
std::istream * mpBuffer
Definition: CReadConfig.h:70
void CReadConfig::rewind ( )

Rewind the buffer

Definition at line 336 of file CReadConfig.cpp.

References mLineNumber, and mpBuffer.

Referenced by getVariable().

337 {
338  mpBuffer->clear();
339  mpBuffer->seekg(0, std::ios_base::beg);
340  mLineNumber = 0;
341 
342  return;
343 }
C_INT32 mLineNumber
Definition: CReadConfig.h:75
std::istream * mpBuffer
Definition: CReadConfig.h:70

Member Data Documentation

C_INT32 CReadConfig::mFail
private

Failure status: 0 = no error !0 = error

Definition at line 87 of file CReadConfig.h.

Referenced by fail(), getVariable(), and initFileBuffer().

std::stringstream CReadConfig::mFileBuffer
private

A buffer for reading the file.

Definition at line 102 of file CReadConfig.h.

Referenced by initFileBuffer().

std::string CReadConfig::mFilename
private

Name of the configuration file.

Definition at line 97 of file CReadConfig.h.

Referenced by getVariable(), and initFileBuffer().

C_INT32 CReadConfig::mLineNumber
private

Current line number in the configuration file

Definition at line 75 of file CReadConfig.h.

Referenced by getVariable(), and rewind().

CReadConfig::Mode CReadConfig::mMode
private

Mode = CReadConfig::SEARCH

Definition at line 80 of file CReadConfig.h.

std::istream* CReadConfig::mpBuffer
private

A pointer to the input buffer

Definition at line 70 of file CReadConfig.h.

Referenced by getVariable(), initFileBuffer(), lookAhead(), and rewind().

std::string CReadConfig::mVersion
private

Definition at line 92 of file CReadConfig.h.

Referenced by CReadConfig(), getVariable(), and getVersion().


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