COPASI API  4.16.103
CWriteConfig.cpp
Go to the documentation of this file.
1 /* Begin CVS Header
2  $Source: /Volumes/Home/Users/shoops/cvs/copasi_dev/copasi/utilities/CWriteConfig.cpp,v $
3  $Revision: 1.19 $
4  $Name: $
5  $Author: shoops $
6  $Date: 2006/07/19 15:55:09 $
7  End CVS Header */
8 
9 // Copyright 2005 by Pedro Mendes, Virginia Tech Intellectual
10 // Properties, Inc. and EML Research, gGmbH.
11 // All rights reserved.
12 
13 // CWriteConfig
14 //
15 // New Class based on pmutils read functionality
16 // (C) Stefan Hoops 2001
17 
18 #include <string>
19 #include <iostream>
20 #include <fstream>
21 
22 #include <stdio.h>
23 #include <assert.h>
24 
25 #include "copasi.h"
26 #include "CGlobals.h"
27 #include "CWriteConfig.h"
28 #include "CCopasiMessage.h"
29 
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 }
41 
42 CWriteConfig::CWriteConfig(const std::string& name, std::ios_base::openmode mode)
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 }
56 
58 {
59  commit();
60 }
61 
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 }
74 
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 }
100 
102 {}
103 
105 {
106  // return the failure state
107  return mFail;
108 }
109 
110 C_INT32 CWriteConfig::setVariable(const std::string & name,
111  const std::string & type,
112  const void *pout)
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 }
160 
161 C_INT32 CWriteConfig::setVariable(const std::string & name,
162  const std::string & type,
163  const void *pout1,
164  const void *pout2)
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 }
191 
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 }
std::string mFileName
Definition: CWriteConfig.h:103
C_INT32 setVariable(const std::string &name, const std::string &type, const void *pout)
#define fatalError()
#define C_INT32
Definition: copasi.h:90
void writeVersion(void)
C_INT32 flush()
std::ios_base::openmode mOpenMode
Definition: CWriteConfig.h:118
C_INT32 mFail
Definition: CWriteConfig.h:125
#define C_INT16
Definition: copasi.h:91
C_INT32 fail()
C_INT32 commit()
unsigned C_INT32 mLineNumber
Definition: CWriteConfig.h:113
std::stringstream mBuffer
Definition: CWriteConfig.h:108
#define C_FLOAT64
Definition: copasi.h:92
void setDefaults()