SPSBenchmark/src/resources.erl

44 lines
1.4 KiB
Erlang

-module(resources).
-export([n_pages/3, urls/2]).
%% @spec n_pages(FileName::string(), FromLine::integer(),
%% ToLine::integer()) -> [string()]
%% @doc Read the file specifing the first and last line indexes.
n_pages(FileName, FromLine, ToLine) ->
{ok, FileDev} = file:open(FileName, [raw, read, read_ahead]),
try
read_lines(FileDev, FromLine, ToLine, [])
after
file:close(FileDev)
end.
%% @spec read_lines(FileDev::IoDevice, FromLine::integer(),
%% ToLine::integer(), Acc::[string()]) -> [string()]
%% @doc Read the file specifing the first and last line indexes.
%% The file is not closed by this function.
read_lines(_FileDev, 0, 0, Acc) ->
Acc;
read_lines(FileDev, 0, ToLine, Acc) ->
case file:read_line(FileDev) of
eof -> Acc;
{ok, Line} ->
Page = string:strip(Line, right, $\n),
read_lines(FileDev, 0, ToLine - 1, [Page | Acc])
end;
read_lines(FileDev, FromLine, ToLine, Acc) ->
case file:read_line(FileDev) of
eof -> Acc;
{ok, _} -> read_lines(FileDev, FromLine - 1, ToLine - 1, Acc)
end.
%% @spec urls(Domain::string(), Pages::[string()]) -> [string()]
%% @doc Transform the given pages to urls prefixing the specified domain.
urls(Domain, Pages) ->
[Domain ++ Page || Page <- Pages, valid_page(Page)].
%% @spec valid_page(Page::string()) -> boolean
%% @doc Check if a page is valid.
valid_page(Page) ->
not lists:suffix(".jpg", Page)
and not lists:suffix(".png", Page).