Skip to content

Commit

Permalink
fix whitespace issues
Browse files Browse the repository at this point in the history
  • Loading branch information
etrepum committed Sep 21, 2011
1 parent b3179d5 commit 9eb8911
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 204 deletions.
248 changes: 124 additions & 124 deletions src/egeoip.erl
Expand Up @@ -326,10 +326,10 @@ init(FileName) ->
%% @spec handle_call(Msg, From, State) -> term()
%% @doc gen_server callback.
handle_call(What,From,State) ->
try
try
do_handle_call(What,From,State)
catch
_:R ->
catch
_:R ->
log_error([{handle_call,What},{error,R}]),
{reply,{error,R},State}
end.
Expand Down Expand Up @@ -386,13 +386,13 @@ new(city) ->
new(default_db(["GeoIPCity.dat", "GeoLiteCity.dat"]));
new(Path) ->
case filelib:is_file(Path) of
true ->
Data = load_file(Path),
Max = ?STRUCTURE_INFO_MAX_SIZE,
R = {ok, State} = read_structures(Path, Data, size(Data) - 3, Max),
true ->
Data = load_file(Path),
Max = ?STRUCTURE_INFO_MAX_SIZE,
R = {ok, State} = read_structures(Path, Data, size(Data) - 3, Max),
ok = check_state(State),
R;
false ->
false ->
{error, {geoip_db_not_found,Path}}
end.

Expand All @@ -402,10 +402,10 @@ lookup(D, Addr) when is_list(Addr);
is_tuple(Addr);
is_binary(Addr) ->
case ip2long(Addr) of
{ok, Ip} ->
lookup(D, Ip);
Error ->
Error
{ok, Ip} ->
lookup(D, Ip);
Error ->
Error
end;
lookup(D, Addr) when is_integer(Addr) ->
get_record(D, Addr).
Expand All @@ -423,33 +423,33 @@ default_db([Path | Rest]) ->

address_fast([N2, N1, N0, $. | Rest], Num, Shift) when Shift >= 8 ->
case list_to_integer([N2, N1, N0]) of
N when N =< 255 ->
address_fast(Rest, Num bor (N bsl Shift), Shift - 8)
N when N =< 255 ->
address_fast(Rest, Num bor (N bsl Shift), Shift - 8)
end;
address_fast([N1, N0, $. | Rest], Num, Shift) when Shift >= 8 ->
case list_to_integer([N1, N0]) of
N when N =< 255 ->
address_fast(Rest, Num bor (N bsl Shift), Shift - 8)
N when N =< 255 ->
address_fast(Rest, Num bor (N bsl Shift), Shift - 8)
end;
address_fast([N0, $. | Rest], Num, Shift) when Shift >= 8 ->
case N0 - $0 of
N when N =< 255 ->
address_fast(Rest, Num bor (N bsl Shift), Shift - 8)
N when N =< 255 ->
address_fast(Rest, Num bor (N bsl Shift), Shift - 8)
end;
address_fast(L=[_N2, _N1, _N0], Num, 0) ->
case list_to_integer(L) of
N when N =< 255 ->
Num bor N
N when N =< 255 ->
Num bor N
end;
address_fast(L=[_N1, _N0], Num, 0) ->
case list_to_integer(L) of
N when N =< 255 ->
Num bor N
N when N =< 255 ->
Num bor N
end;
address_fast([N0], Num, 0) ->
case N0 - $0 of
N when N =< 255 ->
Num bor N
N when N =< 255 ->
Num bor N
end.

%% @spec ip2long(Address) -> {ok, integer()}
Expand All @@ -459,21 +459,21 @@ ip2long(Address) when is_integer(Address) ->
{ok, Address};
ip2long(Address) when is_list(Address) ->
case catch address_fast(Address, 0, 24) of
N when is_integer(N) ->
{ok, N};
_ ->
case inet_parse:address(Address) of
{ok, Tuple} ->
ip2long(Tuple);
Error ->
Error
end
N when is_integer(N) ->
{ok, N};
_ ->
case inet_parse:address(Address) of
{ok, Tuple} ->
ip2long(Tuple);
Error ->
Error
end
end;
ip2long({B3, B2, B1, B0}) ->
{ok, (B3 bsl 24) bor (B2 bsl 16) bor (B1 bsl 8) bor B0};
ip2long({W7, W6, W5, W4, W3, W2, W1, W0}) ->
{ok, (W7 bsl 112) bor (W6 bsl 96) bor (W5 bsl 80) bor (W4 bsl 64) bor
(W3 bsl 48) bor (W2 bsl 32) bor (W1 bsl 16) bor W0};
(W3 bsl 48) bor (W2 bsl 32) bor (W1 bsl 16) bor W0};
ip2long(<<Addr:32>>) ->
{ok, Addr};
ip2long(<<Addr:128>>) ->
Expand All @@ -483,10 +483,10 @@ ip2long(_) ->

get_record(D, Ip) ->
case seek_country(D, Ip) of
{ok, SeekCountry} ->
get_record(D, Ip, SeekCountry);
Error ->
Error
{ok, SeekCountry} ->
get_record(D, Ip, SeekCountry);
Error ->
Error
end.


Expand All @@ -496,44 +496,44 @@ read_structures(_Path, _Data, _, 0) ->
read_structures(Path, Data, Seek, N) ->
<<_:Seek/binary, Delim:3/binary, _/binary>> = Data,
case Delim of
<<255, 255, 255>> ->
<<_:Seek/binary, _:3/binary, DbType, _/binary>> = Data,
Type = case DbType >= 106 of
true ->
DbType - 105;
false ->
DbType
end,
Segments = case Type of
?GEOIP_REGION_EDITION_REV0 ->
?GEOIP_STATE_BEGIN_REV0;
?GEOIP_REGION_EDITION_REV1 ->
?GEOIP_STATE_BEGIN_REV1;
?GEOIP_COUNTRY_EDITION ->
?GEOIP_COUNTRY_BEGIN;
?GEOIP_PROXY_EDITION ->
?GEOIP_COUNTRY_BEGIN;
?GEOIP_NETSPEED_EDITION ->
?GEOIP_COUNTRY_BEGIN;
_ ->
read_segments(Type, Data, Seek + 4)
end,
Length = case Type of
?GEOIP_ORG_EDITION ->
?ORG_RECORD_LENGTH;
?GEOIP_ISP_EDITION ->
?ORG_RECORD_LENGTH;
_ ->
?STANDARD_RECORD_LENGTH
end,
Rec = #geoipdb{type = Type,
segments = Segments,
record_length = Length,
data = Data,
filename = Path},
{ok, Rec};
_ ->
read_structures(Path, Data, Seek - 1, N - 1)
<<255, 255, 255>> ->
<<_:Seek/binary, _:3/binary, DbType, _/binary>> = Data,
Type = case DbType >= 106 of
true ->
DbType - 105;
false ->
DbType
end,
Segments = case Type of
?GEOIP_REGION_EDITION_REV0 ->
?GEOIP_STATE_BEGIN_REV0;
?GEOIP_REGION_EDITION_REV1 ->
?GEOIP_STATE_BEGIN_REV1;
?GEOIP_COUNTRY_EDITION ->
?GEOIP_COUNTRY_BEGIN;
?GEOIP_PROXY_EDITION ->
?GEOIP_COUNTRY_BEGIN;
?GEOIP_NETSPEED_EDITION ->
?GEOIP_COUNTRY_BEGIN;
_ ->
read_segments(Type, Data, Seek + 4)
end,
Length = case Type of
?GEOIP_ORG_EDITION ->
?ORG_RECORD_LENGTH;
?GEOIP_ISP_EDITION ->
?ORG_RECORD_LENGTH;
_ ->
?STANDARD_RECORD_LENGTH
end,
Rec = #geoipdb{type = Type,
segments = Segments,
record_length = Length,
data = Data,
filename = Path},
{ok, Rec};
_ ->
read_structures(Path, Data, Seek - 1, N - 1)
end.


Expand All @@ -555,15 +555,15 @@ get_record(D, _Ip, SeekCountry) ->
Type = D#geoipdb.type,
{DmaCode, AreaCode} = get_record_ex(Type, Country, Data, Seek3 + 6),
Record = #geoip{country_code = Country,
country_code3 = Country3,
country_name = CountryName,
region = Region,
city = City,
postal_code = Postal,
latitude = Lat,
longitude = Lon,
dma_code = DmaCode,
area_code = AreaCode},
country_code3 = Country3,
country_name = CountryName,
region = Region,
city = City,
postal_code = Postal,
latitude = Lat,
longitude = Lon,
dma_code = DmaCode,
area_code = AreaCode},
{ok, Record}.

get_record_ex(?GEOIP_CITY_EDITION_REV1, "US", Data, Seek) ->
Expand All @@ -585,26 +585,26 @@ seek_country(D, Ip, Offset, Depth) ->
Seek = 2 * RecordLength * Offset,
<<_:Seek/binary, X0:RB/little, X1:RB/little, _/binary>> = D#geoipdb.data,
X = case (Ip band (1 bsl Depth)) of
0 -> X0;
_ -> X1
end,
0 -> X0;
_ -> X1
end,
case (X >= D#geoipdb.segments) of
true ->
{ok, X};
false ->
seek_country(D, Ip, X, Depth - 1)
true ->
{ok, X};
false ->
seek_country(D, Ip, X, Depth - 1)
end.

until_null(Binary, Start, Index) ->
Skip = Start + Index,
<<_:Skip/binary, Byte, _/binary>> = Binary,
case Byte of
0 ->
Length = Skip - Start,
<<_:Start/binary, Result:Length/binary, _/binary>> = Binary,
{Result, 1 + Skip};
_ ->
until_null(Binary, Start, 1 + Index)
0 ->
Length = Skip - Start,
<<_:Start/binary, Result:Length/binary, _/binary>> = Binary,
{Result, 1 + Skip};
_ ->
until_null(Binary, Start, 1 + Index)
end.

check_state(D) ->
Expand All @@ -615,30 +615,30 @@ check_state(D) ->

country_code(D, Number) ->
try
element(Number, D#geoipdb.country_codes)
element(Number, D#geoipdb.country_codes)
catch
error:badarg -> ""
error:badarg -> ""
end.

country_code3(D, Number) ->
try
element(Number, D#geoipdb.country_codes3)
element(Number, D#geoipdb.country_codes3)
catch
error:badarg -> ""
error:badarg -> ""
end.

country_name(D, Number) ->
try
element(Number, D#geoipdb.country_names)
element(Number, D#geoipdb.country_names)
catch
error:badarg -> ""
error:badarg -> ""
end.

read_segments(Type, Data, Seek) when Type == ?GEOIP_CITY_EDITION_REV0;
Type == ?GEOIP_CITY_EDITION_REV1;
Type == ?GEOIP_ORG_EDITION;
Type == ?GEOIP_ISP_EDITION;
Type == ?GEOIP_ASNUM_EDITION ->
Type == ?GEOIP_CITY_EDITION_REV1;
Type == ?GEOIP_ORG_EDITION;
Type == ?GEOIP_ISP_EDITION;
Type == ?GEOIP_ASNUM_EDITION ->
Bits = ?SEGMENT_RECORD_LENGTH * 8,
<<_:Seek/binary, Segments:Bits/little, _/binary>> = Data,
Segments.
Expand All @@ -653,13 +653,13 @@ priv_path(Components) ->

load_file(Path) ->
case file:read_file(Path) of
{ok, Raw} ->
case filename:extension(Path) of
".gz" ->
zlib:gunzip(Raw);
_ ->
Raw
end
{ok, Raw} ->
case filename:extension(Path) of
".gz" ->
zlib:gunzip(Raw);
_ ->
Raw
end
end.

benchcall(Fun, 1) ->
Expand All @@ -673,15 +673,15 @@ pytime({MegaSecs, Secs, MicroSecs}) ->

bench(Count) ->
SampleIPs = ["63.224.214.117",
"144.139.80.91",
"88.233.53.82",
"85.250.32.5",
"220.189.211.182",
"211.112.118.99",
"84.94.205.244",
"61.16.226.206",
"64.180.1.78",
"138.217.4.11"],
"144.139.80.91",
"88.233.53.82",
"85.250.32.5",
"220.189.211.182",
"211.112.118.99",
"84.94.205.244",
"61.16.226.206",
"64.180.1.78",
"138.217.4.11"],
StartParse = now(),
benchcall(fun () -> [lookup(X) || X <- SampleIPs] end, Count),
EndParse = now(),
Expand Down

0 comments on commit 9eb8911

Please sign in to comment.