Tizen 2.1 base
[platform/core/system/sync-agent.git] / src / framework / engine-controller / hash_set.c
1 /*
2  * sync-agent
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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 #include <stdlib.h>
19 #include <stdio.h>
20 #include "utility/sync_util.h"
21 #include "engine-controller/hash_set.h"
22
23 #ifndef SYNC_AGENT_LOG
24 #undef LOG_TAG
25 #define LOG_TAG "AF_EC"
26 #endif
27
28 ec_hash_set_t *ec_hash_set_create(void)
29 {
30         _EXTERN_FUNC_ENTER;
31
32         ec_hash_set_t *hash_set = (ec_hash_set_t *) calloc(1, sizeof(ec_hash_set_t));
33         if (hash_set == NULL) {
34                 goto error_part;
35         }
36
37         hash_set->hash_table = NULL;
38
39         _EXTERN_FUNC_EXIT;
40
41         return hash_set;
42
43  error_part:
44         return NULL;
45 }
46
47 void ec_hash_set_free(ec_hash_set_t * hash_set)
48 {
49         _EXTERN_FUNC_ENTER;
50
51         if (hash_set != NULL) {
52                 if (hash_set->hash_table != NULL) {
53                         g_hash_table_destroy(hash_set->hash_table);
54                 }
55
56                 free(hash_set);
57         }
58
59         _EXTERN_FUNC_EXIT;
60 }
61
62 sync_agent_ec_boolean ec_hash_set_add(ec_hash_set_t * hash_set, sync_agent_ec_constpointer element)
63 {
64         _EXTERN_FUNC_ENTER;
65
66         sync_agent_ec_boolean success = true;
67
68         if (hash_set == NULL) {
69                 _DEBUG_ERROR("hash_set == NULL\n");
70                 success = false;
71                 goto return_part;
72         }
73
74         if (hash_set->hash_table == NULL) {
75                 hash_set->hash_table = g_hash_table_new(g_direct_hash, g_direct_equal);
76         }
77
78         g_hash_table_insert(hash_set->hash_table, (gpointer) element, (gpointer) element);
79
80  return_part:
81         _EXTERN_FUNC_EXIT;
82         return success;
83 }
84
85 GList *ec_hash_set_get_element_list(ec_hash_set_t * hash_set)
86 {
87         _EXTERN_FUNC_ENTER;
88
89         GList *element_list = NULL;
90         if (hash_set == NULL) {
91                 _DEBUG_ERROR("hash_set == NULL\n");
92                 goto error_part;
93         }
94
95         if (hash_set->hash_table != NULL) {
96                 element_list = g_hash_table_get_values(hash_set->hash_table);
97         }
98
99         _EXTERN_FUNC_EXIT;
100
101         return element_list;
102
103  error_part:
104
105         return NULL;
106 }