BOOL TInifile::WriteIni() { Lock(); BOOL ret = FALSE; FILE *fp = fopen(ini_file, "w"); if (fp) { for (TIniSection *sec = (TIniSection *)TopObj(); sec; sec = (TIniSection *)NextObj(sec)) { TIniKey *key = (TIniKey *)sec->TopObj(); if (key) { if (sec->Name()) { if (fprintf(fp, "[%s]\n", sec->Name()) < 0) goto END; } while (key) { if (key->Key()) { if (fprintf(fp, "%s=\"%s\"\n", key->Key(), key->Val()) < 0) goto END; } else { if (fprintf(fp, "%s", key->Val()) < 0) goto END; } key = (TIniKey *)sec->NextObj(key); } } } ret = TRUE; END: fclose(fp); } UnLock(); return ret; }
BOOL TInifile::WriteIni() { Lock(); BOOL ret = FALSE; HANDLE hFile = ::CreateFileW(iniFile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if (hFile != INVALID_HANDLE_VALUE) { #define MIN_INI_ALLOC (64 * 1024) #define MAX_INI_ALLOC (10 * 1024 * 1024) #define MIN_LINE_SIZE (2 * 1024) VBuf vbuf(MIN_INI_ALLOC, MAX_INI_ALLOC); char *p = (char *)vbuf.Buf(); for (TIniSection *sec = TopObj(); sec && p; sec = NextObj(sec)) { TIniKey *key = sec->TopObj(); int len = 0; if (key) { if (sec->Name()) { len = sprintf(p, "[%s]\r\n", sec->Name()); p = NextBuf(&vbuf, len, MIN_LINE_SIZE, MIN_INI_ALLOC); } while (key) { if (key->Key()) { len = sprintf(p, "%s=\"%s\"\r\n", key->Key(), key->Val()); p = NextBuf(&vbuf, len, MIN_LINE_SIZE, MIN_INI_ALLOC); } else { len = sprintf(p, "%s\r\n", key->Val()); p = NextBuf(&vbuf, len, MIN_LINE_SIZE, MIN_INI_ALLOC); } key = sec->NextObj(key); } } } DWORD size; ret = ::WriteFile(hFile, vbuf.Buf(), (DWORD)vbuf.UsedSize(), &size, 0); ::CloseHandle(hFile); } UnLock(); return ret; }
DWORD TInifile::GetStr(const char *key_name, char *val, int max_size, const char *default_val) { TIniKey *key = cur_sec ? cur_sec->SearchKey(key_name) : NULL; return sprintf(val, "%.*s", max_size, key ? key->Val() : default_val); }
const char *TInifile::GetRawVal(const char *key_name) { TIniKey *key = curSec ? curSec->SearchKey(key_name) : NULL; return key ? key->Val() : NULL; }