Skip to content

Commit

Permalink
完成相关逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
lc6464 committed Mar 4, 2024
1 parent f1f3009 commit 5f0a452
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Base16384.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Version>2.0.0.0-RC.1</Version>
<Version>2.0.0-RC.1</Version>
<Platforms>x64</Platforms>
</PropertyGroup>
<ItemGroup>
Expand Down
110 changes: 97 additions & 13 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
错误代码:
1. 无法解析命令行参数
2. 无法读取文件(可能是文件不存在
2. 无法读写文件(可能是文件不存在或文件名存在问题
3. Standard IO Stream 开启失败
4. 编解码失败
5. 无法创建输出文件夹(可能是输出文件夹是一个已存在的文件)
Expand Down Expand Up @@ -30,8 +30,12 @@ 4. 编解码失败

// Write to file

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

return output.Exists
? Helpers.PrintErrorMessage("The output file already exists.", null, 2)
: Helpers.WriteToFile(isEncodeMode ? Base16384.EncodeToNewFile : Base16384.DecodeToNewFile,
stdin, output, "<stdin>");

} catch (Exception e) {

Expand All @@ -52,19 +56,70 @@ 4. 编解码失败

}


// Write to file

var codedString = $"{(isEncodeMode ? "en" : "de")}coded";

if (!isDefaultOutput && args[2].Contains(',')) {
return Helpers.PrintErrorMessage("Output file or directory cannot be a list.", null, 2);
}

// Encode or decode multiple files
if (args[1].Contains(',')) {
if (args[1].StartsWith(',') || args[1].EndsWith(',')) {
return Helpers.PrintErrorMessage("Invalid file list.", null, 2);
}

DirectoryInfo outputDirectoryInfo = new(isDefaultOutput ? codedString : args[2]);

if (!outputDirectoryInfo.Exists) { // Try to create output directory
if (File.Exists(outputDirectoryInfo.FullName)) {
return Helpers.PrintErrorMessage("Output directory is an existing file.", null, 5);
}

try {
outputDirectoryInfo.Create();
} catch (Exception e) {
return Helpers.PrintException("Failed to create output directory.", e, 5);
}
}

FileInfo input, output;

// foreach files in the list
foreach (var file in args[1].Split(',')) {
input = new(file);
output = new(Path.Combine(outputDirectoryInfo.FullName, input.Name));

if (output.Exists) {
Console.WriteLine($"{input.Name} -> {output.Name} ... The output file in the directory {outputDirectoryInfo.Name} already exists.");
continue;
}

if (!input.Exists) {
Console.WriteLine($"{input.Name} -> Source file not found.");
continue;
}

Helpers.Execute(isEncodeMode ? Base16384.EncodeFromFileToNewFile : Base16384.DecodeFromFileToNewFile,
input, output, input.Name, output.Name);
}

return 0;

}

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

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

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

if (!outputDirectoryInfo.Exists) { // Try to create output directory
if (File.Exists(output)) {
if (File.Exists(outputDirectoryInfo.FullName)) {
return Helpers.PrintErrorMessage("Output directory is an existing file.", null, 5);
}

Expand All @@ -75,18 +130,47 @@ 4. 编解码失败
}
}

// 此处应该遍历文件夹
FileInfo output;
string relativePath;

// foreach files in the list
foreach (var input in inputDirectoryInfo.EnumerateFiles("*", SearchOption.AllDirectories)) {
relativePath = Path.GetRelativePath(inputDirectoryInfo.FullName, input.FullName);
output = new(Path.Combine(outputDirectoryInfo.FullName, relativePath));

if (output.Exists) {
Console.WriteLine($"{input.Name} -> {output.Name} ... The output file in the directory {Path.GetDirectoryName(relativePath)} already exists.");
continue;
}

if (!input.Exists) {
Console.WriteLine($"{input.Name} -> Source file not found.");
continue;
}

Helpers.Execute(isEncodeMode ? Base16384.EncodeFromFileToNewFile : Base16384.DecodeFromFileToNewFile,
input, output, input.Name, output.Name);
}

return 0;
}

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

// Encode single file

// Encode or decode single file
FileInfo inputFileInfo = new(args[1]),
outputFileInfo = new(isDefaultOutput ? $"{args[1]}.{(isEncodeMode ? "en" : "de")}coded" : args[2]);
outputFileInfo = new(isDefaultOutput ? $"{args[1]}.{codedString}" : args[2]);

if (outputFileInfo.Exists) {
return Helpers.PrintErrorMessage($"{inputFileInfo.Name} -> {outputFileInfo.Name} ... The output file already exists.", null, 2);
}

if (!inputFileInfo.Exists) {
return Helpers.PrintErrorMessage(null, $"{inputFileInfo.Name} -> Source file not found.", 2);
}

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

return Helpers.Execute(isEncodeMode ? Base16384.EncodeFromFileToNewFile : Base16384.DecodeFromFileToNewFile,
inputFileInfo, outputFileInfo, inputFileInfo.Name, outputFileInfo.Name);
return 0;
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
```
Base16384.Net <"e" | "d"> <source | "-"> [out | "-"]
```
- 参数一:e 为编码;d 为解码
- 参数二:源文件(夹)路径,输入 `-` 则表示从标准输入读取数据。
- 参数三:输出文件(夹)路径,可选,其中输出到标准输出时无信息提示
- 参数一:e 为编码;d 为解码
- 参数二:源文件(夹)路径,可以传入逗号分隔的文件列表,输入 `-` 则表示从标准输入读取数据。
- 参数三:输出文件(夹)路径,可选,其中输出到标准输出时无信息提示
- 缺省
- 输入为标准输入时,结果输出到标准输出
- 输入为文件时,结果写入 `源文件名.encoded` 或 `源文件名.decoded`
- 输入为文件夹时,结果写入到 `encoded` 或 `decoded` 文件夹内相应结构的文件内
- 输入为文件夹或逗号分隔的文件列表时,结果写入到 `encoded` 或 `decoded` 文件夹内相应结构的文件内
- `-` 表示输出到标准输出,输入为文件夹时无效
- 其他
- 输入为文件时,输出到指定文件
- 输入为文件夹时,输出到指定文件夹
- 输入为文件夹或逗号分隔的文件列表时,输出到指定文件夹
- 示例
- 编码
- `Base16384.Net.exe e C:\Users\lc6464\Desktop\test.mp4 C:\Users\lc6464\Desktop\test.mp4.txt`
Expand Down

0 comments on commit 5f0a452

Please sign in to comment.