]> git.saurik.com Git - wxWidgets.git/blob - samples/richedit/kbList.cpp
made Shift-JIS encoding upper case; made SJIS the canonical name for it (just because...
[wxWidgets.git] / samples / richedit / kbList.cpp
1 /*-*- c++ -*-********************************************************
2 * kbList.cc : a double linked list *
3 * *
4 * (C) 1998 by Karsten Ballüder (Ballueder@usa.net) *
5 * *
6 * $Id$ *
7 * *
8 * $Log$
9 * Revision 1.5 2005/09/23 12:52:18 MR
10 * Nuke #pragma implementation/interface's
11 *
12 * Revision 1.4 2005/05/31 09:19:38 JS
13 * Typo correction patch [ 1208110 ] Lots of typo corrections
14 * Olly Betts
15 *
16 * Revision 1.3 2004/08/06 17:27:18 ABX
17 * Deleting void is undefined.
18 *
19 * Revision 1.2 2002/01/16 13:39:50 GT
20 * Added checks for wxUSE_IOSTREAMH to determine which iostream(.h) to use
21 *
22 * Revision 1.1 1999/06/07 09:57:12 KB
23 * Formerly known as wxLayout.
24 *
25 * Revision 1.3 1998/11/19 20:34:50 KB
26 * fixes
27 *
28 * Revision 1.8 1998/09/23 08:57:27 KB
29 * changed deletion behaviour
30 *
31 * Revision 1.7 1998/08/16 21:21:29 VZ
32 *
33 * 1) fixed config file bug: it was never created (attempt to create ~/.M/config
34 * always failed, must mkdir("~/.M") first)
35 * 2) "redesign" of "Folder properties" dialog and bug corrected, small change to
36 * MInputBox (it was too wide)
37 * 3) bug in ProvFC when it didn't recognize the books as being in the correct
38 * format (=> messages "can't reopen book") corrected
39 * 4) I tried to enhance MDialog_About(), but it didn't really work... oh well,
40 * I've never said I was an artist
41 *
42 * Revision 1.6 1998/07/08 11:56:56 KB
43 * M compiles and runs on Solaris 2.5/gcc 2.8/c-client gso
44 *
45 * Revision 1.5 1998/06/27 20:07:18 KB
46 * several bug fixes for kbList
47 * started adding my layout stuff
48 *
49 * Revision 1.1.1.1 1998/06/13 21:51:12 karsten
50 * initial code
51 *
52 * Revision 1.4 1998/05/24 14:48:00 KB
53 * lots of progress on Python, but cannot call functions yet
54 * kbList fixes again?
55 *
56 * Revision 1.3 1998/05/18 17:48:34 KB
57 * more list<>->kbList changes, fixes for wxXt, improved makefiles
58 *
59 * Revision 1.2 1998/05/14 16:39:31 VZ
60 *
61 * fixed SIGSEGV in ~kbList if the list is empty
62 *
63 * Revision 1.1 1998/05/13 19:02:11 KB
64 * added kbList, adapted MimeTypes for it, more python, new icons
65 *
66 *******************************************************************/
67
68 #include "wx/wxprec.h"
69
70 #ifdef __BORLANDC__
71 # pragma hdrstop
72 #endif
73
74 #include "wx/wx.h"
75
76 #include "kbList.h"
77
78
79 kbListNode::kbListNode( void *ielement,
80 kbListNode *iprev,
81 kbListNode *inext)
82 {
83 next = inext;
84 prev = iprev;
85 if(prev)
86 prev->next = this;
87 if(next)
88 next->prev = this;
89 element = ielement;
90 }
91
92 kbListNode::~kbListNode()
93 {
94 if(prev)
95 prev->next = next;
96 if(next)
97 next->prev = prev;
98 }
99
100
101 kbList::iterator::iterator(kbListNode *n)
102 {
103 node = n;
104 }
105
106 void *
107 kbList::iterator::operator*()
108 {
109 return node->element;
110 }
111
112 kbList::iterator &
113 kbList::iterator::operator++()
114 {
115 node = node ? node->next : NULL;
116 return *this;
117 }
118
119 kbList::iterator &
120 kbList::iterator::operator--()
121 {
122 node = node ? node->prev : NULL;
123 return *this;
124 }
125 kbList::iterator &
126 kbList::iterator::operator++(int /* foo */)
127 {
128 return operator++();
129 }
130
131 kbList::iterator &
132 kbList::iterator::operator--(int /* bar */)
133 {
134 return operator--();
135 }
136
137
138 bool
139 kbList::iterator::operator !=(kbList::iterator const & i) const
140 {
141 return node != i.node;
142 }
143
144 bool
145 kbList::iterator::operator ==(kbList::iterator const & i) const
146 {
147 return node == i.node;
148 }
149
150 kbList::kbList(bool ownsEntriesFlag)
151 {
152 first = NULL;
153 last = NULL;
154 ownsEntries = ownsEntriesFlag;
155 }
156
157 void
158 kbList::push_back(void *element)
159 {
160 if(! first) // special case of empty list
161 {
162 first = new kbListNode(element);
163 last = first;
164 return;
165 }
166 else
167 last = new kbListNode(element, last);
168 }
169
170 void
171 kbList::push_front(void *element)
172 {
173 if(! first) // special case of empty list
174 {
175 push_back(element);
176 return;
177 }
178 else
179 first = new kbListNode(element, NULL, first);
180 }
181
182 void *
183 kbList::pop_back(void)
184 {
185 iterator i;
186 void *data;
187 bool ownsFlagBak = ownsEntries;
188 i = tail();
189 data = *i;
190 ownsEntries = false;
191 erase(i);
192 ownsEntries = ownsFlagBak;
193 return data;
194 }
195
196 void *
197 kbList::pop_front(void)
198 {
199 iterator i;
200 void *data;
201 bool ownsFlagBak = ownsEntries;
202
203 i = begin();
204 data = *i;
205 ownsEntries = false;
206 erase(i);
207 ownsEntries = ownsFlagBak;
208 return data;
209
210 }
211
212 void
213 kbList::insert(kbList::iterator & i, void *element)
214 {
215 if(! i.Node())
216 return;
217 else if(i.Node() == first)
218 {
219 push_front(element);
220 i = first;
221 return;
222 }
223 i = kbList::iterator(new kbListNode(element, i.Node()->prev, i.Node()));
224 }
225
226 void
227 kbList::doErase(kbList::iterator & i)
228 {
229 kbListNode
230 *node = i.Node(),
231 *prev, *next;
232
233 if(! node) // illegal iterator
234 return;
235
236 prev = node->prev;
237 next = node->next;
238
239 // correct first/last:
240 if(node == first)
241 first = node->next;
242 if(node == last) // don't put else here!
243 last = node->prev;
244
245 // build new links:
246 if(prev)
247 prev->next = next;
248 if(next)
249 next->prev = prev;
250
251 // delete this node and contents:
252 // now done separately
253 //if(ownsEntries)
254 //delete *i;
255 delete i.Node();
256
257 // change the iterator to next element:
258 i = kbList::iterator(next);
259 }
260
261 kbList::~kbList()
262 {
263 kbListNode *next;
264
265 while ( first != NULL )
266 {
267 next = first->next;
268 if(ownsEntries)
269 {
270 #if 0
271 delete first->element;
272 #else
273 wxLogError(wxT("Deleting `void*' is undefined."));
274 wxLogError(wxT("Entries of kbList should be deleted by destructors of derived classes."));
275 #endif
276 }
277 delete first;
278 first = next;
279 }
280 }
281
282 kbList::iterator
283 kbList::begin(void) const
284 {
285 return kbList::iterator(first);
286 }
287
288 kbList::iterator
289 kbList::tail(void) const
290 {
291 return kbList::iterator(last);
292 }
293
294 kbList::iterator
295 kbList::end(void) const
296 {
297 return kbList::iterator(NULL); // the one after the last
298 }
299
300 unsigned
301 kbList::size(void) const // inefficient
302 {
303 unsigned count = 0;
304 kbList::iterator i;
305 for(i = begin(); i != end(); i++, count++)
306 ;
307 return count;
308 }
309
310
311
312
313
314
315
316 #ifdef KBLIST_TEST
317
318 #if wxUSE_IOSTREAMH
319 #include <iostream.h>
320 #else
321 #include <iostream>
322 #endif
323
324 KBLIST_DEFINE(kbListInt,int);
325
326 int main(void)
327 {
328 int
329 n, *ptr;
330 kbListInt
331 l;
332 kbListInt::iterator
333 i;
334
335 for(n = 0; n < 10; n++)
336 {
337 ptr = new int;
338 *ptr = n*n;
339 l.push_back(ptr);
340 }
341
342 i = l.begin(); // first element
343 i++; // 2nd
344 i++; // 3rd
345 i++; // 4th, insert here:
346 ptr = new int;
347 *ptr = 4444;
348 l.insert(i,ptr);
349
350 // this cannot work, because l.end() returns NULL:
351 i = l.end(); // behind last
352 i--; // still behind last
353 l.erase(i); // doesn't do anything
354
355 // this works:
356 i = l.tail(); // last element
357 i--;
358 --i;
359 l.erase(i); // erase 3rd last element (49)
360
361 for(i = l.begin(); i != l.end(); i++)
362 cout << *i << '\t' << *((int *)*i) << endl;
363
364
365 return 0;
366 }
367 #endif