home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 4.ddi / EXAMPLES.ZIP / IMPL2MSG.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  11.1 KB  |  293 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 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 IDE, ie.
  15.  
  16.           implib fname | impl2msg | IDE message window
  17.  
  18.    Compile using the LARGE memory model.
  19. */
  20.  
  21. #include <dir.h>
  22. #include <dos.h>
  23. #include <stdlib.h>
  24. #include <fcntl.h>
  25. #include <string.h>
  26. #include <alloc.h>
  27. #include <io.h>
  28. #include "filter.h"
  29.  
  30. #define TRUE (1 == 1)
  31. #define FALSE !(TRUE)
  32.  
  33. char     CurFile[MAXPATH];       /* Current file in message window */
  34. unsigned BufSize,                /* Size of internal working buffer */
  35.          CurBufLen;              /* Buffer space in use */
  36. char     *InBuffer,              /* Input buffer */
  37.          *OutBuffer,             /* Output buffer */
  38.          *CurInPtr,              /* current character in input buffer */
  39.      *CurOutPtr,             /* current character in output */
  40.          *LinePtr;               /* pointer to the current character in
  41.                                     the input buffer */
  42. char     Line[133];              /* static buffer to store line most recently
  43.                     input */
  44. long int InOff;                  /* position in actual input stream */
  45. char     EndMark;                /* Used to output end of data to message
  46.                                     window */
  47.  
  48. /*************************************************************************
  49. Function  : NextChar
  50. Parameters: none
  51. Returns   : next character in input buffer or 0 on end of file
  52.  
  53. Input from the standard input stream is buffered in a global buffer InBuffer
  54. which is allocated in function main.  The function will return
  55. the next character in the buffer, reading from the input stream when the
  56. buffer becomes empty.
  57. **************************************************************************/
  58. char NextChar(void)
  59. {
  60.   if (CurInPtr < InBuffer+CurBufLen)  /* if buffer is not empty */
  61.   {
  62.     return *(CurInPtr++);             /* return next character */
  63.   }
  64.   else
  65.   {
  66.     CurInPtr = InBuffer;              /* reset pointer to front of buffer */
  67.     lseek(0,InOff,0);                 /* seek to the next section for read */
  68.     InOff += BufSize;                 /* increment pointer to next block */
  69.     if ((CurBufLen = read(0,InBuffer,BufSize)) !=0)
  70.       return NextChar();              /* recursive call merely returns
  71.                                          first character in buffer after read */
  72.     return 0;                         /* return 0 on end of file */
  73.   }
  74. }
  75.  
  76. /*************************************************************************
  77. Function  : flushOut
  78. Parameter : Size   The number of characters to be written out
  79. Returns   : nothing
  80.  
  81. Strings to be sent to the message window are buffered in a buffer called
  82. OutBuffer.  A call to this function will write out Size bytes from OutBuffer
  83. to the standard output stream and resets the output buffer pointer to the
  84. beginning of the buffer.  The output buffer is considered empty after a call
  85. to this function.
  86. *************************************************************************/
  87. void flushOut(unsigned Size)
  88. {
  89.    if (Size != 0)                /* don't flush an empty buffer */
  90.    {
  91.       CurOutPtr = OutBuffer;     /* reset pointer to beginning of buffer */
  92.       lseek(1,0,2);              /* seek output stream to end */
  93.       write(1,OutBuffer,Size);   /* write out Size bytes */
  94.    }
  95. }
  96.  
  97. /************************************************************************
  98. Function  : Put
  99. Parameters: S    pointer to bytes being put into output buffer
  100.         Len  number of bytes to be put in output buffer
  101. Returns   : nothing
  102.  
  103. Put places bytes into OutBuffer so they may be later flushed out into
  104. the standard output stream.
  105. ************************************************************************/
  106. void Put(char *S,int Len)
  107. {
  108.    int i;
  109.  
  110.    for (i = 0; i < Len; i++)
  111.    {
  112.       *CurOutPtr++ = S[i];                   /* place byte in buffer */
  113.       if (CurOutPtr >= OutBuffer+BufSize)    /* if buffer overflows */
  114.      flushOut(BufSize);                  /* flush the buffer */
  115.    }
  116. }
  117.  
  118.  
  119. /************************************************************************
  120. Function  : ProcessLine
  121. Parameters: Line  a pointer to the current line of characters to process
  122. Returns   : nothing
  123.  
  124. Analyze line to determine if it is an error message.  If so, output
  125. relevant information to the message window.
  126.  
  127. To be considered an error message, a line must have one of these forms:
  128.  
  129.    Warning source-file line-no: message
  130.    Error source-file line-no: message
  131.    Warning: message
  132.    Error: message
  133.  
  134.      that is, the line begins with "Warning" or "Error",
  135.      followed by the source filename, the line number, a
  136.      colon, and the message.  The source filename and line
  137.      number may be absent.
  138.  
  139. ************************************************************************/
  140. void ProcessLine(char *Line)
  141. {
  142.    static int HavePutFile = FALSE;
  143.    char     Type, *s;
  144.    unsigned i, HasFileAndLineNumber = FALSE;
  145.  
  146.    if( strncmp( Line, "Warning", 7 ) == 0 )      
  147.    {                                             /* we have a warning line */
  148.      Line += 7;                                  /* flush "Warning" */
  149.      if( *Line != ':' )                          /* ':' doesn't follow */
  150.        HasFileAndLineNumber = TRUE;              /* so file, line present */
  151.    }
  152.    else if( strncmp( Line, "Error", 5 ) == 0 )   
  153.    {                                             /* we have an error line */
  154.      Line += 5;                                  /* flush "Warning" */
  155.      if( *Line != ':' )                          /* ':' doesn't follow */
  156.        HasFileAndLineNumber = TRUE;              /* so file, line present */
  157.    }
  158.    else                                          /* not an error line */
  159.      return;
  160.  
  161.    if( HasFileAndLineNumber )
  162.    {                                      /* need to process them */
  163.      while( *Line == ' ' )                /* flush white-space until */
  164.        Line++;                            /*  Line points to the filename */
  165.  
  166.      s = Line;                            
  167.      while( *s != ' ' )                   /* s points to space after */
  168.        s++;                               /*  filename */
  169.      *s = '\0';                           /* Replace space with null */
  170.  
  171.      if (strcmp(Line,CurFile) != 0)       /* if new filename */
  172.      {
  173.     Type = MsgNewFile;                /* indicate by sending type
  174.                          out to message window */
  175.     strcpy(CurFile,Line);
  176.     Put(&Type,1);
  177.     Put(CurFile,strlen(CurFile)+1);   /* along with the new name */
  178.         HavePutFile = TRUE;
  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.      if( !HavePutFile )
  209.      {
  210.     /* IDE expects the first message to
  211.        be preceded by a filename.  Since
  212.        we don't have one, fake it by
  213.        sending a NULL file before the
  214.        message.
  215.     */
  216.     Type = MsgNewFile;                  /* indicate by sending type
  217.                            out to message window */
  218.     *CurFile = '\0';
  219.     Put(&Type,1);
  220.     Put(CurFile,1);                     /* along with null filename */
  221.     HavePutFile = TRUE;
  222.      }
  223.  
  224.      Type = MsgNewLine;                   /* Put fake line number */
  225.      i    = 1;
  226.      Put(&Type,1);
  227.      Put((char *)&i,2);
  228.      Put((char *)&i,2);
  229.      Put(Line,strlen(Line)+1);            /* put message */
  230.    }
  231. }
  232.  
  233.  
  234. /***********************************************************************
  235. Function  : main
  236.  
  237. Returns   : zero for successful execution
  238.         3    if an error is encountered
  239.  
  240. The main routine allocates buffers for the input and output buffer.
  241. Characters are then read from the input buffer building the line buffer
  242. that will be sent to the filter processor.  Lines are read and filtered
  243. until the end of input is reached.
  244. ***********************************************************************/
  245. int main( void )
  246. {
  247.    char c;
  248.    unsigned long core;
  249.  
  250.    setmode(1,O_BINARY);               /* set output stream to binary mode */
  251.    core = farcoreleft();
  252.    if (core > 64000U)
  253.       BufSize = 64000U;
  254.    else BufSize = (unsigned)core;     /* get available memory */
  255.                       /* stay under 64K */
  256.    if ((CurInPtr = malloc(BufSize)) == NULL) /* allocate buffer space */
  257.       exit(3);
  258. #if 0
  259.    processor = NULL;                  /* set current processor to none */
  260. #endif
  261.  
  262.    InBuffer = CurInPtr;               /* input buffer is first half of space */
  263.    BufSize = BufSize/2;               /* output buffer is 2nd half */
  264.    OutBuffer = InBuffer + BufSize;
  265.    CurOutPtr = OutBuffer;             /* set buffer pointers */
  266.    LinePtr = Line;
  267.    CurBufLen = 0;
  268.    Put(PipeId,PipeIdLen);             /* send ID string to message window */
  269.    while ((c = NextChar()) != 0)      /* read characters */
  270.    {
  271.       if ((c == 13) || (c == 10))     /* build until line end */
  272.       {
  273.      *LinePtr = 0;
  274.      ProcessLine(Line);           /* filter the line */
  275.      LinePtr = Line;
  276.       }
  277.       /* characters are added to buffer up to 132 characters max */
  278.       else if ((FP_OFF(LinePtr) - FP_OFF(&Line)) < 132)
  279.       {
  280.      *LinePtr = c;                /* add to line buffer */
  281.      LinePtr++;
  282.       }
  283.    }
  284.    *LinePtr = 0;
  285.    ProcessLine(Line);                 /* filter last line */
  286.    EndMark = MsgEoFile;
  287.    Put(&EndMark,1);                   /* indicate end of input to
  288.                      the message window */
  289.    flushOut((unsigned)(CurOutPtr-OutBuffer));     /* flush the buffer */
  290.    return 0;                          /* return OK */
  291. }
  292.  
  293.