tizen 2.3 release
[apps/livebox/livebox-viewer.git] / dynamicbox_viewer / src / util.c
1 /*
2  * Copyright 2013  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.1 (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/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 #include <stdio.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <sys/time.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <time.h>
24
25 #include <dlog.h>
26 #include <dynamicbox_errno.h> /* For error code */
27
28 #include "debug.h"
29 #include "util.h"
30
31 int errno;
32 #if defined(_USE_ECORE_TIME_GET)
33 static struct {
34     clockid_t type;
35 } s_info = {
36     .type = CLOCK_MONOTONIC,
37 };
38 #endif
39
40 int util_check_extension(const char *filename, const char *check_ptr)
41 {
42     int name_len;
43
44     name_len = strlen(filename);
45     while (--name_len >= 0 && *check_ptr) {
46         if (filename[name_len] != *check_ptr) {
47             return DBOX_STATUS_ERROR_INVALID_PARAMETER;
48         }
49
50         check_ptr ++;
51     }
52
53     return 0;
54 }
55
56 double util_timestamp(void)
57 {
58 #if defined(_USE_ECORE_TIME_GET)
59     struct timespec ts;
60
61     do {
62         if (clock_gettime(s_info.type, &ts) == 0) {
63             return ts.tv_sec + ts.tv_nsec / 1000000000.0f;
64         }
65
66         ErrPrint("%d: %s\n", s_info.type, strerror(errno));
67         if (s_info.type == CLOCK_MONOTONIC) {
68             s_info.type = CLOCK_REALTIME;
69         } else if (s_info.type == CLOCK_REALTIME) {
70             struct timeval tv;
71             if (gettimeofday(&tv, NULL) < 0) {
72                 ErrPrint("gettimeofday: %s\n", strerror(errno));
73                 break;
74             }
75
76             return tv.tv_sec + tv.tv_usec / 1000000.0f;
77         }
78     } while (1);
79
80     return 0.0f;
81 #else
82     struct timeval tv;
83
84     if (gettimeofday(&tv, NULL) < 0) {
85         ErrPrint("gettimeofday: %s\n", strerror(errno));
86         tv.tv_sec = 0;
87         tv.tv_usec = 0;
88     }
89
90     return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0f;
91 #endif
92 }
93
94 const char *util_basename(const char *name)
95 {
96     int length;
97     length = name ? strlen(name) : 0;
98     if (!length) {
99         return ".";
100     }
101
102     while (--length > 0 && name[length] != '/');
103
104     return length <= 0 ? name : name + length + (name[length] == '/');
105 }
106
107 const char *util_uri_to_path(const char *uri)
108 {
109     int len;
110
111     len = strlen(SCHEMA_FILE);
112     if (strncasecmp(uri, SCHEMA_FILE, len)) {
113         return NULL;
114     }
115
116     return uri + len;
117 }
118
119 int util_unlink(const char *filename)
120 {
121     char *descfile;
122     int desclen;
123     int ret;
124
125     if (!filename) {
126         return DBOX_STATUS_ERROR_INVALID_PARAMETER;
127     }
128
129     desclen = strlen(filename) + 6; /* .desc */
130     descfile = malloc(desclen);
131     if (!descfile) {
132         ErrPrint("Heap: %s\n", strerror(errno));
133         return DBOX_STATUS_ERROR_OUT_OF_MEMORY;
134     }
135
136     ret = snprintf(descfile, desclen, "%s.desc", filename);
137     if (ret < 0) {
138         ErrPrint("Error: %s\n", strerror(errno));
139         free(descfile);
140         return DBOX_STATUS_ERROR_FAULT;
141     }
142
143     (void)unlink(descfile);
144     free(descfile);
145     (void)unlink(filename);
146
147     return DBOX_STATUS_ERROR_NONE;
148 }
149
150 /* End of a file */