mirror of
https://github.com/lionsoul2014/ip2region.git
synced 2025-12-08 19:25:22 +00:00
stdlize the code style
This commit is contained in:
parent
e2fdbda2ae
commit
6bf163230b
@ -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 ) {
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -64,8 +63,7 @@ class Ip2Region
|
||||
$l = 0;
|
||||
$h = $this->totalBlocks;
|
||||
$dataPtr = 0;
|
||||
while ( $l <= $h )
|
||||
{
|
||||
while ( $l <= $h ) {
|
||||
$m = (($l + $h) >> 1);
|
||||
$p = $m * INDEX_BLOCK_LENGTH;
|
||||
|
||||
@ -113,8 +111,7 @@ 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);
|
||||
|
||||
@ -122,8 +119,7 @@ class Ip2Region
|
||||
$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
|
||||
|
||||
@ -5,8 +5,7 @@
|
||||
* @author chenxin<chenxin619315@gmail.com>
|
||||
*/
|
||||
|
||||
if ( $argc < 2 )
|
||||
{
|
||||
if ( $argc < 2 ) {
|
||||
$usage = <<<EOF
|
||||
Usage: php Test.php [ip2region db file] [alrogrithm]
|
||||
+-Algorithm: binary or b-tree\n
|
||||
@ -37,8 +36,7 @@ initializing {$algorithm} ...
|
||||
INIT;
|
||||
echo $initStr, "\n";
|
||||
|
||||
while ( true )
|
||||
{
|
||||
while ( true ) {
|
||||
echo "ip2region>> ";
|
||||
$line = trim(fgets(STDIN));
|
||||
if ( strlen($line) < 2 ) continue;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user