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,2021, 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)
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'",
105 if ((c = strchr(line, '\r')) != nullptr)
113 void strip_comment(char* line)
122 /* search for a comment mark and replace it by a zero */
123 if ((c = strchr(line, COMMENTSIGN)) != nullptr)
129 void upstring(char* str)
131 for (int i = 0; (i < static_cast<int>(strlen(str))); i++)
133 str[i] = toupper(str[i]);
137 void ltrim(char* str)
145 while (('\0' != str[c]) && isspace(str[c]))
152 for (; ('\0' != str[i]); i++)
160 void rtrim(char* str)
167 int nul = strlen(str) - 1;
168 while ((nul > 0) && ((str[nul] == ' ') || (str[nul] == '\t')))
181 int gmx_strcasecmp_min(const char* str1, const char* str2)
183 char ch1 = 0, ch2 = 0;
189 ch1 = toupper(*(str1++));
190 } while ((ch1 == '-') || (ch1 == '_'));
193 ch2 = toupper(*(str2++));
194 } while ((ch2 == '-') || (ch2 == '_'));
204 int gmx_strncasecmp_min(const char* str1, const char* str2, int n)
206 char ch1 = 0, ch2 = 0;
208 const char* stri1 = str1;
209 const char* stri2 = str2;
214 ch1 = toupper(*(str1++));
215 } while ((ch1 == '-') || (ch1 == '_'));
218 ch2 = toupper(*(str2++));
219 } while ((ch2 == '-') || (ch2 == '_'));
225 } while ((ch1 != 0) && (str1 - stri1 < n) && (str2 - stri2 < n));
229 int gmx_strcasecmp(const char* str1, const char* str2)
231 char ch1 = 0, ch2 = 0;
235 ch1 = toupper(*(str1++));
236 ch2 = toupper(*(str2++));
245 int gmx_strncasecmp(const char* str1, const char* str2, int n)
247 char ch1 = 0, ch2 = 0;
256 ch1 = toupper(*(str1++));
257 ch2 = toupper(*(str2++));
263 } while ((ch1 != 0) && (n != 0));
267 char* gmx_strdup(const char* src)
269 char* dest = nullptr;
271 auto length = strlen(src) + 1;
273 std::strncpy(dest, src, length);
278 char* gmx_strndup(const char* src, int n)
280 char* dest = nullptr;
282 int len = strlen(src);
288 strncpy(dest, src, len);
293 /* Magic hash init number for Dan J. Bernsteins algorithm.
294 * Do NOT use any other value unless you really know what you are doing.
296 const unsigned int gmx_string_hash_init = 5381;
299 unsigned int gmx_string_fullhash_func(const char* s, unsigned int hash_init)
303 while ((c = (*s++)) != '\0')
305 hash_init = ((hash_init << 5) + hash_init) ^ c; /* (hash * 33) xor c */
310 unsigned int gmx_string_hash_func(const char* s, unsigned int hash_init)
314 while ((c = toupper(*s++)) != '\0')
318 hash_init = ((hash_init << 5) + hash_init) ^ c; /* (hash * 33) xor c */
324 int gmx_wcmatch(const char* pattern, const char* str)
330 /* Skip multiple wildcards in a sequence */
331 while (*pattern == '*' || *pattern == '?')
334 /* For ?, we need to check that there are characters left
340 return GMX_NO_WCMATCH;
348 /* If the pattern ends after the star, we have a match */
353 /* Match the rest against each possible suffix of str */
356 /* Only do the recursive call if the first character
357 * matches. We don't have to worry about wildcards here,
358 * since we have processed them above. */
359 if (*pattern == *str)
361 /* Match the suffix, and return if a match or an error */
362 int rc = gmx_wcmatch(pattern, str);
363 if (rc != GMX_NO_WCMATCH)
370 /* If no suffix of str matches, we don't have a match */
371 return GMX_NO_WCMATCH;
373 else if ((*pattern == '?' && *str != 0) || *pattern == *str)
379 return GMX_NO_WCMATCH;
383 /* When the pattern runs out, we have a match if the string has ended. */
384 return (*str == 0) ? 0 : GMX_NO_WCMATCH;
387 char* wrap_lines(const char* buf, int line_width, int indent, gmx_bool bIndentFirst)
391 /* characters are copied from buf to b2 with possible spaces changed
392 * into newlines and extra space added for indentation.
393 * i indexes buf (source buffer) and i2 indexes b2 (destination buffer)
394 * i0 points to the beginning of the current line (in buf, source)
395 * lspace and l2space point to the last space on the current line
396 * bFirst is set to prevent indentation of first line
397 * bFitsOnLine says if the first space occurred before line_width, if
398 * that is not the case, we have a word longer than line_width which
399 * will also not fit on the next line, so we might as well keep it on
400 * the current line (where it also won't fit, but looks better)
404 int b2len = strlen(buf) + 1 + indent;
410 for (i2 = 0; (i2 < indent); i2++)
420 /* find the last space before end of line */
421 for (i = i0; ((i - i0 < line_width) || (l2space == -1)) && (buf[i]); i++)
424 /* remember the position of a space */
430 /* if we have a newline before the line is full, reset counters */
431 if (buf[i] == '\n' && buf[i + 1])
436 /* add indentation after the newline */
437 for (int j = 0; (j < indent); j++)
443 /* If we are at the last newline, copy it */
444 if (buf[i] == '\n' && !buf[i + 1])
448 /* if we're not at the end of the string */
451 /* check if one word does not fit on the line */
452 bool bFitsOnLine = (i - i0 <= line_width);
453 /* reset line counters to just after the space */
456 /* if the words fit on the line, and we're beyond the indentation part */
457 if ((bFitsOnLine) && (l2space >= indent))
459 /* start a new line */
461 /* and add indentation */
466 line_width -= indent;
471 for (int j = 0; (j < indent); j++)
475 /* no extra spaces after indent; */
476 while (buf[i0] == ' ')
483 } while (buf[i] != 0);
489 int64_t str_to_int64_t(const char* str, char** endptr)
492 return strtoll(str, endptr, 10);
494 return _strtoi64(str, endptr, 10);
498 char* gmx_step_str(int64_t i, char* buf)
500 sprintf(buf, "%" PRId64, i);