Skip to content

Commit a83a49b

Browse files
authored
Merge pull request #255 from YoRyan/refactor-multitt-reader
Refactor MultiTTFilePreliminaryRead and MultiTTFileRead into a common function: https://blueprints.launchpad.net/or/+spec/separate-multitt https://blueprints.launchpad.net/or/+spec/separate-multitt
2 parents bf8b2ae + 7d6ffc6 commit a83a49b

File tree

1 file changed

+35
-37
lines changed

1 file changed

+35
-37
lines changed

Source/Orts.Formats.OR/TimetableGroupFile.cs

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using System;
1919
using System.Collections.Generic;
2020
using System.IO;
21+
using System.Linq;
2122

2223
namespace Orts.Formats.OR
2324
{
@@ -70,27 +71,10 @@ public TimetableGroupFileLite(TimetableFileLite SingleTTInfo)
7071

7172
void MultiTTFilePreliminaryRead(String filePath, String directory, StreamReader scrStream)
7273
{
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();
9478
}
9579
}
9680

@@ -137,27 +121,41 @@ public TimetableGroupFile(String filePath, String directory)
137121

138122
void MultiTTFileRead(String filePath, String directory, StreamReader scrStream)
139123
{
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+
}
144129

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)
146147
{
147-
if (!String.IsNullOrEmpty(readLine))
148+
if (readLine[0] == '#')
149+
{
150+
if (string.IsNullOrEmpty(description))
151+
description = readLine.Substring(1);
152+
}
153+
else
148154
{
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));
158156
}
159-
readLine = scrStream.ReadLine();
160157
}
158+
return (description, ttfiles);
161159
}
162160
}
163161
}

0 commit comments

Comments
 (0)