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! */
48 #include <sys/types.h>
57 #include "gmx_fatal.h"
62 int continuing(char *s
)
63 /* strip trailing spaces and if s ends with a CONTINUE remove that too.
64 * returns TRUE if s ends with a CONTINUE, FALSE otherwise.
71 if ((sl
> 0) && (s
[sl
-1] == CONTINUE
)) {
81 char *fgets2(char *line
, int n
, FILE *stream
)
82 /* This routine reads a string from stream of max length n
83 * and zero terminated, without newlines
84 * line should be long enough (>= n)
88 if (fgets(line
,n
,stream
) == NULL
) {
91 if ((c
=strchr(line
,'\n')) != NULL
) {
94 /* A line not ending in a newline can only occur at the end of a file,
95 * or because of n being too small.
96 * Since both cases occur very infrequently, we can check for EOF.
98 if (!gmx_eof(stream
)) {
99 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
);
102 if ((c
=strchr(line
,'\r')) != NULL
) {
109 void strip_comment (char *line
)
116 /* search for a comment mark and replace it by a zero */
117 if ((c
= strchr(line
,COMMENTSIGN
)) != NULL
)
121 void upstring (char *str
)
125 for (i
=0; (i
< (int)strlen(str
)); i
++)
126 str
[i
] = toupper(str
[i
]);
129 void ltrim (char *str
)
138 while (('\0' != str
[c
]) && isspace(str
[c
]))
142 for(i
=c
; ('\0' != str
[i
]); i
++)
148 void rtrim (char *str
)
156 while ((nul
> 0) && ((str
[nul
] == ' ') || (str
[nul
] == '\t')) ) {
162 void trim (char *str
)
169 gmx_ctime_r(const time_t *clock
,char *buf
, int n
)
173 #if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
175 ctime_s( tmpbuf
, STRLEN
, clock
);
177 ctime_r(clock
,tmpbuf
);
179 strncpy(buf
,tmpbuf
,n
-1);
185 void nice_header (FILE *out
,const char *fn
)
187 const char *unk
= "onbekend";
193 char timebuf
[STRLEN
];
198 /* Print a nice header above the file */
200 fprintf (out
,"%c\n",COMMENTSIGN
);
201 fprintf (out
,"%c\tFile '%s' was generated\n",COMMENTSIGN
,fn
? fn
: unk
);
206 gh
= gethostname(buf
,255);
213 gmx_ctime_r(&clock
,timebuf
,STRLEN
);
214 fprintf (out
,"%c\tBy user: %s (%d)\n",COMMENTSIGN
,
215 user
? user
: unk
,(int) uid
);
216 fprintf(out
,"%c\tOn host: %s\n",COMMENTSIGN
,(gh
== 0) ? buf
: unk
);
218 fprintf (out
,"%c\tAt date: %s",COMMENTSIGN
,timebuf
);
219 fprintf (out
,"%c\n",COMMENTSIGN
);
222 int strcasecmp_min(const char *str1
, const char *str2
)
229 ch1
=toupper(*(str1
++));
230 while ((ch1
=='-') || (ch1
=='_'));
232 ch2
=toupper(*(str2
++));
233 while ((ch2
=='-') || (ch2
=='_'));
234 if (ch1
!=ch2
) return (ch1
-ch2
);
240 int strncasecmp_min(const char *str1
, const char *str2
, int n
)
250 ch1
=toupper(*(str1
++));
251 while ((ch1
=='-') || (ch1
=='_'));
253 ch2
=toupper(*(str2
++));
254 while ((ch2
=='-') || (ch2
=='_'));
255 if (ch1
!=ch2
) return (ch1
-ch2
);
257 while (ch1
&& (str1
-stri1
<n
) && (str2
-stri2
<n
));
261 int gmx_strcasecmp(const char *str1
, const char *str2
)
267 ch1
=toupper(*(str1
++));
268 ch2
=toupper(*(str2
++));
269 if (ch1
!=ch2
) return (ch1
-ch2
);
275 int gmx_strncasecmp(const char *str1
, const char *str2
, int n
)
284 ch1
=toupper(*(str1
++));
285 ch2
=toupper(*(str2
++));
286 if (ch1
!=ch2
) return (ch1
-ch2
);
293 char *gmx_strdup(const char *src
)
297 snew(dest
,strlen(src
)+1);
304 gmx_strndup(const char *src
, int n
)
315 strncpy(dest
, src
, len
);
321 * \param[in] pattern Pattern to match against.
322 * \param[in] str String to match.
323 * \returns 0 on match, GMX_NO_WCMATCH if there is no match.
325 * Matches \p str against \p pattern, which may contain * and ? wildcards.
326 * All other characters are matched literally.
327 * Currently, it is not possible to match literal * or ?.
330 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
,bool bIndentFirst
)
397 int i
,i0
,i2
,j
,b2len
,lspace
=0,l2space
=0;
398 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
;
417 for(i2
=0; (i2
<indent
); i2
++)
422 /* find the last space before end of line */
423 for(i
=i0
; ((i
-i0
< line_width
) || (l2space
==-1)) && (buf
[i
]); i
++) {
425 /* 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]) {
435 /* add indentation after the newline */
436 for(j
=0; (j
<indent
); j
++)
440 /* If we are at the last newline, copy it */
441 if (buf
[i
]=='\n' && !buf
[i
+1]) {
444 /* if we're not at the end of the string */
446 /* check if one word does not fit on the line */
447 bFitsOnLine
= (i
-i0
<= line_width
);
448 /* reset line counters to just after the space */
451 /* if the words fit on the line, and we're beyond the indentation part */
452 if ( (bFitsOnLine
) && (l2space
>= indent
) ) {
453 /* start a new line */
455 /* and add indentation */
463 for(j
=0; (j
<indent
); j
++)
465 /* no extra spaces after indent; */
477 char **split(char sep
,const char *str
)
485 for(n
=0; (n
<nn
); n
++)
490 while (*str
!= '\0') {
491 while ((*str
!= '\0') && (*str
== sep
))
494 snew(ptr
[nptr
],1+strlen(str
));
496 while ((*str
!= '\0') && (*str
!= sep
)) {
512 str_to_large_int_t(const char *str
, char **endptr
)
515 gmx_large_int_t val
= 0;
526 /* Strip off initial white space */
531 /* Conform to ISO C99 - return original pointer if string does not contain a number */
543 while( ((ch
=*p
) != '\0') && isdigit(ch
) )
545 /* Important to add sign here, so we dont overflow in final multiplication */
550 /* Some sort of overflow has occured, set endptr to original string */
563 char *gmx_strsep(char **stringp
, const char *delim
)
566 int len
=strlen(delim
);
575 if ( (*stringp
)[j
] == '\0')
583 if ( (*stringp
)[j
]==delim
[i
])
586 *stringp
=*stringp
+j
+1;