void XConfig::SaveTo(CTSTR lpPath) { if (RootElement) { String tmpPath = lpPath; tmpPath.AppendString(TEXT(".tmp")); XFile file; if (file.Open(tmpPath, XFILE_WRITE, XFILE_CREATEALWAYS)) { if (WriteFileData(file, 0, RootElement)) { file.Close(); OSRenameFile(tmpPath, lpPath); } else { Log(TEXT("XConfig::SaveTo: WriteFileData failed while writing %s."), lpPath); } } } }
void XConfig::Save() { if(RootElement) { String tmpPath = strFileName; tmpPath.AppendString(TEXT(".tmp")); XFile file; if (file.Open(strFileName, XFILE_WRITE, XFILE_CREATEALWAYS)) { if (WriteFileData(file, 0, RootElement)) { file.Close(); OSRenameFile(tmpPath, strFileName); } else { Log(TEXT("XConfig::Save: WriteFileData failed while writing %s."), strFileName.Array()); } } } }
BOOL ConfigFile::SaveAs(CTSTR lpPath) { XFile newFile; String tmpPath = lpPath; tmpPath.AppendString(TEXT(".tmp")); if (newFile.Open(tmpPath, XFILE_WRITE, XFILE_CREATEALWAYS)) { if (newFile.Write("\xEF\xBB\xBF", 3) != 3) return FALSE; if (!newFile.WriteAsUTF8(lpFileData)) return FALSE; newFile.Close(); if (!OSRenameFile(tmpPath, lpPath)) Log(TEXT("ConfigFile::SaveAs: Unable to move new config file %s to %s"), tmpPath.Array(), lpPath); strFileName = lpPath; return TRUE; } return FALSE; }
void ConfigFile::SetKey(CTSTR lpSection, CTSTR lpKey, CTSTR newvalue) { assert(lpSection); assert(lpKey); TSTR lpTemp = lpFileData, lpEnd = &lpFileData[dwLength], lpSectionStart; DWORD dwSectionNameSize = slen(lpSection), dwKeyNameSize = slen(lpKey); BOOL bInSection = 0; do { lpTemp = sstr(lpTemp, TEXT("\n[")); if(!lpTemp) break; lpTemp += 2; if((scmpi_n(lpTemp, lpSection, dwSectionNameSize) == 0) && (lpTemp[dwSectionNameSize] == ']')) { bInSection = 1; lpSectionStart = lpTemp = schr(lpTemp, '\n')+1; break; } }while(lpTemp < lpEnd); if(!bInSection) { lpTemp -= 2; XFile file(strFileName, XFILE_WRITE, XFILE_CREATEALWAYS); file.Write("\xEF\xBB\xBF", 3); file.WriteAsUTF8(&lpFileData[2], dwLength-4); file.Write("\r\n[", 3); file.WriteAsUTF8(lpSection, dwSectionNameSize); file.Write("]\r\n", 3); file.WriteAsUTF8(lpKey, dwKeyNameSize); file.Write("=", 1); file.WriteAsUTF8(newvalue, slen(newvalue)); file.Write("\r\n", 2); file.Close(); if(LoadFile(XFILE_OPENEXISTING)) LoadData(); return; } do { if(*lpTemp == '[') { XFile file(strFileName, XFILE_WRITE, XFILE_CREATEALWAYS); file.Write("\xEF\xBB\xBF", 3); file.WriteAsUTF8(&lpFileData[2], DWORD(lpSectionStart-lpFileData-2)); file.WriteAsUTF8(lpKey, dwKeyNameSize); file.Write("=", 1); file.WriteAsUTF8(newvalue, slen(newvalue)); file.Write("\r\n", 2); file.WriteAsUTF8(lpSectionStart, slen(lpSectionStart)-2); file.Close(); if(LoadFile(XFILE_OPENEXISTING)) LoadData(); return; } else if(*(LPWORD)lpTemp == '//') { lpTemp = schr(lpTemp, '\n')+1; continue; } else if(bInSection) { if((scmpi_n(lpTemp, lpKey, dwKeyNameSize) == 0) && (lpTemp[dwKeyNameSize] == '=')) { lpTemp = &lpTemp[dwKeyNameSize+1]; TSTR lpNextLine = schr(lpTemp, '\r'); int newlen = slen(newvalue); if ((*lpTemp == '\r' && *newvalue == '\0') || (lpNextLine - lpTemp == newlen && !scmp_n(lpTemp, newvalue, newlen))) return; String tmpFileName = strFileName; tmpFileName += TEXT(".tmp"); XFile file; if (file.Open(tmpFileName, XFILE_WRITE, XFILE_CREATEALWAYS)) { if (file.Write("\xEF\xBB\xBF", 3) != 3) return; if (!file.WriteAsUTF8(&lpFileData[2], DWORD(lpTemp - lpFileData - 2))) return; if (!file.WriteAsUTF8(newvalue, slen(newvalue))) return; if (!file.WriteAsUTF8(lpNextLine, slen(lpNextLine) - 2)) return; file.Close(); if (!OSRenameFile(tmpFileName, strFileName)) Log(TEXT("ConfigFile::SetKey: Unable to move new config file %s to %s"), tmpFileName.Array(), strFileName.Array()); } if(LoadFile(XFILE_OPENEXISTING)) LoadData(); return; } } lpTemp = schr(lpTemp, '\n')+1; }while(lpTemp < lpEnd); XFile file(strFileName, XFILE_WRITE, XFILE_CREATEALWAYS); file.Write("\xEF\xBB\xBF", 3); file.WriteAsUTF8(&lpFileData[2], DWORD(lpSectionStart-lpFileData-2)); file.WriteAsUTF8(lpKey, dwKeyNameSize); file.Write("=", 1); file.WriteAsUTF8(newvalue, slen(newvalue)); file.Write("\r\n", 2); file.WriteAsUTF8(lpSectionStart, slen(lpSectionStart)-2); file.Close(); if(LoadFile(XFILE_OPENEXISTING)) LoadData(); }