2121#include < string.h>
2222#endif // __STRING_H
2323
24+ #include < malloc.h>
25+
2426TTextDevice::TTextDevice ( const TRect& bounds,
2527 TScrollBar *aHScrollBar,
2628 TScrollBar *aVScrollBar,
@@ -120,12 +122,37 @@ Boolean TTerminal::canInsert( ushort amount )
120122 return Boolean ( queBack > T );
121123}
122124
125+ #ifdef __FLAT__
126+
127+ #define DRAW_DYNAMIC_STR 1
128+ #define resizeStr (_len ) \
129+ slen = _len; \
130+ if (slen > scap) \
131+ s = (char *) ::realloc(s, (scap = max(slen, 2 *scap)));
132+
133+ #else
134+
135+ #define DRAW_DYNAMIC_STR 0
136+ #define resizeStr (_len ) \
137+ slen = _len;
138+
139+ #endif // __FLAT__
140+
123141void TTerminal::draw ()
124142{
143+ #if DRAW_DYNAMIC_STR
144+ size_t scap = 256 ;
145+ char *s = (char *) ::malloc (scap);
146+ #else
147+ char s[256 ];
148+ #endif
149+ size_t slen = 0 ;
150+ TScreenCell *_b = (TScreenCell*) alloca (size.x *sizeof (TScreenCell));
151+ TSpan<TScreenCell> b (_b, size.x );
125152 short i;
126153 ushort begLine, endLine;
127- char s[256 ];
128154 ushort bottomLine;
155+ uchar color = mapColor (1 );
129156
130157 bottomLine = size.y + delta.y ;
131158 if ( limit.y > bottomLine )
@@ -151,29 +178,34 @@ void TTerminal::draw()
151178 if (endLine >= begLine)
152179 {
153180 int T = int ( endLine - begLine );
181+ resizeStr (T);
154182 memcpy ( s, &buffer[begLine], T );
155- s[T] = EOS;
156183 }
157184 else
158185 {
159186 int T = int ( bufSize - begLine);
187+ resizeStr (T + endLine);
160188 memcpy ( s, &buffer[begLine], T );
161189 memcpy ( s+T, buffer, endLine );
162- s[T+endLine] = EOS;
163190 }
164- int l = strlen (s);
165- if ( delta.x >= l )
166- *s = EOS;
167- else
168- memmove ( s, &s[delta.x ], l );
169191
170- writeStr ( 0 , i, s, 1 );
171- writeChar ( strlen (s), i, ' ' , 1 , size.x );
192+ {
193+ int x = TText::fill (b, size.x , TStringView (s, slen), color);
194+ while (x < size.x )
195+ ::setCell (b[x++], ' ' , color);
196+ }
197+ writeBuf ( 0 , i, size.x , 1 , b.data () );
172198 endLine = begLine;
173199 bufDec ( endLine );
174200 }
201+ #if DRAW_DYNAMIC_STR
202+ ::free (s);
203+ #endif
175204}
176205
206+ #undef DRAW_DYNAMIC_STR
207+ #undef resizeStr
208+
177209ushort TTerminal::nextLine ( ushort pos )
178210{
179211 if ( pos != queFront )
0 commit comments