128 lines
4.5 KiB
C#
128 lines
4.5 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace File_Folder_Helper.Day;
|
|
|
|
internal static partial class Helper20240106
|
|
{
|
|
|
|
[JsonSourceGenerationOptions(WriteIndented = true)]
|
|
[JsonSerializable(typeof(Dictionary<string, Dictionary<string, string>>))]
|
|
private partial class DictionaryDictionarySourceGenerationContext : JsonSerializerContext
|
|
{
|
|
}
|
|
|
|
private record Record(string Key, Dictionary<string, string> KeyValuePairs);
|
|
|
|
private static Dictionary<string, Dictionary<string, string>> GetKeyValuePairs(List<Record> collection, bool replaceFound)
|
|
{
|
|
Dictionary<string, Dictionary<string, string>> results = [];
|
|
if (replaceFound)
|
|
{
|
|
foreach ((string key, Dictionary<string, string> keyValuePairs) in collection)
|
|
_ = results.TryAdd(key, keyValuePairs);
|
|
}
|
|
else
|
|
{
|
|
foreach ((string key, Dictionary<string, string> keyValuePairs) in collection.OrderBy(l => l.Key))
|
|
_ = results.TryAdd(key, keyValuePairs);
|
|
}
|
|
return results;
|
|
}
|
|
|
|
private static int? GetHeaderLine(string[] lines)
|
|
{
|
|
int? headerLine = null;
|
|
for (int i = 0; i < lines.Length - 1; i++)
|
|
{
|
|
if (!lines[i].Contains('\t'))
|
|
continue;
|
|
headerLine = i;
|
|
}
|
|
return headerLine;
|
|
}
|
|
|
|
private static Dictionary<string, Dictionary<string, string>> GetKeyValuePairs(int keyIndex, int keyLength, string replace, string[] headers, string[] lines, int headerLine)
|
|
{
|
|
Dictionary<string, Dictionary<string, string>> results;
|
|
string? key;
|
|
Record record;
|
|
bool replaceFound = false;
|
|
List<Record> collection = [];
|
|
Dictionary<string, string> keyValuePairs;
|
|
for (int i = headerLine + 1; i < lines.Length; i++)
|
|
{
|
|
key = null;
|
|
keyValuePairs = [];
|
|
for (int j = 0; j < headers.Length; j++)
|
|
{
|
|
if (j > 0)
|
|
i++;
|
|
if (lines.Length <= i)
|
|
{
|
|
keyValuePairs.Clear();
|
|
break;
|
|
}
|
|
if (j == keyIndex)
|
|
{
|
|
key = lines[i];
|
|
if (key.Length != keyLength)
|
|
{
|
|
keyValuePairs.Clear();
|
|
break;
|
|
}
|
|
}
|
|
if (lines[i] != replace)
|
|
_ = keyValuePairs.TryAdd(headers[j], lines[i]);
|
|
else
|
|
{
|
|
if (!replaceFound)
|
|
replaceFound = true;
|
|
_ = keyValuePairs.TryAdd(headers[j], lines[i]);
|
|
j++;
|
|
_ = keyValuePairs.TryAdd(headers[j], lines[i]);
|
|
}
|
|
}
|
|
if (keyValuePairs.Count != headers.Length)
|
|
continue;
|
|
key ??= "-";
|
|
record = new(key, keyValuePairs);
|
|
collection.Add(record);
|
|
}
|
|
results = GetKeyValuePairs(collection, replaceFound);
|
|
return results;
|
|
}
|
|
|
|
internal static void TextToJson(ILogger<Worker> logger, List<string> args)
|
|
{
|
|
string json;
|
|
string[] lines;
|
|
int? headerLine;
|
|
FileInfo fileInfo;
|
|
string replace = args[6];
|
|
int keyIndex = int.Parse(args[4]);
|
|
int keyLength = int.Parse(args[5]);
|
|
string[] headers = args[7].Split(',');
|
|
string[] txtFiles = Directory.GetFiles(args[0], args[2]);
|
|
Dictionary<string, Dictionary<string, string>> keyValuePairs;
|
|
foreach (string txtFile in txtFiles)
|
|
{
|
|
lines = File.ReadAllLines(txtFile);
|
|
if (lines.Length == 0)
|
|
continue;
|
|
headerLine = GetHeaderLine(lines);
|
|
if (headerLine is null)
|
|
continue;
|
|
fileInfo = new(txtFile);
|
|
keyValuePairs = GetKeyValuePairs(keyIndex, keyLength, replace, headers, lines, headerLine.Value);
|
|
if (keyValuePairs.Count == 0)
|
|
continue;
|
|
json = JsonSerializer.Serialize(keyValuePairs, DictionaryDictionarySourceGenerationContext.Default.DictionaryStringDictionaryStringString);
|
|
logger.LogInformation("Writing output file...");
|
|
File.WriteAllText($"{txtFile}-{fileInfo.LastWriteTime.Ticks}.json", json);
|
|
File.WriteAllText(txtFile, string.Empty);
|
|
}
|
|
}
|
|
|
|
} |