2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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 #include "stc-manager-util.h"
19 static int g_debuglog = -1;
21 static void __sync_file_to_disk(const char *path)
24 fp = fopen(path, "a+");
29 STC_LOGD("Sync the file to disk");
33 static GKeyFile *__load_key_file(const char *path)
35 GKeyFile *keyfile = NULL;
38 STC_LOGD("Loading [%s]", path);
40 keyfile = g_key_file_new();
42 if (!g_key_file_load_from_file(keyfile, path, 0, &error)) {
43 STC_LOGD("Unable to load [%s] : %s", path, error->message); //LCOV_EXCL_LINE
44 g_clear_error(&error); //LCOV_EXCL_LINE
45 g_key_file_free(keyfile); //LCOV_EXCL_LINE
46 keyfile = NULL; //LCOV_EXCL_LINE
52 static int __save_key_file(GKeyFile *keyfile, char *path)
59 data = g_key_file_to_data(keyfile, &length, NULL);
61 if (!g_file_set_contents(path, data, length, &error)) {
62 STC_LOGD("Failed to save information : %s", error->message); //LCOV_EXCL_LINE
63 g_error_free(error); //LCOV_EXCL_LINE
64 ret = -EIO; //LCOV_EXCL_LINE
67 __sync_file_to_disk(path);
74 gboolean stc_util_get_config_bool(char *key)
76 char path[MAX_PATH_LENGTH];
80 snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
82 keyfile = __load_key_file(path);
84 keyfile = g_key_file_new();
86 value = g_key_file_get_boolean(keyfile, path, key, NULL);
87 g_key_file_free(keyfile);
92 gchar * stc_util_get_config_str(char *key)
94 char path[MAX_PATH_LENGTH];
98 snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
100 keyfile = __load_key_file(path);
102 keyfile = g_key_file_new();
104 value = g_key_file_get_string(keyfile, path, key, NULL);
105 g_key_file_free(keyfile);
111 int stc_util_get_config_int(char *key)
113 char path[MAX_PATH_LENGTH];
117 snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
119 keyfile = __load_key_file(path);
121 keyfile = g_key_file_new(); //LCOV_EXCL_LINE
124 value = g_key_file_get_integer(keyfile, path, key, NULL);
125 g_key_file_free(keyfile);
131 API void stc_util_set_debuglog(int debuglog)
133 g_debuglog = debuglog;
137 API int stc_util_get_debuglog(void)
139 if (g_debuglog == -1)
140 g_debuglog = stc_util_get_config_int(INFO_DEBUGLOG);
145 void stc_util_initialize_config(void)
147 char path[MAX_PATH_LENGTH];
150 snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
152 keyfile = __load_key_file(path);
154 keyfile = g_key_file_new(); //LCOV_EXCL_LINE
156 g_key_file_set_integer(keyfile, path, INFO_DEBUGLOG, 0);
158 __save_key_file(keyfile, path);