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"
61 int continuing(char *s
)
62 /* strip trailing spaces and if s ends with a CONTINUE remove that too.
63 * returns TRUE if s ends with a CONTINUE, FALSE otherwise.
70 if ((sl
> 0) && (s
[sl
-1] == CONTINUE
)) {
78 char *fgets2(char *line
, int n
, FILE *stream
)
79 /* This routine reads a string from stream of max length n
80 * and zero terminated, without newlines
81 * line should be long enough (>= n)
85 if (fgets(line
,n
,stream
)==NULL
) return NULL
;
86 if ((c
=strchr(line
,'\n'))!=NULL
) *c
=0;
87 if ((c
=strchr(line
,'\r'))!=NULL
) *c
=0;
91 void strip_comment (char *line
)
98 /* search for a comment mark and replace it by a zero */
99 if ((c
= strchr(line
,COMMENTSIGN
)) != NULL
)
103 void upstring (char *str
)
107 for (i
=0; (i
< (int)strlen(str
)); i
++)
108 str
[i
] = toupper(str
[i
]);
111 void ltrim (char *str
)
121 while ((tr
[c
] == ' ') || (tr
[c
] == '\t'))
128 void rtrim (char *str
)
136 while ((nul
> 0) && ((str
[nul
] == ' ') || (str
[nul
] == '\t')) ) {
142 void trim (char *str
)
148 void nice_header (FILE *out
,const char *fn
)
150 const char *unk
= "onbekend";
160 /* Print a nice header above the file */
162 fprintf (out
,"%c\n",COMMENTSIGN
);
163 fprintf (out
,"%c\tFile '%s' was generated\n",COMMENTSIGN
,fn
? fn
: unk
);
168 gh
= gethostname(buf
,255);
175 fprintf (out
,"%c\tBy user: %s (%d)\n",COMMENTSIGN
,
176 user
? user
: unk
,(int) uid
);
177 fprintf(out
,"%c\tOn host: %s\n",COMMENTSIGN
,(gh
== 0) ? buf
: unk
);
179 fprintf (out
,"%c\tAt date: %s",COMMENTSIGN
,ctime(&clock
));
180 fprintf (out
,"%c\n",COMMENTSIGN
);
183 int strcasecmp_min(const char *str1
, const char *str2
)
190 ch1
=toupper(*(str1
++));
191 while ((ch1
=='-') || (ch1
=='_'));
193 ch2
=toupper(*(str2
++));
194 while ((ch2
=='-') || (ch2
=='_'));
195 if (ch1
!=ch2
) return (ch1
-ch2
);
201 int strncasecmp_min(const char *str1
, const char *str2
, int n
)
211 ch1
=toupper(*(str1
++));
212 while ((ch1
=='-') || (ch1
=='_'));
214 ch2
=toupper(*(str2
++));
215 while ((ch2
=='-') || (ch2
=='_'));
216 if (ch1
!=ch2
) return (ch1
-ch2
);
218 while (ch1
&& (str1
-stri1
<n
) && (str2
-stri2
<n
));
222 int gmx_strcasecmp(const char *str1
, const char *str2
)
228 ch1
=toupper(*(str1
++));
229 ch2
=toupper(*(str2
++));
230 if (ch1
!=ch2
) return (ch1
-ch2
);
236 int gmx_strncasecmp(const char *str1
, const char *str2
, int n
)
245 ch1
=toupper(*(str1
++));
246 ch2
=toupper(*(str2
++));
247 if (ch1
!=ch2
) return (ch1
-ch2
);
254 char *gmx_strdup(const char *src
)
258 snew(dest
,strlen(src
)+1);
265 gmx_strndup(const char *src
, int n
)
276 strncpy(dest
, src
, len
);
282 * \param[in] pattern Pattern to match against.
283 * \param[in] str String to match.
284 * \returns 0 on match, GMX_NO_WCMATCH if there is no match.
286 * Matches \p str against \p pattern, which may contain * and ? wildcards.
287 * All other characters are matched literally.
288 * Currently, it is not possible to match literal * or ?.
291 gmx_wcmatch(const char *pattern
, const char *str
)
297 /* Skip multiple wildcards in a sequence */
298 while (*pattern
== '*' || *pattern
== '?')
301 /* For ?, we need to check that there are characters left
307 return GMX_NO_WCMATCH
;
315 /* If the pattern ends after the star, we have a match */
320 /* Match the rest against each possible suffix of str */
323 /* Only do the recursive call if the first character
324 * matches. We don't have to worry about wildcards here,
325 * since we have processed them above. */
326 if (*pattern
== *str
)
329 /* Match the suffix, and return if a match or an error */
330 rc
= gmx_wcmatch(pattern
, str
);
331 if (rc
!= GMX_NO_WCMATCH
)
338 /* If no suffix of str matches, we don't have a match */
339 return GMX_NO_WCMATCH
;
341 else if ((*pattern
== '?' && *str
!= 0) || *pattern
== *str
)
347 return GMX_NO_WCMATCH
;
354 char *wrap_lines(const char *buf
,int line_width
, int indent
,bool bIndentFirst
)
357 int i
,i0
,i2
,j
,b2len
,lspace
=0,l2space
=0;
358 bool bFirst
,bFitsOnLine
;
360 /* characters are copied from buf to b2 with possible spaces changed
361 * into newlines and extra space added for indentation.
362 * i indexes buf (source buffer) and i2 indexes b2 (destination buffer)
363 * i0 points to the beginning of the current line (in buf, source)
364 * lspace and l2space point to the last space on the current line
365 * bFirst is set to prevent indentation of first line
366 * bFitsOnLine says if the first space occurred before line_width, if
367 * that is not the case, we have a word longer than line_width which
368 * will also not fit on the next line, so we might as well keep it on
369 * the current line (where it also won't fit, but looks better)
373 b2len
=strlen(buf
)+1+indent
;
377 for(i2
=0; (i2
<indent
); i2
++)
382 /* find the last space before end of line */
383 for(i
=i0
; ((i
-i0
< line_width
) || (l2space
==-1)) && (buf
[i
]); i
++) {
385 /* remember the position of a space */
390 /* if we have a newline before the line is full, reset counters */
391 if (buf
[i
]=='\n' && buf
[i
+1]) {
395 /* add indentation after the newline */
396 for(j
=0; (j
<indent
); j
++)
400 /* If we are at the last newline, copy it */
401 if (buf
[i
]=='\n' && !buf
[i
+1]) {
404 /* if we're not at the end of the string */
406 /* check if one word does not fit on the line */
407 bFitsOnLine
= (i
-i0
<= line_width
);
408 /* reset line counters to just after the space */
411 /* if the words fit on the line, and we're beyond the indentation part */
412 if ( (bFitsOnLine
) && (l2space
>= indent
) ) {
413 /* start a new line */
415 /* and add indentation */
423 for(j
=0; (j
<indent
); j
++)
425 /* no extra spaces after indent; */
437 char **split(char sep
,char *str
)
445 for(n
=0; (n
<nn
); n
++)
450 while (*str
!= '\0') {
451 while ((*str
!= '\0') && (*str
== sep
))
454 snew(ptr
[nptr
],1+strlen(str
));
456 while ((*str
!= '\0') && (*str
!= sep
)) {
472 str_to_large_int_t(const char *str
, char **endptr
)
475 gmx_large_int_t val
= 0;
486 /* Strip off initial white space */
491 /* Conform to ISO C99 - return original pointer if string does not contain a number */
503 while( ((ch
=*p
) != '\0') && isdigit(ch
) )
505 /* Important to add sign here, so we dont overflow in final multiplication */
510 /* Some sort of overflow has occured, set endptr to original string */
523 char *gmx_strsep(char **stringp
, const char *delim
)
526 int len
=strlen(delim
);
535 if ( (*stringp
)[j
] == '\0')
543 if ( (*stringp
)[j
]==delim
[i
])
546 *stringp
=*stringp
+j
+1;