COPASI API  4.16.103
COptions.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2013 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) 2002 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <string.h>
16 #include <stdlib.h>
17 
18 #include "copasi.h"
19 
20 #ifdef WIN32
21 # include <windows.h>
22 # include <winbase.h>
23 # include <direct.h>
24 # ifdef ERROR
25 # undef ERROR
26 # endif
27 # define getenv _wgetenv
28 # define getcwd _wgetcwd
29 #else
30 # include <unistd.h>
31 #endif
32 
33 #ifdef Darwin
34 # include <Carbon/Carbon.h>
35 #endif
36 
37 #include <sstream>
38 #include <errno.h>
39 
40 #include "COptionParser.h"
41 #include "COptions.h"
42 
44 #include "utilities/CDirEntry.h"
45 #include "utilities/utility.h"
46 
49 
52 
55 
56 void COptions::init(C_INT argc, char *argv[])
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 }
242 
244 {
245  optionType::iterator begin = mOptions.begin();
246  optionType::iterator end = mOptions.end();
247 
248  for (; begin != end; begin++) pdelete(begin->second);
249 }
250 
252 
253 std::string COptions::getEnvironmentVariable(const std::string & name)
254 {
255  CLocaleString Value = getenv(CLocaleString::fromUtf8(name).c_str());
256 
257  return Value.toUtf8();
258 }
259 
260 std::string COptions::getCopasiDir(void)
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 }
337 
338 std::string COptions::getPWD(void)
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 }
364 
365 std::string COptions::getHome(void)
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 }
392 
393 std::string COptions::getTemp(void)
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 }
435 
436 std::string COptions::getConfigDir(void)
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 }
447 
448 std::string COptions::getConfigFile(void)
449 {
450  std::string ConfigDir;
451 
452  getValue("ConfigDir", ConfigDir);
453 
454  return ConfigDir + CDirEntry::Separator + "copasi";
455 }
static void setValue(const std::string &name, const CType &value)
Definition: COptions.h:143
#define C_INT
Definition: copasi.h:115
#define pdelete(p)
Definition: copasi.h:215
static bool isDir(const std::string &path)
Definition: CDirEntry.cpp:65
std::string ExportXPPAUT
Definition: COptionParser.h:81
#define MCConfiguration
std::string CopasiDir
Definition: COptionParser.h:77
static bool isRelativePath(const std::string &path)
Definition: CDirEntry.cpp:414
COptions()
Definition: COptions.cpp:50
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
static void cleanup()
Definition: COptions.cpp:243
static const nonOptionType & getNonOptions()
Definition: COptions.cpp:251
SBMLSchema_enum SBMLSchema
Definition: COptionParser.h:87
#define DESTRUCTOR_TRACE
Definition: copasi.h:206
static std::string getEnvironmentVariable(const std::string &name)
Definition: COptions.cpp:253
static bool compareValue(const std::string &name, const CType &value)
Definition: COptions.h:128
static bool createDir(const std::string &dir, const std::string &parent="")
Definition: CDirEntry.cpp:180
static optionType mOptions
Definition: COptions.h:114
static const std::string Separator
Definition: CDirEntry.h:34
struct MESSAGES Message
std::string ConfigFile
Definition: COptionParser.h:76
static bool isWritable(const std::string &path)
Definition: CDirEntry.cpp:95
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::map< std::string, COptionValue * > optionType
Definition: COptions.h:108
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
std::vector< std::string > nonOptionType
Definition: COptions.h:109
static CLocaleString fromUtf8(const std::string &utf8)
static bool makePathAbsolute(std::string &relativePath, const std::string &absoluteTo)
Definition: CDirEntry.cpp:481
~COptions()
Definition: COptions.cpp:53
static std::string getTemp(void)
Definition: COptions.cpp:393
static void init(C_INT argc=0, char *argv[]=NULL)
Definition: COptions.cpp:56
#define CONSTRUCTOR_TRACE
Definition: copasi.h:202