Merge "Implemented JSON Serialization using Cereal Library"
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / src / uarraylist.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include "uarraylist.h"
24 #include "logger.h"
25 #include "oic_malloc.h"
26
27 #define TAG PCF("UARRAYLIST")
28
29 u_arraylist_t *u_arraylist_create()
30 {
31     u_arraylist_t *list = NULL;
32
33     if (!(list = (u_arraylist_t*) OICMalloc(sizeof(u_arraylist_t))))
34     {
35         return NULL;
36     }
37
38     list->size = U_ARRAYLIST_DEFAULT_SIZE;
39     list->length = 0;
40
41     if (!(list->data = (void*) OICMalloc(list->size * sizeof(void*))))
42     {
43         OICFree(list);
44         return NULL;
45     }
46
47     return list;
48 }
49
50 CAResult_t u_arraylist_free(u_arraylist_t *list)
51 {
52     OICFree(list->data);
53     OICFree(list);
54
55     list = NULL;
56
57     return CA_STATUS_OK;
58 }
59
60 void *u_arraylist_get(const u_arraylist_t *list, uint32_t index)
61 {
62     if (index >= list->length)
63     {
64         return NULL;
65     }
66
67     if (list->data)
68     {
69         return list->data[index];
70     }
71
72     return NULL;
73 }
74
75 CAResult_t u_arraylist_add(u_arraylist_t *list, void *data)
76 {
77     uint32_t new_size = 0;
78
79     if (list->size <= list->length)
80     {
81
82         new_size = list->size + 1;
83         if (!(list->data = (void **) realloc(list->data, new_size * sizeof(void *))))
84         {
85             return -1;
86         }
87
88         (void) memset(list->data + list->size, 0, (new_size - list->size) * sizeof(void *));
89         list->size = new_size;
90     }
91
92     list->data[list->length] = data;
93     list->length++;
94
95     return 0;
96 }
97
98 void *u_arraylist_remove(u_arraylist_t *list, uint32_t index)
99 {
100     void *removed = NULL;
101
102     if (index >= list->length)
103     {
104         return NULL;
105     }
106
107     removed = list->data[index];
108
109     if (index < list->length - 1)
110     {
111         memmove(&list->data[index], &list->data[index + 1],
112                 (list->length - index - 1) * sizeof(void *));
113     }
114
115     list->size--;
116     list->length--;
117
118     if (!(list->data = (void **) realloc(list->data, list->size * sizeof(void *))))
119     {
120         return NULL;
121     }
122
123     return removed;
124 }
125
126 uint32_t u_arraylist_length(const u_arraylist_t *list)
127 {
128     return list->length;
129 }
130
131 uint8_t u_arraylist_contains(const u_arraylist_t *list, void *data)
132 {
133     uint32_t i = 0;
134
135     for (i = 0; i < u_arraylist_length(list); i++)
136     {
137         if (data == u_arraylist_get(list, i))
138         {
139             return 1;
140         }
141         else
142         {
143             continue;
144         }
145     }
146
147     return 0;
148 }
149