2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
38 * \brief Generic C string handling functions.
41 * \ingroup module_utility
43 #ifndef GMX_UTILITY_CSTRINGUTIL_H
44 #define GMX_UTILITY_CSTRINGUTIL_H
49 #include "basedefinitions.h"
51 #include "gmx_header_config.h"
60 /** Continuation character. */
62 /** Comment sign to use. */
63 #define COMMENTSIGN ';'
64 /** Standard size for char* string buffers. */
68 * Strip trailing spaces and if s ends with a ::CONTINUE remove that too.
70 * \returns TRUE if s ends with a CONTINUE, FALSE otherwise.
72 int continuing(char *s);
75 * Reads a line from a stream.
77 * This routine reads a string from stream of max length n
78 * and zero terminated, without newlines.
79 * \p s should be long enough (>= \p n)
81 char *fgets2(char *s, int n, FILE *stream);
83 /** Remove portion of a line after a ::COMMENTSIGN. */
84 void strip_comment(char *line);
86 /** Make a string uppercase. */
87 void upstring(char *str);
89 /** Remove leading whitespace from a string. */
90 void ltrim(char *str);
92 /** Remove trailing whitespace from a string. */
93 void rtrim(char *str);
95 /** Remove leading and trailing whitespace from a string. */
98 /** Portable version of ctime_r. */
99 char *gmx_ctime_r(const time_t *clock, char *buf, int n);
101 /** Prints creation time stamp and user information into a file as comments. */
102 void nice_header(FILE *out, const char *fn);
104 /** Version of gmx_strcasecmp() that also ignores '-' and '_'. */
105 int gmx_strcasecmp_min(const char *str1, const char *str2);
106 /** Version of gmx_strncasecmp() that also ignores '-' and '_'. */
107 int gmx_strncasecmp_min(const char *str1, const char *str2, int n);
109 /** Case-insensitive strcmp(). */
110 int gmx_strcasecmp(const char *str1, const char *str2);
111 /** Case-insensitive strncmp(). */
112 int gmx_strncasecmp(const char *str1, const char *str2, int n);
114 /** Creates a duplicate of \p src. */
115 char *gmx_strdup(const char *src);
116 /** Duplicates first \p n characters of \p src. */
117 char *gmx_strndup(const char *src, int n);
120 * Pattern matching with wildcards.
122 * \param[in] pattern Pattern to match against.
123 * \param[in] str String to match.
124 * \returns 0 on match, GMX_NO_WCMATCH if there is no match.
126 * Matches \p str against \p pattern, which may contain * and ? wildcards.
127 * All other characters are matched literally.
128 * Currently, it is not possible to match literal * or ?.
130 int gmx_wcmatch(const char *pattern, const char *str);
132 /** Return value for gmx_wcmatch() when there is no match. */
133 #define GMX_NO_WCMATCH 1
135 /** Magic hash initialization number from Dan J. Bernstein. */
136 extern const unsigned int
137 gmx_string_hash_init;
140 * Return a hash of the string according to Dan J. Bernsteins algorithm.
142 * \param[in] s String to calculate hash for.
143 * \param[in] hash_init Initial (or previous) hash value.
144 * \returns Updated hash value (hash_init combined with string hash).
146 * On the first invocation for a new string, use the constant
147 * gmx_string_hash_init for the second argument. If you want to create a hash
148 * corresponding to several concatenated strings, provide the returned hash
149 * value as hash_init for the second string, etc.
152 gmx_string_fullhash_func(const char *s, unsigned int hash_init);
155 * Return a hash of the string according to Dan J. Bernsteins algorithm.
157 * \param[in] s String to calculate hash for.
158 * \param[in] hash_init Initial (or previous) hash value.
159 * \returns Updated hash value (hash_init combined with string hash).
161 * Identical to gmx_string_fullhash_func, except that
162 * this routine only uses characters for which isalnum(c) is true,
163 * and all characters are converted to upper case.
166 gmx_string_hash_func(const char *s, unsigned int hash_init);
169 * Wraps lines, optionally indenting lines.
171 * Wraps lines at \p linewidth, indenting all following lines by \p indent
172 * spaces. A temp buffer is allocated and returned, which can be disposed of
173 * if no longer needed.
174 * If \p bIndentFirst is FALSE, then the first line will not be indented, only
175 * the lines that are created due to wapping.
177 char *wrap_lines(const char *buf, int line_width, int indent,
178 gmx_bool bIndentFirst);
181 * Convert a string to gmx_int64_t.
183 * This method works as the standard library function strtol(), except that it
184 * does not support different bases.
186 gmx_int64_t str_to_int64_t(const char *str, char **endptr);
188 /** Minimum size of buffer to pass to gmx_step_str(). */
189 #define STEPSTRSIZE 22
192 * Prints a gmx_int64_t value in buf and returns the pointer to buf.
194 * buf should be large enough to contain i: STEPSTRSIZE (22) chars.
195 * When multiple gmx_int64_t values are printed in the same printf call,
196 * be sure to call gmx_step_str with different buffers.
198 char *gmx_step_str(gmx_int64_t i, char *buf);
200 #ifdef GMX_NATIVE_WINDOWS
201 #define snprintf _snprintf
204 /*! \brief Construct an array of digits found in the input string
206 * \param[in] digitstring String that must contain only digits
207 * \param[out] ndigits Size of return array with the values of the digits
208 * \param[out] digitlist Array of digits found in
209 * digitstring. Allocated by this function
210 * with size *ndigits. Calling code is
211 * responsible for deallocation.
213 * If digitstring is NULL, then ndigits is set to zero and digitlist
214 * to NULL. If digitstring contains a non-digit character, a fatal
217 void parse_digits_from_plain_string(const char *digitstring, int *ndigits, int **digitlist);