|
4 | 4 |
|
5 | 5 | class ConsoleModule
|
6 | 6 | {
|
7 |
| - static void Main() |
8 |
| - { |
9 |
| - string[] args = Environment.GetCommandLineArgs(); |
| 7 | + static void Main() |
| 8 | + { |
| 9 | + string[] args = Environment.GetCommandLineArgs(); |
10 | 10 |
|
11 |
| - // Get command line arguments. |
12 |
| - if (args.Length != 3 || String.IsNullOrWhiteSpace(args[1]) || String.IsNullOrWhiteSpace(args[2])) |
13 |
| - { |
14 |
| - Console.WriteLine("There must be a source and a destination file."); |
15 |
| - ShowSyntax(); |
16 |
| - return; |
17 |
| - } |
| 11 | + // Get command line arguments. |
| 12 | + if (args.Length != 3 || String.IsNullOrWhiteSpace(args[1]) || String.IsNullOrWhiteSpace(args[2])) |
| 13 | + { |
| 14 | + Console.WriteLine("There must be a source and a destination file."); |
| 15 | + ShowSyntax(); |
| 16 | + return; |
| 17 | + } |
18 | 18 |
|
19 |
| - string source = args[1]; |
20 |
| - string destination = args[2]; |
| 19 | + string source = args[1]; |
| 20 | + string destination = args[2]; |
21 | 21 |
|
22 |
| - if(!File.Exists(source)) |
23 |
| - { |
24 |
| - Console.WriteLine("The source file does not exist."); |
25 |
| - return; |
26 |
| - } |
| 22 | + if (!File.Exists(source)) |
| 23 | + { |
| 24 | + Console.WriteLine("The source file does not exist."); |
| 25 | + return; |
| 26 | + } |
27 | 27 |
|
28 |
| - try |
29 |
| - { |
30 |
| - using (var sr = new StreamReader(source)) |
31 |
| - { |
32 |
| - // Check whether destination file exists and exit if it should not be overwritten. |
33 |
| - if (File.Exists(destination)) |
| 28 | + try |
| 29 | + { |
| 30 | + using (var sr = new StreamReader(source)) |
34 | 31 | {
|
35 |
| - Console.Write("The destination file {1} '{0}'{1}exists. Overwrite it? (Y/N) ", |
36 |
| - source, Environment.NewLine); |
37 |
| - ConsoleKeyInfo keyPressed = Console.ReadKey(true); |
38 |
| - if (Char.ToUpper(keyPressed.KeyChar) == 'Y' | Char.ToUpper(keyPressed.KeyChar) == 'N') |
39 |
| - { |
40 |
| - Console.WriteLine(keyPressed.KeyChar); |
41 |
| - if (Char.ToUpper(keyPressed.KeyChar) == 'N') |
42 |
| - return; |
43 |
| - } |
44 |
| - } |
45 |
| - using (var sw = new StreamWriter(destination, false, System.Text.Encoding.UTF8)) |
46 |
| - { |
47 |
| - // Instantiate the encoder |
48 |
| - Encoding encoding = Encoding.GetEncoding("us-ascii", new CyrillicToRomanFallback(), new DecoderExceptionFallback()); |
49 |
| - // This is an encoding operation, so we only need to get the encoder. |
50 |
| - Encoder encoder = encoding.GetEncoder(); |
51 |
| - Decoder decoder = encoding.GetDecoder(); |
| 32 | + // Check whether destination file exists and exit if it should not be overwritten. |
| 33 | + if (File.Exists(destination)) |
| 34 | + { |
| 35 | + Console.Write("The destination file {1} '{0}'{1}exists. Overwrite it? (Y/N) ", |
| 36 | + source, Environment.NewLine); |
| 37 | + ConsoleKeyInfo keyPressed = Console.ReadKey(true); |
| 38 | + if (Char.ToUpper(keyPressed.KeyChar) == 'Y' | Char.ToUpper(keyPressed.KeyChar) == 'N') |
| 39 | + { |
| 40 | + Console.WriteLine(keyPressed.KeyChar); |
| 41 | + if (Char.ToUpper(keyPressed.KeyChar) == 'N') |
| 42 | + return; |
| 43 | + } |
| 44 | + } |
| 45 | + using (var sw = new StreamWriter(destination, false, System.Text.Encoding.UTF8)) |
| 46 | + { |
| 47 | + // Instantiate the encoder |
| 48 | + Encoding encoding = Encoding.GetEncoding("us-ascii", new CyrillicToRomanFallback(), new DecoderExceptionFallback()); |
| 49 | + // This is an encoding operation, so we only need to get the encoder. |
| 50 | + Encoder encoder = encoding.GetEncoder(); |
| 51 | + Decoder decoder = encoding.GetDecoder(); |
52 | 52 |
|
53 |
| - // Define buffer to read characters |
54 |
| - char[] buffer = new char[100]; |
55 |
| - int charsRead; |
| 53 | + // Define buffer to read characters |
| 54 | + char[] buffer = new char[100]; |
| 55 | + int charsRead; |
56 | 56 |
|
57 |
| - do |
58 |
| - { |
59 |
| - // Read next 100 characters from input stream. |
60 |
| - charsRead = sr.ReadBlock(buffer, 0, buffer.Length); |
| 57 | + do |
| 58 | + { |
| 59 | + // Read next 100 characters from input stream. |
| 60 | + charsRead = sr.ReadBlock(buffer, 0, buffer.Length); |
61 | 61 |
|
62 |
| - // Encode characters. |
63 |
| - int byteCount = encoder.GetByteCount(buffer, 0, charsRead, false); |
64 |
| - byte[] bytes = new byte[byteCount]; |
65 |
| - int bytesWritten = encoder.GetBytes(buffer, 0, charsRead, bytes, 0, false); |
| 62 | + // Encode characters. |
| 63 | + int byteCount = encoder.GetByteCount(buffer, 0, charsRead, false); |
| 64 | + byte[] bytes = new byte[byteCount]; |
| 65 | + int bytesWritten = encoder.GetBytes(buffer, 0, charsRead, bytes, 0, false); |
66 | 66 |
|
67 |
| - // Decode characters back to Unicode and write to a UTF-8-encoded file. |
68 |
| - char[] charsToWrite = new char[decoder.GetCharCount(bytes, 0, byteCount)]; |
69 |
| - decoder.GetChars(bytes, 0, bytesWritten, charsToWrite, 0); |
70 |
| - sw.Write(charsToWrite); |
71 |
| - } while (charsRead == buffer.Length); |
| 67 | + // Decode characters back to Unicode and write to a UTF-8-encoded file. |
| 68 | + char[] charsToWrite = new char[decoder.GetCharCount(bytes, 0, byteCount)]; |
| 69 | + decoder.GetChars(bytes, 0, bytesWritten, charsToWrite, 0); |
| 70 | + sw.Write(charsToWrite); |
| 71 | + } while (charsRead == buffer.Length); |
| 72 | + } |
72 | 73 | }
|
73 |
| - } |
74 |
| - } |
75 |
| - catch (DirectoryNotFoundException e) |
76 |
| - { |
77 |
| - Console.WriteLine($"Invalid directory: {e.Message}"); |
78 |
| - return; |
79 |
| - } |
80 |
| - catch (IOException e) |
81 |
| - { |
82 |
| - Console.WriteLine($"I/O exception: {e.Message}"); |
83 |
| - return; |
84 |
| - } |
85 |
| - } |
| 74 | + } |
| 75 | + catch (DirectoryNotFoundException e) |
| 76 | + { |
| 77 | + Console.WriteLine($"Invalid directory: {e.Message}"); |
| 78 | + return; |
| 79 | + } |
| 80 | + catch (IOException e) |
| 81 | + { |
| 82 | + Console.WriteLine($"I/O exception: {e.Message}"); |
| 83 | + return; |
| 84 | + } |
| 85 | + } |
86 | 86 |
|
87 |
| - private static void ShowSyntax() |
88 |
| - { |
89 |
| - Console.WriteLine("\nSyntax: CyrillicToRoman <source> <destination>"); |
90 |
| - Console.WriteLine(" where <source> = source filename"); |
91 |
| - Console.WriteLine(" <destination> = destination filename\n"); |
92 |
| - } |
| 87 | + private static void ShowSyntax() |
| 88 | + { |
| 89 | + Console.WriteLine("\nSyntax: CyrillicToRoman <source> <destination>"); |
| 90 | + Console.WriteLine(" where <source> = source filename"); |
| 91 | + Console.WriteLine(" <destination> = destination filename\n"); |
| 92 | + } |
93 | 93 | }
|
0 commit comments