Scb is a header only library for a custom text save file format based on .ini format.
Front matter:
Data is saved in basic ascii text.
Spaces and newlines between tokens are ignored.
String token is declared like this:
<$string with any --- value$>
Data consists of blocks with string as a name.
Blocks can be empty or have variables.
To change current block you write : followed by a string token with block name.
Block name can not be and empty string.
: <$block name$>
Blocks consist of variables with string as a name and string as a value.
To set a variable you write % followed by a string token for variable name and a string token for a variable value.
Variable name can not be an empty string. Variable value can be an empty string.
% <$variable name$> <$variable value$>
Copy the insides of "include" directory into your include directory, depending on how you structure your project.
Include the library #include <scb_lib/scb.hpp>.
The scb data is saved as std::unordered_map<std::string, std::unordered_map<std::string, std::string>>. Use scb::ScbData as a short cut.
Use scb::ScbStruct class as a tool for keeping the data. This class has functions for reading and writing scb data from/to any stream or string. Class also contains a few operator overloads.
Every class and function provided is located in scb namespace.
Defined as:
using ScbData =
std::unordered_map<std::string, std::unordered_map<std::string, std::string>>;Default constructor and destructor.
class ScbStruct {
private:
ScbData data;
public:
// Returns reference to `ScbData` from this object.
ScbData& getData() noexcept;
// Writes the `ScbData` to the output stream with readable formating.
void write(std::ostream& stream) const;
// Uses `write` on a `std::stringstream` and returns a string.
std::string getString() const;
// Reads a string into `ScbData`, returns false if failed. No discard.
[[nodiscard]] bool read(const std::string& source);
// If stream is `std::cin`, reads a string and calls read(). Otherwise reads
// stream into a `std::stringstream` and calls read(). Returns false if read
// failed. No discard.
[[nodiscard]] bool read(std::istream& stream);
// Clears `ScbData`.
void clear() noexcept;
// Returns `ScbData`.
ScbData& operator()() noexcept;
// Friend operator. Made to be used like a string for output
// streams. Calls write().
friend std::ostream& operator<<(std::ostream& stream, const ScbStruct& v);
// Friend operator. Made to be used like a string for inputing from a stream.
// Throws `std::runtime_error` if failed because can't return a boolean from
// here. If you want to make sure reading did not fail without throws, use
// read() instead.
friend std::istream& operator>>(std::istream& stream, ScbStruct& v);
// Friend operator. Short for read() when using a string. Returns false if
// read() failed. No discard.
[[nodiscard]] friend bool operator>>(const std::string& source, ScbStruct& v)
};Tip: if you want to ignore nodiscard values, set them into std::discard to show your intention.
You can define special names to change how library works:
Library will not include <iostream> and bool ScbStruct::read(std::istream&) will be modified to not check for std::cin and treat it like a normal stream.
The license for this project is in LICENSE file.
Creator:
GitHub: UAPROGRAMER
Email: stasyatskiu2008@gmail.com