Codechange: Use std::string in INI file parsing.
This commit is contained in:
@@ -19,12 +19,10 @@
|
||||
* Construct a new in-memory item of an Ini file.
|
||||
* @param parent the group we belong to
|
||||
* @param name the name of the item
|
||||
* @param last the last element of the name of the item
|
||||
*/
|
||||
IniItem::IniItem(IniGroup *parent, const char *name, const char *last) : next(nullptr), value(nullptr), comment(nullptr)
|
||||
IniItem::IniItem(IniGroup *parent, const std::string &name) : next(nullptr)
|
||||
{
|
||||
this->name = stredup(name, last);
|
||||
str_validate(this->name, this->name + strlen(this->name));
|
||||
this->name = str_validate(name);
|
||||
|
||||
*parent->last_item = this;
|
||||
parent->last_item = &this->next;
|
||||
@@ -33,10 +31,6 @@ IniItem::IniItem(IniGroup *parent, const char *name, const char *last) : next(nu
|
||||
/** Free everything we loaded. */
|
||||
IniItem::~IniItem()
|
||||
{
|
||||
free(this->name);
|
||||
free(this->value);
|
||||
free(this->comment);
|
||||
|
||||
delete this->next;
|
||||
}
|
||||
|
||||
@@ -46,20 +40,21 @@ IniItem::~IniItem()
|
||||
*/
|
||||
void IniItem::SetValue(const char *value)
|
||||
{
|
||||
free(this->value);
|
||||
this->value = stredup(value);
|
||||
if (value == nullptr) {
|
||||
this->value.reset();
|
||||
} else {
|
||||
this->value.emplace(value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Construct a new in-memory group of an Ini file.
|
||||
* @param parent the file we belong to
|
||||
* @param name the name of the group
|
||||
* @param last the last element of the name of the group
|
||||
*/
|
||||
IniGroup::IniGroup(IniLoadFile *parent, const char *name, const char *last) : next(nullptr), type(IGT_VARIABLES), item(nullptr), comment(nullptr)
|
||||
IniGroup::IniGroup(IniLoadFile *parent, const std::string &name) : next(nullptr), type(IGT_VARIABLES), item(nullptr)
|
||||
{
|
||||
this->name = stredup(name, last);
|
||||
str_validate(this->name, this->name + strlen(this->name));
|
||||
this->name = str_validate(name);
|
||||
|
||||
this->last_item = &this->item;
|
||||
*parent->last_group = this;
|
||||
@@ -67,7 +62,7 @@ IniGroup::IniGroup(IniLoadFile *parent, const char *name, const char *last) : ne
|
||||
|
||||
if (parent->list_group_names != nullptr) {
|
||||
for (uint i = 0; parent->list_group_names[i] != nullptr; i++) {
|
||||
if (strcmp(this->name, parent->list_group_names[i]) == 0) {
|
||||
if (this->name == parent->list_group_names[i]) {
|
||||
this->type = IGT_LIST;
|
||||
return;
|
||||
}
|
||||
@@ -75,7 +70,7 @@ IniGroup::IniGroup(IniLoadFile *parent, const char *name, const char *last) : ne
|
||||
}
|
||||
if (parent->seq_group_names != nullptr) {
|
||||
for (uint i = 0; parent->seq_group_names[i] != nullptr; i++) {
|
||||
if (strcmp(this->name, parent->seq_group_names[i]) == 0) {
|
||||
if (this->name == parent->seq_group_names[i]) {
|
||||
this->type = IGT_SEQUENCE;
|
||||
return;
|
||||
}
|
||||
@@ -86,9 +81,6 @@ IniGroup::IniGroup(IniLoadFile *parent, const char *name, const char *last) : ne
|
||||
/** Free everything we loaded. */
|
||||
IniGroup::~IniGroup()
|
||||
{
|
||||
free(this->name);
|
||||
free(this->comment);
|
||||
|
||||
delete this->item;
|
||||
delete this->next;
|
||||
}
|
||||
@@ -100,16 +92,16 @@ IniGroup::~IniGroup()
|
||||
* @param create whether to create an item when not found or not.
|
||||
* @return the requested item or nullptr if not found.
|
||||
*/
|
||||
IniItem *IniGroup::GetItem(const char *name, bool create)
|
||||
IniItem *IniGroup::GetItem(const std::string &name, bool create)
|
||||
{
|
||||
for (IniItem *item = this->item; item != nullptr; item = item->next) {
|
||||
if (strcmp(item->name, name) == 0) return item;
|
||||
if (item->name == name) return item;
|
||||
}
|
||||
|
||||
if (!create) return nullptr;
|
||||
|
||||
/* otherwise make a new one */
|
||||
return new IniItem(this, name, nullptr);
|
||||
return new IniItem(this, name);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,7 +121,6 @@ void IniGroup::Clear()
|
||||
*/
|
||||
IniLoadFile::IniLoadFile(const char * const *list_group_names, const char * const *seq_group_names) :
|
||||
group(nullptr),
|
||||
comment(nullptr),
|
||||
list_group_names(list_group_names),
|
||||
seq_group_names(seq_group_names)
|
||||
{
|
||||
@@ -139,7 +130,6 @@ IniLoadFile::IniLoadFile(const char * const *list_group_names, const char * cons
|
||||
/** Free everything we loaded. */
|
||||
IniLoadFile::~IniLoadFile()
|
||||
{
|
||||
free(this->comment);
|
||||
delete this->group;
|
||||
}
|
||||
|
||||
@@ -147,26 +137,21 @@ IniLoadFile::~IniLoadFile()
|
||||
* Get the group with the given name. If it doesn't exist
|
||||
* and \a create_new is \c true create a new group.
|
||||
* @param name name of the group to find.
|
||||
* @param len the maximum length of said name (\c 0 means length of the string).
|
||||
* @param create_new Allow creation of group if it does not exist.
|
||||
* @return The requested group if it exists or was created, else \c nullptr.
|
||||
*/
|
||||
IniGroup *IniLoadFile::GetGroup(const char *name, size_t len, bool create_new)
|
||||
IniGroup *IniLoadFile::GetGroup(const std::string &name, bool create_new)
|
||||
{
|
||||
if (len == 0) len = strlen(name);
|
||||
|
||||
/* does it exist already? */
|
||||
for (IniGroup *group = this->group; group != nullptr; group = group->next) {
|
||||
if (!strncmp(group->name, name, len) && group->name[len] == 0) {
|
||||
return group;
|
||||
}
|
||||
if (group->name == name) return group;
|
||||
}
|
||||
|
||||
if (!create_new) return nullptr;
|
||||
|
||||
/* otherwise make a new one */
|
||||
IniGroup *group = new IniGroup(this, name, name + len - 1);
|
||||
group->comment = stredup("\n");
|
||||
IniGroup *group = new IniGroup(this, name);
|
||||
group->comment = "\n";
|
||||
return group;
|
||||
}
|
||||
|
||||
@@ -182,7 +167,7 @@ void IniLoadFile::RemoveGroup(const char *name)
|
||||
|
||||
/* does it exist already? */
|
||||
for (group = this->group; group != nullptr; prev = group, group = group->next) {
|
||||
if (strncmp(group->name, name, len) == 0) {
|
||||
if (group->name.compare(0, len, name) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -260,17 +245,17 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
|
||||
e--;
|
||||
}
|
||||
s++; // skip [
|
||||
group = new IniGroup(this, s, e - 1);
|
||||
group = new IniGroup(this, std::string(s, e - s));
|
||||
if (comment_size != 0) {
|
||||
group->comment = stredup(comment, comment + comment_size - 1);
|
||||
group->comment.assign(comment, comment_size);
|
||||
comment_size = 0;
|
||||
}
|
||||
} else if (group != nullptr) {
|
||||
if (group->type == IGT_SEQUENCE) {
|
||||
/* A sequence group, use the line as item name without further interpretation. */
|
||||
IniItem *item = new IniItem(group, buffer, e - 1);
|
||||
IniItem *item = new IniItem(group, std::string(buffer, e - buffer));
|
||||
if (comment_size) {
|
||||
item->comment = stredup(comment, comment + comment_size - 1);
|
||||
item->comment.assign(comment, comment_size);
|
||||
comment_size = 0;
|
||||
}
|
||||
continue;
|
||||
@@ -286,9 +271,9 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
|
||||
}
|
||||
|
||||
/* it's an item in an existing group */
|
||||
IniItem *item = new IniItem(group, s, t - 1);
|
||||
IniItem *item = new IniItem(group, std::string(s, t - s));
|
||||
if (comment_size != 0) {
|
||||
item->comment = stredup(comment, comment + comment_size - 1);
|
||||
item->comment.assign(comment, comment_size);
|
||||
comment_size = 0;
|
||||
}
|
||||
|
||||
@@ -304,8 +289,11 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
|
||||
*e = '\0';
|
||||
|
||||
/* If the value was not quoted and empty, it must be nullptr */
|
||||
item->value = (!quoted && e == t) ? nullptr : stredup(t);
|
||||
if (item->value != nullptr) str_validate(item->value, item->value + strlen(item->value));
|
||||
if (!quoted && e == t) {
|
||||
item->value.reset();
|
||||
} else {
|
||||
item->value = str_validate(std::string(t));
|
||||
}
|
||||
} else {
|
||||
/* it's an orphan item */
|
||||
this->ReportFileError("ini: '", buffer, "' outside of group");
|
||||
@@ -313,7 +301,7 @@ void IniLoadFile::LoadFromDisk(const char *filename, Subdirectory subdir)
|
||||
}
|
||||
|
||||
if (comment_size > 0) {
|
||||
this->comment = stredup(comment, comment + comment_size - 1);
|
||||
this->comment.assign(comment, comment_size);
|
||||
comment_size = 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user