diff options
Diffstat (limited to 'WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser')
9 files changed, 275 insertions, 0 deletions
diff --git a/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/CSV Parser.csproj b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/CSV Parser.csproj new file mode 100644 index 0000000..bf48c73 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/CSV Parser.csproj @@ -0,0 +1,13 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>net6.0</TargetFramework> + <RootNamespace>CSV_Parser</RootNamespace> + <LangVersion>7.3</LangVersion> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="System.Memory" Version="4.5.4" /> + </ItemGroup> + +</Project> diff --git a/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/CSV Parser.csproj.meta b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/CSV Parser.csproj.meta new file mode 100644 index 0000000..da4f4a8 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/CSV Parser.csproj.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: c9cc963a877064748a9e6fe4397c807f +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src.meta b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src.meta new file mode 100644 index 0000000..8e65917 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3e17c685928714443973ed558020bdee +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/CSVParser.cs b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/CSVParser.cs new file mode 100644 index 0000000..c886c19 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/CSVParser.cs @@ -0,0 +1,181 @@ +/* + * CSV Parser for C#. + * + * These codes are licensed under CC0. + * https://github.com/yutokun/CSV-Parser + */ + +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace yutokun +{ + public static class CSVParser + { + /// <summary> + /// Load CSV data from specified path. + /// </summary> + /// <param name="path">CSV file path.</param> + /// <param name="delimiter">Delimiter.</param> + /// <param name="encoding">Type of text encoding. (default UTF-8)</param> + /// <returns>Nested list that CSV parsed.</returns> + public static List<List<string>> LoadFromPath(string path, Delimiter delimiter = Delimiter.Auto, Encoding encoding = null) + { + encoding = encoding ?? Encoding.UTF8; + + if (delimiter == Delimiter.Auto) + { + delimiter = EstimateDelimiter(path); + } + + var data = File.ReadAllText(path, encoding); + return Parse(data, delimiter); + } + + /// <summary> + /// Load CSV data asynchronously from specified path. + /// </summary> + /// <param name="path">CSV file path.</param> + /// <param name="delimiter">Delimiter.</param> + /// <param name="encoding">Type of text encoding. (default UTF-8)</param> + /// <returns>Nested list that CSV parsed.</returns> + public static async Task<List<List<string>>> LoadFromPathAsync(string path, Delimiter delimiter = Delimiter.Auto, Encoding encoding = null) + { + encoding = encoding ?? Encoding.UTF8; + + if (delimiter == Delimiter.Auto) + { + delimiter = EstimateDelimiter(path); + } + + using (var reader = new StreamReader(path, encoding)) + { + var data = await reader.ReadToEndAsync(); + return Parse(data, delimiter); + } + } + + static Delimiter EstimateDelimiter(string path) + { + var extension = Path.GetExtension(path); + if (extension.Equals(".csv", StringComparison.OrdinalIgnoreCase)) + { + return Delimiter.Comma; + } + + if (extension.Equals(".tsv", StringComparison.OrdinalIgnoreCase)) + { + return Delimiter.Tab; + } + + throw new Exception($"Delimiter estimation failed. Unknown Extension: {extension}"); + } + + /// <summary> + /// Load CSV data from string. + /// </summary> + /// <param name="data">CSV string</param> + /// <param name="delimiter">Delimiter.</param> + /// <returns>Nested list that CSV parsed.</returns> + public static List<List<string>> LoadFromString(string data, Delimiter delimiter = Delimiter.Comma) + { + if (delimiter == Delimiter.Auto) throw new InvalidEnumArgumentException("Delimiter estimation from string is not supported."); + return Parse(data, delimiter); + } + + static List<List<string>> Parse(string data, Delimiter delimiter) + { + ConvertToCrlf(ref data); + + var sheet = new List<List<string>>(); + var row = new List<string>(); + var cell = new StringBuilder(); + var insideQuoteCell = false; + var start = 0; + + var delimiterSpan = delimiter.ToChar().ToString().AsSpan(); + var crlfSpan = "\r\n".AsSpan(); + var oneDoubleQuotSpan = "\"".AsSpan(); + var twoDoubleQuotSpan = "\"\"".AsSpan(); + + while (start < data.Length) + { + var length = start <= data.Length - 2 ? 2 : 1; + var span = data.AsSpan(start, length); + + if (span.StartsWith(delimiterSpan)) + { + if (insideQuoteCell) + { + cell.Append(delimiter.ToChar()); + } + else + { + AddCell(row, cell); + } + + start += 1; + } + else if (span.StartsWith(crlfSpan)) + { + if (insideQuoteCell) + { + cell.Append("\r\n"); + } + else + { + AddCell(row, cell); + AddRow(sheet, ref row); + } + + start += 2; + } + else if (span.StartsWith(twoDoubleQuotSpan)) + { + cell.Append("\""); + start += 2; + } + else if (span.StartsWith(oneDoubleQuotSpan)) + { + insideQuoteCell = !insideQuoteCell; + start += 1; + } + else + { + cell.Append(span[0]); + start += 1; + } + } + + if (row.Count > 0 || cell.Length > 0) + { + AddCell(row, cell); + AddRow(sheet, ref row); + } + + return sheet; + } + + static void AddCell(List<string> row, StringBuilder cell) + { + row.Add(cell.ToString()); + cell.Length = 0; // Old C#. + } + + static void AddRow(List<List<string>> sheet, ref List<string> row) + { + sheet.Add(row); + row = new List<string>(); + } + + static void ConvertToCrlf(ref string data) + { + data = Regex.Replace(data, @"\r\n|\r|\n", "\r\n"); + } + } +} diff --git a/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/CSVParser.cs.meta b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/CSVParser.cs.meta new file mode 100644 index 0000000..fb3cd58 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/CSVParser.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1dcf43e5bd8e0204580fc6899a086b00 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/Delimiter.cs b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/Delimiter.cs new file mode 100644 index 0000000..b32dcc3 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/Delimiter.cs @@ -0,0 +1,9 @@ +namespace yutokun +{ + public enum Delimiter + { + Auto, + Comma, + Tab + } +} diff --git a/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/Delimiter.cs.meta b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/Delimiter.cs.meta new file mode 100644 index 0000000..8f7cf00 --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/Delimiter.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e0e2bb27fdbb714468ca87aeaf607331 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/DelimiterExtensions.cs b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/DelimiterExtensions.cs new file mode 100644 index 0000000..a38371d --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/DelimiterExtensions.cs @@ -0,0 +1,24 @@ +using System; +using System.ComponentModel; + +namespace yutokun +{ + public static class DelimiterExtensions + { + public static char ToChar(this Delimiter delimiter) + { + // C# 7.3: Unity 2018.2 - 2020.1 Compatible + switch (delimiter) + { + case Delimiter.Auto: + throw new InvalidEnumArgumentException("Could not return char of Delimiter.Auto."); + case Delimiter.Comma: + return ','; + case Delimiter.Tab: + return '\t'; + default: + throw new ArgumentOutOfRangeException(nameof(delimiter), delimiter, null); + } + } + } +} diff --git a/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/DelimiterExtensions.cs.meta b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/DelimiterExtensions.cs.meta new file mode 100644 index 0000000..d98b86d --- /dev/null +++ b/WorldlineKeepers/Assets/ThirdParty/CSV-Parser/CSV Parser/src/DelimiterExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7b9ab5b183dd311459e9893fe735bea6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |