📄 inifile.cpp
字号:
content.end(),
CIniFile::RecordSectionKeyIs(SectionName,KeyName)); // Locate the Record
iter->Value = Value; // Insert the correct value
return Save(FileName,content); // Save
}
return false; // In the event the file does not load
}
bool CIniFile::RenameSection(string OldSectionName, string NewSectionName, string FileName)
{
vector<Record> content; // Holds the current record // Holds the current record
if (Load(FileName, content)) // Make sure the file is loaded
{
for(vector<Record>::iterator iter = content.begin();
iter < content.end(); iter++) // Loop through the records
{
if(iter->Section == OldSectionName) // Is this the OldSectionName?
iter->Section = NewSectionName; // Now its the NewSectionName
}
return Save(FileName,content); // Save
}
return false; // In the event the file does not load
}
bool CIniFile::CommentRecord(CommentChar cc, string KeyName,string SectionName,string FileName)
{
vector<Record> content; // Holds the current record // Holds the current record
if (Load(FileName, content)) // Make sure the file is loaded
{
vector<Record>::iterator iter = std::find_if(content.begin(),
content.end(),
CIniFile::RecordSectionKeyIs(SectionName,KeyName)); // Locate the Section/Key
if (iter == content.end()) return false; // The Section/Key was not found
iter->Commented = cc; // Change the Comment value
return Save(FileName,content); // Save
}
return false; // In the event the file does not load
}
bool CIniFile::UnCommentRecord(string KeyName,string SectionName,string FileName)
{
vector<Record> content; // Holds the current record // Holds the current record
if (Load(FileName, content)) // Make sure the file is loaded
{
vector<Record>::iterator iter = std::find_if(content.begin(),
content.end(),
CIniFile::RecordSectionKeyIs(SectionName,KeyName)); // Locate the Section/Key
if (iter == content.end()) return false; // The Section/Key was not found
iter->Commented = ' '; // Remove the Comment value
return Save(FileName,content); // Save
}
return false; // In the event the file does not load
}
bool CIniFile::CommentSection(char CommentChar, string SectionName, string FileName)
{
vector<Record> content; // Holds the current record // Holds the current record
if (Load(FileName, content)) // Make sure the file is loaded
{
for(vector<Record>::iterator iter = content.begin(); iter < content.end(); iter++)
{
if(iter->Section == SectionName) // Is this the right section?
iter->Commented = CommentChar; // Change the comment value
}
return Save(FileName,content); // Save
}
return false; // In the event the file does not load
}
bool CIniFile::UnCommentSection(string SectionName, string FileName)
{
vector<Record> content; // Holds the current record // Holds the current record
if (Load(FileName, content)) // Make sure the file is loaded
{
for(vector<Record>::iterator iter = content.begin(); iter < content.end(); iter++)
{
if(iter->Section == SectionName) // Is this the right section?
iter->Commented = ' '; // Remove the comment value
}
return Save(FileName,content); // Save
}
return false; // In the event the file does not load
}
bool CIniFile::DeleteRecord(string KeyName, string SectionName, string FileName)
{
vector<Record> content; // Holds the current record // Holds the current record
if (Load(FileName, content)) // Make sure the file is loaded
{
vector<Record>::iterator iter = std::find_if(content.begin(),
content.end(),
CIniFile::RecordSectionKeyIs(SectionName,KeyName)); // Locate the Section/Key
if (iter == content.end()) return false; // The Section/Key was not found
content.erase(iter); // Remove the Record
return Save(FileName,content); // Save
}
return false; // In the event the file does not load
}
bool CIniFile::DeleteSection(string SectionName, string FileName)
{
vector<Record> content; // Holds the current record // Holds the current record
if (Load(FileName, content)) // Make sure the file is loaded
{
for(int i=(int)content.size()-1;i>-1;i--) // Iterate backwards through the content
{
if(content[i].Section == SectionName) // Is this related to the Section?
content.erase (content.begin()+i); // Then erase it
}
return Save(FileName,content); // Save
}
return false; // In the event the file does not load
}
bool CIniFile::SetSectionComments(string Comments, string SectionName, string FileName)
{
vector<Record> content; // Holds the current record // Holds the current record
if (Load(FileName, content)) // Make sure the file is loaded
{
for(vector<Record>::iterator iter = content.begin(); iter < content.end(); iter++) // Loop through the records
{
if((iter->Section == SectionName) && // Is this the Section?
(iter->Key == "")) // And not a record
{
if (Comments.size() >= 2) // Is there a comment?
{
if (Comments.substr(Comments.size()-2) != "\n") // Does the string end in a newline?
Comments += "\n"; // If not, add one
}
iter->Comments = Comments; // Set the comments
return Save(FileName,content); // Save
}
}
}
return false; // In the event the file does not load
}
bool CIniFile::SetRecordComments(string Comments, string KeyName, string SectionName, string FileName)
{
vector<Record> content; // Holds the current record // Holds the current record
if (Load(FileName, content)) // Make sure the file is loaded
{
vector<Record>::iterator iter = std::find_if(content.begin(),
content.end(),
CIniFile::RecordSectionKeyIs(SectionName,KeyName)); // Locate the Section/Key
if (iter == content.end()) return false; // The Section/Key was not found
if (Comments.size() >= 2) // Is there a comment?
{
if (Comments.substr(Comments.size()-2) != "\n") // Does the string end in a newline?
Comments += "\n"; // If not, add one
}
iter->Comments = Comments; // Set the comments
return Save(FileName,content); // Save
}
return false; // In the event the file does not load
}
vector<CIniFile::Record> CIniFile::GetSections(string FileName)
{
vector<Record> data; // Holds the return data
vector<Record> content; // Holds the current record // Holds the current record
if (Load(FileName, content)) // Make sure the file is loaded
{
for (int i=0;i<(int)content.size();i++) // Loop through the content
{
if(content[i].Key == "") // If this is a section
data.push_back(content[i]); // Add the record to the return data
}
}
return data; // Return the data
}
bool CIniFile::Sort(string FileName, bool Descending)
{
vector<CIniFile::Record> content; // Used to hold the sorted content
vector<CIniFile::Record> sections = GetSections(FileName); // Get a list of Sections
if(!sections.empty()) // Is there anything to process?
{
if(Descending) // Descending or Ascending?
std::sort(sections.begin(), sections.end(), DescendingSectionSort());
else // Sort the Sections
std::sort(sections.begin(), sections.end(), AscendingSectionSort());
for(vector<Record>::iterator iter = sections.begin(); iter < sections.end(); iter++) // For each Section
{
content.push_back(*iter); // Add the sorted Section to the content
vector<CIniFile::Record> records = GetSection(iter->Section ,FileName); // Get a list of Records for this section
if(Descending) // Descending or Ascending?
std::sort(records.begin(), records.end(), DescendingRecordSort());
else // Sort the Records
std::sort(records.begin(), records.end(), AscendingRecordSort());
for(vector<Record>::iterator it = records.begin(); it < records.end(); it++) // For each Record
content.push_back(*it); // Add the sorted Record to the content
}
return Save(FileName,content); // Save
}
return false; // There were no sections
}
bool CIniFile::AddSection(string SectionName, string FileName)
{
vector<Record> content; // Holds the current record // Holds the current record
if (Load(FileName, content)) // Make sure the file is loaded
{
Record s = {"",' ',SectionName,"",""}; // Define a new section
content.push_back(s); // Add the section
return Save(FileName,content); // Save
}
return false; // The file did not open
}
bool CIniFile::Create(string FileName)
{
vector<Record> content; // Create empty content
return Save(FileName,content); // Save
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -