RDK Documentation (Open Sourced RDK Components)
dcalist.c
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
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 
22 /**
23  * @defgroup dca
24  * @{
25  **/
26 
27 
28 
29 
30 /**
31  * @defgroup dca
32  * @{
33  * @defgroup src
34  * @{
35  **/
36 
37 #include "dcautils.h"
38 #include "dcalist.h"
39 #include "safec_lib.h"
40 
41 /*
42  * @addtogroup DCA_APIS
43  * @{
44  */
45 
46 /**
47  * @brief To insert a new pattern node at the beginning of the list.
48  *
49  * @param[in] pch Node head.
50  * @param[in] pattern Search pattern.
51  * @param[in] header Header.
52  * @param[in] dtype Data type.
53  * @param[in] count Pattern count.
54  * @param[in] data Data.
55  *
56  * @return Returns the value of rc.
57  */
58 int insertPCNode(GList **pch, char *pattern, char *header, DType_t dtype, int count, char *data)
59 {
60  pcdata_t *new = NULL;
61  int rc = -1;
62 
63  new = (pcdata_t *) malloc(sizeof(*new));
64  if (NULL != new) {
65  if (pattern != NULL) {
66  new->pattern = strdup(pattern);
67  if (!(new->pattern)) {
68  free(new->pattern);
69  free(new);
70  return rc;
71  }
72  } else {
73  new->pattern = NULL;
74  }
75  if (header != NULL) {
76  new->header = strdup(header);
77  if (!(new->header)) {
78  free(new->header);
79  free(new);
80  return rc;
81 
82  }
83  } else {
84  new->header = NULL;
85  }
86 
87  new->d_type = dtype;
88  if (dtype == INT) {
89  new->count = count;
90  } else if (dtype == STR) {
91  if (NULL != data) {
92  new->data = strdup(data);
93  if (!(new->data)) {
94  free(new->data);
95  free(new);
96  return rc;
97  }
98  } else {
99  new->data = NULL;
100  }
101  }
102 
103  *pch = g_list_append(*pch , new);
104  rc = 0;
105  }
106  return rc;
107 }
108 
109 /**
110  * @brief To do custom comparison.
111  *
112  * @param[in] np Node pattern.
113  * @param[in] sp Search pattern.
114  *
115  * @return Returns status of the operation.
116  * @retval Returns 0 on success and NULL on failure.
117  */
118 gint comparePattern(gconstpointer np, gconstpointer sp)
119 {
120  pcdata_t *tmp = (pcdata_t *)np;
121  if (tmp && tmp->pattern && NULL != strstr(sp, tmp->pattern)) {
122  return 0;
123  }
124  return -1;
125 }
126 
127 /**
128  * @brief To search node from the list based on the given pattern.
129  *
130  * @param[in] pch Node head.
131  * @param[in] pattern Pattern to search.
132  *
133  * @return Returns node on success and NULL on failure.
134  */
135 pcdata_t* searchPCNode(GList *pch, char *pattern)
136 {
137  GList *fnode = NULL;
138  fnode = g_list_find_custom(pch, pattern, (GCompareFunc)comparePattern);
139  if (NULL != fnode)
140  return fnode->data;
141  else
142  return NULL;
143 }
144 
145 /**
146  * @brief Debug function to print the node.
147  *
148  * @param[in] data node data
149  * @param[in] user_data user data
150  */
151 void print_pc_node(gpointer data, gpointer user_data)
152 {
153  UNREFERENCED_PARAMETER(user_data);
154  pcdata_t *node = (pcdata_t *)data;
155  if (node) {
156  LOG("node pattern:%s, header:%s", node->pattern, node->header);
157  if (node->d_type == INT) {
158  LOG("\tcount:%d", node->count);
159  } else if (node->d_type == STR) {
160  LOG("\tdata:%s", node->data);
161  }
162  }
163 }
164 
165 /**
166  * @brief Debug function to print all nodes in the list.
167  *
168  * @param[in] pch node head
169  */
170 void printPCNodes(GList *pch)
171 {
172  g_list_foreach(pch, (GFunc)print_pc_node, NULL);
173 }
174 
175 /**
176  * @brief To delete a node.
177  *
178  * @param[in] node Node head.
179  */
180  void freePCNode(gpointer node)
181 {
182  pcdata_t *tmp = (pcdata_t *)(node);
183  if (NULL != tmp)
184  {
185  if (NULL != tmp->pattern) {
186  free(tmp->pattern);
187  tmp->pattern = NULL;
188  }
189  if (NULL != tmp->header) {
190  free(tmp->header);
191  tmp->header = NULL;
192  }
193  if (tmp->d_type == STR) {
194  if (NULL != tmp->data) {
195  free(tmp->data);
196  tmp->data = NULL;
197  }
198  }
199  free(tmp);
200  }
201 }
202 
203 /**
204  * @brief To delete/clear all the nodes in the list.
205  *
206  * @param[in] pch Node head.
207  */
208 void clearPCNodes(GList **pch)
209 {
210  g_list_free_full(*pch, &freePCNode);
211 }
212 
213 /** @} */ //END OF GROUP DCA_APIS
214 
215 /** @} */
216 
217 
218 /** @} */
219 /** @} */
220 
221 
insertPCNode
int insertPCNode(GList **pch, char *pattern, char *header, DType_t dtype, int count, char *data)
To insert a new pattern node at the beginning of the list.
Definition: dcalist.c:58
comparePattern
gint comparePattern(gconstpointer np, gconstpointer sp)
To do custom comparison.
Definition: dcalist.c:118
clearPCNodes
void clearPCNodes(GList **pch)
To delete/clear all the nodes in the list.
Definition: dcalist.c:208
freePCNode
void freePCNode(gpointer node)
To delete a node.
Definition: dcalist.c:180
print_pc_node
void print_pc_node(gpointer data, gpointer user_data)
Debug function to print the node.
Definition: dcalist.c:151
searchPCNode
pcdata_t * searchPCNode(GList *pch, char *pattern)
To search node from the list based on the given pattern.
Definition: dcalist.c:135
printPCNodes
void printPCNodes(GList *pch)
Debug function to print all nodes in the list.
Definition: dcalist.c:170
pclist
Definition: dcalist.h:48