NAMD
Functions
strlib.h File Reference
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "common.h"

Go to the source code of this file.

Functions

void NAMD_truncate (char *)
 
int NAMD_read_line (FILE *, char *, int bufsize=512)
 
int NAMD_find_word (const char *, const char *)
 
int NAMD_blank_string (char *)
 
void NAMD_find_first_word (char *, char *)
 
int NAMD_read_int (FILE *, const char *)
 
void NAMD_pad (char *, size_t)
 
void NAMD_remove_comment (char *)
 

Function Documentation

int NAMD_blank_string ( char *  )

Definition at line 222 of file strlib.C.

Referenced by Molecule::prepare_qm(), Parameters::read_charmm_parameter_file(), Molecule::read_go_file(), and Parameters::read_parameter_file().

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 }
void NAMD_find_first_word ( char *  ,
char *   
)

Definition at line 258 of file strlib.C.

References STRINGNULL.

Referenced by Parameters::read_charmm_parameter_file(), Molecule::read_go_file(), and Parameters::read_parameter_file().

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 }
#define STRINGNULL
Definition: common.h:128
int NAMD_find_word ( const char *  ,
const char *   
)

Definition at line 180 of file strlib.C.

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 }
void NAMD_pad ( char *  ,
size_t   
)

Definition at line 382 of file strlib.C.

References STRINGNULL.

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 }
#define STRINGNULL
Definition: common.h:128
int NAMD_read_int ( FILE *  ,
const char *   
)

Definition at line 302 of file strlib.C.

References NAMD_die(), and STRINGNULL.

Referenced by Molecule::read_alch_unpert_angles(), Molecule::read_alch_unpert_bonds(), and Molecule::read_alch_unpert_dihedrals().

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 }
#define STRINGNULL
Definition: common.h:128
void NAMD_die(const char *err_msg)
Definition: common.C:85
int NAMD_read_line ( FILE *  fd,
char *  buf,
int  bufsize 
)

Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 by The Board of Trustees of the University of Illinois. All rights reserved.

Definition at line 38 of file strlib.C.

References NAMD_die(), and STRINGNULL.

Referenced by Molecule::prepare_qm(), Parameters::read_charmm_parameter_file(), Molecule::read_go_file(), and Parameters::read_parameter_file().

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 }
#define STRINGNULL
Definition: common.h:128
void NAMD_die(const char *err_msg)
Definition: common.C:85
void NAMD_remove_comment ( char *  )

Definition at line 119 of file strlib.C.

References STRINGNULL.

121 {
122  int i=0;
123 
124  while ( (str[i] != STRINGNULL) && (str[i] != '!') )
125  {
126  i++;
127  }
128 
129  str[i] = STRINGNULL;
130 }
#define STRINGNULL
Definition: common.h:128
void NAMD_truncate ( char *  )

Copyright (c) 1995, 1996, 1997, 1998, 1999, 2000 by The Board of Trustees of the University of Illinois. All rights reserved.

Definition at line 145 of file strlib.C.

References STRINGNULL.

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 }
#define STRINGNULL
Definition: common.h:128