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
|
%%%----------------------------------------------------------------------
%%% File : file_gl.erl
%%% Author : Luke Gorrie <luke@bluetail.com>
%%% Purpose : Group leader server for writing to a file
%%% Created : 22 Oct 2000 by Luke Gorrie <luke@bluetail.com>
%%%----------------------------------------------------------------------
-module(file_gl).
-author('luke@bluetail.com').
-behaviour(gen_server).
%% External exports
-export([start_link/1]).
%% gen_server callbacks
-export([init/1, handle_info/2, terminate/2, code_change/3,
handle_call/3, handle_cast/2]).
-record(state, {fd}).
%%%----------------------------------------------------------------------
%%% API
%%%----------------------------------------------------------------------
start_link(Filename) ->
gen_server:start_link({local, file_gl}, file_gl, Filename, []).
%%%----------------------------------------------------------------------
%%% Callback functions from gen_server
%%%----------------------------------------------------------------------
init(Filename) ->
{ok, Fd} = file:open(Filename, [write]),
{ok, #state{fd=Fd}}.
handle_info({io_request, From, ReplyAs, {put_chars, C}}, State) ->
file:write(State#state.fd, C),
From ! {io_reply, ReplyAs, ok},
{noreply, State};
handle_info({io_request, From, ReplyAs, {put_chars, M, F, A}}, State) ->
file:write(State#state.fd, apply(M, F, A)),
From ! {io_reply, ReplyAs, ok},
{noreply, State};
handle_info({io_request, From, ReplyAs, {get_until, _, _, _}}, State) ->
From ! {io_reply, ReplyAs, eof},
{noreply, State};
handle_info(Info, State) ->
{noreply, State}.
handle_call(_, _, _) ->
exit(no_such_callback).
handle_cast(_, _) ->
exit(no_such_callback).
terminate(Reason, State) ->
file:close(State#state.fd),
ok.
code_change(OldVsn, State, Extra) ->
{ok, State}.
|