57ed64431cab091c167ee4052c82f239446909ff
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 // ////////////////////////////////////////////////////////////////////////// //
65 needCon
: Boolean = true;
67 procedure cbufPutChars (buf
: PChar; count
: Integer);
75 //write(stderr, 'CON: ');
76 for np
:= 0 to count
-1 do
78 if needCon
then begin write(stdout
, 'CON: '); needCon
:= false; end;
79 write(stdout
, buf
[np
]);
80 needCon
:= (buf
[np
] = #10);
84 if changeCount
= 0 then changeCount
:= 1;
90 np
:= (cbuftail
+1) mod ConBufSize
;
93 // we have to make some room; delete top line for this
96 och
:= cbuf
[cbufhead
];
97 cbufhead
:= (cbufhead
+1) mod ConBufSize
;
98 if (cbufhead
= np
) or (och
= #10) then break
;
108 procedure cbufPut (const s
: AnsiString);
110 if length(s
) > 0 then cbufPutChars(@s
[1], length(s
));
114 // ////////////////////////////////////////////////////////////////////////// //
115 // warning! don't modify conbuf while the range is active!
116 function cbufWalkStart (): LongWord; begin result
:= cbuftail
; end;
117 function cbufWalkEnd (pos
: LongWord): LongWord; begin result
:= cbufhead
; end;
118 procedure cbufPrev (var pos
: LongWord); begin pos
:= (pos
+ConBufSize
-1) mod ConBufSize
; end;
119 procedure cbufNext (var pos
: LongWord); begin pos
:= (pos
+1) mod ConBufSize
; end;
121 function cbufAt (const pos
: LongWord): Char; begin result
:= cbuf
[pos
mod ConBufSize
]; end;
124 // ////////////////////////////////////////////////////////////////////////// //
125 procedure cbufLastLine (var sp
: LongWord; var ep
: LongWord);
129 if cbufhead
= cbuftail
then
137 while pos
<> cbufhead
do
139 pp
:= (pos
+ConBufSize
-1) mod ConBufSize
;
140 if cbuf
[pp
] = #10 then break
;
147 function cbufLineUp (var sp
: LongWord; var ep
: LongWord): Boolean;
151 if sp
= cbufhead
then begin sp
:= cbufhead
; ep
:= cbufhead
+1; result
:= false; exit
; end;
152 pos
:= (sp
+ConBufSize
-1) mod ConBufSize
;
153 if (pos
= cbufhead
) or (cbuf
[pos
] <> #10) then begin sp
:= cbufhead
; ep
:= cbufhead
+1; result
:= false; exit
; end;
155 while pos
<> cbufhead
do
157 pp
:= (pos
+ConBufSize
-1) mod ConBufSize
;
158 if cbuf
[pp
] = #10 then break
;
166 procedure cbufClear ();
172 if changeCount
= 0 then changeCount
:= 1;