-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Expand file tree
/
Copy pathConsoleModule.cs
More file actions
102 lines (89 loc) · 3.7 KB
/
ConsoleModule.cs
File metadata and controls
102 lines (89 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.IO;
using System.Security.AccessControl;
using System.Text;
using CyrillicToLatin.Common;
class ConsoleModule
{
static void Main()
{
string[] args = Environment.GetCommandLineArgs();
// Get command line arguments.
if (args.Length != 3 || string.IsNullOrWhiteSpace(args[1]) || string.IsNullOrWhiteSpace(args[2]))
{
Console.WriteLine(Common.NoSourceOrDestinationFile);
ShowSyntax();
return;
}
string source = args[1];
string destination = args[2];
if (!File.Exists(source))
{
Console.WriteLine(Common.NoSourceOrDestinationFile);
return;
}
try
{
using (var sr = new StreamReader(source))
{
// Check whether destination file exists and exit if it should not be overwritten.
if (File.Exists(destination))
{
Console.Write("The destination file {1} '{0}'{1}exists. Overwrite it? (Y/N) ",
source, Environment.NewLine);
ConsoleKeyInfo keyPressed = Console.ReadKey(true);
if (Char.ToUpper(keyPressed.KeyChar) == 'Y' | Char.ToUpper(keyPressed.KeyChar) == 'N')
{
Console.WriteLine(keyPressed.KeyChar);
if (Char.ToUpper(keyPressed.KeyChar) == 'N')
return;
}
}
using (var sw = new StreamWriter(destination, false, System.Text.Encoding.UTF8))
{
// Instantiate the encoder
Encoding encoding = Encoding.GetEncoding("us-ascii", new CyrillicToLatinFallback(), new DecoderExceptionFallback());
// This is an encoding operation, so we only need to get the encoder.
Encoder encoder = encoding.GetEncoder();
Decoder decoder = encoding.GetDecoder();
// Define buffer to read characters
char[] buffer = new char[100];
int charsRead;
do
{
// Read next 100 characters from input stream.
charsRead = sr.ReadBlock(buffer, 0, buffer.Length);
// Encode characters.
int byteCount = encoder.GetByteCount(buffer, 0, charsRead, false);
byte[] bytes = new byte[byteCount];
int bytesWritten = encoder.GetBytes(buffer, 0, charsRead, bytes, 0, false);
// Decode characters back to Unicode and write to a UTF-8-encoded file.
char[] charsToWrite = new char[decoder.GetCharCount(bytes, 0, byteCount)];
decoder.GetChars(bytes, 0, bytesWritten, charsToWrite, 0);
sw.Write(charsToWrite);
} while (charsRead == buffer.Length);
}
}
}
catch (DirectoryNotFoundException e)
{
Console.WriteLine(Common.WrongDirectory);
return;
}
catch (IOException e)
{
Console.WriteLine(Common.WrongIO);
return;
}
}
private static void ShowSyntax()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine();
sb.AppendLine("Syntax: CyrillicToRoman <source> <destination>");
sb.AppendLine(" where <source> = source filename");
sb.AppendLine(" <destination> = destination filename");
sb.AppendLine();
Console.WriteLine(sb.ToString());
}
}