home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_03 / 1n03061a < prev    next >
Text File  |  1990-07-11  |  7KB  |  254 lines

  1. /*
  2.     File: CURLYBRC.C
  3.     Author: Bill Perez/Mario Perez
  4.     Date: 6/17/1990
  5.     Purpose: Finds matching curly braces in current file buffer of PWB.
  6.  
  7.     Operating System: MS-DOS 3.x
  8.  
  9.     Revision: 0.1
  10.  
  11.     Revision History:
  12.  
  13.         0.1 - 6/17/1990 - Initial Coding, Bill Perez.
  14.  
  15.     Notes:
  16.  
  17. */
  18.  
  19. /* Include Files */
  20. #include <stdlib.h>
  21. #include <ext.h>
  22.  
  23. /* Define Statements */
  24. #define MODE_CURLYBRACES    0
  25. #define MODE_PARENTHESIS    1
  26.  
  27. /* Structures */
  28.  
  29. /* Type Definitions */
  30.  
  31. /* Prototypes */
  32. void EXTERNAL WhenLoaded( void );
  33. PWBFUNC CheckCurlyBrace( unsigned argData, ARG _far *pArg, flagType fMeta );
  34. PWBFUNC CheckParenthesis( unsigned argData, ARG _far *pArg, flagType fMeta );
  35.  
  36. /* Global Variables */
  37. char pszOpenBrace[ 2 ] = "{";
  38. char pszCloseBrace[ 2 ] = "}";
  39. char pszOpenParen[ 2 ] = "(";
  40. char pszCloseParen[ 2 ] = ")";
  41.  
  42. /* Switch table */
  43. struct swiDesc swiTable[] =
  44. {
  45.     { "OpenBrace",  toPIF( pszOpenBrace ), SWI_SPECIAL },
  46.     { "CloseBrace", toPIF( pszCloseBrace ), SWI_SPECIAL },
  47.     { "OpenParen",  toPIF( pszOpenParen ), SWI_SPECIAL },
  48.     { "CloseParen", toPIF( pszCloseParen ), SWI_SPECIAL },
  49.     { NULL, NULL, 0, }
  50. };
  51.  
  52. /* Command Table */
  53. struct cmdDesc cmdTable[] =
  54. {
  55.     { "CheckCurlyBrace", CheckCurlyBrace, 0, NOARG | NULLARG },
  56.     { "CheckParenthesis", CheckParenthesis, 0, NOARG | NULLARG },
  57.     { NULL, NULL, 0, 0 }
  58. };
  59.  
  60. // Called during loading by PWB for initialization purposes.
  61. void EXTERNAL WhenLoaded( void )
  62. {
  63.     int hMatch;
  64.  
  65.     DoMessage( "Loading Curly Brace Matcher" );
  66.  
  67.     // Create the top-line menu bar menu
  68.     hMatch =
  69.         AddMenu(
  70.             "Ma~tch",
  71.             "Match curly braces or parenthesis",
  72.             "",
  73.             TRUE );
  74.  
  75.     // Create menu item under Match menu
  76.     AddMenuItem(
  77.         hMatch,
  78.         "Match ~Curly Brace",
  79.         "Matches curly braces",
  80.         NULL,
  81.         "CheckCurlyBrace" );
  82.  
  83.     // Place separator between options
  84.     AddMenuItem( hMatch, "-", "", "", "" );
  85.  
  86.     AddMenuItem(
  87.         hMatch,
  88.         "Match ~Parenthesis",
  89.         "Matches parenthesis",
  90.         NULL,
  91.         "CheckParenthesis" );
  92.  
  93.     // Set key for functions
  94.     SetKey( "CheckCurlyBrace", "shift+ctrl+b" );
  95.     SetKey( "CheckParenthesis", "shift+ctrl+p" );
  96. }
  97.  
  98.  
  99. // Searches from current location for first curly brace till closing curly
  100. //  brace found (counting all curly braces from there until closing one found
  101. //  or end of file found).  Reports error if unmatched brace found.
  102. unsigned int ScanText( unsigned int uMode )
  103. {
  104.     LINE yStart,
  105.         yStartInit,
  106.         lineCurrent,
  107.         lines_in_file;
  108.     COL xStart,
  109.         xStartInit,
  110.         column,
  111.         colMaxLength;
  112.     int brace_count = 0;
  113.     int col;
  114.     PFILE pFile;
  115.     char pszInBuffer[ BUFLEN ];
  116.     char pszOpenChar[ 2 ],
  117.         pszCloseChar[ 2 ];
  118.  
  119.     DoMessage(
  120.         uMode == MODE_CURLYBRACES ?
  121.             "Searching for opening curly brace" :
  122.             "Searching for opening parenthesis" );
  123.  
  124.     farstrcpy(
  125.         pszOpenChar,
  126.         uMode == MODE_CURLYBRACES ? pszOpenBrace : pszOpenParen );
  127.  
  128.     farstrcpy(
  129.         pszCloseChar,
  130.         uMode == MODE_CURLYBRACES ? pszCloseBrace : pszCloseParen );
  131.  
  132.     // Get starting location - (both line and column).
  133.     GetCursor( &xStart, &yStart );
  134.     yStartInit = yStart;
  135.     xStartInit = xStart;
  136.  
  137.     // Get handle to file
  138.     pFile = FileNameToHandle( "", "" );
  139.  
  140.     // Get the number of lines in the file
  141.     lines_in_file = FileLength( pFile );
  142.  
  143.     // Read current line
  144.     colMaxLength = GetLine( yStart, pszInBuffer, pFile );
  145.  
  146.     // Find opening brace on this line (starting first from current location,
  147.     //  then from first column on current line to end of current line - if
  148.     //  not found, then error message in message box).
  149.     if( pszInBuffer[ xStart ] != pszOpenChar[ 0 ] )
  150.     {
  151.         for( column = 0; column < colMaxLength; column++ )
  152.             if( pszInBuffer[ column ] == pszOpenChar[ 0 ] )
  153.             {
  154.                 xStart = column;
  155.                 MoveCur( xStart, yStart );
  156.                 xStartInit = xStart;
  157.                 break;
  158.             }
  159.  
  160.         if( column >= colMaxLength )
  161.         {
  162.             DoMessageBox(
  163.                 "",
  164.                 uMode == MODE_CURLYBRACES ?
  165.                     "Starting curly brace not found" :
  166.                     "Starting parenthesis not found",
  167.                 "",
  168.                 MBOX_OK,
  169.                 0 );
  170.  
  171.             return FALSE;
  172.         }
  173.     }
  174.  
  175.     DoMessage(
  176.         uMode == MODE_CURLYBRACES ?
  177.             "Searching for closing curly brace" :
  178.             "Searching for closing parenthesis" );
  179.  
  180.     /* We found an opening brace, or wouldn't be here */
  181.     brace_count = 1;
  182.  
  183.     // Slide past current opening brace character in current buffer
  184.     xStart++;
  185.  
  186.     // Loop until closing brace found starting from xStart, yStart
  187.     while( brace_count      )
  188.     {
  189.         for( col = xStart; col < colMaxLength; col++ )
  190.         {
  191.             if( pszInBuffer[ col ] == pszOpenChar[ 0 ] )
  192.             {
  193.                 brace_count++;
  194.                 continue;
  195.             }
  196.  
  197.             if( pszInBuffer[ col ] == pszCloseChar[ 0 ] )
  198.             {
  199.                 brace_count--;
  200.  
  201.                 /* More braces to find? */
  202.                 if( brace_count )
  203.                     continue;
  204.                 else
  205.                 {
  206.                     /* All braces found, since none are left */
  207.                     xStart = col;
  208.                     break;
  209.                 }
  210.             }
  211.         }
  212.  
  213.         if( brace_count )
  214.         {
  215.             /* Should really check to see that yStart is in file */
  216.             if( yStart < lines_in_file )
  217.                 colMaxLength = GetLine( ++yStart, pszInBuffer, pFile );
  218.             else
  219.             {
  220.                 MoveCur( xStartInit, yStartInit );
  221.                 DoMessage(
  222.                     uMode == MODE_CURLYBRACES ?
  223.                         "Couldn't find matching curly brace" :
  224.                         "Couldn't find matching parenthesis" );
  225.                 break;
  226.             }
  227.             xStart = 0;
  228.         }
  229.         else
  230.         {
  231.             /* Move the cursor to the row and column found */
  232.             MoveCur( xStart, yStart );
  233.             DoMessage(
  234.                 uMode == MODE_CURLYBRACES ?
  235.                     "Found closing curly brace" :
  236.                     "Found closing parenthesis" );
  237.         }
  238.     }
  239.     return TRUE;
  240. }
  241.  
  242. // Sets mode to search for curly braces, then scans text
  243. PWBFUNC CheckCurlyBrace( unsigned argData, ARG _far *pArg, flagType fMeta )
  244. {
  245.     // Set up characters to search for based on mode
  246.     return ScanText( MODE_CURLYBRACES ) ;
  247. }
  248.  
  249. // Sets mode to parenthesis
  250. PWBFUNC CheckParenthesis( unsigned argData, ARG _far *pArg, flagType fMeta )
  251. {
  252.     return ScanText( MODE_PARENTHESIS );
  253. }
  254.