Compare commits
2 Commits
215220f98b
...
e2d932ea78
Author | SHA1 | Date |
---|---|---|
Brett | e2d932ea78 | |
Brett | 9e4bfad087 |
|
@ -88,38 +88,38 @@ endif()
|
|||
|
||||
message("BLT ${CMAKE_PROJECT_VERSION} Successfully included!")
|
||||
|
||||
if(${BUILD_TESTS})
|
||||
project(BLT_TESTS)
|
||||
|
||||
file(GLOB_RECURSE TESTS_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/tests/*.cpp")
|
||||
|
||||
add_executable(BLT_TESTS ${TESTS_FILES})
|
||||
|
||||
target_link_libraries(BLT_TESTS PUBLIC BLT)
|
||||
|
||||
if(MSVC)
|
||||
#target_compile_options(BLT_TESTS PRIVATE /W4)
|
||||
if(${CMAKE_BUILD_TYPE} MATCHES Debug)
|
||||
target_link_options(BLT_TESTS PRIVATE /DEBUG)
|
||||
endif()
|
||||
else()
|
||||
target_compile_options(BLT_TESTS PRIVATE -Wall -Wextra -Wpedantic)
|
||||
endif()
|
||||
|
||||
if (${ENABLE_ADDRSAN} MATCHES ON)
|
||||
target_compile_options(BLT_TESTS PRIVATE -fsanitize=address)
|
||||
target_link_options(BLT_TESTS PRIVATE -fsanitize=address)
|
||||
endif ()
|
||||
|
||||
if (${ENABLE_UBSAN} MATCHES ON)
|
||||
target_compile_options(BLT_TESTS PRIVATE -fsanitize=undefined)
|
||||
target_link_options(BLT_TESTS PRIVATE -fsanitize=undefined)
|
||||
endif ()
|
||||
|
||||
if (${ENABLE_TSAN} MATCHES ON)
|
||||
target_compile_options(BLT_TESTS PRIVATE -fsanitize=thread)
|
||||
target_link_options(BLT_TESTS PRIVATE -fsanitize=thread)
|
||||
endif ()
|
||||
|
||||
message("BLT tests included!")
|
||||
endif()
|
||||
#if(${BUILD_TESTS})
|
||||
# project(BLT_TESTS)
|
||||
#
|
||||
# file(GLOB_RECURSE TESTS_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/tests/*.cpp")
|
||||
#
|
||||
# add_executable(BLT_TESTS ${TESTS_FILES})
|
||||
#
|
||||
# target_link_libraries(BLT_TESTS PUBLIC BLT)
|
||||
#
|
||||
# if(MSVC)
|
||||
# #target_compile_options(BLT_TESTS PRIVATE /W4)
|
||||
# if(${CMAKE_BUILD_TYPE} MATCHES Debug)
|
||||
# target_link_options(BLT_TESTS PRIVATE /DEBUG)
|
||||
# endif()
|
||||
# else()
|
||||
# target_compile_options(BLT_TESTS PRIVATE -Wall -Wextra -Wpedantic)
|
||||
# endif()
|
||||
#
|
||||
# if (${ENABLE_ADDRSAN} MATCHES ON)
|
||||
# target_compile_options(BLT_TESTS PRIVATE -fsanitize=address)
|
||||
# target_link_options(BLT_TESTS PRIVATE -fsanitize=address)
|
||||
# endif ()
|
||||
#
|
||||
# if (${ENABLE_UBSAN} MATCHES ON)
|
||||
# target_compile_options(BLT_TESTS PRIVATE -fsanitize=undefined)
|
||||
# target_link_options(BLT_TESTS PRIVATE -fsanitize=undefined)
|
||||
# endif ()
|
||||
#
|
||||
# if (${ENABLE_TSAN} MATCHES ON)
|
||||
# target_compile_options(BLT_TESTS PRIVATE -fsanitize=thread)
|
||||
# target_link_options(BLT_TESTS PRIVATE -fsanitize=thread)
|
||||
# endif ()
|
||||
#
|
||||
# message("BLT tests included!")
|
||||
#endif()
|
||||
|
|
|
@ -77,7 +77,7 @@ namespace blt
|
|||
}
|
||||
|
||||
// returns the first flag that starts with '--' otherwise return the first '-' flag
|
||||
[[nodiscard]] std::string getFirstFullFlag();
|
||||
[[nodiscard]] std::string getFirstFullFlag() const;
|
||||
};
|
||||
|
||||
class arg_nargs_t
|
||||
|
@ -127,13 +127,22 @@ namespace blt
|
|||
std::string a_version{};
|
||||
std::string a_metavar{};
|
||||
bool a_required = true;
|
||||
bool a_disable_help = false;
|
||||
};
|
||||
|
||||
class arg_builder
|
||||
{
|
||||
private:
|
||||
arg_properties_t properties;
|
||||
public:
|
||||
arg_builder(const arg_vector_t& flags): properties(flags)
|
||||
explicit arg_builder(const arg_vector_t& flags): properties(flags)
|
||||
{}
|
||||
|
||||
arg_builder(const std::initializer_list<std::string>& flags): properties(flags)
|
||||
{}
|
||||
|
||||
template<typename... string_args>
|
||||
explicit arg_builder(string_args... flags): properties({flags...})
|
||||
{}
|
||||
|
||||
inline arg_properties_t build()
|
||||
|
@ -171,6 +180,12 @@ namespace blt
|
|||
return *this;
|
||||
}
|
||||
|
||||
inline arg_builder& disableHelp()
|
||||
{
|
||||
properties.a_disable_help = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline arg_builder& setHelp(const std::string& help)
|
||||
{
|
||||
properties.a_help = help;
|
||||
|
@ -203,7 +218,7 @@ namespace blt
|
|||
std::vector<std::string> args;
|
||||
size_t currentIndex = 0;
|
||||
public:
|
||||
arg_tokenizer(std::vector<std::string> args): args(std::move(args))
|
||||
explicit arg_tokenizer(std::vector<std::string> args): args(std::move(args))
|
||||
{}
|
||||
|
||||
// returns the current arg
|
||||
|
@ -265,11 +280,11 @@ namespace blt
|
|||
friend arg_parse;
|
||||
private:
|
||||
// stores dest value not the flag/name!
|
||||
HASHSET<std::string> found_args;
|
||||
HASHSET <std::string> found_args;
|
||||
std::vector<std::string> unrecognized_args;
|
||||
public:
|
||||
std::string program_name;
|
||||
HASHMAP<std::string, arg_data_t> data;
|
||||
HASHMAP <std::string, arg_data_t> data;
|
||||
|
||||
inline arg_data_t& operator[](const std::string& key)
|
||||
{
|
||||
|
@ -296,8 +311,11 @@ namespace blt
|
|||
}
|
||||
} loaded_args;
|
||||
|
||||
bool help_disabled = false;
|
||||
std::string help_inclusion;
|
||||
private:
|
||||
static std::string getMetavar(const arg_properties_t* const& arg);
|
||||
|
||||
static std::string getFlagHelp(const arg_properties_t* const& arg);
|
||||
|
||||
static bool takesArgs(const arg_properties_t* const& arg);
|
||||
|
@ -319,6 +337,8 @@ namespace blt
|
|||
|
||||
void processFlag(arg_tokenizer& tokenizer, const std::string& flag);
|
||||
|
||||
void handleFlag(arg_tokenizer& tokenizer, const std::string& flag, const arg_properties_t* properties);
|
||||
|
||||
public:
|
||||
|
||||
template<typename T>
|
||||
|
@ -398,6 +418,11 @@ namespace blt
|
|||
user_args.max_line_length = size;
|
||||
}
|
||||
|
||||
inline void setHelpExtras(std::string str)
|
||||
{
|
||||
help_inclusion = std::move(str);
|
||||
}
|
||||
|
||||
static std::string filename(const std::string& path);
|
||||
|
||||
~arg_parse()
|
||||
|
|
|
@ -13,6 +13,13 @@
|
|||
|
||||
namespace blt::fs
|
||||
{
|
||||
class path
|
||||
{
|
||||
private:
|
||||
std::vector<std::string> paths;
|
||||
public:
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* A simple interface which provides a way of reading the next block of data from a resource.
|
||||
|
@ -102,7 +109,8 @@ namespace blt::fs
|
|||
|
||||
int read(char* buffer, size_t bytes) override;
|
||||
|
||||
size_t gcount() final {
|
||||
size_t gcount() final
|
||||
{
|
||||
return m_stream.gcount();
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,20 @@
|
|||
#ifndef BLT_HASH_MAP_H
|
||||
#define BLT_HASH_MAP_H
|
||||
|
||||
namespace blt
|
||||
{
|
||||
|
||||
// template<typename K, typename V, typename Hash = std::hash<K>, typename Eq = std::equal_to<K>>
|
||||
// class hashmap
|
||||
// {
|
||||
// private:
|
||||
//
|
||||
// public:
|
||||
//
|
||||
// };
|
||||
|
||||
}
|
||||
|
||||
#ifndef HASHMAP
|
||||
#if defined __has_include && __has_include(<parallel_hashmap/phmap.h>)
|
||||
|
||||
|
|
|
@ -11,39 +11,79 @@
|
|||
#ifndef BLT_TIME_H
|
||||
#define BLT_TIME_H
|
||||
|
||||
namespace blt::system {
|
||||
static inline std::string ensureHasDigits(int current, int digits) {
|
||||
namespace blt::system
|
||||
{
|
||||
static inline std::string ensureHasDigits(int current, int digits)
|
||||
{
|
||||
std::string asString = std::to_string(current);
|
||||
auto length = digits - asString.length();
|
||||
if (length <= 0)
|
||||
return asString;
|
||||
std::string zeros;
|
||||
zeros.reserve(length);
|
||||
for (unsigned int i = 0; i < length; i++){
|
||||
for (unsigned int i = 0; i < length; i++)
|
||||
{
|
||||
zeros += '0';
|
||||
}
|
||||
return zeros + asString;
|
||||
}
|
||||
|
||||
static inline auto getCurrentTimeNanoseconds() {
|
||||
static inline auto getCurrentTimeNanoseconds()
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
static inline auto nanoTime() {
|
||||
static inline auto nanoTime()
|
||||
{
|
||||
return getCurrentTimeNanoseconds();
|
||||
}
|
||||
|
||||
static inline auto getCurrentTimeMilliseconds(){
|
||||
static inline auto getCurrentTimeMilliseconds()
|
||||
{
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
|
||||
}
|
||||
|
||||
static inline auto getCPUThreadTime()
|
||||
{
|
||||
#ifdef unix
|
||||
timespec time{};
|
||||
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time);
|
||||
return time.tv_nsec;
|
||||
#else
|
||||
return std::clock();
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline auto getCPUTime()
|
||||
{
|
||||
#ifdef unix
|
||||
timespec time{};
|
||||
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time);
|
||||
return time.tv_nsec;
|
||||
#else
|
||||
return std::clock();
|
||||
#endif
|
||||
}
|
||||
|
||||
static inline auto getCPUTimerResolution()
|
||||
{
|
||||
#ifdef unix
|
||||
timespec res{};
|
||||
clock_getres(CLOCK_PROCESS_CPUTIME_ID, &res);
|
||||
return res.tv_nsec;
|
||||
#else
|
||||
return CLOCKS_PER_SECOND;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Standard time string is formatted as follows:
|
||||
* Year-Month-Date Hour:Min:Second
|
||||
* If you do not want a space in the string use getTimeStringFS(); (Time String for easy filesystem)
|
||||
* @return the BLT standard string of time.now
|
||||
*/
|
||||
static inline std::string getTimeString() {
|
||||
static inline std::string getTimeString()
|
||||
{
|
||||
auto t = std::time(nullptr);
|
||||
auto now = std::localtime(&t);
|
||||
std::stringstream timeString;
|
||||
|
@ -66,7 +106,8 @@ namespace blt::system {
|
|||
* Hour:Min:Second
|
||||
* @return the BLT standard logging string of time.now
|
||||
*/
|
||||
static inline std::string getTimeStringLog() {
|
||||
static inline std::string getTimeStringLog()
|
||||
{
|
||||
auto t = std::time(nullptr);
|
||||
auto now = std::localtime(&t);
|
||||
std::string timeString = "[";
|
||||
|
@ -82,7 +123,8 @@ namespace blt::system {
|
|||
/**
|
||||
* @return the BLT standard string of time.now (See getTimeString()) that is filesystem friendly (FAT compatible).
|
||||
*/
|
||||
static inline std::string getTimeStringFS() {
|
||||
static inline std::string getTimeStringFS()
|
||||
{
|
||||
auto t = std::time(nullptr);
|
||||
auto now = std::localtime(&t);
|
||||
std::stringstream timeString;
|
||||
|
|
|
@ -68,7 +68,7 @@ namespace blt
|
|||
name = str;
|
||||
}
|
||||
|
||||
std::string arg_vector_t::getFirstFullFlag()
|
||||
std::string arg_vector_t::getFirstFullFlag() const
|
||||
{
|
||||
// assign flag so it always exists, will be first '-' flag if we fail to find a '--' flag
|
||||
std::string flag = flags[0];
|
||||
|
@ -231,8 +231,26 @@ namespace blt
|
|||
loaded_args.unrecognized_args.push_back(tokenizer.get());
|
||||
else
|
||||
{
|
||||
loaded_args.data[user_args.name_associations[index]->a_dest] = tokenizer.get();
|
||||
loaded_args.found_args.insert(user_args.name_associations[index]->a_dest);
|
||||
bool found = false;
|
||||
for (const auto& pos_properties : user_args.name_associations)
|
||||
{
|
||||
const auto& flag = tokenizer.get();
|
||||
if (pos_properties->a_flags.contains(flag))
|
||||
{
|
||||
tokenizer.advance();
|
||||
handleFlag(tokenizer, flag, pos_properties);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found)
|
||||
{
|
||||
const auto& arg_property = user_args.name_associations[index];
|
||||
if (arg_property->a_disable_help)
|
||||
help_disabled = true;
|
||||
loaded_args.data[arg_property->a_dest] = tokenizer.get();
|
||||
loaded_args.found_args.insert(arg_property->a_dest);
|
||||
}
|
||||
}
|
||||
tokenizer.advance();
|
||||
}
|
||||
|
@ -282,6 +300,11 @@ namespace blt
|
|||
|
||||
const auto* const properties = user_args.flag_associations.at(flag);
|
||||
|
||||
handleFlag(tokenizer, flag, properties);
|
||||
}
|
||||
|
||||
void arg_parse::handleFlag(arg_tokenizer& tokenizer, const std::string& flag, const arg_properties_t* properties)
|
||||
{
|
||||
if (properties->a_dest.empty())
|
||||
{
|
||||
loaded_args.unrecognized_args.push_back(flag);
|
||||
|
@ -291,6 +314,9 @@ namespace blt
|
|||
|
||||
loaded_args.found_args.insert(dest);
|
||||
|
||||
if (properties->a_disable_help)
|
||||
help_disabled = true;
|
||||
|
||||
switch (properties->a_action)
|
||||
{
|
||||
case arg_action_t::HELP:
|
||||
|
@ -438,6 +464,8 @@ namespace blt
|
|||
|
||||
void arg_parse::printHelp() const
|
||||
{
|
||||
if (help_disabled)
|
||||
return;
|
||||
if (!user_args.prefix.empty())
|
||||
{
|
||||
std::cout << "\n";
|
||||
|
@ -452,7 +480,8 @@ namespace blt
|
|||
{
|
||||
if (!arg->a_flags.isFlag())
|
||||
max_length = std::max(arg->a_flags.name.size(), max_length);
|
||||
else {
|
||||
else
|
||||
{
|
||||
auto tmp = getFlagHelp(arg);
|
||||
max_length = std::max(tmp.size(), max_length);
|
||||
}
|
||||
|
@ -495,7 +524,9 @@ namespace blt
|
|||
|
||||
void arg_parse::printUsage() const
|
||||
{
|
||||
std::string usage = "Usage: " + filename(loaded_args.program_name) + " ";
|
||||
if (help_disabled)
|
||||
return;
|
||||
std::string usage = "Usage: " + filename(loaded_args.program_name) + " " + help_inclusion + " ";
|
||||
std::cout << usage;
|
||||
size_t current_line_length = 0;
|
||||
|
||||
|
@ -566,7 +597,7 @@ namespace blt
|
|||
}
|
||||
tmp += ", ";
|
||||
}
|
||||
tmp = tmp.substr(0, tmp.size()-2);
|
||||
tmp = tmp.substr(0, tmp.size() - 2);
|
||||
return tmp;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue