Skip to content

Commit

Permalink
重构编解码逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
lc6464 committed Feb 19, 2024
1 parent 8248e56 commit 05d6276
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 162 deletions.
1 change: 1 addition & 0 deletions Base16384.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.0.0</Version>
<Platforms>x64</Platforms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="LC6464.Base16384" Version="2.1.0" />
Expand Down
59 changes: 39 additions & 20 deletions Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
namespace ConsoleHelpers;
using System.Diagnostics.CodeAnalysis;

namespace ConsoleHelpers;

public static class Helpers {
public static int WriteToStdOut<T>(Func<T, Stream, long> func, T input, Stream? stdout = null) {
try {
stdout ??= Console.OpenStandardOutput();
try {
func(input, stdout);
} catch {
return 4;
} finally {
stdout.Dispose();
}
} catch {
return 3;
public static readonly string? debugEnv = Environment.GetEnvironmentVariable("Base16384_Net_Debug");
public static readonly bool DEBUG = debugEnv is not null && debugEnv.ToLower() is not "false" and not "0";


public static int PrintErrorMessage(string? message, string? debugMessage = null, int exitCode = 0) {
if (message is not null) {
Console.WriteLine(message);
}
return 0;
if (DEBUG && !string.IsNullOrWhiteSpace(debugMessage)) {
Console.Error.WriteLine(debugMessage);
}
return exitCode;
}

public static int WriteToFile<T>(Func<T, FileInfo, long> func, T input, string inputName, FileInfo output) {
Console.Write($"{inputName} -> {output.Name} ... ");
public static int PrintException(string? message, [NotNull] Exception e, int exitCode = 0) =>
PrintErrorMessage(message, e.ToString(), exitCode);


public static int Execute<TIn, TOut>(Func<TIn, TOut, long> func, TIn input, TOut output, string? inputName, string? outputName, bool isStdOut = false) {
if (!isStdOut) {
Console.Write($"{inputName} -> {outputName} ... ");
}
try {
func(input, output);
} catch {
Console.WriteLine("Failed.");
return 4;
} catch (Exception e) {
return PrintException(isStdOut ? null : "Failed.", e, 4);
}
if (!isStdOut) {
Console.WriteLine("Done.");
}
Console.WriteLine("Done.");
return 0;
}

public static int WriteToStdOut<T>(Func<T, Stream, long> func, T input) {
try {
using var stdout = Console.OpenStandardOutput();
return Execute(func, input, stdout, null, null, true);
} catch (Exception e) {
return PrintException(null, e, 3);
}
}

public static int WriteToFile<T>(Func<T, FileInfo, long> func, T input, FileInfo output, string inputName) =>
Execute(func, input, output, inputName, output.Name);
}
207 changes: 65 additions & 142 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,165 +5,88 @@ 1. 无法解析命令行参数
2. 无法读取文件(可能是文件不存在)
3. Standard IO Stream 开启失败
4. 编解码失败
5. 无法创建输出文件夹(可能是输出文件夹是一个已存在的文件)
*/


if (args.Length is not 2 and not 3 || args is not ["e", ..] and not ["d", ..]) {
Console.WriteLine("Usage: Base16384.Net.exe <\"e\" | \"d\"> <source | \"-\"> [out | \"-\"]");
return 1;
}

if (args[0] == "e") {
// Encode mode
if (args[1] == "-") {
// Read from stdin
try {
using var stdin = Console.OpenStandardInput();
if (args.Length == 2 || args[2] == "-") {
// Write to stdout
return Helpers.WriteToStdOut(Base16384.EncodeToStream, stdin);
}
bool readFromStdin = args[1] == "-",
isDefaultOutput = args.Length == 2,
writeToStdout = (readFromStdin && isDefaultOutput) || (args is [string, string, "-"]), // `x -` or `x x -`
isEncodeMode = args[0] == "e"; // Encode mode or decode mode

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

} catch {
if (args.Length != 2 && args[2] != "-") {
Console.WriteLine("Can not open stdin.");
}
return 3;
}
} else {
// Read from file
if (args is [.., "-"]) {
// Write to stdout
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])) {
if (Directory.Exists(args[1])) {
// 此处应该遍历文件夹
return 0;
}
Console.WriteLine("Source file not found.");
return 2;
}
FileInfo info = new(args[1]);
Console.Write($"{info.Name} -> {info.Name}.encoded ... ");
try {
Base16384.EncodeFromFileToNewFile(info, new($"{args[1]}.encoded"));
} catch {
Console.WriteLine("Failed.");
return 4;
}
Console.WriteLine("Done.");
} else {
// Write to file
if (!File.Exists(args[1])) {
if (Directory.Exists(args[1])) {
// 此处应该遍历文件夹
return 0;
}
Console.WriteLine("Source file not found.");
return 2;
}
FileInfo info1 = new(args[1]),
info2 = new(args[2]);
Console.Write($"{info1.Name} -> {info2.Name} ... ");
try {
Base16384.EncodeFromFileToNewFile(info1, info2);
} catch {
Console.WriteLine("Failed.");
return 4;
}
Console.WriteLine("Done.");
if (readFromStdin) { // Read from stdin
try {
using var stdin = Console.OpenStandardInput();

if (writeToStdout) { // Write to stdout
return Helpers.WriteToStdOut(isEncodeMode ? Base16384.EncodeToStream : Base16384.DecodeToStream, stdin);
}

// Write to file

return Helpers.WriteToFile(isEncodeMode ? Base16384.EncodeToNewFile : Base16384.DecodeToNewFile,
stdin, new(args[2]), "<stdin>");

} catch (Exception e) {

// If write to stdout, do not print message
return Helpers.PrintException(writeToStdout ? null : "Can not open stdin.", e, 3);

}
} else {
// Decode mode
if (args[1] == "-") {
// Read from stdin
try {
using var stdin = Console.OpenStandardInput();
if (args.Length == 2 || args[2] == "-") {
// Write to stdout
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.");
// Read from file

} catch {
if (args.Length != 2 && args[2] != "-") {
Console.WriteLine("Can not open stdin.");
}
return 3;
}
} else {
// Read from file
if (args is [.., "-"]) {
// Write to stdout
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])) {
if (Directory.Exists(args[1])) {
// 此处应该遍历文件夹
return 0;
}
Console.WriteLine("Source file not found.");
return 2;
}
FileInfo info = new(args[1]);
Console.Write($"{info.Name} -> {info.Name}.decoded ... ");
try {
Base16384.DecodeFromFileToNewFile(info, new($"{args[1]}.decoded"));
} catch {
Console.WriteLine("Failed.");
return 4;
}
Console.WriteLine("Done.");
} else {
// Write to file
if (!File.Exists(args[1])) {
if (Directory.Exists(args[1])) {
// 此处应该遍历文件夹
return 0;
}
Console.WriteLine("Source file not found.");
return 2;
if (writeToStdout) { // Write to stdout

return File.Exists(args[1])
? Helpers.WriteToStdOut(isEncodeMode ? Base16384.EncodeFromFileToStream : Base16384.DecodeFromFileToStream,
new FileInfo(args[1]))
: Helpers.PrintErrorMessage(null, "Source file not found.", 2);

}

// Write to file

// File not found or Directory exists
if (!File.Exists(args[1])) {

if (Directory.Exists(args[1])) { // Encode all files in the directory

var output = isDefaultOutput ? $"{(isEncodeMode ? "en" : "de")}coded" : args[2];
DirectoryInfo inputDirectoryInfo = new(args[1]),
outputDirectoryInfo = new(output);

if (!outputDirectoryInfo.Exists) { // Try to create output directory
if (File.Exists(output)) {
return Helpers.PrintErrorMessage("Output directory is an existing file.", null, 5);
}
FileInfo info1 = new(args[1]),
info2 = new(args[2]);
Console.Write($"{info1.Name} -> {info2.Name} ... ");

try {
Base16384.DecodeFromFileToNewFile(info1, info2);
} catch {
Console.WriteLine("Failed.");
return 4;
outputDirectoryInfo.Create();
} catch (Exception e) {
return Helpers.PrintException("Failed to create output directory.", e, 5);
}
Console.WriteLine("Done.");
}

// 此处应该遍历文件夹

return 0;
}

return Helpers.PrintErrorMessage("Source file not found.", null, 2);
}

return 0;
// Encode single file

FileInfo inputFileInfo = new(args[1]),
outputFileInfo = new(isDefaultOutput ? $"{args[1]}.{(isEncodeMode ? "en" : "de")}coded" : args[2]);

return Helpers.Execute(isEncodeMode ? Base16384.EncodeFromFileToNewFile : Base16384.DecodeFromFileToNewFile,
inputFileInfo, outputFileInfo, inputFileInfo.Name, outputFileInfo.Name);

0 comments on commit 05d6276

Please sign in to comment.