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