| | 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 | | using System.Text; |
| | 9 | |
|
| | 10 | | namespace JeremyAnsel.Media.WavefrontObj |
| | 11 | | { |
| | 12 | | internal class LineReader |
| | 13 | | { |
| 3 | 14 | | private static readonly char[] lineSeparators = new char[] { ' ', '\t' }; |
| | 15 | |
|
| 2718 | 16 | | public List<string> HeaderTextLines { get; } = new List<string>(); |
| | 17 | |
|
| | 18 | | public IEnumerable<string[]> Read(Stream stream) |
| | 19 | | { |
| 1491 | 20 | | using (var reader = new StreamReader(stream, Encoding.UTF8, true, 1024, true)) |
| | 21 | | { |
| 1491 | 22 | | string line = string.Empty; |
| 1491 | 23 | | bool isMultiLine = false; |
| 1491 | 24 | | bool readHeaderText = true; |
| | 25 | |
|
| 4629 | 26 | | while (!reader.EndOfStream) |
| | 27 | | { |
| 4071 | 28 | | string? currentLine = reader.ReadLine(); |
| | 29 | |
|
| 4071 | 30 | | if (string.IsNullOrWhiteSpace(currentLine)) |
| | 31 | | { |
| 645 | 32 | | if (readHeaderText) |
| | 33 | | { |
| 645 | 34 | | this.HeaderTextLines.Add(string.Empty); |
| | 35 | | } |
| | 36 | |
|
| 645 | 37 | | continue; |
| | 38 | | } |
| | 39 | |
|
| 3426 | 40 | | if (isMultiLine) |
| | 41 | | { |
| 15 | 42 | | line = line.Substring(0, line.Length - 1) + currentLine; |
| | 43 | | } |
| | 44 | | else |
| | 45 | | { |
| 3411 | 46 | | line = currentLine; |
| | 47 | | } |
| | 48 | |
|
| 3426 | 49 | | line = line.Trim(); |
| | 50 | |
|
| 3426 | 51 | | isMultiLine = line.EndsWith("\\", StringComparison.Ordinal); |
| | 52 | |
|
| 3426 | 53 | | if (isMultiLine) |
| | 54 | | { |
| | 55 | | continue; |
| | 56 | | } |
| | 57 | |
|
| 3411 | 58 | | int commentIndex = line.IndexOf('#'); |
| | 59 | |
|
| 3411 | 60 | | if (commentIndex == 0) |
| | 61 | | { |
| 30 | 62 | | if (readHeaderText) |
| | 63 | | { |
| 24 | 64 | | this.HeaderTextLines.Add(line.Substring(1)); |
| | 65 | | } |
| | 66 | |
|
| 24 | 67 | | continue; |
| | 68 | | } |
| | 69 | |
|
| 3381 | 70 | | if (readHeaderText) |
| | 71 | | { |
| 1488 | 72 | | readHeaderText = false; |
| | 73 | | } |
| | 74 | |
|
| 3381 | 75 | | if (commentIndex != -1) |
| | 76 | | { |
| 3 | 77 | | line = line.Substring(0, commentIndex); |
| | 78 | | } |
| | 79 | |
|
| 3381 | 80 | | string[] values = line.Split(lineSeparators, StringSplitOptions.RemoveEmptyEntries); |
| | 81 | |
|
| 3381 | 82 | | yield return values; |
| | 83 | | } |
| 558 | 84 | | } |
| 558 | 85 | | } |
| | 86 | | } |
| | 87 | | } |
| | 88 | |
|