COPASI API  4.16.103
utility.h
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) 2001 - 2007 by Pedro Mendes, Virginia Tech Intellectual
12 // Properties, Inc. and EML Research, gGmbH.
13 // All rights reserved.
14 
15 #ifndef COPASI_utilities
16 #define COPASI_utilities
17 
18 #include <string>
19 #include <string.h>
20 // #include <stdio.h>
21 #include <stdarg.h>
22 
23 #define INITIALTEXTSIZE 1024
24 
25 struct tm;
26 
27 /**
28  * Returns the time pointed to by pTime in ISO format
29  */
30 std::string ISODateTime(tm * pTime);
31 
32 /**
33  * Returns the current local time in ISO format
34  */
35 std::string LocalTimeStamp();
36 
37 /**
38  * Returns the current time in ISO format
39  */
40 std::string UTCTimeStamp();
41 
42 /**
43  * Check whether the given string represents a number
44  * @param const std::string & str
45  * @return bool isNumber
46  */
47 bool isNumber(const std::string & str);
48 
49 /**
50  * sprintf like function for strings
51  * @param "const char *" format sprintf like format string
52  * @param ...
53  * @return "string"
54  */
55 std::string StringPrint(const char * format, ...);
56 
57 /**
58  * Unesacape a quoted name. If the name is surrounded by quotes
59  * these are removed and included escape sequences '\.' are reduced
60  * to '.' else the name is returned unmodified.
61  * @param const std::string & name
62  * @return std::string unQuote
63  */
64 std::string unQuote(const std::string & name);
65 
66 /**
67  * Surround a name with quotes if the name contains a space or
68  * double quote else the name is returned unmodified. In the
69  * quoted name " and \ are escaped. The parameter additionalEscapes
70  * forces quotes around names containing any of the character in the
71  * parameter.
72  * @param const std::string & name
73  * @param const std::string & additionalEscapes (default: "")
74  * @return std::string quote
75  */
76 std::string quote(const std::string & name,
77  const std::string & additionalEscapes = "");
78 /**
79  * Replaces the all occurrences of target string in str with the replacement
80  * and returns true if the target string was replaced
81  * @param std::string & str
82  * @param const std::string & target
83  * @param const std::string & replacement
84  * @return bool replaced
85  */
86 bool stringReplace(std::string & str,
87  const std::string & target,
88  const std::string & replacement);
89 
90 /*
91  * Fixes a string to be a SName element from SBML
92  * (this is a destructive function, some changes are irreversible)
93  *
94  */
95 void FixSName(const std::string &original, std::string &fixed)
96 ;
97 
98 /**
99  * Convert an attribute to enum. If attribute is NULL
100  * or no matching name is found the parameter enumDefault is returned.
101  * Note: enumNames must be zero terminated.
102  * @param const char * attribute
103  * @param const char ** enumNames
104  * @param const CType & enumDefault
105  * @return CType enum
106  */
107 template <class CType> CType toEnum(const char * attribute,
108  const char ** enumNames,
109  const CType & enumDefault)
110 {
111  if (!attribute) return enumDefault;
112 
113  for (int i = 0; *enumNames; i++, enumNames++)
114  if (!strcmp(attribute, *enumNames)) return static_cast< CType >(i);
115 
116  return enumDefault;
117 }
118 
119 /**
120  * Convert an attribute to enum. If attribute is ""
121  * or no matching name is found the parameter enumDefault is returned.
122  * Note: enumNames must be zero terminated.
123  * @param const std::string & attribute
124  * @param const std::string * enumNames
125  * @param const CType & enumDefault
126  * @return CType enum
127  */
128 template <class CType> CType toEnum(const std::string & attribute,
129  const std::string * enumNames,
130  const CType & enumDefault)
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 }
139 
140 /**
141  * Convert a character sequence to a double
142  * @param const char * str
143  * @param char const ** pTail (default: NULL)
144  * @return double
145  */
146 double strToDouble(const char * str,
147  char const ** pTail = NULL);
148 
149 /**
150  * Convert a character sequence to a double
151  * @param const char * str
152  * @param char const ** pTail (default: NULL)
153  * @return C_INT32
154  */
155 C_INT32 strToInt(const char * str,
156  char const ** pTail = NULL);
157 
158 /**
159  * Convert a character sequence to a double
160  * @param const char * str
161  * @param char const ** pTail (default: NULL)
162  * @return unsigned C_INT32
163  */
164 unsigned C_INT32 strToUnsignedInt(const char * str,
165  char const ** pTail = NULL);
166 
167 /**
168  * Convert a character sequence to a void pointer
169  * @param const std::string str
170  * @return void * pVoid
171  */
172 void * stringToPointer(const std::string str);
173 
174 /**
175  * Convert a void pointer to a string
176  * @param const void * pVoid
177  * @return std::string str
178  */
179 std::string pointerToString(const void * pVoid);
180 
181 /**
182  * Convert a utf8 encoded name to a XmlId
183  * @param const std::string & name
184  * @return std::string SbmlId
185  */
186 std::string nameToSbmlId(const std::string & name);
187 #endif // COPASI_utilities
std::string ISODateTime(tm *pTime)
Definition: utility.cpp:31
std::string StringPrint(const char *format,...)
Definition: utility.cpp:87
double strToDouble(const char *str, char const **pTail=NULL)
Definition: utility.cpp:325
std::string LocalTimeStamp()
Definition: utility.cpp:53
#define C_INT32
Definition: copasi.h:90
std::string unQuote(const std::string &name)
Definition: utility.cpp:120
std::string pointerToString(const void *pVoid)
Definition: utility.cpp:423
void FixSName(const std::string &original, std::string &fixed)
Definition: utility.cpp:188
std::string nameToSbmlId(const std::string &name)
Definition: utility.cpp:437
bool stringReplace(std::string &str, const std::string &target, const std::string &replacement)
Definition: utility.cpp:165
std::string quote(const std::string &name, const std::string &additionalEscapes="")
Definition: utility.cpp:144
unsigned C_INT32 strToUnsignedInt(const char *str, char const **pTail=NULL)
Definition: utility.cpp:384
C_INT32 strToInt(const char *str, char const **pTail=NULL)
Definition: utility.cpp:354
CType toEnum(const char *attribute, const char **enumNames, const CType &enumDefault)
Definition: utility.h:107
std::string UTCTimeStamp()
Definition: utility.cpp:64
bool isNumber(const std::string &str)
Definition: utility.cpp:75
void * stringToPointer(const std::string str)
Definition: utility.cpp:414