Skip to content

Commit

Permalink
优化解码至 stdout 的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
lc6464 committed Dec 12, 2023
1 parent 1db48e4 commit fec9e11
Showing 1 changed file with 19 additions and 38 deletions.
57 changes: 19 additions & 38 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ 4. 编解码失败
// Read from file
if (args is [.., "-"]) {
// Write to stdout
return !File.Exists(args[1])
? 2
: Helpers.WriteToStdOut(Base16384.EncodeFromFileToStream, new FileInfo(args[1]));
return File.Exists(args[1])
? Helpers.WriteToStdOut(Base16384.EncodeFromFileToStream, new FileInfo(args[1]))
: 2;
} else if (args.Length == 2) {
// Write to .decoded file
if (!File.Exists(args[1])) {
Expand Down Expand Up @@ -96,28 +96,20 @@ 4. 编解码失败
using var stdin = Console.OpenStandardInput();
if (args.Length == 2 || args[2] == "-") {
// Write to stdout
try {
using var stdout = Console.OpenStandardOutput();
try {
Base16384.DecodeToStream(stdin, stdout);
} catch {
return 4;
}
} catch {
return 3;
}
} else {
// Write to file
FileInfo info = new(args[2]);
Console.Write($"<stdin> -> {info.Name} ... ");
try {
Base16384.DecodeToNewFile(stdin, info);
} catch {
Console.WriteLine("Failed.");
return 4;
}
Console.WriteLine("Done.");
return Helpers.WriteToStdOut(Base16384.DecodeToStream, stdin);
}

// Write to file
FileInfo info = new(args[2]);
Console.Write($"<stdin> -> {info.Name} ... ");
try {
Base16384.DecodeToNewFile(stdin, info);
} catch {
Console.WriteLine("Failed.");
return 4;
}
Console.WriteLine("Done.");

} catch {
if (args.Length != 2 && args[2] != "-") {
Console.WriteLine("Can not open stdin.");
Expand All @@ -128,20 +120,9 @@ 4. 编解码失败
// Read from file
if (args is [.., "-"]) {
// Write to stdout
if (!File.Exists(args[1])) {
Console.WriteLine("Source file not found.");
return 2;
}
try {
using var stdout = Console.OpenStandardOutput();
try {
Base16384.DecodeFromFileToStream(new(args[1]), stdout);
} catch {
return 4;
}
} catch {
return 3;
}
return File.Exists(args[1])
? Helpers.WriteToStdOut(Base16384.DecodeFromFileToStream, new FileInfo(args[1]))
: 2;
} else if (args.Length == 2) {
// Write to .decoded file
if (!File.Exists(args[1])) {
Expand Down

0 comments on commit fec9e11

Please sign in to comment.