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);
44 g_clear_error(&error);
45 g_key_file_free(keyfile);
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);
67 __sync_file_to_disk(path);
73 gboolean stc_util_get_config_bool(char *key)
75 char path[MAX_PATH_LENGTH];
78 snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
80 keyfile = __load_key_file(path);
82 keyfile = g_key_file_new();
84 return g_key_file_get_boolean(keyfile, path, key, NULL);
87 gchar * stc_util_get_config_str(char *key)
89 char path[MAX_PATH_LENGTH];
92 snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
94 keyfile = __load_key_file(path);
96 keyfile = g_key_file_new();
98 return g_key_file_get_string(keyfile, path, key, NULL);
101 int stc_util_get_config_int(char *key)
103 char path[MAX_PATH_LENGTH];
106 snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
108 keyfile = __load_key_file(path);
110 keyfile = g_key_file_new();
112 return g_key_file_get_integer(keyfile, path, key, NULL);
115 API void stc_util_set_debuglog(int debuglog)
117 g_debuglog = debuglog;
120 API int stc_util_get_debuglog(void)
122 if (g_debuglog == -1)
123 g_debuglog = stc_util_get_config_int(INFO_DEBUGLOG);
128 void stc_util_initialize_config(void)
130 char path[MAX_PATH_LENGTH];
133 snprintf(path, sizeof(path), "%s/%s", INFO_STORAGE_DIR, INFO_CONFIG);
135 keyfile = __load_key_file(path);
137 keyfile = g_key_file_new();
139 g_key_file_set_integer(keyfile, path, INFO_DEBUGLOG, 0);
141 __save_key_file(keyfile, path);