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

#include <COptions.h>

Collaboration diagram for COptions:
Collaboration graph
[legend]

Classes

class  COptionValue
 
class  COptionValueTemplate
 

Public Types

typedef std::map< std::string,
std::string > 
defaultType
 
typedef std::vector< std::string > nonOptionType
 
typedef std::map< std::string,
COptionValue * > 
optionType
 

Public Member Functions

 ~COptions ()
 

Static Public Member Functions

static void cleanup ()
 
template<class CType >
static bool compareValue (const std::string &name, const CType &value)
 
static std::string getEnvironmentVariable (const std::string &name)
 
static const nonOptionTypegetNonOptions ()
 
static std::string getPWD (void)
 
template<class CType >
static void getValue (const std::string &name, CType &value)
 
static void init (C_INT argc=0, char *argv[]=NULL)
 

Private Member Functions

 COptions ()
 

Static Private Member Functions

static std::string getConfigDir (void)
 
static std::string getConfigFile (void)
 
static std::string getCopasiDir (void)
 
static std::string getHome (void)
 
static std::string getTemp (void)
 
template<class CType >
static void setValue (const std::string &name, const CType &value)
 

Static Private Attributes

static nonOptionType mNonOptions
 
static optionType mOptions
 

Detailed Description

COptions class. The class retrieves the options from the commandline and various files. The options as well as their type can be retreived by name.

Created for Copasi by Stefan Hoops 2002

Definition at line 40 of file COptions.h.

Member Typedef Documentation

typedef std::map< std::string, std::string > COptions::defaultType

Definition at line 110 of file COptions.h.

typedef std::vector< std::string > COptions::nonOptionType

Definition at line 109 of file COptions.h.

typedef std::map< std::string, COptionValue * > COptions::optionType

Definition at line 108 of file COptions.h.

Constructor & Destructor Documentation

COptions::COptions ( )
private

Definition at line 50 of file COptions.cpp.

References CONSTRUCTOR_TRACE.

#define CONSTRUCTOR_TRACE
Definition: copasi.h:202
COptions::~COptions ( )

Definition at line 53 of file COptions.cpp.

References DESTRUCTOR_TRACE.

#define DESTRUCTOR_TRACE
Definition: copasi.h:206

Member Function Documentation

void COptions::cleanup ( )
static

Definition at line 243 of file COptions.cpp.

References mOptions, and pdelete.

Referenced by CCopasiRootContainer::destroy().

244 {
245  optionType::iterator begin = mOptions.begin();
246  optionType::iterator end = mOptions.end();
247 
248  for (; begin != end; begin++) pdelete(begin->second);
249 }
#define pdelete(p)
Definition: copasi.h:215
static optionType mOptions
Definition: COptions.h:114
template<class CType >
static bool COptions::compareValue ( const std::string &  name,
const CType &  value 
)
inlinestatic

Definition at line 128 of file COptions.h.

References mOptions.

Referenced by CopasiUI3Window::create(), CCopasiMessage::handler(), init(), main(), CopasiUI3Window::openInitialDocument(), and validate().

130  {return mOptions[name]->compareValue(value);}
static optionType mOptions
Definition: COptions.h:114
std::string COptions::getConfigDir ( void  )
staticprivate

Definition at line 436 of file COptions.cpp.

References CDirEntry::createDir(), getValue(), and CDirEntry::Separator.

Referenced by init().

437 {
438  std::string Home;
439 
440  getValue("Home", Home);
441 
442  if (!CDirEntry::createDir(".copasi", Home))
443  return Home;
444 
445  return Home + CDirEntry::Separator + ".copasi";
446 }
static bool createDir(const std::string &dir, const std::string &parent="")
Definition: CDirEntry.cpp:180
static const std::string Separator
Definition: CDirEntry.h:34
static void getValue(const std::string &name, CType &value)
Definition: COptions.h:124
std::string COptions::getConfigFile ( void  )
staticprivate

Definition at line 448 of file COptions.cpp.

References getValue(), and CDirEntry::Separator.

Referenced by init().

449 {
450  std::string ConfigDir;
451 
452  getValue("ConfigDir", ConfigDir);
453 
454  return ConfigDir + CDirEntry::Separator + "copasi";
455 }
static const std::string Separator
Definition: CDirEntry.h:34
static void getValue(const std::string &name, CType &value)
Definition: COptions.h:124
std::string COptions::getCopasiDir ( void  )
staticprivate

Definition at line 260 of file COptions.cpp.

References CDirEntry::dirName(), getEnvironmentVariable(), MCConfiguration, CCopasiMessage::RAW, and CLocaleString::toUtf8().

Referenced by init().

261 {
262  std::string CopasiDir;
263 
264  CopasiDir = getEnvironmentVariable("COPASIDIR");
265 
266 #ifdef WIN32
267 
268  if (CopasiDir == "")
269  {
270  DWORD PrgNameSize = 256;
271  DWORD Returned;
272  CLocaleString::lchar * PrgName = new CLocaleString::lchar[PrgNameSize];
273 
274  while (!(Returned = GetModuleFileNameW(NULL, PrgName, PrgNameSize)) ||
275  PrgNameSize == Returned)
276  {
277  if (GetLastError() != ERROR_ALREADY_EXISTS)
278  {
279  *PrgName = 0;
280  break;
281  }
282 
283  delete [] PrgName;
284  PrgNameSize *= 2;
285  PrgName = new CLocaleString::lchar[PrgNameSize];
286  }
287 
288  CopasiDir = CLocaleString(PrgName).toUtf8();
289 
290  delete [] PrgName;
291 
292  /* Get rid of the executable */
293  CopasiDir = CDirEntry::dirName(CopasiDir);
294 
295  /* Get rid of bin or sbin */
296  CopasiDir = CDirEntry::dirName(CopasiDir);
297  }
298 
299 #endif // WIN32
300 
301 #ifdef Darwin
302 
303  if (CopasiDir == "")
304  {
305  CFBundleRef MainBundleRef = NULL;
306  MainBundleRef = CFBundleGetMainBundle();
307 
308  if (MainBundleRef != NULL)
309  {
310  CFURLRef pluginRef = NULL;
311  pluginRef = CFBundleCopyBundleURL(MainBundleRef);
312 
313  if (pluginRef != NULL)
314  {
315  CFStringRef macPath = NULL;
316  macPath = CFURLCopyFileSystemPath(pluginRef, kCFURLPOSIXPathStyle);
317 
318  if (macPath != NULL)
319  {
320  CFIndex size = CFStringGetLength(macPath);
321  char* cString = new char[size + 1];
322  CFStringGetCString(macPath, cString, size + 1, kCFStringEncodingUTF8);
323  CopasiDir = CLocaleString(cString).toUtf8();
324  delete[] cString;
325  }
326  }
327  }
328  }
329 
330 #endif // Darwin
331 
332  if (CopasiDir == "")
334 
335  return CopasiDir;
336 }
#define MCConfiguration
static std::string dirName(const std::string &path)
Definition: CDirEntry.cpp:135
static std::string getEnvironmentVariable(const std::string &name)
Definition: COptions.cpp:253
struct MESSAGES Message
std::string toUtf8() const
std::string COptions::getEnvironmentVariable ( const std::string &  name)
static

Definition at line 253 of file COptions.cpp.

References CLocaleString::fromUtf8(), and CLocaleString::toUtf8().

Referenced by getCopasiDir(), getHome(), and getTemp().

254 {
255  CLocaleString Value = getenv(CLocaleString::fromUtf8(name).c_str());
256 
257  return Value.toUtf8();
258 }
std::string toUtf8() const
static CLocaleString fromUtf8(const std::string &utf8)
std::string COptions::getHome ( void  )
staticprivate

Definition at line 365 of file COptions.cpp.

References getEnvironmentVariable().

Referenced by init().

366 {
367  std::string Home;
368 
369  Home = getEnvironmentVariable("HOME");
370 
371 #ifdef WIN32
372 
373  if (Home == "")
374  Home = getEnvironmentVariable("HOMEDRIVE")
375  + getEnvironmentVariable("HOMEPATH");
376 
377 #endif // WIN32
378 
379  if (Home == "")
380  {
381  std::ostringstream error;
382  error << std::endl
383  << " use --home HOME" << std::endl
384  << " or set the environment variable HOME" << std::endl
385  << " to point to your home directory" << std::endl;
386 
387  throw copasi::option_error(error.str());
388  }
389 
390  return Home;
391 }
static std::string getEnvironmentVariable(const std::string &name)
Definition: COptions.cpp:253
const COptions::nonOptionType & COptions::getNonOptions ( )
static

Definition at line 251 of file COptions.cpp.

References mNonOptions.

Referenced by main(), and CopasiUI3Window::openInitialDocument().

251 {return mNonOptions;}
static nonOptionType mNonOptions
Definition: COptions.h:115
std::string COptions::getPWD ( void  )
static

Definition at line 338 of file COptions.cpp.

References CLocaleString::toUtf8().

Referenced by init(), and Copasi2SBMLTranslator::translateToSBML().

339 {
340 #ifdef WIN32
341  int PWDSize = 256;
342 #else // WIN32
343  size_t PWDSize = 256;
344 #endif // not WIN32
345 
346  CLocaleString::lchar * PWD = NULL;
347 
348  while (!(PWD = getcwd(NULL, PWDSize)))
349  {
350  if (errno != ERANGE) break;
351 
352  PWDSize *= 2;
353  }
354 
355  std::string pwd = CLocaleString(PWD).toUtf8();
356 
357  if (PWD)
358  {
359  free(PWD);
360  }
361 
362  return pwd;
363 }
std::string toUtf8() const
std::string COptions::getTemp ( void  )
staticprivate

Definition at line 393 of file COptions.cpp.

References CDirEntry::createDir(), getEnvironmentVariable(), CDirEntry::isDir(), CDirEntry::isWritable(), and CDirEntry::Separator.

Referenced by init().

394 {
395  std::string Temp, User, CreateCopasiDir, CreateUserDir;
396 
397  Temp = getEnvironmentVariable("TEMP");
398 
399  if (Temp == "") Temp = getEnvironmentVariable("TMP");
400 
401  User = getEnvironmentVariable("USER");
402 
403  if (User == "") User = getEnvironmentVariable("USERNAME");
404 
405  if (User == "") User = "CopasiUser";
406 
407  if (Temp == "") // OS specific fallback.
408 #ifdef WIN32
409  Temp = getEnvironmentVariable("windir") + CDirEntry::Separator + "Temp";
410 
411 #else
412  Temp = "/tmp";
413 #endif // WIN32
414 
415  // Assure that Temp exists and is a directory.
416  if (!CDirEntry::isDir(Temp) || !CDirEntry::isWritable(Temp))
417  return "";
418 
419  CreateCopasiDir = Temp + CDirEntry::Separator + "copasi";
420  CreateUserDir = CreateCopasiDir + CDirEntry::Separator + User;
421 
422  //Assure that CreateCopasiDir exists and is a writable directory.
423  if (!CDirEntry::createDir("copasi", Temp))
424  return Temp;
425 
426  Temp = CreateCopasiDir;
427 
428  //Assure that CreateUserDir exists and is a writable directory.
429  if (!CDirEntry::createDir(User, Temp))
430  return Temp;
431 
432  Temp = CreateUserDir;
433  return Temp;
434 }
static bool isDir(const std::string &path)
Definition: CDirEntry.cpp:65
static std::string getEnvironmentVariable(const std::string &name)
Definition: COptions.cpp:253
static bool createDir(const std::string &dir, const std::string &parent="")
Definition: CDirEntry.cpp:180
static const std::string Separator
Definition: CDirEntry.h:34
static bool isWritable(const std::string &path)
Definition: CDirEntry.cpp:95
template<class CType >
static void COptions::getValue ( const std::string &  name,
CType &  value 
)
inlinestatic
void COptions::init ( C_INT  argc = 0,
char *  argv[] = NULL 
)
static

Definition at line 56 of file COptions.cpp.

References C_INT, compareValue(), copasi::options::ConfigDir, copasi::options::ConfigFile, copasi::options::CopasiDir, CDirEntry::dirName(), copasi::options::ExportBerkeleyMadonna, copasi::options::ExportC, copasi::options::ExportSBML, copasi::options::ExportXPPAUT, copasi::COptionParser::get_non_options(), copasi::COptionParser::get_options(), getConfigDir(), getConfigFile(), getCopasiDir(), getHome(), getPWD(), getTemp(), getValue(), copasi::options::Home, copasi::options::ImportSBML, CDirEntry::isRelativePath(), copasi::options::License, CDirEntry::makePathAbsolute(), copasi::options::MaxTime, mNonOptions, copasi::options::NoLogo, copasi::COptionParser::parse(), copasi::options::Save, copasi::options::SBMLSchema, setValue(), copasi::options::Tmp, CLocaleString::toUtf8(), copasi::options::Validate, and copasi::options::Verbose.

Referenced by CCopasiRootContainer::init().

57 {
58  // We convert the commandline arguments to Utf8
59  std::string * Utf8 = new std::string[argc];
60  C_INT i;
61 
62 #ifdef WIN32
63  // We cannot use the commandline arguments provided by main as they are not sufficient
64  // to encode unicode data.
65  CLocaleString::lchar * CommandLine = GetCommandLineW();
66 
67  int intArgC = (int) argc;
68  CLocaleString::lchar ** Utf8V = CommandLineToArgvW(CommandLine, &intArgC);
69 
70  for (i = 0; i < argc; ++i)
71  {
72  Utf8[i] = CLocaleString(Utf8V[i]).toUtf8();
73  }
74 
75  LocalFree(Utf8V);
76 
77 #else
78 
79  for (i = 0; i < argc; ++i)
80  {
81  Utf8[i] = CLocaleString(argv[i]).toUtf8();
82  }
83 
84 #endif
85 
86  if (argc > 0)
87  setValue("Self", Utf8[0]);
88  else
89  setValue("Self", std::string(""));
90 
91  setValue("PWD", getPWD());
92 
93  // First we must clean up the command line by
94  // taking out any SBW commands like -sbwregister and -sbwmodule
95 
96  // The default settings for SBW related options
97  setValue("SBWRegister", false);
98  setValue("SBWModule", false);
99 
100  char **ArgV = new char * [argc];
101  C_INT ArgC = 0;
102 
103  for (i = 0; i < argc; i++)
104  {
105  if (Utf8[i] == "-sbwregister")
106  setValue("SBWRegister", true);
107  else if (Utf8[i] == "-sbwmodule")
108  setValue("SBWModule", true);
109  else
110  {
111  ArgV[ArgC] = strdup(Utf8[i].c_str());
112  ArgC++;
113  }
114  }
115 
116  // Now we are ready to start the Clo++ generated parser.
117  copasi::COptionParser * pPreParser = new copasi::COptionParser;
118  pPreParser->parse(ArgC, ArgV);
119 
120  const copasi::options &PreOptions = pPreParser->get_options();
121 
122  setValue("CopasiDir", PreOptions.CopasiDir);
123 
124  if (compareValue("CopasiDir", (std::string) ""))
125  {
126  setValue("CopasiDir", getCopasiDir());
127  }
128 
129  setValue("Home", PreOptions.Home);
130 
131  if (compareValue("Home", (std::string) ""))
132  setValue("Home", getHome());
133 
134  setValue("Tmp", PreOptions.Tmp);
135 
136  if (compareValue("Tmp", (std::string) ""))
137  setValue("Tmp", getTemp());
138 
139  setValue("ConfigDir", PreOptions.ConfigDir);
140 
141  if (compareValue("ConfigDir", (std::string) ""))
142  setValue("ConfigDir", getConfigDir());
143 
144  setValue("ConfigFile", PreOptions.ConfigFile);
145 
146  if (compareValue("ConfigFile", (std::string) ""))
147  setValue("ConfigFile", getConfigFile());
148 
149  mNonOptions.clear();
150  std::vector< std::string >::const_iterator it = pPreParser->get_non_options().begin();
151  std::vector< std::string >::const_iterator end = pPreParser->get_non_options().end();
152 
153  for (; it != end; ++it)
154  mNonOptions.push_back(*it);
155 
156  const copasi::options &Options = pPreParser->get_options();
157 
158  std::string CopasiDir;
159  getValue("CopasiDir", CopasiDir);
160 
161 #if !defined(DARWIN)
162 
163  if (CopasiDir.empty() && argc > 0)
164  {
165  // in case the directory was not defined on the command line,
166  // or via environment variableinstead of defaulting to *most
167  // likely* non-existing folders, let us default to the most
168  // usual case, that people received the binaries from us, in
169  // that case we know that the binaries are located in
170  // <copasi>/bin while the remaining data is in <copasi>/share
171  // (excluding OSX for the time being as that one ought to have
172  // files bundled up).
173 
174  CopasiDir = argv[0];
175 
176  // if relative, make it an absolute path
177  if (CDirEntry::isRelativePath(CopasiDir))
178  CDirEntry::makePathAbsolute(CopasiDir, getPWD());
179 
180  // strip the executable
181  CopasiDir = CDirEntry::dirName(CopasiDir);
182 
183  // strip the bin directory
184  CopasiDir = CDirEntry::dirName(CopasiDir);
185  }
186 
187 #endif
188 
189  std::string Home;
190  getValue("Home", Home);
191 
192  /* The values for ExampleDir and WizardDir are dependent on CopasiDir
193  and on the OS. */
194 
195 #ifdef Darwin
196  setValue("DefaultConfigDir", CDirEntry::dirName(CopasiDir) + "/config");
197  setValue("ExampleDir", CDirEntry::dirName(CopasiDir) + "/examples");
198  setValue("WizardDir", CopasiDir + "/Contents/Resources/doc/html");
199 #elif WIN32
200  setValue("DefaultConfigDir", CopasiDir + "\\share\\copasi\\config");
201  setValue("ExampleDir", CopasiDir + "\\share\\copasi\\examples");
202  setValue("WizardDir", CopasiDir + "\\share\\copasi\\doc\\html");
203 #else // All Unix flavors have the same installation structure.
204  setValue("DefaultConfigDir", CopasiDir + "/share/copasi/config");
205  setValue("ExampleDir", CopasiDir + "/share/copasi/examples");
206  setValue("WizardDir", CopasiDir + "/share/copasi/doc/html");
207 #endif
208 
209  /* Create manually for each option except for:
210  CopasiDir, ConfigFile, Home, and Default
211  setValue("OptionId", Options.OptionID); */
212 
213  if (Options.Tmp != "") setValue("Tmp", Options.Tmp);
214 
215  setValue("NoLogo", Options.NoLogo);
216  setValue("Validate", Options.Validate);
217  setValue("Verbose", Options.Verbose);
218  setValue("License", Options.License);
219  setValue("Save", Options.Save);
220  setValue("ImportSBML", Options.ImportSBML);
221  setValue("ExportSBML", Options.ExportSBML);
222  setValue("SBMLSchema", Options.SBMLSchema);
223  setValue("ExportC", Options.ExportC);
224  setValue("ExportXPPAUT", Options.ExportXPPAUT);
225  setValue("ExportBerkeleyMadonna", Options.ExportBerkeleyMadonna);
226  setValue("MaxTime", Options.MaxTime);
227 
228 #ifdef COPASI_SEDML
229  setValue("ImportSEDML", Options.ImportSEDML);
230 #endif
231 
232  delete pPreParser;
233 
234  for (i = 0; i < ArgC; i++)
235  {
236  free(ArgV[i]);
237  }
238 
239  delete [] ArgV;
240  delete [] Utf8;
241 }
static void setValue(const std::string &name, const CType &value)
Definition: COptions.h:143
#define C_INT
Definition: copasi.h:115
std::string ExportXPPAUT
Definition: COptionParser.h:81
std::string CopasiDir
Definition: COptionParser.h:77
static bool isRelativePath(const std::string &path)
Definition: CDirEntry.cpp:414
std::string ExportSBML
Definition: COptionParser.h:80
static std::string getCopasiDir(void)
Definition: COptions.cpp:260
static std::string getPWD(void)
Definition: COptions.cpp:338
static std::string getConfigFile(void)
Definition: COptions.cpp:448
static std::string dirName(const std::string &path)
Definition: CDirEntry.cpp:135
std::string Save
Definition: COptionParser.h:88
std::string ConfigDir
Definition: COptionParser.h:75
SBMLSchema_enum SBMLSchema
Definition: COptionParser.h:87
static bool compareValue(const std::string &name, const CType &value)
Definition: COptions.h:128
std::string ConfigFile
Definition: COptionParser.h:76
const std::vector< std::string > & get_non_options(void) const
get a list of nonoptions from the command line
std::string ExportC
Definition: COptionParser.h:79
std::string ExportBerkeleyMadonna
Definition: COptionParser.h:78
static std::string getConfigDir(void)
Definition: COptions.cpp:436
static void getValue(const std::string &name, CType &value)
Definition: COptions.h:124
std::string toUtf8() const
std::string ImportSBML
Definition: COptionParser.h:83
const options & get_options(void) const
get the main options
std::string Tmp
Definition: COptionParser.h:89
static nonOptionType mNonOptions
Definition: COptions.h:115
void parse(int argc, char *argv[], bool call_finalize=true)
parse the command line
static std::string getHome(void)
Definition: COptions.cpp:365
std::string Home
Definition: COptionParser.h:82
static bool makePathAbsolute(std::string &relativePath, const std::string &absoluteTo)
Definition: CDirEntry.cpp:481
static std::string getTemp(void)
Definition: COptions.cpp:393
template<class CType >
static void COptions::setValue ( const std::string &  name,
const CType &  value 
)
inlinestaticprivate

Definition at line 143 of file COptions.h.

References mOptions.

Referenced by init().

145  {
146  if (mOptions[name])
147  mOptions[name]->setValue(value);
148  else
149  mOptions[name] = new COptionValueTemplate< CType >(value);
150 
151  return;
152  }
static optionType mOptions
Definition: COptions.h:114

Member Data Documentation

COptions::nonOptionType COptions::mNonOptions
staticprivate

Definition at line 115 of file COptions.h.

Referenced by getNonOptions(), and init().

COptions::optionType COptions::mOptions
staticprivate

Definition at line 114 of file COptions.h.

Referenced by cleanup(), compareValue(), getValue(), and setValue().


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