From cd33855fc5c2905d468836489583389086adb267 Mon Sep 17 00:00:00 2001 From: Radoslaw Czerski Date: Fri, 5 Aug 2016 11:42:23 +0200 Subject: [PATCH] [clock]Utils added. Change-Id: Ib55d4081b92b72e1fd88bdacc98403ef59ec1075 Signed-off-by: Radoslaw Czerski --- clock/inc/Utils.h | 57 ++++++++++++++++++++++++++++++++++++ clock/project_def.prop | 2 +- clock/src/Utils.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++ common/inc/Utils/Utils.h | 57 ++++++++++++++++++++++++++++++++++++ common/project_def.prop | 2 +- common/src/Utils/Utils.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 262 insertions(+), 2 deletions(-) create mode 100644 clock/inc/Utils.h create mode 100644 clock/src/Utils.cpp create mode 100644 common/inc/Utils/Utils.h create mode 100644 common/src/Utils/Utils.cpp diff --git a/clock/inc/Utils.h b/clock/inc/Utils.h new file mode 100644 index 0000000..8b9fb7e --- /dev/null +++ b/clock/inc/Utils.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _CLOCK_UTILS_H_ +#define _CLOCK_UTILS_H_ + + +namespace common { + namespace utils { + + class Utils { + public: + /* + * @brief Application sub-directories type. + */ + enum AppSubdirectory { + APP_DIR_DATA, + APP_DIR_CACHE, + APP_DIR_RESOURCE, + APP_DIR_SHARED_DATA, + APP_DIR_SHARED_RESOURCE, + APP_DIR_SHARED_TRUSTED, + APP_DIR_EXTERNAL_DATA, + APP_DIR_EXTERNAL_CACHE, + APP_DIR_EXTERNAL_SHARED_DATA, + }; + + /** + * @brief Returns absolute path to resource file located in applications directory. + * + * @param dir type of subdirectory + * @param relative path of resource from starting from given sub dir. + * eg. for APP_DATA_DIR subdir and relative "database.db" => "/home/owner/apps/org.tizen.clock/data/database.db" + * @return absolute path string. + * + * @note returns statically allocated string + */ + static const char *GetAppResourcePath(enum AppSubdirectory dir, const char *relative); + }; + } +} + + +#endif /* _CLOCK_UTILS_H_ */ diff --git a/clock/project_def.prop b/clock/project_def.prop index 018afeb..8af2a10 100644 --- a/clock/project_def.prop +++ b/clock/project_def.prop @@ -9,7 +9,7 @@ type = app profile = mobile-3.0 # C Sources -USER_SRCS = src/clock.c +USER_SRCS = src/Clock.c src/Utils.cpp USER_SRCS_ABS = # EDC Sources diff --git a/clock/src/Utils.cpp b/clock/src/Utils.cpp new file mode 100644 index 0000000..a1f3758 --- /dev/null +++ b/clock/src/Utils.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Utils.h" + +#include +#include +#include +#include +#include +#include + +using namespace common::utils; + +const char *Utils::GetAppResourcePath(enum AppSubdirectory dir, const char *relative) +{ + static char buf[PATH_MAX]; + char *prefix; + + switch (dir) { + case APP_DIR_DATA: + prefix = app_get_data_path(); + break; + case APP_DIR_CACHE: + prefix = app_get_cache_path(); + break; + case APP_DIR_RESOURCE: + prefix = app_get_resource_path(); + break; + case APP_DIR_SHARED_DATA: + prefix = app_get_shared_data_path(); + break; + case APP_DIR_SHARED_RESOURCE: + prefix = app_get_shared_resource_path(); + break; + case APP_DIR_SHARED_TRUSTED: + prefix = app_get_shared_trusted_path(); + break; + case APP_DIR_EXTERNAL_DATA: + prefix = app_get_external_data_path(); + break; + case APP_DIR_EXTERNAL_CACHE: + prefix = app_get_external_cache_path(); + break; + case APP_DIR_EXTERNAL_SHARED_DATA: + prefix = app_get_external_shared_data_path(); + break; + default: + dlog_print(DLOG_FATAL, LOG_TAG, "Not handled directory type."); + return NULL; + } + size_t res = eina_file_path_join(buf, sizeof(buf), prefix, relative); + free(prefix); + if (res > sizeof(buf)) { + dlog_print(DLOG_ERROR, LOG_TAG, "Path exceeded PATH_MAX"); + return NULL; + } + + return &buf[0]; +} diff --git a/common/inc/Utils/Utils.h b/common/inc/Utils/Utils.h new file mode 100644 index 0000000..8b9fb7e --- /dev/null +++ b/common/inc/Utils/Utils.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _CLOCK_UTILS_H_ +#define _CLOCK_UTILS_H_ + + +namespace common { + namespace utils { + + class Utils { + public: + /* + * @brief Application sub-directories type. + */ + enum AppSubdirectory { + APP_DIR_DATA, + APP_DIR_CACHE, + APP_DIR_RESOURCE, + APP_DIR_SHARED_DATA, + APP_DIR_SHARED_RESOURCE, + APP_DIR_SHARED_TRUSTED, + APP_DIR_EXTERNAL_DATA, + APP_DIR_EXTERNAL_CACHE, + APP_DIR_EXTERNAL_SHARED_DATA, + }; + + /** + * @brief Returns absolute path to resource file located in applications directory. + * + * @param dir type of subdirectory + * @param relative path of resource from starting from given sub dir. + * eg. for APP_DATA_DIR subdir and relative "database.db" => "/home/owner/apps/org.tizen.clock/data/database.db" + * @return absolute path string. + * + * @note returns statically allocated string + */ + static const char *GetAppResourcePath(enum AppSubdirectory dir, const char *relative); + }; + } +} + + +#endif /* _CLOCK_UTILS_H_ */ diff --git a/common/project_def.prop b/common/project_def.prop index 065295b..1ce9240 100644 --- a/common/project_def.prop +++ b/common/project_def.prop @@ -35,7 +35,7 @@ USER_OBJS_ABS = # User Includes ## C Compiler -USER_INC_DIRS = inc +USER_INC_DIRS = inc inc/Utils USER_INC_DIRS_ABS = USER_INC_FILES = USER_INC_FILES_ABS = diff --git a/common/src/Utils/Utils.cpp b/common/src/Utils/Utils.cpp new file mode 100644 index 0000000..a1f3758 --- /dev/null +++ b/common/src/Utils/Utils.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Utils.h" + +#include +#include +#include +#include +#include +#include + +using namespace common::utils; + +const char *Utils::GetAppResourcePath(enum AppSubdirectory dir, const char *relative) +{ + static char buf[PATH_MAX]; + char *prefix; + + switch (dir) { + case APP_DIR_DATA: + prefix = app_get_data_path(); + break; + case APP_DIR_CACHE: + prefix = app_get_cache_path(); + break; + case APP_DIR_RESOURCE: + prefix = app_get_resource_path(); + break; + case APP_DIR_SHARED_DATA: + prefix = app_get_shared_data_path(); + break; + case APP_DIR_SHARED_RESOURCE: + prefix = app_get_shared_resource_path(); + break; + case APP_DIR_SHARED_TRUSTED: + prefix = app_get_shared_trusted_path(); + break; + case APP_DIR_EXTERNAL_DATA: + prefix = app_get_external_data_path(); + break; + case APP_DIR_EXTERNAL_CACHE: + prefix = app_get_external_cache_path(); + break; + case APP_DIR_EXTERNAL_SHARED_DATA: + prefix = app_get_external_shared_data_path(); + break; + default: + dlog_print(DLOG_FATAL, LOG_TAG, "Not handled directory type."); + return NULL; + } + size_t res = eina_file_path_join(buf, sizeof(buf), prefix, relative); + free(prefix); + if (res > sizeof(buf)) { + dlog_print(DLOG_ERROR, LOG_TAG, "Path exceeded PATH_MAX"); + return NULL; + } + + return &buf[0]; +} -- 2.7.4