DEADSOFTWARE

changed license to GPLv3 only; sorry, no trust to FSF anymore
[d2df-sdl.git] / src / shared / conbuf.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE a_modes.inc}
16 {.$MODE OBJFPC}
17 unit conbuf;
19 interface
22 procedure cbufPut (const s: AnsiString);
23 procedure cbufPutChars (buf: PChar; count: Integer);
25 function cbufLastChange (): LongWord;
27 function cbufWalkStart (): LongWord;
28 function cbufWalkEnd (pos: LongWord): LongWord;
29 procedure cbufPrev (var pos: LongWord); inline;
30 procedure cbufNext (var pos: LongWord); inline;
32 function cbufAt (const pos: LongWord): Char; inline;
34 // get last line
35 procedure cbufLastLine (var sp: LongWord; var ep: LongWord);
36 // move one line up; `sp` and `ep` MUST be valid values from previous call to `cbufLastLine()`
37 function cbufLineUp (var sp: LongWord; var ep: LongWord): Boolean;
39 procedure cbufClear ();
41 var
42 conbufDumpToStdOut: Boolean = false;
43 conbufConPrefix: Boolean = true;
46 implementation
49 // ////////////////////////////////////////////////////////////////////////// //
50 //const ConBufSize = 64;
51 const ConBufSize = 256*1024;
53 // each line in buffer ends with '\n'; we don't keep offsets or lengthes, as
54 // it's fairly easy to search in buffer, and drawing console is not a common
55 // thing, so it doesn't have to be superfast.
56 var
57 cbuf: packed array [0..ConBufSize-1] of Char;
58 cbufhead: LongWord = 0;
59 cbuftail: LongWord = 0; // `cbuftail` points *at* last char
60 changeCount: LongWord = 1;
63 function cbufLastChange (): LongWord; begin result := changeCount; end;
66 // ////////////////////////////////////////////////////////////////////////// //
67 var
68 needCon: Boolean = true;
70 procedure cbufPutChars (buf: PChar; count: Integer);
71 var
72 np: LongWord;
73 ch, och: Char;
74 begin
75 if count > 0 then
76 begin
77 if conbufDumpToStdOut then
78 begin
79 for np := 0 to count-1 do
80 begin
81 if needCon then
82 begin
83 if conbufConPrefix then write(stdout, 'CON: ');
84 needCon := false;
85 end;
86 write(stdout, buf[np]);
87 needCon := (buf[np] = #10);
88 end;
89 end;
90 Inc(changeCount);
91 if changeCount = 0 then changeCount := 1;
92 while count > 0 do
93 begin
94 Dec(count);
95 ch := buf^;
96 Inc(buf);
97 np := (cbuftail+1) mod ConBufSize;
98 if np = cbufhead then
99 begin
100 // we have to make some room; delete top line for this
101 while true do
102 begin
103 och := cbuf[cbufhead];
104 cbufhead := (cbufhead+1) mod ConBufSize;
105 if (cbufhead = np) or (och = #10) then break;
106 end;
107 end;
108 cbuf[np] := ch;
109 cbuftail := np;
110 end;
111 end;
112 end;
115 procedure cbufPut (const s: AnsiString);
116 begin
117 if length(s) > 0 then cbufPutChars(@s[1], length(s));
118 end;
121 // ////////////////////////////////////////////////////////////////////////// //
122 // warning! don't modify conbuf while the range is active!
123 function cbufWalkStart (): LongWord; begin result := cbuftail; end;
124 function cbufWalkEnd (pos: LongWord): LongWord; begin result := cbufhead; end;
125 procedure cbufPrev (var pos: LongWord); inline; begin pos := (pos+ConBufSize-1) mod ConBufSize; end;
126 procedure cbufNext (var pos: LongWord); inline; begin pos := (pos+1) mod ConBufSize; end;
128 function cbufAt (const pos: LongWord): Char; inline; begin result := cbuf[pos mod ConBufSize]; end;
131 // ////////////////////////////////////////////////////////////////////////// //
132 procedure cbufLastLine (var sp: LongWord; var ep: LongWord);
133 var
134 pos, pp: LongWord;
135 begin
136 if cbufhead = cbuftail then
137 begin
138 sp := cbufhead;
139 ep := cbufhead+1;
140 exit;
141 end;
142 pos := cbuftail;
143 ep := pos;
144 while pos <> cbufhead do
145 begin
146 pp := (pos+ConBufSize-1) mod ConBufSize;
147 if cbuf[pp] = #10 then break;
148 pos := pp;
149 end;
150 sp := pos;
151 end;
154 function cbufLineUp (var sp: LongWord; var ep: LongWord): Boolean;
155 var
156 pos, pp: LongWord;
157 begin
158 if sp = cbufhead then begin sp := cbufhead; ep := cbufhead+1; result := false; exit; end;
159 pos := (sp+ConBufSize-1) mod ConBufSize;
160 if (pos = cbufhead) or (cbuf[pos] <> #10) then begin sp := cbufhead; ep := cbufhead+1; result := false; exit; end;
161 ep := pos;
162 while pos <> cbufhead do
163 begin
164 pp := (pos+ConBufSize-1) mod ConBufSize;
165 if cbuf[pp] = #10 then break;
166 pos := pp;
167 end;
168 sp := pos;
169 result := true;
170 end;
173 procedure cbufClear ();
174 begin
175 cbuf[0] := #10;
176 cbufhead := 0;
177 cbuftail := 0;
178 Inc(changeCount);
179 if changeCount = 0 then changeCount := 1;
180 end;
183 begin
184 cbuf[0] := #10;
185 end.