// *** Copy content of original iniString to temporary string, replace modified values ***
TArray<FString> content;
TSharedPtr<TMap<FString, FString>> currentSection;
TSharedPtr<TMap<FString, FString>> currentSection2;
FString s, sectionName, key, value, key2, value2;
bool reading = true;
bool unmodified;
bool deleted = false;
int lineIdx = 0;
for (int32 i = 0; i <= m_IniLines.Num(); i++)
{
s = m_IniLines[i];
reading = i < m_IniLines.Num();
// *** Check for end of iniString ***
if (reading)
{
unmodified = true;
s = s.Trim();
sectionName = ParseSectionName(s);
}
else
{
unmodified = false;
sectionName.Empty();
}
// *** Check for section names ***
if (!sectionName.IsEmpty() || !reading)
{
if (currentSection.IsValid())
{
// *** Write all remaining modified values before leaving a section ****
if (currentSection->Num() > 0)
{
// *** Optional: All blank lines before new values and sections are removed ****
/*
sb_temp = sw.GetStringBuilder();
while ((sb_temp[sb_temp.Length - 1] == '\n') || (sb_temp[sb_temp.Length - 1] == '\r'))
{
sb_temp.Length = sb_temp.Length - 1;
}
sw.WriteLine();
*/
for (auto fkv : *currentSection)
{
content.Add(FString::Printf(TEXT("%s = %s"), *fkv.Key, *fkv.Value));
}
content.Add(TEXT(""));
currentSection->Empty();
}
}
if (reading)
{
// *** Check if current section is in local modified cache ***
if (m_Modified.Find(sectionName) == nullptr)
{
currentSection = nullptr;
}
}
}
else if (currentSection.IsValid())
{
// *** Check for key+value pair ***
if (ParseKeyValuePair(s, key, value))
{
FString* result = currentSection->Find(key);
if (result != nullptr)
{
value = *result;
unmodified = false;
currentSection->Remove(key);
content.Add(FString::Printf(TEXT("%s = %s"), *key, *value));
}
}
}
// ** Check if the section/key in current line has been deleted ***
if (unmodified)
{
if (!sectionName.IsEmpty())
{
if (!m_Sections.Contains(sectionName))
{
deleted = true;
currentSection2 = nullptr;
}
else
{
deleted = false;
currentSection2 = m_Sections[sectionName];
}
}
else if (currentSection2.IsValid())
{
if (ParseKeyValuePair(s, key2, value2))
{
deleted = !currentSection2->Contains(key2);
}
}
}
// *** Write unmodified lines from the original iniString ***
if (unmodified)
{
if (IsComment(s))
{
content.Add(s);
}
else if (!deleted)
{
content.Add(s);
}
}
}
// *** Cycle on all remaining modified values ***
for (auto sectionPair : m_Modified)
{
currentSection = sectionPair.Value;
if (currentSection->Num() > 0)
{
content.Add(TEXT(""));
// *** Write the section name ***
content.Add(FString::Printf(TEXT("[%s]"), *sectionPair.Key));
// *** Cycle on all key+value pairs in the section ***
for (auto valuePair : *currentSection)
{
// *** Write the key+value pair ***
content.Add(FString::Printf(TEXT("%s = %s"), *(valuePair.Key), *(valuePair.Value)));
}
currentSection->Empty();
}
}
m_Modified.Empty();
// *** Get result to iniString ***
m_IniLines = content;
// ** Write iniString to file ***
if (!m_FilePath.IsEmpty())
{
FFileHelper::SaveStringArrayToFile(m_IniLines, *m_FilePath);
}