3030
3131using ICSharpCode . SharpZipLib . Checksum ;
3232
33- class Cmd_Crc
33+ class Cmd_Checksum
3434{
3535 static void ShowHelp ( )
3636 {
3737 Console . Error . WriteLine ( "Compress or uncompress FILEs (by default, compress FILES in-place)." ) ;
3838 Console . Error . WriteLine ( "Version {0} using SharpZipLib {1}" ,
39- typeof ( Cmd_Crc ) . Assembly . GetName ( ) . Version ,
39+ typeof ( Cmd_Checksum ) . Assembly . GetName ( ) . Version ,
4040 typeof ( IChecksum ) . Assembly . GetName ( ) . Version ) ;
4141 Console . Error . WriteLine ( "" ) ;
4242 Console . Error . WriteLine ( "Mandatory arguments to long options are mandatory for short options too." ) ;
@@ -48,6 +48,11 @@ static void ShowHelp()
4848 Console . Error . WriteLine ( " -9, --best compress better" ) ;
4949 }
5050
51+ #region Instance Fields
52+ private static Command command_ = Command . Nothing ;
53+ private static string file_ ;
54+ #endregion
55+
5156 #region Command parsing
5257 enum Command
5358 {
@@ -65,36 +70,36 @@ public ArgumentParser(string[] args)
6570 {
6671 foreach ( string argument in args ) {
6772 switch ( argument ) {
68- case "-?" : // for backwards compatibility
69- case "-h" :
70- case "--help" :
71- SetCommand ( Command . Help ) ;
72- break ;
73- case "--adler32" :
74- SetCommand ( Command . Adler ) ;
75- break ;
76- case "--bzip2" :
77- SetCommand ( Command . BZip2 ) ;
78- break ;
79- case "--crc32" :
80- SetCommand ( Command . Crc32 ) ;
81- break ;
82- default :
83- if ( argument [ 0 ] == '-' ) {
84- Console . Error . WriteLine ( "Unknown argument {0}" , argument ) ;
85- command_ = Command . Stop ;
86- } else if ( file_ == null ) {
87- file_ = argument ;
88-
89- if ( ! System . IO . File . Exists ( file_ ) ) {
90- Console . Error . WriteLine ( "File not found '{0}'" , file_ ) ;
73+ case "-?" : // for backwards compatibility
74+ case "-h" :
75+ case "--help" :
76+ SetCommand ( Command . Help ) ;
77+ break ;
78+ case "--adler32" :
79+ SetCommand ( Command . Adler ) ;
80+ break ;
81+ case "--bzip2" :
82+ SetCommand ( Command . BZip2 ) ;
83+ break ;
84+ case "--crc32" :
85+ SetCommand ( Command . Crc32 ) ;
86+ break ;
87+ default :
88+ if ( argument [ 0 ] == '-' ) {
89+ Console . Error . WriteLine ( "Unknown argument {0}" , argument ) ;
90+ command_ = Command . Stop ;
91+ } else if ( file_ == null ) {
92+ file_ = argument ;
93+
94+ if ( ! System . IO . File . Exists ( file_ ) ) {
95+ Console . Error . WriteLine ( "File not found '{0}'" , file_ ) ;
96+ command_ = Command . Stop ;
97+ }
98+ } else {
99+ Console . Error . WriteLine ( "File has already been specified" ) ;
91100 command_ = Command . Stop ;
92101 }
93- } else {
94- Console . Error . WriteLine ( "File has already been specified" ) ;
95- command_ = Command . Stop ;
96- }
97- break ;
102+ break ;
98103 }
99104 }
100105
@@ -117,63 +122,64 @@ void SetCommand(Command command)
117122 }
118123 }
119124
120- public string Source
121- {
125+ public string Source {
122126 get { return file_ ; }
123127 }
124128
125- public Command Command
126- {
129+ public Command Command {
127130 get { return command_ ; }
128131 }
129-
130- #region Instance Fields
131- Command command_ = Command . Nothing ;
132- string file_ ;
133- #endregion
134132 }
135133 #endregion
136134
137-
138135 public static int Main ( string [ ] args )
139136 {
140137 if ( args . Length == 0 ) {
141138 ShowHelp ( ) ;
142139 return 1 ;
143140 }
144141
145- if ( ! File . Exists ( args [ 0 ] ) ) {
146- Console . Error . WriteLine ( "Cannot find file {0}" , args [ 0 ] ) ;
142+ var parser = new ArgumentParser ( args ) ;
143+
144+ if ( ! File . Exists ( file_ ) ) {
145+ Console . Error . WriteLine ( "Cannot find file {0}" , file_ ) ;
147146 ShowHelp ( ) ;
148147 return 1 ;
149148 }
150149
151- var parser = new ArgumentParser ( args ) ;
152-
153- using ( FileStream checksumStream = File . OpenRead ( args [ 0 ] ) ) {
150+ using ( FileStream checksumStream = File . OpenRead ( file_ ) ) {
154151
155152 byte [ ] buffer = new byte [ 4096 ] ;
156153 int bytesRead ;
157154
158155 switch ( parser . Command ) {
159- case Command . Help :
160- ShowHelp ( ) ;
161- break ;
162-
163- case Command . Crc32 :
164- var currentCrc = new Crc32 ( ) ;
165- while ( ( bytesRead = checksumStream . Read ( buffer , 0 , buffer . Length ) ) > 0 ) {
166- currentCrc . Update ( buffer , 0 , bytesRead ) ;
167- }
168- Console . WriteLine ( "CRC32 for {0} is 0x{1:X2}" , args [ 0 ] , currentCrc . Value ) ;
169- break ;
156+ case Command . Help :
157+ ShowHelp ( ) ;
158+ break ;
170159
171- case Command . Adler :
172- var currentAdler = new Adler32 ( ) ;
173- while ( ( bytesRead = checksumStream . Read ( buffer , 0 , buffer . Length ) ) > 0 ) {
174- currentAdler . Update ( buffer , 0 , bytesRead ) ;
175- }
176- break ;
160+ case Command . Crc32 :
161+ var currentCrc = new Crc32 ( ) ;
162+ while ( ( bytesRead = checksumStream . Read ( buffer , 0 , buffer . Length ) ) > 0 ) {
163+ currentCrc . Update ( buffer , 0 , bytesRead ) ;
164+ }
165+ Console . WriteLine ( "CRC32 for {0} is 0x{1:X8}" , args [ 0 ] , currentCrc . Value ) ;
166+ break ;
167+
168+ case Command . BZip2 :
169+ var currentBZip2Crc = new BZip2Crc ( ) ;
170+ while ( ( bytesRead = checksumStream . Read ( buffer , 0 , buffer . Length ) ) > 0 ) {
171+ currentBZip2Crc . Update ( buffer , 0 , bytesRead ) ;
172+ }
173+ Console . WriteLine ( "BZip2CRC32 for {0} is 0x{1:X8}" , args [ 0 ] , currentBZip2Crc . Value ) ;
174+ break ;
175+
176+ case Command . Adler :
177+ var currentAdler = new Adler32 ( ) ;
178+ while ( ( bytesRead = checksumStream . Read ( buffer , 0 , buffer . Length ) ) > 0 ) {
179+ currentAdler . Update ( buffer , 0 , bytesRead ) ;
180+ }
181+ Console . WriteLine ( "Adler32 for {0} is 0x{1:X8}" , args [ 0 ] , currentAdler . Value ) ;
182+ break ;
177183 }
178184 }
179185 return 0 ;
0 commit comments