581a8818b5f1672a6a5047e9ea1d0e2d87bf2c02
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/>.
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);
30 procedure cbufNext (var pos
: LongWord);
32 function cbufAt (const pos
: LongWord): Char;
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 ();
45 // ////////////////////////////////////////////////////////////////////////// //
46 //const ConBufSize = 64;
47 const ConBufSize
= 256*1024;
49 // each line in buffer ends with '\n'; we don't keep offsets or lengthes, as
50 // it's fairly easy to search in buffer, and drawing console is not a common
51 // thing, so it doesn't have to be superfast.
53 cbuf
: packed array [0..ConBufSize
-1] of Char;
54 cbufhead
: LongWord = 0;
55 cbuftail
: LongWord = 0; // `cbuftail` points *at* last char
56 changeCount
: LongWord = 1;
59 function cbufLastChange (): LongWord; begin result
:= changeCount
; end;
62 // ////////////////////////////////////////////////////////////////////////// //
63 procedure cbufPutChars (buf
: PChar; count
: Integer);
71 if changeCount
= 0 then changeCount
:= 1;
77 np
:= (cbuftail
+1) mod ConBufSize
;
80 // we have to make some room; delete top line for this
83 och
:= cbuf
[cbufhead
];
84 cbufhead
:= (cbufhead
+1) mod ConBufSize
;
85 if (cbufhead
= np
) or (och
= #10) then break
;
95 procedure cbufPut (const s
: AnsiString);
97 if length(s
) > 0 then cbufPutChars(@s
[1], length(s
));
101 // ////////////////////////////////////////////////////////////////////////// //
102 // warning! don't modify conbuf while the range is active!
103 function cbufWalkStart (): LongWord; begin result
:= cbuftail
; end;
104 function cbufWalkEnd (pos
: LongWord): LongWord; begin result
:= cbufhead
; end;
105 procedure cbufPrev (var pos
: LongWord); begin pos
:= (pos
+ConBufSize
-1) mod ConBufSize
; end;
106 procedure cbufNext (var pos
: LongWord); begin pos
:= (pos
+1) mod ConBufSize
; end;
108 function cbufAt (const pos
: LongWord): Char; begin result
:= cbuf
[pos
mod ConBufSize
]; end;
111 // ////////////////////////////////////////////////////////////////////////// //
112 procedure cbufLastLine (var sp
: LongWord; var ep
: LongWord);
116 if cbufhead
= cbuftail
then
124 while pos
<> cbufhead
do
126 pp
:= (pos
+ConBufSize
-1) mod ConBufSize
;
127 if cbuf
[pp
] = #10 then break
;
134 function cbufLineUp (var sp
: LongWord; var ep
: LongWord): Boolean;
138 if sp
= cbufhead
then begin sp
:= cbufhead
; ep
:= cbufhead
+1; result
:= false; exit
; end;
139 pos
:= (sp
+ConBufSize
-1) mod ConBufSize
;
140 if (pos
= cbufhead
) or (cbuf
[pos
] <> #10) then begin sp
:= cbufhead
; ep
:= cbufhead
+1; result
:= false; exit
; end;
142 while pos
<> cbufhead
do
144 pp
:= (pos
+ConBufSize
-1) mod ConBufSize
;
145 if cbuf
[pp
] = #10 then break
;
153 procedure cbufClear ();
159 if changeCount
= 0 then changeCount
:= 1;