Revert " modify license, permission and remove ^M char"
[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
386         pSimpleList->nodeCount--;
387
388         return pObject;
389 }
390
391 bool
392 TextSimpleList::DeleteAllObject(SimpleList* pSimpleList)
393 {
394         SysTryReturn(NID_GRP
395                         , pSimpleList
396                         , false, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
397
398         SimpleNode* pNode = null;
399         SimpleNode* pNextNode = null;
400         void* pObject = null;
401
402         if (pSimpleList->nodeCount <= 0)
403         {
404                 return false;
405         }
406
407         pNode = pSimpleList->pHeaderNode;
408
409         while (pNode != null)
410         {
411                 pNextNode = pNode->pNext;
412                 pObject = pNode->pObject;
413                 free(pObject);
414                 delete pNode;
415                 pNode = null;
416
417                 pNode = pNextNode;
418         }
419
420         pSimpleList->nodeCount = 0;
421         pSimpleList->pHeaderNode = null;
422         pSimpleList->pTailNode = null;
423
424         return true;
425 }
426
427 void*
428 TextSimpleList::GetFirstObject(SimpleList* pSimpleList)
429 {
430         SysTryReturn(NID_GRP
431                         , pSimpleList
432                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
433
434         SimpleNode* pNode = null;
435
436         pNode = GetFirstNode(pSimpleList);
437
438         SysTryReturn(NID_GRP
439                 , pNode
440                 , false, E_SYSTEM, "[E_SYSTEM] Fail to get first node.");
441
442         return pNode->pObject;
443 }
444
445 void*
446 TextSimpleList::GetNthObject(SimpleList* pSimpleList, int index)
447 {
448         if (pSimpleList->nodeCount == 0)
449         {
450                 return null;
451         }
452
453         SysTryReturn(NID_GRP
454                         , pSimpleList && index >= 0 && index < pSimpleList->nodeCount
455                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
456
457         SimpleNode* pNode = null;
458
459         pNode = GetNthNode(pSimpleList, index);
460
461         SysTryReturn(NID_GRP
462                 , pNode
463                 , false, E_SYSTEM, "[E_SYSTEM] Fail to get Nth Node.");
464
465         return pNode->pObject;
466 }
467
468 int
469 TextSimpleList::GetObjectIndex(SimpleList* pSimpleList, void* pObject)
470 {
471         SysTryReturn(NID_GRP
472                         , pSimpleList
473                         , -1, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
474
475         SimpleNode* pNode = null;
476         int index = 0;
477
478         pNode = pSimpleList->pHeaderNode;
479         while (pNode)
480         {
481                 if (pNode->pObject == pObject)
482                 {
483                         return index;
484                 }
485
486                 index++;
487                 pNode = pNode->pNext;
488         }
489
490         return -1;
491 }
492
493 SimpleNode*
494 TextSimpleList::InsertObjectAfterNode(SimpleList* pSimpleList, SimpleNode* pSimpleNode, void* pObject)
495 {
496         SysTryReturn(NID_GRP
497                         , pSimpleList
498                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
499
500         SimpleNode* pNode = null;
501         pNode = new (std::nothrow) SimpleNode;
502         if (pNode == null)
503         {
504                 return null;
505         }
506
507         pNode->pObject = pObject;
508
509         pNode->pNext = pSimpleNode->pNext;
510         if (pSimpleNode->pNext != null)
511         {
512                 pSimpleNode->pNext->pPrev = pNode;
513         }
514
515         pSimpleNode->pNext = pNode;
516         pNode->pPrev = pSimpleNode;
517
518         if (pSimpleList->pTailNode == pSimpleNode)
519         {
520                 pSimpleList->pTailNode = pNode;
521         }
522
523         pSimpleList->nodeCount++;
524
525         return pNode;
526 }
527
528 SimpleNode*
529 TextSimpleList::InsertObjectBeforeNode(SimpleList* pSimpleList, SimpleNode* pSimpleNode, void* pObject)
530 {
531         SysTryReturn(NID_GRP
532                         , pSimpleList
533                         , null, E_INVALID_ARG, "[E_INVALID_ARG] The argument is invalid.");
534
535         SimpleNode* pNode = null;
536         pNode = new (std::nothrow) SimpleNode;
537         if (pNode == null)
538         {
539                 return null;
540         }
541
542         pNode->pObject = pObject;
543
544         if (pSimpleNode->pPrev == null)
545         {
546                 pNode->pPrev = null;
547                 pNode->pNext = pSimpleNode;
548                 pSimpleNode->pPrev = pNode;
549                 pSimpleList->pHeaderNode = pNode;
550         }
551         else
552         {
553                 pNode->pNext = pSimpleNode;
554                 pSimpleNode->pPrev->pNext = pNode;
555                 pNode->pPrev = pSimpleNode->pPrev;
556                 pSimpleNode->pPrev = pNode;
557         }
558
559         pSimpleList->nodeCount++;
560         return pNode;
561 }
562
563 }}} // Tizen::Graphics::_Text