Skip to content
Newer
Older
100644 44 lines (33 sloc) 1.08 KB
b4c3c37 @etrepum more refactoring, new mochitemp module for creating and removing temp…
etrepum authored Mar 25, 2010
1 %% @author Bob Ippolito <bob@mochimedia.com>
2 %% @copyright 2007 Mochi Media, Inc.
3
4 %% @doc Utilities for dealing with IO devices (open files).
5
6 -module(mochiweb_io).
7 -author('bob@mochimedia.com').
8
9 -export([iodevice_stream/3, iodevice_stream/2]).
10 -export([iodevice_foldl/4, iodevice_foldl/3]).
11 -export([iodevice_size/1]).
12 -define(READ_SIZE, 8192).
13
14 iodevice_foldl(F, Acc, IoDevice) ->
15 iodevice_foldl(F, Acc, IoDevice, ?READ_SIZE).
16
17 iodevice_foldl(F, Acc, IoDevice, BufferSize) ->
18 case file:read(IoDevice, BufferSize) of
19 eof ->
20 Acc;
21 {ok, Data} ->
22 iodevice_foldl(F, F(Data, Acc), IoDevice, BufferSize)
23 end.
24
25 iodevice_stream(Callback, IoDevice) ->
26 iodevice_stream(Callback, IoDevice, ?READ_SIZE).
27
28 iodevice_stream(Callback, IoDevice, BufferSize) ->
29 F = fun (Data, ok) -> Callback(Data) end,
30 ok = iodevice_foldl(F, ok, IoDevice, BufferSize).
31
32 iodevice_size(IoDevice) ->
33 {ok, Size} = file:position(IoDevice, eof),
34 {ok, 0} = file:position(IoDevice, bof),
35 Size.
36
37
38 %%
39 %% Tests
40 %%
41 -ifdef(TEST).
cb46038 @etrepum include eunit only if TEST is already defined
etrepum authored Dec 1, 2010
42 -include_lib("eunit/include/eunit.hrl").
b4c3c37 @etrepum more refactoring, new mochitemp module for creating and removing temp…
etrepum authored Mar 25, 2010
43 -endif.
Something went wrong with that request. Please try again.