3 * This source code is part of
7 * GROningen MAchine for Chemical Simulations
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
33 * GROningen Mixture of Alchemy and Childrens' Stories
35 /* This file is completely threadsafe - keep it that way! */
39 #include "gromacs/utility/gmx_header_config.h"
49 #include <sys/types.h>
52 #ifdef HAVE_SYS_TIME_H
68 #include "gmx_fatal.h"
73 int continuing(char *s)
80 if ((sl > 0) && (s[sl-1] == CONTINUE))
93 char *fgets2(char *line, int n, FILE *stream)
96 if (fgets(line, n, stream) == NULL)
100 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))
112 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);
115 if ((c = strchr(line, '\r')) != NULL)
123 void strip_comment (char *line)
132 /* search for a comment mark and replace it by a zero */
133 if ((c = strchr(line, COMMENTSIGN)) != NULL)
139 void upstring (char *str)
143 for (i = 0; (i < (int)strlen(str)); i++)
145 str[i] = toupper(str[i]);
149 void ltrim (char *str)
160 while (('\0' != str[c]) && isspace(str[c]))
166 for (i = c; ('\0' != str[i]); i++)
174 void rtrim (char *str)
184 while ((nul > 0) && ((str[nul] == ' ') || (str[nul] == '\t')) )
191 void trim (char *str)
198 gmx_ctime_r(const time_t *clock, char *buf, int n)
202 #ifdef GMX_NATIVE_WINDOWS
204 ctime_s( tmpbuf, STRLEN, clock );
205 #elif (defined(__sun))
207 ctime_r(clock, tmpbuf, n);
209 ctime_r(clock, tmpbuf);
211 strncpy(buf, tmpbuf, n-1);
217 void nice_header (FILE *out, const char *fn)
219 const char *unk = "onbekend";
221 const char *user = unk;
229 char timebuf[STRLEN];
234 /* Print a nice header above the file */
236 fprintf (out, "%c\n", COMMENTSIGN);
237 fprintf (out, "%c\tFile '%s' was generated\n", COMMENTSIGN, fn ? fn : unk);
242 gh = gethostname(buf, 255);
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_hash_func(const char *s, unsigned int hash_init)
393 while ((c = toupper(*s++)) != '\0')
397 hash_init = ((hash_init << 5) + hash_init) ^ c; /* (hash * 33) xor c */
404 gmx_wcmatch(const char *pattern, const char *str)
410 /* Skip multiple wildcards in a sequence */
411 while (*pattern == '*' || *pattern == '?')
414 /* For ?, we need to check that there are characters left
420 return GMX_NO_WCMATCH;
428 /* If the pattern ends after the star, we have a match */
433 /* Match the rest against each possible suffix of str */
436 /* Only do the recursive call if the first character
437 * matches. We don't have to worry about wildcards here,
438 * since we have processed them above. */
439 if (*pattern == *str)
442 /* Match the suffix, and return if a match or an error */
443 rc = gmx_wcmatch(pattern, str);
444 if (rc != GMX_NO_WCMATCH)
451 /* If no suffix of str matches, we don't have a match */
452 return GMX_NO_WCMATCH;
454 else if ((*pattern == '?' && *str != 0) || *pattern == *str)
460 return GMX_NO_WCMATCH;
464 /* When the pattern runs out, we have a match if the string has ended. */
465 return (*str == 0) ? 0 : GMX_NO_WCMATCH;
468 char *wrap_lines(const char *buf, int line_width, int indent, gmx_bool bIndentFirst)
471 int i, i0, i2, j, b2len, lspace = 0, l2space = 0;
472 gmx_bool bFirst, bFitsOnLine;
474 /* characters are copied from buf to b2 with possible spaces changed
475 * into newlines and extra space added for indentation.
476 * i indexes buf (source buffer) and i2 indexes b2 (destination buffer)
477 * i0 points to the beginning of the current line (in buf, source)
478 * lspace and l2space point to the last space on the current line
479 * bFirst is set to prevent indentation of first line
480 * bFitsOnLine says if the first space occurred before line_width, if
481 * that is not the case, we have a word longer than line_width which
482 * will also not fit on the next line, so we might as well keep it on
483 * the current line (where it also won't fit, but looks better)
487 b2len = strlen(buf)+1+indent;
492 for (i2 = 0; (i2 < indent); i2++)
501 /* find the last space before end of line */
502 for (i = i0; ((i-i0 < line_width) || (l2space == -1)) && (buf[i]); i++)
505 /* remember the position of a space */
511 /* if we have a newline before the line is full, reset counters */
512 if (buf[i] == '\n' && buf[i+1])
517 /* add indentation after the newline */
518 for (j = 0; (j < indent); j++)
524 /* If we are at the last newline, copy it */
525 if (buf[i] == '\n' && !buf[i+1])
529 /* if we're not at the end of the string */
532 /* check if one word does not fit on the line */
533 bFitsOnLine = (i-i0 <= line_width);
534 /* reset line counters to just after the space */
537 /* if the words fit on the line, and we're beyond the indentation part */
538 if ( (bFitsOnLine) && (l2space >= indent) )
540 /* start a new line */
542 /* and add indentation */
547 line_width -= indent;
552 for (j = 0; (j < indent); j++)
556 /* no extra spaces after indent; */
557 while (buf[i0] == ' ')
571 char **split(char sep, const char *str)
581 for (n = 0; (n < nn); n++)
592 while ((*str != '\0') && (*str == sep))
598 snew(ptr[nptr], 1+strlen(str));
600 while ((*str != '\0') && (*str != sep))
617 str_to_large_int_t(const char *str, char **endptr)
620 gmx_large_int_t val = 0;
631 /* Strip off initial white space */
636 /* Conform to ISO C99 - return original pointer if string does not contain a number */
639 *endptr = (char *)str;
648 while ( ((ch = *p) != '\0') && isdigit(ch) )
650 /* Important to add sign here, so we dont overflow in final multiplication */
655 /* Some sort of overflow has occured, set endptr to original string */
656 *endptr = (char *)str;
668 char *gmx_strsep(char **stringp, const char *delim)
671 int len = strlen(delim);
682 if ( (*stringp)[j] == '\0')
688 for (i = 0; i < len; i++)
690 if ( (*stringp)[j] == delim[i])
692 (*stringp)[j] = '\0';
693 *stringp = *stringp+j+1;