Line data Source code
1 : ///////////////////////////////////////////////////////////////////////////
2 : //
3 : // FILE: textbuf.cpp
4 : // TextBuffer class methods
5 : //
6 : // Part of: Scid (Shane's Chess Information Database)
7 : // Version: 2.7
8 : //
9 : // Notice: Copyright (c) 1999-2001 Shane Hudson. All rights reserved.
10 : //
11 : // Author: Shane Hudson (sgh@users.sourceforge.net)
12 : //
13 : ///////////////////////////////////////////////////////////////////////////
14 :
15 : #include "textbuf.h"
16 : #include "misc.h"
17 :
18 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 : //### TextBuffer::Init(): Initialise the textbuffer.
20 : void
21 1 : TextBuffer::Init (void)
22 : {
23 1 : BufferSize = Column = IndentColumn = LineCount = ByteCount = 0;
24 1 : LineIsEmpty = 1;
25 1 : Buffer = Current = NULL;
26 1 : WrapColumn = 80;
27 1 : ConvertNewlines = true;
28 1 : HasTranslations = false;
29 1 : PausedTranslations = false;
30 1 : }
31 :
32 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
33 : //### TextBuffer::Free(): Free the TextBuffer.
34 : void
35 1 : TextBuffer::Free (void)
36 : {
37 1 : if (Buffer != NULL) {
38 : #ifdef WINCE
39 : my_Tcl_Free( Buffer);
40 : #else
41 1 : delete[] Buffer;
42 : #endif
43 1 : Buffer = NULL;
44 1 : BufferSize = 0;
45 : }
46 1 : }
47 :
48 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
49 : //### TextBuffer::Empty(): Empty the TextBuffer.
50 : void
51 2037 : TextBuffer::Empty (void)
52 : {
53 2037 : ASSERT(Buffer != NULL);
54 2037 : ByteCount = Column = LineCount = 0; LineIsEmpty = 1;
55 2037 : Current = Buffer;
56 2037 : *Current = 0;
57 2037 : ConvertNewlines = true;
58 2037 : HasTranslations = false;
59 2037 : }
60 :
61 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62 : // TextBuffer::AddTranslation():
63 : // Adds a translation for a character.
64 : // The translation string will be printed in place of that character.
65 : void
66 0 : TextBuffer::AddTranslation (char ch, const char * str)
67 : {
68 0 : if (! HasTranslations) {
69 0 : HasTranslations = true;
70 0 : for (uint i=0; i < 256; i++) {
71 0 : Translation [i] = NULL;
72 : }
73 : }
74 0 : Translation [(byte) ch] = str;
75 0 : }
76 :
77 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78 : //### TextBuffer::SetBufferSize(): Set the buffer size.
79 : void
80 1 : TextBuffer::SetBufferSize (uint length)
81 : {
82 : #ifdef WINCE
83 : if (Buffer != NULL) { my_Tcl_Free( Buffer); }
84 : Buffer = my_Tcl_Alloc(sizeof(char[length]));
85 : #else
86 1 : if (Buffer != NULL) { delete[] Buffer; }
87 1 : Buffer = new char[length];
88 : #endif
89 1 : BufferSize = length;
90 1 : ByteCount = Column = LineCount = 0; LineIsEmpty = 1;
91 1 : Current = Buffer;
92 1 : *Current = 0;
93 1 : }
94 :
95 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96 : //### TextBuffer::NewLine(): Add a newline.
97 : errorT
98 492711 : TextBuffer::NewLine ()
99 : {
100 492711 : ASSERT (Current != NULL);
101 492711 : if (ByteCount >= BufferSize) { return ERROR_BufferFull; }
102 492711 : *Current++ = '\n';
103 492711 : LineCount++; ByteCount++; LineIsEmpty = 1;
104 492711 : Column = 0;
105 492711 : while (Column < IndentColumn) {
106 0 : if (ByteCount >= BufferSize) { return ERROR_BufferFull; }
107 0 : *Current++ = ' '; Column++; ByteCount++;
108 : }
109 492711 : *Current = 0;
110 492711 : return OK;
111 : }
112 :
113 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
114 : //### TextBuffer::Indent(): Indent to the current Indentation level..
115 : errorT
116 0 : TextBuffer::Indent ()
117 : {
118 0 : ASSERT (Current != NULL);
119 0 : if (!LineIsEmpty) {
120 0 : return NewLine();
121 : } else {
122 0 : while (Column < IndentColumn) {
123 0 : if (ByteCount >= BufferSize) { return ERROR_BufferFull; }
124 0 : *Current++ = ' '; Column++; ByteCount++;
125 : }
126 0 : *Current = 0;
127 : }
128 0 : return OK;
129 : }
130 :
131 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
132 : //### TextBuffer::PrintLine(): Print a string then newline. Does not
133 : // check for the line going past WrapColumn.
134 : errorT
135 0 : TextBuffer::PrintLine (const char * str)
136 : {
137 0 : ASSERT(Current != NULL);
138 0 : while (*str != 0) {
139 0 : if (ByteCount > BufferSize) { return ERROR_BufferFull; }
140 0 : AddChar (*str);
141 0 : str++;
142 : }
143 0 : return NewLine();
144 : }
145 :
146 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147 : //### TextBuffer::PrintWord(): Prints a word, wrapping if necessary.
148 : // It does NOT add a space, since that is left to the caller to
149 : // provide in the string.
150 : errorT
151 3435078 : TextBuffer::PrintWord (const char * str)
152 : {
153 3435078 : ASSERT(Current != NULL);
154 3435078 : uint length = strLength (str);
155 3435078 : if (Column + length >= WrapColumn) { NewLine(); }
156 3435078 : if (ByteCount + length >= BufferSize) { return ERROR_BufferFull; }
157 90639324 : while (*str != 0) {
158 43602123 : char ch = *str;
159 43602123 : AddChar (ch);
160 43602123 : str++;
161 43602123 : Column++;
162 : }
163 3435078 : *Current = 0; // add trailing end-of-string to buffer
164 3435078 : LineIsEmpty = 0;
165 3435078 : return OK;
166 : }
167 :
168 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
169 : //### TextBuffer::PrintSpace(): Prints a space OR a newline character,
170 : // but not both.
171 : errorT
172 2429823 : TextBuffer::PrintSpace (void)
173 : {
174 2429823 : if (ByteCount + 1 >= BufferSize) { return ERROR_BufferFull; }
175 2429823 : if (Column + 1 >= WrapColumn) {
176 18919 : NewLine();
177 : } else {
178 2410904 : *Current = ' '; Current++; ByteCount++; Column++; LineIsEmpty = 0;
179 : }
180 2429823 : return OK;
181 : }
182 :
183 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184 : //### TextBuffer::PrintChar(): prints a single char, adding a newline
185 : // first if necessary.
186 : errorT
187 616956 : TextBuffer::PrintChar (char b)
188 : {
189 616956 : if (Column + 1 >= WrapColumn) { NewLine(); }
190 616956 : if (ByteCount + 1 >= BufferSize) { return ERROR_BufferFull; }
191 616956 : AddChar (b);
192 616956 : Column++; LineIsEmpty = 0;
193 616956 : return OK;
194 : }
195 :
196 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
197 : //### TextBuffer::PrintString(): Print a string, wrapping at spaces.
198 : // Also converts newlines in the string into spaces.
199 : errorT
200 731923 : TextBuffer::PrintString (const char * str)
201 : {
202 : errorT err;
203 : char currentWord[1024]; // should be long enough for a word
204 776152 : while (*str != 0) {
205 714783 : char * b = currentWord;
206 714783 : *b = 0;
207 : // get next word and print it:
208 69205257 : while (*str != ' ' && *str != '\n' && *str != '\0') {
209 34245237 : *b = *str; b++; str++;
210 : }
211 : // end of word/line/text reached
212 714783 : *b = 0;
213 714783 : err = PrintWord (currentWord);
214 714783 : if (err != OK) { return err; }
215 714783 : if (*str == 0) { return OK; }
216 44229 : if (*str == '\n' && !ConvertNewlines) {
217 17140 : err = NewLine();
218 : } else {
219 27089 : err = PrintSpace();
220 : }
221 44229 : if (err != OK) { return err; }
222 44229 : str++;
223 : }
224 17140 : return OK;
225 : }
226 :
227 : //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
228 : //### TextBuffer::PrintInt(): Print a decimal number followed by string
229 : // as a word (so it appends a space at the end and wraps if
230 : // necessary).
231 : errorT
232 1155998 : TextBuffer::PrintInt (uint i, const char * str)
233 : {
234 : char temp[255];
235 1155998 : sprintf(temp, "%d%s", i, str);
236 1155998 : return PrintWord(temp);
237 : }
238 :
239 : ///////////////////////////////////////////////////////////////////////////
240 : // EOF: textbuf.cpp
241 : ///////////////////////////////////////////////////////////////////////////
|