Skip to content

Add reader class #1

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

Merged
merged 8 commits into from
Jun 27, 2021
Merged
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
28 changes: 28 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Language: Cpp
BasedOnStyle: LLVM
AlwaysBreakTemplateDeclarations: Yes
BreakBeforeBraces: Attach
ColumnLimit: 160
SpaceAfterTemplateKeyword: true
Standard: c++17
TabWidth: 4
IndentWidth: 4
UseTab: Always
AllowShortEnumsOnASingleLine: true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortBlocksOnASingleLine: Always
AllowShortIfStatementsOnASingleLine: Always
AllowShortLoopsOnASingleLine: true
IncludeCategories:
# Headers in <> without extension.
- Regex: '<([A-Za-z0-9\/-_])+>'
Priority: 1
# Headers in <> with .h extension.
- Regex: '<([A-Za-z0-9\/-_])+\.h>'
Priority: 10
# Headers in <> with .hpp extension.
- Regex: '<([A-Za-z0-9\/-_])+\.hpp>'
Priority: 20
PointerAlignment: Left
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ cmake_minimum_required(VERSION 3.19)

project(pini)
add_executable(${PROJECT_NAME} src/main.cpp

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line can be simplified, especially if you plan to add more files in the future it is not feasible to keep this list of files growing

src/util.cpp)
src/util.cpp
src/pini.cpp)
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Werror=return-type)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_17)

Expand Down
22 changes: 15 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
#include "util.hpp"
#include <ios>
#include <iostream>
#include "pini.hpp"
#include "util.hpp"

int main() {
std::vector<std::string> lines = util::get_lines("src/main.cpp");
for (auto str : lines) {
std::cout << str << std::endl;
}
std::cout << "\n";
}
pn::pini pin;

std::string_view raw_input{"a=5\nb=10\n23=c"};
std::filesystem::path filename{"test/test.ini"};

std::cout << std::boolalpha << pin.load_file(filename) << "\n";
std::cout << "attack: " << pin.get_uint32("attack ") << "\n";
std::cout << "def " << pin.get_uint64("def ") << "\n";
std::cout << "health " << pin.get_int64("health ") << "\n";
std::cout << "def " << pin.get_double("def ") << "\n";
std::cout << "sp " << pin.get_int32("skill points ") << "\n";
}
70 changes: 70 additions & 0 deletions src/pini.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "pini.hpp"
#include <cstddef>
#include <cstdint>
#include <sstream>
#include <string_view>
#include <vector>
#include "util.hpp"

namespace pn {
namespace {
pini::map_type read_file(std::filesystem::path const& filename) {
std::vector<std::string> lines = util::get_lines(filename);
return util::insert_pairs(lines);
}

pini::map_type read_text(std::string_view text) {
std::string str;
std::stringstream str_stream;
str_stream << text;
std::vector<std::string> lines;
while (getline(str_stream, str, '\n')) { lines.push_back(str); }
return util::insert_pairs(lines);
}

} // namespace

bool pini::load_file(std::filesystem::path const& filename) {
key_value_pairs = read_file(filename);
return !key_value_pairs.empty();
}

bool pini::load_text(std::string_view text) {
key_value_pairs = read_text(text);
return !key_value_pairs.empty();
}

double pini::get_double(std::string const& key, double def) const {
auto it = key_value_pairs.find(key);
if (it == key_value_pairs.end()) { return def; }
return std::atof(it->second.c_str());
}

std::uint64_t pini::get_uint64(std::string const& key, std::uint64_t def) const {
if (auto it = key_value_pairs.find(key); it != key_value_pairs.end()) {
try {
return stoull(it->second);
} catch (std::exception const& e) { std::cerr << e.what() << '\n'; }
}
return def;
}

std::int64_t pini::get_int64(std::string const& key, std::int64_t def) const {
if (auto it = key_value_pairs.find(key); it != key_value_pairs.end()) {
try {
return stoll(it->second);
} catch (std::exception const& e) { std::cerr << e.what() << '\n'; }
}
return def;
}

std::uint32_t pini::get_uint32(std::string const& key, std::uint32_t def) const { return static_cast<std::uint32_t>(pini::get_uint64(key, def)); }

std::int32_t pini::get_int32(std::string const& key, std::uint32_t def) const { return static_cast<std::int32_t>(pini::get_int64(key, def)); }

std::string_view pini::get_string(std::string const& key) const {
auto it = key_value_pairs.find(key);
if (it == key_value_pairs.end()) { return {}; }
return it->second;
}
} // namespace pn
37 changes: 37 additions & 0 deletions src/pini.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <cstdint>
#include <filesystem>
#include <sstream>
#include <string_view>
#include <unordered_map>
#include <vector>
#include "util.hpp"

namespace pn {
class pini {
public:
using map_type = std::unordered_map<std::string, std::string>;

map_type const& get_pairs() const { return key_value_pairs; }
bool load_file(std::filesystem::path const& filename);
bool load_text(std::string_view text);

// return double
double get_double(std::string const& key, double def = 0) const;
// return unsigned 64 bit integer - <unsigned long long>
std::uint64_t get_uint64(std::string const& key, std::uint64_t def = 0) const;
// return signed 64 bit integer - <long long>
std::int64_t get_int64(std::string const& key, std::int64_t def = 0) const;

// return unsigned 32 bit integer
std::uint32_t get_uint32(std::string const& key, std::uint32_t def = 0) const;
// return signed 32 bit integer
std::int32_t get_int32(std::string const& key, std::uint32_t def = 0) const;
// return string value
std::string_view get_string(std::string const& key) const;

private:
map_type key_value_pairs;
};
} // namespace pn
42 changes: 21 additions & 21 deletions src/util.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
#include "util.hpp"
#include <cassert>

namespace util {

std::vector<std::string> get_lines(std::filesystem::path const &filename) {
std::vector<std::string> ret;
std::string temp_line;
if (auto file = std::ifstream(filename)) {
while (std::getline(file, temp_line)) {
ret.push_back(temp_line);
}
} else {
std::cout << "File failed to open\n";
}
return ret;
std::vector<std::string> get_lines(std::filesystem::path const& filename) {
std::vector<std::string> ret;
std::string temp_line;
if (auto file = std::ifstream(filename)) {
while (std::getline(file, temp_line)) { ret.push_back(temp_line); }
} else {
std::cout << "File failed to open\n";
}
return ret;
}

/* std::vector<std::string> separate_substrings(const std::string &str) {
std::vector<std::string> ret;
std::string word;
std::stringstream s_stream(str);
while (std::getline(s_stream, word, ' ')) {
ret.push_back(word);
}
return ret;
} */
std::unordered_map<std::string, std::string> insert_pairs(std::vector<std::string> file_lines) {
std::unordered_map<std::string, std::string> key_value_pairs;

} // namespace util
for (const auto& str : file_lines) {
if (auto delimeter_pos = str.find('='); delimeter_pos != std::string::npos) {
key_value_pairs.insert({str.substr(0, delimeter_pos), str.substr(delimeter_pos + 1)});
}
}
return key_value_pairs;
}

} // namespace util
8 changes: 5 additions & 3 deletions src/util.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#pragma once

#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>

namespace util {
// return vector of strings containing each line of the given file
std::vector<std::string> get_lines(std::filesystem::path const &filename);

// returns a vector of words contained in a string
// std::vector<std::string> separate_substrings(const std::string &str);
// extract key-value pairs from given vector of strings and insert them in an
// unordered_map
std::unordered_map<std::string, std::string>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird lines breaks, I suggest you add a clang-format file to clean up your formatting everywhere.

insert_pairs(std::vector<std::string> file_lines);
} // namespace util
4 changes: 4 additions & 0 deletions test/test.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
health = 9
attack = 4.5
def = -9.2314g
skill points = 8