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, 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"
49 #include <sys/types.h>
53 #ifdef HAVE_SYS_TIME_H
63 #include "gromacs/utility/basedefinitions.h"
64 #include "gromacs/utility/basenetwork.h"
65 #include "gromacs/utility/fatalerror.h"
66 #include "gromacs/utility/futil.h"
67 #include "gromacs/utility/smalloc.h"
69 int continuing(char *s)
76 if ((sl > 0) && (s[sl-1] == CONTINUE))
89 char *fgets2(char *line, int n, FILE *stream)
92 if (fgets(line, n, stream) == NULL)
96 if ((c = strchr(line, '\n')) != NULL)
102 /* A line not ending in a newline can only occur at the end of a file,
103 * or because of n being too small.
104 * Since both cases occur very infrequently, we can check for EOF.
108 gmx_fatal(FARGS, "An input file contains a line longer than %d characters, while the buffer passed to fgets2 has size %d. The line starts with: '%20.20s'", n, n, line);
111 if ((c = strchr(line, '\r')) != NULL)
119 void strip_comment (char *line)
128 /* search for a comment mark and replace it by a zero */
129 if ((c = strchr(line, COMMENTSIGN)) != NULL)
135 void upstring (char *str)
139 for (i = 0; (i < (int)strlen(str)); i++)
141 str[i] = toupper(str[i]);
145 void ltrim (char *str)
156 while (('\0' != str[c]) && isspace(str[c]))
162 for (i = c; ('\0' != str[i]); i++)
170 void rtrim (char *str)
180 while ((nul > 0) && ((str[nul] == ' ') || (str[nul] == '\t')) )
187 void trim (char *str)
194 gmx_ctime_r(const time_t *clock, char *buf, int n)
198 ctime_s( buf, n, clock );
199 #elif defined(GMX_NATIVE_WINDOWS)
200 char *tmpbuf = ctime( clock );
201 strncpy(buf, tmpbuf, n-1);
203 #elif (defined(__sun))
205 ctime_r(clock, buf, n);
208 ctime_r(clock, tmpbuf);
209 strncpy(buf, tmpbuf, n-1);
215 void nice_header (FILE *out, const char *fn)
217 const char *unk = "onbekend";
227 char timebuf[STRLEN];
232 /* Print a nice header above the file */
234 fprintf (out, "%c\n", COMMENTSIGN);
235 fprintf (out, "%c\tFile '%s' was generated\n", COMMENTSIGN, fn ? fn : unk);
240 gh = gmx_gethostname(buf, 255);
241 /* pw returns null on error (e.g. compute nodes lack /etc/passwd) */
242 user = pw ? pw->pw_name : unk;
249 gmx_ctime_r(&clock, timebuf, STRLEN);
250 fprintf (out, "%c\tBy user: %s (%d)\n", COMMENTSIGN,
251 user ? user : unk, (int) uid);
252 fprintf(out, "%c\tOn host: %s\n", COMMENTSIGN, (gh == 0) ? buf : unk);
254 fprintf (out, "%c\tAt date: %s", COMMENTSIGN, timebuf);
255 fprintf (out, "%c\n", COMMENTSIGN);
259 int gmx_strcasecmp_min(const char *str1, const char *str2)
267 ch1 = toupper(*(str1++));
269 while ((ch1 == '-') || (ch1 == '_'));
272 ch2 = toupper(*(str2++));
274 while ((ch2 == '-') || (ch2 == '_'));
285 int gmx_strncasecmp_min(const char *str1, const char *str2, int n)
290 stri1 = (char *)str1;
291 stri2 = (char *)str2;
296 ch1 = toupper(*(str1++));
298 while ((ch1 == '-') || (ch1 == '_'));
301 ch2 = toupper(*(str2++));
303 while ((ch2 == '-') || (ch2 == '_'));
310 while (ch1 && (str1-stri1 < n) && (str2-stri2 < n));
314 int gmx_strcasecmp(const char *str1, const char *str2)
320 ch1 = toupper(*(str1++));
321 ch2 = toupper(*(str2++));
331 int gmx_strncasecmp(const char *str1, const char *str2, int n)
342 ch1 = toupper(*(str1++));
343 ch2 = toupper(*(str2++));
354 char *gmx_strdup(const char *src)
358 snew(dest, strlen(src)+1);
365 gmx_strndup(const char *src, int n)
376 strncpy(dest, src, len);
381 /* Magic hash init number for Dan J. Bernsteins algorithm.
382 * Do NOT use any other value unless you really know what you are doing.
385 gmx_string_hash_init = 5381;
389 gmx_string_fullhash_func(const char *s, unsigned int hash_init)
393 while ((c = (*s++)) != '\0')
395 hash_init = ((hash_init << 5) + hash_init) ^ c; /* (hash * 33) xor c */
401 gmx_string_hash_func(const char *s, unsigned int hash_init)
405 while ((c = toupper(*s++)) != '\0')
409 hash_init = ((hash_init << 5) + hash_init) ^ c; /* (hash * 33) xor c */
416 gmx_wcmatch(const char *pattern, const char *str)
422 /* Skip multiple wildcards in a sequence */
423 while (*pattern == '*' || *pattern == '?')
426 /* For ?, we need to check that there are characters left
432 return GMX_NO_WCMATCH;
440 /* If the pattern ends after the star, we have a match */
445 /* Match the rest against each possible suffix of str */
448 /* Only do the recursive call if the first character
449 * matches. We don't have to worry about wildcards here,
450 * since we have processed them above. */
451 if (*pattern == *str)
454 /* Match the suffix, and return if a match or an error */
455 rc = gmx_wcmatch(pattern, str);
456 if (rc != GMX_NO_WCMATCH)
463 /* If no suffix of str matches, we don't have a match */
464 return GMX_NO_WCMATCH;
466 else if ((*pattern == '?' && *str != 0) || *pattern == *str)
472 return GMX_NO_WCMATCH;
476 /* When the pattern runs out, we have a match if the string has ended. */
477 return (*str == 0) ? 0 : GMX_NO_WCMATCH;
480 char *wrap_lines(const char *buf, int line_width, int indent, gmx_bool bIndentFirst)
483 int i, i0, i2, j, b2len, lspace = 0, l2space = 0;
484 gmx_bool bFirst, bFitsOnLine;
486 /* characters are copied from buf to b2 with possible spaces changed
487 * into newlines and extra space added for indentation.
488 * i indexes buf (source buffer) and i2 indexes b2 (destination buffer)
489 * i0 points to the beginning of the current line (in buf, source)
490 * lspace and l2space point to the last space on the current line
491 * bFirst is set to prevent indentation of first line
492 * bFitsOnLine says if the first space occurred before line_width, if
493 * that is not the case, we have a word longer than line_width which
494 * will also not fit on the next line, so we might as well keep it on
495 * the current line (where it also won't fit, but looks better)
499 b2len = strlen(buf)+1+indent;
504 for (i2 = 0; (i2 < indent); i2++)
513 /* find the last space before end of line */
514 for (i = i0; ((i-i0 < line_width) || (l2space == -1)) && (buf[i]); i++)
517 /* remember the position of a space */
523 /* if we have a newline before the line is full, reset counters */
524 if (buf[i] == '\n' && buf[i+1])
529 /* add indentation after the newline */
530 for (j = 0; (j < indent); j++)
536 /* If we are at the last newline, copy it */
537 if (buf[i] == '\n' && !buf[i+1])
541 /* if we're not at the end of the string */
544 /* check if one word does not fit on the line */
545 bFitsOnLine = (i-i0 <= line_width);
546 /* reset line counters to just after the space */
549 /* if the words fit on the line, and we're beyond the indentation part */
550 if ( (bFitsOnLine) && (l2space >= indent) )
552 /* start a new line */
554 /* and add indentation */
559 line_width -= indent;
564 for (j = 0; (j < indent); j++)
568 /* no extra spaces after indent; */
569 while (buf[i0] == ' ')
584 str_to_int64_t(const char *str, char **endptr)
587 return strtoll(str, endptr, 10);
589 return _strtoi64(str, endptr, 10);
593 char *gmx_step_str(gmx_int64_t i, char *buf)
595 sprintf(buf, "%"GMX_PRId64, i);
599 void parse_digits_from_plain_string(const char *digitstring, int *ndigits, int **digitlist)
603 if (NULL == digitstring)
610 *ndigits = strlen(digitstring);
612 snew(*digitlist, *ndigits);
614 for (i = 0; i < *ndigits; i++)
616 if (digitstring[i] < '0' || digitstring[i] > '9')
618 gmx_fatal(FARGS, "Invalid character in digit-only string: '%c'\n",
621 (*digitlist)[i] = digitstring[i] - '0';
625 static void parse_digits_from_csv_string(const char gmx_unused *digitstring, int gmx_unused *ndigits, int gmx_unused *digitlist)
627 /* TODO Implement csv format to support (e.g.) more than 10
628 different GPUs in a node. */
629 gmx_incons("Not implemented yet");