COPASI API  4.16.103
CReadConfig.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 by Pedro Mendes, Virginia Tech Intellectual
2 // Properties, Inc., University of Heidelberg, and The University
3 // of Manchester.
4 // All rights reserved.
5 
6 // Copyright (C) 2008 - 2009 by Pedro Mendes, Virginia Tech Intellectual
7 // Properties, Inc., EML Research, gGmbH, University of Heidelberg,
8 // and The University of Manchester.
9 // All rights reserved.
10 
11 // Copyright (C) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 // CReadConfig
16 //
17 // New Class based on pmutils read functionality
18 // (C) Stefan Hoops 2001
19 
20 #include <sstream>
21 #include <stdlib.h>
22 #include <assert.h>
23 
24 #include "copasi.h"
25 
26 #include "CCopasiMessage.h"
27 #include "CReadConfig.h"
28 #include "utility.h"
30 
31 // char *initFileBuffer(char *name);
32 // static C_INT32 GetFileSize(const char *name);
33 
35  mpBuffer(NULL),
36  mLineNumber(-1),
37  mMode(CReadConfig::NEXT),
38  mFail(0),
39  mVersion(),
40  mFilename(),
41  mFileBuffer()
42 {}
43 
44 CReadConfig::CReadConfig(const std::string& name):
45  mpBuffer(NULL),
46  mLineNumber(-1),
47  mMode(CReadConfig::NEXT),
48  mFail(0),
49  mVersion(),
50  mFilename(name),
51  mFileBuffer()
52 {
54 
55  getVariable("Version", "string", &mVersion);
56 }
57 
58 CReadConfig::CReadConfig(std::istream & in):
59  mpBuffer(&in),
60  mLineNumber(-1),
61  mMode(CReadConfig::NEXT),
62  mFail(0),
63  mVersion(),
64  mFilename(),
65  mFileBuffer()
66 {}
67 
69 {}
70 
72 {
73  // return the failure state
74  return mFail;
75 }
76 std::string CReadConfig::getVersion() {return mVersion;}
77 
79 {}
80 
81 C_INT32 CReadConfig::getVariable(const std::string& name,
82  const std::string& type,
83  void * pout,
84  enum Mode mode)
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 }
248 
249 C_INT32 CReadConfig::getVariable(const std::string& name,
250  const std::string& type,
251  void * pout1,
252  void * pout2,
253  enum Mode mode)
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 }
281 
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 }
323 
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 }
335 
337 {
338  mpBuffer->clear();
339  mpBuffer->seekg(0, std::ios_base::beg);
340  mLineNumber = 0;
341 
342  return;
343 }
std::string lookAhead()
C_INT32 initFileBuffer()
std::string mFilename
Definition: CReadConfig.h:97
std::string getVersion()
Definition: CReadConfig.cpp:76
#define C_INT32
Definition: copasi.h:90
C_INT32 fail()
Definition: CReadConfig.cpp:71
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
std::stringstream mFileBuffer
Definition: CReadConfig.h:102
double strToDouble(const char *str, char const **pTail)
Definition: utility.cpp:325
#define C_FLOAT64
Definition: copasi.h:92
C_INT32 getVariable(const std::string &name, const std::string &type, void *pout, CReadConfig::Mode mode=CReadConfig::NEXT)
Definition: CReadConfig.cpp:81
static CLocaleString fromUtf8(const std::string &utf8)
void getDefaults()
Definition: CReadConfig.cpp:78
std::istream * mpBuffer
Definition: CReadConfig.h:70