52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
using System.Globalization;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.IO;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace NonaBGS.EDDB {
|
|
public class Stations {
|
|
private JObject root = null;
|
|
private Dictionary<int, List<string>> by_system_id = null;
|
|
|
|
public JObject JSON => root;
|
|
|
|
private Stations() {
|
|
}
|
|
|
|
private void Initialise() {
|
|
by_system_id = new Dictionary<int, List<string>>();
|
|
foreach (var station in root.Properties()) {
|
|
int id = int.Parse(station.Name);
|
|
var names = root.Value<JArray>(id.ToString()).Values<string>().ToArray();
|
|
|
|
if (!by_system_id.ContainsKey(id)) {
|
|
by_system_id[id] = new List<string>();
|
|
}
|
|
|
|
by_system_id[id].AddRange(names);
|
|
}
|
|
}
|
|
|
|
public static Stations FromFile(string filename) {
|
|
string alltext = File.ReadAllText(filename);
|
|
Stations stations = new Stations {
|
|
root = JObject.Parse(alltext),
|
|
};
|
|
stations.Initialise();
|
|
|
|
return stations;
|
|
}
|
|
|
|
public string[] StationNamesBySystemId(int systemid, string filter) {
|
|
if (!by_system_id.ContainsKey(systemid)) {
|
|
return new string[0];
|
|
}
|
|
return by_system_id[systemid]
|
|
.Where(x => CultureInfo.InvariantCulture.CompareInfo.IndexOf(x, filter, CompareOptions.IgnoreCase) > -1)
|
|
.ToArray()
|
|
;
|
|
}
|
|
}
|
|
}
|