stdlize the code style

This commit is contained in:
lionsoul 2016-04-07 14:33:58 +08:00
parent e2fdbda2ae
commit 6bf163230b
11 changed files with 86 additions and 86 deletions

View File

@ -20,22 +20,19 @@ IP2R_API uint_t ip2region_create(ip2region_t ip2rObj, char *dbFile)
memset(ip2rObj, 0x00, sizeof(ip2region_entry)); memset(ip2rObj, 0x00, sizeof(ip2region_entry));
ip2rObj->headerLen = 0; ip2rObj->headerLen = 0;
ip2rObj->HeaderSip = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH); ip2rObj->HeaderSip = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH);
if ( ip2rObj->HeaderSip == NULL ) if ( ip2rObj->HeaderSip == NULL ) {
{
return 0; return 0;
} }
ip2rObj->HeaderPtr = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH); ip2rObj->HeaderPtr = (uint_t *) IP2R_MALLOC(TOTAL_HEADER_LENGTH);
if ( ip2rObj->HeaderPtr == NULL ) if ( ip2rObj->HeaderPtr == NULL ) {
{
IP2R_FREE(ip2rObj->HeaderSip); IP2R_FREE(ip2rObj->HeaderSip);
return 0; return 0;
} }
//open the db file //open the db file
ip2rObj->dbHandler = fopen(dbFile, "rb"); ip2rObj->dbHandler = fopen(dbFile, "rb");
if ( ip2rObj->dbHandler == NULL ) if ( ip2rObj->dbHandler == NULL ) {
{
IP2R_FREE(ip2rObj->HeaderSip); IP2R_FREE(ip2rObj->HeaderSip);
IP2R_FREE(ip2rObj->HeaderPtr); IP2R_FREE(ip2rObj->HeaderPtr);
return 0; return 0;
@ -61,8 +58,7 @@ IP2R_API uint_t ip2region_destroy(ip2region_t ip2rObj)
ip2rObj->HeaderPtr = NULL; ip2rObj->HeaderPtr = NULL;
//close the db file resource //close the db file resource
if ( ip2rObj->dbHandler != NULL ) if ( ip2rObj->dbHandler != NULL ) {
{
fclose(ip2rObj->dbHandler); fclose(ip2rObj->dbHandler);
ip2rObj->dbHandler = NULL; ip2rObj->dbHandler = NULL;
} }
@ -85,8 +81,7 @@ IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, databloc
char buffer[256]; char buffer[256];
int dataLen, dataptr; int dataLen, dataptr;
if ( ip2rObj->totalBlocks == 0 ) if ( ip2rObj->totalBlocks == 0 ) {
{
fseek(ip2rObj->dbHandler, 0, 0); fseek(ip2rObj->dbHandler, 0, 0);
if ( fread(buffer, 8, 1, ip2rObj->dbHandler) != 1 ) { if ( fread(buffer, 8, 1, ip2rObj->dbHandler) != 1 ) {
return 0; 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 ) { if ( fread(buffer, INDEX_BLOCK_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
return 0; return 0;
} }
sip = getUnsignedInt(buffer, 0); sip = getUnsignedInt(buffer, 0);
if ( ip < sip ) { if ( ip < sip ) {
h = m - 1; 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; uint_t sip, eip, idxptr, dptr;
char buffer[TOTAL_HEADER_LENGTH]; char buffer[TOTAL_HEADER_LENGTH];
if ( ip2rObj->headerLen == 0 ) if ( ip2rObj->headerLen == 0 ) {
{
idx = 0; idx = 0;
fseek(ip2rObj->dbHandler, 8, 0); //pass the super block fseek(ip2rObj->dbHandler, 8, 0); //pass the super block
if ( fread(buffer, TOTAL_HEADER_LENGTH, 1, ip2rObj->dbHandler) != 1 ) { if ( fread(buffer, TOTAL_HEADER_LENGTH, 1, ip2rObj->dbHandler) != 1 ) {
return 0; return 0;
} }
for ( i = 0; i < TOTAL_HEADER_LENGTH; i += 8 ) for ( i = 0; i < TOTAL_HEADER_LENGTH; i += 8 ) {
{
sip = getUnsignedInt(buffer, i); sip = getUnsignedInt(buffer, i);
idxptr = getUnsignedInt(buffer, i + 4); idxptr = getUnsignedInt(buffer, i + 4);
if ( idxptr == 0 ) break; if ( idxptr == 0 ) break;
@ -307,8 +301,7 @@ IP2R_API uint_t ip2long(char *ip)
char buffer[4], *cs = ip; char buffer[4], *cs = ip;
uint_t ipval = 0; uint_t ipval = 0;
while ( *cs != '\0' ) while ( *cs != '\0' ) {
{
if ( *cs == '.' ) { if ( *cs == '.' ) {
//single part length limit //single part length limit
if ( i > 3 ) { if ( i > 3 ) {

View File

@ -68,16 +68,14 @@ int main( int argc, char **argv )
//create a new ip2rObj //create a new ip2rObj
printf("+--initializing %s ... \n", algorithm); 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"); println("Error: Fail to create the ip2region object");
return 0; return 0;
} }
__PRINT_ABOUT__; __PRINT_ABOUT__;
while ( 1 ) while ( 1 ) {
{
print("ip2region>> "); print("ip2region>> ");
getLine( stdin, line ); getLine( stdin, line );
if ( strlen(line) < 2 ) continue; if ( strlen(line) < 2 ) continue;

View File

@ -23,20 +23,24 @@ public class DataBlock
this.region = region; this.region = region;
} }
public int getCityId() { public int getCityId()
{
return city_id; return city_id;
} }
public DataBlock setCityId(int city_id) { public DataBlock setCityId(int city_id)
{
this.city_id = city_id; this.city_id = city_id;
return this; return this;
} }
public String getRegion() { public String getRegion()
{
return region; return region;
} }
public DataBlock setRegion(String region) { public DataBlock setRegion(String region)
{
this.region = region; this.region = region;
return this; return this;
} }

View File

@ -25,9 +25,9 @@ public class DbConfig
* @param dataBlockSize * @param dataBlockSize
* @throws DbMakerConfigException * @throws DbMakerConfigException
*/ */
public DbConfig( int totalHeaderSize ) throws DbMakerConfigException { public DbConfig( int totalHeaderSize ) throws DbMakerConfigException
if ( (totalHeaderSize % 8) != 0 )
{ {
if ( (totalHeaderSize % 8) != 0 ) {
throw new DbMakerConfigException("totalHeaderSize must be times of 8"); throw new DbMakerConfigException("totalHeaderSize must be times of 8");
} }
@ -35,24 +35,29 @@ public class DbConfig
this.indexBlockSize = 4096; //4 * 1024 this.indexBlockSize = 4096; //4 * 1024
} }
public DbConfig() throws DbMakerConfigException { public DbConfig() throws DbMakerConfigException
{
this(8192); this(8192);
} }
public int getTotalHeaderSize() { public int getTotalHeaderSize()
{
return totalHeaderSize; return totalHeaderSize;
} }
public DbConfig setTotalHeaderSize(int totalHeaderSize) { public DbConfig setTotalHeaderSize(int totalHeaderSize)
{
this.totalHeaderSize = totalHeaderSize; this.totalHeaderSize = totalHeaderSize;
return this; return this;
} }
public int getIndexBlockSize() { public int getIndexBlockSize()
{
return indexBlockSize; return indexBlockSize;
} }
public DbConfig setIndexBlockSize(int dataBlockSize) { public DbConfig setIndexBlockSize(int dataBlockSize)
{
this.indexBlockSize = dataBlockSize; this.indexBlockSize = dataBlockSize;
return this; return this;
} }

View File

@ -88,8 +88,7 @@ public class DbSearcher
public DataBlock btreeSearch( long ip ) throws IOException public DataBlock btreeSearch( long ip ) throws IOException
{ {
//check and load the header //check and load the header
if ( HeaderSip == null ) if ( HeaderSip == null ) {
{
raf.seek(8L); //pass the super block raf.seek(8L); //pass the super block
//byte[] b = new byte[dbConfig.getTotalHeaderSize()]; //byte[] b = new byte[dbConfig.getTotalHeaderSize()];
byte[] b = new byte[4096]; byte[] b = new byte[4096];
@ -121,8 +120,7 @@ public class DbSearcher
} }
int l = 0, h = headerLength, sptr = 0, eptr = 0; int l = 0, h = headerLength, sptr = 0, eptr = 0;
while ( l <= h ) while ( l <= h ) {
{
int m = (l + h) >> 1; int m = (l + h) >> 1;
//perfetc matched, just return it //perfetc matched, just return it
@ -230,8 +228,7 @@ public class DbSearcher
public DataBlock binarySearch( long ip ) throws IOException public DataBlock binarySearch( long ip ) throws IOException
{ {
int blen = IndexBlock.getIndexBlockLength(); int blen = IndexBlock.getIndexBlockLength();
if ( totalIndexBlocks == 0 ) if ( totalIndexBlocks == 0 ) {
{
raf.seek(0L); raf.seek(0L);
byte[] superBytes = new byte[8]; byte[] superBytes = new byte[8];
raf.readFully(superBytes, 0, superBytes.length); raf.readFully(superBytes, 0, superBytes.length);

View File

@ -23,20 +23,24 @@ public class HeaderBlock
this.indexPtr = indexPtr; this.indexPtr = indexPtr;
} }
public long getIndexStartIp() { public long getIndexStartIp()
{
return indexStartIp; return indexStartIp;
} }
public HeaderBlock setIndexStartIp(long indexStartIp) { public HeaderBlock setIndexStartIp(long indexStartIp)
{
this.indexStartIp = indexStartIp; this.indexStartIp = indexStartIp;
return this; return this;
} }
public int getIndexPtr() { public int getIndexPtr()
{
return indexPtr; return indexPtr;
} }
public HeaderBlock setIndexPtr(int indexPtr) { public HeaderBlock setIndexPtr(int indexPtr)
{
this.indexPtr = indexPtr; this.indexPtr = indexPtr;
return this; return this;
} }

View File

@ -37,43 +37,52 @@ public class IndexBlock
this.dataLen = dataLen; this.dataLen = dataLen;
} }
public long getStartIp() { public long getStartIp()
{
return startIp; return startIp;
} }
public IndexBlock setStartIp(long startIp) { public IndexBlock setStartIp(long startIp)
{
this.startIp = startIp; this.startIp = startIp;
return this; return this;
} }
public long getEndIp() { public long getEndIp()
{
return endIp; return endIp;
} }
public IndexBlock setEndIp(long endIp) { public IndexBlock setEndIp(long endIp)
{
this.endIp = endIp; this.endIp = endIp;
return this; return this;
} }
public int getDataPtr() { public int getDataPtr()
{
return dataPtr; return dataPtr;
} }
public IndexBlock setDataPtr(int dataPtr) { public IndexBlock setDataPtr(int dataPtr)
{
this.dataPtr = dataPtr; this.dataPtr = dataPtr;
return this; return this;
} }
public int getDataLen() { public int getDataLen()
{
return dataLen; return dataLen;
} }
public IndexBlock setDataLen(int dataLen) { public IndexBlock setDataLen(int dataLen)
{
this.dataLen = dataLen; this.dataLen = dataLen;
return this; return this;
} }
public static int getIndexBlockLength() { public static int getIndexBlockLength()
{
return LENGTH; return LENGTH;
} }

View File

@ -17,8 +17,7 @@ public class Util
*/ */
public static void write( byte[] b, int offset, long v, int bytes) 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); b[offset++] = (byte)((v >>> (8 * i)) & 0xFF);
} }
} }
@ -133,8 +132,7 @@ public class Util
String[] p = ip.split("\\."); String[] p = ip.split("\\.");
if ( p.length != 4 ) return false; if ( p.length != 4 ) return false;
for ( String pp : p ) for ( String pp : p ) {
{
if ( pp.length() > 3 ) return false; if ( pp.length() > 3 ) return false;
int val = Integer.valueOf(pp); int val = Integer.valueOf(pp);
if ( val > 255 ) return false; if ( val > 255 ) return false;

View File

@ -51,8 +51,7 @@ public class TestSearcher
double sTime = 0, cTime = 0; double sTime = 0, cTime = 0;
String line = null; String line = null;
DataBlock dataBlock = null; DataBlock dataBlock = null;
while ( true ) while ( true ) {
{
System.out.print("ip2region>> "); System.out.print("ip2region>> ");
line = reader.readLine().trim(); line = reader.readLine().trim();
if ( line.length() < 2 ) continue; if ( line.length() < 2 ) continue;

View File

@ -50,8 +50,7 @@ class Ip2Region
{ {
//check and conver the ip address //check and conver the ip address
if ( is_string($ip) ) $ip = ip2long($ip); if ( is_string($ip) ) $ip = ip2long($ip);
if ( $this->totalBlocks == 0 ) if ( $this->totalBlocks == 0 ) {
{
fseek($this->dbFileHandler, 0); fseek($this->dbFileHandler, 0);
$superBlock = fread($this->dbFileHandler, 8); $superBlock = fread($this->dbFileHandler, 8);
@ -64,8 +63,7 @@ class Ip2Region
$l = 0; $l = 0;
$h = $this->totalBlocks; $h = $this->totalBlocks;
$dataPtr = 0; $dataPtr = 0;
while ( $l <= $h ) while ( $l <= $h ) {
{
$m = (($l + $h) >> 1); $m = (($l + $h) >> 1);
$p = $m * INDEX_BLOCK_LENGTH; $p = $m * INDEX_BLOCK_LENGTH;
@ -113,8 +111,7 @@ class Ip2Region
if ( is_string($ip) ) $ip = ip2long($ip); if ( is_string($ip) ) $ip = ip2long($ip);
//check and load the header //check and load the header
if ( $this->HeaderSip == NULL ) if ( $this->HeaderSip == NULL ) {
{
fseek($this->dbFileHandler, 8); fseek($this->dbFileHandler, 8);
$buffer = fread($this->dbFileHandler, TOTAL_HEADER_LENGTH); $buffer = fread($this->dbFileHandler, TOTAL_HEADER_LENGTH);
@ -122,8 +119,7 @@ class Ip2Region
$idx = 0; $idx = 0;
$this->HeaderSip = array(); $this->HeaderSip = array();
$this->HeaderPtr = 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); $startIp = self::getLong($buffer, $i);
$dataPtr = self::getLong($buffer, $i + 4); $dataPtr = self::getLong($buffer, $i + 4);
if ( $dataPtr == 0 ) break; if ( $dataPtr == 0 ) break;
@ -138,8 +134,7 @@ class Ip2Region
//1. define the index block with the binary search //1. define the index block with the binary search
$l = 0; $h = $this->headerLen; $sptr = 0; $eptr = 0; $l = 0; $h = $this->headerLen; $sptr = 0; $eptr = 0;
while ( $l <= $h ) while ( $l <= $h ) {
{
$m = (($l + $h) >> 1); $m = (($l + $h) >> 1);
//perfetc matched, just return it //perfetc matched, just return it

View File

@ -5,8 +5,7 @@
* @author chenxin<chenxin619315@gmail.com> * @author chenxin<chenxin619315@gmail.com>
*/ */
if ( $argc < 2 ) if ( $argc < 2 ) {
{
$usage = <<<EOF $usage = <<<EOF
Usage: php Test.php [ip2region db file] [alrogrithm] Usage: php Test.php [ip2region db file] [alrogrithm]
+-Algorithm: binary or b-tree\n +-Algorithm: binary or b-tree\n
@ -37,8 +36,7 @@ initializing {$algorithm} ...
INIT; INIT;
echo $initStr, "\n"; echo $initStr, "\n";
while ( true ) while ( true ) {
{
echo "ip2region>> "; echo "ip2region>> ";
$line = trim(fgets(STDIN)); $line = trim(fgets(STDIN));
if ( strlen($line) < 2 ) continue; if ( strlen($line) < 2 ) continue;