1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
#include <libaksettings.h>
#include <libaklog.h>
#include <stdio.h>
static int test_ak_settings_read_example()
{
printf("Testing: %s\n", __func__);
if (!ak_settings_load_settings_binary())
{
ak_log_warning(__func__, "No existing settings or error loading.\n");
}
const char *bindir = ak_settings_get_setting("AK_BINDIR");
if (bindir)
{
char *some_text;
asprintf(&some_text, "Current bin directory: %s\n", bindir);
ak_log_info(__func__, some_text);
}
ak_settings_free_settings();
return 0;
}
static int test_ak_settings_read_write_example()
{
printf("Testing: %s\n", __func__);
if (!ak_settings_load_settings_binary())
{
ak_log_warning(__func__, "No existing settings or error loading.\n");
}
ak_settings_import_from_environment();
ak_settings_set_setting("username", "john_doe");
ak_settings_set_setting("theme", "dark");
ak_settings_set_setting("volume", "75");
ak_settings_set_setting("theme", "light");
if (!ak_settings_save_settings_binary())
{
printf("Error saving settings!\n");
ak_settings_free_settings();
return 1;
}
const char *theme = ak_settings_get_setting("theme");
if (theme)
{
char *some_text;
asprintf(&some_text, "Current theme: %s\n", theme);
ak_log_info(__func__, some_text);
}
const char *bindir = ak_settings_get_setting("AK_BINDIR");
if (bindir)
{
char *some_text;
asprintf(&some_text, "Current bin directory: %s\n", bindir);
ak_log_info(__func__, some_text);
}
ak_settings_free_settings();
return 0;
}
static int test_ak_settings()
{
char *some_text;
if ( test_ak_settings_read_write_example() == 0 )
{
asprintf(&some_text, "Passed test");
ak_log_info(__func__, some_text);
return 0;
}
else
{
asprintf(&some_text, "Failed test");
ak_log_error(__func__, some_text);
return 1;
}
}
static void test_import()
{
ak_settings_import_from_environment();
}
static int test_read_and_dump()
{
printf("Testing: %s\n", __func__);
if (!ak_settings_load_settings_binary())
{
ak_log_warning(__func__, "No existing settings or error loading.\n");
}
AKSetting* ak_settings;
ak_settings = ak_settings_get_all();
for ( int i = 0; ak_settings[i].key != NULL; ++i )
{
printf("%s=%s\n", ak_settings[i].key, ak_settings[i].value);
}
ak_settings_free_settings();
return 0;
}
int main()
{
test_import();
test_ak_settings();
test_ak_settings_read_example();
test_read_and_dump();
return 0;
}
|