Tizen 2.1 base
[framework/osp/uifw.git] / src / graphics / text / FGrp_TextTextSimpleList.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /*
19  * @file        FGrp_TextTextSimpleList.cpp
20  * @brief       This is the implementation file for TextSimpleList class.
21  */
22
23 #include <new>
24 #include <FBaseSysLog.h>
25 #include "FGrp_TextTextSimpleList.h"
26 #include "FGrp_TextCommon.h"
27
28 namespace Tizen { namespace Graphics
29 {
30
31 namespace _Text
32 {
33 SimpleList*
34 TextSimpleList::Create(void)
35 {
36         SimpleList* pSimpleList = null;
37
38         pSimpleList = new (std::nothrow) SimpleList;
39
40         return pSimpleList;
41 }
42
43 bool
44 TextSimpleList::Destory(SimpleList* pSimpleList)
45 {
46         DeleteAllObject(pSimpleList);
47
48         delete pSimpleList;
49         pSimpleList = null;
50
51         return true;
52 }
53
54 bool
55 TextSimpleList::Init(SimpleList* pSimpleList)
56 {
57         SysTryReturn(NID_GRP
58                         , pSimpleList
59                         , false, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
60
61         pSimpleList->pHeaderNode = null;
62         pSimpleList->pTailNode = null;
63         pSimpleList->nodeCount = 0;
64
65         return true;
66 }
67
68 SimpleNode*
69 TextSimpleList::InsertObject(SimpleList* pSimpleList, void* pObject, int index)
70 {
71         SysTryReturn(NID_GRP
72                         , pSimpleList
73                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
74
75         SimpleNode* pNode = null;
76         SimpleNode* pNextNode = null;
77
78         if (index < 1)
79         {
80                 return AddObject(pSimpleList, pObject);
81         }
82
83         if (index >= pSimpleList->nodeCount)
84         {
85                 return AppendObject(pSimpleList, pObject);
86         }
87
88         pNode = new (std::nothrow) SimpleNode;
89         if (pNode == null)
90         {
91                 return null;
92         }
93
94         pNode->pObject = pObject;
95
96         SimpleNode* pPrevNode = GetNthNode(pSimpleList, index - 1);
97         SysTryCatch(NID_GRP
98                 , pPrevNode
99                 , , E_SYSTEM, "[E_SYSTEM] Fail to get node.");
100
101         pNextNode = pPrevNode->pNext;
102         SysTryCatch(NID_GRP
103                 , pNextNode
104                 , , E_SYSTEM, "[E_SYSTEM] Fail to get node.");
105
106         pNode->pNext = pNextNode;
107         pPrevNode->pNext = pNode;
108
109         pNode->pPrev = pPrevNode;
110         pNextNode->pPrev = pNode;
111
112         pSimpleList->nodeCount++;
113
114         return pNode;
115
116 CATCH:
117         delete pNode;
118         pNode = null;
119
120         return null;
121 }
122
123 bool
124 TextSimpleList::IsEmpty(SimpleList* pSimpleList)
125 {
126         SysTryReturn(NID_GRP
127                         , pSimpleList
128                         , false, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
129
130         bool r = false;
131
132         if (pSimpleList->nodeCount == 0)
133         {
134                 r = true;
135         }
136
137         return r;
138 }
139
140 int
141 TextSimpleList::GetCount(SimpleList* pSimpleList)
142 {
143         SysTryReturn(NID_GRP
144                         , pSimpleList
145                         , false, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
146
147         return pSimpleList->nodeCount;
148 }
149
150 SimpleNode*
151 TextSimpleList::AddObject(SimpleList* pSimpleList, void* pObject)
152 {
153         SysTryReturn(NID_GRP
154                         , pSimpleList
155                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
156
157         SimpleNode* pNode = null;
158         pNode = new (std::nothrow) SimpleNode;
159         if (pNode == null)
160         {
161                 return null;
162         }
163
164         pNode->pObject = pObject;
165
166         if (pSimpleList->pHeaderNode != null)
167         {
168                 pNode->pNext = pSimpleList->pHeaderNode;
169                 pNode->pPrev = null;
170                 pSimpleList->pHeaderNode->pPrev = pNode;
171                 pSimpleList->pHeaderNode = pNode;
172         }
173         else
174         {
175                 pSimpleList->pHeaderNode = pNode;
176                 pSimpleList->pTailNode = pNode;
177                 pNode->pNext = null;
178                 pNode->pPrev = null;
179         }
180
181         pSimpleList->nodeCount++;
182
183         return pNode;
184 }
185
186 SimpleNode*
187 TextSimpleList::AppendObject(SimpleList* pSimpleList, void* pObject)
188 {
189         SysTryReturn(NID_GRP
190                         , pSimpleList
191                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
192
193         SimpleNode* pNode = null;
194         pNode = new (std::nothrow) SimpleNode;
195         if (pNode == null)
196         {
197                 return null;
198         }
199
200         pNode->pObject = pObject;
201
202         if (pSimpleList->pHeaderNode != null)
203         {
204                 SimpleNode* pPrevNode = pSimpleList->pTailNode;
205
206                 pPrevNode->pNext = pNode;
207                 pNode->pNext = null;
208                 pNode->pPrev = pPrevNode;
209                 pSimpleList->pTailNode = pNode;
210         }
211         else
212         {
213                 pSimpleList->pHeaderNode = pNode;
214                 pSimpleList->pTailNode = pNode;
215                 pNode->pNext = null;
216                 pNode->pPrev = null;
217         }
218
219         pSimpleList->nodeCount++;
220
221         return pNode;
222 }
223
224 SimpleNode*
225 TextSimpleList::GetFirstNode(SimpleList* pSimpleList)
226 {
227         SysTryReturn(NID_GRP
228                         , pSimpleList
229                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
230
231         return pSimpleList->pHeaderNode;
232 }
233
234 SimpleNode*
235 TextSimpleList::GetLastNode(SimpleList* pSimpleList)
236 {
237         SysTryReturn(NID_GRP
238                         , pSimpleList
239                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
240
241         return pSimpleList->pTailNode;
242 }
243
244 bool
245 TextSimpleList::DeleteNode(SimpleList* pSimpleList, SimpleNode* pNode)
246 {
247         SysTryReturn(NID_GRP
248                         , pSimpleList
249                         , false, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
250
251         SimpleNode* pPrevNode = null;
252
253         if (pSimpleList->pHeaderNode == pNode)
254         {
255                 if (pSimpleList->pTailNode == pNode)
256                 {
257                         pSimpleList->pHeaderNode = null;
258                         pSimpleList->pTailNode = null;
259                 }
260                 else
261                 {
262                         pSimpleList->pHeaderNode = pSimpleList->pHeaderNode->pNext;
263                         if (pSimpleList->pHeaderNode != null)
264                         {
265                                 pSimpleList->pHeaderNode->pPrev = null;
266                         }
267                 }
268         }
269         else if (pSimpleList->pTailNode == pNode)
270         {
271                 pSimpleList->pTailNode = pSimpleList->pTailNode->pPrev;
272                 pSimpleList->pTailNode->pNext = null;
273         }
274         else
275         {
276                 pPrevNode = pSimpleList->pHeaderNode;
277                 while (pPrevNode && pPrevNode->pNext != pNode)
278                 {
279                         pPrevNode = pPrevNode->pNext;
280                 }
281
282                 if (!pPrevNode)
283                 {
284                         return false;
285                 }
286
287                 pPrevNode->pNext = pNode->pNext;
288
289                 if (pPrevNode->pNext != null)
290                 {
291                         pPrevNode->pNext->pPrev = pPrevNode;
292                 }
293         }
294
295         delete pNode;
296         pNode = null;
297         pSimpleList->nodeCount--;
298
299         return true;
300 }
301
302 SimpleNode*
303 TextSimpleList::GetNthNode(SimpleList* pSimpleList, int index)
304 {
305         SysTryReturn(NID_GRP
306                         , pSimpleList
307                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
308
309         SimpleNode* pNode = null;
310         int n = 0;
311
312         if (index < 0)
313         {
314                 return null;
315         }
316
317         pNode = pSimpleList->pHeaderNode;
318         while (pNode && n < index)
319         {
320                 pNode = pNode->pNext;
321                 n++;
322         }
323
324         return pNode;
325 }
326
327 void*
328 TextSimpleList::DeleteNthObject(SimpleList* pSimpleList, int index)
329 {
330         SysTryReturn(NID_GRP
331                         , pSimpleList
332                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
333
334         SimpleNode* pNode = null;
335         void* pObject = null;
336
337         if (pSimpleList->nodeCount == 0)
338         {
339                 return null;
340         }
341
342         pNode = GetNthNode(pSimpleList, index);
343         if (!pNode)
344         {
345                 return null;
346         }
347
348         pObject = pNode->pObject;
349
350         if (pSimpleList->pHeaderNode == pNode)
351         {
352                 if (pSimpleList->pTailNode == pNode)
353                 {
354                         pSimpleList->pHeaderNode = null;
355                         pSimpleList->pTailNode = null;
356                 }
357                 else
358                 {
359                         pSimpleList->pHeaderNode = pSimpleList->pHeaderNode->pNext;
360                         if (pSimpleList->pHeaderNode != null)
361                         {
362                                 pSimpleList->pHeaderNode->pPrev = null;
363                         }
364                 }
365         }
366         else if (pSimpleList->pTailNode == pNode)
367         {
368                 pSimpleList->pTailNode = pSimpleList->pTailNode->pPrev;
369                 pSimpleList->pTailNode->pNext = null;
370         }
371         else
372         {
373                 SimpleNode* pPrevNode = null;
374
375                 pPrevNode = pNode->pPrev;
376                 pPrevNode->pNext = pNode->pNext;
377                 if (pPrevNode->pNext != null)
378                 {
379                         pPrevNode->pNext->pPrev = pPrevNode;
380                 }
381         }
382
383         delete pNode;
384         pNode = null;
385         pSimpleList->nodeCount--;
386
387         return pObject;
388 }
389
390 bool
391 TextSimpleList::DeleteAllObject(SimpleList* pSimpleList)
392 {
393         SysTryReturn(NID_GRP
394                         , pSimpleList
395                         , false, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
396
397         SimpleNode* pNode = null;
398         SimpleNode* pNextNode = null;
399         void* pObject = null;
400
401         if (pSimpleList->nodeCount <= 0)
402         {
403                 return false;
404         }
405
406         pNode = pSimpleList->pHeaderNode;
407
408         while (pNode != null)
409         {
410                 pNextNode = pNode->pNext;
411                 pObject = pNode->pObject;
412                 free(pObject);
413                 delete pNode;
414                 pNode = null;
415
416                 pNode = pNextNode;
417         }
418
419         pSimpleList->nodeCount = 0;
420         pSimpleList->pHeaderNode = null;
421         pSimpleList->pTailNode = null;
422
423         return true;
424 }
425
426 void*
427 TextSimpleList::GetFirstObject(SimpleList* pSimpleList)
428 {
429         SysTryReturn(NID_GRP
430                         , pSimpleList
431                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
432
433         SimpleNode* pNode = null;
434
435         pNode = GetFirstNode(pSimpleList);
436
437         SysTryReturn(NID_GRP
438                 , pNode
439                 , false, E_SYSTEM, "[E_SYSTEM] Fail to get first node.");
440
441         return pNode->pObject;
442 }
443
444 void*
445 TextSimpleList::GetNthObject(SimpleList* pSimpleList, int index)
446 {
447         if (pSimpleList->nodeCount == 0)
448         {
449                 return null;
450         }
451
452         SysTryReturn(NID_GRP
453                         , pSimpleList && index >= 0 && index < pSimpleList->nodeCount
454                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
455
456         SimpleNode* pNode = null;
457
458         pNode = GetNthNode(pSimpleList, index);
459
460         SysTryReturn(NID_GRP
461                 , pNode
462                 , false, E_SYSTEM, "[E_SYSTEM] Fail to get Nth Node.");
463
464         return pNode->pObject;
465 }
466
467 int
468 TextSimpleList::GetObjectIndex(SimpleList* pSimpleList, void* pObject)
469 {
470         SysTryReturn(NID_GRP
471                         , pSimpleList
472                         , -1, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
473
474         SimpleNode* pNode = null;
475         int index = 0;
476
477         pNode = pSimpleList->pHeaderNode;
478         while (pNode)
479         {
480                 if (pNode->pObject == pObject)
481                 {
482                         return index;
483                 }
484
485                 index++;
486                 pNode = pNode->pNext;
487         }
488
489         return -1;
490 }
491
492 SimpleNode*
493 TextSimpleList::InsertObjectAfterNode(SimpleList* pSimpleList, SimpleNode* pSimpleNode, void* pObject)
494 {
495         SysTryReturn(NID_GRP
496                         , pSimpleList
497                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
498
499         SimpleNode* pNode = null;
500         pNode = new (std::nothrow) SimpleNode;
501         if (pNode == null)
502         {
503                 return null;
504         }
505
506         pNode->pObject = pObject;
507
508         pNode->pNext = pSimpleNode->pNext;
509         if (pSimpleNode->pNext != null)
510         {
511                 pSimpleNode->pNext->pPrev = pNode;
512         }
513
514         pSimpleNode->pNext = pNode;
515         pNode->pPrev = pSimpleNode;
516
517         if (pSimpleList->pTailNode == pSimpleNode)
518         {
519                 pSimpleList->pTailNode = pNode;
520         }
521
522         pSimpleList->nodeCount++;
523
524         return pNode;
525 }
526
527 SimpleNode*
528 TextSimpleList::InsertObjectBeforeNode(SimpleList* pSimpleList, SimpleNode* pSimpleNode, void* pObject)
529 {
530         SysTryReturn(NID_GRP
531                         , pSimpleList
532                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
533
534         SimpleNode* pNode = null;
535         pNode = new (std::nothrow) SimpleNode;
536         if (pNode == null)
537         {
538                 return null;
539         }
540
541         pNode->pObject = pObject;
542
543         if (pSimpleNode->pPrev == null)
544         {
545                 pNode->pPrev = null;
546                 pNode->pNext = pSimpleNode;
547                 pSimpleNode->pPrev = pNode;
548                 pSimpleList->pHeaderNode = pNode;
549         }
550         else
551         {
552                 pNode->pNext = pSimpleNode;
553                 pSimpleNode->pPrev->pNext = pNode;
554                 pNode->pPrev = pSimpleNode->pPrev;
555                 pSimpleNode->pPrev = pNode;
556         }
557
558         pSimpleList->nodeCount++;
559         return pNode;
560 }
561
562 }}} // Tizen::Graphics::_Text