COPASI API  4.16.103
Macros | Functions
utility.h File Reference
#include <string>
#include <string.h>
#include <stdarg.h>
Include dependency graph for utility.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define INITIALTEXTSIZE   1024
 

Functions

void FixSName (const std::string &original, std::string &fixed)
 
bool isNumber (const std::string &str)
 
std::string ISODateTime (tm *pTime)
 
std::string LocalTimeStamp ()
 
std::string nameToSbmlId (const std::string &name)
 
std::string pointerToString (const void *pVoid)
 
std::string quote (const std::string &name, const std::string &additionalEscapes="")
 
std::string StringPrint (const char *format,...)
 
bool stringReplace (std::string &str, const std::string &target, const std::string &replacement)
 
voidstringToPointer (const std::string str)
 
double strToDouble (const char *str, char const **pTail=NULL)
 
C_INT32 strToInt (const char *str, char const **pTail=NULL)
 
unsigned C_INT32 strToUnsignedInt (const char *str, char const **pTail=NULL)
 
template<class CType >
CType toEnum (const char *attribute, const char **enumNames, const CType &enumDefault)
 
template<class CType >
CType toEnum (const std::string &attribute, const std::string *enumNames, const CType &enumDefault)
 
std::string unQuote (const std::string &name)
 
std::string UTCTimeStamp ()
 

Macro Definition Documentation

#define INITIALTEXTSIZE   1024

Definition at line 23 of file utility.h.

Function Documentation

void FixSName ( const std::string &  original,
std::string &  fixed 
)

Definition at line 188 of file utility.cpp.

189 {
190  size_t i, len;
191 
192  // check reserved names
193  if (original == "abs") {fixed = "_abs"; return;}
194 
195  if (original == "acos") {fixed = "_acos"; return;}
196 
197  if (original == "and") {fixed = "_and"; return;}
198 
199  if (original == "asin") {fixed = "_asin"; return;}
200 
201  if (original == "atan") {fixed = "_atan"; return;}
202 
203  if (original == "ceil") {fixed = "_ceil"; return;}
204 
205  if (original == "cos") {fixed = "_cos"; return;}
206 
207  if (original == "exp") {fixed = "_exp"; return;}
208 
209  if (original == "floor") {fixed = "_floor"; return;}
210 
211  if (original == "hilli") {fixed = "_hilli"; return;}
212 
213  if (original == "hillmmr") {fixed = "_hillmmr"; return;}
214 
215  if (original == "hillmr") {fixed = "_hillmr"; return;}
216 
217  if (original == "hillr") {fixed = "_hillr"; return;}
218 
219  if (original == "isouur") {fixed = "_isouur"; return;}
220 
221  if (original == "log") {fixed = "_log"; return;}
222 
223  if (original == "log10") {fixed = "_log10"; return;}
224 
225  if (original == "massi") {fixed = "_massi"; return;}
226 
227  if (original == "massr") {fixed = "_massr"; return;}
228 
229  if (original == "not") {fixed = "_not"; return;}
230 
231  if (original == "or") {fixed = "_or"; return;}
232 
233  if (original == "ordbbr") {fixed = "_ordbbr"; return;}
234 
235  if (original == "ordbur") {fixed = "_ordbur"; return;}
236 
237  if (original == "ordubr") {fixed = "_ordubr"; return;}
238 
239  if (original == "pow") {fixed = "_pow"; return;}
240 
241  if (original == "ppbr") {fixed = "_ppbr"; return;}
242 
243  if (original == "sin") {fixed = "_sin"; return;}
244 
245  if (original == "sqr") {fixed = "_sqr"; return;}
246 
247  if (original == "sqrt") {fixed = "_sqrt"; return;}
248 
249  if (original == "substance") {fixed = "_substance"; return;}
250 
251  if (original == "time") {fixed = "_time"; return;}
252 
253  if (original == "tan") {fixed = "_tan"; return;}
254 
255  if (original == "umai") {fixed = "_umai"; return;}
256 
257  if (original == "umar") {fixed = "_umar"; return;}
258 
259  if (original == "uai") {fixed = "_uai"; return;}
260 
261  if (original == "ualii") {fixed = "_ualii"; return;}
262 
263  if (original == "uar") {fixed = "_uar"; return;}
264 
265  if (original == "ucii") {fixed = "_ucii"; return;}
266 
267  if (original == "ucir") {fixed = "_ucir"; return;}
268 
269  if (original == "ucti") {fixed = "_ucti"; return;}
270 
271  if (original == "uctr") {fixed = "_uctr"; return;}
272 
273  if (original == "uhmi") {fixed = "_uhmi"; return;}
274 
275  if (original == "uhmr") {fixed = "_uhmr"; return;}
276 
277  if (original == "umi") {fixed = "_umi"; return;}
278 
279  if (original == "unii") {fixed = "_unii"; return;}
280 
281  if (original == "unir") {fixed = "_unir"; return;}
282 
283  if (original == "uuhr") {fixed = "_uuhr"; return;}
284 
285  if (original == "umr") {fixed = "_umr"; return;}
286 
287  if (original == "usii") {fixed = "_usii"; return;}
288 
289  if (original == "usir") {fixed = "_usir"; return;}
290 
291  if (original == "uuci") {fixed = "_uuci"; return;}
292 
293  if (original == "uucr") {fixed = "_uucr"; return;}
294 
295  if (original == "uui") {fixed = "_uui"; return;}
296 
297  if (original == "uur") {fixed = "_uur"; return;}
298 
299  if (original == "volume") {fixed = "_volume"; return;}
300 
301  if (original == "xor") {fixed = "_xor"; return;}
302 
303  len = original.length();
304 
305  // check rule for initial characters
306  // if first not a letter...
307  if (((original[0] < 'A') || (original[0] > 'z')) && (original[0] != '_'))
308  {
309  if ((original[0] >= '0') && (original[0] <= '9'))
310  fixed = "_" + original;
311  else
312  {fixed = original; fixed [0] = '_';}
313  }
314  else
315  fixed = original;
316 
317  len = fixed.length();
318 
319  for (i = 1; i < len; i++)
320  if ((fixed [i] != '_') && ((fixed [i] < 'A') || (fixed [i] > 'z')) &&
321  ((fixed [i] < '0') || (fixed [i] > '9')))
322  fixed [i] = '_';
323 }
bool isNumber ( const std::string &  str)

Check whether the given string represents a number

Parameters
conststd::string & str
Returns
bool isNumber

Definition at line 75 of file utility.cpp.

References strToDouble().

Referenced by COptItem::compileLowerBound(), COptItem::compileUpperBound(), ConverterASTNode::ConverterASTNode(), CMetabNameInterface::getDisplayName(), CQFittingItemWidget::loadSelection(), CQFittingItemWidget::saveSelection(), COptItem::setLowerBound(), CQFittingItemWidget::setTableText(), COptItem::setUpperBound(), CQFittingItemWidget::slotCheckLowerInf(), CQFittingItemWidget::slotCheckUpperInf(), CQFittingItemWidget::slotLowerLostFocus(), CQFittingItemWidget::slotStartLostFocus(), CQFittingItemWidget::slotUpperLostFocus(), and CChemEqInterface::writeElement().

76 {
77  if (str.find_first_of("+-.0123456789")) return false;
78 
79  const char * Tail;
80  strToDouble(str.c_str(), & Tail);
81 
82  if (*Tail) return false;
83 
84  return true;
85 }
double strToDouble(const char *str, char const **pTail)
Definition: utility.cpp:325
std::string ISODateTime ( tm *  pTime)

Returns the time pointed to by pTime in ISO format

Definition at line 31 of file utility.cpp.

Referenced by LocalTimeStamp(), and UTCTimeStamp().

32 {
33  char str[20];
34 
35  if (pTime)
36  {
37  sprintf(str, "%d-%.02d-%.02d %.02d:%.02d:%.02d",
38  pTime->tm_year + 1900,
39  pTime->tm_mon + 1,
40  pTime->tm_mday,
41  pTime->tm_hour,
42  pTime->tm_min,
43  pTime->tm_sec);
44  }
45  else
46  {
47  sprintf(str, "0000-00-00 00:00:00");
48  }
49 
50  return (std::string) str;
51 }
std::string LocalTimeStamp ( )

Returns the current local time in ISO format

Definition at line 53 of file utility.cpp.

References ISODateTime().

Referenced by CCopasiMessage::handler().

54 {
55  time_t Time;
56  time(&Time);
57 
58  tm *sTime = NULL;
59  sTime = localtime(&Time);
60 
61  return ISODateTime(sTime);
62 }
std::string ISODateTime(tm *pTime)
Definition: utility.cpp:31
std::string nameToSbmlId ( const std::string &  name)

Convert a utf8 encoded name to a XmlId

Parameters
conststd::string & name
Returns
std::string SbmlId

Definition at line 437 of file utility.cpp.

Referenced by CSBMLExporter::createUniqueId().

438 {
439  // We convert all non allowed characters to '_'
440  // letter ::= ’a’..’z’,’A’..’Z’
441  // digit ::= ’0’..’9’
442  // idChar ::= letter | digit | ’_’
443  // SId ::= (letter | ’_’ ) idChar*
444 
445  std::ostringstream IdStream;
446 
447  std::string::const_iterator it = name.begin();
448  std::string::const_iterator end = name.end();
449 
450  if ('0' <= *it && *it <= '9')
451  {
452  IdStream << '_';
453  }
454 
455  for (; it != end; ++it)
456  {
457  // second, third or forth character of a multi-byte encoding
458  if (0x80 == (*it & 0xc0))
459  {
460  continue;
461  }
462 
463  if (('0' <= *it && *it <= '9') ||
464  ('a' <= *it && *it <= 'z') ||
465  ('A' <= *it && *it <= 'Z'))
466  {
467  IdStream << *it;
468  }
469  else
470  {
471  IdStream << '_';
472  }
473  }
474 
475  std::string Id = IdStream.str();
476 
477  if (Id[Id.length() - 1] != '_')
478  {
479  return Id;
480  }
481 
482  return Id.substr(0, Id.length() - 1);
483 }
char name[1]
Definition: stdsoap2.h:1342
std::string pointerToString ( const void pVoid)

Convert a void pointer to a string

Parameters
constvoid * pVoid
Returns
std::string str

Definition at line 423 of file utility.cpp.

References fatalError.

Referenced by CEvaluationNodeObject::CEvaluationNodeObject(), and CEvaluationNodeObject::setObjectValuePtr().

424 {
425  char String[19];
426 
427  int Printed = sprintf(String, "%p", pVoid);
428 
429  if (Printed < 0 || 18 < Printed)
430  {
431  fatalError();
432  }
433 
434  return String;
435 }
#define fatalError()
std::string quote ( const std::string &  name,
const std::string &  additionalEscapes = "" 
)

Surround a name with quotes if the name contains a space or double quote else the name is returned unmodified. In the quoted name " and \ are escaped. The parameter additionalEscapes forces quotes around names containing any of the character in the parameter.

Parameters
conststd::string & name
conststd::string & additionalEscapes (default: "")
Returns
std::string quote

Definition at line 144 of file utility.cpp.

References toBeEscaped.

Referenced by CChemEqInterface::addModifier(), CEvaluationNodeCall::CEvaluationNodeCall(), CEvaluationNodeCall::getBerkeleyMadonnaString(), CEvaluationNodeCall::getCCodeString(), CEvaluationNodeCall::getData(), CMetabNameInterface::getDisplayName(), CEvaluationNodeCall::getDisplayString(), CEvaluationNodeCall::getInfix(), CEvaluationNodeCall::getMMLString(), CEvaluationNodeCall::getXPPString(), CEvaluationNodeCall::setData(), and CMetabNameInterface::splitDisplayName().

146 {
147  if (name.find_first_of(" \"" + additionalEscapes) == std::string::npos)
148  return name;
149 
150 #define toBeEscaped "\\\""
151  std::string Escaped(name);
152  std::string::size_type pos = Escaped.find_first_of(toBeEscaped);
153 
154  while (pos != std::string::npos)
155  {
156  Escaped.insert(pos, "\\");
157  pos += 2;
158  pos = Escaped.find_first_of(toBeEscaped, pos);
159  }
160 
161  return "\"" + Escaped + "\"";
162 #undef toBeEscaped
163 }
char name[1]
Definition: stdsoap2.h:1342
#define toBeEscaped
std::string StringPrint ( const char *  format,
  ... 
)

sprintf like function for strings

Parameters
const char *format sprintf like format string
...
Returns
"string"

Definition at line 87 of file utility.cpp.

References C_INT32, and INITIALTEXTSIZE.

Referenced by CExperimentSet::addExperiment(), CExperiment::elevateChildren(), CExperimentObjectMap::getDefaultScale(), CExperimentObjectMap::getName(), CExperimentObjectMap::getObjectCN(), CExperimentObjectMap::getRole(), CExperimentObjectMap::getScale(), CCopasiTimeVariable::LL2String(), CQExperimentData::load(), CReaction::loadOld(), CReaction::loadOneRole(), CExperimentObjectMap::setNumCols(), CExperimentObjectMap::setObjectCN(), CExperimentObjectMap::setRole(), CExperimentObjectMap::setScale(), CVersion::setString(), CQExperimentData::slotFileAdd(), and CCopasiXMLParser::ParameterDescriptionElement::start().

88 {
89  C_INT32 TextSize = INITIALTEXTSIZE;
90  C_INT32 Printed = 0;
91 
92  char *Text = NULL;
93 
94  Text = new char[TextSize + 1];
95 
96  va_list Arguments; // = NULL;
97  va_start(Arguments, format);
98  Printed = vsnprintf(Text, TextSize + 1, format, Arguments);
99  va_end(Arguments);
100 
101  while (Printed < 0 || TextSize < Printed)
102  {
103  delete [] Text;
104 
105  (Printed < 0) ? TextSize *= 2 : TextSize = Printed;
106  Text = new char[TextSize + 1];
107 
108  va_list Arguments; // = NULL;
109  va_start(Arguments, format);
110  Printed = vsnprintf(Text, TextSize + 1, format, Arguments);
111  va_end(Arguments);
112  }
113 
114  std::string Result = Text;
115 
116  delete [] Text;
117  return Result;
118 }
#define C_INT32
Definition: copasi.h:90
#define INITIALTEXTSIZE
bool stringReplace ( std::string &  str,
const std::string &  target,
const std::string &  replacement 
)

Replaces the all occurrences of target string in str with the replacement and returns true if the target string was replaced

Parameters
std::string& str
conststd::string & target
conststd::string & replacement
Returns
bool replaced

Definition at line 165 of file utility.cpp.

Referenced by CModel::convert2NonReversible().

166 {
167  bool replaced = false;
168 
169  std::string::size_type pos = str.find(target, 0);
170 
171  while (pos != std::string::npos)
172  {
173  replaced = true;
174 
175  str.replace(pos, target.length(), replacement, 0, std::string::npos);
176  pos = str.find(target, 0);
177  }
178 
179  return replaced;
180 }
void* stringToPointer ( const std::string  str)

Convert a character sequence to a void pointer

Parameters
conststd::string str
Returns
void * pVoid

Definition at line 414 of file utility.cpp.

Referenced by CEvaluationNodeObject::compile(), CMathExpression::compile(), and CMathExpression::copy().

415 {
416  void * pPointer;
417 
418  sscanf(str.c_str(), "%p", &pPointer);
419 
420  return pPointer;
421 }
double strToDouble ( const char *  str,
char const **  pTail = NULL 
)

Convert a character sequence to a double

Parameters
constchar * str
charconst ** pTail (default: NULL)
Returns
double

Definition at line 325 of file utility.cpp.

Referenced by CEvaluationNodeNumber::CEvaluationNodeNumber(), COptItem::compileLowerBound(), COptItem::compileUpperBound(), CReadConfig::getVariable(), isNumber(), CMiaseParser::newSimulation(), operator>>(), COptItem::setLowerBound(), COptItem::setUpperBound(), CCopasiXMLParser::RectangleElement::start(), CCopasiXMLParser::EllipseElement::start(), CCopasiXMLParser::TextElement::start(), CCopasiXMLParser::RenderCurveElement::start(), CCopasiXMLParser::PolygonElement::start(), CCopasiXMLParser::GroupElement::start(), test000087::test_simulate_reaction_flux_reference_1(), CEvaluationNodeNumber::toAST(), and CChemEqParserBase::yyparse().

327 {
328  double Value = std::numeric_limits<C_FLOAT64>::quiet_NaN();
329 
330  if (pTail != NULL)
331  {
332  *pTail = str;
333  }
334 
335  if (str == NULL || *str == 0x0)
336  {
337  return Value;
338  }
339 
340  std::istringstream in;
341 
342  in.imbue(std::locale::classic());
343  in.str(str);
344 
345  in >> Value;
346 
347  if (pTail != NULL && !isnan(Value))
348  {
349  *pTail = str + std::min< size_t >(in.tellg(), strlen(str));
350  }
351 
352  return Value;
353 }
C_INT32 strToInt ( const char *  str,
char const **  pTail = NULL 
)

Convert a character sequence to a double

Parameters
constchar * str
charconst ** pTail (default: NULL)
Returns
C_INT32

Definition at line 354 of file utility.cpp.

References C_INT32.

Referenced by CCopasiXMLParser::ParameterElement::start(), and CCopasiXMLParser::COPASIElement::start().

356 {
357  C_INT32 Value = std::numeric_limits< C_INT32 >::quiet_NaN();
358 
359  if (pTail != NULL)
360  {
361  *pTail = str;
362  }
363 
364  if (str == NULL || *str == 0x0)
365  {
366  return Value;
367  }
368 
369  std::istringstream in;
370 
371  in.imbue(std::locale::classic());
372  in.str(str);
373 
374  in >> Value;
375 
376  if (pTail != NULL && !isnan(Value))
377  {
378  *pTail = str + std::min< size_t >(in.tellg(), strlen(str));
379  }
380 
381  return Value;
382 }
#define C_INT32
Definition: copasi.h:90
unsigned C_INT32 strToUnsignedInt ( const char *  str,
char const **  pTail = NULL 
)

Convert a character sequence to a double

Parameters
constchar * str
charconst ** pTail (default: NULL)
Returns
unsigned C_INT32

Definition at line 384 of file utility.cpp.

References C_INT32.

Referenced by CCopasiParameterGroup::getObject(), CCopasiXMLParser::CompartmentElement::start(), CCopasiXMLParser::ParameterElement::start(), CCopasiXMLParser::ReportElement::start(), CCopasiXMLParser::ParameterDescriptionElement::start(), and CCopasiXMLParser::SliderElement::start().

386 {
387  unsigned C_INT32 Value = std::numeric_limits< unsigned C_INT32 >::quiet_NaN();
388 
389  if (pTail != NULL)
390  {
391  *pTail = str;
392  }
393 
394  if (str == NULL || *str == 0x0)
395  {
396  return Value;
397  }
398 
399  std::istringstream in;
400 
401  in.imbue(std::locale::classic());
402  in.str(str);
403 
404  in >> Value;
405 
406  if (pTail != NULL && !isnan(Value))
407  {
408  *pTail = str + std::min< size_t >(in.tellg(), strlen(str));
409  }
410 
411  return Value;
412 }
#define C_INT32
Definition: copasi.h:90
template<class CType >
CType toEnum ( const char *  attribute,
const char **  enumNames,
const CType &  enumDefault 
)
template<class CType >
CType toEnum ( const std::string &  attribute,
const std::string *  enumNames,
const CType &  enumDefault 
)

Convert an attribute to enum. If attribute is "" or no matching name is found the parameter enumDefault is returned. Note: enumNames must be zero terminated.

Parameters
conststd::string & attribute
conststd::string * enumNames
constCType & enumDefault
Returns
CType enum

Definition at line 128 of file utility.h.

131 {
132  if (attribute == "") return enumDefault;
133 
134  for (int i = 0; *enumNames != ""; i++, enumNames++)
135  if (attribute == *enumNames) return static_cast< CType >(i);
136 
137  return enumDefault;
138 }
std::string unQuote ( const std::string &  name)

Unesacape a quoted name. If the name is surrounded by quotes these are removed and included escape sequences '.' are reduced to '.' else the name is returned unmodified.

Parameters
conststd::string & name
Returns
std::string unQuote

Definition at line 120 of file utility.cpp.

Referenced by CEvaluationNodeCall::CEvaluationNodeCall(), CModel::findMetabByName(), CEvaluationNodeCall::getData(), CCopasiVectorN< CReportDefinition >::getIndex(), CEvaluationNodeCall::setData(), and CChemEqParser::yylex().

121 {
122  std::string Name = name;
123  std::string::size_type len = Name.length();
124 
125  if (len > 1 && Name[0] == '"' && Name[len - 1] == '"')
126  {
127  // Remove surrounding double quotes.
128  Name = Name.substr(1, len - 2);
129 
130  // Remove escape sequences.
131  std::string::size_type pos = Name.find("\\");
132 
133  while (pos != std::string::npos)
134  {
135  Name.erase(pos, 1);
136  pos++;
137  pos = Name.find("\\", pos);
138  }
139  }
140 
141  return Name;
142 }
char name[1]
Definition: stdsoap2.h:1342
std::string UTCTimeStamp ( )

Returns the current time in ISO format

Definition at line 64 of file utility.cpp.

References ISODateTime().

Referenced by createParameterSetsForExperiment(), CCopasiXML::save(), and CConfigurationFile::CXML::save().

65 {
66  time_t Time;
67  time(&Time);
68 
69  tm *sTime = NULL;
70  sTime = gmtime(&Time);
71 
72  return ISODateTime(sTime);
73 }
std::string ISODateTime(tm *pTime)
Definition: utility.cpp:31