Advertisement
Guest User

Program.cs

a guest
Jan 6th, 2022
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.59 KB | None | 0 0
  1. IList<FileInfo> fileInfos =
  2.       new DirectoryInfo( "/path/to/reference/source/directory" )
  3.             .GetFiles( "*.cs" )
  4.             .ToList( );
  5.  
  6. Console.WriteLine( $"found {fileInfos.Count} cs files" );
  7.  
  8. int globalAllmanCount = 0;
  9. int globalKAndRCount = 0;
  10. int globalMixedCount = 0;
  11.  
  12. void UpdateStats( int allmanCount, int kAndRCount ) {
  13.    if( allmanCount+kAndRCount == 0 )
  14.       return;
  15.  
  16.    float kAndRPercent = kAndRCount*100f/( allmanCount+kAndRCount );
  17.    if( kAndRPercent <= .1f )
  18.       ++globalAllmanCount;
  19.    else if( kAndRCount >= .9f )
  20.       ++globalKAndRCount;
  21.    else
  22.       ++globalMixedCount;
  23. }
  24.  
  25. async Task ScanFile( FileInfo fi ) {
  26.    using StreamReader sr = new(fi.OpenRead( ));
  27.    string line;
  28.    int allmanCount = 0;
  29.    int kAndRCount = 0;
  30.    while( ( line = await sr.ReadLineAsync( ) ) != null ) {
  31.       if( !line.Contains( '{' ) )
  32.          continue;
  33.  
  34.       if( string.IsNullOrWhiteSpace( line.Split( '{' )[ 0 ] ) )
  35.          ++allmanCount;
  36.       else
  37.          ++kAndRCount;
  38.    }
  39.  
  40.    UpdateStats( allmanCount, kAndRCount );
  41. }
  42.  
  43. int cnt = 0;
  44. foreach( FileInfo fi in fileInfos ) {
  45.    await ScanFile( fi );
  46.    ++cnt;
  47.    Console.Write( $"\rscanned {cnt}/{fileInfos.Count} files {cnt*100f/fileInfos.Count:0.00}%" );
  48. }
  49.  
  50. Console.WriteLine( );
  51.  
  52. int globalTotal = globalAllmanCount+globalKAndRCount+globalMixedCount;
  53. Console.WriteLine( $"K&R style: \t{globalKAndRCount*100f/globalTotal:0.00}%" );
  54. Console.WriteLine( $"Allman style: \t{globalAllmanCount*100f/globalTotal:0.00}%" );
  55. Console.WriteLine( $"mixed style: \t{globalMixedCount*100f/globalTotal:0.00}%" );
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement