Thanks to all re avoiding process dictionary

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Thanks to all re avoiding process dictionary

Donald Steven
Thanks everyone for your help with this.  The following code replaces the ugly use of the process dictionary with a simple process to increment or decrement the buffer index as needed.

%%% --------------------------------------------------------------------------------------------------------------------
%%%
%%% Fun : getc
%%% Purpose : Read or put back the next character from/to the specified InputBuffer and return its integer value or eof
%%% Created : 02 January 2019
%%%
%%% Notes : 1) This block of code includes startiLoop, iLoop, and stopiLoop which handle the Ibuffer index increments
%%%          and decrements.
%%%
%%% --------------------------------------------------------------------------------------------------------------------
startiLoop() -> Pid = spawn(fun() -> iLoop(0) end), Pid. % the pid is saved in the file record (see stddef.hrl)
stopiLoop(Pid) -> Pid ! stopiLoop, ok.                   % not used as yet
iLoop(I) ->
receive
{incI, GetcPid} -> GetcPid ! {self(), I + 1}, iLoop(I + 1);
{decI, GetcPid} -> GetcPid ! {self(), I + 1}, iLoop(I - 1);
stopiLoop -> exit(normal)
end.
getc(IBuffer, Mode) ->
    ILoopPid = IBuffer#file.pid,
    case Mode of
        ?READMODE   ->
            ILoopPid ! {incI, self()},
            receive
        {ILoopPid, I} -> I
            end,
            case I > IBuffer#file.size of
                true -> eof;
                false -> lists:nth(I, IBuffer#file.buffer)
            end;
        ?PUTBAKMODE -> ILoopPid ! {decI, self()};
        _           -> fatalError("getc: illegal mode", "")
    end.                   

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions