#include "precompile.h" #include "memutil.h" #include "strutil.h" #include "iniutil.h" #include "vsscanf.h" TOOLKIT_API char *inifile_read_str(const char *file, const char *section, const char *key, const char *defval) { char tmp[8192]; tmp[0] = 0; GetPrivateProfileStringA(section, key, defval, tmp, sizeof(tmp), file); return _strdup(tmp); } TOOLKIT_API int inifile_read_int(const char *file, const char *section, const char *key, int defval) { return (int)GetPrivateProfileIntA(section, key, (DWORD)defval, file); } TOOLKIT_API array_header_t* inifile_read_section_all(const char *file) { array_header_t* arr; size_t n; char *p, *t; size_t cnt; DWORD dwSize; arr = array_make(32, sizeof(char*)); n = 512; p = (char*)malloc(n); dwSize = GetPrivateProfileSectionNamesA(p, n, file); while (dwSize == n-2) { n = n * 2; p = (char*)realloc(p, n); dwSize = GetPrivateProfileSectionNamesA(p, n, file); } cnt = 0; t = p; while (*t != '\0') { cnt ++; t += strlen(t) + 1; } t = p; while (*t != '\0') { ARRAY_PUSH(arr, char*) = _strdup(t); t += strlen(t) + 1; } free(p); return arr; } TOOLKIT_API array_header_t* inifile_read_section_key_all(const char *file, const char *section) { array_header_t* arr; size_t n = 512; char *p, *t; DWORD dwSize; size_t cnt = 0; arr = array_make(32, sizeof(char*)); n = 512; p = (char*)malloc(n); dwSize = GetPrivateProfileStringA(section, NULL, "", p, n, file); while (dwSize == n-2) { n = n * 2; p = (char*)realloc(p, n); dwSize = GetPrivateProfileStringA(section, NULL, "", p, n, file); } cnt = 0; t = p; while (*t != '\0') { cnt ++; t += strlen(t) + 1; } t = p; while (*t != '\0') { ARRAY_PUSH(arr, char*) = _strdup(t); t += strlen(t) + 1; } free(p); return arr; } TOOLKIT_API int inifile_format_readv(const char *file, const char *section, const char *key, const char *fmt, va_list arg) { char *str = inifile_read_str(file, section, key, ""); int rc; if (strlen(str) == 0) { rc = -1; goto on_error; } rc = vsscanf1(str, fmt, arg); on_error: free(str); return rc; } TOOLKIT_API int inifile_format_read(const char *file, const char *section, const char *key, const char *fmt, ...) { va_list arg; int rc; va_start(arg, fmt); rc = inifile_format_readv(file, section, key, fmt, arg); va_end(arg); return rc; } TOOLKIT_API int inifile_write_str(const char *file, const char *section, const char *key, const char *v) { return WritePrivateProfileStringA(section, key, v, file) ? 0 : -1; } TOOLKIT_API int inifile_write_int(const char *file, const char *section, const char *key, int v) { char tmp[32]; sprintf(tmp, "%u", v); return WritePrivateProfileStringA(section, key, tmp, file) ? 0 : -1; } TOOLKIT_API int inifile_format_writev(const char *file, const char *section, const char *key, const char *fmt, va_list arg) { char *p; int rc; int n; n = _vscprintf(fmt, arg); if (n < 0) return -1; p = malloc(n + 1); vsprintf(p, fmt, arg); rc = inifile_write_str(file, section, key, p); free(p); return rc; } TOOLKIT_API int inifile_format_write(const char *file, const char *section, const char *key, const char *fmt, ...) { va_list arg; int rc; va_start(arg, fmt); rc = inifile_format_writev(file, section, key, fmt, arg); va_end(arg); return rc; }