4 * This source code is part of
8 * GROningen MAchine for Chemical Simulations
11 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
12 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
13 * Copyright (c) 2001-2004, The GROMACS development team,
14 * check out http://www.gromacs.org for more information.
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * If you want to redistribute modifications, please consider that
22 * scientific software is very special. Version control is crucial -
23 * bugs must be traceable. We will be happy to consider code for
24 * inclusion in the official distribution, but derived work must not
25 * be called official GROMACS. Details are found in the README & COPYING
26 * files - if they are missing, get the official version at www.gromacs.org.
28 * To help us fund GROMACS development, we humbly ask that you cite
29 * the papers on the package - you can find them in the top README file.
31 * For more info, check our website at http://www.gromacs.org
34 * GROningen Mixture of Alchemy and Childrens' Stories
36 /* This file is completely threadsafe - keep it that way! */
46 #include <sys/types.h>
58 int continuing(char *s
)
59 /* strip trailing spaces and if s ends with a CONTINUE remove that too.
60 * returns TRUE if s ends with a CONTINUE, FALSE otherwise.
67 if ((sl
> 0) && (s
[sl
-1] == CONTINUE
)) {
75 char *fgets2(char *line
, int n
, FILE *stream
)
76 /* This routine reads a string from stream of max length n
77 * and zero terminated, without newlines
78 * line should be long enough (>= n)
82 if (fgets(line
,n
,stream
)==NULL
) return NULL
;
83 if ((c
=strchr(line
,'\n'))!=NULL
) *c
=0;
87 void strip_comment (char *line
)
94 /* search for a comment mark and replace it by a zero */
95 if ((c
= strchr(line
,COMMENTSIGN
)) != NULL
)
99 void upstring (char *str
)
103 for (i
=0; (i
< (int)strlen(str
)); i
++)
104 str
[i
] = toupper(str
[i
]);
107 void ltrim (char *str
)
117 while ((tr
[c
] == ' ') || (tr
[c
] == '\t'))
124 void rtrim (char *str
)
132 while ((nul
> 0) && ((str
[nul
] == ' ') || (str
[nul
] == '\t')) ) {
138 void trim (char *str
)
144 void nice_header (FILE *out
,char *fn
)
146 char *unk
= "onbekend";
156 /* Print a nice header above the file */
158 fprintf (out
,"%c\n",COMMENTSIGN
);
159 fprintf (out
,"%c\tFile '%s' was generated\n",COMMENTSIGN
,fn
? fn
: unk
);
164 gh
= gethostname(buf
,255);
171 fprintf (out
,"%c\tBy user: %s (%d)\n",COMMENTSIGN
,
172 user
? user
: unk
,(int) uid
);
173 fprintf(out
,"%c\tOn host: %s\n",COMMENTSIGN
,(gh
== 0) ? buf
: unk
);
175 fprintf (out
,"%c\tAt date: %s",COMMENTSIGN
,ctime(&clock
));
176 fprintf (out
,"%c\n",COMMENTSIGN
);
179 int strcasecmp_min(const char *str1
, const char *str2
)
186 ch1
=toupper(*(str1
++));
187 while ((ch1
=='-') || (ch1
=='_'));
189 ch2
=toupper(*(str2
++));
190 while ((ch2
=='-') || (ch2
=='_'));
191 if (ch1
!=ch2
) return (ch1
-ch2
);
197 int strncasecmp_min(const char *str1
, const char *str2
, int n
)
207 ch1
=toupper(*(str1
++));
208 while ((ch1
=='-') || (ch1
=='_'));
210 ch2
=toupper(*(str2
++));
211 while ((ch2
=='-') || (ch2
=='_'));
212 if (ch1
!=ch2
) return (ch1
-ch2
);
214 while (ch1
&& (str1
-stri1
<n
) && (str2
-stri2
<n
));
218 int gmx_strcasecmp(const char *str1
, const char *str2
)
224 ch1
=toupper(*(str1
++));
225 ch2
=toupper(*(str2
++));
226 if (ch1
!=ch2
) return (ch1
-ch2
);
232 int gmx_strncasecmp(const char *str1
, const char *str2
, int n
)
241 ch1
=toupper(*(str1
++));
242 ch2
=toupper(*(str2
++));
243 if (ch1
!=ch2
) return (ch1
-ch2
);
250 char *gmx_strdup(const char *src
)
254 snew(dest
,strlen(src
)+1);
260 char *wrap_lines(const char *buf
,int line_width
, int indent
,bool bIndentFirst
)
263 int i
,i0
,i2
,j
,b2len
,lspace
=0,l2space
=0;
264 bool bFirst
,bFitsOnLine
;
266 /* characters are copied from buf to b2 with possible spaces changed
267 * into newlines and extra space added for indentation.
268 * i indexes buf (source buffer) and i2 indexes b2 (destination buffer)
269 * i0 points to the beginning of the current line (in buf, source)
270 * lspace and l2space point to the last space on the current line
271 * bFirst is set to prevent indentation of first line
272 * bFitsOnLine says if the first space occurred before line_width, if
273 * that is not the case, we have a word longer than line_width which
274 * will also not fit on the next line, so we might as well keep it on
275 * the current line (where it also won't fit, but looks better)
279 b2len
=strlen(buf
)+1+indent
;
283 for(i2
=0; (i2
<indent
); i2
++)
288 /* find the last space before end of line */
289 for(i
=i0
; ((i
-i0
< line_width
) || (l2space
==-1)) && (buf
[i
]); i
++) {
291 /* remember the position of a space */
296 /* if we have a newline before the line is full, reset counters */
297 if (buf
[i
]=='\n' && buf
[i
+1]) {
301 /* add indentation after the newline */
302 for(j
=0; (j
<indent
); j
++)
306 /* check if one word does not fit on the line */
307 bFitsOnLine
= (i
-i0
<= line_width
);
308 /* if we're not at the end of the string */
310 /* reset line counters to just after the space */
313 /* if the words fit on the line, and we're beyond the indentation part */
314 if ( (bFitsOnLine
) && (l2space
>= indent
) ) {
315 /* start a new line */
317 /* and add indentation */
325 for(j
=0; (j
<indent
); j
++)
327 /* no extra spaces after indent; */