stdlize the code style and add the author info
This commit is contained in:
parent
a67936d835
commit
9de920ef2b
|
|
@ -1,9 +1,10 @@
|
|||
/**
|
||||
* default ip2region implementation
|
||||
* ip2region implementation with mmap support created by Leo Ma
|
||||
*
|
||||
* @see #ip2region.h
|
||||
* @author chenxin<chenxin619315@gmail.com>
|
||||
* @date 2015-10-30
|
||||
* @see #ip2region.h
|
||||
* @author chenxin<chenxin619315@gmail.com>
|
||||
* @author Leo Ma<http://git.oschina.net/begeekmyfriend>
|
||||
* @date 2017/01/12
|
||||
*/
|
||||
|
||||
#include "ip2region.h"
|
||||
|
|
@ -98,7 +99,6 @@ IP2R_API uint_t ip2region_binary_search(ip2region_t ip2rObj, uint_t ip, databloc
|
|||
int l, h, m, p;
|
||||
uint_t sip, eip, dptr;
|
||||
int dataLen, dataptr;
|
||||
long filesize;
|
||||
char *buffer;
|
||||
|
||||
l = 0; h = ip2rObj->totalBlocks; dptr = 0;
|
||||
|
|
@ -142,89 +142,6 @@ IP2R_API uint_t ip2region_binary_search_string(ip2region_t ip2rObj, char *ip, da
|
|||
return ip2region_binary_search(ip2rObj, ip2long(ip), datablock);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the region associated with the specifield ip address with file binary search algorithm
|
||||
*
|
||||
* @param ip2rObj
|
||||
* @param ip
|
||||
* @param datablock
|
||||
* @return uint_t
|
||||
*/
|
||||
IP2R_API uint_t ip2region_file_search(ip2region_t ip2rObj, uint_t ip, datablock_t datablock)
|
||||
{
|
||||
int l, h, m, p;
|
||||
uint_t sip, eip, dptr;
|
||||
char buffer[256];
|
||||
int dataLen, dataptr;
|
||||
|
||||
FILE *dbHandler = fopen(ip2rObj->dbFile, "rb");
|
||||
if ( dbHandler == NULL ) {
|
||||
return 0;
|
||||
}
|
||||
fseek(dbHandler, 0, 0);
|
||||
if ( fread(buffer, 8, 1, dbHandler) != 1 ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
ip2rObj->firstIndexPtr = getUnsignedInt(buffer, 0);
|
||||
ip2rObj->lastIndexPtr = getUnsignedInt(buffer, 4);
|
||||
ip2rObj->totalBlocks = (ip2rObj->lastIndexPtr-ip2rObj->firstIndexPtr)/INDEX_BLOCK_LENGTH + 1;
|
||||
|
||||
//binary search the index blocks to define the data block
|
||||
l = 0; h = ip2rObj->totalBlocks; dptr = 0;
|
||||
while ( l <= h ) {
|
||||
m = (l + h) >> 1;
|
||||
p = ip2rObj->firstIndexPtr + m * INDEX_BLOCK_LENGTH;
|
||||
|
||||
fseek(dbHandler, p, 0);
|
||||
if ( fread(buffer, INDEX_BLOCK_LENGTH, 1, dbHandler) != 1 ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
sip = getUnsignedInt(buffer, 0);
|
||||
if ( ip < sip ) {
|
||||
h = m - 1;
|
||||
} else {
|
||||
eip = getUnsignedInt(buffer, 4);
|
||||
if ( ip > eip ) {
|
||||
l = m + 1;
|
||||
} else {
|
||||
dptr = getUnsignedInt(buffer, 8);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( dptr == 0 ) return 0;
|
||||
|
||||
//get the data
|
||||
dataLen = ((dptr >> 24) & 0xFF);
|
||||
dataptr = (dptr & 0x00FFFFFF);
|
||||
|
||||
//memset(data, 0x00, sizeof(data));
|
||||
fseek(dbHandler, dataptr, 0);
|
||||
if ( fread(buffer, dataLen, 1, dbHandler) != 1 ) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
//fill the data to the datablock
|
||||
datablock->city_id = getUnsignedInt(buffer, 0);
|
||||
dataLen -= 4; //reduce the length of the city_id
|
||||
memcpy(datablock->region, buffer + 4, dataLen);
|
||||
datablock->region[dataLen] = '\0';
|
||||
|
||||
if ( dbHandler != NULL ) {
|
||||
fclose(dbHandler);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
IP2R_API uint_t ip2region_file_search_string(ip2region_t ip2rObj, char *ip, datablock_t datablock)
|
||||
{
|
||||
return ip2region_file_search(ip2rObj, ip2long(ip), datablock);
|
||||
}
|
||||
|
||||
/**
|
||||
* get the region associated with the specifield ip address with b-tree algorithm
|
||||
*
|
||||
|
|
|
|||
|
|
@ -52,9 +52,9 @@ typedef struct {
|
|||
uint_t *HeaderSip; //header start ip blocks
|
||||
uint_t *HeaderPtr; //header ptr blocks
|
||||
uint_t headerLen; //header block number
|
||||
char *dbFile; //path of db file
|
||||
char *dbBinStr; //db binary string for memory search mode
|
||||
uint_t dbSize;
|
||||
char *dbFile; //path of db file
|
||||
char *dbBinStr; //db binary string for memory search mode
|
||||
uint_t dbSize;
|
||||
uint_t firstIndexPtr; //first index ptr
|
||||
uint_t lastIndexPtr; //last index ptr
|
||||
uint_t totalBlocks; //total index blocks number
|
||||
|
|
@ -97,18 +97,6 @@ IP2R_API uint_t ip2region_destroy(ip2region_t);
|
|||
IP2R_API uint_t ip2region_binary_search(ip2region_t, uint_t, datablock_t);
|
||||
IP2R_API uint_t ip2region_binary_search_string(ip2region_t, char *, datablock_t);
|
||||
|
||||
|
||||
/**
|
||||
* get the region associated with the specified ip address with file binary search algorithm
|
||||
*
|
||||
* @param ip2rObj
|
||||
* @param ip
|
||||
* @param datablock
|
||||
* @return uint_t
|
||||
*/
|
||||
IP2R_API uint_t ip2region_file_search(ip2region_t, uint_t, datablock_t);
|
||||
IP2R_API uint_t ip2region_file_search_string(ip2region_t, char *, datablock_t);
|
||||
|
||||
/**
|
||||
* get the region associated with the specified ip address with b-tree algorithm
|
||||
*
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
/**
|
||||
* test ip2region searcher program
|
||||
*
|
||||
* @author chenxin<chenxin619315@gmail.com>
|
||||
* @author chenxin<chenxin619315@gmail.com>
|
||||
* @date 2015-10-30
|
||||
*/
|
||||
|
||||
|
|
@ -58,16 +58,13 @@ int main( int argc, char **argv )
|
|||
return 0;
|
||||
}
|
||||
|
||||
dbFile = argv[1];
|
||||
dbFile = argv[1];
|
||||
algorithm = "B-tree";
|
||||
func_ptr = ip2region_btree_search_string;
|
||||
if ( argc >= 3 ) {
|
||||
if ( strcmp(argv[2], "binary") == 0 ) {
|
||||
algorithm = "File";
|
||||
func_ptr = ip2region_binary_search_string;
|
||||
} else if ( strcmp(argv[2], "file") == 0 ) {
|
||||
algorithm = "File";
|
||||
func_ptr = ip2region_file_search_string;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue