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, version 3 of the License ONLY.
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.
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/>.
15 {$INCLUDE a_modes.inc}
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;
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 ();
42 conbufDumpToStdOut
: Boolean = false;
43 conbufConPrefix
: Boolean = true;
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
83 if conbufConPrefix
then write(stdout
, 'CON: ');
86 write(stdout
, buf
[np
]);
87 needCon
:= (buf
[np
] = #10);
91 if changeCount
= 0 then changeCount
:= 1;
97 np
:= (cbuftail
+1) mod ConBufSize
;
100 // we have to make some room; delete top line for this
103 och
:= cbuf
[cbufhead
];
104 cbufhead
:= (cbufhead
+1) mod ConBufSize
;
105 if (cbufhead
= np
) or (och
= #10) then break
;
115 procedure cbufPut (const s
: AnsiString);
117 if length(s
) > 0 then cbufPutChars(@s
[1], length(s
));
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);
136 if cbufhead
= cbuftail
then
144 while pos
<> cbufhead
do
146 pp
:= (pos
+ConBufSize
-1) mod ConBufSize
;
147 if cbuf
[pp
] = #10 then break
;
154 function cbufLineUp (var sp
: LongWord; var ep
: LongWord): Boolean;
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;
162 while pos
<> cbufhead
do
164 pp
:= (pos
+ConBufSize
-1) mod ConBufSize
;
165 if cbuf
[pp
] = #10 then break
;
173 procedure cbufClear ();
179 if changeCount
= 0 then changeCount
:= 1;