add comment LCOV_EXCL
[platform/core/pim/calendar-service.git] / common / cal_utils.c
1 /*
2  * Calendar Service
3  *
4  * Copyright (c) 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <stdlib.h>
21 #include <glib.h>
22 #include <glib/gprintf.h>
23 #include "cal_internal.h"
24
25 char* cal_strdup(const char *src)
26 {
27         if (src)
28                 return strdup(src);
29         return NULL;
30 }
31
32 static gint _sort_cb(gconstpointer a, gconstpointer b)
33 {
34         return GPOINTER_TO_INT(a) < GPOINTER_TO_INT(b) ? -1 : 1;
35 }
36
37 char* cal_strdup_with_sort(const char *src)
38 {
39         RETV_IF(NULL == src, NULL);
40
41         char **t = NULL;
42         t = g_strsplit_set(src, " ,", -1);
43         if (NULL == t) {
44                 /* LCOV_EXCL_START */
45                 ERR("g_strsplit_set() Fail");
46                 return NULL;
47                 /* LCOV_EXCL_STOP */
48         }
49         int len_t = 0;
50         len_t = g_strv_length(t);
51         if (0 == len_t) {
52                 /* LCOV_EXCL_START */
53                 ERR("Empty src");
54                 return NULL;
55                 /* LCOV_EXCL_STOP */
56         }
57
58         int i;
59         GList *l = NULL;
60         for (i = 0; i < len_t; i++) {
61                 if (NULL == t[i] || '\0' == *t[i])
62                         continue;
63                 int num = atoi(t[i]);
64                 if (0 == num) {
65                         /* LCOV_EXCL_START */
66                         ERR("Invalid parameter[%s]", t[i]);
67                         continue;
68                         /* LCOV_EXCL_STOP */
69                 }
70                 if (g_list_find(l, GINT_TO_POINTER(num))) {
71                         /* LCOV_EXCL_START */
72                         ERR("Find same data[%s]", t[i]);
73                         continue;
74                         /* LCOV_EXCL_STOP */
75                 }
76                 l = g_list_append(l, GINT_TO_POINTER(num));
77         }
78         l = g_list_sort(l, _sort_cb);
79
80         int len_src = strlen(src) +1;
81         char *out_str = calloc(len_src, sizeof(char));
82         if (NULL == out_str) {
83                 /* LCOV_EXCL_START */
84                 ERR("calloc() Fail");
85                 g_list_free(l);
86                 g_strfreev(t);
87                 return NULL;
88                 /* LCOV_EXCL_STOP */
89         }
90         int len = 0;
91         GList *cursor = NULL;
92         cursor = g_list_first(l);
93         while (cursor) {
94                 len += snprintf(out_str +len, len_src -len, "%d,", GPOINTER_TO_INT(cursor->data));
95                 cursor = g_list_next(cursor);
96         }
97         if (out_str)
98                 out_str[len -1] = '\0';
99
100         g_list_free(l);
101         g_strfreev(t);
102
103         return out_str;
104 }