config.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include "main.h"
  2. #include "./config.h"
  3. #include "./api__ml_online.h"
  4. #include <shlwapi.h>
  5. #include <strsafe.h>
  6. #define CONFIG_SUFFIX L"Plugins\\ml"
  7. #define CONFIG_SECTION "ml_online_config"
  8. void C_Config::Flush(void)
  9. {
  10. #ifndef C_CONFIG_WIN32NATIVE
  11. if (!m_dirty) return;
  12. FILE *fp=fopen(m_inifile,"wt");
  13. if (!fp) return;
  14. fprintf(fp,"[ml_online_config]\n");
  15. int x;
  16. if (m_strs)
  17. {
  18. for (x = 0; x < m_num_strs; x ++)
  19. {
  20. char name[17] = {0};
  21. memcpy(name,m_strs[x].name,16);
  22. name[16]=0;
  23. if (m_strs[x].value) fprintf(fp,"%s=%s\n",name,m_strs[x].value);
  24. }
  25. }
  26. fclose(fp);
  27. m_dirty=0;
  28. #endif
  29. }
  30. C_Config::~C_Config()
  31. {
  32. #ifndef C_CONFIG_WIN32NATIVE
  33. int x;
  34. Flush();
  35. if (m_strs) for (x = 0; x < m_num_strs; x ++) free(m_strs[x].value);
  36. free(m_strs);
  37. #endif
  38. Plugin_FreeAnsiString(m_inifile);
  39. }
  40. C_Config::C_Config(char *ini)
  41. {
  42. memset(m_strbuf,0,sizeof(m_strbuf));
  43. m_inifile= Plugin_CopyAnsiString(ini);
  44. #ifndef C_CONFIG_WIN32NATIVE
  45. m_dirty=0;
  46. m_strs=NULL;
  47. m_num_strs=m_num_strs_alloc=0;
  48. // read config
  49. FILE *fp=fopen(m_inifile,"rt");
  50. if (!fp) return;
  51. for (;;)
  52. {
  53. char buf[4096] = {0};
  54. fgets(buf,sizeof(buf),fp);
  55. if (!buf[0] || feof(fp)) break;
  56. for (;;)
  57. {
  58. int l=strlen(buf);
  59. if (l > 0 && (buf[l-1] == '\n' || buf[l-1] == '\r')) buf[l-1]=0;
  60. else break;
  61. }
  62. if (buf[0] != '[')
  63. {
  64. char *p=strstr(buf,"=");
  65. if (p)
  66. {
  67. *p++=0;
  68. WriteString(buf,p);
  69. }
  70. }
  71. }
  72. m_dirty=0;
  73. fclose(fp);
  74. #endif
  75. }
  76. void C_Config::WriteInt(char *name, int value)
  77. {
  78. char buf[32] = {0};
  79. StringCchPrintfA(buf, ARRAYSIZE(buf), "%d",value);
  80. WriteString(name,buf);
  81. }
  82. int C_Config::ReadInt(char *name, int defvalue)
  83. {
  84. #ifndef C_CONFIG_WIN32NATIVE
  85. char *t=ReadString(name,"");
  86. if (*t) return atoi(t);
  87. return defvalue;
  88. #else
  89. return GetPrivateProfileIntA("ml_online_config",name,defvalue,m_inifile);
  90. #endif
  91. }
  92. char *C_Config::WriteString(char *name, char *string)
  93. {
  94. #ifndef C_CONFIG_WIN32NATIVE
  95. m_dirty=1;
  96. for (int x = 0; x < m_num_strs; x ++)
  97. {
  98. if (m_strs[x].value && !strncmp(name,m_strs[x].name,16))
  99. {
  100. unsigned int l=(strlen(m_strs[x].value)+16)&~15;
  101. if (strlen(string)<l)
  102. {
  103. strcpy(m_strs[x].value,string);
  104. }
  105. else
  106. {
  107. free(m_strs[x].value);
  108. m_strs[x].value = (char *)malloc((strlen(string)+16)&~15);
  109. strcpy(m_strs[x].value,string);
  110. }
  111. return m_strs[x].value;
  112. }
  113. }
  114. // not already in there
  115. if (m_num_strs >= m_num_strs_alloc || !m_strs)
  116. {
  117. m_old_num_strs_alloc = m_num_strs_alloc;
  118. m_num_strs_alloc=m_num_strs*3/2+8;
  119. strType *data = (strType*)::realloc(m_strs, sizeof(strType) * m_num_strs_alloc);
  120. if (data)
  121. {
  122. m_strs = data;
  123. }
  124. else
  125. {
  126. data = (strType*)::malloc(sizeof(strType) * m_num_strs_alloc);
  127. if (data)
  128. {
  129. memcpy(data, m_strs, sizeof(strType) * m_old_num_strs_alloc);
  130. free(m_strs);
  131. m_strs = data;
  132. }
  133. else m_num_strs_alloc = m_old_num_strs_alloc;
  134. }
  135. }
  136. strncpy(m_strs[m_num_strs].name,name,16);
  137. m_strs[m_num_strs].value = (char *)malloc((strlen(string)+16)&~15);
  138. if (m_strs[m_num_strs].value)
  139. {
  140. strcpy(m_strs[m_num_strs].value,string);
  141. return m_strs[m_num_strs++].value;
  142. }
  143. return "";
  144. #else
  145. WritePrivateProfileStringA("ml_online_config",name,string,m_inifile);
  146. return name;
  147. #endif
  148. }
  149. char *C_Config::ReadString( char *name, char *defstr )
  150. {
  151. #ifndef C_CONFIG_WIN32NATIVE
  152. int x;
  153. for ( x = 0; x < m_num_strs; x++ )
  154. {
  155. if ( m_strs[ x ].value && !::strncmp( name, m_strs[ x ].name, 16 ) )
  156. {
  157. return m_strs[ x ].value;
  158. }
  159. }
  160. return defstr;
  161. #else
  162. static char foobuf[] = "___________ml_online_lameness___________";
  163. m_strbuf[ 0 ] = 0;
  164. GetPrivateProfileStringA( "ml_online_config", name, foobuf, m_strbuf, sizeof( m_strbuf ), m_inifile );
  165. if ( !strcmp( foobuf, m_strbuf ) || !strcmp( m_strbuf, "" ) )
  166. {
  167. return defstr;
  168. }
  169. m_strbuf[ sizeof( m_strbuf ) - 1 ] = 0;
  170. return m_strbuf;
  171. #endif
  172. }
  173. static LPCSTR Config_GetPath()
  174. {
  175. static LPSTR configPath = NULL;
  176. if (NULL == configPath)
  177. {
  178. LPCWSTR p = (NULL != WASABI_API_APP) ? WASABI_API_APP->path_getUserSettingsPath() : NULL;
  179. if (NULL != p)
  180. {
  181. WCHAR szBuffer[MAX_PATH * 2] = {0};
  182. if (0 != PathCombine(szBuffer, p, CONFIG_SUFFIX))
  183. {
  184. OMUTILITY->EnsurePathExist(szBuffer);
  185. PathAppend(szBuffer, L"ml_online.ini");
  186. configPath = Plugin_WideCharToMultiByte(CP_UTF8, 0, szBuffer, -1, NULL, NULL);
  187. }
  188. }
  189. }
  190. return configPath;
  191. }
  192. DWORD Config_ReadStr(LPCSTR lpSectionName, LPCSTR lpKeyName, LPCSTR lpDefault, LPSTR lpReturnedString, DWORD nSize)
  193. {
  194. if (NULL == lpSectionName) lpSectionName = CONFIG_SECTION;
  195. return GetPrivateProfileStringA(lpSectionName, lpKeyName, lpDefault, lpReturnedString, nSize, Config_GetPath());
  196. }
  197. UINT Config_ReadInt(LPCSTR lpSectionName, LPCSTR lpKeyName, INT nDefault)
  198. {
  199. if (NULL == lpSectionName) lpSectionName = CONFIG_SECTION;
  200. return GetPrivateProfileIntA(lpSectionName, lpKeyName, nDefault, Config_GetPath());
  201. }
  202. HRESULT Config_WriteStr(LPCSTR lpSectionName, LPCSTR lpKeyName, LPCSTR lpString)
  203. {
  204. LPCSTR configPath = Config_GetPath();
  205. if (NULL == configPath || '\0' == *configPath)
  206. return E_UNEXPECTED;
  207. if (NULL == lpSectionName) lpSectionName = CONFIG_SECTION;
  208. if (0 != WritePrivateProfileStringA(lpSectionName, lpKeyName, lpString, configPath))
  209. return S_OK;
  210. DWORD errorCode = GetLastError();
  211. return HRESULT_FROM_WIN32(errorCode);
  212. }
  213. HRESULT Config_WriteInt(LPCSTR lpSectionName, LPCSTR lpKeyName, INT nValue)
  214. {
  215. char szBuffer[32] = {0};
  216. HRESULT hr = StringCchPrintfA(szBuffer, ARRAYSIZE(szBuffer), "%d", nValue);
  217. if (FAILED(hr)) return hr;
  218. if (NULL == lpSectionName) lpSectionName = CONFIG_SECTION;
  219. return Config_WriteStr(lpSectionName, lpKeyName, szBuffer);
  220. }
  221. HRESULT Config_WriteSection(LPCSTR lpSectionName, LPCSTR lpData)
  222. {
  223. LPCSTR configPath = Config_GetPath();
  224. if (NULL == configPath || '\0' == *configPath)
  225. return E_UNEXPECTED;
  226. if (NULL == lpSectionName) lpSectionName = CONFIG_SECTION;
  227. if (0 == WritePrivateProfileSectionA(lpSectionName, lpData, configPath))
  228. return S_OK;
  229. DWORD errorCode = GetLastError();
  230. return HRESULT_FROM_WIN32(errorCode);
  231. }
  232. HRESULT Config_ReadServiceIdList(LPCSTR lpSectionName, LPCSTR lpKeyName, CHAR separator, ReadServiceIdCallback callback, void *data)
  233. {
  234. if (NULL == callback)
  235. return E_INVALIDARG;
  236. DWORD bufferSize = 16384;
  237. LPSTR buffer = Plugin_MallocAnsiString(bufferSize);
  238. if (NULL == buffer) return E_OUTOFMEMORY;
  239. DWORD bufferLen = Config_ReadStr(lpSectionName, lpKeyName, NULL, buffer, bufferSize);
  240. if (0 != bufferLen)
  241. {
  242. LPSTR cursor = buffer;
  243. LPSTR block = cursor;
  244. UINT serviceId;
  245. for(;;)
  246. {
  247. if (separator == *cursor || '\0' == *cursor)
  248. {
  249. while (' ' == *block && block < cursor) block++;
  250. if (block < cursor &&
  251. FALSE != StrToIntExA(block, STIF_SUPPORT_HEX, (INT*)&serviceId) &&
  252. 0 != serviceId)
  253. {
  254. if (FALSE == callback(serviceId, data))
  255. break;
  256. }
  257. if ('\0' == *cursor)
  258. break;
  259. cursor = CharNextA(cursor);
  260. block = cursor;
  261. }
  262. else
  263. cursor = CharNextA(cursor);
  264. }
  265. }
  266. Plugin_FreeAnsiString(buffer);
  267. return S_OK;
  268. }