Esempio n. 1
0
    public Location GetLocation(long ipnum)
    {
        int record_pointer;

        byte[]   record_buf = new byte[FULL_RECORD_LENGTH];
        char[]   record_buf2 = new char[FULL_RECORD_LENGTH];
        int      record_buf_offset = 0;
        Location record = new Location();
        int      str_length = 0;
        int      j, Seek_country;
        double   latitude = 0, longitude = 0;

        try {
            Seek_country = SeekCountry(ipnum);
            if (Seek_country == databaseSegments[0])
            {
                return(null);
            }
            record_pointer = Seek_country + ((2 * recordLength - 1) * databaseSegments[0]);
            if (memoryMode == MemoryMode.Cached)
            {
                Array.Copy(dbbuffer, record_pointer, record_buf, 0, Math.Min(dbbuffer.Length - record_pointer, FULL_RECORD_LENGTH));
            }
            else
            {
                file.Seek(record_pointer, SeekOrigin.Begin);
                file.Read(record_buf, 0, FULL_RECORD_LENGTH);
            }
            for (int a0 = 0; a0 < FULL_RECORD_LENGTH; a0++)
            {
                record_buf2[a0] = Convert.ToChar(record_buf[a0]);
            }
            // get country
            record.CountryCode = CountryCodes[unsignedByteToInt(record_buf[0])];
            record.CountryName = CountryNames[unsignedByteToInt(record_buf[0])];
            record_buf_offset++;

            // get region
            while (record_buf[record_buf_offset + str_length] != '\0')
            {
                str_length++;
            }
            if (str_length > 0)
            {
                record.Region = new String(record_buf2, record_buf_offset, str_length);
            }
            record_buf_offset += str_length + 1;
            str_length         = 0;

            // get region_name
            record.RegionName = RegionName.GetRegionName(record.CountryCode, record.Region);

            // get city
            while (record_buf[record_buf_offset + str_length] != '\0')
            {
                str_length++;
            }
            if (str_length > 0)
            {
                record.City = new String(record_buf2, record_buf_offset, str_length);
            }
            record_buf_offset += (str_length + 1);
            str_length         = 0;

            // get postal code
            while (record_buf[record_buf_offset + str_length] != '\0')
            {
                str_length++;
            }
            if (str_length > 0)
            {
                record.PostalCode = new String(record_buf2, record_buf_offset, str_length);
            }
            record_buf_offset += (str_length + 1);

            // get latitude
            for (j = 0; j < 3; j++)
            {
                latitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8));
            }
            record.Latitude    = (float)latitude / 10000 - 180;
            record_buf_offset += 3;

            // get longitude
            for (j = 0; j < 3; j++)
            {
                longitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8));
            }
            record.Longitude = (float)longitude / 10000 - 180;

            record.MetroCode = record.DmaCode = 0;
            record.AreaCode  = 0;
            if (databaseType == DatabaseInfo.CITY_EDITION_REV1)
            {
                // get metro_code
                int metroarea_combo = 0;
                if (record.CountryCode == "US")
                {
                    record_buf_offset += 3;
                    for (j = 0; j < 3; j++)
                    {
                        metroarea_combo += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8));
                    }
                    record.MetroCode = record.DmaCode = metroarea_combo / 1000;
                    record.AreaCode  = metroarea_combo % 1000;
                }
            }
        } catch (IOException) {
            Console.Write("IO Exception while seting up segments");
        }
        return(record);
    }
Esempio n. 2
0
        public Location GetLocation(long ipnum)
        {
            byte[]   recordBuf = new byte[FullRecordLength];
            char[]   recordBuf2 = new char[FullRecordLength];
            int      recordBufOffset = 0;
            Location record = new Location();
            int      strLength = 0;
            double   latitude = 0, longitude = 0;

            try
            {
                int seekCountry = this.SeekCountry(ipnum);
                if (seekCountry == this.databaseSegments[0])
                {
                    return(null);
                }
                int recordPointer = seekCountry + ((2 * this.recordLength - 1) * this.databaseSegments[0]);
                this.dbStream.Seek(recordPointer, SeekOrigin.Begin);
                this.dbStream.Read(recordBuf, 0, FullRecordLength);
                for (int a0 = 0; a0 < FullRecordLength; a0++)
                {
                    recordBuf2[a0] = Convert.ToChar(recordBuf[a0]);
                }
                // get country
                record.CountryCode = CountryCodes[(recordBuf[0]).UnsignedByteToInt()];
                record.CountryName = CountryNames[(recordBuf[0]).UnsignedByteToInt()];
                recordBufOffset++;

                // get region
                while (recordBuf[recordBufOffset + strLength] != '\0')
                {
                    strLength++;
                }
                if (strLength > 0)
                {
                    record.Region = new string(recordBuf2, recordBufOffset, strLength);
                }
                recordBufOffset += strLength + 1;
                strLength        = 0;

                // get region_name
                record.RegionName = RegionName.GetRegionName(record.CountryCode, record.Region);

                // get city
                while (recordBuf[recordBufOffset + strLength] != '\0')
                {
                    strLength++;
                }
                if (strLength > 0)
                {
                    record.City = new string(recordBuf2, recordBufOffset, strLength);
                }
                recordBufOffset += (strLength + 1);
                strLength        = 0;

                // get postal code
                while (recordBuf[recordBufOffset + strLength] != '\0')
                {
                    strLength++;
                }
                if (strLength > 0)
                {
                    record.PostalCode = new string(recordBuf2, recordBufOffset, strLength);
                }
                recordBufOffset += (strLength + 1);

                // get latitude
                int j;
                for (j = 0; j < 3; j++)
                {
                    latitude += ((recordBuf[recordBufOffset + j]).UnsignedByteToInt() << (j * 8));
                }
                record.Latitude  = (float)latitude / 10000 - 180;
                recordBufOffset += 3;

                // get longitude
                for (j = 0; j < 3; j++)
                {
                    longitude += ((recordBuf[recordBufOffset + j]).UnsignedByteToInt() << (j * 8));
                }
                record.Longitude = (float)longitude / 10000 - 180;

                record.MetroCode = record.DMACode = 0;
                record.AreaCode  = 0;
                if (this.databaseType == (byte)DatabaseType.CityEditionRev1)
                {
                    // get metro_code
                    int metroareaCombo = 0;
                    if (record.CountryCode == "US")
                    {
                        recordBufOffset += 3;
                        for (j = 0; j < 3; j++)
                        {
                            metroareaCombo += ((recordBuf[recordBufOffset + j]).UnsignedByteToInt() << (j * 8));
                        }
                        record.MetroCode = record.DMACode = metroareaCombo / 1000;
                        record.AreaCode  = metroareaCombo % 1000;
                    }
                }
            }
            catch (IOException)
            {
                Console.Write("IO Exception while seting up segments");
            }
            return(record);
        }