changed reading hint
[gromacs/adressmacs.git] / src / gmxlib / string2.c
blobe23930d253aa9db71209983cac9d69c0401a880d
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 2.0
12 * Copyright (c) 1991-1999
13 * BIOSON Research Institute, Dept. of Biophysical Chemistry
14 * University of Groningen, The Netherlands
16 * Please refer to:
17 * GROMACS: A message-passing parallel molecular dynamics implementation
18 * H.J.C. Berendsen, D. van der Spoel and R. van Drunen
19 * Comp. Phys. Comm. 91, 43-56 (1995)
21 * Also check out our WWW page:
22 * http://md.chem.rug.nl/~gmx
23 * or e-mail to:
24 * gromacs@chem.rug.nl
26 * And Hey:
27 * Green Red Orange Magenta Azure Cyan Skyblue
29 static char *SRCID_string2_c = "$Id$";
31 #include <stdio.h>
32 #include <ctype.h>
33 #include <stdlib.h>
34 #include <sys/types.h>
35 #ifndef NO_PWUID
36 #include <pwd.h>
37 #endif
38 #include <time.h>
40 #include "typedefs.h"
41 #include "smalloc.h"
42 #include "fatal.h"
43 #include "macros.h"
44 #include "string2.h"
46 int continuing(char *s)
47 /* strip trailing spaces and if s ends with a CONTINUE remove that too.
48 * returns TRUE if s ends with a CONTINUE, FALSE otherwise.
51 int sl;
53 rtrim(s);
54 sl = strlen(s);
55 if ((sl > 0) && (s[sl-1] == CONTINUE)) {
56 s[sl-1] = 0;
57 return TRUE;
59 else
60 return FALSE;
63 char *fgets2(char *line, int n, FILE *stream)
64 /* This routine reads a string from stream of max length n
65 * and zero terminated, without newlines
66 * line should be long enough (>= n)
69 char *c;
70 if (fgets(line,n,stream)==NULL) return NULL;
71 if ((c=strchr(line,'\n'))!=NULL) *c=0;
72 return line;
75 void strip_comment (char *line)
77 char *c;
79 if (!line)
80 return;
82 /* search for a comment mark and replace it by a zero */
83 if ((c = strchr(line,COMMENTSIGN)) != NULL)
84 (*c) = 0;
87 void upstring (char *str)
89 int i;
91 for (i=0; (i < (int)strlen(str)); i++)
92 str[i] = toupper(str[i]);
95 void ltrim (char *str)
97 char *tr;
98 int c;
100 if (!str)
101 return;
103 tr = strdup (str);
104 c = 0;
105 while ((tr[c] == ' ') || (tr[c] == '\t'))
106 c++;
108 strcpy (str,tr+c);
109 free (tr);
112 void rtrim (char *str)
114 int nul;
116 if (!str)
117 return;
119 nul = strlen(str)-1;
120 while ((nul > 0) && ((str[nul] == ' ') || (str[nul] == '\t')) ) {
121 str[nul] = '\0';
122 nul--;
126 void trim (char *str)
128 ltrim (str);
129 rtrim (str);
132 void nice_header (FILE *out,char *fn)
134 time_t clock;
135 #ifndef NO_PWUID
136 struct passwd *pw;
137 uid_t uid;
138 #endif
140 /* Print a nice header above the file */
141 clock = time (0);
142 fprintf (out,"%c\n",COMMENTSIGN);
143 #ifndef NO_PWUID
144 uid = getuid();
145 if ((pw = getpwuid(uid)) != NULL)
146 fprintf (out,"%c\tUser %s (%d)\n",COMMENTSIGN,pw->pw_name,(int) uid);
147 else
148 fprintf (out,"%c\tUser %d not in password file\n",COMMENTSIGN,(int) uid);
149 #endif
150 fprintf (out,"%c\t%s",COMMENTSIGN,ctime(&clock));
151 fprintf (out,"%c\t%s\n",COMMENTSIGN,fn);
152 fprintf (out,"%c\n",COMMENTSIGN);
155 int gmx_strcasecmp(const char *str1, const char *str2)
157 char ch1,ch2;
162 ch1=toupper(*(str1++));
163 while ((ch1=='-') || (ch1=='_'));
165 ch2=toupper(*(str2++));
166 while ((ch2=='-') || (ch2=='_'));
167 if (ch1!=ch2) return (ch1-ch2);
169 while (ch1);
170 return 0;
173 char *gmx_strdup(const char *src)
175 char *dest;
177 snew(dest,strlen(src)+1);
178 strcpy(dest,src);
180 return dest;
183 char *wrap_lines(char *buf,int line_width, int indent)
185 char *b2;
186 int i,i0,i2,j,b2len,lspace=0,l2space=0;
187 bool bFirst,bFitsOnLine;
189 /* characters are copied from buf to b2 with possible spaces changed
190 * into newlines and extra space added for indentation.
191 * i indexes buf (source buffer) and i2 indexes b2 (destination buffer)
192 * i0 points to the beginning of the current line (in buf, source)
193 * lspace and l2space point to the last space on the current line
194 * bFirst is set to prevent indentation of first line
195 * bFitsOnLine says if the first space occurred before line_width, if
196 * that is not the case, we have a word longer than line_width which
197 * will also not fit on the next line, so we might as well keep it on
198 * the current line (where it also won't fit, but looks better)
201 b2=NULL;
202 b2len=strlen(buf)+1;
203 snew(b2,b2len);
204 i0=0;
205 i2=0;
206 bFirst=TRUE;
207 do {
208 l2space = -1;
209 /* find the last space before end of line */
210 for(i=i0; ((i-i0 < line_width) || (l2space==-1)) && (buf[i]); i++) {
211 b2[i2++] = buf[i];
212 /* remember the position of a space */
213 if (buf[i] == ' ') {
214 lspace = i;
215 l2space = i2-1;
217 /* if we have a newline before the line is full, reset counters */
218 if (buf[i]=='\n' && buf[i+1]) {
219 i0=i+1;
220 b2len+=indent;
221 srenew(b2, b2len);
222 /* add indentation after the newline */
223 for(j=0; (j<indent); j++)
224 b2[i2++]=' ';
227 /* check if one word does not fit on the line */
228 bFitsOnLine = (i-i0 <= line_width);
229 /* if we're not at the end of the string */
230 if (buf[i]) {
231 /* reset line counters to just after the space */
232 i0 = lspace+1;
233 i2 = l2space+1;
234 /* if the words fit on the line, and we're beyond the indentation part */
235 if ( (bFitsOnLine) && (l2space >= indent) ) {
236 /* start a new line */
237 b2[l2space] = '\n';
238 /* and add indentation */
239 if (indent) {
240 if (bFirst) {
241 line_width-=indent;
242 bFirst=FALSE;
244 b2len+=indent;
245 srenew(b2, b2len);
246 for(j=0; (j<indent); j++)
247 b2[i2++]=' ';
248 /* no extra spaces after indent; */
249 while(buf[i0]==' ')
250 i0++;
254 } while (buf[i]);
255 b2[i2] = '\0';
257 return b2;