iniutil.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #define TAG TOOLKIT_TAG(".iniutil")
  8. #include "memutil.h"
  9. TOOLKIT_API char* inifile_read_str(const char* file,
  10. const char* section,
  11. const char* key,
  12. const char* defval)
  13. {
  14. char tmp[8192];
  15. tmp[0] = 0;
  16. GetPrivateProfileStringA(section, key, defval, tmp, sizeof(tmp), file);
  17. return _strdup(tmp);
  18. }
  19. TOOLKIT_API int inifile_read_int(const char *file,
  20. const char *section,
  21. const char *key,
  22. int defval)
  23. {
  24. return (int)GetPrivateProfileIntA(section, key, (DWORD)defval, file);
  25. }
  26. TOOLKIT_API array_header_t* inifile_read_section_all(const char *file)
  27. {
  28. array_header_t* arr;
  29. size_t n;
  30. char *p, *t;
  31. size_t cnt;
  32. DWORD dwSize;
  33. arr = array_make(32, sizeof(char*));
  34. n = 512;
  35. p = (char*)malloc(n);
  36. if (!p) {
  37. return NULL;
  38. }
  39. dwSize = GetPrivateProfileSectionNamesA(p, n, file);
  40. while (dwSize == n-2) {
  41. n = n * 2;
  42. p = (char*)realloc(p, n);
  43. if (!p) {
  44. return NULL;
  45. }
  46. dwSize = GetPrivateProfileSectionNamesA(p, n, file);
  47. }
  48. cnt = 0;
  49. t = p;
  50. while (*t != '\0') {
  51. cnt ++;
  52. t += strlen(t) + 1;
  53. }
  54. t = p;
  55. while (*t != '\0') {
  56. ARRAY_PUSH(arr, char*) = _strdup(t);
  57. t += strlen(t) + 1;
  58. }
  59. free(p);
  60. return arr;
  61. }
  62. TOOLKIT_API array_header_t* inifile_read_section_key_all(const char *file, const char *section)
  63. {
  64. array_header_t* arr;
  65. size_t n = 512;
  66. char *p, *t;
  67. DWORD dwSize;
  68. size_t cnt = 0;
  69. arr = array_make(32, sizeof(char*));
  70. n = 512;
  71. p = (char*)malloc(n);
  72. if (!p) {
  73. return NULL;
  74. }
  75. dwSize = GetPrivateProfileStringA(section, NULL, "", 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 = GetPrivateProfileStringA(section, NULL, "", 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. ARRAY_PUSH(arr, char*) = _strdup(t);
  93. t += strlen(t) + 1;
  94. }
  95. free(p);
  96. return arr;
  97. }
  98. TOOLKIT_API int inifile_format_readv(const char *file, const char *section, const char *key, const char *fmt, va_list arg)
  99. {
  100. char *str = inifile_read_str(file, section, key, "");
  101. int rc;
  102. if (strlen(str) == 0) {
  103. rc = -1;
  104. goto on_error;
  105. }
  106. rc = vsscanf1(str, fmt, arg);
  107. on_error:
  108. free(str);
  109. return rc;
  110. }
  111. TOOLKIT_API int inifile_format_read(const char *file, const char *section, const char *key, const char *fmt, ...)
  112. {
  113. va_list arg;
  114. int rc;
  115. va_start(arg, fmt);
  116. rc = inifile_format_readv(file, section, key, fmt, arg);
  117. va_end(arg);
  118. return rc;
  119. }
  120. TOOLKIT_API int inifile_write_str(const char *file, const char *section, const char *key, const char *v)
  121. {
  122. return WritePrivateProfileStringA(section, key, v, file) ? 0 : -1;
  123. }
  124. TOOLKIT_API int inifile_write_int(const char *file, const char *section, const char *key, int v)
  125. {
  126. char tmp[32];
  127. sprintf(tmp, "%u", v);
  128. return WritePrivateProfileStringA(section, key, tmp, file) ? 0 : -1;
  129. }
  130. TOOLKIT_API int inifile_format_writev(const char *file, const char *section, const char *key, const char *fmt, va_list arg)
  131. {
  132. char *p;
  133. int rc;
  134. int n;
  135. n = _vscprintf(fmt, arg);
  136. if (n < 0)
  137. return -1;
  138. p = malloc(n + 1);
  139. vsprintf(p, fmt, arg);
  140. rc = inifile_write_str(file, section, key, p);
  141. free(p);
  142. return rc;
  143. }
  144. TOOLKIT_API int inifile_format_write(const char *file, const char *section, const char *key, const char *fmt, ...)
  145. {
  146. va_list arg;
  147. int rc;
  148. va_start(arg, fmt);
  149. rc = inifile_format_writev(file, section, key, fmt, arg);
  150. va_end(arg);
  151. return rc;
  152. }