iniutil.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "precompile.h"
  2. #include "strutil.h"
  3. #include "iniutil.h"
  4. #include "vsscanf.h"
  5. #include <winpr/string.h>
  6. #include <winpr/wlog.h>
  7. #include <winpr/ini.h>
  8. #define TAG TOOLKIT_TAG(".iniutil")
  9. #include "memutil.h"
  10. #ifndef _WIN32
  11. #define GBK_COMPACT
  12. #endif //NOT _WIN32
  13. #ifdef GBK_COMPACT
  14. #include "charset.h"
  15. #endif // GBK_COMPACT
  16. TOOLKIT_API int inifile_read_str_s(
  17. const char* section, const char* key, const char* defval, char* retval, int val_size, const char* file)
  18. {
  19. char* result = inifile_read_str(file, section, key, defval);
  20. memset(retval, '\0', val_size);
  21. if (result) {
  22. strcpy_s(retval, val_size, result);
  23. free(result);
  24. }
  25. return strlen(retval);
  26. }
  27. static void inifile_translate_string2utf8(char* tmp)
  28. {
  29. #ifdef GBK_COMPACT
  30. if (!toolkit_detect_utf8_str(tmp)) {
  31. const size_t len = toolkit_gbk2utf8(tmp, strlen(tmp) + 1, NULL, 0);
  32. if (len > 0) {
  33. char* temp = CALLOC_T(len, char);
  34. memset(temp, 0, len);
  35. if (toolkit_gbk2utf8(tmp, strlen(tmp) + 1, temp, len) > 0) {
  36. WLog_DBG(TAG, "replace tmp buffer: %s", temp);
  37. strcpy(tmp, temp);
  38. }
  39. FREE(temp);
  40. }
  41. }
  42. #endif // GBK_COMPACT
  43. }
  44. TOOLKIT_API char* inifile_read_str(const char* file,
  45. const char* section,
  46. const char* key,
  47. const char* defval)
  48. {
  49. char tmp[8192];
  50. tmp[0] = 0;
  51. GetPrivateProfileStringA(section, key, defval, tmp, sizeof(tmp), file);
  52. inifile_translate_string2utf8(tmp);
  53. return _strdup(tmp);
  54. }
  55. TOOLKIT_API int inifile_read_int(const char *file,
  56. const char *section,
  57. const char *key,
  58. int defval)
  59. {
  60. return (int)GetPrivateProfileIntA(section, key, (DWORD)defval, file);
  61. }
  62. TOOLKIT_API array_header_t* inifile_read_section_all(const char *file)
  63. {
  64. array_header_t* arr;
  65. size_t n;
  66. char *p, *t;
  67. size_t cnt;
  68. DWORD dwSize;
  69. arr = array_make(32, sizeof(char*));
  70. n = 512;
  71. p = (char*)malloc(n);
  72. if (!p) {
  73. return NULL;
  74. }
  75. dwSize = GetPrivateProfileSectionNamesA(p, n, file);
  76. while (dwSize == n-2) {
  77. n = n * 2;
  78. p = (char*)realloc(p, n);
  79. if (!p) {
  80. return NULL;
  81. }
  82. dwSize = GetPrivateProfileSectionNamesA(p, n, file);
  83. }
  84. cnt = 0;
  85. t = p;
  86. while (*t != '\0') {
  87. cnt ++;
  88. t += strlen(t) + 1;
  89. }
  90. t = p;
  91. while (*t != '\0') {
  92. char* tmp = _strdup(t);
  93. inifile_translate_string2utf8(tmp);
  94. ARRAY_PUSH(arr, char*) = _strdup(tmp);
  95. FREE(tmp);
  96. t += strlen(t) + 1;
  97. }
  98. free(p);
  99. return arr;
  100. }
  101. TOOLKIT_API array_header_t* inifile_read_section_key_all(const char *file, const char *section)
  102. {
  103. array_header_t* arr;
  104. size_t n = 512;
  105. char *p, *t;
  106. DWORD dwSize;
  107. size_t cnt = 0;
  108. arr = array_make(32, sizeof(char*));
  109. n = 512;
  110. p = (char*)malloc(n);
  111. if (!p) {
  112. return NULL;
  113. }
  114. dwSize = GetPrivateProfileStringA(section, NULL, "", p, n, file);
  115. while (dwSize == n-2) {
  116. n = n * 2;
  117. p = (char*)realloc(p, n);
  118. if (!p) {
  119. return NULL;
  120. }
  121. dwSize = GetPrivateProfileStringA(section, NULL, "", p, n, file);
  122. }
  123. cnt = 0;
  124. t = p;
  125. while (*t != '\0') {
  126. cnt ++;
  127. t += strlen(t) + 1;
  128. }
  129. t = p;
  130. while (*t != '\0') {
  131. char* tmp = _strdup(t);
  132. inifile_translate_string2utf8(tmp);
  133. ARRAY_PUSH(arr, char*) = _strdup(tmp);
  134. FREE(tmp);
  135. t += strlen(t) + 1;
  136. }
  137. free(p);
  138. return arr;
  139. }
  140. TOOLKIT_API int inifile_format_readv(const char *file, const char *section, const char *key, const char *fmt, va_list arg)
  141. {
  142. char *str = inifile_read_str(file, section, key, "");
  143. int rc;
  144. if (strlen(str) == 0) {
  145. rc = -1;
  146. goto on_error;
  147. }
  148. rc = vsscanf1(str, fmt, arg);
  149. on_error:
  150. free(str);
  151. return rc;
  152. }
  153. TOOLKIT_API int inifile_format_read(const char *file, const char *section, const char *key, const char *fmt, ...)
  154. {
  155. va_list arg;
  156. int rc;
  157. va_start(arg, fmt);
  158. rc = inifile_format_readv(file, section, key, fmt, arg);
  159. va_end(arg);
  160. return rc;
  161. }
  162. TOOLKIT_API int inifile_write_str(const char *file, const char *section, const char *key, const char *v)
  163. {
  164. return WritePrivateProfileStringA(section, key, v, file) ? 0 : -1;
  165. }
  166. TOOLKIT_API int inifile_write_int(const char *file, const char *section, const char *key, int v)
  167. {
  168. char tmp[32];
  169. sprintf(tmp, "%u", v);
  170. return WritePrivateProfileStringA(section, key, tmp, file) ? 0 : -1;
  171. }
  172. TOOLKIT_API int inifile_format_writev(const char *file, const char *section, const char *key, const char *fmt, va_list arg)
  173. {
  174. char *p;
  175. int rc;
  176. int n;
  177. n = _vscprintf(fmt, arg);
  178. if (n < 0)
  179. return -1;
  180. p = malloc(n + 1);
  181. vsprintf(p, fmt, arg);
  182. rc = inifile_write_str(file, section, key, p);
  183. free(p);
  184. return rc;
  185. }
  186. TOOLKIT_API int inifile_format_write(const char *file, const char *section, const char *key, const char *fmt, ...)
  187. {
  188. va_list arg;
  189. int rc;
  190. va_start(arg, fmt);
  191. rc = inifile_format_writev(file, section, key, fmt, arg);
  192. va_end(arg);
  193. return rc;
  194. }