2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file wrt_utility.cpp
19 * @author Wei Dong(d.wei@samsung.com)
20 * @author Ma Quan(jason.ma@samsung.com)
21 * @brief This file implemented some common functions for widget manager
35 #include <sys/types.h>
37 #include <dpl/log/log.h>
38 #include <wrt_utility.h>
42 bool _WrtUtilSetAbsolutePath(char* absolutePath,
43 const char* parentPath,
47 if (NULL == absolutePath || NULL == parentPath || NULL == fileName) {
50 len = strlen(parentPath);
52 // not check the valid of parentPath and fileName.
53 if (parentPath[len - 1] == '/') {
54 snprintf(absolutePath,
55 MAX_WIDGET_PATH_LENGTH,
60 snprintf(absolutePath,
61 MAX_WIDGET_PATH_LENGTH,
67 LogDebug("The parent path is null");
71 //some widget use Windows notation. We need to change '\' to '/'
72 for (int i = 0; absolutePath[i] != 0; ++i) {
73 if (absolutePath[i] == '\\') {
74 absolutePath[i] = '/';
81 // KW bool _WrtUtilConvertStrToBool(char* value, bool *result)
83 // KW bool ret = false;
84 // KW if (NULL == value || NULL == result)
89 // KW char* source = value;
90 // KW char* changed = (char*)malloc(strlen(value) + 1);
91 // KW if (NULL == changed)
95 // KW memset(changed, 0, strlen(value) + 1);
97 // KW char* cur = changed;
100 // KW *cur++ = tolower(*source++);
102 // KW if (!strcmp(changed,"false") || !strcmp(changed,"0"))
104 // KW *result = false;
107 // KW else if(!strcmp(changed,"true") || !strcmp(changed,"1"))
109 // KW *result = true;
117 void _WrtUtilGetDirAndFileName(const char* fullPath,
123 if (NULL == fullPath || (NULL == dirName && NULL == fileName)) {
127 length = strlen(fullPath);
128 for (index = length - 1; index >= 0; index--) {
129 if ('/' == fullPath[index]) {
130 if (index == length - 1) {
131 LogDebug(" Warning: The end of directroy is '/'! ");
133 *dirName = (char*)malloc(sizeof(char) * (length + 1));
134 if (*dirName != NULL) {
135 memset(*dirName, 0, sizeof(char) * (length + 1));
136 strncpy(*dirName, fullPath, length);
146 int dirName_len = index + 2;
148 *dirName = (char*)malloc(sizeof(char) * dirName_len);
149 if (*dirName != NULL) {
150 memset(*dirName, 0, sizeof(char) * dirName_len);
151 strncpy(*dirName, fullPath, dirName_len - 1);
156 int fileName_len = length - index;
158 *fileName = (char*)malloc(sizeof(char) * fileName_len);
159 if (*fileName != NULL) {
160 memset(*fileName, 0, sizeof(char) * fileName_len);
161 strncpy(*fileName, &fullPath[index + 1], fileName_len - 1);
166 *fileName = (char*)malloc(sizeof(char) * (length + 1));
167 if (*fileName != NULL) {
168 memset(*fileName, 0, sizeof(char) * (length + 1));
169 strncpy(*fileName, fullPath, length);
175 // KW bool _WrtUtilStringCmp(const char* srcStr, const char* destStr)
177 // KW bool ret = false;
178 // KW char* strString = NULL;
179 // KW char* destString = NULL;
181 // KW if (NULL == srcStr || NULL == destStr )
186 // KW _WrtUtilStringToLower(srcStr, &strString);
187 // KW _WrtUtilStringToLower(destStr,&destString);
189 // KW if(!strcmp(strString, destString))
194 // KW free(strString);
195 // KW free(destString);
200 // check it deeply later.
201 bool _WrtMakeDir (const char *path,
209 const int defaultMode = 0777;
210 if (!(flags & WRT_FILEUTILS_RECUR)) {
211 if (mkdir(path, defaultMode) < 0) {
212 LogDebug("Failed to make dir " << path);
215 if (mode != -1 && chmod(path, mode) < 0) {
216 LogDebug("Failed to chmod");
222 if (stat(path, &st) < 0 && errno == ENOENT) {
232 parent = dirname(buf);
233 //ret = _WrtMakeDir(parent, (defaultMode & ~mask) | 0300, WRT_FILEUTILS_RECUR);
234 ret = _WrtMakeDir(parent, (defaultMode) | 0300, WRT_FILEUTILS_RECUR);
237 if ((!ret) || (!_WrtMakeDir(path, mode, 0))) {
238 LogDebug("Failed to _WrtMakeDir");
247 bool _WrtUtilChangeDir(const char* path)
252 if (-1 == chdir(path)) {
253 if (ENOENT == errno) {
254 if (!_WrtMakeDir(path, 0664, WRT_FILEUTILS_RECUR)) {
257 if (-1 == chdir(path)) {
269 bool _WrtUtilRemoveDir(const char* path)
272 struct dirent* ptr = NULL;
273 char childPath[MAX_WIDGET_PATH_LENGTH + 1] = { 0 };
275 LogWarning("Path is null");
280 while ((ptr = readdir(dir)) != NULL) {
281 if ((!strcmp(ptr->d_name, ".")) || (!strcmp(ptr->d_name, ".."))) {
284 int len = strlen(path);
285 if (path[len - 1] == '/') {
287 MAX_WIDGET_PATH_LENGTH,
293 MAX_WIDGET_PATH_LENGTH,
298 if (ptr->d_type == DT_DIR) {
299 if (!_WrtUtilRemoveDir(childPath)) {
304 if (unlink(childPath) != 0) {
306 LogWarning("Failed to remove file " << childPath);
312 } else if (errno == ENOTDIR) {
313 if (unlink(path) != 0) {
314 LogWarning("Failed to remove file " << path);
318 } else if (errno == ENOENT) { //not exist
319 LogWarning("Cannot remove non existent directory " << path);
322 LogWarning("Can't remove directory " << path);
325 if (rmdir(path) != 0) {
326 LogWarning("Removing directory failed :" << path << " errno: " << errno);
334 _WrtUtilStringToLower(const char* str,
337 if (!str || !lowerStr) {
342 int length = strlen(str);
344 *lowerStr = (char*)malloc(length + 1);
350 memset(*lowerStr, 0, length + 1);
351 strncpy(*lowerStr, str, length);
355 while (*str != '\0') {
356 *cur++ = tolower(*str++);