1 (* Copyright (C) DooM 2D:Forever Developers
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, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE a_modes.inc}
23 procedure cbufPut (const s
: AnsiString);
24 procedure cbufPutChars (buf
: PChar; count
: Integer);
26 function cbufLastChange (): LongWord;
28 function cbufWalkStart (): LongWord;
29 function cbufWalkEnd (pos
: LongWord): LongWord;
30 procedure cbufPrev (var pos
: LongWord); inline;
31 procedure cbufNext (var pos
: LongWord); inline;
33 function cbufAt (const pos
: LongWord): Char; inline;
36 procedure cbufLastLine (var sp
: LongWord; var ep
: LongWord);
37 // move one line up; `sp` and `ep` MUST be valid values from previous call to `cbufLastLine()`
38 function cbufLineUp (var sp
: LongWord; var ep
: LongWord): Boolean;
40 procedure cbufClear ();
43 conbufDumpToStdOut
: Boolean = false;
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.
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 // ////////////////////////////////////////////////////////////////////////// //
68 needCon
: Boolean = true;
70 procedure cbufPutChars (buf
: PChar; count
: Integer);
77 if conbufDumpToStdOut
then
79 for np
:= 0 to count
-1 do
81 if needCon
then begin write(stdout
, 'CON: '); needCon
:= false; end;
82 write(stdout
, buf
[np
]);
83 needCon
:= (buf
[np
] = #10);
87 if changeCount
= 0 then changeCount
:= 1;
93 np
:= (cbuftail
+1) mod ConBufSize
;
96 // we have to make some room; delete top line for this
99 och
:= cbuf
[cbufhead
];
100 cbufhead
:= (cbufhead
+1) mod ConBufSize
;
101 if (cbufhead
= np
) or (och
= #10) then break
;
111 procedure cbufPut (const s
: AnsiString);
113 if length(s
) > 0 then cbufPutChars(@s
[1], length(s
));
117 // ////////////////////////////////////////////////////////////////////////// //
118 // warning! don't modify conbuf while the range is active!
119 function cbufWalkStart (): LongWord; begin result
:= cbuftail
; end;
120 function cbufWalkEnd (pos
: LongWord): LongWord; begin result
:= cbufhead
; end;
121 procedure cbufPrev (var pos
: LongWord); inline; begin pos
:= (pos
+ConBufSize
-1) mod ConBufSize
; end;
122 procedure cbufNext (var pos
: LongWord); inline; begin pos
:= (pos
+1) mod ConBufSize
; end;
124 function cbufAt (const pos
: LongWord): Char; inline; begin result
:= cbuf
[pos
mod ConBufSize
]; end;
127 // ////////////////////////////////////////////////////////////////////////// //
128 procedure cbufLastLine (var sp
: LongWord; var ep
: LongWord);
132 if cbufhead
= cbuftail
then
140 while pos
<> cbufhead
do
142 pp
:= (pos
+ConBufSize
-1) mod ConBufSize
;
143 if cbuf
[pp
] = #10 then break
;
150 function cbufLineUp (var sp
: LongWord; var ep
: LongWord): Boolean;
154 if sp
= cbufhead
then begin sp
:= cbufhead
; ep
:= cbufhead
+1; result
:= false; exit
; end;
155 pos
:= (sp
+ConBufSize
-1) mod ConBufSize
;
156 if (pos
= cbufhead
) or (cbuf
[pos
] <> #10) then begin sp
:= cbufhead
; ep
:= cbufhead
+1; result
:= false; exit
; end;
158 while pos
<> cbufhead
do
160 pp
:= (pos
+ConBufSize
-1) mod ConBufSize
;
161 if cbuf
[pp
] = #10 then break
;
169 procedure cbufClear ();
175 if changeCount
= 0 then changeCount
:= 1;