Merge branch 'release-4-6' into release-5-0
[alexxy/gromacs.git] / src / gromacs / utility / cstringutil.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
10  *
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.
15  *
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.
20  *
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.
25  *
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.
33  *
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.
36  */
37 /*! \file
38  * \brief Generic C string handling functions.
39  *
40  * \inpublicapi
41  * \ingroup module_utility
42  */
43 #ifndef GMX_UTILITY_CSTRINGUTIL_H
44 #define GMX_UTILITY_CSTRINGUTIL_H
45
46 #include <stdio.h>
47 #include <time.h>
48
49 #include "../legacyheaders/types/simple.h"
50
51 #include "gmx_header_config.h"
52
53 /* Suppress Cygwin compiler warnings from using newlib version of
54  * ctype.h */
55 #ifdef GMX_CYGWIN
56 #include <ctype.h>
57
58 #undef isdigit
59 #undef isstring
60 #undef isspace
61 #undef isalnum
62 #undef isalpha
63 #undef ispunct
64 #undef isxdigit
65 #undef isupper
66 #undef islower
67 #undef toupper
68 #undef tolower
69 #endif
70
71 #ifdef __cplusplus
72 extern "C" {
73 #endif
74 #if 0
75 }
76 #endif
77
78 /** Continuation character. */
79 #define CONTINUE    '\\'
80 /** Comment sign to use. */
81 #define COMMENTSIGN ';'
82
83 /*! \brief
84  * Strip trailing spaces and if s ends with a ::CONTINUE remove that too.
85  *
86  * \returns TRUE if s ends with a CONTINUE, FALSE otherwise.
87  */
88 int continuing(char *s);
89
90 /*! \brief
91  * Reads a line from a stream.
92  *
93  * This routine reads a string from stream of max length n
94  * and zero terminated, without newlines.
95  * \p s should be long enough (>= \p n)
96  */
97 char *fgets2(char *s, int n, FILE *stream);
98
99 /** Remove portion of a line after a ::COMMENTSIGN.  */
100 void strip_comment(char *line);
101
102 /** Make a string uppercase. */
103 void upstring(char *str);
104
105 /** Remove leading whitespace from a string. */
106 void ltrim(char *str);
107
108 /** Remove trailing whitespace from a string. */
109 void rtrim(char *str);
110
111 /** Remove leading and trailing whitespace from a string. */
112 void trim(char *str);
113
114 /** Portable version of ctime_r. */
115 char *gmx_ctime_r(const time_t *clock, char *buf, int n);
116
117 /** Prints creation time stamp and user information into a file as comments. */
118 void nice_header(FILE *out, const char *fn);
119
120 /** Version of gmx_strcasecmp() that also ignores '-' and '_'. */
121 int gmx_strcasecmp_min(const char *str1, const char *str2);
122 /** Version of gmx_strncasecmp() that also ignores '-' and '_'. */
123 int gmx_strncasecmp_min(const char *str1, const char *str2, int n);
124
125 /** Case-insensitive strcmp(). */
126 int gmx_strcasecmp(const char *str1, const char *str2);
127 /** Case-insensitive strncmp(). */
128 int gmx_strncasecmp(const char *str1, const char *str2, int n);
129
130 /** Creates a duplicate of \p src. */
131 char *gmx_strdup(const char *src);
132 /** Duplicates first \p n characters of \p src. */
133 char *gmx_strndup(const char *src, int n);
134
135 /*! \brief
136  * Pattern matching with wildcards.
137  *
138  * \param[in] pattern  Pattern to match against.
139  * \param[in] str      String to match.
140  * \returns   0 on match, GMX_NO_WCMATCH if there is no match.
141  *
142  * Matches \p str against \p pattern, which may contain * and ? wildcards.
143  * All other characters are matched literally.
144  * Currently, it is not possible to match literal * or ?.
145  */
146 int gmx_wcmatch(const char *pattern, const char *str);
147
148 /** Return value for gmx_wcmatch() when there is no match. */
149 #define GMX_NO_WCMATCH 1
150
151 /** Magic hash initialization number from Dan J. Bernstein. */
152 extern const unsigned int
153     gmx_string_hash_init;
154
155 /*! \brief
156  * Return a hash of the string according to Dan J. Bernsteins algorithm.
157  *
158  * \param[in] s          String to calculate hash for.
159  * \param[in] hash_init  Initial (or previous) hash value.
160  * \returns   Updated hash value (hash_init combined with string hash).
161  *
162  * On the first invocation for a new string, use the constant
163  * gmx_string_hash_init for the second argument. If you want to create a hash
164  * corresponding to several concatenated strings, provide the returned hash
165  * value as hash_init for the second string, etc.
166  */
167 unsigned int
168 gmx_string_fullhash_func(const char *s, unsigned int hash_init);
169
170 /*! \brief
171  * Return a hash of the string according to Dan J. Bernsteins algorithm.
172  *
173  * \param[in] s          String to calculate hash for.
174  * \param[in] hash_init  Initial (or previous) hash value.
175  * \returns   Updated hash value (hash_init combined with string hash).
176  *
177  * Identical to gmx_string_fullhash_func, except that
178  * this routine only uses characters for which isalnum(c) is true,
179  * and all characters are converted to upper case.
180  */
181 unsigned int
182 gmx_string_hash_func(const char *s, unsigned int hash_init);
183
184 /*! \brief
185  * Wraps lines, optionally indenting lines.
186  *
187  * Wraps lines at \p linewidth, indenting all following lines by \p indent
188  * spaces.  A temp buffer is allocated and returned, which can be disposed of
189  * if no longer needed.
190  * If \p bIndentFirst is FALSE, then the first line will not be indented, only
191  * the lines that are created due to wapping.
192  */
193 char *wrap_lines(const char *buf, int line_width, int indent,
194                  gmx_bool bIndentFirst);
195
196 /*! \brief
197  * Convert a string to gmx_int64_t.
198  *
199  * This method works as the standard library function strtol(), except that it
200  * does not support different bases.
201  */
202 gmx_int64_t str_to_int64_t(const char *str, char **endptr);
203
204 #ifdef GMX_NATIVE_WINDOWS
205 #define snprintf _snprintf
206 #endif
207
208 /*! \brief Construct an array of digits found in the input string
209  *
210  * \param[in]  digitstring  String that must contain only digits
211  * \param[out] ndigits      Size of return array with the values of the digits
212  * \param[out] digitlist    Array of digits found in
213  *                          digitstring. Allocated by this function
214  *                          with size *ndigits. Calling code is
215  *                          responsible for deallocation.
216  *
217  * If digitstring is NULL, then ndigits is set to zero and digitlist
218  * to NULL. If digitstring contains a non-digit character, a fatal
219  * error results.
220  */
221 void parse_digits_from_plain_string(const char *digitstring, int *ndigits, int **digitlist);
222
223 #ifdef __cplusplus
224 }
225 #endif
226
227 #endif