| | | 1 | | // <copyright file="LineReader.cs" company="Jérémy Ansel"> |
| | | 2 | | // Copyright (c) 2017, 2019 Jérémy Ansel |
| | | 3 | | // </copyright> |
| | | 4 | | // <license> |
| | | 5 | | // Licensed under the MIT license. See LICENSE.txt |
| | | 6 | | // </license> |
| | | 7 | | |
| | | 8 | | #if !NET6_0_OR_GREATER |
| | | 9 | | |
| | | 10 | | using System.Text; |
| | | 11 | | |
| | | 12 | | namespace JeremyAnsel.Media.WavefrontObj |
| | | 13 | | { |
| | | 14 | | internal class LineReader |
| | | 15 | | { |
| | 1 | 16 | | public static readonly char[] LineSeparators = new char[] { ' ', '\t' }; |
| | | 17 | | |
| | 1391 | 18 | | public List<string> HeaderTextLines { get; } = new List<string>(); |
| | | 19 | | |
| | | 20 | | public IEnumerable<string> Read(Stream stream) |
| | | 21 | | { |
| | 717 | 22 | | using (var reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true)) |
| | | 23 | | { |
| | 717 | 24 | | string line = string.Empty; |
| | 717 | 25 | | bool isMultiLine = false; |
| | 717 | 26 | | bool readHeaderText = true; |
| | | 27 | | |
| | 2224 | 28 | | while (!reader.EndOfStream) |
| | | 29 | | { |
| | 1818 | 30 | | string? currentLine = reader.ReadLine(); |
| | | 31 | | |
| | 1818 | 32 | | if (string.IsNullOrWhiteSpace(currentLine)) |
| | | 33 | | { |
| | 259 | 34 | | if (readHeaderText) |
| | | 35 | | { |
| | 259 | 36 | | this.HeaderTextLines.Add(string.Empty); |
| | | 37 | | } |
| | | 38 | | |
| | 259 | 39 | | continue; |
| | | 40 | | } |
| | | 41 | | |
| | 1559 | 42 | | if (isMultiLine) |
| | | 43 | | { |
| | 7 | 44 | | line = line.Substring(0, line.Length - 1) + currentLine; |
| | | 45 | | } |
| | | 46 | | else |
| | | 47 | | { |
| | 1552 | 48 | | line = currentLine; |
| | | 49 | | } |
| | | 50 | | |
| | 1559 | 51 | | line = line.Trim(); |
| | | 52 | | |
| | 1559 | 53 | | isMultiLine = line.EndsWith("\\", StringComparison.Ordinal); |
| | | 54 | | |
| | 1559 | 55 | | if (isMultiLine) |
| | | 56 | | { |
| | | 57 | | continue; |
| | | 58 | | } |
| | | 59 | | |
| | 1552 | 60 | | int commentIndex = line.IndexOf('#'); |
| | | 61 | | |
| | 1552 | 62 | | if (commentIndex == 0) |
| | | 63 | | { |
| | 11 | 64 | | if (readHeaderText) |
| | | 65 | | { |
| | 9 | 66 | | this.HeaderTextLines.Add(line.Substring(1)); |
| | | 67 | | } |
| | | 68 | | |
| | 9 | 69 | | continue; |
| | | 70 | | } |
| | | 71 | | |
| | 1541 | 72 | | if (readHeaderText) |
| | | 73 | | { |
| | 716 | 74 | | readHeaderText = false; |
| | | 75 | | } |
| | | 76 | | |
| | 1541 | 77 | | if (commentIndex != -1) |
| | | 78 | | { |
| | 2 | 79 | | line = line.Substring(0, commentIndex); |
| | | 80 | | } |
| | | 81 | | |
| | 1541 | 82 | | yield return line; |
| | | 83 | | } |
| | 406 | 84 | | } |
| | 406 | 85 | | } |
| | | 86 | | } |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | #endif |
| | | 90 | | |