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 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012,2013, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * 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! */
42 #include "visibility.h"
52 #include <sys/types.h>
55 #ifdef HAVE_SYS_TIME_H
68 #include "gmx_fatal.h"
73 int continuing(char *s)
74 /* strip trailing spaces and if s ends with a CONTINUE remove that too.
75 * returns TRUE if s ends with a CONTINUE, FALSE otherwise.
83 if ((sl > 0) && (s[sl-1] == CONTINUE)) {
93 char *fgets2(char *line, int n, FILE *stream)
94 /* This routine reads a string from stream of max length n
95 * and zero terminated, without newlines
96 * line should be long enough (>= n)
100 if (fgets(line,n,stream) == NULL) {
103 if ((c=strchr(line,'\n')) != NULL) {
106 /* A line not ending in a newline can only occur at the end of a file,
107 * or because of n being too small.
108 * Since both cases occur very infrequently, we can check for EOF.
110 if (!gmx_eof(stream)) {
111 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);
114 if ((c=strchr(line,'\r')) != NULL) {
121 void strip_comment (char *line)
128 /* search for a comment mark and replace it by a zero */
129 if ((c = strchr(line,COMMENTSIGN)) != NULL)
133 void upstring (char *str)
137 for (i=0; (i < (int)strlen(str)); i++)
138 str[i] = toupper(str[i]);
141 void ltrim (char *str)
150 while (('\0' != str[c]) && isspace(str[c]))
154 for(i=c; ('\0' != str[i]); i++)
160 void rtrim (char *str)
168 while ((nul > 0) && ((str[nul] == ' ') || (str[nul] == '\t')) ) {
174 void trim (char *str)
182 gmx_ctime_r(const time_t *clock,char *buf, int n)
186 #ifdef GMX_NATIVE_WINDOWS
188 ctime_s( tmpbuf, STRLEN, clock );
189 #elif (defined(__sun))
191 ctime_r(clock, tmpbuf, n);
193 ctime_r(clock,tmpbuf);
195 strncpy(buf,tmpbuf,n-1);
201 void nice_header (FILE *out,const char *fn)
203 const char *unk = "onbekend";
205 const char *user=unk;
209 char timebuf[STRLEN];
214 /* Print a nice header above the file */
216 fprintf (out,"%c\n",COMMENTSIGN);
217 fprintf (out,"%c\tFile '%s' was generated\n",COMMENTSIGN,fn ? fn : unk);
222 gh = gethostname(buf,255);
229 gmx_ctime_r(&clock,timebuf,STRLEN);
230 fprintf (out,"%c\tBy user: %s (%d)\n",COMMENTSIGN,
231 user ? user : unk,(int) uid);
232 fprintf(out,"%c\tOn host: %s\n",COMMENTSIGN,(gh == 0) ? buf : unk);
234 fprintf (out,"%c\tAt date: %s",COMMENTSIGN,timebuf);
235 fprintf (out,"%c\n",COMMENTSIGN);
239 int gmx_strcasecmp_min(const char *str1, const char *str2)
247 ch1=toupper(*(str1++));
249 while ((ch1=='-') || (ch1=='_'));
252 ch2=toupper(*(str2++));
254 while ((ch2=='-') || (ch2=='_'));
256 if (ch1!=ch2) return (ch1-ch2);
262 int gmx_strncasecmp_min(const char *str1, const char *str2, int n)
273 ch1=toupper(*(str1++));
275 while ((ch1=='-') || (ch1=='_'));
278 ch2=toupper(*(str2++));
280 while ((ch2=='-') || (ch2=='_'));
282 if (ch1!=ch2) return (ch1-ch2);
284 while (ch1 && (str1-stri1<n) && (str2-stri2<n));
288 int gmx_strcasecmp(const char *str1, const char *str2)
294 ch1=toupper(*(str1++));
295 ch2=toupper(*(str2++));
296 if (ch1!=ch2) return (ch1-ch2);
302 int gmx_strncasecmp(const char *str1, const char *str2, int n)
311 ch1=toupper(*(str1++));
312 ch2=toupper(*(str2++));
313 if (ch1!=ch2) return (ch1-ch2);
320 char *gmx_strdup(const char *src)
324 snew(dest,strlen(src)+1);
331 gmx_strndup(const char *src, int n)
342 strncpy(dest, src, len);
347 /* Magic hash init number for Dan J. Bernsteins algorithm.
348 * Do NOT use any other value unless you really know what you are doing.
351 gmx_string_hash_init = 5381;
355 gmx_string_hash_func(const char *s, unsigned int hash_init)
359 while ((c = toupper(*s++)) != '\0')
361 if(isalnum(c)) hash_init = ((hash_init << 5) + hash_init) ^ c; /* (hash * 33) xor c */
367 * \param[in] pattern Pattern to match against.
368 * \param[in] str String to match.
369 * \returns 0 on match, GMX_NO_WCMATCH if there is no match.
371 * Matches \p str against \p pattern, which may contain * and ? wildcards.
372 * All other characters are matched literally.
373 * Currently, it is not possible to match literal * or ?.
376 gmx_wcmatch(const char *pattern, const char *str)
382 /* Skip multiple wildcards in a sequence */
383 while (*pattern == '*' || *pattern == '?')
386 /* For ?, we need to check that there are characters left
392 return GMX_NO_WCMATCH;
400 /* If the pattern ends after the star, we have a match */
405 /* Match the rest against each possible suffix of str */
408 /* Only do the recursive call if the first character
409 * matches. We don't have to worry about wildcards here,
410 * since we have processed them above. */
411 if (*pattern == *str)
414 /* Match the suffix, and return if a match or an error */
415 rc = gmx_wcmatch(pattern, str);
416 if (rc != GMX_NO_WCMATCH)
423 /* If no suffix of str matches, we don't have a match */
424 return GMX_NO_WCMATCH;
426 else if ((*pattern == '?' && *str != 0) || *pattern == *str)
432 return GMX_NO_WCMATCH;
436 /* When the pattern runs out, we have a match if the string has ended. */
437 return (*str == 0) ? 0 : GMX_NO_WCMATCH;
440 char *wrap_lines(const char *buf,int line_width, int indent,gmx_bool bIndentFirst)
443 int i,i0,i2,j,b2len,lspace=0,l2space=0;
444 gmx_bool bFirst,bFitsOnLine;
446 /* characters are copied from buf to b2 with possible spaces changed
447 * into newlines and extra space added for indentation.
448 * i indexes buf (source buffer) and i2 indexes b2 (destination buffer)
449 * i0 points to the beginning of the current line (in buf, source)
450 * lspace and l2space point to the last space on the current line
451 * bFirst is set to prevent indentation of first line
452 * bFitsOnLine says if the first space occurred before line_width, if
453 * that is not the case, we have a word longer than line_width which
454 * will also not fit on the next line, so we might as well keep it on
455 * the current line (where it also won't fit, but looks better)
459 b2len=strlen(buf)+1+indent;
463 for(i2=0; (i2<indent); i2++)
468 /* find the last space before end of line */
469 for(i=i0; ((i-i0 < line_width) || (l2space==-1)) && (buf[i]); i++) {
471 /* remember the position of a space */
476 /* if we have a newline before the line is full, reset counters */
477 if (buf[i]=='\n' && buf[i+1]) {
481 /* add indentation after the newline */
482 for(j=0; (j<indent); j++)
486 /* If we are at the last newline, copy it */
487 if (buf[i]=='\n' && !buf[i+1]) {
490 /* if we're not at the end of the string */
492 /* check if one word does not fit on the line */
493 bFitsOnLine = (i-i0 <= line_width);
494 /* reset line counters to just after the space */
497 /* if the words fit on the line, and we're beyond the indentation part */
498 if ( (bFitsOnLine) && (l2space >= indent) ) {
499 /* start a new line */
501 /* and add indentation */
509 for(j=0; (j<indent); j++)
511 /* no extra spaces after indent; */
523 char **split(char sep,char *str)
531 for(n=0; (n<nn); n++)
536 while (*str != '\0') {
537 while ((*str != '\0') && (*str == sep))
540 snew(ptr[nptr],1+strlen(str));
542 while ((*str != '\0') && (*str != sep)) {
558 str_to_large_int_t(const char *str, char **endptr)
561 gmx_large_int_t val = 0;
572 /* Strip off initial white space */
577 /* Conform to ISO C99 - return original pointer if string does not contain a number */
589 while( ((ch=*p) != '\0') && isdigit(ch) )
591 /* Important to add sign here, so we dont overflow in final multiplication */
596 /* Some sort of overflow has occured, set endptr to original string */
609 char *gmx_strsep(char **stringp, const char *delim)
612 int len=strlen(delim);
621 if ( (*stringp)[j] == '\0')
629 if ( (*stringp)[j]==delim[i])
632 *stringp=*stringp+j+1;