DEADSOFTWARE

"--log" cli arg; fixed bug with console output (endless loop)
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
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.
12 *
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/>.
15 *)
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);
30 procedure cbufNext (var pos: LongWord);
32 function cbufAt (const pos: LongWord): Char;
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;
45 implementation
48 // ////////////////////////////////////////////////////////////////////////// //
49 //const ConBufSize = 64;
50 const ConBufSize = 256*1024;
52 // each line in buffer ends with '\n'; we don't keep offsets or lengthes, as
53 // it's fairly easy to search in buffer, and drawing console is not a common
54 // thing, so it doesn't have to be superfast.
55 var
56 cbuf: packed array [0..ConBufSize-1] of Char;
57 cbufhead: LongWord = 0;
58 cbuftail: LongWord = 0; // `cbuftail` points *at* last char
59 changeCount: LongWord = 1;
62 function cbufLastChange (): LongWord; begin result := changeCount; end;
65 // ////////////////////////////////////////////////////////////////////////// //
66 var
67 needCon: Boolean = true;
69 procedure cbufPutChars (buf: PChar; count: Integer);
70 var
71 np: LongWord;
72 ch, och: Char;
73 begin
74 if count > 0 then
75 begin
76 if conbufDumpToStdOut then
77 begin
78 for np := 0 to count-1 do
79 begin
80 if needCon then begin write(stdout, 'CON: '); needCon := false; end;
81 write(stdout, buf[np]);
82 needCon := (buf[np] = #10);
83 end;
84 end;
85 Inc(changeCount);
86 if changeCount = 0 then changeCount := 1;
87 while count > 0 do
88 begin
89 Dec(count);
90 ch := buf^;
91 Inc(buf);
92 np := (cbuftail+1) mod ConBufSize;
93 if np = cbufhead then
94 begin
95 // we have to make some room; delete top line for this
96 while true do
97 begin
98 och := cbuf[cbufhead];
99 cbufhead := (cbufhead+1) mod ConBufSize;
100 if (cbufhead = np) or (och = #10) then break;
101 end;
102 end;
103 cbuf[np] := ch;
104 cbuftail := np;
105 end;
106 end;
107 end;
110 procedure cbufPut (const s: AnsiString);
111 begin
112 if length(s) > 0 then cbufPutChars(@s[1], length(s));
113 end;
116 // ////////////////////////////////////////////////////////////////////////// //
117 // warning! don't modify conbuf while the range is active!
118 function cbufWalkStart (): LongWord; begin result := cbuftail; end;
119 function cbufWalkEnd (pos: LongWord): LongWord; begin result := cbufhead; end;
120 procedure cbufPrev (var pos: LongWord); begin pos := (pos+ConBufSize-1) mod ConBufSize; end;
121 procedure cbufNext (var pos: LongWord); begin pos := (pos+1) mod ConBufSize; end;
123 function cbufAt (const pos: LongWord): Char; begin result := cbuf[pos mod ConBufSize]; end;
126 // ////////////////////////////////////////////////////////////////////////// //
127 procedure cbufLastLine (var sp: LongWord; var ep: LongWord);
128 var
129 pos, pp: LongWord;
130 begin
131 if cbufhead = cbuftail then
132 begin
133 sp := cbufhead;
134 ep := cbufhead+1;
135 exit;
136 end;
137 pos := cbuftail;
138 ep := pos;
139 while pos <> cbufhead do
140 begin
141 pp := (pos+ConBufSize-1) mod ConBufSize;
142 if cbuf[pp] = #10 then break;
143 pos := pp;
144 end;
145 sp := pos;
146 end;
149 function cbufLineUp (var sp: LongWord; var ep: LongWord): Boolean;
150 var
151 pos, pp: LongWord;
152 begin
153 if sp = cbufhead then begin sp := cbufhead; ep := cbufhead+1; result := false; exit; end;
154 pos := (sp+ConBufSize-1) mod ConBufSize;
155 if (pos = cbufhead) or (cbuf[pos] <> #10) then begin sp := cbufhead; ep := cbufhead+1; result := false; exit; end;
156 ep := pos;
157 while pos <> cbufhead do
158 begin
159 pp := (pos+ConBufSize-1) mod ConBufSize;
160 if cbuf[pp] = #10 then break;
161 pos := pp;
162 end;
163 sp := pos;
164 result := true;
165 end;
168 procedure cbufClear ();
169 begin
170 cbuf[0] := #10;
171 cbufhead := 0;
172 cbuftail := 0;
173 Inc(changeCount);
174 if changeCount = 0 then changeCount := 1;
175 end;
178 begin
179 cbuf[0] := #10;
180 end.