mirror of
https://github.com/Team-Neptune/SimpleIniParser.git
synced 2024-11-26 22:10:24 +00:00
✨ Added ability to find all options and sections.
This commit is contained in:
parent
b708c8f1ab
commit
96f8dd3814
@ -16,6 +16,7 @@
|
||||
*/
|
||||
|
||||
#include "SimpleIniParser/Ini.hpp"
|
||||
#include "SimpleIniParser/IniHelper.hpp"
|
||||
#include "SimpleIniParser/IniSection.hpp"
|
||||
#include "SimpleIniParser/IniOption.hpp"
|
||||
#include "SimpleIniParser/IniStringHelper.hpp"
|
||||
|
@ -17,9 +17,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "IniSection.hpp"
|
||||
#include "IniOption.hpp"
|
||||
@ -35,8 +35,10 @@ namespace simpleIniParser {
|
||||
std::string build();
|
||||
IniOption * findFirstOption(std::string term, bool caseSensitive = true, IniOptionType type = IniOptionType::Any, IniOptionSearchField field = IniOptionSearchField::Key);
|
||||
IniOption * findOrCreateFirstOption(std::string key, std::string val, bool caseSensitive = true, IniOptionType type = IniOptionType::Any, IniOptionSearchField field = IniOptionSearchField::Key);
|
||||
std::vector<IniOption *> findAllOptions(std::string term, bool caseSensitive = true, IniOptionType type = IniOptionType::Any, IniOptionSearchField field = IniOptionSearchField::Key);
|
||||
IniSection * findSection(std::string term, bool caseSensitive = true, IniSectionType type = IniSectionType::Any);
|
||||
IniSection * findOrCreateSection(std::string term, bool caseSensitive = true, IniSectionType type = IniSectionType::Any);
|
||||
std::vector<IniSection *> findAllSections(std::string term, bool caseSensitive = true, IniSectionType type = IniSectionType::Any);
|
||||
bool writeToFile(std::string path);
|
||||
static Ini * parseFile(std::string path);
|
||||
static Ini * parseFileWithMagic(std::string path, std::string magic);
|
||||
@ -44,5 +46,6 @@ namespace simpleIniParser {
|
||||
|
||||
private:
|
||||
static Ini * _parseContent(std::stringstream * content, std::string magic);
|
||||
|
||||
};
|
||||
}
|
||||
|
32
include/SimpleIniParser/IniHelper.hpp
Normal file
32
include/SimpleIniParser/IniHelper.hpp
Normal file
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* SimpleIniParser
|
||||
* Copyright (c) 2020 Nichole Mattera
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "IniOption.hpp"
|
||||
#include "IniSection.hpp"
|
||||
|
||||
namespace simpleIniParser {
|
||||
class IniHelper {
|
||||
public:
|
||||
static bool findOption(const IniOption * obj, std::string term, bool caseSensitive, IniOptionType type, IniOptionSearchField field);
|
||||
static bool findSection(const IniSection * obj, std::string term, bool caseSensitive, IniSectionType type);
|
||||
|
||||
};
|
||||
}
|
@ -44,8 +44,6 @@ namespace simpleIniParser {
|
||||
std::vector<IniOption *> findAllOptions(std::string term, bool caseSensitive = true, IniOptionType type = IniOptionType::Any, IniOptionSearchField field = IniOptionSearchField::Key);
|
||||
std::string build();
|
||||
static IniSection * parse(std::string line, bool parseComments);
|
||||
|
||||
private:
|
||||
static bool _findElements(const IniOption * obj, std::string term, bool caseSensitive, IniOptionType type, IniOptionSearchField field);
|
||||
|
||||
};
|
||||
}
|
||||
|
@ -17,11 +17,13 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <switch.h>
|
||||
|
||||
#include "Ini.hpp"
|
||||
#include "IniHelper.hpp"
|
||||
#include "IniOption.hpp"
|
||||
#include "IniStringHelper.hpp"
|
||||
|
||||
@ -61,20 +63,18 @@ namespace simpleIniParser {
|
||||
IniStringHelper::toupper(term);
|
||||
}
|
||||
|
||||
auto it = find_if(options.begin(), options.end(), [&term, &caseSensitive, &type, &field](const IniOption * obj) {
|
||||
if (type != IniOptionType::Any && type != obj->type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string fieldValue = "";
|
||||
if (field == IniOptionSearchField::Key) {
|
||||
fieldValue = (!caseSensitive) ? IniStringHelper::toupper_copy(obj->key) : obj->key;
|
||||
} else {
|
||||
fieldValue = (!caseSensitive) ? IniStringHelper::toupper_copy(obj->value) : obj->value;
|
||||
}
|
||||
|
||||
return fieldValue == term;
|
||||
});
|
||||
auto it = std::find_if(
|
||||
options.begin(),
|
||||
options.end(),
|
||||
std::bind(
|
||||
IniHelper::findOption,
|
||||
std::placeholders::_1,
|
||||
term,
|
||||
caseSensitive,
|
||||
type,
|
||||
field
|
||||
)
|
||||
);
|
||||
|
||||
if (it == options.end())
|
||||
return nullptr;
|
||||
@ -94,19 +94,46 @@ namespace simpleIniParser {
|
||||
return it;
|
||||
}
|
||||
|
||||
std::vector<IniOption *> Ini::findAllOptions(std::string term, bool caseSensitive, IniOptionType type, IniOptionSearchField field) {
|
||||
std::vector<IniOption *> results;
|
||||
|
||||
if (!caseSensitive) {
|
||||
IniStringHelper::toupper(term);
|
||||
}
|
||||
|
||||
std::copy_if(
|
||||
options.begin(),
|
||||
options.end(),
|
||||
std::back_inserter(results),
|
||||
std::bind(
|
||||
IniHelper::findOption,
|
||||
std::placeholders::_1,
|
||||
term,
|
||||
caseSensitive,
|
||||
type,
|
||||
field
|
||||
)
|
||||
);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
IniSection * Ini::findSection(std::string term, bool caseSensitive, IniSectionType type) {
|
||||
if (!caseSensitive) {
|
||||
IniStringHelper::toupper(term);
|
||||
}
|
||||
|
||||
auto it = find_if(sections.begin(), sections.end(), [&term, &caseSensitive, &type](const IniSection * obj) {
|
||||
if (type != IniSectionType::Any && type != obj->type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string fieldValue = (!caseSensitive) ? IniStringHelper::toupper_copy(obj->value) : obj->value;
|
||||
return fieldValue == term;
|
||||
});
|
||||
auto it = std::find_if(
|
||||
sections.begin(),
|
||||
sections.end(),
|
||||
std::bind(
|
||||
IniHelper::findSection,
|
||||
std::placeholders::_1,
|
||||
term,
|
||||
caseSensitive,
|
||||
type
|
||||
)
|
||||
);
|
||||
|
||||
if (it == sections.end())
|
||||
return nullptr;
|
||||
@ -125,6 +152,29 @@ namespace simpleIniParser {
|
||||
return it;
|
||||
}
|
||||
|
||||
std::vector<IniSection *> Ini::findAllSections(std::string term, bool caseSensitive, IniSectionType type) {
|
||||
std::vector<IniSection *> results;
|
||||
|
||||
if (!caseSensitive) {
|
||||
IniStringHelper::toupper(term);
|
||||
}
|
||||
|
||||
std::copy_if(
|
||||
sections.begin(),
|
||||
sections.end(),
|
||||
std::back_inserter(results),
|
||||
std::bind(
|
||||
IniHelper::findSection,
|
||||
std::placeholders::_1,
|
||||
term,
|
||||
caseSensitive,
|
||||
type
|
||||
)
|
||||
);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
bool Ini::writeToFile(std::string path) {
|
||||
std::ofstream file(path);
|
||||
if (!file.is_open())
|
||||
|
47
source/SimpleIniParser/IniHelper.cpp
Normal file
47
source/SimpleIniParser/IniHelper.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* SimpleIniParser
|
||||
* Copyright (c) 2020 Nichole Mattera
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "IniHelper.hpp"
|
||||
#include "IniStringHelper.hpp"
|
||||
|
||||
namespace simpleIniParser {
|
||||
bool IniHelper::findOption(const IniOption * obj, std::string term, bool caseSensitive, IniOptionType type, IniOptionSearchField field) {
|
||||
if (type != IniOptionType::Any && type != obj->type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string fieldValue = "";
|
||||
if (field == IniOptionSearchField::Key) {
|
||||
fieldValue = (!caseSensitive) ? IniStringHelper::toupper_copy(obj->key) : obj->key;
|
||||
} else {
|
||||
fieldValue = (!caseSensitive) ? IniStringHelper::toupper_copy(obj->value) : obj->value;
|
||||
}
|
||||
|
||||
return fieldValue == term;
|
||||
}
|
||||
|
||||
bool findSection(const IniSection * obj, std::string term, bool caseSensitive, IniSectionType type) {
|
||||
if (type != IniSectionType::Any && type != obj->type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string fieldValue = (!caseSensitive) ? IniStringHelper::toupper_copy(obj->value) : obj->value;
|
||||
return fieldValue == term;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
|
||||
#include "IniHelper.hpp"
|
||||
#include "IniSection.hpp"
|
||||
#include "IniStringHelper.hpp"
|
||||
|
||||
@ -47,7 +48,7 @@ namespace simpleIniParser {
|
||||
options.begin(),
|
||||
options.end(),
|
||||
std::bind(
|
||||
_findElements,
|
||||
IniHelper::findOption,
|
||||
std::placeholders::_1,
|
||||
term,
|
||||
caseSensitive,
|
||||
@ -85,7 +86,7 @@ namespace simpleIniParser {
|
||||
options.end(),
|
||||
std::back_inserter(results),
|
||||
std::bind(
|
||||
_findElements,
|
||||
IniHelper::findOption,
|
||||
std::placeholders::_1,
|
||||
term,
|
||||
caseSensitive,
|
||||
@ -132,19 +133,4 @@ namespace simpleIniParser {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool IniSection::_findElements(const IniOption * obj, std::string term, bool caseSensitive, IniOptionType type, IniOptionSearchField field) {
|
||||
if (type != IniOptionType::Any && type != obj->type) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string fieldValue = "";
|
||||
if (field == IniOptionSearchField::Key) {
|
||||
fieldValue = (!caseSensitive) ? IniStringHelper::toupper_copy(obj->key) : obj->key;
|
||||
} else {
|
||||
fieldValue = (!caseSensitive) ? IniStringHelper::toupper_copy(obj->value) : obj->value;
|
||||
}
|
||||
|
||||
return fieldValue == term;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user