| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- #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;
- }
|