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