2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "maps_error.h"
19 #include "maps_extra_types.h"
20 #include "maps_util.h"
21 #include "maps_condition.h"
23 typedef struct _maps_item_list_s
28 EXPORT_API int maps_item_list_create(maps_item_list_h *list)
30 if (!maps_condition_check_maps_feature())
31 return MAPS_ERROR_NOT_SUPPORTED;
33 return MAPS_ERROR_INVALID_PARAMETER;
34 maps_item_list_s *l = g_slice_new0(maps_item_list_s);
37 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
38 return MAPS_ERROR_OUT_OF_MEMORY;
43 *list = (maps_item_list_h *) l;
44 return MAPS_ERROR_NONE;
47 EXPORT_API int maps_item_list_destroy(maps_item_list_h list)
49 if (!maps_condition_check_maps_feature())
50 return MAPS_ERROR_NOT_SUPPORTED;
52 return MAPS_ERROR_INVALID_PARAMETER;
53 maps_item_list_s *l = (maps_item_list_s *) list;
56 g_slice_free(maps_item_list_s, l);
57 return MAPS_ERROR_NONE;
60 EXPORT_API int maps_item_list_append(maps_item_list_h list, const void *data,
61 maps_item_list_clone_cb clone_func)
63 if (!maps_condition_check_maps_feature())
64 return MAPS_ERROR_NOT_SUPPORTED;
66 return MAPS_ERROR_INVALID_PARAMETER;
69 const int error = clone_func((void *) data, &p);
70 if (error != MAPS_ERROR_NONE)
75 maps_item_list_s *l = (maps_item_list_s *) list;
76 l->l = g_list_append(l->l, (gpointer) p);
77 return MAPS_ERROR_NONE;
80 EXPORT_API int maps_item_list_foreach(maps_item_list_h list,
81 maps_item_list_clone_cb clone_func,
82 maps_item_list_foreach_cb callback,
85 if (!maps_condition_check_maps_feature())
86 return MAPS_ERROR_NOT_SUPPORTED;
87 if (!list || !callback)
88 return MAPS_ERROR_INVALID_PARAMETER;
89 maps_item_list_s *l = (maps_item_list_s *) list;
91 return MAPS_ERROR_NOT_FOUND;
93 const int total = g_list_length(l->l);
94 GList *head = g_list_first(l->l);
97 void *data = head->data;
101 if (clone_func(data, &clone) != MAPS_ERROR_NONE)
106 if (!callback(index++, total, clone, user_data))
110 return MAPS_ERROR_NONE;
113 EXPORT_API int maps_item_list_remove(maps_item_list_h list,
115 maps_item_list_free_cb free_func)
117 if (!maps_condition_check_maps_feature())
118 return MAPS_ERROR_NOT_SUPPORTED;
120 return MAPS_ERROR_INVALID_PARAMETER;
121 maps_item_list_s *l = (maps_item_list_s *) list;
123 l->l = g_list_remove(l->l, item);
127 return MAPS_ERROR_NONE;
130 EXPORT_API int maps_item_list_foreach_noclone(maps_item_list_h list, maps_item_list_foreach_noclone_cb callback, void *user_data)
132 if (!maps_condition_check_maps_feature())
133 return MAPS_ERROR_NOT_SUPPORTED;
134 if (!list || !callback)
135 return MAPS_ERROR_INVALID_PARAMETER;
136 maps_item_list_s *l = (maps_item_list_s *) list;
139 return MAPS_ERROR_NOT_FOUND;
141 GList *head = g_list_first(l->l);
144 void *data = head->data;
146 if (!callback(index++, data, user_data))
149 return MAPS_ERROR_NONE;
152 EXPORT_API int maps_item_list_remove_all(maps_item_list_h list,
153 maps_item_list_free_cb free_func)
155 if (!maps_condition_check_maps_feature())
156 return MAPS_ERROR_NOT_SUPPORTED;
158 return MAPS_ERROR_INVALID_PARAMETER;
160 maps_item_list_s *l = (maps_item_list_s *) list;
162 GList *head = g_list_first(l->l);
165 free_func(head->data);
172 return MAPS_ERROR_NONE;
175 EXPORT_API int maps_item_list_clone(const maps_item_list_h origin,
176 maps_item_list_clone_cb clone_func,
177 maps_item_list_h *cloned)
179 if (!maps_condition_check_maps_feature())
180 return MAPS_ERROR_NOT_SUPPORTED;
181 if (!cloned || !origin || !clone_func)
182 return MAPS_ERROR_INVALID_PARAMETER;
184 int error = MAPS_ERROR_NONE;
186 error = maps_item_list_create(cloned);
187 if (!(*cloned) || (error != MAPS_ERROR_NONE))
190 maps_item_list_s *l = (maps_item_list_s *) origin;
192 return MAPS_ERROR_NONE;
194 maps_item_list_s *l_cloned = (maps_item_list_s *) (*cloned);
196 /* It would be cool when we will have glib v 2.34 */
197 /*l_cloned->l = g_list_copy_deep(l->l, clone_func, NULL); */
199 GList *head = g_list_first(l->l);
201 void *cloned_item = NULL;
202 const int error = clone_func(head->data, &cloned_item);
203 if (error == MAPS_ERROR_NONE)
205 g_list_append(l_cloned->l,
206 (gpointer) cloned_item);
209 return MAPS_ERROR_NONE;
212 maps_item_list_destroy(*cloned);
217 EXPORT_API int maps_item_list_get_length(maps_item_list_h list, int *length)
219 if (!maps_condition_check_maps_feature())
220 return MAPS_ERROR_NOT_SUPPORTED;
221 if (!list || !length)
222 return MAPS_ERROR_INVALID_PARAMETER;
223 maps_item_list_s *l = (maps_item_list_s *)list;
227 *length = g_list_length(l->l);
228 return MAPS_ERROR_NONE;
231 /*----------------------------------------------------------------------------*/
233 typedef struct _maps_string_hashtable_s
236 } maps_string_hashtable_s;
238 EXPORT_API int maps_string_hashtable_create(maps_string_hashtable_h *table)
240 if (!maps_condition_check_maps_feature())
241 return MAPS_ERROR_NOT_SUPPORTED;
243 return MAPS_ERROR_INVALID_PARAMETER;
245 maps_string_hashtable_s *t = g_slice_new0(maps_string_hashtable_s);
247 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
248 return MAPS_ERROR_OUT_OF_MEMORY;
251 t->t = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
253 return MAPS_ERROR_NONE;
256 EXPORT_API int maps_string_hashtable_destroy(maps_string_hashtable_h table)
258 if (!maps_condition_check_maps_feature())
259 return MAPS_ERROR_NOT_SUPPORTED;
261 return MAPS_ERROR_INVALID_PARAMETER;
262 maps_string_hashtable_s *t = (maps_string_hashtable_s *) table;
264 g_hash_table_unref(t->t);
265 g_slice_free(maps_string_hashtable_s, t);
266 return MAPS_ERROR_NONE;
269 EXPORT_API int maps_string_hashtable_set(maps_string_hashtable_h table,
270 const char *key, const char *value)
272 if (!maps_condition_check_maps_feature())
273 return MAPS_ERROR_NOT_SUPPORTED;
274 if (!table || !key || !value)
275 return MAPS_ERROR_INVALID_PARAMETER;
276 maps_string_hashtable_s *t = (maps_string_hashtable_s *) table;
278 g_hash_table_insert(t->t, (gpointer) g_strdup(key),
279 (gpointer) g_strdup(value));
280 return MAPS_ERROR_NONE;
283 EXPORT_API int maps_string_hashtable_get(maps_string_hashtable_h table,
284 const char *key, char **value)
286 if (!maps_condition_check_maps_feature())
287 return MAPS_ERROR_NOT_SUPPORTED;
288 if (!table || !key || !value)
289 return MAPS_ERROR_INVALID_PARAMETER;
290 maps_string_hashtable_s *t = (maps_string_hashtable_s *) table;
292 return MAPS_ERROR_NOT_FOUND;
293 char *table_value = (char *) g_hash_table_lookup(t->t, key);
295 return MAPS_ERROR_NONE;
296 return maps_get_string(table_value, strlen(table_value), value);
299 EXPORT_API int maps_string_hashtable_remove(maps_string_hashtable_h table,
302 if (!maps_condition_check_maps_feature())
303 return MAPS_ERROR_NOT_SUPPORTED;
305 return MAPS_ERROR_INVALID_PARAMETER;
306 maps_string_hashtable_s *t = (maps_string_hashtable_s *) table;
308 g_hash_table_remove(t->t, (gpointer) key);
309 return MAPS_ERROR_NONE;
312 EXPORT_API int maps_string_hashtable_foreach(maps_string_hashtable_h table,
313 maps_string_hashtable_foreach_cb
317 if (!maps_condition_check_maps_feature())
318 return MAPS_ERROR_NOT_SUPPORTED;
319 if (!table || !callback)
320 return MAPS_ERROR_INVALID_PARAMETER;
321 maps_string_hashtable_s *t = (maps_string_hashtable_s *) table;
324 return MAPS_ERROR_NOT_FOUND;
328 const int total = g_hash_table_size(t->t);
330 g_hash_table_iter_init(&iter, t->t);
331 while (g_hash_table_iter_next(&iter, &key, &value)) {
332 char *key_clone = NULL;
333 if (maps_item_hashtable_clone_string(key,
334 (void **) &key_clone) != MAPS_ERROR_NONE)
336 char *value_clone = NULL;
337 if (maps_item_hashtable_clone_string(value,
338 (void **) &value_clone) != MAPS_ERROR_NONE)
341 if (!callback(index++, total, key_clone, value_clone,
345 return MAPS_ERROR_NONE;
348 EXPORT_API int maps_string_hashtable_clone(const maps_string_hashtable_h origin,
349 maps_string_hashtable_h *cloned)
351 if (!maps_condition_check_maps_feature())
352 return MAPS_ERROR_NOT_SUPPORTED;
353 if (!cloned || !origin)
354 return MAPS_ERROR_INVALID_PARAMETER;
356 int error = MAPS_ERROR_NONE;
358 error = maps_string_hashtable_create(cloned);
359 if (!(*cloned) || (error != MAPS_ERROR_NONE))
362 maps_string_hashtable_s *t = (maps_string_hashtable_s *) origin;
365 return MAPS_ERROR_NONE;
367 maps_string_hashtable_s *t_cloned =
368 (maps_string_hashtable_s *) (*cloned);
372 g_hash_table_iter_init(&iter, t->t);
373 while (g_hash_table_iter_next(&iter, &key, &value)) {
374 g_hash_table_insert(t_cloned->t,
375 (gpointer) g_strdup((const gchar *) key),
376 (gpointer) g_strdup((const gchar *) value));
379 return MAPS_ERROR_NONE;
382 maps_string_hashtable_destroy(*cloned);
387 EXPORT_API int maps_string_hashtable_contains(maps_string_hashtable_h table,
388 const char * key, bool * contains)
390 if (!maps_condition_check_maps_feature())
391 return MAPS_ERROR_NOT_SUPPORTED;
392 if (!table || !key || !contains)
393 return MAPS_ERROR_INVALID_PARAMETER;
394 maps_string_hashtable_s *t = (maps_string_hashtable_s *) table;
396 *contains = g_hash_table_contains(t->t, (gpointer) key);
397 return MAPS_ERROR_NONE;
400 /*----------------------------------------------------------------------------*/
401 typedef struct _maps_int_hashtable_s
404 } maps_int_hashtable_s;
406 EXPORT_API int maps_int_hashtable_create(maps_int_hashtable_h *table)
408 if (!maps_condition_check_maps_feature())
409 return MAPS_ERROR_NOT_SUPPORTED;
411 return MAPS_ERROR_INVALID_PARAMETER;
412 maps_int_hashtable_s *t = g_slice_new0(maps_int_hashtable_s);
415 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
416 return MAPS_ERROR_OUT_OF_MEMORY;
419 t->t = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, g_free);
421 return MAPS_ERROR_NONE;
424 EXPORT_API int maps_int_hashtable_destroy(maps_int_hashtable_h table)
426 if (!maps_condition_check_maps_feature())
427 return MAPS_ERROR_NOT_SUPPORTED;
429 return MAPS_ERROR_INVALID_PARAMETER;
430 maps_int_hashtable_s *t = (maps_int_hashtable_s *) table;
432 g_hash_table_unref(t->t);
433 g_slice_free(maps_int_hashtable_s, t);
434 return MAPS_ERROR_NONE;
437 EXPORT_API int maps_int_hashtable_set(maps_int_hashtable_h table,
438 const int key, const int value)
440 if (!maps_condition_check_maps_feature())
441 return MAPS_ERROR_NOT_SUPPORTED;
443 return MAPS_ERROR_INVALID_PARAMETER;
444 maps_int_hashtable_s *t = (maps_int_hashtable_s *) table;
446 int *new_key = g_new0(int, 1);
448 int *new_value = g_new0(int, 1);
450 g_hash_table_insert(t->t,
452 (gpointer)new_value);
454 return MAPS_ERROR_NONE;
457 EXPORT_API int maps_int_hashtable_get(maps_int_hashtable_h table,
458 const int key, int *value)
460 if (!maps_condition_check_maps_feature())
461 return MAPS_ERROR_NOT_SUPPORTED;
462 if (!table || !value)
463 return MAPS_ERROR_INVALID_PARAMETER;
464 maps_int_hashtable_s *t = (maps_int_hashtable_s *) table;
466 return MAPS_ERROR_NOT_FOUND;
467 const int *table_value = (const int *) g_hash_table_lookup(t->t, &key);
469 return MAPS_ERROR_NONE;
470 *value = *table_value;
471 return MAPS_ERROR_NONE;
474 EXPORT_API int maps_int_hashtable_remove(maps_int_hashtable_h table,
477 if (!maps_condition_check_maps_feature())
478 return MAPS_ERROR_NOT_SUPPORTED;
480 return MAPS_ERROR_INVALID_PARAMETER;
481 maps_int_hashtable_s *t = (maps_int_hashtable_s *) table;
483 g_hash_table_remove(t->t, (gpointer) &key);
484 return MAPS_ERROR_NONE;
487 EXPORT_API int maps_int_hashtable_foreach(maps_int_hashtable_h table,
488 maps_int_hashtable_foreach_cb callback,
491 if (!maps_condition_check_maps_feature())
492 return MAPS_ERROR_NOT_SUPPORTED;
493 if (!table || !callback)
494 return MAPS_ERROR_INVALID_PARAMETER;
495 maps_int_hashtable_s *t = (maps_int_hashtable_s *) table;
498 return MAPS_ERROR_NOT_FOUND;
502 const int total = g_hash_table_size(t->t);
504 g_hash_table_iter_init(&iter, t->t);
505 while (g_hash_table_iter_next(&iter, &key, &value)) {
509 int *key_ptr = (int *)key;
510 int *value_ptr = (int *)value;
511 if (!callback(index++, total, *key_ptr, *value_ptr, user_data))
514 return MAPS_ERROR_NONE;
517 EXPORT_API int maps_int_hashtable_clone(const maps_int_hashtable_h origin,
518 maps_int_hashtable_h *cloned)
520 if (!maps_condition_check_maps_feature())
521 return MAPS_ERROR_NOT_SUPPORTED;
522 if (!cloned || !origin)
523 return MAPS_ERROR_INVALID_PARAMETER;
525 int error = MAPS_ERROR_NONE;
527 error = maps_int_hashtable_create(cloned);
528 if (!(*cloned) || (error != MAPS_ERROR_NONE))
531 maps_int_hashtable_s *t = (maps_int_hashtable_s *) origin;
534 return MAPS_ERROR_NONE;
536 maps_int_hashtable_s *t_cloned =
537 (maps_int_hashtable_s *) (*cloned);
541 g_hash_table_iter_init(&iter, t->t);
542 while (g_hash_table_iter_next(&iter, &key, &value)) {
543 int *new_key = g_new0(int, 1);
544 *new_key = *((int *)key);
545 int *new_value = g_new0(int, 1);
546 *new_value = *((int *)value);
547 g_hash_table_insert(t_cloned->t,
549 (gpointer) new_value);
552 return MAPS_ERROR_NONE;
555 maps_int_hashtable_destroy(*cloned);
560 EXPORT_API int maps_int_hashtable_contains(maps_int_hashtable_h table,
561 const int key, bool *contains)
563 if (!maps_condition_check_maps_feature())
564 return MAPS_ERROR_NOT_SUPPORTED;
565 if (!table || !contains)
566 return MAPS_ERROR_INVALID_PARAMETER;
567 maps_int_hashtable_s *t = (maps_int_hashtable_s *) table;
569 *contains = g_hash_table_contains(t->t, (gpointer)&key);
570 return MAPS_ERROR_NONE;
573 /*----------------------------------------------------------------------------*/
575 typedef struct _maps_hashtable_item_s
578 maps_item_hashtable_clone_cb clone_func;
579 maps_item_hashtable_free_cb free_func;
580 } maps_hashtable_item_s;
582 void _maps_hashtable_item_create(maps_hashtable_item_s **item)
584 *item = g_slice_new0(maps_hashtable_item_s);
587 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
590 void _maps_hashtable_item_destroy(gpointer data)
595 maps_hashtable_item_s *item = (maps_hashtable_item_s *) data;
596 if (item && item->value && item->free_func)
597 item->free_func(item->value);
599 g_slice_free(maps_hashtable_item_s, item);
602 /*----------------------------------------------------------------------------*/
604 typedef struct _maps_item_hashtable_s
607 } maps_item_hashtable_s;
609 EXPORT_API int maps_item_hashtable_create(maps_item_hashtable_h *table)
611 if (!maps_condition_check_maps_feature())
612 return MAPS_ERROR_NOT_SUPPORTED;
614 return MAPS_ERROR_INVALID_PARAMETER;
615 maps_item_hashtable_s *t = g_slice_new0(maps_item_hashtable_s);
618 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
619 return MAPS_ERROR_OUT_OF_MEMORY;
622 t->t = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
623 _maps_hashtable_item_destroy);
625 return MAPS_ERROR_NONE;
628 EXPORT_API int maps_item_hashtable_destroy(maps_item_hashtable_h table)
630 if (!maps_condition_check_maps_feature())
631 return MAPS_ERROR_NOT_SUPPORTED;
633 return MAPS_ERROR_INVALID_PARAMETER;
634 maps_item_hashtable_s *t = (maps_item_hashtable_s *) table;
636 g_hash_table_unref(t->t);
637 g_slice_free(maps_item_hashtable_s, t);
638 return MAPS_ERROR_NONE;
641 EXPORT_API int maps_item_hashtable_clone_string(void *origin, void **cloned)
643 if (!maps_condition_check_maps_feature())
644 return MAPS_ERROR_NOT_SUPPORTED;
645 if (!origin || !cloned)
646 return MAPS_ERROR_INVALID_PARAMETER;
647 return maps_get_string((char *) origin, strlen((char *) origin),
651 EXPORT_API int maps_item_hashtable_free_string(void *data)
653 if (!maps_condition_check_maps_feature())
654 return MAPS_ERROR_NOT_SUPPORTED;
656 return MAPS_ERROR_NONE;
659 EXPORT_API int maps_item_hashtable_clone_int(void *origin, void **cloned)
661 if (!maps_condition_check_maps_feature())
662 return MAPS_ERROR_NOT_SUPPORTED;
663 if (!origin || !cloned)
664 return MAPS_ERROR_INVALID_PARAMETER;
665 int *n_cloned = g_new0(int, 1);
667 if (n_cloned == NULL) {
668 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
669 return MAPS_ERROR_OUT_OF_MEMORY;
672 int *n_origin = (int *) origin;
673 *n_cloned = *n_origin;
675 return MAPS_ERROR_NONE;
678 EXPORT_API int maps_item_hashtable_free_int(void *data)
680 if (!maps_condition_check_maps_feature())
681 return MAPS_ERROR_NOT_SUPPORTED;
683 return MAPS_ERROR_NONE;
686 EXPORT_API int maps_item_hashtable_clone_float(void *origin, void **cloned)
688 if (!maps_condition_check_maps_feature())
689 return MAPS_ERROR_NOT_SUPPORTED;
690 if (!origin || !cloned)
691 return MAPS_ERROR_INVALID_PARAMETER;
692 double *n_cloned = g_new0(double, 1);
694 if (n_cloned == NULL) {
695 MAPS_LOGE("OUT_OF_MEMORY(0x%08x)", MAPS_ERROR_OUT_OF_MEMORY);
696 return MAPS_ERROR_OUT_OF_MEMORY;
699 double *n_origin = (double *) origin;
700 *n_cloned = *n_origin;
702 return MAPS_ERROR_NONE;
705 EXPORT_API int maps_item_hashtable_free_float(void *data)
707 if (!maps_condition_check_maps_feature())
708 return MAPS_ERROR_NOT_SUPPORTED;
710 return MAPS_ERROR_NONE;
713 EXPORT_API int maps_item_hashtable_set_string(maps_item_hashtable_h table,
717 if (!maps_condition_check_maps_feature())
718 return MAPS_ERROR_NOT_SUPPORTED;
719 return maps_item_hashtable_set(table, key, value,
720 maps_item_hashtable_clone_string,
721 maps_item_hashtable_free_string);
724 EXPORT_API int maps_item_hashtable_set_int(maps_item_hashtable_h table,
725 const char *key, const int value)
727 if (!maps_condition_check_maps_feature())
728 return MAPS_ERROR_NOT_SUPPORTED;
729 return maps_item_hashtable_set(table, key, &value,
730 maps_item_hashtable_clone_int, maps_item_hashtable_free_int);
733 EXPORT_API int maps_item_hashtable_set_float(maps_item_hashtable_h table,
737 if (!maps_condition_check_maps_feature())
738 return MAPS_ERROR_NOT_SUPPORTED;
739 return maps_item_hashtable_set(table, key, &value,
740 maps_item_hashtable_clone_float,
741 maps_item_hashtable_free_float);
744 EXPORT_API int maps_item_hashtable_set(maps_item_hashtable_h table,
745 const char *key, const void *value,
746 maps_item_hashtable_clone_cb clone_func,
747 maps_item_hashtable_free_cb free_func)
749 if (!maps_condition_check_maps_feature())
750 return MAPS_ERROR_NOT_SUPPORTED;
751 if (!table || !key || !value || !clone_func || !free_func)
752 return MAPS_ERROR_INVALID_PARAMETER;
753 maps_item_hashtable_s *t = (maps_item_hashtable_s *) table;
756 maps_hashtable_item_s *item = NULL;
757 _maps_hashtable_item_create(&item);
759 const int error = clone_func((void *) value, &item->value);
760 if (error != MAPS_ERROR_NONE) {
761 _maps_hashtable_item_destroy(item);
764 item->clone_func = clone_func;
765 item->free_func = free_func;
767 g_hash_table_insert(t->t, (gpointer) g_strdup(key),
771 return MAPS_ERROR_NONE;
774 EXPORT_API int maps_item_hashtable_get_string(maps_item_hashtable_h table,
775 const char *key, char **value)
777 if (!maps_condition_check_maps_feature())
778 return MAPS_ERROR_NOT_SUPPORTED;
779 return maps_item_hashtable_get(table, key, (void **) value);
782 EXPORT_API int maps_item_hashtable_get_int(maps_item_hashtable_h table,
783 const char *key, int *value)
785 if (!maps_condition_check_maps_feature())
786 return MAPS_ERROR_NOT_SUPPORTED;
787 if (!table || !key || !value)
788 return MAPS_ERROR_INVALID_PARAMETER;
790 const int error = maps_item_hashtable_get(table, key, (void **) &data);
791 if (error == MAPS_ERROR_NONE) {
793 maps_item_hashtable_free_int(data);
798 EXPORT_API int maps_item_hashtable_get_float(maps_item_hashtable_h table,
799 const char *key, double *value)
801 if (!maps_condition_check_maps_feature())
802 return MAPS_ERROR_NOT_SUPPORTED;
803 if (!table || !key || !value)
804 return MAPS_ERROR_INVALID_PARAMETER;
806 const int error = maps_item_hashtable_get(table, key, (void **) &data);
807 if (error == MAPS_ERROR_NONE) {
809 maps_item_hashtable_free_float(data);
814 EXPORT_API int maps_item_hashtable_get(maps_item_hashtable_h table,
815 const char *key, void **value)
817 if (!maps_condition_check_maps_feature())
818 return MAPS_ERROR_NOT_SUPPORTED;
819 if (!table || !key || !value)
820 return MAPS_ERROR_INVALID_PARAMETER;
821 maps_item_hashtable_s *t = (maps_item_hashtable_s *) table;
823 return MAPS_ERROR_NOT_FOUND;
824 maps_hashtable_item_s *item =
825 (maps_hashtable_item_s *) g_hash_table_lookup(t->t, key);
826 if (!item || !item->clone_func)
827 return MAPS_ERROR_NOT_FOUND;
828 return item->clone_func(item->value, value);
831 EXPORT_API int maps_item_hashtable_remove(maps_item_hashtable_h table,
834 if (!maps_condition_check_maps_feature())
835 return MAPS_ERROR_NOT_SUPPORTED;
837 return MAPS_ERROR_INVALID_PARAMETER;
838 maps_item_hashtable_s *t = (maps_item_hashtable_s *) table;
840 g_hash_table_remove(t->t, (gpointer) key);
841 return MAPS_ERROR_NONE;
844 EXPORT_API int maps_item_hashtable_foreach(maps_item_hashtable_h table,
845 maps_item_hashtable_foreach_cb
849 if (!maps_condition_check_maps_feature())
850 return MAPS_ERROR_NOT_SUPPORTED;
851 if (!table || !callback)
852 return MAPS_ERROR_INVALID_PARAMETER;
853 maps_item_hashtable_s *t = (maps_item_hashtable_s *) table;
855 return MAPS_ERROR_NOT_FOUND;
859 const int total = g_hash_table_size(t->t);
861 g_hash_table_iter_init(&iter, t->t);
862 while (g_hash_table_iter_next(&iter, &key, &value)) {
863 maps_hashtable_item_s *item = (maps_hashtable_item_s *) value;
864 if (!item || !item->clone_func)
867 char *key_clone = NULL;
868 if (maps_item_hashtable_clone_string(key,
869 (void **) &key_clone) != MAPS_ERROR_NONE)
871 void *value_clone = NULL;
872 if (item->clone_func(item->value,
873 &value_clone) != MAPS_ERROR_NONE)
876 if (!callback(index++, total, key_clone, value_clone,
881 return MAPS_ERROR_NONE;
884 EXPORT_API int maps_item_hashtable_clone(const maps_item_hashtable_h origin,
885 maps_item_hashtable_h* cloned)
887 if (!maps_condition_check_maps_feature())
888 return MAPS_ERROR_NOT_SUPPORTED;
889 if (!cloned || !origin)
890 return MAPS_ERROR_INVALID_PARAMETER;
892 int error = MAPS_ERROR_NONE;
894 error = maps_item_hashtable_create(cloned);
895 if (!(*cloned) || (error != MAPS_ERROR_NONE))
898 maps_item_hashtable_s *t = (maps_item_hashtable_s *) origin;
901 return MAPS_ERROR_NONE;
905 g_hash_table_iter_init(&iter, t->t);
906 while (g_hash_table_iter_next(&iter, &key, &value)) {
907 maps_hashtable_item_s *item =
908 (maps_hashtable_item_s *) value;
911 maps_item_hashtable_set(*cloned, (const char *) key,
912 (const void *) item->value, item->clone_func,
916 return MAPS_ERROR_NONE;
919 maps_item_hashtable_destroy(*cloned);
924 EXPORT_API int maps_item_hashtable_contains(maps_item_hashtable_h table,
925 const char *key, bool *contains)
927 if (!maps_condition_check_maps_feature())
928 return MAPS_ERROR_NOT_SUPPORTED;
929 if (!table || !key || !contains)
930 return MAPS_ERROR_INVALID_PARAMETER;
931 maps_item_hashtable_s *t = (maps_item_hashtable_s *) table;
933 *contains = g_hash_table_contains(t->t, (gpointer) key);
934 return MAPS_ERROR_NONE;