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