PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ExternalCode/sbml/util/util.h

https://github.com/liaochen/igame
C Header | 241 lines | 69 code | 42 blank | 130 comment | 0 complexity | 0897482cfa2b7c4887158938de1c2743 MD5 | raw file
  1. /**
  2. * @file util.h
  3. * @brief Utility functions
  4. * @author Ben Bornstein
  5. *
  6. * $Id: util.h 9686 2009-06-30 17:37:35Z ajouraku $
  7. * $HeadURL: https://sbml.svn.sourceforge.net/svnroot/sbml/branches/rel-4-0-0/src/util/util.h $
  8. *
  9. *<!---------------------------------------------------------------------------
  10. * This file is part of libSBML. Please visit http://sbml.org for more
  11. * information about SBML, and the latest version of libSBML.
  12. *
  13. * Copyright 2005-2009 California Institute of Technology.
  14. * Copyright 2002-2005 California Institute of Technology and
  15. * Japan Science and Technology Corporation.
  16. *
  17. * This library is free software; you can redistribute it and/or modify it
  18. * under the terms of the GNU Lesser General Public License as published by
  19. * the Free Software Foundation. A copy of the license agreement is provided
  20. * in the file named "LICENSE.txt" included with this software distribution and
  21. * also available online as http://sbml.org/software/libsbml/license.html
  22. *----------------------------------------------------------------------- -->*/
  23. #ifndef util_h
  24. #define util_h
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #include <sbml/common/extern.h>
  28. /** @cond doxygen-libsbml-internal */
  29. LIBSBML_CPP_NAMESPACE_BEGIN
  30. BEGIN_C_DECLS
  31. /**
  32. * Easier-to-read and NULL-friendly string comparison.
  33. */
  34. LIBSBML_EXTERN
  35. unsigned int
  36. streq (const char *s, const char *t);
  37. /**
  38. * Identical to snprintf except printing always occurs according to the
  39. * "C" locale. This function does not affect the locale of the calling
  40. * program.
  41. */
  42. int
  43. c_locale_snprintf (char *str, size_t size, const char *format, ...);
  44. /**
  45. * Identical to vsnprintf except printing always occurs according to the
  46. * "C" locale. This function does not affect the locale of the calling
  47. * program.
  48. */
  49. int
  50. c_locale_vsnprintf (char *str, size_t size, const char *format, va_list ap);
  51. /**
  52. * Identical to strtod except conversion always occurs according to the
  53. * "C" locale. This function does not affect the locale of the calling
  54. * program.
  55. */
  56. double
  57. c_locale_strtod (const char *nptr, char **endptr);
  58. /**
  59. * Attempts to open filename for the given access mode and return a pointer
  60. * to it. If the filename could not be opened, prints an error message and
  61. * exits.
  62. */
  63. LIBSBML_EXTERN
  64. FILE *
  65. safe_fopen (const char *filename, const char *mode);
  66. /**
  67. * Returns a pointer to a new string which is the concatenation of the
  68. * strings str1 and str2. Memory for the new string is obtained with
  69. * safe_malloc() and can be freed with safe_free().
  70. *
  71. * NOTE: This strcat behaves differently than standard library strcat().
  72. */
  73. LIBSBML_EXTERN
  74. char *
  75. safe_strcat (const char *str1, const char *str2);
  76. /**
  77. * @return a pointer to a new string which is a duplicate of the string s.
  78. * Memory for the string is obtained with safe_malloc() and can be freed
  79. * with safe_free().
  80. */
  81. LIBSBML_EXTERN
  82. char *
  83. safe_strdup (const char* s);
  84. /**
  85. * Compares two strings s1 and s2, ignoring the case of the characters.
  86. *
  87. * @return an integer less than, equal to, or greater than zero if s1 is
  88. * found, respectively, to be less than, to match, or be greater than s2.
  89. */
  90. LIBSBML_EXTERN
  91. int
  92. strcmp_insensitive (const char *s1, const char *s2);
  93. /**
  94. * Peforms a binary search on the string table strings to find string s.
  95. *
  96. * All strings from strings[lo] to strings[hi] are searched. The string
  97. * comparison function used is strcmp_insensitive(). Since the search is
  98. * binary, the strings table must be sorted, irrespecitve of case.
  99. *
  100. * @return the index of s in strings, if s was found, or stop + 1
  101. * otherwise.
  102. */
  103. LIBSBML_EXTERN
  104. int
  105. util_bsearchStringsI (const char **strings, const char *s, int lo, int hi);
  106. /**
  107. * @returns true (non-zero) if filename exists, false (zero) otherwise.
  108. */
  109. LIBSBML_EXTERN
  110. int
  111. util_file_exists (const char *filename);
  112. /**
  113. * Removes leading and trailing whitespace from the string s.
  114. *
  115. * @return a pointer to a new string which is a duplicate of the string s,
  116. * with leading and trailing whitespace removed or NULL is s is NULL.
  117. *
  118. * Whitespace is determined by isspace().
  119. */
  120. LIBSBML_EXTERN
  121. char *
  122. util_trim (const char *s);
  123. /**
  124. * Removes leading and trailing whitespace from the string s.
  125. *
  126. * @return a pointer to the first non-whitespace character of the string s
  127. * (which may be the terminating NULL), or NULL if s is NULL. The first
  128. * trailing whitespace character will be replaced with NULL.
  129. *
  130. * Whitespace is determined by isspace().
  131. */
  132. LIBSBML_EXTERN
  133. char *
  134. util_trim_in_place (char *s);
  135. /** @endcond doxygen-libsbml-internal */
  136. /**
  137. * @return a (quiet) NaN.
  138. */
  139. LIBSBML_EXTERN
  140. double
  141. util_NaN (void);
  142. /**
  143. * @return IEEE-754 Negative Infinity.
  144. */
  145. LIBSBML_EXTERN
  146. double
  147. util_NegInf (void);
  148. /**
  149. * @return IEEE-754 Positive Infinity
  150. */
  151. LIBSBML_EXTERN
  152. double
  153. util_PosInf (void);
  154. /**
  155. * @return IEEE-754 Negative Zero.
  156. */
  157. LIBSBML_EXTERN
  158. double
  159. util_NegZero (void);
  160. /**
  161. * @return -1 if d represents negative infinity, 1 if d represents positive
  162. * infinity and 0 otherwise.
  163. */
  164. LIBSBML_EXTERN
  165. int
  166. util_isInf (double d);
  167. /**
  168. * @return true (non-zero) if d is an IEEE-754 negative zero, false (zero)
  169. * otherwise.
  170. */
  171. LIBSBML_EXTERN
  172. int
  173. util_isNegZero (double d);
  174. /** @cond doxygen-libsbml-internal */
  175. /**
  176. * Function prototypes to supress gcc compiler warnings about implicit
  177. * declaration of isnan and finite which occur despite the inclusion of
  178. * math.h where they are defined. In fact, even a simple example
  179. * triggers this warning:
  180. *
  181. * #include <math.h>
  182. *
  183. * int main (int argc, char **argv) { return isnan(1.0); }
  184. *
  185. * % gcc -ansi -Wall test.c
  186. * test.c:3: warning: implicit declaration of function `isnan'
  187. */
  188. #ifndef __cplusplus
  189. #if !defined(isnan)
  190. int isnan (double d);
  191. #endif
  192. #if !defined(finite)
  193. int finite (double d);
  194. #endif
  195. #endif
  196. /** @endcond doxygen-libsbml-internal */
  197. END_C_DECLS
  198. LIBSBML_CPP_NAMESPACE_END
  199. #endif /** util_h **/