154 Entries =
new List<RudolphTableEntry>();
156 using(StreamReader reader =
new StreamReader(csvFile))
159 string csvContent = reader.ReadToEnd();
160 List<string> csvLines = csvContent.Split(
"\n").Select(line => line.Replace(
"\"",
"").Trim()).Where(line => !
string.IsNullOrEmpty(line)).ToList();
163 bool wasGenderOrAgeFound =
false;
164 bool collectRudolphScoreLines =
false;
165 string currentRudolphScoreTimesStr =
"";
167 foreach (
string line
in csvLines)
170 if(line.Contains(FEMALE_MARKER) || line.Contains(MALE_MARKER))
173 wasGenderOrAgeFound =
true;
176 else if (line.Contains(AGE_MARKER) || line.Contains(AGE_MARKER_ALTERNATIVE))
180 Match match = Regex.Match(line, $
"(.*) {AGE_MARKER}");
181 Match matchAlternative = Regex.Match(line, $
"{AGE_MARKER_ALTERNATIVE} (.*)");
182 if ((match.Success && match.Groups.Count >= 2) || (matchAlternative.Success && matchAlternative.Groups.Count >= 2))
184 ageStr = match.Success ? match.Groups[1].Value : matchAlternative.Groups[1].Value;
185 if(
byte.TryParse(ageStr, out age))
188 wasGenderOrAgeFound =
true;
193 else if (line.Contains(OPEN_AGE_MARKER))
196 baseEntry.IsOpenAge =
true;
197 wasGenderOrAgeFound =
true;
200 else if(line.Contains(END_PAGE_MARKER))
202 baseEntry =
new RudolphTableEntry() { Gender = baseEntry.Gender, RudolphScore = 0 };
203 wasGenderOrAgeFound =
false;
206 else if(line.Trim().Length <= 2 && wasGenderOrAgeFound)
209 if (
byte.TryParse(line.Trim(), out score))
211 if(baseEntry.RudolphScore != score)
214 collectRudolphScoreLines =
true;
215 baseEntry.RudolphScore = score;
220 collectRudolphScoreLines =
false;
222 currentRudolphScoreTimesStr =
"";
227 else if(collectRudolphScoreLines && wasGenderOrAgeFound)
229 currentRudolphScoreTimesStr +=
" " + line;
246 List<RudolphTableEntry> rudolphScoreEntries =
new List<RudolphTableEntry>();
248 MatchCollection timeMatches = Regex.Matches(rudolphScoreTimesStr, TIME_FORMAT_REGEX);
249 if (timeMatches.Count != NUMBER_TIME_COLUMNS_RUDOLPH_TABLE)
251 throw new Exception(
"Error while parsing rudolph table: line encountered that contains a wrong number of times.");
253 int timeColumnIndex = 0;
254 foreach (Match match
in timeMatches)
258 if (TimeSpan.TryParseExact(match.Value, TIME_FORMAT_STRING, CultureInfo.InvariantCulture, out time))
260 singleEntry.Time = time;
261 singleEntry.SwimmingStyle = SWIMMINGSTYLES_PER_TIME_COLUMN[timeColumnIndex];
262 singleEntry.Distance = DISTANCES_PER_TIME_COLUMN[timeColumnIndex];
263 rudolphScoreEntries.Add(singleEntry);
268 return rudolphScoreEntries;
287 List<RudolphTableEntry> entries =
Entries.Where(e => e.Gender == gender && e.SwimmingStyle == swimmingStyle && e.Distance == distance && e.RudolphScore == rudolphScore).ToList();
288 if(entries ==
null || entries.Count == 0) {
return null; }
290 byte maxAgeEntries = entries.Max(e => e.Age);
291 if (age > maxAgeEntries)
293 return entries.FirstOrDefault(e => e.IsOpenAge);
297 return entries.FirstOrDefault(e => e.Age == age);