91 lines
2.4 KiB
C#
91 lines
2.4 KiB
C#
using System.Text.RegularExpressions;
|
|
|
|
namespace ConsoleApp2.Helpers;
|
|
|
|
public static partial class DumpDataHelper
|
|
{
|
|
[GeneratedRegex(@"'.+\.dat'")]
|
|
private static partial Regex MatchDatFile();
|
|
[GeneratedRegex(@"\([^)]*\)")]
|
|
private static partial Regex MatchBrackets();
|
|
|
|
|
|
public static async Task<string[]> GetCsvHeadersFromSqlFileAsync(string filePath)
|
|
{
|
|
var txt = await File.ReadAllTextAsync(filePath);
|
|
var match = MatchBrackets().Match(txt);
|
|
|
|
return ParseHeader(match.ValueSpan);
|
|
}
|
|
|
|
private static string[] ParseHeader(ReadOnlySpan<char> headerStr)
|
|
{
|
|
headerStr = headerStr[1..^1];
|
|
Span<Range> ranges = stackalloc Range[50];
|
|
var count = headerStr.Split(ranges, ',');
|
|
var arr = new string[count];
|
|
|
|
for (var i = 0; i < count; i++)
|
|
{
|
|
arr[i] = headerStr[ranges[i]].Trim("@`").ToString(); // 消除列名的反引号,如果是变量则消除@
|
|
}
|
|
|
|
return arr;
|
|
}
|
|
|
|
|
|
public static string GetTableName(ReadOnlySpan<char> filePath)
|
|
{
|
|
filePath = filePath[(filePath.LastIndexOf('\\') + 1)..];
|
|
var firstDotIdx = -1;
|
|
var secondDotIdx = -1;
|
|
var times = 0;
|
|
for (var i = 0; i < filePath.Length; i++)
|
|
{
|
|
if (filePath[i] == '.')
|
|
{
|
|
++times;
|
|
if(times == 1)
|
|
firstDotIdx = i;
|
|
if (times == 2)
|
|
{
|
|
secondDotIdx = i;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return filePath[(firstDotIdx+1)..secondDotIdx].ToString();
|
|
}
|
|
|
|
public static async Task<string[]> GetCsvFileNamesFromSqlFileAsync(string filePath)
|
|
{
|
|
var txt = await File.ReadAllTextAsync(filePath);
|
|
var matches = MatchDatFile().Matches(txt);
|
|
return matches.Select(match => match.ValueSpan[1..^1].ToString()).ToArray();
|
|
}
|
|
|
|
public static bool CheckHexField(string? str)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(str))
|
|
return false;
|
|
|
|
if (str.StartsWith('\"'))
|
|
return false;
|
|
|
|
var isDigit = true;
|
|
|
|
foreach (var c in str)
|
|
{
|
|
if (!char.IsAsciiHexDigit(c))
|
|
return false;
|
|
if (!char.IsNumber(c))
|
|
isDigit = false;
|
|
}
|
|
|
|
if (isDigit)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
} |