More files to C++.
[alexxy/gromacs.git] / src / gromacs / legacyheaders / string2.h
1 /*
2  *
3  *                This source code is part of
4  *
5  *                 G   R   O   M   A   C   S
6  *
7  *          GROningen MAchine for Chemical Simulations
8  *
9  *                        VERSION 3.2.0
10  * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12  * Copyright (c) 2001-2004, The GROMACS development team,
13  * check out http://www.gromacs.org for more information.
14
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * as published by the Free Software Foundation; either version 2
18  * of the License, or (at your option) any later version.
19  *
20  * If you want to redistribute modifications, please consider that
21  * scientific software is very special. Version control is crucial -
22  * bugs must be traceable. We will be happy to consider code for
23  * inclusion in the official distribution, but derived work must not
24  * be called official GROMACS. Details are found in the README & COPYING
25  * files - if they are missing, get the official version at www.gromacs.org.
26  *
27  * To help us fund GROMACS development, we humbly ask that you cite
28  * the papers on the package - you can find them in the top README file.
29  *
30  * For more info, check our website at http://www.gromacs.org
31  *
32  * And Hey:
33  * Gromacs Runs On Most of All Computer Systems
34  */
35 /*! \file
36  * \brief Generic string handling functions.
37  */
38 #ifndef _string2_h
39 #define _string2_h
40
41 /*
42  *
43  * string2.h
44  * David van der Spoel
45  *
46  */
47
48
49 #include <string.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <ctype.h>
53 #include <time.h>
54 #include <errno.h>
55 #include "../utility/gmx_header_config.h"
56
57 #include "types/simple.h"
58
59 /* Suppress Cygwin compiler warnings from using newlib version of
60  * ctype.h */
61 #ifdef GMX_CYGWIN
62 #undef isdigit
63 #undef isstring
64 #undef isspace
65 #undef isalnum
66 #undef isalpha
67 #undef ispunct
68 #undef isxdigit
69 #undef isupper
70 #undef islower
71 #undef toupper
72 #undef tolower
73 #endif
74
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78 #if 0
79 }
80 #endif
81
82 /** Continuation character. */
83 #define CONTINUE    '\\'
84 /** Comment sign to use. */
85 #define COMMENTSIGN ';'
86
87 /*! \brief
88  * Strip trailing spaces and if s ends with a ::CONTINUE remove that too.
89  *
90  * \returns TRUE if s ends with a CONTINUE, FALSE otherwise.
91  */
92 int continuing(char *s);
93
94 /*! \brief
95  * Reads a line from a stream.
96  *
97  * This routine reads a string from stream of max length n
98  * and zero terminated, without newlines.
99  * \p s should be long enough (>= \p n)
100  */
101 char *fgets2(char *s, int n, FILE *stream);
102
103 /** Remove portion of a line after a ::COMMENTSIGN.  */
104 void strip_comment(char *line);
105
106 /** Make a string uppercase. */
107 void upstring(char *str);
108
109 /** Remove leading whitespace from a string. */
110 void ltrim(char *str);
111
112 /** Remove trailing whitespace from a string. */
113 void rtrim(char *str);
114
115 /** Remove leading and trailing whitespace from a string. */
116 void trim(char *str);
117
118 /** Portable version of ctime_r. */
119 char *gmx_ctime_r(const time_t *clock, char *buf, int n);
120
121 /** Prints creation time stamp and user information into a file as comments. */
122 void nice_header(FILE *out, const char *fn);
123
124 /** Version of gmx_strcasecmp() that also ignores '-' and '_'. */
125 int gmx_strcasecmp_min(const char *str1, const char *str2);
126 /** Version of gmx_strncasecmp() that also ignores '-' and '_'. */
127 int gmx_strncasecmp_min(const char *str1, const char *str2, int n);
128
129 /** Case-insensitive strcmp(). */
130 int gmx_strcasecmp(const char *str1, const char *str2);
131 /** Case-insensitive strncmp(). */
132 int gmx_strncasecmp(const char *str1, const char *str2, int n);
133
134 /** Creates a duplicate of \p src. */
135 char *gmx_strdup(const char *src);
136 /** Duplicates first \p n characters of \p src. */
137 char *gmx_strndup(const char *src, int n);
138
139 /*! \brief
140  * Pattern matching with wildcards.
141  *
142  * \param[in] pattern  Pattern to match against.
143  * \param[in] str      String to match.
144  * \returns   0 on match, GMX_NO_WCMATCH if there is no match.
145  *
146  * Matches \p str against \p pattern, which may contain * and ? wildcards.
147  * All other characters are matched literally.
148  * Currently, it is not possible to match literal * or ?.
149  */
150 int gmx_wcmatch(const char *pattern, const char *str);
151
152 /** Magic hash initialization number from Dan J. Bernstein. */
153 extern const unsigned int
154     gmx_string_hash_init;
155
156 /*! \brief
157  * Return a hash of the string according to Dan J. Bernsteins algorithm.
158  *
159  * \param[in] s          String to calculate hash for.
160  * \param[in] hash_init  Initial (or previous) hash value.
161  * \returns   Updated hash value (hash_init combined with string hash).
162  *
163  * This routine only uses characters for which isalnum(c) is true,
164  * and all characters are converted to upper case.
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_hash_func(const char *s, unsigned int hash_init);
172
173 /** Return value for gmx_wcmatch() when there is no match. */
174 #define GMX_NO_WCMATCH 1
175
176 /** Our implementation of strsep, the thread-safe replacement for strtok. */
177 char *gmx_strsep(char **stringp, const char *delim);
178
179 /*! \brief
180  * Wraps lines, optionally indenting lines.
181  *
182  * Wraps lines at \p linewidth, indenting all following lines by \p indent
183  * spaces.  A temp buffer is allocated and returned, which can be disposed of
184  * if no longer needed.
185  * If \p bIndentFirst is FALSE, then the first line will not be indented, only
186  * the lines that are created due to wapping.
187  */
188 char *wrap_lines(const char *buf, int line_width, int indent,
189                  gmx_bool bIndentFirst);
190
191 /** Implementation of the well-known Perl function split. */
192 char **split(char sep, const char *str);
193
194 /*! \brief
195  * Convert a string to gmx_large_int_t.
196  *
197  * This method works as the standard library function strtol(), except that it
198  * does not support different bases.
199  *
200  * \attention
201  * The following differences are present from the standard behavior:
202  *  - \p endptr cannot be NULL.
203  *  - If an overflow occurs, returns zero and \p *endptr will equal \p str.
204  *    errno is still set to ERANGE.
205  */
206 gmx_large_int_t str_to_large_int_t(const char *str, char **endptr);
207
208 #ifdef GMX_NATIVE_WINDOWS
209 #define snprintf _snprintf
210 #endif
211
212 #ifdef __cplusplus
213 }
214 #endif
215
216 #endif  /* _string2_h */