home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c081_11 / 6.ddi / EXAMPLES.ZIP / RC2MSG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-13  |  10.8 KB  |  300 lines

  1. /*
  2.    EXAMPLE SOURCE CODE FOR RC FILTER
  3.  
  4.    RC2Msg.C
  5.    Copyright (c) 1991 Borland International, Inc.
  6.    All rights reserved.
  7.  
  8.    RC2Msg - Resource compiler output filter to Turbo C++ IDE message window
  9.  
  10.    This filter accepts input through the standard input stream, converts
  11.    it and outputs it to the standard output stream.  The streams are linked
  12.    through pipes, such that the input stream is the output from the resource
  13.    compiler being invoked, and the output stream is connected to the message
  14.    window of the Turbo C++ IDE, ie.
  15.  
  16.           rc fname | rc2msg | Turbo C++ message window
  17.  
  18.    Compile using Turbo C++ in the LARGE memory model
  19.  
  20.    tcc -ml rc2msg
  21. */
  22.  
  23. #include <dir.h>
  24. #include <dos.h>
  25. #include <stdlib.h>
  26. #include <fcntl.h>
  27. #include <string.h>
  28. #include <alloc.h>
  29. #include <io.h>
  30. #include "filter.h"
  31.  
  32. #define TRUE (1 == 1)
  33. #define FALSE !(TRUE)
  34.  
  35. char     CurFile[MAXPATH];       /* Current file in message window */
  36. unsigned BufSize,                /* Size of internal working buffer */
  37.          CurBufLen;              /* Buffer space in use */
  38. char     *InBuffer,              /* Input buffer */
  39.          *OutBuffer,             /* Output buffer */
  40.          *CurInPtr,              /* current character in input buffer */
  41.          *CurOutPtr,             /* current character in output */
  42.          *LinePtr;               /* pointer to the current character in
  43.                                     the input buffer */
  44. char     Line[133];              /* static buffer to store line most recently
  45.                                     input */
  46. long int InOff;                  /* position in actual input stream */
  47. char     EndMark;                /* Used to output end of data to message
  48.                                     window */
  49.  
  50. /*************************************************************************
  51. Function  : NextChar
  52. Parameters: none
  53. Returns   : next character in input buffer or 0 on end of file
  54.  
  55. Input from the standard input stream is buffered in a global buffer InBuffer
  56. which is allocated in function main.  The function will return
  57. the next character in the buffer, reading from the input stream when the
  58. buffer becomes empty.
  59. **************************************************************************/
  60. char NextChar(void)
  61. {
  62.   if (CurInPtr < InBuffer+CurBufLen)  /* if buffer is not empty */
  63.   {
  64.     return *(CurInPtr++);             /* return next character */
  65.   }
  66.   else
  67.   {
  68.     CurInPtr = InBuffer;              /* reset pointer to front of buffer */
  69.     lseek(0,InOff,0);                 /* seek to the next section for read */
  70.     InOff += BufSize;                 /* increment pointer to next block */
  71.     if ((CurBufLen = read(0,InBuffer,BufSize)) !=0)
  72.       return NextChar();              /* recursive call merely returns
  73.                                          first character in buffer after read */
  74.     return 0;                         /* return 0 on end of file */
  75.   }
  76. }
  77.  
  78. /*************************************************************************
  79. Function  : flushOut
  80. Parameter : Size   The number of characters to be written out
  81. Returns   : nothing
  82.  
  83. Strings to be sent to the message window are buffered in a buffer called
  84. OutBuffer.  A call to this function will write out Size bytes from OutBuffer
  85. to the standard output stream and resets the output buffer pointer to the
  86. beginning of the buffer.  The output buffer is considered empty after a call
  87. to this function.
  88. *************************************************************************/
  89. void flushOut(unsigned Size)
  90. {
  91.    if (Size != 0)                /* don't flush an empty buffer */
  92.    {
  93.       CurOutPtr = OutBuffer;     /* reset pointer to beginning of buffer */
  94.       lseek(1,0,2);              /* seek output stream to end */
  95.       write(1,OutBuffer,Size);   /* write out Size bytes */
  96.    }
  97. }
  98.  
  99. /************************************************************************
  100. Function  : Put
  101. Parameters: S    pointer to bytes being put into output buffer
  102.             Len  number of bytes to be put in output buffer
  103. Returns   : nothing
  104.  
  105. Put places bytes into OutBuffer so they may be later flushed out into
  106. the standard output stream.
  107. ************************************************************************/
  108. void Put(char *S,int Len)
  109. {
  110.    int i;
  111.  
  112.    for (i = 0; i < Len; i++)
  113.    {
  114.       *CurOutPtr++ = S[i];                   /* place byte in buffer */
  115.       if (CurOutPtr >= OutBuffer+BufSize)    /* if buffer overflows */
  116.          flushOut(BufSize);                  /* flush the buffer */
  117.    }
  118. }
  119.  
  120.  
  121. /************************************************************************
  122. Function  : ProcessLine
  123. Parameters: Line  a pointer to the current line of characters to process
  124. Returns   : nothing
  125.  
  126. Analyze line to determine if it is an error message.  If so, output
  127. relevant information to the message window.
  128.  
  129. To be considered an error message, a line must NOT :
  130.    . be blank
  131.    . start with "Microsoft" or "Copyright" (these lines form the Resource
  132.         Compiler header)
  133.  
  134. Any lines passing the above conditions are considered error messages.
  135. If the line contains a line number, it is of the form:
  136.  
  137.    source-file(LINE #) : message text        OR
  138.    source-file (LINE #) : message text
  139.  
  140.      that is, it contains a '(' followed by a ':'.  The line number
  141.      is sent to the Message Window followed by the message text.
  142.  
  143. If the line does not contain a line number, it is sent verbatim to 
  144. the Message Window.
  145. ************************************************************************/
  146. void ProcessLine(char *Line)
  147. {
  148.    static int HavePutFile = FALSE;
  149.    int        HasLineNumber;
  150.    char       Type;
  151.    unsigned   i;
  152.    char       *s, *LeftParen, *Colon;
  153.  
  154.    /* if blank line, return */
  155.    while( *Line && ( (*Line == '\r') || (*Line == '\n') ) )
  156.      Line++;
  157.    if( *Line == '\0' )
  158.      return;
  159.  
  160.    /* if line starts with "Microsoft" or "Copyright", it's not
  161.       an error line */
  162.  
  163.    if( strncmp( Line, "Microsoft", 9 ) == 0 ||
  164.        strncmp( Line, "Copyright", 9 ) == 0 )
  165.  
  166.      return;
  167.  
  168.  
  169.    HasLineNumber = FALSE;
  170.    LeftParen = strchr( Line, '(' );       /* if '(' in the line */
  171.    if( LeftParen != NULL )                /* Line may contain line number */
  172.      HasLineNumber = TRUE;
  173.  
  174.    if( HasLineNumber ) {
  175.      Colon = strchr(LeftParen, ':' );     /* if no ':' following the '(' */
  176.      if( Colon == NULL )                  /* Line doesn't contain line number */
  177.        HasLineNumber = FALSE;
  178.    }
  179.  
  180.    if( HasLineNumber ) {
  181.      s = LeftParen-1;                       /* position s after last char */
  182.      while( *s == ' ' )                     /* in filename */
  183.        s--;
  184.      s++;
  185.      *s = 0;                                /* and null-terminate */
  186.  
  187.      if( strcmp(Line,CurFile) != 0 )        /* if new filename */
  188.      {
  189.     Type = MsgNewFile;                  /* indicate by sending type
  190.                            out to message window */
  191.     strcpy(CurFile,Line);
  192.     Put(&Type,1);
  193.     Put(CurFile,strlen(CurFile)+1);     /* along with the new name */
  194.     HavePutFile = TRUE;
  195.      }
  196.  
  197.      s = strchr(Line,')');                   /* find the right paren */
  198.      *s = 0;                                 /* and null-terminate line# */
  199.      i = atoi(LeftParen+1);                  /* line# starts after ( */
  200.                          /* convert line# to integer */
  201.  
  202.      Type = MsgNewLine;                      /* set type to new line */
  203.      Put(&Type,1);                           /* indicate need for new line */
  204.      Put((char *)&i,2);                      /* put the number out */
  205.      i=1;                                    /* set column in message box */
  206.      Put((char *)&i,2);                      /* tab over to put message */
  207.  
  208.      s = Colon+1;                            /* position s at first */
  209.      while( *s == ' ' )                      /* non-blank char after : */
  210.        s++;                                  /* Rest of line is message */
  211.  
  212.      Put( s,strlen(s)+1);                    /* output the message */
  213.    }
  214.    else {                                    /* no line number */
  215.      if( !HavePutFile )
  216.      {
  217.     /* IDE expects the first message to
  218.        be preceded by a filename.  Since
  219.        we don't have one, fake it by
  220.        sending a NULL file before the
  221.        message.
  222.     */
  223.     Type = MsgNewFile;                  /* indicate by sending type
  224.                            out to message window */
  225.     *CurFile = '\0';
  226.     Put(&Type,1);
  227.     Put(CurFile,1);                     /* along with null filename */
  228.     HavePutFile = TRUE;
  229.      }
  230.  
  231.      Type = MsgNewLine;                      /* Fake line # etc. */
  232.      i    = 1;
  233.      Put(&Type,1);
  234.      Put((char *)&i,2);
  235.      Put((char *)&i,2);
  236.      Put(Line,strlen(Line)+1);
  237.    }
  238. }
  239.  
  240.  
  241.  
  242. /***********************************************************************
  243. Function  : main
  244.  
  245. Returns   : zero for successful execution
  246.         3    if an error is encountered
  247.  
  248. The main routine allocates buffers for the input and output buffer.
  249. Characters are then read from the input buffer building the line buffer
  250. that will be sent to the filter processor.  Lines are read and filtered
  251. until the end of input is reached.
  252. ***********************************************************************/
  253. int main( void )
  254. {
  255.    char c;
  256.    unsigned long core;
  257.  
  258.    setmode(1,O_BINARY);               /* set output stream to binary mode */
  259.    core = farcoreleft();
  260.    if (core > 64000U)
  261.       BufSize = 64000U;
  262.    else BufSize = (unsigned)core;     /* get available memory */
  263.                                       /* stay under 64K */
  264.    if ((CurInPtr = malloc(BufSize)) == NULL) /* allocate buffer space */
  265.       exit(3);
  266. #if 0
  267.    processor = NULL;                  /* set current processor to none */
  268. #endif
  269.  
  270.    InBuffer = CurInPtr;               /* input buffer is first half of space */
  271.    BufSize = BufSize/2;               /* output buffer is 2nd half */
  272.    OutBuffer = InBuffer + BufSize;
  273.    CurOutPtr = OutBuffer;             /* set buffer pointers */
  274.    LinePtr = Line;
  275.    CurBufLen = 0;
  276.    Put(PipeId,PipeIdLen);             /* send ID string to message window */
  277.    while ((c = NextChar()) != 0)      /* read characters */
  278.    {
  279.       if ((c == 13) || (c == 10))     /* build until line end */
  280.       {
  281.          *LinePtr = 0;
  282.          ProcessLine(Line);           /* filter the line */
  283.          LinePtr = Line;
  284.       }
  285.       /* characters are added to buffer up to 132 characters max */
  286.       else if ((FP_OFF(LinePtr) - FP_OFF(&Line)) < 132)
  287.       {
  288.          *LinePtr = c;                /* add to line buffer */
  289.          LinePtr++;
  290.       }
  291.    }
  292.    *LinePtr = 0;
  293.    ProcessLine(Line);                 /* filter last line */
  294.    EndMark = MsgEoFile;
  295.    Put(&EndMark,1);                   /* indicate end of input to
  296.                                          the message window */
  297.    flushOut((unsigned)(CurOutPtr-OutBuffer));     /* flush the buffer */
  298.    return 0;                          /* return OK */
  299. }
  300.