Upped the version to 3.2.0
[gromacs.git] / src / gmxlib / string2.c
blobdf59eb7e553b6aad9bc8a7a42c6029e10e813ca5
1 /*
2 * $Id$
3 *
4 * This source code is part of
5 *
6 * G R O M A C S
7 *
8 * GROningen MAchine for Chemical Simulations
9 *
10 * VERSION 3.2.0
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
33 * And Hey:
34 * GROningen Mixture of Alchemy and Childrens' Stories
36 /* This file is completely threadsafe - keep it that way! */
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
43 #include <stdio.h>
44 #include <ctype.h>
45 #include <stdlib.h>
46 #include <sys/types.h>
47 #ifndef NO_PWUID
48 #include <pwd.h>
49 #endif
50 #include <time.h>
52 #include "typedefs.h"
53 #include "smalloc.h"
54 #include "fatal.h"
55 #include "macros.h"
56 #include "string2.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.
63 int sl;
65 rtrim(s);
66 sl = strlen(s);
67 if ((sl > 0) && (s[sl-1] == CONTINUE)) {
68 s[sl-1] = 0;
69 return TRUE;
71 else
72 return FALSE;
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)
81 char *c;
82 if (fgets(line,n,stream)==NULL) return NULL;
83 if ((c=strchr(line,'\n'))!=NULL) *c=0;
84 return line;
87 void strip_comment (char *line)
89 char *c;
91 if (!line)
92 return;
94 /* search for a comment mark and replace it by a zero */
95 if ((c = strchr(line,COMMENTSIGN)) != NULL)
96 (*c) = 0;
99 void upstring (char *str)
101 int i;
103 for (i=0; (i < (int)strlen(str)); i++)
104 str[i] = toupper(str[i]);
107 void ltrim (char *str)
109 char *tr;
110 int c;
112 if (!str)
113 return;
115 tr = strdup (str);
116 c = 0;
117 while ((tr[c] == ' ') || (tr[c] == '\t'))
118 c++;
120 strcpy (str,tr+c);
121 free (tr);
124 void rtrim (char *str)
126 int nul;
128 if (!str)
129 return;
131 nul = strlen(str)-1;
132 while ((nul > 0) && ((str[nul] == ' ') || (str[nul] == '\t')) ) {
133 str[nul] = '\0';
134 nul--;
138 void trim (char *str)
140 ltrim (str);
141 rtrim (str);
144 void nice_header (FILE *out,char *fn)
146 char *unk = "onbekend";
147 time_t clock;
148 char *user=NULL;
149 int gh;
150 uid_t uid;
151 char buf[256];
152 #ifndef NO_PWUID
153 struct passwd *pw;
154 #endif
156 /* Print a nice header above the file */
157 clock = time (0);
158 fprintf (out,"%c\n",COMMENTSIGN);
159 fprintf (out,"%c\tFile '%s' was generated\n",COMMENTSIGN,fn ? fn : unk);
161 #ifndef NO_PWUID
162 uid = getuid();
163 pw = getpwuid(uid);
164 gh = gethostname(buf,255);
165 user= pw->pw_name;
166 #else
167 uid = 0;
168 gh = -1;
169 #endif
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)
181 char ch1,ch2;
186 ch1=toupper(*(str1++));
187 while ((ch1=='-') || (ch1=='_'));
189 ch2=toupper(*(str2++));
190 while ((ch2=='-') || (ch2=='_'));
191 if (ch1!=ch2) return (ch1-ch2);
193 while (ch1);
194 return 0;
197 int strncasecmp_min(const char *str1, const char *str2, int n)
199 char ch1,ch2;
200 char *stri1, *stri2;
202 stri1=(char *)str1;
203 stri2=(char *)str2;
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));
215 return 0;
218 int gmx_strcasecmp(const char *str1, const char *str2)
220 char ch1,ch2;
224 ch1=toupper(*(str1++));
225 ch2=toupper(*(str2++));
226 if (ch1!=ch2) return (ch1-ch2);
228 while (ch1);
229 return 0;
232 int gmx_strncasecmp(const char *str1, const char *str2, int n)
234 char ch1,ch2;
236 if(n==0)
237 return 0;
241 ch1=toupper(*(str1++));
242 ch2=toupper(*(str2++));
243 if (ch1!=ch2) return (ch1-ch2);
244 n--;
246 while (ch1 && n);
247 return 0;
250 char *gmx_strdup(const char *src)
252 char *dest;
254 snew(dest,strlen(src)+1);
255 strcpy(dest,src);
257 return dest;
260 char *wrap_lines(const char *buf,int line_width, int indent,bool bIndentFirst)
262 char *b2;
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)
278 b2=NULL;
279 b2len=strlen(buf)+1+indent;
280 snew(b2,b2len);
281 i0=i2=0;
282 if (bIndentFirst)
283 for(i2=0; (i2<indent); i2++)
284 b2[i2] = ' ';
285 bFirst=TRUE;
286 do {
287 l2space = -1;
288 /* find the last space before end of line */
289 for(i=i0; ((i-i0 < line_width) || (l2space==-1)) && (buf[i]); i++) {
290 b2[i2++] = buf[i];
291 /* remember the position of a space */
292 if (buf[i] == ' ') {
293 lspace = i;
294 l2space = i2-1;
296 /* if we have a newline before the line is full, reset counters */
297 if (buf[i]=='\n' && buf[i+1]) {
298 i0=i+1;
299 b2len+=indent;
300 srenew(b2, b2len);
301 /* add indentation after the newline */
302 for(j=0; (j<indent); j++)
303 b2[i2++]=' ';
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 */
309 if (buf[i]) {
310 /* reset line counters to just after the space */
311 i0 = lspace+1;
312 i2 = l2space+1;
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 */
316 b2[l2space] = '\n';
317 /* and add indentation */
318 if (indent) {
319 if (bFirst) {
320 line_width-=indent;
321 bFirst=FALSE;
323 b2len+=indent;
324 srenew(b2, b2len);
325 for(j=0; (j<indent); j++)
326 b2[i2++]=' ';
327 /* no extra spaces after indent; */
328 while(buf[i0]==' ')
329 i0++;
333 } while (buf[i]);
334 b2[i2] = '\0';
336 return b2;