2
* Copyright 2010 Inalogic® Inc.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License, as
6
* published by the Free Software Foundation; either version 2.1 or 3.0
9
* This program is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranties of
11
* MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
12
* PURPOSE. See the applicable version of the GNU Lesser General Public
13
* License for more details.
15
* You should have received a copy of both the GNU Lesser General Public
16
* License along with this program. If not, see <http://www.gnu.org/licenses/>
18
* Authored by: Jay Taoko <jaytaoko@inalogic.com>
23
#include "NuxCore/NuxCore.h"
24
#include "GLResource.h"
30
struct RecordSectionIs : std::unary_function<CIniFile::Record, bool>
34
RecordSectionIs (const std::string §ion) : section_ (section) {}
36
bool operator() ( const CIniFile::Record &rec ) const
38
return rec.Section == section_;
42
struct RecordSectionKeyIs : std::unary_function<CIniFile::Record, bool>
47
RecordSectionKeyIs (const std::string §ion, const std::string &key) : section_ (section), key_ (key) {}
49
bool operator() ( const CIniFile::Record &rec ) const
51
return ( (rec.Section == section_) && (rec.Key == key_) );
55
struct AscendingSectionSort
57
bool operator() (const CIniFile::Record &Start, const CIniFile::Record &End)
59
return Start.Section < End.Section;
63
struct DescendingSectionSort
65
bool operator() (const CIniFile::Record &Start, const CIniFile::Record &End)
67
return Start.Section > End.Section;
71
struct AscendingRecordSort
73
bool operator() (const CIniFile::Record &Start, const CIniFile::Record &End)
75
return Start.Key < End.Key;
79
struct DescendingRecordSort
81
bool operator() (const CIniFile::Record &Start, const CIniFile::Record &End)
83
return Start.Key > End.Key;
87
CIniFile::CIniFile (void) // Default constructor
91
CIniFile::~CIniFile (void)
95
// A function to trim whitespace from both sides of a given string
96
void Trim (std::string &str, const std::string &ChrsToTrim = " \t\n\r", int TrimDir = 0)
98
size_t startIndex = str.find_first_not_of (ChrsToTrim);
100
if (startIndex == std::string::npos)
106
if (TrimDir < 2) str = str.substr (startIndex, str.size() - startIndex);
108
if (TrimDir != 1) str = str.substr (0, str.find_last_not_of (ChrsToTrim) + 1);
111
//inline void TrimRight(std::string& str, const std::string & ChrsToTrim = " \t\n\r")
113
// Trim(str, ChrsToTrim, 2);
116
//inline void TrimLeft(std::string& str, const std::string & ChrsToTrim = " \t\n\r")
118
// Trim(str, ChrsToTrim, 1);
121
// A function to transform a string to uppercase if neccessary
122
//void UCase(string& str, bool ucase)
124
// if(ucase) transform(str.begin(), str.end(), str.begin(), toupper);
127
bool CIniFile::Load (std::string FileName, std::vector<Record>& content)
129
std::string s; // Holds the current line from the ini file
130
std::string CurrentSection; // Holds the current section name
132
std::ifstream inFile (FileName.c_str() ); // Create an input filestream
134
if (!inFile.is_open() ) return false; // If the input file doesn't open, then return
136
content.clear(); // Clear the content vector
138
std::string comments = ""; // A string to store comments in
140
while (!inFile.eof() /*!std::getline(inFile, s).eof()*/) // Read until the end of the file
142
std::getline (inFile, s);
143
Trim (s); // Trim whitespace from the ends
145
if (!s.empty() ) // Make sure its not a blank line
147
Record r; // Define a new record
149
if ( (s[0] == '#') || (s[0] == ';') ) // Is this a commented line?
151
if ( (s.find ('[') == std::string::npos) && // If there is no [ or =
152
(s.find ('=') == std::string::npos) ) // Then it's a comment
154
comments += s + '\n'; // Add the comment to the current comments string
158
r.Commented = s[0]; // Save the comment character
159
s.erase (s.begin() ); // Remove the comment for further processing
161
}// Remove any more whitespace
163
else r.Commented = ' '; // else mark it as not being a comment
165
if (s.find ('[') != std::string::npos) // Is this line a section?
167
s.erase (s.begin() ); // Erase the leading bracket
168
s.erase (s.find (']') ); // Erase the trailing bracket
170
r.Comments = comments; // Add the comments string (if any)
171
comments = ""; // Clear the comments for re-use
172
r.Section = s; // Set the Section value
173
r.Key = ""; // Set the Key value
174
r.Value = ""; // Set the Value value
178
if (s.find ('=') != std::string::npos) // Is this line a Key/Value?
180
r.Comments = comments; // Add the comments string (if any)
181
comments = ""; // Clear the comments for re-use
182
r.Section = CurrentSection; // Set the section to the current Section
183
r.Key = s.substr (0, s.find ('=') ); // Set the Key value to everything before the = sign
185
r.Value = s.substr (s.find ('=') + 1); // Set the Value to everything after the = sign
189
if (comments == "") // Don't add a record yet if its a comment line
190
content.push_back (r); // Add the record to content
194
inFile.close(); // Close the file
198
bool CIniFile::Save (std::string FileName, std::vector<Record>& content)
200
std::ofstream outFile (FileName.c_str() ); // Create an output filestream
202
if (!outFile.is_open() ) return false; // If the output file doesn't open, then return
204
for (int i = 0; i < (int) content.size(); i++) // Loop through each vector
206
outFile << content[i].Comments; // Write out the comments
208
if (content[i].Key == "") // Is this a section?
209
outFile << content[i].Commented << "["
210
<< content[i].Section << "]" << std::endl; // Then format the section
212
outFile << content[i].Commented << content[i].Key
213
<< "=" << content[i].Value << std::endl; // Else format a key/value
216
outFile.close(); // Close the file
220
std::string CIniFile::Content (std::string FileName)
222
std::string s = ""; // Hold our return string
223
std::vector<Record> content; // Holds the current record // Holds the current record
225
if (Load (FileName, content) ) // Make sure the file loads
227
for (int i = 0; i < (int) content.size(); i++) // Loop through the content
229
if (content[i].Comments != "") s += content[i].Comments; // Add the comments
231
if (content[i].Commented != ' ') s += content[i].Commented; // If this is commented, then add it
233
if ( (content[i].Key == "") ) // Is this a section?
234
s += '[' + content[i].Section + ']'; // Add the section
235
else s += content[i].Key + '=' + content[i].Value; // Or the Key value to the return srting
237
if (i != (int) content.size() ) s += '\n'; // If this is not the last line, add a CrLf
240
return s; // Return the contents
246
std::vector<std::string> CIniFile::GetSectionNames (std::string FileName)
248
std::vector<std::string> data; // Holds the return data
249
std::vector<Record> content; // Holds the current record // Holds the current record
251
if (Load (FileName, content) ) // Make sure the file is loaded
253
for (int i = 0; i < (int) content.size(); i++) // Loop through the content
255
if (content[i].Key == "") // If there is no key value, then its a section
256
data.push_back (content[i].Section); // Add the section to the return data
260
return data; // Return the data
263
std::vector<CIniFile::Record> CIniFile::GetSection (std::string SectionName, std::string FileName)
265
std::vector<Record> data; // Holds the return data
266
std::vector<Record> content; // Holds the current record // Holds the current record
268
if (Load (FileName, content) ) // Make sure the file is loaded
270
for (int i = 0; i < (int) content.size(); i++) // Loop through the content
272
if ( (content[i].Section == SectionName) && // If this is the section name we want
273
(content[i].Key != "") ) // but not the section name itself
274
data.push_back (content[i]); // Add the record to the return data
278
return data; // Return the data
281
bool CIniFile::RecordExists (std::string KeyName, std::string SectionName, std::string FileName)
283
std::vector<Record> content; // Holds the current record // Holds the current record
285
if (Load (FileName, content) ) // Make sure the file is loaded
287
std::vector<Record>::iterator iter = std::find_if (content.begin(),
289
RecordSectionKeyIs (SectionName, KeyName) ); // Locate the Section/Key
291
if (iter == content.end() ) return false; // The Section/Key was not found
294
return true; // The Section/Key was found
297
bool CIniFile::SectionExists (std::string SectionName, std::string FileName)
299
std::vector<Record> content; // Holds the current record // Holds the current record
301
if (Load (FileName, content) ) // Make sure the file is loaded
303
std::vector<Record>::iterator iter = std::find_if (content.begin(),
305
RecordSectionIs (SectionName) ); // Locate the Section
307
if (iter == content.end() ) return false; // The Section was not found
310
return true; // The Section was found
313
std::vector<CIniFile::Record> CIniFile::GetRecord (std::string KeyName, std::string SectionName, std::string FileName)
315
std::vector<Record> data; // Holds the return data
316
std::vector<Record> content; // Holds the current record // Holds the current record
318
if (Load (FileName, content) ) // Make sure the file is loaded
320
std::vector<Record>::iterator iter = std::find_if (content.begin(),
322
RecordSectionKeyIs (SectionName, KeyName) ); // Locate the Record
324
if (iter == content.end() ) return data; // The Record was not found
326
data.push_back (*iter); // The Record was found
329
return data; // Return the Record
332
std::string CIniFile::GetValue (std::string KeyName, std::string SectionName, std::string FileName)
334
std::vector<Record> content = GetRecord (KeyName, SectionName, FileName); // Get the Record
336
if (!content.empty() ) // Make sure there is a value to return
337
return content[0].Value; // And return the value
339
return ""; // No value was found
342
bool CIniFile::SetValue (std::string KeyName, std::string Value, std::string SectionName, std::string FileName)
344
std::vector<Record> content; // Holds the current record // Holds the current record
346
if (Load (FileName, content) ) // Make sure the file is loaded
348
if (!SectionExists (SectionName, FileName) ) // If the Section doesn't exist
350
Record s = {"", ' ', SectionName, "", ""}; // Define a new section
351
Record r = {"", ' ', SectionName, KeyName, Value}; // Define a new record
352
content.push_back (s); // Add the section
353
content.push_back (r); // Add the record
354
return Save (FileName, content); // Save
357
if (!RecordExists (KeyName, SectionName, FileName) ) // If the Key doesn't exist
359
std::vector<Record>::iterator iter = std::find_if (content.begin(),
361
RecordSectionIs (SectionName) ); // Locate the Section
362
iter++; // Advance just past the section
363
Record r = {"", ' ', SectionName, KeyName, Value}; // Define a new record
364
content.insert (iter, r); // Add the record
365
return Save (FileName, content); // Save
368
std::vector<Record>::iterator iter = std::find_if (content.begin(),
370
RecordSectionKeyIs (SectionName, KeyName) ); // Locate the Record
372
iter->Value = Value; // Insert the correct value
373
return Save (FileName, content); // Save
376
return false; // In the event the file does not load
379
bool CIniFile::RenameSection (std::string OldSectionName, std::string NewSectionName, std::string FileName)
381
std::vector<Record> content; // Holds the current record // Holds the current record
383
if (Load (FileName, content) ) // Make sure the file is loaded
385
for (std::vector<Record>::iterator iter = content.begin();
386
iter < content.end(); iter++) // Loop through the records
388
if (iter->Section == OldSectionName) // Is this the OldSectionName?
389
iter->Section = NewSectionName; // Now its the NewSectionName
392
return Save (FileName, content); // Save
395
return false; // In the event the file does not load
398
bool CIniFile::CommentRecord (CommentChar cc, std::string KeyName, std::string SectionName, std::string FileName)
400
std::vector<Record> content; // Holds the current record // Holds the current record
402
if (Load (FileName, content) ) // Make sure the file is loaded
404
std::vector<Record>::iterator iter = std::find_if (content.begin(),
406
RecordSectionKeyIs (SectionName, KeyName) ); // Locate the Section/Key
408
if (iter == content.end() ) return false; // The Section/Key was not found
410
iter->Commented = cc; // Change the Comment value
411
return Save (FileName, content); // Save
415
return false; // In the event the file does not load
418
bool CIniFile::UnCommentRecord (std::string KeyName, std::string SectionName, std::string FileName)
420
std::vector<Record> content; // Holds the current record // Holds the current record
422
if (Load (FileName, content) ) // Make sure the file is loaded
424
std::vector<Record>::iterator iter = std::find_if (content.begin(),
426
RecordSectionKeyIs (SectionName, KeyName) ); // Locate the Section/Key
428
if (iter == content.end() ) return false; // The Section/Key was not found
430
iter->Commented = ' '; // Remove the Comment value
431
return Save (FileName, content); // Save
435
return false; // In the event the file does not load
438
bool CIniFile::CommentSection (char CommentChar, std::string SectionName, std::string FileName)
440
std::vector<Record> content; // Holds the current record // Holds the current record
442
if (Load (FileName, content) ) // Make sure the file is loaded
444
for (std::vector<Record>::iterator iter = content.begin(); iter < content.end(); iter++)
446
if (iter->Section == SectionName) // Is this the right section?
447
iter->Commented = CommentChar; // Change the comment value
450
return Save (FileName, content); // Save
453
return false; // In the event the file does not load
456
bool CIniFile::UnCommentSection (std::string SectionName, std::string FileName)
458
std::vector<Record> content; // Holds the current record // Holds the current record
460
if (Load (FileName, content) ) // Make sure the file is loaded
462
for (std::vector<Record>::iterator iter = content.begin(); iter < content.end(); iter++)
464
if (iter->Section == SectionName) // Is this the right section?
465
iter->Commented = ' '; // Remove the comment value
468
return Save (FileName, content); // Save
471
return false; // In the event the file does not load
474
bool CIniFile::DeleteRecord (std::string KeyName, std::string SectionName, std::string FileName)
476
std::vector<Record> content; // Holds the current record // Holds the current record
478
if (Load (FileName, content) ) // Make sure the file is loaded
480
std::vector<Record>::iterator iter = std::find_if (content.begin(),
482
RecordSectionKeyIs (SectionName, KeyName) ); // Locate the Section/Key
484
if (iter == content.end() ) return false; // The Section/Key was not found
486
content.erase (iter); // Remove the Record
487
return Save (FileName, content); // Save
491
return false; // In the event the file does not load
494
bool CIniFile::DeleteSection (std::string SectionName, std::string FileName)
496
std::vector<Record> content; // Holds the current record // Holds the current record
498
if (Load (FileName, content) ) // Make sure the file is loaded
500
for (int i = (int) content.size() - 1; i > -1; i--) // Iterate backwards through the content
502
if (content[i].Section == SectionName) // Is this related to the Section?
503
content.erase (content.begin() + i); // Then erase it
506
return Save (FileName, content); // Save
509
return false; // In the event the file does not load
512
bool CIniFile::SetSectionComments (std::string Comments, std::string SectionName, std::string FileName)
514
std::vector<Record> content; // Holds the current record // Holds the current record
516
if (Load (FileName, content) ) // Make sure the file is loaded
518
for (std::vector<Record>::iterator iter = content.begin(); iter < content.end(); iter++) // Loop through the records
520
if ( (iter->Section == SectionName) && // Is this the Section?
521
(iter->Key == "") ) // And not a record
523
if (Comments.size() >= 2) // Is there a comment?
525
if (Comments.substr (Comments.size() - 2) != "\n") // Does the string end in a newline?
526
Comments += "\n"; // If not, add one
529
iter->Comments = Comments; // Set the comments
531
return Save (FileName, content); // Save
536
return false; // In the event the file does not load
539
bool CIniFile::SetRecordComments (std::string Comments, std::string KeyName, std::string SectionName, std::string FileName)
541
std::vector<Record> content; // Holds the current record // Holds the current record
543
if (Load (FileName, content) ) // Make sure the file is loaded
545
std::vector<Record>::iterator iter = std::find_if (content.begin(),
547
RecordSectionKeyIs (SectionName, KeyName) ); // Locate the Section/Key
549
if (iter == content.end() ) return false; // The Section/Key was not found
551
if (Comments.size() >= 2) // Is there a comment?
553
if (Comments.substr (Comments.size() - 2) != "\n") // Does the string end in a newline?
554
Comments += "\n"; // If not, add one
557
iter->Comments = Comments; // Set the comments
558
return Save (FileName, content); // Save
562
return false; // In the event the file does not load
565
std::vector<CIniFile::Record> CIniFile::GetSections (std::string FileName)
567
std::vector<Record> data; // Holds the return data
568
std::vector<Record> content; // Holds the current record // Holds the current record
570
if (Load (FileName, content) ) // Make sure the file is loaded
572
for (int i = 0; i < (int) content.size(); i++) // Loop through the content
574
if (content[i].Key == "") // If this is a section
575
data.push_back (content[i]); // Add the record to the return data
579
return data; // Return the data
583
bool CIniFile::Sort (std::string FileName, bool Descending)
585
std::vector<CIniFile::Record> content; // Used to hold the sorted content
586
std::vector<CIniFile::Record> sections = GetSections (FileName); // Get a list of Sections
588
if (!sections.empty() ) // Is there anything to process?
591
if (Descending) // Descending or Ascending?
592
std::sort (sections.begin(), sections.end(), DescendingSectionSort() );
593
else // Sort the Sections
594
std::sort (sections.begin(), sections.end(), AscendingSectionSort() );
596
for (std::vector<Record>::iterator iter = sections.begin(); iter < sections.end(); iter++) // For each Section
598
content.push_back (*iter); // Add the sorted Section to the content
600
std::vector<CIniFile::Record> records = GetSection (iter->Section , FileName); // Get a list of Records for this section
602
if (Descending) // Descending or Ascending?
603
std::sort (records.begin(), records.end(), DescendingRecordSort() );
604
else // Sort the Records
605
std::sort (records.begin(), records.end(), AscendingRecordSort() );
607
for (std::vector<Record>::iterator it = records.begin(); it < records.end(); it++) // For each Record
608
content.push_back (*it); // Add the sorted Record to the content
611
return Save (FileName, content); // Save
614
return false; // There were no sections
617
bool CIniFile::AddSection (std::string SectionName, std::string FileName)
619
std::vector<Record> content; // Holds the current record // Holds the current record
621
if (Load (FileName, content) ) // Make sure the file is loaded
623
Record s = {"", ' ', SectionName, "", ""}; // Define a new section
624
content.push_back (s); // Add the section
625
return Save (FileName, content); // Save
628
return false; // The file did not open
631
bool CIniFile::Create (std::string FileName)
633
std::vector<Record> content; // Create empty content
634
return Save (FileName, content); // Save
638
void Show (std::string FileName)
640
std::cout << std::endl
641
<< "++++++++++++++++++++++++++++++++++++++++"
643
<< "+ Contents of the file are below +"
645
<< "++++++++++++++++++++++++++++++++++++++++"
647
<< CIniFile::Content (FileName)
649
<< "++++++++++++++++++++++++++++++++++++++++"
650
<< std::endl << std::endl;
656
std::string FileName = "test.ini";
659
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
660
std::cout << "Attempting to create a new file called \"test.ini\"" << std::endl << std::endl;
661
std::cout << "string FileName = \"test.ini\";" << std::endl;
662
std::cout << "CIniFile::Create(FileName);" << std::endl << std::endl;
664
if (CIniFile::Create (FileName) ) std::cout << "File was successfully created" << std::endl << std::endl;
665
else std::cout << "Failed to create the file" << std::endl << std::endl;
669
// Create a new section
670
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
671
std::cout << "Attempting to create a new section called [MySection]" << std::endl << std::endl;
672
std::cout << "CIniFile::AddSection(\"MySection\", FileName);" << std::endl << std::endl;
674
if (CIniFile::AddSection ("MySection", FileName) ) std::cout << "Section was successfully created" << std::endl << std::endl;
675
else std::cout << "Failed to create the section" << std::endl << std::endl;
679
// Add a key to the section
680
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
681
std::cout << "Attempting to add a new key/value (MyKey=MyValue) to [MySection]" << std::endl << std::endl;
682
std::cout << "CIniFile::SetValue(\"MyKey\",\"MyValue\",\"MySection\",FileName);" << std::endl << std::endl;
684
if (CIniFile::SetValue ("MyKey", "MyValue", "MySection", FileName) ) std::cout << "Record was successfully created" << std::endl << std::endl;
685
else std::cout << "Failed to create the record" << std::endl << std::endl;
689
// Add a key and create a section
690
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
691
std::cout << "Attempting to add a new key/value (TestKey=TestValue)" << std::endl << "and create a new section [TestSection] at the same time" << std::endl << std::endl;
692
std::cout << "CIniFile::SetValue(\"TestKey\",\"TestValue\",\"TestSection\",FileName);" << std::endl << std::endl;
694
if (CIniFile::SetValue ("TestKey", "TestValue", "TestSection", FileName) ) std::cout << "Record and section were successfully created" << std::endl << std::endl;
695
else std::cout << "Failed to create the record and section" << std::endl << std::endl;
699
// Change a key value
700
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
701
std::cout << "Attempting to change the key/value for (MyKey=MyValue) to (MyKey=YourValue)" << std::endl << std::endl;
702
std::cout << "CIniFile::SetValue(\"MyKey\",\"YourValue\",\"MySection\",FileName);" << std::endl << std::endl;
704
if (CIniFile::SetValue ("MyKey", "YourValue", "MySection", FileName) ) std::cout << "Record was successfully changed" << std::endl << std::endl;
705
else std::cout << "Failed to change the record" << std::endl << std::endl;
710
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
711
std::cout << "Attempting to get the value of MyKey" << std::endl << std::endl;
712
std::cout << "CIniFile::GetValue(\"MyKey\",\"MySection\",FileName);" << std::endl << std::endl;
713
std::string v = CIniFile::GetValue ("MyKey", "MySection", FileName);
714
std::cout << "The value of MyKey is: " << v.c_str() << std::endl << std::endl;
717
// Get a list of Sections
718
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
719
std::cout << "Attempting to get a list of sections" << std::endl << std::endl;
720
std::cout << "CIniFile::GetSectionNames(FileName);" << std::endl << std::endl;
721
std::vector<std::string> s = CIniFile::GetSectionNames (FileName);
722
std::cout << "The sections are returned as a std::vector<std::string>\n\n";
724
for (int i = 0; i < (int) s.size(); i++)
725
std::cout << s[i].c_str() << std::endl;
730
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
731
std::cout << "Attempting to verify that [MySection] exists" << std::endl << std::endl;
732
std::cout << "CIniFile::SectionExists(\"MySection\",FileName);" << std::endl << std::endl;
734
if (CIniFile::SectionExists ("MySection", FileName) ) std::cout << "Section exists" << std::endl << std::endl;
735
else std::cout << "Section does not exist" << std::endl << std::endl;
740
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
741
std::cout << "Attempting to verify that MyKey exists" << std::endl << std::endl;
742
std::cout << "CIniFile::RecordExists(\"MyKey\",\"MySection\",FileName);" << std::endl << std::endl;
744
if (CIniFile::RecordExists ("MyKey", "MySection", FileName) ) std::cout << "Record exists" << std::endl << std::endl;
745
else std::cout << "Record does not exist" << std::endl << std::endl;
750
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
751
std::cout << "BE CAREFUL - functions in CIniFile are CASE-SENSITIVE" << std::endl << std::endl;
752
std::cout << "CIniFile::RecordExists(\"mykey\",\"MySection\",FileName);" << std::endl << std::endl;
754
if (CIniFile::RecordExists ("mykey", "MySection", FileName) ) std::cout << "Record exists" << std::endl << std::endl;
755
else std::cout << "Record does not exist" << std::endl << std::endl;
759
// Add a comment to the section
760
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
761
std::cout << "Attempting to add comments to [MySection]" << std::endl << std::endl;
762
std::cout << "CIniFile::SetSectionComments(\"# This Section was created by CIniFile\\n\",\"MySection\",FileName);" << std::endl << std::endl;
764
if (CIniFile::SetSectionComments ("# This Section was created by CIniFile\n", "MySection", FileName) ) std::cout << "Comments were successfully added" << std::endl << std::endl;
765
else std::cout << "Failed to add the comments" << std::endl << std::endl;
769
// Add a comment to the record
770
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
771
std::cout << "Attempting to add comments to MyKey" << std::endl << std::endl;
772
std::cout << "CIniFile::SetRecordComments(\"# This Key was created by CIniFile\\n\",\"MyKey\",\"MySection\",FileName);" << std::endl << std::endl;
774
if (CIniFile::SetRecordComments ("# This Key was created by CIniFile\n", "MyKey", "MySection", FileName) ) std::cout << "Comments were successfully added" << std::endl << std::endl;
775
else std::cout << "Failed to add the comments" << std::endl << std::endl;
780
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
781
std::cout << "Attempting to rename [MySection] to [YourSection]" << std::endl << std::endl;
782
std::cout << "CIniFile::RenameSection(\"MySection\",\"YourSection\",FileName);" << std::endl << std::endl;
784
if (CIniFile::RenameSection ("MySection", "YourSection", FileName) ) std::cout << "Section was successfully changed" << std::endl << std::endl;
785
else std::cout << "Failed to change the section" << std::endl << std::endl;
790
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
791
std::cout << "Multiple comments can be added by putting \\n# in the comments string" << std::endl << std::endl;
792
std::cout << "CIniFile::SetSectionComments(\"# This Section was created by CIniFile\\n# Kids, don't try this at home \\n\",\"YourSection\",FileName);" << std::endl << std::endl;
794
if (CIniFile::SetSectionComments ("# This Section was created by CIniFile\n# Kids, don't try this at home", "YourSection", FileName) ) std::cout << "Comments were successfully added" << std::endl << std::endl;
795
else std::cout << "Failed to add the comments" << std::endl << std::endl;
800
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
801
std::cout << "Comments are removed by setting them to \"\"" << std::endl << std::endl;
802
std::cout << "CIniFile::SetRecordComments(\"\",\"MyKey\",\"YourSection\",FileName);" << std::endl << std::endl;
804
if (CIniFile::SetRecordComments ("", "MyKey", "YourSection", FileName) ) std::cout << "Comments were successfully removed" << std::endl << std::endl;
805
else std::cout << "Failed to remove the comments" << std::endl << std::endl;
809
// Comment entire section
810
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
811
std::cout << "Attempting to comment the entire section [YourSection]" << std::endl << std::endl;
812
std::cout << "CIniFile::CommentSection(\"#\",\"YourSection\",FileName);" << std::endl << std::endl;
814
if (CIniFile::CommentSection ('#', "YourSection", FileName) ) std::cout << "Section was successfully commented" << std::endl << std::endl;
815
else std::cout << "Failed to comment the section" << std::endl << std::endl;
819
// UnComment entire section
820
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
821
std::cout << "Attempting to un-comment the entire section [YourSection]" << std::endl << std::endl;
822
std::cout << "CIniFile::UnCommentSection(\"YourSection\",FileName);" << std::endl << std::endl;
824
if (CIniFile::UnCommentSection ("YourSection", FileName) ) std::cout << "Section was successfully un-commented" << std::endl << std::endl;
825
else std::cout << "Failed to un-comment the section" << std::endl << std::endl;
829
// Comment a single record
830
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
831
std::cout << "Attempting to comment the record MyKey" << std::endl << std::endl;
832
std::cout << "(Note that both # and ; are recognized as commented lines by CIniFile)" << std::endl << std::endl;
833
std::cout << "CIniFile::CommentRecord(CIniFile::CommentChar::Pound,\"MyKey\",\"YourSection\",FileName);" << std::endl << std::endl;
835
if (CIniFile::CommentRecord (CIniFile::Pound, "MyKey", "YourSection", FileName) )
836
std::cout << "Record was successfully commented" << std::endl << std::endl;
838
std::cout << "Failed to comment the record" << std::endl << std::endl;
842
// UnComment a single record
843
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
844
std::cout << "Attempting to un-comment the record MyKey" << std::endl << std::endl;
845
std::cout << "CIniFile::UnCommentRecord(\"MyKey\",\"YourSection\",FileName);" << std::endl << std::endl;
847
if (CIniFile::UnCommentRecord ("MyKey", "YourSection", FileName) ) std::cout << "Record was successfully un-commented" << std::endl << std::endl;
848
else std::cout << "Failed to un-comment the record" << std::endl << std::endl;
853
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
854
std::cout << "Attempting to sort the file - false means ASCENDING, true means DESCENDING" << std::endl << std::endl;
855
std::cout << "(Note that the comments will stay with their targets)" << std::endl << std::endl;
856
std::cout << "CIniFile::Sort(FileName,false);" << std::endl << std::endl;
858
if (CIniFile::Sort (FileName, false) ) std::cout << "File was successfully sorted" << std::endl << std::endl;
859
else std::cout << "Failed to sort the file" << std::endl << std::endl;
863
// Delete entire section
864
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
865
std::cout << "Attempting to delete the entire section [TestSection]" << std::endl << std::endl;
866
std::cout << "CIniFile::DeleteSection(\"TestSection\",FileName);" << std::endl << std::endl;
868
if (CIniFile::DeleteSection ("TestSection", FileName) ) std::cout << "Section was successfully deleted" << std::endl << std::endl;
869
else std::cout << "Failed to delete the section" << std::endl << std::endl;
874
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
875
std::cout << "Attempting to delete the record <yKey" << std::endl << std::endl;
876
std::cout << "CIniFile::DeleteRecord(\"MyKey\",\"YourSection\",FileName);" << std::endl << std::endl;
878
if (CIniFile::DeleteRecord ("MyKey", "YourSection", FileName) ) std::cout << "Record was successfully deleted" << std::endl << std::endl;
879
else std::cout << "Failed to delete the record" << std::endl << std::endl;
884
std::cout << "TestIniFile - Demo program for the CIniFile Class" << std::endl << std::endl;
885
std::cout << "Finally, the content of the file can be retrieved as a std::string" << std::endl << std::endl;
886
std::cout << "CIniFile::Content(FileName);" << std::endl << std::endl;
887
std::cout << "The contents of the file throughout this demo have used this function to display the contents below" << std::endl;