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

  1. /*
  2.    EXAMPLE SOURCE CODE FOR IMPLIB FILTER
  3.  
  4.    Impl2Msg.C
  5.    Copyright (c) 1991 Borland International, Inc.
  6.    All rights reserved.
  7.  
  8.    Impl2Msg - Import Librarian 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 import
  13.    librarian being invoked, and the output stream is connected to the message
  14.    window of the Turbo C++ IDE, ie.
  15.  
  16.           implib fname | impl2msg | Turbo C++ message window
  17.  
  18.    Compile using Turbo C++ in the LARGE memory model
  19.  
  20.    tcc -ml impl2msg
  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 have one of these forms:
  130.  
  131.    Warning source-file line-no: message
  132.    Error source-file line-no: message
  133.    Warning: message
  134.    Error: message
  135.  
  136.      that is, the line begins with "Warning" or "Error",
  137.      followed by the source filename, the line number, a
  138.      colon, and the message.  The source filename and line
  139.      number may be absent.
  140.  
  141. ************************************************************************/
  142. void ProcessLine(char *Line)
  143. {
  144.    char     Type, *s;
  145.    unsigned i, HasFileAndLineNumber = FALSE;
  146.  
  147.    if( strncmp( Line, "Warning", 7 ) == 0 )      
  148.    {                                             /* we have a warning line */
  149.      Line += 7;                                  /* flush "Warning" */
  150.      if( *Line != ':' )                          /* ':' doesn't follow */
  151.        HasFileAndLineNumber = TRUE;              /* so file, line present */
  152.    }
  153.    else if( strncmp( Line, "Error", 5 ) == 0 )   
  154.    {                                             /* we have an error line */
  155.      Line += 5;                                  /* flush "Warning" */
  156.      if( *Line != ':' )                          /* ':' doesn't follow */
  157.        HasFileAndLineNumber = TRUE;              /* so file, line present */
  158.    }
  159.    else                                          /* not an error line */
  160.      return;
  161.  
  162.    if( HasFileAndLineNumber )
  163.    {                                      /* need to process them */
  164.      while( *Line == ' ' )                /* flush white-space until */
  165.        Line++;                            /*  Line points to the filename */
  166.  
  167.      s = Line;                            
  168.      while( *s != ' ' )                   /* s points to space after */
  169.        s++;                               /*  filename */
  170.      *s = '\0';                           /* Replace space with null */
  171.  
  172.      if (strcmp(Line,CurFile) != 0)       /* if new filename */
  173.      {
  174.     Type = MsgNewFile;                /* indicate by sending type
  175.                          out to message window */
  176.     strcpy(CurFile,Line);
  177.     Put(&Type,1);
  178.     Put(CurFile,strlen(CurFile)+1);   /* along with the new name */
  179.      }
  180.  
  181.      s++;                                 /* flush white space until */
  182.      while( *s == ' ' )                   /*  s points to line number */
  183.        s++;
  184.      Line = s;                            /* Line points to line number too */
  185.      while( *s != ':' )                   /* position s at ':' after */
  186.        s++;                               /*  line number */
  187.      *s = '\0';                           /* replace ':' with null */
  188.      i = atoi(Line);                      /* convert line number to int */
  189.  
  190.      Type = MsgNewLine;                   /* set type to new line */
  191.      Put(&Type,1);                        /* indicate need for new line */
  192.      Put((char *)&i,2);                   /* put the number out */
  193.      i=1;                                 /* set column in message box */
  194.      Put((char *)&i,2);                   /* tab over to put message */
  195.  
  196.      s++;                                 /* bump past null */
  197.      while( *s == ' ' )                   /* flush white space until */
  198.        s++;                               /*  s points to message */
  199.  
  200.      Put( s,strlen(s)+1);                 /* output message */
  201.    }
  202.    else                                   /* no file or line number */
  203.    {
  204.      Line++;                              /* position Line past ':' */
  205.      while( *Line == ' ' )                /* and at start of message */
  206.        Line++;
  207.  
  208.      Type = MsgNewLine;                   /* Put fake line number */
  209.      i    = 1;
  210.      Put(&Type,1);
  211.      Put((char *)&i,2);
  212.      Put((char *)&i,2);
  213.      Put(Line,strlen(Line)+1);            /* put message */
  214.    }
  215. }
  216.  
  217.  
  218. /***********************************************************************
  219. Function  : main
  220.  
  221. Returns   : zero for successful execution
  222.         3    if an error is encountered
  223.  
  224. The main routine allocates buffers for the input and output buffer.
  225. Characters are then read from the input buffer building the line buffer
  226. that will be sent to the filter processor.  Lines are read and filtered
  227. until the end of input is reached.
  228. ***********************************************************************/
  229. int main( void )
  230. {
  231.    char c;
  232.    unsigned long core;
  233.  
  234.    setmode(1,O_BINARY);               /* set output stream to binary mode */
  235.    core = farcoreleft();
  236.    if (core > 64000U)
  237.       BufSize = 64000U;
  238.    else BufSize = (unsigned)core;     /* get available memory */
  239.                       /* stay under 64K */
  240.    if ((CurInPtr = malloc(BufSize)) == NULL) /* allocate buffer space */
  241.       exit(3);
  242. #if 0
  243.    processor = NULL;                  /* set current processor to none */
  244. #endif
  245.  
  246.    InBuffer = CurInPtr;               /* input buffer is first half of space */
  247.    BufSize = BufSize/2;               /* output buffer is 2nd half */
  248.    OutBuffer = InBuffer + BufSize;
  249.    CurOutPtr = OutBuffer;             /* set buffer pointers */
  250.    LinePtr = Line;
  251.    CurBufLen = 0;
  252.    Put(PipeId,PipeIdLen);             /* send ID string to message window */
  253.    while ((c = NextChar()) != 0)      /* read characters */
  254.    {
  255.       if ((c == 13) || (c == 10))     /* build until line end */
  256.       {
  257.      *LinePtr = 0;
  258.      ProcessLine(Line);           /* filter the line */
  259.      LinePtr = Line;
  260.       }
  261.       /* characters are added to buffer up to 132 characters max */
  262.       else if ((FP_OFF(LinePtr) - FP_OFF(&Line)) < 132)
  263.       {
  264.      *LinePtr = c;                /* add to line buffer */
  265.      LinePtr++;
  266.       }
  267.    }
  268.    *LinePtr = 0;
  269.    ProcessLine(Line);                 /* filter last line */
  270.    EndMark = MsgEoFile;
  271.    Put(&EndMark,1);                   /* indicate end of input to
  272.                      the message window */
  273.    flushOut((unsigned)(CurOutPtr-OutBuffer));     /* flush the buffer */
  274.    return 0;                          /* return OK */
  275. }
  276.  
  277.