1
0

config.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include <shlwapi.h>
  2. #include "..\..\..\nu\ns_wc.h"
  3. #include "config.h"
  4. #include <strsafe.h>
  5. ///////////////////
  6. ///
  7. /// UNICODE VERSION
  8. ///
  9. /////
  10. ConfigW::ConfigW()
  11. {
  12. emptyBOM = FALSE;
  13. buff[0] = 0;
  14. buffA = NULL;
  15. fileName = NULL;
  16. defSection = NULL;
  17. }
  18. ConfigW::ConfigW(const wchar_t *ini, const wchar_t *section)
  19. {
  20. emptyBOM = FALSE;
  21. buff[0] = 0;
  22. buffA = NULL;
  23. fileName = NULL;
  24. defSection = NULL;
  25. if (SetIniFile(ini)) SetSection(section);
  26. }
  27. ConfigW::~ConfigW()
  28. {
  29. if (fileName) free(fileName);
  30. if (defSection) free(defSection);
  31. if (buffA) free(buffA);
  32. if (emptyBOM) RemoveEmptyFile();
  33. }
  34. void ConfigW::Flush(void)
  35. {
  36. return;
  37. }
  38. BOOL ConfigW::Write(const wchar_t *section, const wchar_t *name, const wchar_t *value)
  39. {
  40. return (fileName) ? WritePrivateProfileString(section, name, value, fileName) : FALSE;
  41. }
  42. BOOL ConfigW::Write(const wchar_t *section, const wchar_t *name, double value)
  43. {
  44. wchar_t tmp[48] = {0};
  45. if (S_OK != StringCchPrintf(tmp, 48, L"%g", value)) return FALSE;
  46. return Write(section, name, tmp);
  47. }
  48. BOOL ConfigW::Write(const wchar_t *section, const wchar_t *name, long long value)
  49. {
  50. wchar_t tmp[32] = {0};
  51. if (S_OK != StringCchPrintf(tmp, 32, L"%I64d", value)) return FALSE;
  52. return Write(section,name, tmp);
  53. }
  54. BOOL ConfigW::Write(const wchar_t *section, const wchar_t *name, int value)
  55. {
  56. wchar_t tmp[16] = {0};
  57. if (S_OK != StringCchPrintf(tmp, 16, L"%d", value)) return FALSE;
  58. return Write(section,name, tmp);
  59. }
  60. BOOL ConfigW::Write(const wchar_t *section, const wchar_t *name, const char *value)
  61. {
  62. int len = (int)strlen(value) + 1;
  63. wchar_t *tmp = (wchar_t*)malloc(len*sizeof(wchar_t));
  64. if (!tmp) return FALSE;
  65. BOOL ret = FALSE;
  66. if (MultiByteToWideCharSZ(CP_ACP, 0, value, -1, tmp, len))
  67. {
  68. ret = Write(section,name, tmp);
  69. }
  70. free(tmp);
  71. return ret;
  72. }
  73. BOOL ConfigW::Write(const wchar_t *name, int value)
  74. {
  75. if(!defSection) return FALSE;
  76. return Write(defSection, name, value);
  77. }
  78. BOOL ConfigW::Write(const wchar_t *name, long long value)
  79. {
  80. if(!defSection) return FALSE;
  81. return Write(defSection, name, value);
  82. }
  83. BOOL ConfigW::Write(const wchar_t *name, double value)
  84. {
  85. if(!defSection) return FALSE;
  86. return Write(defSection, name, value);
  87. }
  88. BOOL ConfigW::Write(const wchar_t *name, const wchar_t *value)
  89. {
  90. if(!defSection) return FALSE;
  91. return Write(defSection, name, value);
  92. }
  93. BOOL ConfigW::Write(const wchar_t *name, const char value)
  94. {
  95. if(!defSection) return FALSE;
  96. return Write(defSection, name, value);
  97. }
  98. int ConfigW::ReadInt(const wchar_t *section, const wchar_t *name, int defvalue)
  99. {
  100. if (!fileName) return defvalue;
  101. return GetPrivateProfileInt(section, name, defvalue, fileName);
  102. }
  103. long long ConfigW::ReadInt64(const wchar_t *section, const wchar_t *name, long long defvalue)
  104. {
  105. if (!fileName) return defvalue;
  106. wchar_t tmp[32] = {0};
  107. if (S_OK != StringCchPrintf(tmp, 32, L"%I64d", defvalue)) return defvalue;
  108. const wchar_t *ret = ReadStringW(section, name, tmp);
  109. return _wtoi64(ret);
  110. }
  111. double ConfigW::ReadDouble(const wchar_t *section, const wchar_t *name, double defvalue)
  112. {
  113. if (!fileName) return defvalue;
  114. wchar_t tmp[32] = {0};
  115. if (S_OK != StringCchPrintf(tmp, 32, L"%g", defvalue)) return defvalue;
  116. const wchar_t *ret = ReadStringW(section, name, tmp);
  117. return _wtof(ret);
  118. }
  119. const char* ConfigW::ReadStringA(const wchar_t *section, const wchar_t *name, const char *defvalue)
  120. {
  121. if (buffA) free(buffA);
  122. if (!fileName) return defvalue;
  123. int len = (int)strlen(defvalue) + 1;
  124. wchar_t *tmp = (wchar_t*)malloc(len *sizeof(wchar_t));
  125. if (!tmp) return defvalue;
  126. if (!MultiByteToWideCharSZ(CP_ACP, 0, defvalue, -1, tmp, len))
  127. {
  128. free(tmp);
  129. return defvalue;
  130. }
  131. const wchar_t *ret = ReadStringW(section, name, tmp);
  132. if (!ret || lstrcmp(ret, tmp) == 0)
  133. {
  134. free(tmp);
  135. return defvalue;
  136. }
  137. free(tmp);
  138. len = (int)lstrlen(ret) + 1;
  139. buffA = (char*)malloc(len*sizeof(char));
  140. if (!buffA) return defvalue;
  141. if (WideCharToMultiByteSZ(CP_ACP, 0, ret, -1, buffA, len, NULL, NULL))
  142. {
  143. free(buffA);
  144. buffA = NULL;
  145. return defvalue;
  146. }
  147. return buffA;
  148. }
  149. const wchar_t* ConfigW::ReadStringW(const wchar_t *section, const wchar_t *name, const wchar_t *defvalue)
  150. {
  151. if (!fileName) return defvalue;
  152. static wchar_t def[] = L"_$~$_";
  153. buff[0] = 0;
  154. int len = GetPrivateProfileString(section, name, def, buff, BUFF_SIZE, fileName);
  155. if (!len || !lstrcmp(def,buff)) return defvalue;
  156. buff[BUFF_SIZE-1]=0;
  157. return buff;
  158. }
  159. int ConfigW::ReadInt(const wchar_t *name, int defvalue)
  160. {
  161. if(!defSection) return defvalue;
  162. return ReadInt(defSection, name, defvalue);
  163. }
  164. long long ConfigW::ReadInt64(const wchar_t *name, long long defvalue)
  165. {
  166. if(!defSection) return defvalue;
  167. return ReadInt64(defSection, name, defvalue);
  168. }
  169. double ConfigW::ReadDouble(const wchar_t *name, double defvalue)
  170. {
  171. if(!defSection) return defvalue;
  172. return ReadDouble(defSection, name, defvalue);
  173. }
  174. const char* ConfigW::ReadStringA(const wchar_t *name, const char *defvalue)
  175. {
  176. if(!defSection) return defvalue;
  177. return ReadStringA(defSection, name, defvalue);
  178. }
  179. const wchar_t* ConfigW::ReadStringW(const wchar_t *name, const wchar_t *defvalue)
  180. {
  181. if(!defSection) return defvalue;
  182. return ReadStringW(defSection, name, defvalue);
  183. }
  184. BOOL ConfigW::SetSection(const wchar_t *section)
  185. {
  186. if (defSection)
  187. {
  188. free(defSection);
  189. defSection = NULL;
  190. }
  191. size_t len;
  192. len = lstrlen(section) + 1;
  193. defSection = (wchar_t*)malloc(len*sizeof(wchar_t));
  194. if (NULL == defSection)
  195. {
  196. return FALSE;
  197. }
  198. if (S_OK != StringCchCopy(defSection, len, section))
  199. {
  200. free(defSection);
  201. defSection = NULL;
  202. return FALSE;
  203. }
  204. return TRUE;
  205. }
  206. BOOL ConfigW::SetIniFile(const wchar_t *file)
  207. {
  208. if (fileName)
  209. {
  210. if (emptyBOM) RemoveEmptyFile();
  211. free(fileName);
  212. fileName = NULL;
  213. }
  214. size_t len;
  215. len = lstrlen(file) + 1;
  216. fileName = (wchar_t*)malloc(len*sizeof(wchar_t));
  217. if (NULL == fileName) return FALSE;
  218. if (S_OK != StringCchCopy(fileName, len, file))
  219. {
  220. free(fileName);
  221. fileName = NULL;
  222. return FALSE;
  223. }
  224. if (fileName) CreateFileWithBOM();
  225. return TRUE;
  226. }
  227. const wchar_t* ConfigW::GetSection(void)
  228. {
  229. return defSection;
  230. }
  231. const wchar_t* ConfigW::GetFile(void)
  232. {
  233. return fileName;
  234. }
  235. void ConfigW::CreateFileWithBOM(void)
  236. {
  237. // benski> this doesn't seem to be working on win9x
  238. HANDLE hFile = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  239. if (INVALID_HANDLE_VALUE == hFile)
  240. {
  241. WORD wBOM = 0xFEFF;
  242. DWORD num = 0;
  243. hFile = CreateFile(fileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
  244. WriteFile(hFile, &wBOM, sizeof(WORD), &num, NULL);
  245. emptyBOM = TRUE;
  246. }
  247. CloseHandle(hFile);
  248. }
  249. void ConfigW::RemoveEmptyFile(void)
  250. {
  251. emptyBOM = FALSE;
  252. HANDLE hFile = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  253. if (INVALID_HANDLE_VALUE == hFile) return;
  254. DWORD fsize;
  255. fsize = GetFileSize(hFile, NULL);
  256. CloseHandle(hFile);
  257. if (fsize == 2) DeleteFile(fileName);
  258. return;
  259. }
  260. BOOL ConfigW::IsFileExist(void)
  261. {
  262. HANDLE hFile = CreateFile(fileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  263. BOOL exist = (INVALID_HANDLE_VALUE != hFile);
  264. if (exist)
  265. {
  266. exist = (GetFileSize(hFile, NULL) != 2);
  267. CloseHandle(hFile);
  268. }
  269. return exist;
  270. }