AGS: Common: in IniFile replaced string indexes from int with size_t

This is long overdue since String class changed int indexes to size_t.

From upstream b81f9e31e8b8bbbbf46e570427d16deea5102d3a
This commit is contained in:
Walter Agazzi 2023-04-22 18:42:48 +02:00 committed by Eugene Sandulenko
parent c3fc687b51
commit 1b3a417e75
3 changed files with 33 additions and 27 deletions

View File

@ -41,7 +41,7 @@ IniFile::ItemDef::ItemDef(const String &key, const String &value) {
Value.second = Value.first + value.GetLength();
}
IniFile::ItemDef::ItemDef(const String &line, const StrPos &key, const StrPos &value, int sep_at) {
IniFile::ItemDef::ItemDef(const String &line, const StrPos &key, const StrPos &value, size_t sep_at) {
Line = line;
Key = key;
Value = value;
@ -53,7 +53,7 @@ void IniFile::ItemDef::SetKey(const String &key) {
return;
if (IsKeyValue()) {
int diff = key.GetLength() - (Key.second - Key.first);
size_t diff = key.GetLength() - (Key.second - Key.first);
ReplaceSubString(Line, Key, key);
Key.second += diff;
Value.first += diff;
@ -67,8 +67,8 @@ void IniFile::ItemDef::SetValue(const String &value) {
if (!IsKeyValue())
return; // no key
if (SepAt > 0) { // replacing existing value
int diff = static_cast<int>(value.GetLength()) - (Value.second - Value.first);
if (SepAt != String::NoIndex) { // replacing existing value
size_t diff = value.GetLength() - (Value.second - Value.first);
ReplaceSubString(Line, Value, value);
Value.second += diff;
} else { // inserting value behind the key
@ -98,7 +98,7 @@ void IniFile::SectionDef::SetName(const String &sec_name) {
if (sec_name.IsEmpty())
return;
int diff = sec_name.GetLength() - (Name.second - Name.first);
size_t diff = sec_name.GetLength() - (Name.second - Name.first);
ReplaceSubString(Header, Name, sec_name);
Name.second += diff;
}
@ -198,7 +198,7 @@ void IniFile::Read(Stream *in) {
if ((endl - pstr >= 2 && *pstr == '/' && *(pstr + 1) == '/') ||
(endl - pstr >= 1 && (*pstr == '#' || *pstr == ';'))) {
StrPos nullpos(0, 0);
cur_section->InsertItem(cur_section->End(), ItemDef(line, nullpos, nullpos, -1));
cur_section->InsertItem(cur_section->End(), ItemDef(line, nullpos, nullpos, String::NoIndex));
continue;
}
@ -231,7 +231,7 @@ void IniFile::Read(Stream *in) {
// Create an item and parse value, if any
StrPos keypos(str_at - cstr, str_end - cstr);
StrPos valpos(0, 0);
int sep_at = -1;
size_t sep_at = String::NoIndex;
if (pstr != endl) {
sep_at = pstr - cstr;
ParsePaddedString(++pstr, endl, str_at, str_end);

View File

@ -44,10 +44,14 @@ class IniFile {
public:
// Position of a string in the line of text:
// is defined by a pair of first and next-after-last character indices
typedef std::pair<int, int> StrPos;
typedef std::pair<size_t, size_t> StrPos;
// Location of section in the array of text lines:
// is defined by a pair of first and next-after-last line indices
typedef std::pair<int, int> SectionPos;
typedef std::pair<size_t, size_t> SectionPos;
inline static bool IsValidStrPos(const StrPos &pos) {
return pos.first < pos.second;
}
// Item definition
// Valid key indicates a key-value line; no key means unparsed
@ -55,7 +59,7 @@ public:
class ItemDef {
public:
ItemDef(const String &key, const String &value);
ItemDef(const String &line, const StrPos &key, const StrPos &value, int sep_at);
ItemDef(const String &line, const StrPos &key, const StrPos &value, size_t sep_at);
String GetLine() const {
return Line;
}
@ -65,8 +69,9 @@ public:
String GetValue() const {
return SubString(Line, Value);
}
bool IsKeyValue() const {
return Key.second - Key.first > 0;
// Tells if this is a valid key/value item, which means that it has a valid key
bool IsKeyValue() const {
return IsValidStrPos(Key);
}
void SetKey(const String &key);
void SetValue(const String &value);
@ -74,7 +79,7 @@ public:
private:
String Line; // actual text
StrPos Key; // position of item key
int SepAt; // position of the separator (assignment) symbol
size_t SepAt; // position of the separator (assignment) symbol
StrPos Value; // position of item value
};
// Linked list of items
@ -96,8 +101,9 @@ public:
size_t GetItemCount() const {
return Items.size();
}
bool IsGlobal() const {
return Name.second - Name.first <= 0;
// Tells if this is a "global" section, which means that it has no name
bool IsGlobal() const {
return !IsValidStrPos(Name);
}
ItemIterator Begin() {
return Items.begin();

View File

@ -115,20 +115,20 @@ void Test_IniFile() {
delete fs;
// there are explicit sections and 1 implicit global one
const int section_count = 5;
const size_t section_count = 5;
// Test reading from the custom ini file
{
assert(ini.GetSectionCount() == section_count);
IniFile::ConstSectionIterator sec = ini.CBegin();
assert(sec->GetItemCount() == 1);
assert(sec->GetItemCount() == 1u);
IniFile::ConstItemIterator item = sec->CBegin();
assert(item->GetKey() == "global_item");
assert(item->GetValue() == "global_value");
++sec;
assert(sec->GetName() == "section1");
assert(sec->GetItemCount() == 5);
assert(sec->GetItemCount() == 5u);
item = sec->CBegin();
assert(item->GetKey() == "item1");
assert(item->GetValue() == "");
@ -146,7 +146,7 @@ void Test_IniFile() {
++sec;
assert(sec->GetName() == "this_section_should_be_deleted");
assert(sec->GetItemCount() == 3);
assert(sec->GetItemCount() == 3u);
item = sec->CBegin();
assert(item->GetKey() == "item1");
assert(item->GetValue() == "value1");
@ -158,7 +158,7 @@ void Test_IniFile() {
++sec;
assert(sec->GetName() == "section3");
assert(sec->GetItemCount() == 2);
assert(sec->GetItemCount() == 2u);
item = sec->CBegin();
assert(item->GetKey() == "item_to_be_deleted");
assert(item->GetValue() == "value");
@ -168,7 +168,7 @@ void Test_IniFile() {
++sec;
assert(sec->GetName() == "section4");
assert(sec->GetItemCount() == 1);
assert(sec->GetItemCount() == 1u);
item = sec->CBegin();
assert(item->GetKey() == "item1");
assert(item->GetValue() == "value");
@ -234,18 +234,18 @@ void Test_IniFile() {
ConfigTree tree;
IniUtil::Read("test.ini", tree);
assert(tree.size() == 5);
assert(tree.size() == 5u);
assert(tree.find("") != tree.end()); // global section
assert(tree.find("section1") != tree.end());
assert(tree.find("section3") != tree.end());
assert(tree.find("section4") != tree.end());
assert(tree.find("section5") != tree.end());
StringOrderMap &sub_tree = tree[""];
assert(sub_tree.size() == 1);
assert(sub_tree.size() == 1u);
assert(sub_tree.find("global_item") != sub_tree.end());
assert(sub_tree["global_item"] == "global_value");
sub_tree = tree["section1"];
assert(sub_tree.size() == 4);
assert(sub_tree.size() == 4u);
assert(sub_tree.find("item1") != sub_tree.end());
assert(sub_tree.find("item2") != sub_tree.end());
assert(sub_tree.find("item3") != sub_tree.end());
@ -255,11 +255,11 @@ void Test_IniFile() {
assert(sub_tree["item3"] == "value3");
assert(sub_tree["new_item"] == "new_value");
sub_tree = tree["section3"];
assert(sub_tree.size() == 1);
assert(sub_tree.size() == 1u);
assert(sub_tree.find("item_to_be_kept") != sub_tree.end());
assert(sub_tree["item_to_be_kept"] == "another value");
sub_tree = tree["section4"];
assert(sub_tree.size() == 3);
assert(sub_tree.size() == 3u);
assert(sub_tree.find("new_item1") != sub_tree.end());
assert(sub_tree.find("item1") != sub_tree.end());
assert(sub_tree.find("new_item2") != sub_tree.end());
@ -267,7 +267,7 @@ void Test_IniFile() {
assert(sub_tree["item1"] == "value");
assert(sub_tree["new_item2"] == "new_value2");
sub_tree = tree["section5"];
assert(sub_tree.size() == 3);
assert(sub_tree.size() == 3u);
assert(sub_tree.find("item5_1") != sub_tree.end());
assert(sub_tree.find("item5_2") != sub_tree.end());
assert(sub_tree.find("item5_3") != sub_tree.end());