diff options
author | dec05eba <dec05eba@protonmail.com> | 2017-12-09 01:14:41 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2017-12-09 01:14:47 +0100 |
commit | fb2072deb3e50afdb062570a3a80ec1afb5bfb56 (patch) | |
tree | 44baddd61a9aac3ead96ac8a6fab95d087d0bdd7 /include | |
parent | bf24f6fc48b4eebb06cdcd7029d1d31d4c6028dd (diff) |
Finished project config file parsing
Diffstat (limited to 'include')
-rw-r--r-- | include/Conf.hpp | 66 | ||||
-rw-r--r-- | include/FileUtil.hpp | 5 | ||||
-rw-r--r-- | include/Result.hpp | 26 | ||||
-rw-r--r-- | include/StringView.hpp | 76 | ||||
-rw-r--r-- | include/env.hpp | 24 | ||||
-rw-r--r-- | include/types.hpp | 25 | ||||
-rwxr-xr-x | include/utils.hpp | 10 |
7 files changed, 218 insertions, 14 deletions
diff --git a/include/Conf.hpp b/include/Conf.hpp index 2b38e95..c27046b 100644 --- a/include/Conf.hpp +++ b/include/Conf.hpp @@ -2,11 +2,73 @@ #define SIBS_CONF_HPP #include "Result.hpp" -#include <string> +#include "StringView.hpp" +#include "utils.hpp" +#include <vector> +#include <cassert> namespace sibs { - Result<std::string> readConf(const char *filepath); + class ConfigValue + { + public: + enum class Type + { + NONE, + SINGLE, + LIST + }; + + ConfigValue() : type(Type::NONE) {} + + ConfigValue(StringView value) : + type(Type::SINGLE) + { + values.push_back(value); + } + + ConfigValue(const std::vector<StringView> &_values) : + type(Type::LIST), + values(_values) + { + + } + + bool isSingle() const { return type == Type::SINGLE; } + bool isList() const { return type == Type::LIST; } + + StringView asSingle() const + { + assert(isSingle()); + return values[0]; + } + + const std::vector<StringView> asList() const + { + assert(isList()); + return values; + } + private: + Type type; + std::vector<StringView> values; + }; + + class Parser; + + class ConfigCallback + { + friend class Parser; + protected: + virtual void processObject(StringView name) = 0; + virtual void processField(StringView name, const ConfigValue &value) = 0; + virtual void finished() = 0; + }; + + class Config + { + public: + static Result<bool> readFromFile(const char *filepath, const ConfigCallback &callback); + }; } #endif //SIBS_CONF_HPP diff --git a/include/FileUtil.hpp b/include/FileUtil.hpp index 402f5f1..345c72a 100644 --- a/include/FileUtil.hpp +++ b/include/FileUtil.hpp @@ -3,6 +3,7 @@ #include "../external/tinydir.h" #include "Result.hpp" +#include "StringView.hpp" #include <functional> namespace sibs @@ -17,8 +18,8 @@ namespace sibs }; FileType getFileType(const char *path); - void walkDirectory(const char *directory, FileWalkCallbackFunc callbackFunc); - Result<std::string> getFileContent(const char *filepath); + void walkDirFiles(const char *directory, FileWalkCallbackFunc callbackFunc); + Result<StringView> getFileContent(const char *filepath); } #endif //SIBS_FILEUTIL_HPP diff --git a/include/Result.hpp b/include/Result.hpp index 31f9e4d..3ee60e5 100644 --- a/include/Result.hpp +++ b/include/Result.hpp @@ -2,6 +2,7 @@ #define SIBS_RESULT_HPP #include <cassert> +#include <string> namespace sibs { @@ -11,34 +12,39 @@ namespace sibs public: static Result Ok(const T &value) { - return Result(value); + Result result(value); + result.error = false; + return result; } - static Result Err(const char *errMsg) + static Result Err(const std::string &errMsg) { - return Result(errMsg); + Result result; + result.errMsg = errMsg; + result.error = true; + return result; } - bool isOk() const { return !errMsg; } - bool isErr() const { return errMsg; } + bool isOk() const { return !error; } + bool isErr() const { return error; } - T& unwrap() + const T& unwrap() const { assert(isOk()); return value; } - const char *getErrMsg() const + const std::string &getErrMsg() const { assert(isErr()); return errMsg; } private: - Result(const T &_value) : value(_value), errMsg(nullptr){} - Result(const char *_errMsg) : errMsg(_errMsg){} + Result(const T &_value = T()) : value(_value) {} private: T value; - const char *errMsg; + std::string errMsg; + bool error; }; } diff --git a/include/StringView.hpp b/include/StringView.hpp new file mode 100644 index 0000000..adfa237 --- /dev/null +++ b/include/StringView.hpp @@ -0,0 +1,76 @@ +#ifndef SIBS_STRING_VIEW_HPP +#define SIBS_STRING_VIEW_HPP + +#define XXH_FORCE_NATIVE_FORMAT +#include "../external/xxhash.h" +#include "types.hpp" +#include "env.hpp" +#include <cstring> +#include <unordered_map> + +namespace sibs +{ + class StringView + { + public: + StringView() : data(nullptr), size(0) + { + + } + + StringView(const StringView &other) : data(other.data), size(other.size) + { + + } + + StringView(const char *_data) : data(_data), size(strlen(_data)) + { + + } + + StringView(const char *_data, usize _size) : data(_data), size(_size) + { + + } + + bool equals(const StringView &other) const + { + if(size != other.size) return false; + return memcmp(data, other.data, size) == 0; + } + + size_t hash() const + { + #if defined(CISB_ENV_64BIT) + return XXH64(data, size, 0xdec05eba); + #else + return XXH32(data, size, 0xdec05eba); + #endif + } + + const char *data; + usize size; + }; + + struct StringViewHash + { + size_t operator()(const StringView &stringView) const + { + return stringView.hash(); + } + }; + + struct StringViewCompare + { + public: + bool operator()(const StringView &lhs, const StringView &rhs) const + { + return lhs.equals(rhs); + } + }; + + template <typename ValueType> + using StringViewMap = std::unordered_map<const StringView, ValueType, StringViewHash, StringViewCompare>; +} + +#endif // SIBS_STRING_VIEW_HPP diff --git a/include/env.hpp b/include/env.hpp new file mode 100644 index 0000000..5d0a163 --- /dev/null +++ b/include/env.hpp @@ -0,0 +1,24 @@ +#ifndef SIBS_ENV_HPP +#define SIBS_ENV_HPP + +#if defined(_WIN32) || defined(_WIN64) + #if defined(_WIN64) + #define CISB_ENV_64BIT + #else + #define CISB_ENV_32BIT + #endif +#endif + +#if defined(__GNUC__) + #if defined(__x86_64__) || defined(__pc64__) + #define CISB_ENV_64BIT + #else + #define CISB_ENV_32BIT + #endif +#endif + +#if !defined(CISB_ENV_32BIT) && !defined(CISB_ENV_64BIT) + #error "System is not detected as either 32-bit or 64-bit" +#endif + +#endif // SIBS_ENV_HPP diff --git a/include/types.hpp b/include/types.hpp new file mode 100644 index 0000000..081c13b --- /dev/null +++ b/include/types.hpp @@ -0,0 +1,25 @@ +#ifndef SIBS_TYPES_HPP +#define SIBS_TYPES_HPP + +#include <cstdint> + +namespace sibs +{ + typedef int8_t i8; + typedef int16_t i16; + typedef int32_t i32; + typedef int64_t i64; + + typedef uint8_t u8; + typedef uint16_t u16; + typedef uint32_t u32; + typedef uint64_t u64; + + typedef float f32; + typedef double f64; + + typedef intptr_t ssize; + typedef uintptr_t usize; +} + +#endif //SIBS_TYPES_HPP diff --git a/include/utils.hpp b/include/utils.hpp new file mode 100755 index 0000000..9ad9d97 --- /dev/null +++ b/include/utils.hpp @@ -0,0 +1,10 @@ +#ifndef SIBS_UTILS_HPP +#define SIBS_UTILS_HPP + +// Disable copying for a class or struct +#define DISABLE_COPY(ClassName) \ + ClassName(ClassName&) = delete; \ + ClassName(ClassName&&) = delete; \ + ClassName& operator = (ClassName&) = delete; + +#endif // SIBS_UTILS_HPP |