home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 241_01 / putstrin.cr < prev    next >
Text File  |  1987-08-31  |  4KB  |  157 lines

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