aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_devices/config.cpp
blob: e2abd6bbb4b51b51a317c6436d14115302d87be0 (plain) (blame)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "main.h"
#include "./config.h"

#include <strsafe.h>

#define CONFIG_SUFFIX		L"Plugins\\ml"
#define CONFIG_FILE			L"ml_devices.ini"

static const char *
Config_GetPath(BOOL ensureExist)
{
	static const char *configPath = NULL;
	if (NULL == configPath)
	{
		const wchar_t *userPath;
		wchar_t buffer[MAX_PATH * 2] = {0};

		if (NULL == WASABI_API_APP)
			return NULL;

		userPath = WASABI_API_APP->path_getUserSettingsPath();
		if (NULL == userPath)
			return NULL;
		
		if (0 != PathCombine(buffer, userPath, CONFIG_SUFFIX))
		{
			if ((FALSE == ensureExist || SUCCEEDED(Plugin_EnsurePathExist(buffer))) &&
				FALSE != PathAppend(buffer,CONFIG_FILE))
			{
				configPath = String_ToAnsi(CP_UTF8, 0, buffer, -1, NULL, NULL);
			}
		}
	}

	return configPath;
}


unsigned long 
Config_ReadString(const char *section,  const char *key,  const char *defaultValue, char *returnedString, unsigned long size)
{
	return GetPrivateProfileStringA(section, key, defaultValue, returnedString, size, Config_GetPath(FALSE));
}

unsigned int
Config_ReadInt(const char *section, const char *key, int defaultValue)
{
	return GetPrivateProfileIntA(section, key, defaultValue, Config_GetPath(FALSE));
}

BOOL
Config_ReadBool(const char *section, const char *key, BOOL defaultValue)
{
	char buffer[32] = {0};
	int length = Config_ReadString(section, key, NULL, buffer, ARRAYSIZE(buffer));
	
	if (0 == length) 
		return defaultValue;
	
	if (1 == length)
	{
		switch(*buffer)
		{
			case '0':
			case 'n':
			case 'f':
				return FALSE;
			case '1':
			case 'y':
			case 't':
				return TRUE;
		}
	}
	else
	{
		if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, NORM_IGNORECASE, "yes", -1, buffer, length) ||
			CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, NORM_IGNORECASE, "true", -1, buffer, length))
		{
			return TRUE;
		}

		if (CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, NORM_IGNORECASE, "no", -1, buffer, length) ||
			CSTR_EQUAL == CompareStringA(CSTR_INVARIANT, NORM_IGNORECASE, "false", -1, buffer, length))
		{
			return FALSE;
		}
	}

	if (FALSE != StrToIntExA(buffer, STIF_SUPPORT_HEX,  &length))
		return (0 != length);

	return defaultValue;
}

BOOL
Config_WriteString(const char *section, const char *key, const char *value)
{
	const char *configPath = Config_GetPath(TRUE);
	if (NULL == configPath || '\0' == *configPath) 
		return FALSE;
		
	return (0 != WritePrivateProfileStringA(section, key, value, configPath));
}

BOOL
Config_WriteInt(const char *section, const char *key, int value)
{
	char buffer[32] = {0};
	if (FAILED(StringCchPrintfA(buffer, ARRAYSIZE(buffer), "%d", value)))
		return FALSE;

	return Config_WriteString(section, key, buffer);
}

BOOL
Config_WriteBool(const char *section, const char *key, BOOL value)
{
	return Config_WriteString(section, key, (FALSE != value) ? "yes" : "no"); 
}