Skip to content

Commit

Permalink
Inireader: support obraining section values
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkvdb committed Nov 30, 2018
1 parent 390445c commit 06d168d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
5 changes: 4 additions & 1 deletion include/infra/inireader.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "infra/cast.h"
#include "infra/filesystem.h"
#include "infra/typetraits.h"

#include <optional>
Expand All @@ -15,14 +16,16 @@ namespace inf {
class IniReader
{
public:
IniReader(const std::string& filename);
IniReader(const fs::path& filename);
IniReader(const IniReader&) = default;
IniReader(IniReader&&) = default;
IniReader& operator=(const IniReader&) = default;
IniReader& operator=(IniReader&&) = default;

// Return the list of sections found in ini file
std::vector<std::string> sections() const;
// Return all the key, values for the given section name
std::unordered_map<std::string, std::string> section(std::string_view name) const;

template <typename T>
std::optional<T> get(std::string_view section, std::string_view name) const noexcept
Expand Down
19 changes: 15 additions & 4 deletions inireader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace inf {

/* Typedef for prototype of handler function. */
typedef int (*ini_handler)(void* user, const char* section,
const char* name, const char* value);
const char* name, const char* value);

/* Typedef for prototype of fgets-style reader function. */
typedef char* (*ini_reader)(char* str, int num, void* stream);
Expand Down Expand Up @@ -105,7 +105,7 @@ inline static char* strncpy0(char* dest, const char* src, size_t size)
/* Same as ini_parse(), but takes an ini_reader function pointer instead of
filename. Used for implementing custom or string-based I/O. */
inline int ini_parse_stream(ini_reader reader, void* stream, ini_handler handler,
void* user)
void* user)
{
/* Uses a fair bit of stack (use heap instead if you need to) */
#if INI_USE_STACK
Expand Down Expand Up @@ -228,9 +228,9 @@ static int ini_parse(const char* filename, ini_handler handler, void* user)
return error;
}

IniReader::IniReader(const std::string& filename)
IniReader::IniReader(const fs::path& filename)
{
auto error = ini_parse(filename.c_str(), valueHandler, this);
auto error = ini_parse(filename.u8string().c_str(), valueHandler, this);
if (error != 0) {
throw RuntimeError("Failed to parse ini file, error on line {} ({})", error, filename);
}
Expand All @@ -246,6 +246,17 @@ std::vector<std::string> IniReader::sections() const
return result;
}

std::unordered_map<std::string, std::string> IniReader::section(std::string_view name) const
{
std::unordered_map<std::string, std::string> result;

if (auto iter = _values.find(str::lowercase(name)); iter != _values.end()) {
result = iter->second;
}

return result;
}

std::optional<std::string_view> IniReader::get_string(std::string_view section, std::string_view name) const noexcept
{
std::optional<std::string_view> result;
Expand Down

0 comments on commit 06d168d

Please sign in to comment.