-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjj.yaws
90 lines (78 loc) · 2.01 KB
/
jj.yaws
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<erl>
%
% ircDDB livelog
%
% Copyright (C) 2010 Michael Dirska, DL1BFF ([email protected])
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%
%
out(A) ->
[ {header, {cache_control, "no-cache"}},
{content, "text/plain",
case file:open("/home/mdirska/web/livelog2.txt",[read, raw]) of
{ok, Fd} ->
do_read(Fd, A);
Err ->
"err"
end } ].
do_read(Fd, A) ->
case file:position(Fd, {eof, 0}) of
{ok, NewPosition} ->
EndPos = NewPosition div 100,
P = yaws_api:parse_query(A),
ReqPos = case lists:keysearch("p", 1, P) of
{ K, { Key, Value } } ->
try
list_to_integer(Value)
catch
error:Reason -> 0
end ;
false ->
0
end,
Num = EndPos - ReqPos,
if
(Num > 0) and (Num < 100) ->
do_read(Fd, ReqPos, Num - 1);
true ->
if
EndPos =< 100 ->
do_read(Fd, 0, EndPos - 1);
true ->
do_read(Fd, EndPos - 100, 99)
end
end;
{error, Reason} ->
f("err ~p", [Reason])
end.
do_read(Fd, Pos, Num) ->
file:position(Fd, Pos * 100),
Result = f("~B:~s~n", [Pos,
case file:read(Fd, 99) of
{ok, Data} ->
string:strip(Data);
eof ->
"";
{error, Reason} ->
f("error ~p", [Reason])
end ]) ,
if
Num > 0 ->
Result ++ do_read(Fd, Pos + 1, Num - 1);
true ->
file:close(Fd),
Result
end.
</erl>