iniutil.c 3.3 KB

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