Advertisement
tolikpunkoff

GetCountryDisk SxGeoSharp function

Sep 30th, 2018
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. //Поиск информации о стране по ID на диске        
  2.         private Dictionary<string, object> GetCountryDisk(byte CountryID)
  3.         {
  4.             //становимся на начало таблицы со странами
  5.             long TableStart = Header.countries_begin;
  6.             Seek(TableStart, SeekOrigin.Begin);
  7.  
  8.             long Readed = 0;
  9.             int NextRead = (int)Header.CountrySize;
  10.             SxGeoUnpack Unpacker = new SxGeoUnpack(Header.pack_country, Header.DBEncoding);
  11.  
  12.             while (Readed < Header.CountrySize - 1)
  13.             {
  14.                 //читаем запись
  15.                 byte[] buf = ReadBytes(NextRead);
  16.                 if (buf == null)
  17.                 {
  18.                     return null;
  19.                 }
  20.  
  21.                 //распаковываем запись
  22.                 int RealLength = 0;
  23.                 Dictionary<string, object> Record = Unpacker.Unpack(buf,
  24.                     out RealLength);
  25.  
  26.                 //проверяем, не нашли ли запись
  27.                 if ((byte)Record["id"] == CountryID)
  28.                 {
  29.                     return Record;
  30.                 }
  31.  
  32.                 //Сохраняем количество фактических байт записи
  33.                 Readed += RealLength;
  34.                 //Отступаем в потоке назад
  35.                 long backstep = 0;
  36.                 if (TableStart + Readed + Header.MaxCountry > FileSize)
  37.                 {
  38.                     //если на чтение последних записей файла не хватило
  39.                     //максимальной длины записи                    
  40.                     backstep = -NextRead + RealLength;
  41.                     NextRead = (int)(FileSize - TableStart - Readed);
  42.                     //break;
  43.                 }
  44.                 else
  45.                 {
  46.                     backstep = -NextRead + RealLength;
  47.                 }
  48.  
  49.                 Seek(backstep, SeekOrigin.Current);
  50.             }            
  51.             return null;
  52.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement