|
18 | 18 | using System; |
19 | 19 | using System.Collections.Generic; |
20 | 20 | using System.IO; |
| 21 | +using System.Linq; |
21 | 22 |
|
22 | 23 | namespace Orts.Formats.OR |
23 | 24 | { |
@@ -70,27 +71,10 @@ public TimetableGroupFileLite(TimetableFileLite SingleTTInfo) |
70 | 71 |
|
71 | 72 | void MultiTTFilePreliminaryRead(String filePath, String directory, StreamReader scrStream) |
72 | 73 | { |
73 | | - String readLine; |
74 | | - |
75 | | - // read first line - first character is separator, rest is train info |
76 | | - readLine = scrStream.ReadLine(); |
77 | | - |
78 | | - while (readLine != null) |
79 | | - { |
80 | | - if (!String.IsNullOrEmpty(readLine)) |
81 | | - { |
82 | | - if (String.Compare(readLine.Substring(0, 1), "#") == 0) |
83 | | - { |
84 | | - if (String.IsNullOrEmpty(Description)) Description = String.Copy(readLine.Substring(1)); |
85 | | - } |
86 | | - else |
87 | | - { |
88 | | - String ttfile = System.IO.Path.Combine(directory, readLine); |
89 | | - ORTTInfo.Add(new TimetableFileLite(ttfile)); |
90 | | - } |
91 | | - } |
92 | | - readLine = scrStream.ReadLine(); |
93 | | - } |
| 74 | + var (description, ttfiles) = TimetableGroupFileUtilities.ReadMultiTTFiles(directory, scrStream); |
| 75 | + Description = description; |
| 76 | + ORTTInfo = (from ttfile in ttfiles |
| 77 | + select new TimetableFileLite(ttfile)).ToList(); |
94 | 78 | } |
95 | 79 | } |
96 | 80 |
|
@@ -137,27 +121,41 @@ public TimetableGroupFile(String filePath, String directory) |
137 | 121 |
|
138 | 122 | void MultiTTFileRead(String filePath, String directory, StreamReader scrStream) |
139 | 123 | { |
140 | | - String readLine; |
141 | | - |
142 | | - // read first line - first character is separator, rest is train info |
143 | | - readLine = scrStream.ReadLine(); |
| 124 | + var (description, ttfiles) = TimetableGroupFileUtilities.ReadMultiTTFiles(directory, scrStream); |
| 125 | + Description = description; |
| 126 | + TTFiles = ttfiles.ToList(); |
| 127 | + } |
| 128 | + } |
144 | 129 |
|
145 | | - while (readLine != null) |
| 130 | + internal class TimetableGroupFileUtilities |
| 131 | + { |
| 132 | + /// <summary> |
| 133 | + /// Extract a description and list of file paths from a MultiTTfile. |
| 134 | + /// </summary> |
| 135 | + /// <param name="directory">The directory where the MultiTTfile is located, to prepend to all filenames.</param> |
| 136 | + /// <param name="scrStream">The MultiTTfile stream reader.</param> |
| 137 | + /// <returns>The description (null if not present) and the list of file paths.</returns> |
| 138 | + public static (string, IEnumerable<string>) ReadMultiTTFiles(string directory, StreamReader scrStream) |
| 139 | + { |
| 140 | + string description = null; |
| 141 | + var ttfiles = new List<string>(); |
| 142 | + var validLines = scrStream |
| 143 | + .ReadToEnd() |
| 144 | + .Split(new[] { '\n', '\r' }) |
| 145 | + .Where((string l) => !string.IsNullOrEmpty(l)); |
| 146 | + foreach (string readLine in validLines) |
146 | 147 | { |
147 | | - if (!String.IsNullOrEmpty(readLine)) |
| 148 | + if (readLine[0] == '#') |
| 149 | + { |
| 150 | + if (string.IsNullOrEmpty(description)) |
| 151 | + description = readLine.Substring(1); |
| 152 | + } |
| 153 | + else |
148 | 154 | { |
149 | | - if (String.Compare(readLine.Substring(0, 1), "#") == 0) |
150 | | - { |
151 | | - if (String.IsNullOrEmpty(Description)) Description = String.Copy(readLine.Substring(1)); |
152 | | - } |
153 | | - else |
154 | | - { |
155 | | - String ttfile = System.IO.Path.Combine(directory, readLine); |
156 | | - TTFiles.Add(ttfile); |
157 | | - } |
| 155 | + ttfiles.Add(Path.Combine(directory, readLine)); |
158 | 156 | } |
159 | | - readLine = scrStream.ReadLine(); |
160 | 157 | } |
| 158 | + return (description, ttfiles); |
161 | 159 | } |
162 | 160 | } |
163 | 161 | } |
0 commit comments