Brett 2023-07-29 13:38:19 -04:00
parent 21425cff55
commit 90350e8584
4 changed files with 153 additions and 26 deletions

View File

@ -45,6 +45,10 @@ else()
set(PARSE_FILES "") set(PARSE_FILES "")
endif() endif()
if(EXISTS libraries/parallel-hashmap)
include_directories(libraries/parallel-hashmap)
endif()
#include zlib if the user has it. #include zlib if the user has it.
find_package(ZLIB QUIET) find_package(ZLIB QUIET)
@ -69,6 +73,10 @@ target_include_directories(BLT PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/config/)
if(${ZLIB_FOUND}) if(${ZLIB_FOUND})
target_link_libraries(BLT PUBLIC ZLIB::ZLIB) target_link_libraries(BLT PUBLIC ZLIB::ZLIB)
endif() endif()
if(EXISTS ${CMAKE_SOURCE_DIR}/libraries/parallel-hashmap)
message("Including phmap")
target_include_directories(BLT PUBLIC libraries/parallel-hashmap)
endif()
if(MSVC) if(MSVC)
#target_compile_options(BLT PRIVATE /W4) #target_compile_options(BLT PRIVATE /W4)

View File

@ -17,12 +17,29 @@
#include "blt/std/filesystem.h" #include "blt/std/filesystem.h"
#include "blt/std/logging.h" #include "blt/std/logging.h"
namespace blt::nbt {
#ifndef HASHMAP #ifndef HASHMAP
#if defined __has_include && __has_include(<parallel_hashmap/phmap.h>)
#define HASHMAP HASHMAP #define HASHMAP HASHMAP
#include <parallel_hashmap/phmap.h>
#include <parallel_hashmap/phmap_fwd_decl.h>
template<typename K, typename V> template<typename K, typename V>
using HASHMAP = std::unordered_map<K, V>; using HASHMAP = phmap::flat_hash_map<K, V>();
template<typename K>
using HASHSET = phmap::flat_hash_set<K>();
#else
#define HASHMAP HASHMAP
#include <unordered_map>
#include <set>
template<typename K, typename V>
using HASHMAP = std::unordered_map<K, V>();
template<typename K>
using HASHSET = std::unordered_set<K>();
#endif #endif
#endif
namespace blt::nbt {
void writeUTF8String(blt::fs::block_writer& stream, const std::string& str); void writeUTF8String(blt::fs::block_writer& stream, const std::string& str);

View File

@ -11,15 +11,32 @@
#include <string> #include <string>
#include <initializer_list> #include <initializer_list>
#include <optional> #include <optional>
#include <unordered_map>
namespace blt::parser {
#ifndef HASHMAP #ifndef HASHMAP
#if defined __has_include && __has_include(<parallel_hashmap/phmap.h>)
#define HASHMAP HASHMAP #define HASHMAP HASHMAP
#include <parallel_hashmap/phmap.h>
#include <parallel_hashmap/phmap_fwd_decl.h>
template<typename K, typename V>
using HASHMAP = phmap::flat_hash_map<K, V>();
template<typename K>
using HASHSET = phmap::flat_hash_set<K>();
#else
#define HASHMAP HASHMAP
#include <unordered_map>
#include <set>
template<typename K, typename V> template<typename K, typename V>
using HASHMAP = std::unordered_map<K, V>(); using HASHMAP = std::unordered_map<K, V>();
template<typename K>
using HASHSET = std::unordered_set<K>();
#endif #endif
#endif
namespace blt::parser {
enum class arg_action { enum class arg_action {
STORE, STORE,
@ -47,21 +64,30 @@ namespace blt::parser {
std::vector<std::string> flags; std::vector<std::string> flags;
void insertAndSort(const std::string& str); void insertAndSort(const std::string& str);
public: public:
arg_vector() = default; arg_vector() = default;
arg_vector(std::vector<std::string> args);
arg_vector(const std::vector<std::string>& args);
arg_vector(std::initializer_list<std::string> args); arg_vector(std::initializer_list<std::string> args);
arg_vector(const std::string& arg); arg_vector(const std::string& arg);
arg_vector(const char* arg); arg_vector(const char* arg);
arg_vector& operator=(const std::string& arg); arg_vector& operator=(const std::string& arg);
arg_vector& operator=(const char* arg); arg_vector& operator=(const char* arg);
arg_vector& operator=(std::initializer_list<std::string>& args); arg_vector& operator=(std::initializer_list<std::string>& args);
arg_vector& operator=(std::vector<std::string>& args); arg_vector& operator=(std::vector<std::string>& args);
[[nodiscard]] inline std::vector<std::string>& getNames() { [[nodiscard]] inline std::vector<std::string>& getNames() {
return names; return names;
} }
[[nodiscard]] inline std::vector<std::string>& getFlags() { [[nodiscard]] inline std::vector<std::string>& getFlags() {
return flags; return flags;
} }
@ -74,20 +100,30 @@ namespace blt::parser {
static constexpr int ALL_REQUIRED = 0x4; static constexpr int ALL_REQUIRED = 0x4;
int args = 0; int args = 0;
int flags = 0; int flags = 0;
void decode(char c); void decode(char c);
public: public:
arg_nargs() = default; arg_nargs() = default;
arg_nargs(int args): args(args) {} arg_nargs(int args): args(args) {}
arg_nargs(char c); arg_nargs(char c);
arg_nargs(std::string s); arg_nargs(std::string s);
arg_nargs(const char* s); arg_nargs(const char* s);
arg_nargs& operator=(const std::string& s); arg_nargs& operator=(const std::string& s);
arg_nargs& operator=(const char* s); arg_nargs& operator=(const char* s);
arg_nargs& operator=(char c); arg_nargs& operator=(char c);
arg_nargs& operator=(int args); arg_nargs& operator=(int args);
}; };
struct args_properties { struct arg_properties {
private: private:
public: public:
arg_vector a_flags; arg_vector a_flags;
@ -101,12 +137,69 @@ namespace blt::parser {
bool a_required = false; bool a_required = false;
}; };
class argparse { class arg_tokenizer {
private:
static constexpr char FLAG = '-';
std::vector<std::string> args;
size_t nextIndex = 0;
inline const std::string& get(size_t i){
return args[i];
}
inline const std::string& next(size_t& i){
return args[i++];
}
inline bool hasNext(size_t i){
return (size_t)i < args.size();
}
inline bool isFlag(size_t i){
return get(i).starts_with(FLAG);
}
public:
arg_tokenizer() = default;
arg_tokenizer(const char** argv, size_t argc);
inline void forward() {
nextIndex++;
}
inline const std::string& get() {
return get(nextIndex);
}
inline const std::string& next() {
return next(nextIndex);
}
inline bool hasNext() {
return hasNext(nextIndex);
}
inline bool isFlag() {
return isFlag(nextIndex);
}
inline bool isValue() {
return isValue(nextIndex);
}
};
struct arg_results {
private: private:
public: public:
};
class argparse {
private:
arg_tokenizer tokenizer;
public:
argparse() = default; argparse() = default;
void addArgument(const arg_properties& args);
}; };
} }

View File

@ -7,7 +7,7 @@
namespace blt::parser { namespace blt::parser {
arg_vector::arg_vector(std::vector<std::string> args) { arg_vector::arg_vector(const std::vector<std::string>& args) {
for (auto& arg : args) for (auto& arg : args)
insertAndSort(arg); insertAndSort(arg);
} }
@ -94,4 +94,13 @@ namespace blt::parser {
if (c == '*') if (c == '*')
flags |= ALL; flags |= ALL;
} }
arg_tokenizer::arg_tokenizer(const char** argv, size_t argc) {
for (size_t i = 0; i < argc; i++)
args.emplace_back(argv[i]);
}
void argparse::addArgument(const arg_properties& args) {
}
} }