NAMD
strlib.C
Go to the documentation of this file.
1 
7 /*
8  strlib contains a bunch of functions that are useful for basic
9  file input and string manipulation. See strlib.h for a list and
10  description of the functions that are available.
11 */
12 
13 #include "strlib.h"
14 
15 /************************************************************************/
16 /* */
17 /* FUNCTION NAMD_read_line */
18 /* */
19 /* INPUTS: */
20 /* fd - FILE to read line from */
21 /* buf - buffer to put line into */
22 /* */
23 /* OUTPUTS: */
24 /* this function returns a 0 if the line is read successfully or */
25 /* a -1 if an EOF is encountered */
26 /* */
27 /* NAMD_read_line reads in a line from an open file. It places */
28 /* the line that is read in into the buffer passed in via buf. If */
29 /* an EOF is encountered, a -1 is returned. If an EOF is encountered */
30 /* in the middle of a line, the program will terminate abnormally. */
31 /* Also, any comments of the form {blah blah blah} will automatically */
32 /* be skipped by NAMD_read_line, even if usch comments span lines. */
33 /* Also, the string will be left justified. That is, any spaces at */
34 /* the begining of the line will be removed. */
35 /* */
36 /************************************************************************/
37 
38 int NAMD_read_line(FILE *fd, char *buf, int bufsize)
39 
40 {
41  int i=0; // Current position in buf
42  int c; // Character read in from file
43 
44  /* Loop and read characters until we get either an EOF or a */
45  /* newline */
46  while ( ((c=fgetc(fd)) != EOF) && (c != '\n') )
47  {
48  /* If we encounter a bracketed comment, skip it. This */
49  /* basically means read EVERYTHING until the next } and*/
50  /* throw it into the big bit bucket */
51  if (c == '{')
52  {
53  while ( ((c=fgetc(fd)) != EOF) && (c!='}') )
54  {
55  }
56 
57  if (c==EOF)
58  {
59  buf[i]=STRINGNULL;
60  char *err_msg = new char[128 + strlen(buf)];
61  sprintf(err_msg, "ABNORMAL EOF FOUND - buffer=*%s*\n", buf);
62  NAMD_die(err_msg);
63  }
64 
65  continue;
66  }
67 
68  /* Also, use this little piece of logic to remove */
69  /* any leading spaces from the line */
70  if ((i>0) || !isspace(c))
71  {
72  buf[i] = c;
73  i++;
74  if ( i == bufsize ) {
75  i--;
76  buf[i]=STRINGNULL;
77  char *err_msg = new char[128 + strlen(buf)];
78  sprintf(err_msg, "BUFFER OVERRUN - buffer=*%s*\n",
79  buf);
80  NAMD_die(err_msg);
81  }
82  }
83  }
84 
85  /* NULL terminate the string */
86  buf[i]=STRINGNULL;
87 
88  /* Check for an EOF in the middle of a line */
89  if ((c==EOF) && (i!=0))
90  {
91  buf[i]=STRINGNULL;
92  char *err_msg = new char[128 + strlen(buf)];
93  sprintf(err_msg, "ABNORMAL EOF FOUND - buffer=*%s*\n", buf);
94  NAMD_die(err_msg);
95  }
96 
97  /* Return the appropriate value */
98  if (c==EOF)
99  return(-1);
100  else
101  return(0);
102 }
103 /* END OF FUNCTION NAMD_read_line */
104 
105 /************************************************************************/
106 /* */
107 /* FUNCTION NAMD_remove_comment */
108 /* */
109 /* INPUTS: */
110 /* str - String to remove comment from */
111 /* */
112 /* This function removes comments from the end of a line that */
113 /* are of the form: */
114 /* */
115 /* sample line ! This is a comment */
116 /* */
117 /************************************************************************/
118 
119 void NAMD_remove_comment(char *str)
120 
121 {
122  int i=0;
123 
124  while ( (str[i] != STRINGNULL) && (str[i] != '!') )
125  {
126  i++;
127  }
128 
129  str[i] = STRINGNULL;
130 }
131 /* END OF FUNCTION NAMD_remove_comment */
132 
133 /************************************************************************/
134 /* */
135 /* FUNCTION NAMD_truncate */
136 /* */
137 /* INPUTS: */
138 /* str - string to truncate */
139 /* */
140 /* NAMD_truncate will remove any trailing spaces from a string. */
141 /* i.e. "AB DF FG " would be truncated to "AB DF FG". */
142 /* */
143 /************************************************************************/
144 
145 void NAMD_truncate(char *str)
146 
147 {
148  int i; // Current position in str
149 
150  i=strlen(str);
151 
152  /* Loop from the back of the string until we find a non-space */
153  for (i--; i>=0 && isspace(str[i]); i--)
154  {
155  }
156 
157  str[i+1]=STRINGNULL;
158 }
159 /* END OF FUNCTION NAMD_truncate */
160 
161 /************************************************************************/
162 /* */
163 /* FUNCTION NAMD_find_word */
164 /* */
165 /* INPUTS: */
166 /* source - the string to be searched in */
167 /* search - the string to be searched for */
168 /* */
169 /* OUTPUTS: */
170 /* a 1 is returned if the string search is found within the string */
171 /* source, otherwise a 0 is returned. */
172 /* */
173 /* NAMD_find_word searches for one string within another. It is */
174 /* usually used to determine if a word appears within a given line. */
175 /* If the word is found, a 1 is returned. Otherwise, 0 is returned. */
176 /* Case is ignored while doing this search. */
177 /* */
178 /************************************************************************/
179 
180 int NAMD_find_word(const char *source, const char *search)
181 
182 {
183  int i=0; // Position inside source
184  int search_length; // Length of string search
185  int source_length; // Length of string source
186  int found=0; // Flag 1-> found the value
187 
188  search_length=strlen(search);
189  source_length=strlen(source);
190 
191  /* While we haven't found it and we haven't readched the */
192  /* point where our current position plus the length of the */
193  /* string we are looking for is longer than the string itself,*/
194  /* check the next search_length characters for a match */
195  while (!found && ((i+search_length)<=(source_length)))
196  {
197  found = (strncasecmp(source+i, search, search_length)==0);
198 
199  i++;
200  }
201 
202  return(found);
203 }
204 /* END OF FUNCTION NAMD_find_word */
205 
206 /************************************************************************/
207 /* */
208 /* FUNCTION NAMD_blank_str */
209 /* */
210 /* INPUTS: */
211 /* str - string to test */
212 /* */
213 /* OUTPUTS: */
214 /* a 1 is returned if the string is blank, otherwise a 0 is */
215 /* returned */
216 /* */
217 /* NAMD_blank_str tests to see if a string is blank. That is, */
218 /* contains only characters where isspace() is true */
219 /* */
220 /************************************************************************/
221 
222 int NAMD_blank_string(char *str)
223 {
224  int i; // Current position in str
225  int blank=1; // Flag 1-> string is blank
226  int len; // length of the string str
227 
228  len=strlen(str);
229 
230  for (i=0; i<len && blank; i++)
231  {
232  blank = isspace(str[i]);
233  }
234 
235  return(blank);
236 }
237 /* END OF FUNCTION NAMD_blank_string */
238 
239 /************************************************************************/
240 /* */
241 /* FUNCTION NAMD_find_first_word */
242 /* */
243 /* INPUTS: */
244 /* source - string to obtain word from */
245 /* word - buffer to place word into */
246 /* */
247 /* OUTPUTS: */
248 /* word is returned containing the first word of source */
249 /* */
250 /* This function finds the first word in a string. The first word */
251 /* is defined to be the first set of continuous non-space charaters */
252 /* in a string. So in the string " AB14^ FDGFD GFDG" the first */
253 /* word would be "AB14^". The word is returned in the string pointed */
254 /* to by word. */
255 /* */
256 /************************************************************************/
257 
258 void NAMD_find_first_word(char *source, char *word)
259 
260 {
261  int i=0; // Position within source
262  int j=0; // Position within word
263 
264  /* Skip leading white space */
265  while ( (source[i] != STRINGNULL) && isspace(source[i]))
266  i++;
267 
268  /* Copy the word */
269  while ( (source[i] != STRINGNULL) && !isspace(source[i]))
270  {
271  word[j]=source[i];
272  i++;
273  j++;
274  }
275 
276  word[j]=STRINGNULL;
277 
278  return;
279 }
280 /* END OF FUNCTION NAMD_find_first_word */
281 
282 /************************************************************************/
283 /* */
284 /* FUNCTION NAMD_read_int */
285 /* */
286 /* INPUTS: */
287 /* fd - file descriptor to read from */
288 /* msg - string indicating what we are trying to read */
289 /* */
290 /* OUTPUTS: */
291 /* the value of the next integer in the file is returned */
292 /* */
293 /* NAMD_read_int is used to read in integer lists from the .psf */
294 /* file. It will read the next integer it finds in the file passed */
295 /* to it. If an alpha character is encountered, the program */
296 /* terminates. If an EOF is encountered, the program terminates. */
297 /* The string msg is used to indicate what we were trying to read */
298 /* in any error messages. */
299 /* */
300 /************************************************************************/
301 
302 int NAMD_read_int(FILE *fd, const char *msg)
303 
304 {
305  int i; // Loop counter
306  int c; // Character read in from file
307  char tmp_string[11]; // Temporary string for integer
308  int isNeg;
309 
310  /* Skip white space */
311  while ( ((c=fgetc(fd)) == '\n') || isspace(c) )
312  {
313  }
314 
315  /* Check to make sure we didn't hit EOF */
316  if (c==EOF)
317  {
318  char err_msg[128];
319 
320  sprintf(err_msg, "EOF ENCOUNTERED READING %s FROM PSF FILE", msg);
321  NAMD_die(err_msg);
322  }
323 
324  /* Now read in the integer itself */
325  i=0;
326 
327  /* Modified to read an integer with '-' or '+' sign --Chao Mei */
328  isNeg = 0;
329  if(c=='-'){
330  c = fgetc(fd);
331  isNeg = 1;
332  }
333  if(c=='+')
334  c = fgetc(fd);
335 
336 
337  while (!isspace(c))
338  {
339  /* Check to make sure we only get #'s */
340  if (!isdigit(c))
341  {
342  char err_msg[128];
343 
344  sprintf(err_msg, "ALPHA CHARCTER ENCOUNTERED WHILE READING %s FROM PSF FILE", msg);
345  NAMD_die(err_msg);
346  }
347 
348  tmp_string[i] = c;
349  i++;
350 
351  c=fgetc(fd);
352 
353  /* Check to make sure we didn't hit EOF*/
354  if (c==EOF)
355  {
356  char err_msg[128];
357 
358  sprintf(err_msg, "EOF ENCOUNTERED WHILE READING %s FROM PSF FILE", msg);
359  NAMD_die(err_msg);
360  }
361  }
362 
363  tmp_string[i]=STRINGNULL;
364 
365  /* Convert the string to an integer and return its value */
366  if(isNeg)
367  return(-atoi(tmp_string));
368  else
369  return(atoi(tmp_string));
370 }
371 /* END OF FUNCTION NAMD_read_int */
372 
373 /************************************************************************/
374 /* */
375 /* FUNCTION NAMD_pad */
376 /* */
377 /* This function pads a string with leading spaces to a specified */
378 /* length. */
379 /* */
380 /************************************************************************/
381 
382 void NAMD_pad(char *str, size_t length)
383 
384 {
385  char tmp_string[128];
386  size_t i;
387 
388  if (strlen(str) >= length)
389  return;
390 
391  for (i=0; i<(length-strlen(str)); i++)
392  {
393  tmp_string[i] = ' ';
394  }
395 
396  tmp_string[i] = STRINGNULL;
397 
398  strcat(str, tmp_string);
399 }
400 /* END OF FUNCTION NAMD_pad */
401 
int NAMD_read_line(FILE *fd, char *buf, int bufsize)
Definition: strlib.C:38
void NAMD_find_first_word(char *source, char *word)
Definition: strlib.C:258
int NAMD_blank_string(char *str)
Definition: strlib.C:222
#define STRINGNULL
Definition: common.h:128
int NAMD_find_word(const char *source, const char *search)
Definition: strlib.C:180
void NAMD_die(const char *err_msg)
Definition: common.C:85
void NAMD_truncate(char *str)
Definition: strlib.C:145
void NAMD_pad(char *str, size_t length)
Definition: strlib.C:382
int NAMD_read_int(FILE *fd, const char *msg)
Definition: strlib.C:302
void NAMD_remove_comment(char *str)
Definition: strlib.C:119