85 lines
3.4 KiB
C#
85 lines
3.4 KiB
C#
using Microsoft.Extensions.Logging;
|
|
|
|
namespace File_Folder_Helper.ADO2025.PI5;
|
|
|
|
internal static partial class Helper20250306 {
|
|
|
|
internal static void ProcessDataStandardFormatToJson(ILogger<Worker> logger, List<string> args) {
|
|
string searchPattern = args[2];
|
|
string sourceDirectory = Path.GetFullPath(args[0]);
|
|
string[] files = Directory.GetFiles(sourceDirectory, searchPattern, SearchOption.TopDirectoryOnly);
|
|
if (files.Length != 1)
|
|
logger.LogWarning("<{files}>(s)", files.Length);
|
|
else
|
|
ProcessDataStandardFormatToJson(logger, files[0]);
|
|
}
|
|
|
|
private static void ProcessDataStandardFormatToJson(ILogger<Worker> logger, string file) {
|
|
string[] lines = File.ReadAllLines(file);
|
|
int? columnTitlesLine = GetProcessDataStandardFormatColumnTitlesLine(lines);
|
|
if (columnTitlesLine is null)
|
|
logger.LogWarning("<{columnTitlesLine}> is null", nameof(columnTitlesLine));
|
|
else {
|
|
string? text = ProcessDataStandardFormatToLastDataLine(lines, columnTitlesLine.Value);
|
|
File.WriteAllText(Path.Combine(".vscode", "helper", ".lbl"), text);
|
|
if (lines.Length < columnTitlesLine.Value + 1)
|
|
logger.LogWarning("<{lines}>(s)", lines.Length);
|
|
else {
|
|
string json = ProcessDataStandardFormatToJson(columnTitlesLine.Value, [], lines);
|
|
File.WriteAllText(Path.Combine(".vscode", "helper", ".json"), json);
|
|
}
|
|
}
|
|
}
|
|
|
|
private static int? GetProcessDataStandardFormatColumnTitlesLine(string[] lines) {
|
|
int? result = null;
|
|
bool foundEndOfFile = false;
|
|
for (int i = 0; i < lines.Length; i++) {
|
|
if (lines[i] == "EOF")
|
|
foundEndOfFile = true;
|
|
if (foundEndOfFile && lines[i].StartsWith("END_OFFSET") && i + 3 < lines.Length) {
|
|
result = i + 2;
|
|
break;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static string? ProcessDataStandardFormatToLastDataLine(string[] lines, int columnTitlesLine) {
|
|
string? result = null;
|
|
for (int i = columnTitlesLine + 1; i < lines.Length; i++) {
|
|
if (lines[i].StartsWith("NUM_DATA_ROWS")) {
|
|
result = lines[i - 2];
|
|
break;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static string ProcessDataStandardFormatToJson(int columnTitlesLine, string[] columns, string[] lines) {
|
|
#pragma warning disable CA1845, IDE0057
|
|
string result = "[\n";
|
|
string line;
|
|
string value;
|
|
string[] segments;
|
|
if (columns.Length == 0)
|
|
columns = lines[columnTitlesLine].Trim().Split('|');
|
|
int columnsLength = columns.Length - 2;
|
|
for (int i = columnTitlesLine + 1; i < lines.Length; i++) {
|
|
line = "{";
|
|
segments = lines[i].Trim().Split('|');
|
|
if (segments.Length != columnsLength)
|
|
continue;
|
|
for (int c = 1; c < segments.Length; c++) {
|
|
value = segments[c].Replace("\"", "\\\"").Replace("\\", "\\\\");
|
|
line += '"' + columns[c].Trim('"') + '"' + ':' + '"' + value + '"' + ',';
|
|
}
|
|
line = line.Substring(0, line.Length - 1) + '}' + ',' + '\n';
|
|
result += line;
|
|
}
|
|
result = result.Substring(0, result.Length - 2) + ']';
|
|
return result;
|
|
#pragma warning restore CA1845, IDE0057
|
|
}
|
|
|
|
} |