afd26efca87827b9a951a74b5d52f61d4933dc23
[platform/framework/web/data-provider-slave.git] / src / util.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.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
7  *
8  * http://floralicense.org
9  *
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.
15  */
16
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <sys/time.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <dlfcn.h>
25 #include <pthread.h>
26
27 #include <Eina.h>
28 #include <Ecore.h>
29
30 #include <dlog.h>
31 #include <aul.h>
32 #include <livebox-errno.h>
33
34 #include "critical_log.h"
35 #include "util.h"
36 #include "conf.h"
37 #include "debug.h"
38
39 HAPI int util_check_ext(const char *icon, const char *ext)
40 {
41         int len;
42
43         len = strlen(icon) - 1;
44         while (len >= 0 && *ext && icon[len] == *ext) {
45                 len--;
46                 ext++;
47         }
48
49         return *ext ? 0 : 1;
50 }
51
52 HAPI int util_get_filesize(const char *filename)
53 {
54         struct stat buf;
55
56         if (stat(filename, &buf) < 0) {
57                 ErrPrint("error: %s\n", strerror(errno));
58                 return LB_STATUS_ERROR_IO;
59         }
60
61         if (!S_ISREG(buf.st_mode)) {
62                 ErrPrint("%s is not a file\n", filename);
63                 return LB_STATUS_ERROR_INVALID;
64         }
65
66         return buf.st_size;
67 }
68
69 HAPI double util_timestamp(void)
70 {
71         struct timeval tv;
72
73         gettimeofday(&tv, NULL);
74
75         return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
76 }
77
78 HAPI const char *util_basename(const char *name)
79 {
80         int length;
81
82         length = name ? strlen(name) : 0;
83         if (!length)
84                 return ".";
85
86         while (--length > 0 && name[length] != '/');
87
88         return length <= 0 ? name : name + length + (name[length] == '/');
89 }
90
91 /*!
92  * \note
93  * Just trying to find the nearest module.
94  * It could be wrong.
95  */
96 HAPI char *util_get_current_module(char **symbol)
97 {
98         int *stack;
99         Dl_info dinfo;
100         const char *ptr;
101         char *ret;
102         pthread_attr_t attr;
103         unsigned int stack_boundary = 0;
104         unsigned int stack_size = 0;
105         register int i;
106
107         if (!pthread_getattr_np(pthread_self(), &attr)) {
108                 if (!pthread_attr_getstack(&attr, (void *)&stack_boundary, &stack_size))
109                         stack_boundary += stack_size;
110                 pthread_attr_destroy(&attr);
111         }
112
113         ret = NULL;
114         for (i = 0, stack = (int *)&stack; (unsigned int)stack < stack_boundary ; stack++, i++) {
115                 if (!dladdr((void *)*stack, &dinfo))
116                         continue;
117
118                 ptr = util_basename(dinfo.dli_fname);
119                 if (strncmp(ptr, "liblive-", strlen("liblive-"))) {
120                         DbgPrint("[%d] fname[%s] symbol[%s]\n", i, dinfo.dli_fname, dinfo.dli_sname);
121                         /*
122                         if (!strcmp(ptr, EXEC_NAME))
123                                 return EXEC_NAME;
124                         */
125
126                         continue;
127                 }
128
129                 ret = strdup(ptr);
130
131                 if (symbol) {
132                         if (dinfo.dli_sname)
133                                 *symbol = strdup(dinfo.dli_sname);
134                         else
135                                 *symbol = NULL;
136                 }
137                 break;
138         }
139
140         return ret;
141 }
142
143 HAPI const char *util_uri_to_path(const char *uri)
144 {
145         int len;
146
147         len = strlen(SCHEMA_FILE);
148         if (strncasecmp(uri, SCHEMA_FILE, len))
149                 return NULL;
150
151         return uri + len;
152 }
153
154 HAPI double util_time_delay_for_compensation(double period)
155 {
156         struct timeval tv;
157         unsigned long long curtime;
158         unsigned long long _period;
159         unsigned long long remain;
160         unsigned int sec;
161         unsigned int usec;
162         double ret;
163
164         gettimeofday(&tv, NULL);
165         curtime = (unsigned long long)tv.tv_sec * 1000000llu + (unsigned long long)tv.tv_usec;
166
167         sec = (unsigned int)period;
168         usec = (period - sec) * 1000000;
169         _period = (unsigned long long)sec * 1000000llu + usec;
170
171         remain = curtime % _period;
172
173         sec = (unsigned int)(remain / 1000000llu);
174         usec = (unsigned int)(remain % 1000000llu);
175
176         ret = (double)sec + (double)usec / 1000000.0f;
177         return period - ret;
178 }
179
180 HAPI void *util_timer_add(double interval, Eina_Bool (*cb)(void *data), void *data)
181 {
182         Ecore_Timer *timer;
183         double delay;
184
185         timer = ecore_timer_add(interval, cb, data);
186         if (!timer)
187                 return NULL;
188
189         delay = util_time_delay_for_compensation(interval) - interval;
190         ecore_timer_delay(timer, delay);
191         DbgPrint("Compensate timer: %lf\n", delay);
192
193         return timer;
194 }
195
196 HAPI void util_timer_interval_set(void *timer, double interval)
197 {
198         double delay;
199         ecore_timer_interval_set(timer, interval);
200
201         delay = util_time_delay_for_compensation(interval) - interval;
202         ecore_timer_delay(timer, delay);
203 }
204
205 /* End of a file */