public void CreateTable(string Table, string Format, SxGeoDirType tabletype) { TableName = Table; RecordFormat = Format; TableType = tabletype; SxGeoHeader Header = Database.GetHeader(); Unpacker = new SxGeoUnpack(RecordFormat, Header.DBEncoding); //добавляем табличку SxDS.Tables.Add(TableName); Dictionary <string, Type> RecordTypes = Unpacker.GetRecordTypes(); //добавляем колонки foreach (string FieldName in RecordTypes.Keys) { SxDS.Tables[TableName].Columns.Add(FieldName, RecordTypes[FieldName]); } switch (TableType) { case SxGeoDirType.Cites: { TableStart = Header.cites_begin; TableSize = Header.CitySize; MaxRecordSize = Header.MaxCity; }; break; case SxGeoDirType.Countries: { TableStart = Header.countries_begin + 1; //вот хз почему +1, иначе не работает TableSize = Header.CountrySize; MaxRecordSize = Header.MaxCountry; }; break; case SxGeoDirType.Regions: { TableStart = Header.regions_begin + 1; //вот хз почему +1, иначе не работает TableSize = Header.RegionSize; MaxRecordSize = Header.MaxRegion; }; break; } }
public Dictionary <string, object> GetIPInfo(string IP, SxGeoInfoType InfoType) { Dictionary <string, object> data_city = new Dictionary <string, object>(); Dictionary <string, object> data_country = new Dictionary <string, object>(); Dictionary <string, object> data_region = new Dictionary <string, object>(); if (!IsOpen) { ErrorMessage = "Database not open."; return(null); } if (!IPConverter.IsIP(IP)) //проверяем IP ли это) { ErrorMessage = IP + " is not valid IP address."; return(null); } //получаем ID IP-адреса uint ID = SearchID(IP); if (ID == 0) //не нашли { ErrorMessage = "Not found."; return(null); } //создаем переменные для хранения ответа IPInfo = new Dictionary <string, object>(); IPInfoTypes = new Dictionary <string, Type>(); //добавляем сам адрес IPInfo.Add("ip", IP); IPInfoTypes.Add("ip", typeof(string)); if (Header.IdLen == 1) //БД SxGeo, ничего кроме ISO-кода вывести не сможем { IPInfo.Add("country_iso", IdToIso(ID)); IPInfoTypes.Add("country_iso", typeof(string)); return(IPInfo); } //БД SxGeoCountry, можем вывести много чего SxGeoUnpack Unpacker = null; byte[] buf = null; //если найденный 'ID' < размера справочника городов //город не найден - только страна if (ID < Header.CountrySize) { Unpacker = new SxGeoUnpack(Header.pack_country, Header.DBEncoding); buf = ReadDBDirs(Header.countries_begin, ID, Header.MaxCountry, cities_db); data_country = Unpacker.Unpack(buf); AddData(data_country, Unpacker.GetRecordTypes(), "country_"); return(IPInfo); } //город найден, находим и распаковываем информацию о городе Unpacker = new SxGeoUnpack(Header.pack_city, Header.DBEncoding); buf = ReadDBDirs(Header.countries_begin, ID, Header.MaxCity, cities_db); data_city = Unpacker.Unpack(buf); //о стране по ID страны data_country = GetCountry((byte)data_city["country_id"]); switch (InfoType) { case SxGeoInfoType.OnlyCountry: //только информация о стране { AddData(data_country, SxGeoUnpack.GetRecordTypes(Header.pack_country), "country_"); }; break; case SxGeoInfoType.CountryCity: //страна+город { AddData(data_country, SxGeoUnpack.GetRecordTypes(Header.pack_country), "country_"); AddData(data_city, Unpacker.GetRecordTypes(), "city_"); }; break; default: //полная информация с регионом (если есть) { Unpacker = new SxGeoUnpack(Header.pack_region, Header.DBEncoding); buf = ReadDBDirs(Header.regions_begin, (uint)data_city["region_seek"], Header.MaxRegion, regions_db); data_region = Unpacker.Unpack(buf); AddData(data_country, SxGeoUnpack.GetRecordTypes(Header.pack_country), "country_"); AddData(data_city, SxGeoUnpack.GetRecordTypes(Header.pack_city), "city_"); AddData(data_region, Unpacker.GetRecordTypes(), "region_"); }; break; } return(IPInfo); }