Guest User

Untitled

a guest
Jan 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3.  
  4. const char brackets[] = { '(', ')', '[', ']', '{', '}' };
  5.  
  6. void checkChar( unsigned short charCounter[], char lineChar );
  7. void checkChar( unsigned short charCounter[], char lineChar ){
  8.     int i;
  9.     for( i = 0; i < sizeof brackets; i++ ){
  10.         if( lineChar == brackets[ i ] ){
  11.             charCounter[ i ]++;
  12.         }
  13.     }
  14. }
  15. int main( void ){
  16.     char filename[ 128 ];
  17.     char line[ 256 ];
  18.     short int i, j;
  19.     bool InQString, InDQString, InComment;
  20.     unsigned short int bracketsCount[ sizeof brackets ] = { 0 };
  21.     FILE * file;
  22.    
  23.     InQString = InDQString = InComment = false;
  24.  
  25.     printf( "file to check:" );
  26.     fgets( filename, 128, stdin );
  27.     for( i = 0; i < sizeof filename; i++ ){
  28.         if( filename[ i ] == '\n' ){
  29.             filename[ i ] = '\0';
  30.             break;
  31.         }
  32.     }
  33.    
  34.     file = fopen( filename, "r" );
  35.     if( file == NULL ){
  36.         perror( filename );
  37.         return 1;
  38.     }
  39.     for( i = 1; fgets( line, sizeof line, file ); i++ ){
  40.         for( j = 0; j < sizeof line; j++ ){
  41.             if( line[ j ] == '\0' ) break;
  42.            
  43.             /* brackets */
  44.             if( line[ j ] == '/' && line[ j + 1 ] && InComment == false && InDQString == false && InQString == false ){
  45.                 InComment = true;
  46.                 continue;
  47.             }else if( line[ j ] == '*' && line[ j + 1 ] == '/' && InComment == true && InDQString == false && InQString == false ){
  48.                 InComment = false;
  49.                 continue;
  50.             }
  51.             if( InComment == false ){
  52.                 /* single quotes */
  53.                 if( line[ j ] == '\'' && InDQString == false && InQString == false ){
  54.                     InQString = true;
  55.                     continue;
  56.                 }else if( line[ j ] == '\'' && line[ j - 1 ] != '\\' && InDQString == false && InQString == true ){
  57.                     InQString = false;
  58.                     continue;
  59.                 }    
  60.  
  61.                 /* double quotes */
  62.                 if( line[ j ] == '"' && InDQString == false && InQString == false ){
  63.                     InDQString = true;
  64.                     continue;
  65.                 }else if( line[ j ] == '"' && line[ j - 1 ] != '\\' && InDQString == true && InQString == false ){
  66.                     InDQString = false;
  67.                     continue;
  68.                 }
  69.  
  70.                 if( InDQString == false && InQString == false ){
  71.                     checkChar( bracketsCount, line[ j ] );
  72.                 }
  73.             }
  74.             /*brackets end*/
  75.         }
  76.         printf( "Line %d: %s\n", i, line );
  77.     }
  78.             checkChar( bracketsCount, line[ j ] );            
  79.     for( i = 0; i < sizeof brackets; i++ ){
  80.         printf( "%c: %d, ", brackets[i], bracketsCount[i] );
  81.     }
  82.    
  83.  
  84.     return 0;
  85. }
Add Comment
Please, Sign In to add comment