74037fe543b6851733bac1838b483b1eb1a3d477
[platform/core/api/maps-service.git] / src / maps_util.h
1 /* Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef __MAPS_UTIL_H__
17 #define __MAPS_UTIL_H__
18
19 #include <dlog.h>
20 #include <glib.h>
21
22 /*#include "stdio.h"
23
24 #ifndef EXPORT_API
25 #define EXPORT_API __attribute__((__visibility__("default")))
26 #endif*/
27
28 #ifdef LOG_TAG
29 #undef LOG_TAG
30 #endif
31 #define LOG_TAG "CAPI_MAPS_SERVICE"
32
33 #define MAPS_BASE_ID_MAX_LEN    128
34 #define MAPS_BASE_NAME_MAX_LEN  128
35 #define MAPS_BASE_URL_MAX_LEN   512
36 #define MAPS_BASE_DESC_MAX_LEN  1024
37 #define MAPS_BASE_TYPE_MAX_LEN  64
38 #define MAPS_BASE_DATE_MAX_LEN  32
39 #define MAPS_PLUGINS_PATH_PREFIX        LIBDIR"/maps/plugins"
40
41 /*
42 * Internal Macros
43 */
44 #define MAPS_LOGD(fmt, args...)  LOGD(fmt, ##args)
45 #define MAPS_LOGW(fmt, args...)  LOGW(fmt, ##args)
46 #define MAPS_LOGI(fmt, args...)  LOGI(fmt, ##args)
47 #define MAPS_LOGE(fmt, args...)  LOGE(fmt, ##args)
48 #define MAPS_SECLOG(fmt, args...)  SECURE_LOGD(fmt, ##args)
49
50 #define MAPS_CHECK_CONDITION(condition, error, msg)     \
51         do { \
52                 if (condition) { \
53                 } else { \
54                         MAPS_LOGE("%s(0x%08x)", msg, error); \
55                         return error; \
56                 } \
57         } while (0)
58
59 #define MAPS_NULL_ARG_CHECK_RETURN_FALSE(arg)\
60         do { \
61                 if (arg != NULL) { \
62                 } else  { \
63                         MAPS_LOGE("MAPS_ERROR_INVALID_PARAMETER");  \
64                         return false; };        \
65         } while (0)
66
67 #define MAPS_NULL_ARG_CHECK(arg)        \
68         MAPS_CHECK_CONDITION(arg != NULL, MAPS_ERROR_INVALID_PARAMETER, "MAPS_ERROR_INVALID_PARAMETER")
69
70 #define MAPS_PRINT_ERROR_CODE_RETURN(code) \
71         do { \
72                 MAPS_LOGE("%s(0x%08x)", #code, code); \
73                 return code;    \
74         } while (0)
75
76 #undef VERSION
77 #define VERSION(a, b, c)        (((a) << 16) + ((b) << 8) + (c))
78
79 /*----------------------------------------------------------------------------*/
80
81 #define _S(s) #s
82
83 /*
84  * @brief       Copies one string to another and releases receiver if needed.
85  * @details This function copies one string to another and releases receiver if
86  * needed.
87  * @since_tizen @if MOBILE 2.4 @elseif WEARABLE 2.3.2 @endif
88  *
89  * @param[out]  dst             The destination string pointer.
90  * @param[in]   src             The original string pointer.
91  * @param[in]   max_length      The maximum size of bytes to be copied.
92  * @return      0 on success, otherwise a negative error value.
93  * @retval      #MAPS_ERROR_NONE Successful
94  * @retval      #MAPS_ERROR_INVALID_PARAMETER Invalid parameter
95  */
96 int maps_set_string(const char *src, const int max_length, char **dst);
97
98 /*
99  * @brief       Copies one string to another.
100  * @details This function copies one string to another.
101  * @since_tizen @if MOBILE 2.4 @elseif WEARABLE 2.3.2 @endif
102  *
103  * @param[out]  dst             The destination string pointer.
104  * @param[in]   src             The original string pointer.
105  * @param[in]   max_length      The maximum size of bytes to be copied.
106  * @return      0 on success, otherwise a negative error value.
107  * @retval      #MAPS_ERROR_NONE Successful
108  * @retval      #MAPS_ERROR_INVALID_PARAMETER Invalid parameter
109  */
110 int maps_get_string(const char *src, const int max_length, char **dst);
111
112 /**
113  * @brief       Get the screen dpi of current device.
114  * @details This function gets the screen dpi of current device.
115  *
116  * @param[out]  dpi             The screen dpi of current device.
117  * @return      0 on success, otherwise a negative error value
118  * @retval      #MAPS_ERROR_NONE Successful
119  * @retval      #MAPS_ERROR_INVALID_PARAMETER Invalid parameter
120  */
121 int maps_get_screen_dpi(int *dpi);
122
123
124 /* Prevent utility highlights defects in std::vector and std::string, so
125 *  simplified versions of that classes are implemented */
126 template <class T> class vector {
127 private:
128         GArray *parray;
129         unsigned int current_size;
130 public:
131         vector() : parray(NULL), current_size(0)
132         {
133                 parray = g_array_new(false, false, sizeof(T *));
134         }
135         ~vector()
136         {
137                 /*if (!parray || (current_size == 0))*/
138                 if (!parray)
139                         return;
140                 for (unsigned int i = 0; i < current_size; i++) {
141                         T *item = g_array_index(parray, T *, i);
142                         if (item)
143                                 delete item;
144                 }
145                 g_array_free(parray, TRUE);
146                 parray = NULL;
147                 current_size = 0;
148         }
149 public:
150         void push_back(const T &value)
151         {
152                 T *clone = new T(value);
153                 if (clone) {
154                         g_array_append_val(parray, clone);
155                         current_size++;
156                 }
157         }
158         T &operator[](size_t idx) const
159         {
160                 T *item = g_array_index(parray, T *, idx);
161                 return *item;
162         }
163         unsigned int size() const
164         {
165                 return current_size;
166         }
167         bool empty() const
168         {
169                 return size() == 0;
170         }
171 };
172
173 class string
174 {
175 private:
176         char *pstring;
177 public:
178         string() : pstring(NULL)
179         {
180         }
181         string(const char *pstr) : pstring(NULL)
182         {
183                 pstring = g_strdup(pstr);
184         }
185         string(const string &s) : pstring(NULL)
186         {
187                 pstring = g_strdup(s.pstring);
188         }
189         ~string()
190         {
191                 g_free(pstring);
192                 pstring = NULL;
193         }
194 public:
195         string &operator=(const string &s)
196         {
197                 if (this != &s) {
198                         g_free(pstring);
199                         pstring = g_strdup(s.pstring);
200                 }
201                 return *this;
202         }
203         bool operator==(const string &s) const
204         {
205                 return g_strcmp0(pstring, s.pstring) == 0;
206         }
207         bool operator!=(const string &s) const
208         {
209                 return !(*this == s);
210         }
211 public:
212         char *c_str() const
213         {
214                 return pstring;
215         }
216         bool empty() const
217         {
218                 return !pstring || (*this == string(""));
219         }
220 };
221
222 typedef enum {
223         TIZEN_PROFILE_UNKNOWN = 0,
224         TIZEN_PROFILE_MOBILE = 0x1,
225         TIZEN_PROFILE_WEARABLE = 0x2,
226         TIZEN_PROFILE_TV = 0x4,
227         TIZEN_PROFILE_IVI = 0x8,
228         TIZEN_PROFILE_COMMON = 0x10,
229 } tizen_profile_t;
230 extern tizen_profile_t _get_tizen_profile();
231 #endif                          /* __MAPS_UTIL_H__ */