示例#1
0
        private static void ParseAsn()
        {
            Asns = new List <Asn>(ushort.MaxValue);
            List <int> asnNumbers = new List <int>(ushort.MaxValue);

            AsnRanges = CsvFile
                        .EnumerateBinary(Csv.Asn, ",", true, false, Encoding.UTF8)
                        .Select(row =>
            {
                int number = row[1].Int32Value.Value;

                AddAsn(number, row[2].Value);
                ConvertCidr(row[0].Value, out uint from, out uint to);

                return(new AsnRange
                {
                    Asn = number,
                    From = from,
                    To = to
                });
            })
                        .ToArray();

            AsnRanges6 = CsvFile
                         .EnumerateBinary(Csv.Asn6, ",", true, false, Encoding.UTF8)
                         .Select(row =>
            {
                int number = row[1].Int32Value.Value;

                AddAsn(number, row[2].Value);
                ConvertCidr(row[0].Value, out byte[] from, out byte[] to);

                return(new AsnRange6
                {
                    Asn = number,
                    From = from,
                    To = to
                });
            })
                         .ToArray();

            Asns = Asns.OrderBy(asn => asn.Organization, StringComparer.OrdinalIgnoreCase).ToList();

            void AddAsn(int number, string organization)
            {
                if (!asnNumbers.Contains(number))
                {
                    asnNumbers.Add(number);
                    Asns.Add(new Asn
                    {
                        Number       = number,
                        Organization = organization
                    });
                }
            }
        }
示例#2
0
        private static void ParseCountries()
        {
            Countries = CsvFile
                        .EnumerateBinary(Csv.Country, ",", true, false, Encoding.UTF8)
                        .Where(row => row[5].Value != "")
                        .Select(row =>
            {
                string isoCode          = row[4].Value.ToUpper();
                string continentIsoCode = row[2].Value.ToUpper();
                if (isoCode.Length != 2 || isoCode[0] > byte.MaxValue || isoCode[1] > byte.MaxValue)
                {
                    throw new FormatException("Country ISO code must be composed of two ANSI characters.");
                }
                if (continentIsoCode.Length != 2 || continentIsoCode[0] > byte.MaxValue || continentIsoCode[1] > byte.MaxValue)
                {
                    throw new FormatException("Country continent code must be composed of two ANSI characters.");
                }

                return(new Country
                {
                    ID = row[0].Int32Value.Value,
                    Name = row[5].Value,
                    IsoCode = isoCode,
                    Continent = row[3].Value,
                    ContinentIsoCode = continentIsoCode,
                    EuropeanUnion = row[6].Int32Value.Value == 1
                });
            })
                        .OrderBy(country => country.Name, StringComparer.OrdinalIgnoreCase)
                        .ToArray();

            if (Countries.Length > 255)
            {
                throw new OverflowException("The limit of 255 countries has been exceeded.");
            }

            IPRanges = CsvFile
                       .EnumerateBinary(Csv.Range, ",", true, false, Encoding.UTF8)
                       .Where(row => row[1].Value != "")
                       .Select(row => new
            {
                Range        = row[0].Value,
                CountryIndex = GetCountryIndex(row[1].Int32Value.Value)
            })
                       .Where(range => range.CountryIndex != null)
                       .Select(range =>
            {
                ConvertCidr(range.Range, out uint from, out uint to);

                return(new IPRange
                {
                    CountryIndex = range.CountryIndex.Value,
                    From = from,
                    To = to
                });
            })
                       .ToArray();

            IPRanges6 = CsvFile
                        .EnumerateBinary(Csv.Range6, ",", true, false, Encoding.UTF8)
                        .Where(row => row[1].Value != "")
                        .Select(row => new
            {
                Range        = row[0].Value,
                CountryIndex = GetCountryIndex(row[1].Int32Value.Value)
            })
                        .Where(range => range.CountryIndex != null)
                        .Select(range =>
            {
                ConvertCidr(range.Range, out byte[] from, out byte[] to);

                return(new IPRange6
                {
                    CountryIndex = range.CountryIndex.Value,
                    From = from,
                    To = to
                });
            })
                        .ToArray();

            byte?GetCountryIndex(int id)
            {
                int index = Countries.IndexOf(country => country.ID == id);

                return(index != -1 ? (byte)index : (byte?)null);
            }
        }
示例#3
0
        private static void ParseCities()
        {
            Cities = CsvFile
                     .EnumerateBinary(Csv.City, ",", true, false, Encoding.UTF8)
                     .Select(row => new City
            {
                ID                  = row[0].Int32Value.Value,
                CountryIndex        = GetCountryIndex(row[4].Value),
                Name                = row[10].Value,
                Subdivision1Name    = row[7].Value,
                Subdivision1IsoCode = row[6].Value,
                Subdivision2Name    = row[9].Value,
                Subdivision2IsoCode = row[8].Value,
                TimeZone            = row[12].Value
            })
                     .OrderBy(city => city.Name, StringComparer.OrdinalIgnoreCase)
                     .ToArray();

            int[] cityIndices = Create.Array(Cities.Max(city => city.ID) + 1, -1);
            for (int i = 0; i < Cities.Length; i++)
            {
                cityIndices[Cities[i].ID] = i;
            }

            CityRanges = CsvFile
                         .EnumerateBinary(Csv.CityRange, ",", true, false, Encoding.UTF8)
                         .Where(row => row[1].Value != "")
                         .Select(row =>
            {
                ConvertCidr(row[0].Value, out uint from, out uint to);

                return(new CityRange
                {
                    CityIndex = GetCityIndex(row[1].Int32Value.Value),
                    PostalCode = row[6].Value,
                    Latitude = row[7].Value.ToSingleOrNull().Value,
                    Longitude = row[8].Value.ToSingleOrNull().Value,
                    AccuracyRadius = Convert.ToInt16(row[9].Int32Value.Value),
                    From = from,
                    To = to
                });
            })
                         .ToArray();

            CityRanges6 = CsvFile
                          .EnumerateBinary(Csv.CityRange6, ",", true, false, Encoding.UTF8)
                          .Where(row => row[1].Value != "")
                          .Select(row =>
            {
                ConvertCidr(row[0].Value, out byte[] from, out byte[] to);

                return(new CityRange6
                {
                    CityIndex = GetCityIndex(row[1].Int32Value.Value),
                    PostalCode = row[6].Value,
                    Latitude = row[7].Value.ToSingleOrNull().Value,
                    Longitude = row[8].Value.ToSingleOrNull().Value,
                    AccuracyRadius = Convert.ToInt16(row[9].Int32Value.Value),
                    From = from,
                    To = to
                });
            })
                          .ToArray();

            byte GetCountryIndex(string isoCode)
            {
                int index = Countries.IndexOf(country => country.IsoCode == isoCode);

                return(index != -1 ? (byte)index : byte.MaxValue);
            }

            int GetCityIndex(int id)
            {
                int index = cityIndices[id];

                return(index != -1 ? index : throw new FormatException("City ID '" + id + "' not found."));
            }
        }