2 * Copyright 2013 Samsung Electronics Co., Ltd
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
8 * http://floralicense.org
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.
22 #include <sys/types.h>
32 #include <livebox-errno.h>
34 #include "critical_log.h"
39 HAPI int util_check_ext(const char *icon, const char *ext)
43 len = strlen(icon) - 1;
44 while (len >= 0 && *ext && icon[len] == *ext) {
52 HAPI int util_get_filesize(const char *filename)
56 if (stat(filename, &buf) < 0) {
57 ErrPrint("error: %s\n", strerror(errno));
58 return LB_STATUS_ERROR_IO;
61 if (!S_ISREG(buf.st_mode)) {
62 ErrPrint("%s is not a file\n", filename);
63 return LB_STATUS_ERROR_INVALID;
69 HAPI double util_timestamp(void)
73 gettimeofday(&tv, NULL);
75 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
78 HAPI const char *util_basename(const char *name)
82 length = name ? strlen(name) : 0;
86 while (--length > 0 && name[length] != '/');
88 return length <= 0 ? name : name + length + (name[length] == '/');
93 * Just trying to find the nearest module.
96 HAPI char *util_get_current_module(char **symbol)
103 unsigned int stack_boundary = 0;
104 unsigned int stack_size = 0;
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);
114 for (i = 0, stack = (int *)&stack; (unsigned int)stack < stack_boundary ; stack++, i++) {
115 if (!dladdr((void *)*stack, &dinfo))
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);
122 if (!strcmp(ptr, EXEC_NAME))
133 *symbol = strdup(dinfo.dli_sname);
143 HAPI const char *util_uri_to_path(const char *uri)
147 len = strlen(SCHEMA_FILE);
148 if (strncasecmp(uri, SCHEMA_FILE, len))
154 HAPI double util_time_delay_for_compensation(double period)
157 unsigned long long curtime;
158 unsigned long long _period;
159 unsigned long long remain;
164 gettimeofday(&tv, NULL);
165 curtime = (unsigned long long)tv.tv_sec * 1000000llu + (unsigned long long)tv.tv_usec;
167 sec = (unsigned int)period;
168 usec = (period - sec) * 1000000;
169 _period = (unsigned long long)sec * 1000000llu + usec;
171 remain = curtime % _period;
173 sec = (unsigned int)(remain / 1000000llu);
174 usec = (unsigned int)(remain % 1000000llu);
176 ret = (double)sec + (double)usec / 1000000.0f;
180 HAPI void *util_timer_add(double interval, Eina_Bool (*cb)(void *data), void *data)
185 timer = ecore_timer_add(interval, cb, data);
189 delay = util_time_delay_for_compensation(interval) - interval;
190 ecore_timer_delay(timer, delay);
191 DbgPrint("Compensate timer: %lf\n", delay);
196 HAPI void util_timer_interval_set(void *timer, double interval)
199 ecore_timer_interval_set(timer, interval);
201 delay = util_time_delay_for_compensation(interval) - interval;
202 ecore_timer_delay(timer, delay);