aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Library/ml_online/config.cpp
blob: c6e87cb2f023b087642d502c849918cfa74c84b1 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include "main.h"
#include "./config.h"
#include "./api__ml_online.h"

#include <shlwapi.h>
#include <strsafe.h>

#define CONFIG_SUFFIX		L"Plugins\\ml"
#define CONFIG_SECTION		"ml_online_config"

void C_Config::Flush(void)
{
#ifndef C_CONFIG_WIN32NATIVE
  if (!m_dirty) return;
  FILE *fp=fopen(m_inifile,"wt");
  if (!fp) return;

  fprintf(fp,"[ml_online_config]\n");
  int x;
  if (m_strs) 
  {
    for (x = 0; x < m_num_strs; x ++)
    {
      char name[17] = {0};
      memcpy(name,m_strs[x].name,16);
      name[16]=0;
      if (m_strs[x].value) fprintf(fp,"%s=%s\n",name,m_strs[x].value);
    }
  }
  fclose(fp);
  m_dirty=0;
#endif
}

C_Config::~C_Config()
{
#ifndef C_CONFIG_WIN32NATIVE
  int x;
  Flush();
  if (m_strs) for (x = 0; x < m_num_strs; x ++) free(m_strs[x].value);
  free(m_strs);
#endif

  Plugin_FreeAnsiString(m_inifile);
}

C_Config::C_Config(char *ini)
{
  memset(m_strbuf,0,sizeof(m_strbuf));
  m_inifile= Plugin_CopyAnsiString(ini);

#ifndef C_CONFIG_WIN32NATIVE
  m_dirty=0;
  m_strs=NULL;
  m_num_strs=m_num_strs_alloc=0;

  // read config
  FILE *fp=fopen(m_inifile,"rt");
  if (!fp) return;

  for (;;)
  {
    char buf[4096] = {0};
    fgets(buf,sizeof(buf),fp);
    if (!buf[0] || feof(fp)) break;
    for (;;)
    {
      int l=strlen(buf);
      if (l > 0 && (buf[l-1] == '\n' || buf[l-1] == '\r')) buf[l-1]=0;
      else break;
    }
    if (buf[0] != '[') 
    {
      char *p=strstr(buf,"=");
      if (p)
      {
        *p++=0;
        WriteString(buf,p);
      }
    }
  }
  m_dirty=0;
  fclose(fp);
#endif
}

void C_Config::WriteInt(char *name, int value)
{
  char buf[32] = {0};
  StringCchPrintfA(buf, ARRAYSIZE(buf), "%d",value);
  WriteString(name,buf);
}

int C_Config::ReadInt(char *name, int defvalue)
{
#ifndef C_CONFIG_WIN32NATIVE
  char *t=ReadString(name,"");
  if (*t) return atoi(t);
  return defvalue;
#else
  return GetPrivateProfileIntA("ml_online_config",name,defvalue,m_inifile);
#endif
}

char *C_Config::WriteString(char *name, char *string)
{
#ifndef C_CONFIG_WIN32NATIVE
  	m_dirty=1;
  	for (int x = 0; x < m_num_strs; x ++)
  	{
    	if (m_strs[x].value && !strncmp(name,m_strs[x].name,16))
    	{
      		unsigned int l=(strlen(m_strs[x].value)+16)&~15;
      		if (strlen(string)<l)
      		{
        		strcpy(m_strs[x].value,string);
      		}
      		else
      		{
        		free(m_strs[x].value);
        		m_strs[x].value = (char *)malloc((strlen(string)+16)&~15);
        		strcpy(m_strs[x].value,string);
      		}
      		return m_strs[x].value;
    	}
  	}

  	// not already in there
  	if (m_num_strs >= m_num_strs_alloc || !m_strs)
  	{
		m_old_num_strs_alloc = m_num_strs_alloc;
    	m_num_strs_alloc=m_num_strs*3/2+8;
		strType *data = (strType*)::realloc(m_strs, sizeof(strType) * m_num_strs_alloc);
		if (data)
		{
			m_strs = data;
		}
		else
		{
    		data = (strType*)::malloc(sizeof(strType) * m_num_strs_alloc);
			if (data)
			{
				memcpy(data, m_strs, sizeof(strType) * m_old_num_strs_alloc);
				free(m_strs);
				m_strs = data;
			}
			else m_num_strs_alloc = m_old_num_strs_alloc;
		}
  	}
  	strncpy(m_strs[m_num_strs].name,name,16);
  	m_strs[m_num_strs].value = (char *)malloc((strlen(string)+16)&~15);
	if (m_strs[m_num_strs].value)
	{
  		strcpy(m_strs[m_num_strs].value,string);
  		return m_strs[m_num_strs++].value;
	}
	return "";
#else
  	WritePrivateProfileStringA("ml_online_config",name,string,m_inifile);
  	return name;
#endif
}

char *C_Config::ReadString( char *name, char *defstr )
{
#ifndef C_CONFIG_WIN32NATIVE
	int x;
	for ( x = 0; x < m_num_strs; x++ )
	{
		if ( m_strs[ x ].value && !::strncmp( name, m_strs[ x ].name, 16 ) )
		{
			return m_strs[ x ].value;
		}
	}
	return defstr;
#else
	static char foobuf[] = "___________ml_online_lameness___________";
	m_strbuf[ 0 ] = 0;
	GetPrivateProfileStringA( "ml_online_config", name, foobuf, m_strbuf, sizeof( m_strbuf ), m_inifile );
	if ( !strcmp( foobuf, m_strbuf ) || !strcmp( m_strbuf, "" ) )
	{
		return defstr;
	}

	m_strbuf[ sizeof( m_strbuf ) - 1 ] = 0;
	return m_strbuf;
#endif
}

static LPCSTR Config_GetPath()
{
	static LPSTR configPath = NULL;
	if (NULL == configPath)
	{
		LPCWSTR p = (NULL != WASABI_API_APP) ? WASABI_API_APP->path_getUserSettingsPath() : NULL;
		if (NULL != p)
		{
			WCHAR szBuffer[MAX_PATH * 2] = {0};
			if (0 != PathCombine(szBuffer, p, CONFIG_SUFFIX))
			{
				OMUTILITY->EnsurePathExist(szBuffer);
				PathAppend(szBuffer, L"ml_online.ini");
				configPath = Plugin_WideCharToMultiByte(CP_UTF8, 0, szBuffer, -1, NULL, NULL);
			}
		}
	}

	return configPath;
}

DWORD Config_ReadStr(LPCSTR lpSectionName, LPCSTR lpKeyName, LPCSTR lpDefault, LPSTR lpReturnedString, DWORD nSize)
{
	if (NULL == lpSectionName) lpSectionName = CONFIG_SECTION;
	return GetPrivateProfileStringA(lpSectionName, lpKeyName, lpDefault, lpReturnedString, nSize, Config_GetPath());
}

UINT Config_ReadInt(LPCSTR lpSectionName, LPCSTR lpKeyName, INT nDefault)
{
	if (NULL == lpSectionName) lpSectionName = CONFIG_SECTION;
	return GetPrivateProfileIntA(lpSectionName, lpKeyName, nDefault, Config_GetPath());
}

HRESULT Config_WriteStr(LPCSTR lpSectionName, LPCSTR lpKeyName, LPCSTR lpString)
{
	LPCSTR configPath = Config_GetPath();
	if (NULL == configPath || '\0' == *configPath) 
		return E_UNEXPECTED;
	
	if (NULL == lpSectionName) lpSectionName = CONFIG_SECTION;
	if (0 != WritePrivateProfileStringA(lpSectionName, lpKeyName, lpString, configPath))
		return S_OK;

	DWORD errorCode = GetLastError();
	return HRESULT_FROM_WIN32(errorCode);
}

HRESULT Config_WriteInt(LPCSTR lpSectionName, LPCSTR lpKeyName, INT nValue)
{
	char szBuffer[32] = {0};
	HRESULT hr = StringCchPrintfA(szBuffer, ARRAYSIZE(szBuffer), "%d", nValue);
	if (FAILED(hr)) return hr;

	if (NULL == lpSectionName) lpSectionName = CONFIG_SECTION;
	return Config_WriteStr(lpSectionName, lpKeyName, szBuffer);
}

HRESULT Config_WriteSection(LPCSTR lpSectionName, LPCSTR lpData)
{
	LPCSTR configPath = Config_GetPath();
	if (NULL == configPath || '\0' == *configPath) 
		return E_UNEXPECTED;
	
	if (NULL == lpSectionName) lpSectionName = CONFIG_SECTION;
	if (0 == WritePrivateProfileSectionA(lpSectionName, lpData, configPath))
		return S_OK;

	DWORD errorCode = GetLastError();
	return HRESULT_FROM_WIN32(errorCode);
}

HRESULT Config_ReadServiceIdList(LPCSTR lpSectionName, LPCSTR lpKeyName, CHAR separator, ReadServiceIdCallback callback, void *data)
{
	if (NULL == callback)
		return E_INVALIDARG;

	DWORD bufferSize = 16384;
	LPSTR buffer = Plugin_MallocAnsiString(bufferSize);
	if (NULL  == buffer) return E_OUTOFMEMORY;

	DWORD bufferLen = Config_ReadStr(lpSectionName, lpKeyName, NULL, buffer, bufferSize);
	if (0 != bufferLen) 
	{
		LPSTR cursor = buffer;
		LPSTR block = cursor;
		UINT serviceId;
		for(;;)
		{
			if (separator == *cursor || '\0' == *cursor)
			{
				while (' ' == *block && block < cursor) block++;
				
				if (block < cursor &&
					FALSE != StrToIntExA(block, STIF_SUPPORT_HEX, (INT*)&serviceId) &&
					0 != serviceId)
				{
	
					if (FALSE == callback(serviceId, data))
						break;
				}

				if ('\0' == *cursor)
					break;

				cursor = CharNextA(cursor);
				block = cursor;
			}
			else
				cursor = CharNextA(cursor);
		}
	}

	Plugin_FreeAnsiString(buffer);
	return S_OK;
}