strlib.C

Go to the documentation of this file.
00001 
00007 /*
00008    strlib contains a bunch of functions that are useful for basic 
00009    file input and string manipulation.  See strlib.h for a list and 
00010    description of the functions that are available.
00011 */
00012 
00013 #include "strlib.h"
00014 
00015 /************************************************************************/
00016 /*                                                                      */
00017 /*                      FUNCTION NAMD_read_line                         */
00018 /*                                                                      */
00019 /*   INPUTS:                                                            */
00020 /*      fd - FILE to read line from                                     */
00021 /*      buf - buffer to put line into                                   */
00022 /*                                                                      */
00023 /*   OUTPUTS:                                                           */
00024 /*      this function returns a 0 if the line is read successfully or   */
00025 /*   a -1 if an EOF is encountered                                      */
00026 /*                                                                      */
00027 /*      NAMD_read_line reads in a line from an open file.  It places    */
00028 /*   the line that is read in into the buffer passed in via buf.  If    */
00029 /*   an EOF is encountered, a -1 is returned.  If an EOF is encountered */
00030 /*   in the middle of a line, the program will terminate abnormally.    */
00031 /*   Also, any comments of the form {blah blah blah} will automatically */
00032 /*   be skipped by NAMD_read_line, even if usch comments span lines.    */
00033 /*   Also, the string will be left justified.  That is, any spaces at   */
00034 /*   the begining of the line will be removed.                          */
00035 /*                                                                      */
00036 /************************************************************************/
00037 
00038 int NAMD_read_line(FILE *fd, char *buf, int bufsize)
00039 
00040 {
00041         int i=0;        //  Current position in buf
00042         int c;          //  Character read in from file
00043 
00044         /*  Loop and read characters until we get either an EOF or a    */
00045         /*  newline                                                     */
00046         while ( ((c=fgetc(fd)) != EOF) && (c != '\n') )
00047         {
00048                 /*  If we encounter a bracketed comment, skip it.  This */
00049                 /*  basically means read EVERYTHING until the next } and*/
00050                 /*  throw it into the big bit bucket                    */
00051                 if (c == '{')
00052                 {
00053                         while ( ((c=fgetc(fd)) != EOF) && (c!='}') )
00054                         {
00055                         }
00056 
00057                         if (c==EOF)
00058                         {
00059                                 buf[i]=STRINGNULL;
00060                                 char *err_msg = new char[128 + strlen(buf)];
00061                                 sprintf(err_msg, "ABNORMAL EOF FOUND - buffer=*%s*\n", buf);
00062                                 NAMD_die(err_msg);
00063                         }
00064 
00065                         continue;
00066                 }
00067 
00068                 /*  Also, use this little piece of logic to remove      */
00069                 /*  any leading spaces from the line                    */
00070                 if ((i>0) || !isspace(c))
00071                 {
00072                         buf[i] = c;
00073                         i++;
00074                         if ( i == bufsize ) {
00075                                 i--;
00076                                 buf[i]=STRINGNULL;
00077                                 char *err_msg = new char[128 + strlen(buf)];
00078                                 sprintf(err_msg, "BUFFER OVERRUN - buffer=*%s*\n", 
00079                                    buf);
00080                                 NAMD_die(err_msg);
00081                         }
00082                 }
00083         }
00084 
00085         /*  NULL terminate the string                                   */
00086         buf[i]=STRINGNULL;
00087 
00088         /*  Check for an EOF in the middle of a line                    */
00089         if ((c==EOF) && (i!=0))
00090         {
00091                 buf[i]=STRINGNULL;
00092                 char *err_msg = new char[128 + strlen(buf)];
00093                 sprintf(err_msg, "ABNORMAL EOF FOUND - buffer=*%s*\n", buf);
00094                 NAMD_die(err_msg);
00095         }
00096 
00097         /*  Return the appropriate value                                */
00098         if (c==EOF)
00099                 return(-1);
00100         else
00101                 return(0);
00102 }
00103 /*                      END OF FUNCTION NAMD_read_line                  */
00104 
00105 /************************************************************************/
00106 /*                                                                      */
00107 /*                      FUNCTION NAMD_remove_comment                    */
00108 /*                                                                      */
00109 /*   INPUTS:                                                            */
00110 /*      str - String to remove comment from                             */
00111 /*                                                                      */
00112 /*      This function removes comments from the end of a line that      */
00113 /*   are of the form:                                                   */
00114 /*                                                                      */
00115 /*      sample line             ! This is a comment                     */
00116 /*                                                                      */
00117 /************************************************************************/
00118 
00119 void NAMD_remove_comment(char *str)
00120 
00121 {
00122         int i=0;
00123 
00124         while ( (str[i] != STRINGNULL) && (str[i] != '!') )
00125         {
00126                 i++;
00127         }
00128 
00129         str[i] = STRINGNULL;
00130 }
00131 /*                      END OF FUNCTION NAMD_remove_comment             */
00132 
00133 /************************************************************************/
00134 /*                                                                      */
00135 /*                      FUNCTION NAMD_truncate                          */
00136 /*                                                                      */
00137 /*   INPUTS:                                                            */
00138 /*      str - string to truncate                                        */
00139 /*                                                                      */
00140 /*      NAMD_truncate will remove any trailing spaces from a string.    */
00141 /*   i.e.  "AB DF FG     "  would be truncated to "AB DF FG".           */
00142 /*                                                                      */
00143 /************************************************************************/
00144 
00145 void NAMD_truncate(char *str)
00146 
00147 {
00148         int i;          //  Current position in str
00149 
00150         i=strlen(str);
00151 
00152         /*  Loop from the back of the string until we find a non-space  */
00153         for (i--; i>=0 && isspace(str[i]); i--)
00154         {
00155         }
00156         
00157         str[i+1]=STRINGNULL;
00158 }
00159 /*                      END OF FUNCTION NAMD_truncate                   */
00160 
00161 /************************************************************************/
00162 /*                                                                      */
00163 /*                      FUNCTION NAMD_find_word                         */
00164 /*                                                                      */
00165 /*   INPUTS:                                                            */
00166 /*      source - the string to be searched in                           */
00167 /*      search - the string to be searched for                          */
00168 /*                                                                      */
00169 /*   OUTPUTS:                                                           */
00170 /*      a 1 is returned if the string search is found within the string */
00171 /*   source, otherwise a 0 is returned.                                 */
00172 /*                                                                      */
00173 /*      NAMD_find_word searches for one string within another.  It is   */
00174 /*   usually used to determine if a word appears within a given line.   */
00175 /*   If the word is found, a 1 is returned.  Otherwise, 0 is returned.  */
00176 /*   Case is ignored while doing this search.                           */
00177 /*                                                                      */
00178 /************************************************************************/
00179 
00180 int NAMD_find_word(const char *source, const char *search)
00181 
00182 {
00183         int i=0;                //  Position inside source
00184         int search_length;      //  Length of string search
00185         int source_length;      //  Length of string source
00186         int found=0;            //  Flag 1-> found the value
00187 
00188         search_length=strlen(search);
00189         source_length=strlen(source);
00190 
00191         /*  While we haven't found it and we haven't readched the      */
00192         /*  point where our current position plus the length of the    */
00193         /*  string we are looking for is longer than the string itself,*/
00194         /*  check the next search_length characters for a match        */
00195         while (!found && ((i+search_length)<=(source_length)))
00196         {
00197                 found = (strncasecmp(source+i, search, search_length)==0);
00198 
00199                 i++;
00200         }
00201 
00202         return(found);
00203 }
00204 /*                      END OF FUNCTION NAMD_find_word                  */
00205 
00206 /************************************************************************/
00207 /*                                                                      */
00208 /*                      FUNCTION NAMD_blank_str                         */
00209 /*                                                                      */
00210 /*   INPUTS:                                                            */
00211 /*      str - string to test                                            */
00212 /*                                                                      */
00213 /*   OUTPUTS:                                                           */
00214 /*      a 1 is returned if the string is blank, otherwise a 0 is        */
00215 /*   returned                                                           */
00216 /*                                                                      */
00217 /*      NAMD_blank_str tests to see if a string is blank.  That is,     */
00218 /*   contains only characters where isspace() is true                   */
00219 /*                                                                      */
00220 /************************************************************************/
00221 
00222 int NAMD_blank_string(char *str)
00223 {
00224         int i;          // Current position in str
00225         int blank=1;    // Flag 1-> string is blank
00226         int len;        // length of the string str
00227 
00228         len=strlen(str);
00229 
00230         for (i=0; i<len && blank; i++)
00231         {
00232                 blank = isspace(str[i]);
00233         }
00234 
00235         return(blank);
00236 }
00237 /*                      END OF FUNCTION NAMD_blank_string               */
00238 
00239 /************************************************************************/
00240 /*                                                                      */
00241 /*                      FUNCTION NAMD_find_first_word                   */
00242 /*                                                                      */
00243 /*   INPUTS:                                                            */
00244 /*      source - string to obtain word from                             */
00245 /*      word - buffer to place word into                                */
00246 /*                                                                      */
00247 /*   OUTPUTS:                                                           */
00248 /*      word is returned containing the first word of source            */
00249 /*                                                                      */
00250 /*      This function finds the first word in a string.  The first word */
00251 /*   is defined to be the first set of continuous non-space charaters   */
00252 /*   in a string.  So in the string "   AB14^  FDGFD GFDG"  the first   */
00253 /*   word would be "AB14^".  The word is returned in the string pointed */
00254 /*   to by word.                                                        */
00255 /*                                                                      */
00256 /************************************************************************/
00257 
00258 void NAMD_find_first_word(char *source, char *word)
00259 
00260 {
00261         int i=0;        //  Position within source
00262         int j=0;        //  Position within word
00263 
00264         /*  Skip leading white space                                    */
00265         while ( (source[i] != STRINGNULL) && isspace(source[i]))
00266                 i++;
00267 
00268         /*  Copy the word                                               */
00269         while ( (source[i] != STRINGNULL) && !isspace(source[i]))
00270         {
00271                 word[j]=source[i];
00272                 i++;
00273                 j++;
00274         }
00275 
00276         word[j]=STRINGNULL;
00277 
00278         return;
00279 }
00280 /*                      END OF FUNCTION NAMD_find_first_word            */
00281 
00282 /************************************************************************/
00283 /*                                                                      */
00284 /*                      FUNCTION NAMD_read_int                          */
00285 /*                                                                      */
00286 /*   INPUTS:                                                            */
00287 /*      fd - file descriptor to read from                               */
00288 /*      msg - string indicating what we are trying to read              */
00289 /*                                                                      */
00290 /*   OUTPUTS:                                                           */
00291 /*      the value of the next integer in the file is returned           */
00292 /*                                                                      */
00293 /*      NAMD_read_int is used to read in integer lists from the .psf    */
00294 /*   file.  It will read the next integer it finds in the file passed   */
00295 /*   to it.  If an alpha character is encountered, the program          */
00296 /*   terminates.  If an EOF is encountered, the program terminates.     */
00297 /*   The string msg is used to indicate what we were trying to read     */
00298 /*   in any error messages.                                             */
00299 /*                                                                      */
00300 /************************************************************************/
00301 
00302 int NAMD_read_int(FILE *fd, const char *msg)
00303 
00304 {
00305         int i;                  //  Loop counter
00306         int c;                  //  Character read in from file
00307         char tmp_string[11];    //  Temporary string for integer
00308         int isNeg;
00309     
00310         /*  Skip white space                            */
00311         while ( ((c=fgetc(fd)) == '\n') || isspace(c) )
00312         {
00313         }
00314 
00315         /*  Check to make sure we didn't hit EOF        */
00316         if (c==EOF)
00317         {
00318                 char err_msg[128];
00319 
00320                 sprintf(err_msg, "EOF ENCOUNTERED READING %s FROM PSF FILE", msg);
00321                 NAMD_die(err_msg);
00322         }
00323 
00324         /*  Now read in the integer itself              */
00325         i=0;
00326 
00327         /* Modified to read an integer with '-' or '+' sign --Chao Mei */
00328         isNeg = 0;
00329         if(c=='-'){
00330             c = fgetc(fd);
00331             isNeg = 1;
00332         }
00333         if(c=='+')
00334             c = fgetc(fd);
00335                 
00336 
00337         while (!isspace(c))
00338         {
00339                 /*  Check to make sure we only get #'s  */
00340                 if (!isdigit(c))
00341                 {
00342                         char err_msg[128];
00343 
00344                         sprintf(err_msg, "ALPHA CHARCTER ENCOUNTERED WHILE READING %s FROM PSF FILE", msg);
00345                         NAMD_die(err_msg);
00346                 }
00347 
00348                 tmp_string[i] = c;
00349                 i++;
00350 
00351                 c=fgetc(fd);
00352 
00353                 /*  Check to make sure we didn't hit EOF*/
00354                 if (c==EOF)
00355                 {
00356                         char err_msg[128];
00357 
00358                         sprintf(err_msg, "EOF ENCOUNTERED WHILE READING %s FROM PSF FILE", msg);
00359                         NAMD_die(err_msg);
00360                 }
00361         }
00362 
00363         tmp_string[i]=STRINGNULL;
00364 
00365         /*  Convert the string to an integer and return its value       */
00366         if(isNeg)
00367             return(-atoi(tmp_string));
00368         else
00369            return(atoi(tmp_string));
00370 }
00371 /*                      END OF FUNCTION NAMD_read_int                   */
00372 
00373 /************************************************************************/
00374 /*                                                                      */
00375 /*                      FUNCTION NAMD_pad                               */
00376 /*                                                                      */
00377 /*      This function pads a string with leading spaces to a specified  */
00378 /*   length.                                                            */
00379 /*                                                                      */
00380 /************************************************************************/
00381 
00382 void NAMD_pad(char *str, size_t length)
00383 
00384 {
00385         char tmp_string[128];
00386         size_t i;
00387 
00388         if (strlen(str) >= length)
00389                 return;
00390 
00391         for (i=0; i<(length-strlen(str)); i++)
00392         {
00393                 tmp_string[i] = ' ';
00394         }
00395 
00396         tmp_string[i] = STRINGNULL;
00397 
00398         strcat(str, tmp_string);
00399 }
00400 /*                      END OF FUNCTION NAMD_pad                        */
00401 

Generated on Mon Nov 20 01:17:14 2017 for NAMD by  doxygen 1.4.7