COPASI API  4.16.103
utility.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010 - 2014 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) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #include <algorithm>
16 #include <cmath>
17 #include <limits>
18 #include <string>
19 #include <sstream>
20 #include <iostream>
21 
22 #include <stdlib.h>
23 #include <string.h>
24 #include <time.h>
25 
26 #include "copasi.h"
27 
28 #include "utility.h"
29 #include "CCopasiMessage.h"
30 
31 std::string ISODateTime(tm * pTime)
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 }
52 
53 std::string LocalTimeStamp()
54 {
55  time_t Time;
56  time(&Time);
57 
58  tm *sTime = NULL;
59  sTime = localtime(&Time);
60 
61  return ISODateTime(sTime);
62 }
63 
64 std::string UTCTimeStamp()
65 {
66  time_t Time;
67  time(&Time);
68 
69  tm *sTime = NULL;
70  sTime = gmtime(&Time);
71 
72  return ISODateTime(sTime);
73 }
74 
75 bool isNumber(const std::string & str)
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 }
86 
87 std::string StringPrint(const char * format, ...)
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 }
119 
120 std::string unQuote(const std::string & name)
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 }
143 
144 std::string quote(const std::string & name,
145  const std::string & additionalEscapes)
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 }
164 
165 bool stringReplace(std::string & str, const std::string & target, const std::string & replacement)
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 }
181 
182 /*
183  * Fixes a string to be a SName element from SBML
184  * (this is a destructive function, some changes are irreversible)
185  *
186  */
187 
188 void FixSName(const std::string &original, std::string &fixed)
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 }
324 
325 double strToDouble(const char * str,
326  char const ** pTail)
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 }
354 C_INT32 strToInt(const char * str,
355  char const ** pTail)
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 }
383 
384 unsigned C_INT32 strToUnsignedInt(const char * str,
385  char const ** pTail)
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 }
413 
414 void * stringToPointer(const std::string str)
415 {
416  void * pPointer;
417 
418  sscanf(str.c_str(), "%p", &pPointer);
419 
420  return pPointer;
421 }
422 
423 std::string pointerToString(const void * pVoid)
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 }
436 
437 std::string nameToSbmlId(const std::string & name)
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 }
std::string unQuote(const std::string &name)
Definition: utility.cpp:120
bool stringReplace(std::string &str, const std::string &target, const std::string &replacement)
Definition: utility.cpp:165
std::string LocalTimeStamp()
Definition: utility.cpp:53
bool isNumber(const std::string &str)
Definition: utility.cpp:75
std::string nameToSbmlId(const std::string &name)
Definition: utility.cpp:437
#define fatalError()
std::string ISODateTime(tm *pTime)
Definition: utility.cpp:31
std::string pointerToString(const void *pVoid)
Definition: utility.cpp:423
void FixSName(const std::string &original, std::string &fixed)
Definition: utility.cpp:188
#define C_INT32
Definition: copasi.h:90
#define INITIALTEXTSIZE
unsigned C_INT32 strToUnsignedInt(const char *str, char const **pTail)
Definition: utility.cpp:384
std::string quote(const std::string &name, const std::string &additionalEscapes)
Definition: utility.cpp:144
double strToDouble(const char *str, char const **pTail)
Definition: utility.cpp:325
void * stringToPointer(const std::string str)
Definition: utility.cpp:414
std::string StringPrint(const char *format,...)
Definition: utility.cpp:87
std::string UTCTimeStamp()
Definition: utility.cpp:64
#define toBeEscaped
C_INT32 strToInt(const char *str, char const **pTail)
Definition: utility.cpp:354