aboutsummaryrefslogtreecommitdiffstats
path: root/lib/pman/src/pman_buf_buffer.erl
blob: ad92eb1f3e680598f339af3cd6e2c188eef3b4d5 (plain) (blame)
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
91
92
93
94
95
96
97
98
99
100
101
102
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1997-2009. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%
%%%----------------------------------------------------------------------
%%% Purpose : The purpouse of the buffer process is to take
%%%           care of the data that is received by the converter
%%%           process and pass it on to the printer process in chunks
%%%	      that can be handled.
%%%
%%%           This module is a part of the buffering system, and
%%%           should not be used except through the API defined
%%%           in the pman_buf module.
%%%
%%%----------------------------------------------------------------------

-module(pman_buf_buffer).

%%-compile(export_all).
-export([init/1]).

-include("pman_buf.hrl").



%%
%% Initialization function for the buffer process.
%% To be started with spawn from the calling process.
%%

init(Editor) ->
    Printer_pid = spawn_link(pman_buf_printer,init,[Editor,self()]),
    receive
	{converter_pid,Pid} ->
	    Pid!{buffer,accept},
	    buffer_loop([],0,0,Printer_pid,Pid)
    end.



%%
%% Receive loop for the buffer process.
%%

buffer_loop(Buffer,Size,Acc,Printer,Converter) ->
    receive
	{save_buffer,Name} ->
	    Printer!{save_buffer,Name},
	    buffer_loop(Buffer,Size,Acc,Printer,Converter);
	{raw,Raw,Length} ->   %%output to editor
	    New_Size = Size + Length,
	    if New_Size < ?BUFF_SIZE ->
		    Converter!{buffer,accept};
	       true -> ok
	    end,
	    Print = lists:map(fun(X) -> pman_buf_utils:textformat(X) end, Raw),
	    New_Buff = lists:append(Buffer,Print),
	    buffer_loop(New_Buff,New_Size,Acc,Printer,Converter);
	{clear,Text,N_Converter} ->
	    Converter!{buffer,accept},
	    Printer!clear,
	    buffer_loop([Text],1,1,Printer,N_Converter);
	{printer,send} when Buffer /= [] ->
	     if
		 Acc  > ?EDITOR_MAX ->
		     Printer!clear,
		     Printer !{buffer,"Cleared Buffer due to Size\n\n"},
		     buffer_loop(Buffer,Size,1,Printer,Converter);
		 true ->
		     {Length,Rest,Print} = pman_buf_utils:split(Buffer,
								?PRINT_LEN,
								0,
								[]),
		     Printer ! {buffer,Print},
		     New_Size = Size - Length,
		     if New_Size < ?BUFF_SIZE ->
			     Converter!{buffer,accept};
			true -> ok
		     end,
		     buffer_loop(Rest,New_Size,Acc+Length,Printer,Converter)
	     end;
	{converter,file} ->
	    Converter!{buffer,Buffer},
	    self()!{raw,[to_file],1},
	    buffer_loop([],0,Acc,Printer,Converter)
    end.