85 lines
3.3 KiB
C#

namespace Adaptation.FileHandlers.csv;
public class CSV
{
#nullable disable
#nullable enable
#pragma warning disable CA1834
public string? FileVersion { get; set; }
public Info? Info { get; set; }
public Setup? Setup { get; set; }
public LayerHeader? LayerHeader { get; set; }
public ProfileHeader? ProfileHeader { get; set; }
public Calibration? Calibration { get; set; }
public RawData? RawData { get; set; }
internal static CSV GetCSV(string path)
{
CSV result;
int? endInfo = null;
int? endSetup = null;
int? endLayers = null;
int? startInfo = null;
int? startSetup = null;
int? endProfiles = null;
int? startLayers = null;
int? startRawData = null;
int? startProfiles = null;
int? endCalibration = null;
int? startCalibration = null;
#if NET
string[] lines = File.ReadAllLines(path, System.Text.Encoding.Latin1); // µ³®
# else
string[] lines = File.ReadAllLines(path, System.Text.Encoding.GetEncoding("ISO-8859-1")); // µ³®
# endif
string? fileVersion = lines.Length == 0 ? null : GetFileVersion(lines.First());
for (int i = 1; i < lines.Length; i++)
{
if (lines[i].StartsWith("--INFO--"))
startInfo = i + 1;
else if (lines[i].StartsWith("--SETUP--"))
(endInfo, startSetup) = (i, i + 1);
else if (lines[i].StartsWith("--LAYERS--"))
(endSetup, startLayers) = (i, i + 1);
else if (lines[i].StartsWith("--PROFILES--"))
(endLayers, startProfiles) = (i, i + 1);
else if (lines[i].StartsWith("--CALIBRATION--"))
(endProfiles, startCalibration) = (i, i + 1);
else if (lines[i].StartsWith("--RAWDATA--"))
(endCalibration, startRawData) = (i, i + 1);
}
RawData? rawData = startRawData is null ? null : RawData.GetRawData(lines, startRawData.Value, lines.Length);
Info? info = startInfo is null || endInfo is null ? null : Info.GetInfo(lines, startInfo.Value, endInfo.Value);
Setup? setup = startSetup is null || endSetup is null ? null : Setup.GetSetup(lines, startSetup.Value, endSetup.Value);
LayerHeader? layerHeader = startLayers is null || endLayers is null ? null : LayerHeader.GetLayerHeader(lines, startLayers.Value, endLayers.Value);
ProfileHeader? profileHeader = startProfiles is null || endProfiles is null ? null : ProfileHeader.GetProfileHeader(lines, startProfiles.Value, endProfiles.Value);
Calibration? calibration = startCalibration is null || endCalibration is null ? null : Calibration.GetCalibration(lines, startCalibration.Value, endCalibration.Value);
result = new()
{
FileVersion = fileVersion,
Info = info,
Setup = setup,
LayerHeader = layerHeader,
ProfileHeader = profileHeader,
Calibration = calibration,
RawData = rawData,
};
return result;
}
private static string GetFileVersion(string line)
{
string result;
string[] segments = line.Split(',');
if (segments.Length < 2)
result = string.Empty;
else
result = segments.Last();
return result;
}
}