diff --git a/binding/c/ip2region.c b/binding/c/ip2region.c index cc16d77..c20d88e 100644 --- a/binding/c/ip2region.c +++ b/binding/c/ip2region.c @@ -20,22 +20,19 @@ IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, char *dbFile) memset(ip2rObj, 0x00, sizeof(ip2region_entry)); ip2rObj->headerLen = 0; ip2rObj->HeaderSip = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH); - if ( ip2rObj->HeaderSip == NULL ) - { + if ( ip2rObj->HeaderSip == NULL ) { return 0; } ip2rObj->HeaderPtr = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH); - if ( ip2rObj->HeaderPtr == NULL ) - { + if ( ip2rObj->HeaderPtr == NULL ) { IP2R_FREE(ip2rObj->HeaderSip); return 0; } //open the db file ip2rObj->dbHandler = fopen(dbFile, "rb"); - if ( ip2rObj->dbHandler == NULL ) - { + if ( ip2rObj->dbHandler == NULL ) { IP2R_FREE(ip2rObj->HeaderSip); IP2R_FREE(ip2rObj->HeaderPtr); return 0; @@ -61,8 +58,7 @@ IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj) ip2rObj->HeaderPtr = NULL; //close the db file resource - if ( ip2rObj->dbHandler != NULL ) - { + if ( ip2rObj->dbHandler != NULL ) { fclose(ip2rObj->dbHandler); ip2rObj->dbHandler = NULL; } @@ -85,8 +81,7 @@ IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, databloc char buffer[256]; int dataLen, dataptr; - if ( ip2rObj->totalBlocks == 0 ) - { + if ( ip2rObj->totalBlocks == 0 ) { fseek(ip2rObj->dbHandler, 0, 0); if ( fread(buffer, 8, 1, ip2rObj->dbHandler) != 1 ) { return 0; @@ -107,6 +102,7 @@ IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, databloc if ( fread(buffer, INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) { return 0; } + sip = getUnsignedInt(buffer, 0); if ( ip < sip ) { h = m - 1; @@ -162,16 +158,14 @@ IP2R_API uint_t ip2region_btree_search(ip2region_t ip2rObj, uint_t ip, datablock uint_t sip, eip, idxptr, dptr; char buffer[TOTAL_HEADER_LENGTH]; - if ( ip2rObj->headerLen == 0 ) - { + if ( ip2rObj->headerLen == 0 ) { idx = 0; fseek(ip2rObj->dbHandler, 8, 0); //pass the super block if ( fread(buffer, TOTAL_HEADER_LENGTH, 1, ip2rObj->dbHandler) != 1 ) { return 0; } - for ( i = 0; i < TOTAL_HEADER_LENGTH; i += 8 ) - { + for ( i = 0; i < TOTAL_HEADER_LENGTH; i += 8 ) { sip = getUnsignedInt(buffer, i); idxptr = getUnsignedInt(buffer, i + 4); if ( idxptr == 0 ) break; @@ -307,8 +301,7 @@ IP2R_API uint_t ip2long(char *ip) char buffer[4], *cs = ip; uint_t ipval = 0; - while ( *cs != '\0' ) - { + while ( *cs != '\0' ) { if ( *cs == '.' ) { //single part length limit if ( i > 3 ) { diff --git a/binding/c/testSearcher.c b/binding/c/testSearcher.c index 53abbe4..d5d8e32 100644 --- a/binding/c/testSearcher.c +++ b/binding/c/testSearcher.c @@ -68,16 +68,14 @@ int main( int argc, char **argv ) //create a new ip2rObj printf("+--initializing %s ... \n", algorithm); - if ( ip2region_create(&ip2rEntry, dbFile) == 0 ) - { + if ( ip2region_create(&ip2rEntry, dbFile) == 0 ) { println("Error: Fail to create the ip2region object"); return 0; } __PRINT_ABOUT__; - while ( 1 ) - { + while ( 1 ) { print("ip2region>> "); getLine( stdin, line ); if ( strlen(line) < 2 ) continue; diff --git a/binding/java/src/org/lionsoul/ip2region/DataBlock.java b/binding/java/src/org/lionsoul/ip2region/DataBlock.java index fdf8311..9afd7be 100644 --- a/binding/java/src/org/lionsoul/ip2region/DataBlock.java +++ b/binding/java/src/org/lionsoul/ip2region/DataBlock.java @@ -23,20 +23,24 @@ public class DataBlock this.region = region; } - public int getCityId() { + public int getCityId() + { return city_id; } - public DataBlock setCityId(int city_id) { + public DataBlock setCityId(int city_id) + { this.city_id = city_id; return this; } - public String getRegion() { + public String getRegion() + { return region; } - public DataBlock setRegion(String region) { + public DataBlock setRegion(String region) + { this.region = region; return this; } diff --git a/binding/java/src/org/lionsoul/ip2region/DbConfig.java b/binding/java/src/org/lionsoul/ip2region/DbConfig.java index acc2d4a..1aa473f 100644 --- a/binding/java/src/org/lionsoul/ip2region/DbConfig.java +++ b/binding/java/src/org/lionsoul/ip2region/DbConfig.java @@ -25,9 +25,9 @@ public class DbConfig * @param dataBlockSize * @throws DbMakerConfigException */ - public DbConfig( int totalHeaderSize ) throws DbMakerConfigException { - if ( (totalHeaderSize % 8) != 0 ) - { + public DbConfig( int totalHeaderSize ) throws DbMakerConfigException + { + if ( (totalHeaderSize % 8) != 0 ) { throw new DbMakerConfigException("totalHeaderSize must be times of 8"); } @@ -35,24 +35,29 @@ public class DbConfig this.indexBlockSize = 4096; //4 * 1024 } - public DbConfig() throws DbMakerConfigException { + public DbConfig() throws DbMakerConfigException + { this(8192); } - public int getTotalHeaderSize() { + public int getTotalHeaderSize() + { return totalHeaderSize; } - public DbConfig setTotalHeaderSize(int totalHeaderSize) { + public DbConfig setTotalHeaderSize(int totalHeaderSize) + { this.totalHeaderSize = totalHeaderSize; return this; } - public int getIndexBlockSize() { + public int getIndexBlockSize() + { return indexBlockSize; } - public DbConfig setIndexBlockSize(int dataBlockSize) { + public DbConfig setIndexBlockSize(int dataBlockSize) + { this.indexBlockSize = dataBlockSize; return this; } diff --git a/binding/java/src/org/lionsoul/ip2region/DbSearcher.java b/binding/java/src/org/lionsoul/ip2region/DbSearcher.java index 6312727..488842f 100644 --- a/binding/java/src/org/lionsoul/ip2region/DbSearcher.java +++ b/binding/java/src/org/lionsoul/ip2region/DbSearcher.java @@ -88,8 +88,7 @@ public class DbSearcher public DataBlock btreeSearch( long ip ) throws IOException { //check and load the header - if ( HeaderSip == null ) - { + if ( HeaderSip == null ) { raf.seek(8L); //pass the super block //byte[] b = new byte[dbConfig.getTotalHeaderSize()]; byte[] b = new byte[4096]; @@ -121,8 +120,7 @@ public class DbSearcher } int l = 0, h = headerLength, sptr = 0, eptr = 0; - while ( l <= h ) - { + while ( l <= h ) { int m = (l + h) >> 1; //perfetc matched, just return it @@ -230,8 +228,7 @@ public class DbSearcher public DataBlock binarySearch( long ip ) throws IOException { int blen = IndexBlock.getIndexBlockLength(); - if ( totalIndexBlocks == 0 ) - { + if ( totalIndexBlocks == 0 ) { raf.seek(0L); byte[] superBytes = new byte[8]; raf.readFully(superBytes, 0, superBytes.length); diff --git a/binding/java/src/org/lionsoul/ip2region/HeaderBlock.java b/binding/java/src/org/lionsoul/ip2region/HeaderBlock.java index 28b04ce..4435a9b 100644 --- a/binding/java/src/org/lionsoul/ip2region/HeaderBlock.java +++ b/binding/java/src/org/lionsoul/ip2region/HeaderBlock.java @@ -23,20 +23,24 @@ public class HeaderBlock this.indexPtr = indexPtr; } - public long getIndexStartIp() { + public long getIndexStartIp() + { return indexStartIp; } - public HeaderBlock setIndexStartIp(long indexStartIp) { + public HeaderBlock setIndexStartIp(long indexStartIp) + { this.indexStartIp = indexStartIp; return this; } - public int getIndexPtr() { + public int getIndexPtr() + { return indexPtr; } - public HeaderBlock setIndexPtr(int indexPtr) { + public HeaderBlock setIndexPtr(int indexPtr) + { this.indexPtr = indexPtr; return this; } diff --git a/binding/java/src/org/lionsoul/ip2region/IndexBlock.java b/binding/java/src/org/lionsoul/ip2region/IndexBlock.java index 5d6b741..8f4abd7 100644 --- a/binding/java/src/org/lionsoul/ip2region/IndexBlock.java +++ b/binding/java/src/org/lionsoul/ip2region/IndexBlock.java @@ -37,43 +37,52 @@ public class IndexBlock this.dataLen = dataLen; } - public long getStartIp() { + public long getStartIp() + { return startIp; } - public IndexBlock setStartIp(long startIp) { + public IndexBlock setStartIp(long startIp) + { this.startIp = startIp; return this; } - public long getEndIp() { + public long getEndIp() + { return endIp; } - public IndexBlock setEndIp(long endIp) { + public IndexBlock setEndIp(long endIp) + { this.endIp = endIp; return this; } - public int getDataPtr() { + public int getDataPtr() + { return dataPtr; } - public IndexBlock setDataPtr(int dataPtr) { + public IndexBlock setDataPtr(int dataPtr) + { this.dataPtr = dataPtr; return this; } - public int getDataLen() { + public int getDataLen() + { return dataLen; } - public IndexBlock setDataLen(int dataLen) { + public IndexBlock setDataLen(int dataLen) + { this.dataLen = dataLen; return this; } - public static int getIndexBlockLength() { + public static int getIndexBlockLength() + { return LENGTH; } diff --git a/binding/java/src/org/lionsoul/ip2region/Util.java b/binding/java/src/org/lionsoul/ip2region/Util.java index 0a0b153..f94d3ee 100644 --- a/binding/java/src/org/lionsoul/ip2region/Util.java +++ b/binding/java/src/org/lionsoul/ip2region/Util.java @@ -17,8 +17,7 @@ public class Util */ public static void write( byte[] b, int offset, long v, int bytes) { - for ( int i = 0; i < bytes; i++ ) - { + for ( int i = 0; i < bytes; i++ ) { b[offset++] = (byte)((v >>> (8 * i)) & 0xFF); } } @@ -133,8 +132,7 @@ public class Util String[] p = ip.split("\\."); if ( p.length != 4 ) return false; - for ( String pp : p ) - { + for ( String pp : p ) { if ( pp.length() > 3 ) return false; int val = Integer.valueOf(pp); if ( val > 255 ) return false; diff --git a/binding/java/src/org/lionsoul/ip2region/test/TestSearcher.java b/binding/java/src/org/lionsoul/ip2region/test/TestSearcher.java index 73228a8..0717da3 100644 --- a/binding/java/src/org/lionsoul/ip2region/test/TestSearcher.java +++ b/binding/java/src/org/lionsoul/ip2region/test/TestSearcher.java @@ -51,8 +51,7 @@ public class TestSearcher double sTime = 0, cTime = 0; String line = null; DataBlock dataBlock = null; - while ( true ) - { + while ( true ) { System.out.print("ip2region>> "); line = reader.readLine().trim(); if ( line.length() < 2 ) continue; diff --git a/binding/php/Ip2Region.class.php b/binding/php/Ip2Region.class.php index 066930a..ceb019a 100644 --- a/binding/php/Ip2Region.class.php +++ b/binding/php/Ip2Region.class.php @@ -50,8 +50,7 @@ class Ip2Region { //check and conver the ip address if ( is_string($ip) ) $ip = ip2long($ip); - if ( $this->totalBlocks == 0 ) - { + if ( $this->totalBlocks == 0 ) { fseek($this->dbFileHandler, 0); $superBlock = fread($this->dbFileHandler, 8); @@ -61,19 +60,18 @@ class Ip2Region } //binary search to define the data - $l = 0; - $h = $this->totalBlocks; + $l = 0; + $h = $this->totalBlocks; $dataPtr = 0; - while ( $l <= $h ) - { - $m = (($l + $h) >> 1); - $p = $m * INDEX_BLOCK_LENGTH; + while ( $l <= $h ) { + $m = (($l + $h) >> 1); + $p = $m * INDEX_BLOCK_LENGTH; fseek($this->dbFileHandler, $this->firstIndexPtr + $p); $buffer = fread($this->dbFileHandler, INDEX_BLOCK_LENGTH); $sip = self::getLong($buffer, 0); if ( $ip < $sip ) { - $h = $m - 1; + $h = $m - 1; } else { $eip = self::getLong($buffer, 4); if ( $ip > $eip ) { @@ -94,7 +92,7 @@ class Ip2Region $dataPtr = ($dataPtr & 0x00FFFFFF); fseek($this->dbFileHandler, $dataPtr); - $data = fread($this->dbFileHandler, $dataLen); + $data = fread($this->dbFileHandler, $dataLen); return array( 'city_id' => self::getLong($data, 0), @@ -113,17 +111,15 @@ class Ip2Region if ( is_string($ip) ) $ip = ip2long($ip); //check and load the header - if ( $this->HeaderSip == NULL ) - { + if ( $this->HeaderSip == NULL ) { fseek($this->dbFileHandler, 8); - $buffer = fread($this->dbFileHandler, TOTAL_HEADER_LENGTH); + $buffer = fread($this->dbFileHandler, TOTAL_HEADER_LENGTH); //fill the header $idx = 0; $this->HeaderSip = array(); $this->HeaderPtr = array(); - for ( $i = 0; $i < TOTAL_HEADER_LENGTH; $i += 8 ) - { + for ( $i = 0; $i < TOTAL_HEADER_LENGTH; $i += 8 ) { $startIp = self::getLong($buffer, $i); $dataPtr = self::getLong($buffer, $i + 4); if ( $dataPtr == 0 ) break; @@ -138,8 +134,7 @@ class Ip2Region //1. define the index block with the binary search $l = 0; $h = $this->headerLen; $sptr = 0; $eptr = 0; - while ( $l <= $h ) - { + while ( $l <= $h ) { $m = (($l + $h) >> 1); //perfetc matched, just return it @@ -234,8 +229,8 @@ class Ip2Region { return ( (ord($b[$offset++])) | - (ord($b[$offset++]) << 8) | - (ord($b[$offset++]) << 16) | + (ord($b[$offset++]) << 8) | + (ord($b[$offset++]) << 16) | (ord($b[$offset ]) << 24) ); } diff --git a/binding/php/testSeacher.php b/binding/php/testSeacher.php index 9c1768b..5f2d8aa 100644 --- a/binding/php/testSeacher.php +++ b/binding/php/testSeacher.php @@ -5,8 +5,7 @@ * @author chenxin */ -if ( $argc < 2 ) -{ +if ( $argc < 2 ) { $usage = <<> "; $line = trim(fgets(STDIN)); if ( strlen($line) < 2 ) continue; @@ -49,7 +47,7 @@ while ( true ) } $s_time = getTime(); - $data = $method==2 ? $ip2regionObj->binarySearch($line) : $ip2regionObj->btreeSearch($line); + $data = $method==2 ? $ip2regionObj->binarySearch($line) : $ip2regionObj->btreeSearch($line); $c_time = getTime() - $s_time; printf("%s|%s in %.5f millseconds\n", $data['city_id'], $data['region'], $c_time); }