-
Notifications
You must be signed in to change notification settings - Fork 1
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
960cf31
add reader class
NikolaJelic 7fe3852
improved reader
NikolaJelic f54cf35
reader able to take strings
NikolaJelic 0a0d6a4
remove excess variables
NikolaJelic 90263bb
change raw string delimiter
NikolaJelic 704a043
extract integers, strings and doubles
NikolaJelic 9670b76
fix get_pairs()
NikolaJelic e3e376a
edit const placement
NikolaJelic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weird lines breaks, I suggest you add a |
||
insert_pairs(std::vector<std::string> file_lines); | ||
} // namespace util |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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