Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System
- Imports System.Collections.Generic
- Imports System.Globalization
- Imports System.IO
- Imports System.Linq
- Imports CIFReader.Dictionaries
- Imports CIFReader.Exceptions
- Namespace CIFReader.Classes
- Class CifData
- Private ReadOnly _filelines As List(Of String)
- Public Sub New(filepath As String)
- _filelines = New List(Of String)()
- Try
- Dim validTypes As String() = {"BS", "BX", "LO", "LI", "LT"}
- Using CIFReader = New StreamReader(filepath)
- While CIFReader.Peek() <> -1
- Dim line As String = CIFReader.ReadLine()
- If line IsNot Nothing AndAlso validTypes.Contains(line.Substring(0, 2)) Then
- _filelines.Add(line)
- End If
- End While
- End Using
- Catch ex As Exception
- Throw New LoadException("Error: " & ex.Message & Environment.NewLine &
- "Please ensure a valid CIF is selected")
- End Try
- End Sub
- Public Function BuildSchedule() As Schedule
- Dim cifSchedule = New Schedule()
- Try
- Dim tiploc_1 As Tiploc = Tiploc.GetInstance()
- For i As Integer = 0 To _filelines.Count - 1
- Dim stopLocation As String
- Select Case _filelines(i).Substring(0, 2)
- Case "BS"
- If True Then
- Dim passStops As String() = {"1", "2"}
- If passStops.Contains(_filelines(i).Substring(32, 1)) = False OrElse _filelines(i).Substring(2, 1) = "D" Then
- Dim [stop] As Integer = _filelines.Count()
- Dim newi As Integer = 0
- For j As Integer = i + 1 To [stop] - 1
- If _filelines(j).Substring(0, 2) = "BS" Then
- [stop] = j
- newi = j - 1
- End If
- Next
- i = newi
- Exit Select
- End If
- BuildTrain(cifSchedule, i)
- Exit Select
- End If
- Case "LO"
- If True Then
- Const stopType As String = "Origin"
- stopLocation = _filelines(i).Substring(2, 7).Trim()
- stopLocation = tiploc_1.getFullName(stopLocation)
- Dim publicDeparture As DateTime = DateTime.ParseExact(_filelines(i).Substring(15, 4), "HHmm", CultureInfo.InvariantCulture)
- Dim workingDeparture As DateTime = workingTimes(i, 10)
- Dim platform As String = _filelines(i).Substring(19, 3).Trim()
- Dim line As String = _filelines(i).Substring(22, 3).Trim()
- Dim allowances As Double = 0.0
- 'Engineering Allowance
- allowances = allowancesAdd(i, 25)
- 'Pathing Allowance
- allowances = allowancesAdd(i, 27)
- 'Performance Allowance
- allowances = allowancesAdd(i, 41)
- Dim last As Integer = cifSchedule.TrainArray.Count() - 1
- cifSchedule.TrainArray(last).AddLocation(New Location(stopType, stopLocation,
- publicDeparture, workingDeparture,
- platform, line, allowances))
- Exit Select
- End If
- Case "LI"
- If True Then
- Dim validStops As String() = {"T", "U", "D"}
- If validStops.Contains(_filelines(i).Substring(42, 1)) = False Then
- Else
- Const stopType As String = "Intermediate"
- stopLocation = _filelines(i).Substring(2, 7).Trim
- stopLocation = tiploc_1.getFullName(stopLocation)
- Dim publicArrival As DateTime = DateTime.ParseExact(_filelines(i).Substring(25, 4), "HHmm", CultureInfo.InvariantCulture)
- Dim publicDeparture As DateTime = DateTime.ParseExact(_filelines(i).Substring(29, 4), "HHmm", CultureInfo.InvariantCulture)
- 'Flag for inputting Half-minutes to working times, if possible.
- Dim workingArrival As DateTime = workingTimes(i, 10)
- Dim workingDeparture As DateTime = workingTimes(i, 15)
- Dim platform As String = _filelines(i).Substring(33, 3).Trim
- Dim line As String = _filelines(i).Substring(36, 3).Trim
- Dim allowances As Double = 0.0
- 'Engineering Allowance
- allowances = allowancesAdd(i, 54)
- 'Pathing Allowance
- allowances = allowancesAdd(i, 56)
- 'Performance Allowance
- allowances = allowancesAdd(i, 58)
- Dim last As Integer = cifSchedule.TrainArray.Count() - 1
- cifSchedule.TrainArray(last).AddLocation(New Location(stopType, stopLocation, publicArrival,
- publicDeparture, workingArrival,
- workingDeparture, platform, line,
- allowances))
- End If
- Exit Select
- End If
- Case "LT"
- If True Then
- Const stopType As String = "Terminus"
- stopLocation = _filelines(i).Substring(2, 7).Trim
- stopLocation = tiploc_1.getFullName(stopLocation)
- Dim publicArrival As DateTime = DateTime.ParseExact(_filelines(i).Substring(15, 4), "HHmm", CultureInfo.InvariantCulture)
- Dim workingArrival As DateTime = workingTimes(i, 10)
- Dim platform As String = _filelines(i).Substring(19, 3)
- Dim last As Integer = cifSchedule.TrainArray.Count() - 1
- cifSchedule.TrainArray(last).AddLocation(New Location(stopType, stopLocation, publicArrival,
- workingArrival, platform))
- cifSchedule.TrainArray(last).locationList.Sort()
- Exit Select
- End If
- End Select
- Next
- Catch kex As KeyNotFoundException
- Throw New LoadException(kex.Message)
- Catch ex As Exception
- Throw New LoadException("Error: " & ex.Message & Environment.NewLine & "Please ensure CIF is not corrupt")
- End Try
- If cifSchedule.TrainArray.Count = 0 Then
- Throw New LoadException("Error: Invalid CIF" & Environment.NewLine & "No trains found")
- End If
- cifSchedule.TrainArray.Sort()
- Return cifSchedule
- End Function
- Private Sub BuildTrain(schedule As Schedule, i As Integer)
- Dim tiploc_1 As Tiploc = Tiploc.GetInstance()
- Dim tocDict As Toc = Toc.GetInstance()
- Dim startDate As DateTime = DateTime.ParseExact(_filelines(i).Substring(9, 6), "yyMMdd", CultureInfo.InvariantCulture)
- Dim endDate As DateTime
- If _filelines(i).Substring(15, 6) = "999999" Then
- endDate = DateTime.MaxValue
- Else
- endDate = DateTime.ParseExact(_filelines(i).Substring(15, 6), "yyMMdd", CultureInfo.InvariantCulture)
- End If
- Dim daysRun As String = _filelines(i).Substring(21, 7)
- Dim headCode As String = _filelines(i).Substring(32, 4)
- Dim serviceCode As String = _filelines(i).Substring(41, 8)
- Dim toc_2 As String = _filelines(i + 1).Substring(11, 2)
- toc_2 = tocDict.GetFullName(toc_2)
- If schedule.GetTocs().Contains(toc_2) = False Then
- schedule.AddToc(toc_2)
- End If
- Dim endLoop As Integer = _filelines.Count
- Dim midnightTrain As Boolean = False
- Dim originLocation As String = String.Empty
- Dim originTime As DateTime = DateTime.Now
- Dim destinationLocation As String = String.Empty
- Dim destinationTime As DateTime = DateTime.Now
- For j As Integer = i + 2 To endLoop - 1
- Select Case _filelines(j).Substring(0, 2)
- Case "LO"
- If True Then
- originLocation = tiploc_1.getFullName(_filelines(j).Substring(2, 7).Trim)
- originTime = Date.ParseExact(_filelines(j).Substring(15, 4), "HHmm", CultureInfo.InvariantCulture)
- If originTime.TimeOfDay >= New TimeSpan(0, 0, 0) And originTime.TimeOfDay < New TimeSpan(4, 0, 0) Then
- midnightTrain = True
- End If
- Exit Select
- End If
- Case "LT"
- If True Then
- destinationLocation = tiploc_1.getFullName(_filelines(j).Substring(2, 7).Trim)
- destinationTime = Date.ParseExact(_filelines(j).Substring(15, 4), "HHmm", CultureInfo.InvariantCulture)
- Exit For
- End If
- End Select
- Next
- schedule.AddTrain(New Train(midnightTrain, daysRun, toc_2, originTime, originLocation, destinationTime,
- destinationLocation, headCode, "unknown", serviceCode, startDate, endDate))
- End Sub
- Public Function allowancesAdd(row, column)
- Dim allowances As Double = 0.0
- If _filelines(row).Substring(column, 2) = " " Then
- allowances += 0.0
- ElseIf _filelines(row).Substring(column + 1, 1) = "H" And _filelines(row).Substring(column, 1) <> " " Then
- allowances += 0.5 + Double.Parse(_filelines(row).Substring(column, 1))
- ElseIf _filelines(row).Substring(column + 1, 1) = "H" And _filelines(row).Substring(column, 1) = " " Then
- allowances += 0.5
- Else
- allowances += Double.Parse(_filelines(row).Substring(column, 2).Trim)
- End If
- Return allowances
- End Function
- Public Function workingTimes(row, column)
- Dim workingTime As DateTime
- Dim timeString As String = _filelines(row).Substring(column, 4)
- If _filelines(row).Substring(column + 4, 1) = "H" Then
- workingTime = DateAdd(DateInterval.Second, 30, Date.ParseExact(timeString, "HHmm", CultureInfo.InvariantCulture))
- Else
- workingTime = Date.ParseExact(timeString, "HHmm", CultureInfo.InvariantCulture)
- End If
- Return workingTime
- End Function
- End Class
- End Namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement