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) 2013,2014,2015,2016,2017,2018,2019, 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.
37 /* This file is completely threadsafe - keep it that way! */
40 #include "cstringutil.h"
51 #include "gromacs/utility/basedefinitions.h"
52 #include "gromacs/utility/fatalerror.h"
53 #include "gromacs/utility/futil.h"
54 #include "gromacs/utility/smalloc.h"
56 //! Comment sign to use.
57 #define COMMENTSIGN ';'
59 int continuing(char* s)
66 if ((sl > 0) && (s[sl - 1] == CONTINUE))
78 char* fgets2(char* line, int n, FILE* stream)
81 if (fgets(line, n, stream) == nullptr)
85 if ((c = strchr(line, '\n')) != nullptr)
91 /* A line not ending in a newline can only occur at the end of a file,
92 * or because of n being too small.
93 * Since both cases occur very infrequently, we can check for EOF.
98 "An input file contains a line longer than %d characters, while the buffer "
99 "passed to fgets2 has size %d. The line starts with: '%20.20s'",
103 if ((c = strchr(line, '\r')) != nullptr)
111 void strip_comment(char* line)
120 /* search for a comment mark and replace it by a zero */
121 if ((c = strchr(line, COMMENTSIGN)) != nullptr)
127 void upstring(char* str)
131 for (i = 0; (i < static_cast<int>(strlen(str))); i++)
133 str[i] = toupper(str[i]);
137 void ltrim(char* str)
147 while (('\0' != str[c]) && isspace(str[c]))
153 for (i = c; ('\0' != str[i]); i++)
161 void rtrim(char* str)
170 nul = strlen(str) - 1;
171 while ((nul > 0) && ((str[nul] == ' ') || (str[nul] == '\t')))
184 int gmx_strcasecmp_min(const char* str1, const char* str2)
192 ch1 = toupper(*(str1++));
193 } while ((ch1 == '-') || (ch1 == '_'));
196 ch2 = toupper(*(str2++));
197 } while ((ch2 == '-') || (ch2 == '_'));
207 int gmx_strncasecmp_min(const char* str1, const char* str2, int n)
212 stri1 = const_cast<char*>(str1);
213 stri2 = const_cast<char*>(str2);
218 ch1 = toupper(*(str1++));
219 } while ((ch1 == '-') || (ch1 == '_'));
222 ch2 = toupper(*(str2++));
223 } while ((ch2 == '-') || (ch2 == '_'));
229 } while ((ch1 != 0) && (str1 - stri1 < n) && (str2 - stri2 < n));
233 int gmx_strcasecmp(const char* str1, const char* str2)
239 ch1 = toupper(*(str1++));
240 ch2 = toupper(*(str2++));
249 int gmx_strncasecmp(const char* str1, const char* str2, int n)
260 ch1 = toupper(*(str1++));
261 ch2 = toupper(*(str2++));
267 } while ((ch1 != 0) && (n != 0));
271 char* gmx_strdup(const char* src)
275 auto length = strlen(src) + 1;
277 std::strncpy(dest, src, length);
282 char* gmx_strndup(const char* src, int n)
293 strncpy(dest, src, len);
298 /* Magic hash init number for Dan J. Bernsteins algorithm.
299 * Do NOT use any other value unless you really know what you are doing.
301 const unsigned int gmx_string_hash_init = 5381;
304 unsigned int gmx_string_fullhash_func(const char* s, unsigned int hash_init)
308 while ((c = (*s++)) != '\0')
310 hash_init = ((hash_init << 5) + hash_init) ^ c; /* (hash * 33) xor c */
315 unsigned int gmx_string_hash_func(const char* s, unsigned int hash_init)
319 while ((c = toupper(*s++)) != '\0')
323 hash_init = ((hash_init << 5) + hash_init) ^ c; /* (hash * 33) xor c */
329 int gmx_wcmatch(const char* pattern, const char* str)
335 /* Skip multiple wildcards in a sequence */
336 while (*pattern == '*' || *pattern == '?')
339 /* For ?, we need to check that there are characters left
345 return GMX_NO_WCMATCH;
353 /* If the pattern ends after the star, we have a match */
358 /* Match the rest against each possible suffix of str */
361 /* Only do the recursive call if the first character
362 * matches. We don't have to worry about wildcards here,
363 * since we have processed them above. */
364 if (*pattern == *str)
367 /* Match the suffix, and return if a match or an error */
368 rc = gmx_wcmatch(pattern, str);
369 if (rc != GMX_NO_WCMATCH)
376 /* If no suffix of str matches, we don't have a match */
377 return GMX_NO_WCMATCH;
379 else if ((*pattern == '?' && *str != 0) || *pattern == *str)
385 return GMX_NO_WCMATCH;
389 /* When the pattern runs out, we have a match if the string has ended. */
390 return (*str == 0) ? 0 : GMX_NO_WCMATCH;
393 char* wrap_lines(const char* buf, int line_width, int indent, gmx_bool bIndentFirst)
396 int i, i0, i2, j, b2len, lspace = 0, l2space = 0;
397 gmx_bool bFirst, bFitsOnLine;
399 /* characters are copied from buf to b2 with possible spaces changed
400 * into newlines and extra space added for indentation.
401 * i indexes buf (source buffer) and i2 indexes b2 (destination buffer)
402 * i0 points to the beginning of the current line (in buf, source)
403 * lspace and l2space point to the last space on the current line
404 * bFirst is set to prevent indentation of first line
405 * bFitsOnLine says if the first space occurred before line_width, if
406 * that is not the case, we have a word longer than line_width which
407 * will also not fit on the next line, so we might as well keep it on
408 * the current line (where it also won't fit, but looks better)
412 b2len = strlen(buf) + 1 + indent;
417 for (i2 = 0; (i2 < indent); i2++)
426 /* find the last space before end of line */
427 for (i = i0; ((i - i0 < line_width) || (l2space == -1)) && (buf[i]); i++)
430 /* remember the position of a space */
436 /* if we have a newline before the line is full, reset counters */
437 if (buf[i] == '\n' && buf[i + 1])
442 /* add indentation after the newline */
443 for (j = 0; (j < indent); j++)
449 /* If we are at the last newline, copy it */
450 if (buf[i] == '\n' && !buf[i + 1])
454 /* if we're not at the end of the string */
457 /* check if one word does not fit on the line */
458 bFitsOnLine = (i - i0 <= line_width);
459 /* reset line counters to just after the space */
462 /* if the words fit on the line, and we're beyond the indentation part */
463 if ((bFitsOnLine) && (l2space >= indent))
465 /* start a new line */
467 /* and add indentation */
472 line_width -= indent;
477 for (j = 0; (j < indent); j++)
481 /* no extra spaces after indent; */
482 while (buf[i0] == ' ')
489 } while (buf[i] != 0);
495 int64_t str_to_int64_t(const char* str, char** endptr)
498 return strtoll(str, endptr, 10);
500 return _strtoi64(str, endptr, 10);
504 char* gmx_step_str(int64_t i, char* buf)
506 sprintf(buf, "%" PRId64, i);