home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / educaton / inf_src.arc / PUTSTRIN.C < prev    next >
C/C++ Source or Header  |  1986-03-14  |  3KB  |  165 lines

  1.  
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "expert.h"
  5.  
  6.  
  7. /*****************************************************************
  8. **
  9. **    PUT STRING
  10. **
  11. **    looks for string on rule line and
  12. **        either returns the offset into the
  13. **        string buffer if found or places
  14. **        it into the string buffer and returns
  15. **        the offset to it.
  16. **    strBuffOfst is the pointer to the offset to
  17. **        the last used location in the string buffer
  18. **
  19. **
  20. *****************************************************************/
  21.  
  22. int    putString (infile,strBuff,strBuffOfst) 
  23.  
  24.     FILE    *infile ;
  25.     char    *strBuff ;
  26.     int    *strBuffOfst ;
  27.  
  28. {
  29. int    i,offSet,strLen,c,newline ;
  30. char    buff[MAX_STR_LEN] ;
  31.  
  32. #ifdef    DEBUG
  33.     fprintf(stdout,"\nDEBUG-PUTSTRING strBuffOfst= %d",*strBuffOfst) ;
  34. #endif
  35.  
  36. /*
  37. **
  38. **    get file positioned to first non blank character (after keyword)
  39. **
  40. */
  41.  
  42. while( ( c=getc(infile)) == BLANK )
  43.     putchar(c) ;
  44. if( c == EOL )
  45.     {
  46.     putchar(c) ;
  47.     return (LINE_ERROR) ;
  48.     }
  49. if( c == EOF )
  50.     return (KEY_EOF) ;
  51. ungetc(c,infile);
  52.  
  53. #ifdef    DEBUG
  54.     fprintf(stdout,"\nDEBUG-PUTSTRING c = %c", c) ;
  55. #endif
  56. /*
  57. **
  58. **     load the string on the line into the string buffer for 
  59. **    later comparison to strings in the line buffer.
  60. **
  61. */
  62. newline = FALSE ;
  63. for( i = 0 ; i < (MAX_STR_LEN-1) ; i++ )
  64.     {
  65.     buff[i]=c=getc(infile) ;
  66.  
  67. #ifdef    DEBUG
  68.     fprintf(stdout,"\nDEBUG-PUTSTRING c =%c, i = %d -- ", c,i) ;
  69. #endif
  70.     putchar(c) ;
  71.     if(c == '\\')
  72.         {
  73. #ifdef    DEBUG
  74.     fprintf(stdout,"\nDEBUG-PUTSTRING newline is TRUE");
  75. #endif
  76.         newline = TRUE ;
  77.         continue ;
  78.         }
  79.     if( newline == TRUE )
  80.         {
  81.         if( c == 'n' ) 
  82.             {
  83.             i -= 1 ;
  84.             buff[i] = '\n' ;
  85.             }
  86. #ifdef    DEBUG
  87.         fprintf(stdout,"\nDEBUG-PUTSTRING newline is FALSE");
  88. #endif
  89.         newline = FALSE ;
  90.         continue ;
  91.         }
  92.     if(c == EOL)
  93.         {
  94.         buff[i]=0 ;
  95.         break ;
  96.         }
  97.     if(c == EOF)
  98.         {
  99.         buff[i]=0 ;
  100.         ungetc(c,infile) ;
  101.         break ;
  102.         }
  103.     }
  104.  
  105. /*
  106. **    remove trailing blanks in the buffer 
  107. **
  108. */
  109. for( --i ; i > -1 ; i-- )
  110.     {
  111.     if( buff[i] != BLANK )
  112.         break ;
  113.     buff[i]=0 ;
  114.     }    
  115. if(i <= 0)
  116.     return(STR_LEN_ERROR) ;
  117. strLen = i ;
  118.  
  119. buff[MAX_STR_LEN-1] = 0 ;
  120.  
  121. /*
  122. **    at this point strLen should be the offset to the last character
  123. **    in the string and is also, therefore, the length of the string -1
  124. **
  125. **    search for the string in the string buffer
  126. **    return offset to string if found else put the string
  127. **    in the string buffer and uptate buffer offset returning
  128. **    pointer to new string
  129. **
  130. */
  131.  
  132. offSet = 0 ;
  133. while( offSet < *strBuffOfst )
  134.     {
  135.     if( strcmp(&strBuff[offSet], buff) == 0 )
  136.         return(offSet) ;
  137.     while( strBuff[++offSet] ) ;       /* look for end of null trmntd string */
  138.     ++offSet ;            /* move past the null to next string  */
  139.     }
  140. #ifdef    DEBUG
  141.     fprintf(stdout,"\nDEBUG-PUTSTRING strBuffOfst= %d",*strBuffOfst) ;
  142.     fprintf(stdout,"\nDEBUG-PUTSTRING offSet = %d",offSet) ;
  143.     fprintf(stdout,"\nDEBUG-PUTSTRING strBuff= %s",&strBuff[offSet]);
  144.     fprintf(stdout,"\nDEBUG-PUTSTRING buff   = %s",buff) ;
  145. #endif
  146. strcpy(&strBuff[*strBuffOfst],buff) ;
  147. if(*strBuffOfst == 0 )
  148.     {
  149.     *strBuffOfst += strLen + 2 ;
  150. #ifdef    DEBUG
  151.     fprintf(stdout,"\nDEBUG-PUTSTRING strBuffOfst= %d",*strBuffOfst) ;
  152. #endif
  153.     return(0) ;
  154.     }
  155. else
  156.     {
  157.     offSet = *strBuffOfst ;
  158.     *strBuffOfst += strLen + 2 ;
  159. #ifdef    DEBUG
  160.     fprintf(stdout,"\nDEBUG-PUTSTRING strBuffOfst= %d",*strBuffOfst) ;
  161. #endif
  162.     return(offSet) ;
  163.     }
  164. }
  165.