-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.cs
233 lines (211 loc) · 7.57 KB
/
Helper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#region Copyright
// *******************************************************************************
// Copyright (c) 2000-2011 Paul Stancer.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
// Paul Stancer - initial implementation
// *******************************************************************************
#endregion
#region using
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Stancer.GTFSEngine.Entities;
using Ximura;
#endregion
namespace Stancer.GTFSEngine
{
/// <summary>
/// This class provides extension methods to help decode and convert string values to their system type.
/// </summary>
public static class Helper
{
public static string ValidateNotEmptyOrNull(this CSVRowItem item, string field)
{
string data = item[field];
if (data == null || data == "")
throw new ArgumentNullException(string.Format(@"Missing mandatory field ""{0}"" at line {1}", field, item.LineID));
return data;
}
public static TimeSpan ToTimeSpan(this string item)
{
int hours=0, mins=0, secs=0;
if ((item.Length == 8 &&
(
int.TryParse(item.Substring(0, 2), out hours) ||
int.TryParse(item.Substring(3, 2), out mins) ||
int.TryParse(item.Substring(6, 2), out secs)
))
||
(item.Length == 7 &&
(
int.TryParse(item.Substring(0, 1), out hours) ||
int.TryParse(item.Substring(2, 2), out mins) ||
int.TryParse(item.Substring(5, 2), out secs)
)))
return new TimeSpan(hours, mins, secs);
throw new ArgumentException("The string is not the correct format.");
}
public static DateTime ToDate(this string item)
{
if (item.Length != 8)
throw new ArgumentOutOfRangeException("The length of the string should be 8 characters.");
return new DateTime(
int.Parse(item.Substring(0, 4)),
int.Parse(item.Substring(4, 2)),
int.Parse(item.Substring(6, 2)));
}
public static LocationType? ToLocationType(this string item)
{
switch (item)
{
case "":
case null:
return null;
case "0":
return LocationType.Stop;
case "1":
return LocationType.Station;
default:
throw new ArgumentOutOfRangeException();
}
}
public static RouteType ToRouteType(this string item)
{
switch (item)
{
case "0":
return RouteType.Tram;
case "1":
return RouteType.Subway;
case "2":
return RouteType.Rail;
case "3":
return RouteType.Bus;
case "4":
return RouteType.Ferry;
case "5":
return RouteType.CableCar;
case "6":
return RouteType.Gondala;
case "7":
return RouteType.Fenicular;
default:
throw new ArgumentOutOfRangeException(string.Format("{0} is not supported for Route type.", item));
}
}
public static DropOffType? ToDropOffType(this string item)
{
switch(item)
{
case "":
case null:
return null;
case "0":
return DropOffType.RegularlyScheduled;
case "1":
return DropOffType.NoPickupAvailable;
case "2":
return DropOffType.MustPhone;
case "3":
return DropOffType.MustAskDriver;
default:
throw new ArgumentOutOfRangeException();
}
}
public static TransferOptionsType ToTransferOptionsType(this string item)
{
switch (item)
{
case "":
case "0":
return TransferOptionsType.RecommendedTransferPoint;
case "1":
return TransferOptionsType.TimedTransferPoint;
case "2":
return TransferOptionsType.TransferTimeRequired;
case "3":
return TransferOptionsType.TransfersNotPossible;
default:
throw new ArgumentOutOfRangeException("transfer_type", item);
}
}
public static DirectionType? ToDirectionType(this string item)
{
switch (item)
{
case "0":
return DirectionType.Outbound;
case "1":
return DirectionType.Inbound;
default:
return null;
}
}
public static PickUpType? ToPickUpType(this string item)
{
switch (item)
{
case "":
case null:
return null;
case "0":
return PickUpType.RegularlyScheduled;
case "1":
return PickUpType.NoPickupAvailable;
case "2":
return PickUpType.MustPhone;
case "3":
return PickUpType.MustAskDriver;
default:
throw new ArgumentOutOfRangeException();
}
}
public static CalendarExceptionType ToCalendarExceptionType(this string item)
{
switch (item)
{
case "1":
return CalendarExceptionType.ServiceAdded;
case "2":
return CalendarExceptionType.ServiceRemoved;
default:
throw new ArgumentOutOfRangeException();
}
}
public static PaymentMethodType ToPaymentMethodType(this string item)
{
switch (item)
{
case "0":
return PaymentMethodType.PayOnboard;
case "1":
return PaymentMethodType.PayBefore;
default:
throw new ArgumentOutOfRangeException();
}
}
public static TransferType ToTransferType(this string item)
{
switch (item)
{
case "0":
return TransferType.NoTransfers;
case "1":
return TransferType.Once;
case "2":
return TransferType.Twice;
case "":
return TransferType.Unlimited;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}