COPASI API  4.16.103
CConfigurationFile.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2015 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) 2006 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include "copasi.h"
16 
17 #include "CConfigurationFile.h"
18 #include "COptions.h"
19 
21 #include "utilities/CVersion.h"
22 #include "utilities/utility.h"
23 #include "utilities/CDirEntry.h"
24 #include "xml/CCopasiXMLParser.h"
25 #include "MIRIAM/CConstants.h"
26 
27 CRecentFiles::CRecentFiles(const std::string & name,
28  const CCopasiContainer * pParent):
29  CCopasiParameterGroup(name, pParent),
30  mpMaxFiles(NULL),
31  mpRecentFiles(NULL)
33 
35  const CCopasiContainer * pParent):
36  CCopasiParameterGroup(src, pParent),
37  mpMaxFiles(NULL),
38  mpRecentFiles(NULL)
40 
42  const CCopasiContainer * pParent):
43  CCopasiParameterGroup(group, pParent),
44  mpMaxFiles(NULL),
45  mpRecentFiles(NULL)
47 
49 {}
50 
52 {
53  mpMaxFiles =
54  assertParameter("MaxFiles", CCopasiParameter::UINT, (unsigned C_INT32) 5)->getValue().pUINT;
55  mpRecentFiles = assertGroup("Recent Files");
56 }
57 
58 void CRecentFiles::addFile(const std::string & file)
59 {
60  std::string FileName = CDirEntry::normalize(file);
61 
62 #ifdef WIN32
63  std::string::size_type pos = FileName.find('\\');
64 
65  while (pos != std::string::npos)
66  {
67  FileName[pos] = '/';
68  pos = FileName.find('\\', pos);
69  }
70 
71 #endif
72 
73  std::string PWD;
74  COptions::getValue("PWD", PWD);
75 
76  if (CDirEntry::isRelativePath(FileName) &&
77  !CDirEntry::makePathAbsolute(FileName, PWD))
78  FileName = CDirEntry::fileName(FileName);
79 
82 
83  std::string NewFile = FileName;
84  std::string ExistingFile;
85 
86  for (; it != end; ++it)
87  {
88  ExistingFile = *(*it)->getValue().pSTRING;
89  (*it)->setValue(NewFile);
90 
91  if (ExistingFile == FileName) return;
92 
93  NewFile = ExistingFile;
94  }
95 
96  if (mpRecentFiles->size() < *mpMaxFiles)
98 
99  return;
100 }
101 
102 CConfigurationFile::CConfigurationFile(const std::string & name,
103  const CCopasiContainer * pParent):
104  CCopasiParameterGroup(name, pParent),
105  mpRecentFiles(NULL),
106  mpRecentSBMLFiles(NULL),
107  mpRecentSEDMLFiles(NULL),
108  mpRecentMIRIAMResources(NULL),
109  mpApplicationFont(NULL),
110  mpValidateUnits(NULL),
111  mpUseOpenGL(NULL),
112  mpUseAdvancedSliders(NULL),
113  mpUseAdvancedEditing(NULL),
114  mpNormalizePerExperiment(NULL),
115  mpWorkingDirectory(NULL)
117 
119  const CCopasiContainer * pParent):
120  CCopasiParameterGroup(src, pParent),
121  mpRecentFiles(NULL),
122  mpRecentSBMLFiles(NULL),
123  mpRecentSEDMLFiles(NULL),
124  mpRecentMIRIAMResources(NULL),
125  mpApplicationFont(NULL),
126  mpValidateUnits(NULL),
127  mpUseOpenGL(NULL),
128  mpUseAdvancedSliders(NULL),
129  mpUseAdvancedEditing(NULL),
130  mpNormalizePerExperiment(NULL),
131  mpWorkingDirectory(NULL)
133 
135  const CCopasiContainer * pParent):
136  CCopasiParameterGroup(group, pParent),
137  mpRecentFiles(NULL),
138  mpRecentSBMLFiles(NULL),
139  mpRecentSEDMLFiles(NULL),
140  mpRecentMIRIAMResources(NULL),
141  mpApplicationFont(NULL),
142  mpValidateUnits(NULL),
143  mpUseOpenGL(NULL),
144  mpUseAdvancedSliders(NULL),
145  mpUseAdvancedEditing(NULL),
146  mpNormalizePerExperiment(NULL),
147  mpWorkingDirectory(NULL)
149 
151 {}
152 
154 {
155  bool success = true;
156 
157  mpRecentFiles =
158  elevate<CRecentFiles, CCopasiParameterGroup>(getGroup("Recent Files"));
159 
160  if (!mpRecentFiles) success = false;
161 
163  elevate<CRecentFiles, CCopasiParameterGroup>(getGroup("Recent SBML Files"));
164 
165  if (!mpRecentSBMLFiles) success = false;
166 
168  elevate<CRecentFiles, CCopasiParameterGroup>(getGroup("Recent SEDML Files"));
169 
170  if (!mpRecentSEDMLFiles) success = false;
171 
173  elevate<CMIRIAMResources, CCopasiParameterGroup>(getGroup("MIRIAM Resources"));
175 
176  if (!mpRecentMIRIAMResources) success = false;
177 
178  return success;
179 }
180 
182 {
183  assertGroup("Recent Files");
184  assertGroup("Recent SBML Files");
185  assertGroup("Recent SEDML Files");
186 
188  assertParameter("Application Font", CCopasiParameter::STRING, std::string(""))->getValue().pSTRING;
189 
190  assertGroup("MIRIAM Resources");
191 
194  mpUseAdvancedSliders = assertParameter("Use Advanced Sliders", CCopasiParameter::BOOL, true)->getValue().pBOOL;
195  mpUseAdvancedEditing = assertParameter("Use Advanced Editing", CCopasiParameter::BOOL, false)->getValue().pBOOL;
196  mpNormalizePerExperiment = assertParameter("Normalize Weights per Experiment", CCopasiParameter::BOOL, true)->getValue().pBOOL;
197  mpWorkingDirectory = assertParameter("Working Directory", CCopasiParameter::STRING, std::string(""))->getValue().pSTRING;
198 
199  elevateChildren();
200 }
201 
203 {
204  std::string ConfigFile;
205  COptions::getValue("ConfigFile", ConfigFile);
206 
208 
209  XML.setConfiguration(*this);
210 
211  bool success = XML.CCopasiXMLInterface::save(ConfigFile, ConfigFile);
212 
213  return success;
214 }
215 
217 {
218  std::string ConfigFile;
219  COptions::getValue("ConfigFile", ConfigFile);
220 
222 
223  bool success = XML.CCopasiXMLInterface::load(ConfigFile, ConfigFile);
224 
225  if (success)
226  {
227  *this = XML.getConfiguration();
229  }
230 
232  {
233  // We load the default MIRIAM resources, which are part of the COPASI installation.
234  std::string MIRIAMResourceFile;
235  COptions::getValue("DefaultConfigDir", MIRIAMResourceFile);
236  MIRIAMResourceFile += CDirEntry::Separator + "MIRIAMResources.xml";
237 
238  CConfigurationFile::CXML XMLMIRIAMResource;
239 
240  if (XMLMIRIAMResource.CCopasiXMLInterface::load(MIRIAMResourceFile,
241  MIRIAMResourceFile))
242  {
243  const CCopasiParameterGroup* group = XMLMIRIAMResource.getConfiguration().getGroup("MIRIAM Resoures");
244 
245  if (group == NULL) return false;
246 
247  *mpRecentMIRIAMResources = *group;
249  }
250  else
251  success = false;
252  }
253 
254  return success;
255 }
256 
258 {return *mpRecentFiles;}
259 
261 {return *mpRecentSBMLFiles;}
262 
263 //TODO SEDML
265 {return *mpRecentSEDMLFiles;}
266 
268 {return *mpRecentMIRIAMResources;}
269 
271 {* mpRecentMIRIAMResources = miriamResources;}
272 
273 const std::string CConfigurationFile::getApplicationFont() const
274 {
275  return *mpApplicationFont;
276 }
277 
278 void CConfigurationFile::setApplicationFont(const std::string & applicationFont)
279 {
280  *mpApplicationFont = applicationFont;
281 }
282 
284 {
285  return *mpUseOpenGL;
286 }
287 
289 {
291 }
292 
294 {
295  return *mpUseAdvancedSliders;
296 }
297 
298 void CConfigurationFile::setUseAdvancedSliders(bool useAdvancedSliders)
299 {
301 }
302 
304 {
305  return *mpUseAdvancedEditing;
306 }
307 
308 void CConfigurationFile::setUseAdvancedEditing(bool useAdvancedEditing)
309 {
311 }
312 
314 {
315  return *mpNormalizePerExperiment;
316 }
317 
319 {
321 }
322 
324 {
325  return *mpValidateUnits;
326 }
327 
329 {
330  return *mpWorkingDirectory;
331 }
332 
333 void CConfigurationFile::setWorkingDirectory(const std::string & workingDirectory)
334 {
335  *mpWorkingDirectory = workingDirectory;
336 }
337 
338 void CConfigurationFile::setValidateUnits(bool validateUnits)
339 {
341 }
342 
345  mConfiguration("Configuration")
346 {
347  mConfiguration.assertGroup("Recent Files");
348  mConfiguration.assertGroup("Recent SBML Files");
349  mConfiguration.assertGroup("MIRIAM Resources");
350  mConfiguration.assertGroup("Recent SEDML Files");
351 }
352 
354 {}
355 
356 bool CConfigurationFile::CXML::save(std::ostream & os,
357  const std::string & relativeTo)
358 {
359  mPWD = relativeTo;
360 
361  os.imbue(std::locale::classic());
362  os.precision(16);
363 
364  mpOstream = &os;
365 
366  *mpOstream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
367  << std::endl;
368 
369  *mpOstream << "<!-- generated with COPASI "
371  << " (http://www.copasi.org) at "
372  << UTCTimeStamp()
373  << " UTC -->"
374  << std::endl;
375 
376  saveParameter(mConfiguration);
377 
378  return true;
379 }
380 
381 bool CConfigurationFile::CXML::load(std::istream & is,
382  const std::string & relativeTo)
383 {
384  mPWD = relativeTo;
385 
386  is.imbue(std::locale::classic());
387  is.precision(16);
388 
389  mpIstream = &is;
390  bool success = true;
391  bool done = false;
392 
393  CVersion Version;
394  CCopasiXMLParser Parser(Version);
395 
396 #define BUFFER_SIZE 0xfffe
397  char * pBuffer = new char[BUFFER_SIZE + 1];
398 
399  while (!done)
400  {
401  mpIstream->get(pBuffer, BUFFER_SIZE, 0);
402 
403  if (mpIstream->eof()) done = true;
404 
405  if (mpIstream->fail() && !done)
406  {
407  std::string ConfigFile;
408  COptions::getValue("ConfigFile", ConfigFile);
410 
411  done = true;
412  success = false;
413  }
414 
415  if (!Parser.parse(pBuffer, -1, done))
416  {
418  Parser.getCurrentLineNumber(),
419  Parser.getCurrentColumnNumber(),
420  Parser.getErrorString());
421  done = true;
422  success = false;
423  }
424  }
425 
426  delete [] pBuffer;
427 #undef BUFFER_SIZE
428 
429  if (success && Parser.getCurrentGroup() != NULL)
430  {
431  mConfiguration = * Parser.getCurrentGroup();
432  mConfiguration.setObjectName("Configuration");
433 
434  delete Parser.getCurrentGroup();
435  }
436  else
437  mConfiguration.clear();
438 
439  return success;
440 }
441 
443 {mConfiguration = configuration;}
444 
446 {return mConfiguration;}
CCopasiParameterGroup * mpRecentFiles
std::string * mpApplicationFont
unsigned C_INT32 * mpMaxFiles
static void setMIRIAMResources(const CMIRIAMResources *pResources)
Definition: CConstants.cpp:81
#define MCConfiguration
static bool isRelativePath(const std::string &path)
Definition: CDirEntry.cpp:414
const CCopasiParameterGroup & getConfiguration() const
const std::string getWorkingDirectory() const
const CCopasiParameterGroup & getResourceList() const
void setUseAdvancedSliders(bool useAdvancedSliders)
virtual bool save(std::ostream &os, const std::string &relativeTo)
const std::string getApplicationFont() const
void setUseAdvancedEditing(bool useAdvancedEditing)
#define MCXML
index_iterator endIndex() const
CRecentFiles & getRecentFiles()
static std::string fileName(const std::string &path)
Definition: CDirEntry.cpp:119
std::string * mpWorkingDirectory
void setUseOpenGL(bool useOpenGL)
CRecentFiles * mpRecentSEDMLFiles
#define C_INT32
Definition: copasi.h:90
bool normalizePerExperiment() const
void setWorkingDirectory(const std::string &workingDirectory)
bool useAdvancedSliders() const
CMIRIAMResources & getRecentMIRIAMResources()
bool useAdvancedEditing() const
static const std::string Separator
Definition: CDirEntry.h:34
struct MESSAGES Message
void addFile(const std::string &file)
index_iterator beginIndex() const
const Value & getValue() const
int getCurrentColumnNumber()
Definition: CExpat.h:480
CRecentFiles * mpRecentFiles
unsigned C_INT32 * pUINT
CCopasiParameterGroup * assertGroup(const std::string &name)
CRecentFiles & getRecentSBMLFiles()
void setValidateUnits(bool validateUnits)
CRecentFiles & getRecentSEDMLFiles()
void setApplicationFont(const std::string &applicationFont)
static void getValue(const std::string &name, CType &value)
Definition: COptions.h:124
CCopasiParameterGroup mConfiguration
CRecentFiles(const std::string &name="Recent Files", const CCopasiContainer *pParent=NULL)
virtual bool load(std::istream &is, const std::string &relativeTo)
void setRecentMIRIAMResources(const CMIRIAMResources &miriamResources)
virtual bool elevateChildren()
long int flag
Definition: f2c.h:52
CMIRIAMResources * mpRecentMIRIAMResources
virtual ~CRecentFiles()
void setConfiguration(const CCopasiParameterGroup &configuration)
void setNormalizePerExperiment(bool flag)
bool addParameter(const CCopasiParameter &parameter)
std::string UTCTimeStamp()
Definition: utility.cpp:64
#define BUFFER_SIZE
CConfigurationFile(const std::string &name="Configuration", const CCopasiContainer *pParent=NULL)
CRecentFiles * mpRecentSBMLFiles
static std::string normalize(const std::string &path)
Definition: CDirEntry.cpp:560
int getCurrentLineNumber() const
Definition: CExpat.h:470
bool parse(const char *pszBuffer, int nLength=-1, bool fIsFinal=true)
Definition: CExpat.h:135
CCopasiParameter * assertParameter(const std::string &name, const CCopasiParameter::Type type, const CType &defaultValue)
CCopasiParameterGroup * getGroup(const std::string &name)
const XML_LChar * getErrorString()
Definition: CExpat.h:445
static bool makePathAbsolute(std::string &relativePath, const std::string &absoluteTo)
Definition: CDirEntry.cpp:481
const CCopasiParameterGroup * getCurrentGroup() const
bool setObjectName(const std::string &name)
const std::string & getVersion() const
Definition: CVersion.cpp:139
parameterGroup::iterator index_iterator
static const CVersion VERSION
Definition: CVersion.h:164