| | 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 | |
|
| 1123 | 18 | | public List<string> HeaderTextLines { get; } = new List<string>(); |
| | 19 | |
|
| | 20 | | public IEnumerable<string> Read(Stream stream) |
| | 21 | | { |
| 593 | 22 | | using (var reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true)) |
| | 23 | | { |
| 593 | 24 | | string line = string.Empty; |
| 593 | 25 | | bool isMultiLine = false; |
| 593 | 26 | | bool readHeaderText = true; |
| | 27 | |
|
| 1835 | 28 | | while (!reader.EndOfStream) |
| | 29 | | { |
| 1553 | 30 | | string? currentLine = reader.ReadLine(); |
| | 31 | |
|
| 1553 | 32 | | if (string.IsNullOrWhiteSpace(currentLine)) |
| | 33 | | { |
| 239 | 34 | | if (readHeaderText) |
| | 35 | | { |
| 239 | 36 | | this.HeaderTextLines.Add(string.Empty); |
| | 37 | | } |
| | 38 | |
|
| 239 | 39 | | continue; |
| | 40 | | } |
| | 41 | |
|
| 1314 | 42 | | if (isMultiLine) |
| | 43 | | { |
| 7 | 44 | | line = line.Substring(0, line.Length - 1) + currentLine; |
| | 45 | | } |
| | 46 | | else |
| | 47 | | { |
| 1307 | 48 | | line = currentLine; |
| | 49 | | } |
| | 50 | |
|
| 1314 | 51 | | line = line.Trim(); |
| | 52 | |
|
| 1314 | 53 | | isMultiLine = line.EndsWith("\\", StringComparison.Ordinal); |
| | 54 | |
|
| 1314 | 55 | | if (isMultiLine) |
| | 56 | | { |
| | 57 | | continue; |
| | 58 | | } |
| | 59 | |
|
| 1307 | 60 | | int commentIndex = line.IndexOf('#'); |
| | 61 | |
|
| 1307 | 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 | |
|
| 1296 | 72 | | if (readHeaderText) |
| | 73 | | { |
| 592 | 74 | | readHeaderText = false; |
| | 75 | | } |
| | 76 | |
|
| 1296 | 77 | | if (commentIndex != -1) |
| | 78 | | { |
| 2 | 79 | | line = line.Substring(0, commentIndex); |
| | 80 | | } |
| | 81 | |
|
| 1296 | 82 | | yield return line; |
| | 83 | | } |
| 282 | 84 | | } |
| 282 | 85 | | } |
| | 86 | | } |
| | 87 | | } |
| | 88 | |
|
| | 89 | | #endif |
| | 90 | |
|