Skip to content

Fix parser usage in multiple compilation units #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 28 additions & 29 deletions INI.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <map>
#include <vector>

Expand All @@ -42,43 +43,43 @@
#endif


fini_string_t& l_trim(fini_string_t& str, const fini_string_t trim_chars = "\t\v\f; ") {
static inline fini_string_t& l_trim(fini_string_t& str, const fini_string_t trim_chars = "\t\v\f; ") {
str.erase(0, str.find_first_not_of(trim_chars)); return str;
}

fini_string_t& r_trim(fini_string_t& str, const fini_string_t trim_chars = "\t\v\f; ") {
static inline fini_string_t& r_trim(fini_string_t& str, const fini_string_t trim_chars = "\t\v\f; ") {
str.erase(str.find_last_not_of(trim_chars) + 1); return str;
}

fini_string_t& trim(fini_string_t& str, const fini_string_t trim_chars = "\t\v\f; ") {
static inline fini_string_t& trim(fini_string_t& str, const fini_string_t trim_chars = "\t\v\f; ") {
return l_trim(r_trim(str, trim_chars), trim_chars);
}

template <typename T>
T convert_to (const fini_string_t &str) {
inline T convert_to (const fini_string_t &str) {
fini_sstream_t ss(str);
T num;
ss >> num;
return num;
}

template <>
fini_string_t convert_to<fini_string_t>(const fini_string_t &str) {
inline fini_string_t convert_to<fini_string_t>(const fini_string_t &str) {
return str;
}

template <>
const char* convert_to<const char*>(const fini_string_t &str) {
inline const char* convert_to<const char*>(const fini_string_t &str) {
return str.c_str();
}

static int INI_DEFAULT_PARSE_FLAGS = 0, INI_DEFAULT_SAVE_FLAGS = 0;

///
class INI
{
public:
/// Define
static int PARSE_FLAGS, SAVE_FLAGS;

typedef fini_char_t data_t;

typedef typename std::map<fini_string_t, fini_string_t> keys_t;
Expand Down Expand Up @@ -124,7 +125,7 @@ class INI
};

/// Definition
INI::INI(const INI& from): source(from.source), filename(from.filename) {
inline INI::INI(const INI& from): source(from.source), filename(from.filename) {
// Deep clone INI
for(auto i: from.sections) {
select(i.first);
Expand All @@ -133,23 +134,23 @@ INI::INI(const INI& from): source(from.source), filename(from.filename) {
}
}

INI::INI(fini_string_t filename, bool doParse, int parseFlags): source(SOURCE_FILE), filename(filename) {
inline INI::INI(fini_string_t filename, bool doParse, int parseFlags): source(SOURCE_FILE), filename(filename) {
this->create("");

if (doParse)
parse(parseFlags);
}

INI:: ~INI() {
inline INI:: ~INI() {
clear();
}

void INI::clear() {
inline void INI::clear() {
sections.clear();
}

bool INI::parse(int parseFlags) {
parseFlags = (parseFlags > 0)? parseFlags: PARSE_FLAGS;
inline bool INI::parse(int parseFlags) {
parseFlags = (parseFlags > 0)? parseFlags: INI_DEFAULT_PARSE_FLAGS;

switch(source)
{
Expand All @@ -174,9 +175,7 @@ bool INI::parse(int parseFlags) {
return true;
}

int INI::PARSE_FLAGS = 0, INI::SAVE_FLAGS = 0;

void INI::_parseFile(fini_ifstream_t& file, int parseFlags) {
inline void INI::_parseFile(fini_ifstream_t& file, int parseFlags) {
fini_string_t line;
fini_string_t section; // Set default section (support for sectionless files)
size_t i = 0;
Expand Down Expand Up @@ -224,8 +223,8 @@ void INI::_parseFile(fini_ifstream_t& file, int parseFlags) {
}
}

bool INI::save(fini_string_t filename, int saveFlags) {
saveFlags = (saveFlags > 0)? saveFlags: SAVE_FLAGS;
inline bool INI::save(fini_string_t filename, int saveFlags) {
saveFlags = (saveFlags > 0)? saveFlags: INI_DEFAULT_SAVE_FLAGS;

fini_ofstream_t file((filename == "")? this->filename: filename, std::ios::trunc);
if (!file.is_open())
Expand Down Expand Up @@ -277,13 +276,13 @@ bool INI::save(fini_string_t filename, int saveFlags) {
}

//Provide bracket access to section contents
INI::keys_t& INI::operator[](fini_string_t section) {
inline INI::keys_t& INI::operator[](fini_string_t section) {
select(section);
return *current;
}

//Create a new section and select it
void INI::create(fini_string_t section) {
inline void INI::create(fini_string_t section) {
if (section != "" && sections.find(section) != sections.end()) {
std::cerr << "Error: cpp-feather-ini-parser: Duplicate section '" << section << "'" << std::endl;
throw -1;
Expand All @@ -294,15 +293,15 @@ void INI::create(fini_string_t section) {
}

//Removes a section including all key/value pairs
void INI::remove(fini_string_t section) {
inline void INI::remove(fini_string_t section) {
if (select(section, true))
sections.erase(section);

current = NULL;
}

//Select a section for performing operations
bool INI::select(fini_string_t section, bool noCreate) {
inline bool INI::select(fini_string_t section, bool noCreate) {
sections_t::iterator sectionsit = sections.find(section);
if (sectionsit == sections.end()) {
if (!noCreate)
Expand All @@ -315,11 +314,11 @@ bool INI::select(fini_string_t section, bool noCreate) {
return true;
}

fini_string_t INI::get(fini_string_t section, fini_string_t key, fini_string_t def) {
inline fini_string_t INI::get(fini_string_t section, fini_string_t key, fini_string_t def) {
return get(key, def);
}

fini_string_t INI::get(fini_string_t key, fini_string_t def) {
inline fini_string_t INI::get(fini_string_t key, fini_string_t def) {
auto it = current->find(key);
if (it == current->end())
return def;
Expand All @@ -328,26 +327,26 @@ fini_string_t INI::get(fini_string_t key, fini_string_t def) {
}

template<class T>
T INI::getAs(fini_string_t section, fini_string_t key, T def) {
inline T INI::getAs(fini_string_t section, fini_string_t key, T def) {
return getAs<T>(key, def);
}

template<class T>
T INI::getAs(fini_string_t key, T def) {
inline T INI::getAs(fini_string_t key, T def) {
auto it = current->find(key);
if (it == current->end())
return def;

return convert_to<T>(it->second);
}

void INI::set(fini_string_t section, fini_string_t key, fini_string_t value) {
inline void INI::set(fini_string_t section, fini_string_t key, fini_string_t value) {
if (!select(section))
create(section);

set(key, value);
}

void INI::set(fini_string_t key, fini_string_t value) {
inline void INI::set(fini_string_t key, fini_string_t value) {
(*current)[key] = value;
}
4 changes: 2 additions & 2 deletions example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ std::string getStringFromFile(const std::string& path); //Source for data loadin

int main()
{
INI::PARSE_FLAGS = INI::PARSE_COMMENTS_ALL | INI::PARSE_COMMENTS_SLASH | INI::PARSE_COMMENTS_HASH;
INI::SAVE_FLAGS = INI::SAVE_PRUNE | INI::SAVE_PADDING_SECTIONS | INI::SAVE_SPACE_SECTIONS | INI::SAVE_SPACE_KEYS | INI::SAVE_TAB_KEYS | INI::SAVE_SEMICOLON_KEYS;
INI_DEFAULT_PARSE_FLAGS = INI::PARSE_COMMENTS_ALL | INI::PARSE_COMMENTS_SLASH | INI::PARSE_COMMENTS_HASH;
INI_DEFAULT_SAVE_FLAGS = INI::SAVE_PRUNE | INI::SAVE_PADDING_SECTIONS | INI::SAVE_SPACE_SECTIONS | INI::SAVE_SPACE_KEYS | INI::SAVE_TAB_KEYS | INI::SAVE_SEMICOLON_KEYS;

INI ini2("file.ini", true); // Assign ini file and parse
INI ini(ini2); // Clone
Expand Down