#include "precompile.h" #include "strutil.h" #include "iniutil.h" #include "vsscanf.h" #include #include #include #define TAG TOOLKIT_TAG(".iniutil") #include "memutil.h" #ifndef _WIN32 #define GBK_COMPACT #endif //NOT _WIN32 #ifdef GBK_COMPACT #include "charset.h" #endif // GBK_COMPACT TOOLKIT_API int inifile_read_str_s( const char* section, const char* key, const char* defval, char* retval, int val_size, const char* file) { char* result = inifile_read_str(file, section, key, defval); memset(retval, '\0', val_size); if (result) { strcpy_s(retval, val_size, result); free(result); } return strlen(retval); } static void inifile_translate_string2utf8(char* tmp) { #ifdef GBK_COMPACT if (!toolkit_detect_utf8_str(tmp)) { const size_t len = toolkit_gbk2utf8(tmp, strlen(tmp) + 1, NULL, 0); if (len > 0) { char* temp = CALLOC_T(len, char); memset(temp, 0, len); if (toolkit_gbk2utf8(tmp, strlen(tmp) + 1, temp, len) > 0) { WLog_DBG(TAG, "replace tmp buffer: %s", temp); strcpy(tmp, temp); } FREE(temp); } } #endif // GBK_COMPACT } 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); inifile_translate_string2utf8(tmp); 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); if (!p) { return NULL; } dwSize = GetPrivateProfileSectionNamesA(p, n, file); while (dwSize == n-2) { n = n * 2; p = (char*)realloc(p, n); if (!p) { return NULL; } dwSize = GetPrivateProfileSectionNamesA(p, n, file); } cnt = 0; t = p; while (*t != '\0') { cnt ++; t += strlen(t) + 1; } t = p; while (*t != '\0') { char* tmp = _strdup(t); inifile_translate_string2utf8(tmp); ARRAY_PUSH(arr, char*) = _strdup(tmp); FREE(tmp); 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); if (!p) { return NULL; } dwSize = GetPrivateProfileStringA(section, NULL, "", p, n, file); while (dwSize == n-2) { n = n * 2; p = (char*)realloc(p, n); if (!p) { return NULL; } dwSize = GetPrivateProfileStringA(section, NULL, "", p, n, file); } cnt = 0; t = p; while (*t != '\0') { cnt ++; t += strlen(t) + 1; } t = p; while (*t != '\0') { char* tmp = _strdup(t); inifile_translate_string2utf8(tmp); ARRAY_PUSH(arr, char*) = _strdup(tmp); FREE(tmp); 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; }