summaryrefslogtreecommitdiffstats
path: root/src/modules/objects/class_list.cpp
blob: 2d6c9e841de30a4005bb62f30a48b215b69dc18e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//=============================================================================
//
//   File : class_list.cpp
//   Creation date : Wed Sep 09 2000 21:07:55 by Szymon Stefanek
//
//   This file is part of the KVirc irc client distribution
//   Copyright (C) 1999-2000 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//============================================================================

#include "kvi_debug.h"

#include "kvi_malloc.h"
#include "kvi_locale.h"

#include "class_list.h"

#include <stdlib.h>


/*
	@doc: list
	@keyterms:
		list object class, sorted list
	@title:
		list class
	@type:
		class
	@short:
		Abstracts a double linked list of strings
	@inherits:
		[class]object[/class]
	@description:


		This object class abstracts a double linked list of strings.
		You can insert/remove items by zero based position, at the end (tail) or at the
		beginning (head). The list incorporates an efficient iteration
		method by using the [classfnc:list]$moveFirst[/classfnc](),[classfnc:list]$moveLast[/classfnc](),
		[classfnc:list]$moveNext[/classfnc](),[classfnc:list]$movePrev[/classfnc](),
		[classfnc:list]$current[/classfnc]() and [classfnc:list]$eof[/classfnc]() functions.
	@functions:
		!fn: <integer> $count()
		Returns the number of items in the list

		!fn: <boolean> $isEmpty()
		Returns $true if the list is empty, $false otherwise.

		!fn: $clear()
		Removes all the items from the list.This is the same as
		[classfnc:list]$removeAll[/classfnc]().

		!fn: $removeAll()
		Removes all the items from the list. This is the same as
		[classfnc:list]$clear[/classfnc]().

		!fn: $append(<item:variant>)
		Inserts the <item> at the end (tail) of the list.

		!fn: $prepend(<item:variant>)
		Inserts the <item> at the beginning (head) of the list.

		!fn: $insert(<index:uint>,<item:variant>)
		Inserts the <item> at zero-based position <index> in the list.
		If <index> is greater or equal to [classfnc:list]$count[/classfnc]()
		then the item is simply appended to the end of the list.

		!fn: $add(<index:uint>,<item:variant>)
		This is exactly the same as [classfnc:list]$insert[/classfnc]().

		!fn: <variant> $item(<index:uint>)
		Returns the item at zero-based <index>. If <index> is greater
		or equal to [classfnc:list]$count[/classfnc]() (beyond the end of the list)
		then [fnc]$nothing[/fnc] is returned.

		!fn: <boolean> $remove(<index:uint>)
		Removes the item at zero-based <index>. Returns $true
		if the item was succesfully removed and $false otherwise (i.e. <index>
		pointed beyond the end of the list).

		!fn: <boolean> $removeFirst()
		Removes the first item from the list. Returns $true
		if the item was succesfully removed (the list was not empty)
		and $false otherwise.

		!fn: <boolean> $removeLast()
		Removes the last item from the list. Returns $true
		if the item was succesfully removed (the list was not empty)
		and $false otherwise.

		!fn: <boolean> $removeCurrent()
		Removes the current item from the list. Returns $true
		if the item was succesfully removed or $false otherwise.
		Invalidates any iteration operation.

		!fn: <boolean> $moveFirst()
		Moves the iterator to the first item in the list and returns
		$true if the move was succesfull (i.e., the list is not empty)
		and $false otherwise.

		!fn: <boolean> $moveLast()
		Moves the iterator to the last item in the list and returns
		$true if the move was succesfull (i.e., the list is not empty)
		and $false otherwise.

		!fn: <boolean> $movePrev()
		Moves the iterator to the previous item and returns $true
		if the move was succesfull (i.e., there IS a previous item)
		and $false otherwise.

		!fn: <boolean> $moveNext()
		Moves the iterator to the next item and returns $true
		if the move was succesfull (i.e., there IS a next item)
		and $false otherwise.

		!fn: <boolean> $eof()
		Returns $true if the iterator points to a valid
		item in the list (and thus [classfnc:list]$current[/classfnc]()
		would return a valid value) and $false otherwise.

		!fn: <boolean> $current()
		Returns the item pointed by the current iterator
		or [fnc]$nothing[/fnc] is the iterator is not valid (points
		beyond the end of the list).
*/


KVSO_BEGIN_REGISTERCLASS(KviKvsObject_list,"list","object")
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"count",function_count)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"isEmpty",function_isEmpty)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"clear",function_clear)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"removeAll",function_clear)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"append",function_append)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"prepend",function_prepend)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"insert",function_insert)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"add",function_insert)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"at",function_at)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"item",function_at)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"remove",function_remove)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"removeFirst",function_removeFirst)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"removeLast",function_removeLast)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"removeCurrent",function_removeCurrent)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"moveFirst",function_moveFirst)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"moveNext",function_moveNext)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"movePrev",function_movePrev)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"moveLast",function_moveLast)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"sort",function_sort)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"eof",function_eof)
	KVSO_REGISTER_HANDLER(KviKvsObject_list,"current",function_current)
KVSO_END_REGISTERCLASS(KviKvsObject_list)


KVSO_BEGIN_CONSTRUCTOR(KviKvsObject_list,KviKvsObject)
	m_pDataList = new KviPointerList<KviKvsVariant>;
	m_pDataList->setAutoDelete(true);
KVSO_END_CONSTRUCTOR(KviKvsObject_list)


KVSO_BEGIN_DESTRUCTOR(KviKvsObject_list)
	delete m_pDataList;
	m_pDataList = 0;
KVSO_END_CONSTRUCTOR(KviKvsObject_list)

bool KviKvsObject_list::function_current(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)
	{
		c->returnValue()->setNothing();
		return true;
	}
	KviKvsVariant * v = m_pDataList->current();
	if(v)c->returnValue()->copyFrom(*v);
	else c->returnValue()->setNothing();
	return true;
}

bool KviKvsObject_list::function_eof(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)
	{
		c->returnValue()->setBoolean(true);
		return true;
	}
	c->returnValue()->setBoolean(m_pDataList->current() != 0);
	return true;
}

bool KviKvsObject_list::function_moveLast(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)
	{
		c->returnValue()->setBoolean(false);
		return true;
	}
	c->returnValue()->setBoolean(m_pDataList->last() != 0);
	return true;
}

bool KviKvsObject_list::function_movePrev(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)
	{
		c->returnValue()->setBoolean(false);
		return true;
	}
	c->returnValue()->setBoolean(m_pDataList->prev() != 0);
	return true;
}

bool KviKvsObject_list::function_moveNext(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)
	{
		c->returnValue()->setBoolean(false);
		return true;
	}
	c->returnValue()->setBoolean(m_pDataList->next() != 0);
	return true;
}

bool KviKvsObject_list::function_moveFirst(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)
	{
		c->returnValue()->setBoolean(false);
		return true;
	}
	c->returnValue()->setBoolean(m_pDataList->first() != 0);
	return true;
}

bool KviKvsObject_list::function_removeLast(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)
	{
		c->returnValue()->setBoolean(false);
		return true;
	}
	c->returnValue()->setBoolean(m_pDataList->removeLast());
	return true;
}

bool KviKvsObject_list::function_removeCurrent(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)
	{
		c->returnValue()->setBoolean(false);
		return true;
	}
	if(m_pDataList->current())
	{
		m_pDataList->removeCurrent();
		c->returnValue()->setBoolean(true);
	} else {
		c->returnValue()->setBoolean(false);
	}
	return true;
}

bool KviKvsObject_list::function_removeFirst(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)
	{
		c->returnValue()->setBoolean(false);
		return true;
	}
	c->returnValue()->setBoolean(m_pDataList->removeFirst());
	return true;
}

bool KviKvsObject_list::function_remove(KviKvsObjectFunctionCall *c)
{
	kvs_uint_t uIndex;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("index",KVS_PT_UINT,0,uIndex)
	KVSO_PARAMETERS_END(c)
	if(!m_pDataList)
	{
		c->returnValue()->setBoolean(false);
		return true;
	}
	c->returnValue()->setBoolean(m_pDataList->remove(uIndex));
	return true;
}

bool KviKvsObject_list::function_at(KviKvsObjectFunctionCall *c)
{
	kvs_uint_t uIndex;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("index",KVS_PT_UINT,0,uIndex)
	KVSO_PARAMETERS_END(c)
	if(!m_pDataList)
	{
		c->returnValue()->setNothing();
		return true;
	}
	KviKvsVariant * v = m_pDataList->at(uIndex);
	if(v)c->returnValue()->copyFrom(*v);
	else c->returnValue()->setNothing();
	return true;
}

bool KviKvsObject_list::function_insert(KviKvsObjectFunctionCall *c)
{
	kvs_uint_t uIndex;
	KviKvsVariant * pVar;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("index",KVS_PT_UINT,0,uIndex)
		KVSO_PARAMETER("item",KVS_PT_VARIANT,0,pVar)
	KVSO_PARAMETERS_END(c)
	if(!m_pDataList)return true;
	m_pDataList->insert(uIndex,new KviKvsVariant(*pVar));
	return true;
}

bool KviKvsObject_list::function_prepend(KviKvsObjectFunctionCall *c)
{
	KviKvsVariant * pVar;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("item",KVS_PT_VARIANT,0,pVar)
	KVSO_PARAMETERS_END(c)
	if(!m_pDataList)return true;
	m_pDataList->prepend(new KviKvsVariant(*pVar));
	return true;
}

bool KviKvsObject_list::function_append(KviKvsObjectFunctionCall *c)
{
	KviKvsVariant * pVar;
	KVSO_PARAMETERS_BEGIN(c)
		KVSO_PARAMETER("item",KVS_PT_VARIANT,0,pVar)
	KVSO_PARAMETERS_END(c)
	if(!m_pDataList)return true;
	m_pDataList->append(new KviKvsVariant(*pVar));
	return true;
}

bool KviKvsObject_list::function_clear(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)return true;
	m_pDataList->clear();
	return true;
}

inline int kvi_compare(const KviKvsVariant * p1,const KviKvsVariant * p2)
{
	return p1->compare(p2);
}

bool KviKvsObject_list::function_sort(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)return true;
	m_pDataList->sort();
	return true;
}

bool KviKvsObject_list::function_isEmpty(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)
	{
		c->returnValue()->setBoolean(true);
		return true;
	}
	c->returnValue()->setBoolean(m_pDataList->isEmpty());
	return true;
}

bool KviKvsObject_list::function_count(KviKvsObjectFunctionCall *c)
{
	if(!m_pDataList)
	{
		c->returnValue()->setInteger(0);
		return true;
	}
	c->returnValue()->setInteger(m_pDataList->count());
	return true;
}