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 by the GROMACS development team.
7 * Copyright (c) 2018,2019,2020, by the GROMACS development team, led by
8 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
9 * and including many others, as listed in the AUTHORS file in the
10 * top-level source directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
38 /* This file is completely threadsafe - keep it that way! */
41 #include "cstringutil.h"
52 #include "gromacs/utility/basedefinitions.h"
53 #include "gromacs/utility/fatalerror.h"
54 #include "gromacs/utility/futil.h"
55 #include "gromacs/utility/smalloc.h"
57 //! Comment sign to use.
58 #define COMMENTSIGN ';'
60 int continuing(char* s)
67 if ((sl > 0) && (s[sl - 1] == CONTINUE))
79 char* fgets2(char* line, int n, FILE* stream)
82 if (fgets(line, n, stream) == nullptr)
86 if ((c = strchr(line, '\n')) != nullptr)
92 /* A line not ending in a newline can only occur at the end of a file,
93 * or because of n being too small.
94 * Since both cases occur very infrequently, we can check for EOF.
99 "An input file contains a line longer than %d characters, while the buffer "
100 "passed to fgets2 has size %d. The line starts with: '%20.20s'",
104 if ((c = strchr(line, '\r')) != nullptr)
112 void strip_comment(char* line)
121 /* search for a comment mark and replace it by a zero */
122 if ((c = strchr(line, COMMENTSIGN)) != nullptr)
128 void upstring(char* str)
132 for (i = 0; (i < static_cast<int>(strlen(str))); i++)
134 str[i] = toupper(str[i]);
138 void ltrim(char* str)
148 while (('\0' != str[c]) && isspace(str[c]))
154 for (i = c; ('\0' != str[i]); i++)
162 void rtrim(char* str)
171 nul = strlen(str) - 1;
172 while ((nul > 0) && ((str[nul] == ' ') || (str[nul] == '\t')))
185 int gmx_strcasecmp_min(const char* str1, const char* str2)
193 ch1 = toupper(*(str1++));
194 } while ((ch1 == '-') || (ch1 == '_'));
197 ch2 = toupper(*(str2++));
198 } while ((ch2 == '-') || (ch2 == '_'));
208 int gmx_strncasecmp_min(const char* str1, const char* str2, int n)
213 stri1 = const_cast<char*>(str1);
214 stri2 = const_cast<char*>(str2);
219 ch1 = toupper(*(str1++));
220 } while ((ch1 == '-') || (ch1 == '_'));
223 ch2 = toupper(*(str2++));
224 } while ((ch2 == '-') || (ch2 == '_'));
230 } while ((ch1 != 0) && (str1 - stri1 < n) && (str2 - stri2 < n));
234 int gmx_strcasecmp(const char* str1, const char* str2)
240 ch1 = toupper(*(str1++));
241 ch2 = toupper(*(str2++));
250 int gmx_strncasecmp(const char* str1, const char* str2, int n)
261 ch1 = toupper(*(str1++));
262 ch2 = toupper(*(str2++));
268 } while ((ch1 != 0) && (n != 0));
272 char* gmx_strdup(const char* src)
276 auto length = strlen(src) + 1;
278 std::strncpy(dest, src, length);
283 char* gmx_strndup(const char* src, int n)
294 strncpy(dest, src, len);
299 /* Magic hash init number for Dan J. Bernsteins algorithm.
300 * Do NOT use any other value unless you really know what you are doing.
302 const unsigned int gmx_string_hash_init = 5381;
305 unsigned int gmx_string_fullhash_func(const char* s, unsigned int hash_init)
309 while ((c = (*s++)) != '\0')
311 hash_init = ((hash_init << 5) + hash_init) ^ c; /* (hash * 33) xor c */
316 unsigned int gmx_string_hash_func(const char* s, unsigned int hash_init)
320 while ((c = toupper(*s++)) != '\0')
324 hash_init = ((hash_init << 5) + hash_init) ^ c; /* (hash * 33) xor c */
330 int gmx_wcmatch(const char* pattern, const char* str)
336 /* Skip multiple wildcards in a sequence */
337 while (*pattern == '*' || *pattern == '?')
340 /* For ?, we need to check that there are characters left
346 return GMX_NO_WCMATCH;
354 /* If the pattern ends after the star, we have a match */
359 /* Match the rest against each possible suffix of str */
362 /* Only do the recursive call if the first character
363 * matches. We don't have to worry about wildcards here,
364 * since we have processed them above. */
365 if (*pattern == *str)
368 /* Match the suffix, and return if a match or an error */
369 rc = gmx_wcmatch(pattern, str);
370 if (rc != GMX_NO_WCMATCH)
377 /* If no suffix of str matches, we don't have a match */
378 return GMX_NO_WCMATCH;
380 else if ((*pattern == '?' && *str != 0) || *pattern == *str)
386 return GMX_NO_WCMATCH;
390 /* When the pattern runs out, we have a match if the string has ended. */
391 return (*str == 0) ? 0 : GMX_NO_WCMATCH;
394 char* wrap_lines(const char* buf, int line_width, int indent, gmx_bool bIndentFirst)
397 int i, i0, i2, j, b2len, lspace = 0, l2space = 0;
398 gmx_bool bFirst, bFitsOnLine;
400 /* characters are copied from buf to b2 with possible spaces changed
401 * into newlines and extra space added for indentation.
402 * i indexes buf (source buffer) and i2 indexes b2 (destination buffer)
403 * i0 points to the beginning of the current line (in buf, source)
404 * lspace and l2space point to the last space on the current line
405 * bFirst is set to prevent indentation of first line
406 * bFitsOnLine says if the first space occurred before line_width, if
407 * that is not the case, we have a word longer than line_width which
408 * will also not fit on the next line, so we might as well keep it on
409 * the current line (where it also won't fit, but looks better)
413 b2len = strlen(buf) + 1 + indent;
418 for (i2 = 0; (i2 < indent); i2++)
427 /* find the last space before end of line */
428 for (i = i0; ((i - i0 < line_width) || (l2space == -1)) && (buf[i]); i++)
431 /* remember the position of a space */
437 /* if we have a newline before the line is full, reset counters */
438 if (buf[i] == '\n' && buf[i + 1])
443 /* add indentation after the newline */
444 for (j = 0; (j < indent); j++)
450 /* If we are at the last newline, copy it */
451 if (buf[i] == '\n' && !buf[i + 1])
455 /* if we're not at the end of the string */
458 /* check if one word does not fit on the line */
459 bFitsOnLine = (i - i0 <= line_width);
460 /* reset line counters to just after the space */
463 /* if the words fit on the line, and we're beyond the indentation part */
464 if ((bFitsOnLine) && (l2space >= indent))
466 /* start a new line */
468 /* and add indentation */
473 line_width -= indent;
478 for (j = 0; (j < indent); j++)
482 /* no extra spaces after indent; */
483 while (buf[i0] == ' ')
490 } while (buf[i] != 0);
496 int64_t str_to_int64_t(const char* str, char** endptr)
499 return strtoll(str, endptr, 10);
501 return _strtoi64(str, endptr, 10);
505 char* gmx_step_str(int64_t i, char* buf)
507 sprintf(buf, "%" PRId64, i);