sophuwu.site > crls
removing unused libraries
sophuwu sophie@sophuwu.site
Sun, 06 Aug 2023 08:38:31 +0200
commit

fa567199f75ee229ab17e1f0f0b30b01847f7908

parent

f732a24737823494480c92ab5620efdcbfa34839

4 files changed, 9 insertions(+), 2267 deletions(-)

jump to
D argh.h

@@ -1,485 +0,0 @@

-#pragma once - -#include <algorithm> -#include <sstream> -#include <limits> -#include <string> -#include <vector> -#include <set> -#include <map> -#include <cassert> - -namespace argh -{ - // Terminology: - // A command line is composed of 2 types of args: - // 1. Positional args, i.e. free standing values - // 2. Options: args beginning with '-'. We identify two kinds: - // 2.1: Flags: boolean options => (exist ? true : false) - // 2.2: Parameters: a name followed by a non-option value - -#if !defined(__GNUC__) || (__GNUC__ >= 5) - using string_stream = std::istringstream; -#else - // Until GCC 5, istringstream did not have a move constructor. - // stringstream_proxy is used instead, as a workaround. - class stringstream_proxy - { - public: - stringstream_proxy() = default; - - // Construct with a value. - stringstream_proxy(std::string const& value) : - stream_(value) - {} - - // Copy constructor. - stringstream_proxy(const stringstream_proxy& other) : - stream_(other.stream_.str()) - { - stream_.setstate(other.stream_.rdstate()); - } - - void setstate(std::ios_base::iostate state) { stream_.setstate(state); } - - // Stream out the value of the parameter. - // If the conversion was not possible, the stream will enter the fail state, - // and operator bool will return false. - template<typename T> - stringstream_proxy& operator >> (T& thing) - { - stream_ >> thing; - return *this; - } - - - // Get the string value. - std::string str() const { return stream_.str(); } - - std::stringbuf* rdbuf() const { return stream_.rdbuf(); } - - // Check the state of the stream. - // False when the most recent stream operation failed - explicit operator bool() const { return !!stream_; } - - ~stringstream_proxy() = default; - private: - std::istringstream stream_; - }; - using string_stream = stringstream_proxy; -#endif - - class multimap_iteration_wrapper - { - public: - using container_t = std::multimap<std::string, std::string>; - using iterator_t = container_t::const_iterator; - using difference_t = container_t::difference_type; - explicit multimap_iteration_wrapper(const iterator_t& lb, const iterator_t& ub) - : lb_(lb) - , ub_(ub) - {} - - iterator_t begin() const { return lb_; } - iterator_t end() const { return ub_; } - difference_t size() const { return std::distance(lb_, ub_); } - - private: - iterator_t lb_; - iterator_t ub_; - }; - - class parser - { - public: - enum Mode { PREFER_FLAG_FOR_UNREG_OPTION = 1 << 0, - PREFER_PARAM_FOR_UNREG_OPTION = 1 << 1, - NO_SPLIT_ON_EQUALSIGN = 1 << 2, - SINGLE_DASH_IS_MULTIFLAG = 1 << 3, - }; - - parser() = default; - - parser(std::initializer_list<char const* const> pre_reg_names) - { add_params(pre_reg_names); } - - parser(const char* const argv[], int mode = PREFER_FLAG_FOR_UNREG_OPTION) - { parse(argv, mode); } - - parser(int argc, const char* const argv[], int mode = PREFER_FLAG_FOR_UNREG_OPTION) - { parse(argc, argv, mode); } - - void add_param(std::string const& name); - void add_params(std::string const& name); - - void add_param(std::initializer_list<char const* const> init_list); - void add_params(std::initializer_list<char const* const> init_list); - - void parse(const char* const argv[], int mode = PREFER_FLAG_FOR_UNREG_OPTION); - void parse(int argc, const char* const argv[], int mode = PREFER_FLAG_FOR_UNREG_OPTION); - - std::multiset<std::string> const& flags() const { return flags_; } - std::multimap<std::string, std::string> const& params() const { return params_; } - multimap_iteration_wrapper params(std::string const& name) const; - std::vector<std::string> const& pos_args() const { return pos_args_; } - - // begin() and end() for using range-for over positional args. - std::vector<std::string>::const_iterator begin() const { return pos_args_.cbegin(); } - std::vector<std::string>::const_iterator end() const { return pos_args_.cend(); } - size_t size() const { return pos_args_.size(); } - - ////////////////////////////////////////////////////////////////////////// - // Accessors - - // flag (boolean) accessors: return true if the flag appeared, otherwise false. - bool operator[](std::string const& name) const; - - // multiple flag (boolean) accessors: return true if at least one of the flag appeared, otherwise false. - bool operator[](std::initializer_list<char const* const> init_list) const; - - // returns positional arg string by order. Like argv[] but without the options - std::string const& operator[](size_t ind) const; - - // returns a std::istream that can be used to convert a positional arg to a typed value. - string_stream operator()(size_t ind) const; - - // same as above, but with a default value in case the arg is missing (index out of range). - template<typename T> - string_stream operator()(size_t ind, T&& def_val) const; - - // parameter accessors, give a name get an std::istream that can be used to convert to a typed value. - // call .str() on result to get as string - string_stream operator()(std::string const& name) const; - - // accessor for a parameter with multiple names, give a list of names, get an std::istream that can be used to convert to a typed value. - // call .str() on result to get as string - // returns the first value in the list to be found. - string_stream operator()(std::initializer_list<char const* const> init_list) const; - - // same as above, but with a default value in case the param was missing. - // Non-string def_val types must have an operator<<() (output stream operator) - // If T only has an input stream operator, pass the string version of the type as in "3" instead of 3. - template<typename T> - string_stream operator()(std::string const& name, T&& def_val) const; - - // same as above but for a list of names. returns the first value to be found. - template<typename T> - string_stream operator()(std::initializer_list<char const* const> init_list, T&& def_val) const; - - private: - string_stream bad_stream() const; - std::string trim_leading_dashes(std::string const& name) const; - bool is_number(std::string const& arg) const; - bool is_option(std::string const& arg) const; - bool got_flag(std::string const& name) const; - bool is_param(std::string const& name) const; - - private: - std::vector<std::string> args_; - std::multimap<std::string, std::string> params_; - std::vector<std::string> pos_args_; - std::multiset<std::string> flags_; - std::set<std::string> registeredParams_; - std::string empty_; - }; - - - ////////////////////////////////////////////////////////////////////////// - - inline void parser::parse(const char * const argv[], int mode) - { - int argc = 0; - for (auto argvp = argv; *argvp; ++argc, ++argvp); - parse(argc, argv, mode); - } - - ////////////////////////////////////////////////////////////////////////// - - inline void parser::parse(int argc, const char* const argv[], int mode /*= PREFER_FLAG_FOR_UNREG_OPTION*/) - { - // clear out possible previous parsing remnants - flags_.clear(); - params_.clear(); - pos_args_.clear(); - - // convert to strings - args_.resize(static_cast<decltype(args_)::size_type>(argc)); - std::transform(argv, argv + argc, args_.begin(), [](const char* const arg) { return arg; }); - - // parse line - for (auto i = 0u; i < args_.size(); ++i) - { - if (!is_option(args_[i])) - { - pos_args_.emplace_back(args_[i]); - continue; - } - - auto name = trim_leading_dashes(args_[i]); - - if (!(mode & NO_SPLIT_ON_EQUALSIGN)) - { - auto equalPos = name.find('='); - if (equalPos != std::string::npos) - { - params_.insert({ name.substr(0, equalPos), name.substr(equalPos + 1) }); - continue; - } - } - - // if the option is unregistered and should be a multi-flag - if (1 == (args_[i].size() - name.size()) && // single dash - argh::parser::SINGLE_DASH_IS_MULTIFLAG & mode && // multi-flag mode - !is_param(name)) // unregistered - { - std::string keep_param; - - if (!name.empty() && is_param(std::string(1ul, name.back()))) // last char is param - { - keep_param += name.back(); - name.resize(name.size() - 1); - } - - for (auto const& c : name) - { - flags_.emplace(std::string{ c }); - } - - if (!keep_param.empty()) - { - name = keep_param; - } - else - { - continue; // do not consider other options for this arg - } - } - - // any potential option will get as its value the next arg, unless that arg is an option too - // in that case it will be determined a flag. - if (i == args_.size() - 1 || is_option(args_[i + 1])) - { - flags_.emplace(name); - continue; - } - - // if 'name' is a pre-registered option, then the next arg cannot be a free parameter to it is skipped - // otherwise we have 2 modes: - // PREFER_FLAG_FOR_UNREG_OPTION: a non-registered 'name' is determined a flag. - // The following value (the next arg) will be a free parameter. - // - // PREFER_PARAM_FOR_UNREG_OPTION: a non-registered 'name' is determined a parameter, the next arg - // will be the value of that option. - - assert(!(mode & argh::parser::PREFER_FLAG_FOR_UNREG_OPTION) - || !(mode & argh::parser::PREFER_PARAM_FOR_UNREG_OPTION)); - - bool preferParam = mode & argh::parser::PREFER_PARAM_FOR_UNREG_OPTION; - - if (is_param(name) || preferParam) - { - params_.insert({ name, args_[i + 1] }); - ++i; // skip next value, it is not a free parameter - continue; - } - else - { - flags_.emplace(name); - } - } - } - - ////////////////////////////////////////////////////////////////////////// - - inline string_stream parser::bad_stream() const - { - string_stream bad; - bad.setstate(std::ios_base::failbit); - return bad; - } - - ////////////////////////////////////////////////////////////////////////// - - inline bool parser::is_number(std::string const& arg) const - { - // inefficient but simple way to determine if a string is a number (which can start with a '-') - std::istringstream istr(arg); - double number; - istr >> number; - return !(istr.fail() || istr.bad()); - } - - ////////////////////////////////////////////////////////////////////////// - - inline bool parser::is_option(std::string const& arg) const - { - assert(0 != arg.size()); - if (is_number(arg)) - return false; - return '-' == arg[0]; - } - - ////////////////////////////////////////////////////////////////////////// - - inline std::string parser::trim_leading_dashes(std::string const& name) const - { - auto pos = name.find_first_not_of('-'); - return std::string::npos != pos ? name.substr(pos) : name; - } - - ////////////////////////////////////////////////////////////////////////// - - inline bool argh::parser::got_flag(std::string const& name) const - { - return flags_.end() != flags_.find(trim_leading_dashes(name)); - } - - ////////////////////////////////////////////////////////////////////////// - - inline bool argh::parser::is_param(std::string const& name) const - { - return registeredParams_.count(name); - } - - ////////////////////////////////////////////////////////////////////////// - - inline bool parser::operator[](std::string const& name) const - { - return got_flag(name); - } - - ////////////////////////////////////////////////////////////////////////// - - inline bool parser::operator[](std::initializer_list<char const* const> init_list) const - { - return std::any_of(init_list.begin(), init_list.end(), [&](char const* const name) { return got_flag(name); }); - } - - ////////////////////////////////////////////////////////////////////////// - - inline std::string const& parser::operator[](size_t ind) const - { - if (ind < pos_args_.size()) - return pos_args_[ind]; - return empty_; - } - - ////////////////////////////////////////////////////////////////////////// - - inline string_stream parser::operator()(std::string const& name) const - { - auto optIt = params_.find(trim_leading_dashes(name)); - if (params_.end() != optIt) - return string_stream(optIt->second); - return bad_stream(); - } - - ////////////////////////////////////////////////////////////////////////// - - inline string_stream parser::operator()(std::initializer_list<char const* const> init_list) const - { - for (auto& name : init_list) - { - auto optIt = params_.find(trim_leading_dashes(name)); - if (params_.end() != optIt) - return string_stream(optIt->second); - } - return bad_stream(); - } - - ////////////////////////////////////////////////////////////////////////// - - template<typename T> - string_stream parser::operator()(std::string const& name, T&& def_val) const - { - auto optIt = params_.find(trim_leading_dashes(name)); - if (params_.end() != optIt) - return string_stream(optIt->second); - - std::ostringstream ostr; - ostr.precision(std::numeric_limits<long double>::max_digits10); - ostr << def_val; - return string_stream(ostr.str()); // use default - } - - ////////////////////////////////////////////////////////////////////////// - - // same as above but for a list of names. returns the first value to be found. - template<typename T> - string_stream parser::operator()(std::initializer_list<char const* const> init_list, T&& def_val) const - { - for (auto& name : init_list) - { - auto optIt = params_.find(trim_leading_dashes(name)); - if (params_.end() != optIt) - return string_stream(optIt->second); - } - std::ostringstream ostr; - ostr.precision(std::numeric_limits<long double>::max_digits10); - ostr << def_val; - return string_stream(ostr.str()); // use default - } - - ////////////////////////////////////////////////////////////////////////// - - inline string_stream parser::operator()(size_t ind) const - { - if (pos_args_.size() <= ind) - return bad_stream(); - - return string_stream(pos_args_[ind]); - } - - ////////////////////////////////////////////////////////////////////////// - - template<typename T> - string_stream parser::operator()(size_t ind, T&& def_val) const - { - if (pos_args_.size() <= ind) - { - std::ostringstream ostr; - ostr.precision(std::numeric_limits<long double>::max_digits10); - ostr << def_val; - return string_stream(ostr.str()); - } - - return string_stream(pos_args_[ind]); - } - - ////////////////////////////////////////////////////////////////////////// - - inline void parser::add_param(std::string const& name) - { - registeredParams_.insert(trim_leading_dashes(name)); - } - - ////////////////////////////////////////////////////////////////////////// - - inline void parser::add_param(std::initializer_list<const char *const> init_list) - { - parser::add_params(init_list); - } - - ////////////////////////////////////////////////////////////////////////// - - inline void parser::add_params(std::initializer_list<char const* const> init_list) - { - for (auto& name : init_list) - registeredParams_.insert(trim_leading_dashes(name)); - } - - ////////////////////////////////////////////////////////////////////////// - - inline void parser::add_params(const std::string &name) - { - parser::add_param(name); - } - - ////////////////////////////////////////////////////////////////////////// - - inline multimap_iteration_wrapper parser::params(std::string const& name) const - { - auto trimmed_name = trim_leading_dashes(name); - return multimap_iteration_wrapper(params_.lower_bound(trimmed_name), params_.upper_bound(trimmed_name)); - } -}
M main.cppmain.cpp

@@ -126,7 +126,7 @@ else item.size = convsize(0);

files.push_back(item); } len /= i; - return len*10; + return (10+len)*10; } int charcmp(char a, char b) {

@@ -173,11 +173,11 @@ r.init(30);

std::string path = getpath(argc, argv); - r.print2d(path+"\n\n", path.length()); - r.next(); - if (!std::filesystem::exists(path) || !std::filesystem::is_directory(path)) { - r.print2d(" Directory not found.\n\n"); + r.print2d(path+"\n"); + r.next(); + r.next(); + r.print2d(" Directory not found.\n"); return 1; }

@@ -190,7 +190,11 @@ sortdir(files,dirlist);

if (dirlist.size()<30) r.init(dirlist.size()); + r.print2d(path+"\n", path.length()); + r.next(); + for (int i = 0; i < dirlist.size(); i++) { + printf(" "); r.print2d(dirlist[i], len); r.next(); }
D termcolor.hpp

@@ -1,939 +0,0 @@

-//! -//! termcolor -//! ~~~~~~~~~ -//! -//! termcolor is a header-only c++ library for printing colored messages -//! to the terminal. Written just for fun with a help of the Force. -//! -//! :copyright: (c) 2013 by Ihor Kalnytskyi -//! :license: BSD, see LICENSE for details -//! - -#ifndef TERMCOLOR_HPP_ -#define TERMCOLOR_HPP_ - -#include <iostream> - -// Detect target's platform and set some macros in order to wrap platform -// specific code this library depends on. -#if defined(_WIN32) || defined(_WIN64) -# define TERMCOLOR_TARGET_WINDOWS -#elif defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)) -# define TERMCOLOR_TARGET_POSIX -#endif - -// If implementation has not been explicitly set, try to choose one based on -// target platform. -#if !defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) && !defined(TERMCOLOR_USE_WINDOWS_API) && !defined(TERMCOLOR_USE_NOOP) -# if defined(TERMCOLOR_TARGET_POSIX) -# define TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES -# define TERMCOLOR_AUTODETECTED_IMPLEMENTATION -# elif defined(TERMCOLOR_TARGET_WINDOWS) -# define TERMCOLOR_USE_WINDOWS_API -# define TERMCOLOR_AUTODETECTED_IMPLEMENTATION -# endif -#endif - -// These headers provide isatty()/fileno() functions, which are used for -// testing whether a standard stream refers to the terminal. -#if defined(TERMCOLOR_TARGET_POSIX) -# include <unistd.h> -#elif defined(TERMCOLOR_TARGET_WINDOWS) -# include <io.h> -# include <windows.h> -#endif - - -namespace termcolor -{ - // Forward declaration of the `_internal` namespace. - // All comments are below. - namespace _internal - { - inline int colorize_index(); - inline FILE* get_standard_stream(const std::ostream& stream); - inline FILE* get_standard_stream(const std::wostream& stream); - template <typename CharT> - bool is_colorized(std::basic_ostream<CharT>& stream); - template <typename CharT> - bool is_atty(const std::basic_ostream<CharT>& stream); - - #if defined(TERMCOLOR_TARGET_WINDOWS) - template <typename CharT> - void win_change_attributes(std::basic_ostream<CharT>& stream, int foreground, int background = -1); - #endif - } - - template <typename CharT> - std::basic_ostream<CharT>& colorize(std::basic_ostream<CharT>& stream) - { - stream.iword(_internal::colorize_index()) = 1L; - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& nocolorize(std::basic_ostream<CharT>& stream) - { - stream.iword(_internal::colorize_index()) = 0L; - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& reset(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[00m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, -1); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& bold(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[1m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& dark(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[2m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& italic(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[3m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& underline(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[4m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, COMMON_LVB_UNDERSCORE); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& blink(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[5m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& reverse(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[7m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& concealed(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[8m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& crossed(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[9m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - #endif - } - return stream; - } - - template <uint8_t code, typename CharT> - std::basic_ostream<CharT>& color(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[38;5;" << +code << "m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - #endif - } - return stream; - } - - template <uint8_t code, typename CharT> - std::basic_ostream<CharT>& on_color(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[48;5;" << +code << "m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - #endif - } - return stream; - } - - template <uint8_t r, uint8_t g, uint8_t b, typename CharT> - std::basic_ostream<CharT>& color(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[38;2;" << +r << ";" << +g << ";" << +b << "m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - #endif - } - return stream; - } - - template <uint8_t r, uint8_t g, uint8_t b, typename CharT> - std::basic_ostream<CharT>& on_color(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[48;2;" << +r << ";" << +g << ";" << +b << "m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& grey(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[30m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - 0 // grey (black) - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& red(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[31m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_RED - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& green(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[32m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_GREEN - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& yellow(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[33m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_GREEN | FOREGROUND_RED - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& blue(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[34m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_BLUE - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& magenta(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[35m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_BLUE | FOREGROUND_RED - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& cyan(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[36m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_BLUE | FOREGROUND_GREEN - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& white(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[37m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED - ); - #endif - } - return stream; - } - - - template <typename CharT> - std::basic_ostream<CharT>& bright_grey(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[90m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - 0 | FOREGROUND_INTENSITY // grey (black) - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& bright_red(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[91m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_RED | FOREGROUND_INTENSITY - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& bright_green(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[92m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_GREEN | FOREGROUND_INTENSITY - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& bright_yellow(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[93m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& bright_blue(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[94m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_BLUE | FOREGROUND_INTENSITY - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& bright_magenta(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[95m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& bright_cyan(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[96m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& bright_white(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[97m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, - FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY - ); - #endif - } - return stream; - } - - - template <typename CharT> - std::basic_ostream<CharT>& on_grey(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[40m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - 0 // grey (black) - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_red(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[41m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_RED - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_green(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[42m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_GREEN - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_yellow(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[43m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_GREEN | BACKGROUND_RED - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_blue(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[44m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_BLUE - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_magenta(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[45m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_BLUE | BACKGROUND_RED - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_cyan(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[46m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_GREEN | BACKGROUND_BLUE - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_white(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[47m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED - ); - #endif - } - - return stream; - } - - - template <typename CharT> - std::basic_ostream<CharT>& on_bright_grey(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[100m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - 0 | BACKGROUND_INTENSITY // grey (black) - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_bright_red(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[101m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_RED | BACKGROUND_INTENSITY - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_bright_green(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[102m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_GREEN | BACKGROUND_INTENSITY - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_bright_yellow(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[103m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_bright_blue(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[104m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_BLUE | BACKGROUND_INTENSITY - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_bright_magenta(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[105m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_bright_cyan(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[106m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_INTENSITY - ); - #endif - } - return stream; - } - - template <typename CharT> - std::basic_ostream<CharT>& on_bright_white(std::basic_ostream<CharT>& stream) - { - if (_internal::is_colorized(stream)) - { - #if defined(TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES) - stream << "\033[107m"; - #elif defined(TERMCOLOR_USE_WINDOWS_API) - _internal::win_change_attributes(stream, -1, - BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY - ); - #endif - } - - return stream; - } - - - - //! Since C++ hasn't a way to hide something in the header from - //! the outer access, I have to introduce this namespace which - //! is used for internal purpose and should't be access from - //! the user code. - namespace _internal - { - // An index to be used to access a private storage of I/O streams. See - // colorize / nocolorize I/O manipulators for details. Due to the fact - // that static variables ain't shared between translation units, inline - // function with local static variable is used to do the trick and share - // the variable value between translation units. - inline int colorize_index() - { - static int colorize_index = std::ios_base::xalloc(); - return colorize_index; - } - - //! Since C++ hasn't a true way to extract stream handler - //! from the a given `std::ostream` object, I have to write - //! this kind of hack. - inline - FILE* get_standard_stream(const std::ostream& stream) - { - if (&stream == &std::cout) - return stdout; - else if (&stream == &std::cerr || &stream == &std::clog) - return stderr; - - return nullptr; - } - - //! Since C++ hasn't a true way to extract stream handler - //! from the a given `std::wostream` object, I have to write - //! this kind of hack. - inline - FILE* get_standard_stream(const std::wostream& stream) - { - if (&stream == &std::wcout) - return stdout; - else if (&stream == &std::wcerr || &stream == &std::wclog) - return stderr; - - return nullptr; - } - - // Say whether a given stream should be colorized or not. It's always - // true for ATTY streams and may be true for streams marked with - // colorize flag. - template <typename CharT> - bool is_colorized(std::basic_ostream<CharT>& stream) - { - return is_atty(stream) || static_cast<bool>(stream.iword(colorize_index())); - } - - //! Test whether a given `std::ostream` object refers to - //! a terminal. - template <typename CharT> - bool is_atty(const std::basic_ostream<CharT>& stream) - { - FILE* std_stream = get_standard_stream(stream); - - // Unfortunately, fileno() ends with segmentation fault - // if invalid file descriptor is passed. So we need to - // handle this case gracefully and assume it's not a tty - // if standard stream is not detected, and 0 is returned. - if (!std_stream) - return false; - - #if defined(TERMCOLOR_TARGET_POSIX) - return ::isatty(fileno(std_stream)); - #elif defined(TERMCOLOR_TARGET_WINDOWS) - return ::_isatty(_fileno(std_stream)); - #else - return false; - #endif - } - - #if defined(TERMCOLOR_TARGET_WINDOWS) - - //! same hack as used in get_standard_stream function, but for Windows with `std::ostream` - inline HANDLE get_terminal_handle(std::ostream& stream) - { - if (&stream == &std::cout) - return GetStdHandle(STD_OUTPUT_HANDLE); - else if (&stream == &std::cerr || &stream == &std::clog) - return GetStdHandle(STD_ERROR_HANDLE); - return nullptr; - } - - //! same hack as used in get_standard_stream function, but for Windows with `std::wostream` - inline HANDLE get_terminal_handle(std::wostream& stream) - { - if (&stream == &std::wcout) - return GetStdHandle(STD_OUTPUT_HANDLE); - else if (&stream == &std::wcerr || &stream == &std::wclog) - return GetStdHandle(STD_ERROR_HANDLE); - return nullptr; - } - - //! Change Windows Terminal colors attribute. If some - //! parameter is `-1` then attribute won't changed. - template <typename CharT> - void win_change_attributes(std::basic_ostream<CharT>& stream, int foreground, int background) - { - // yeah, i know.. it's ugly, it's windows. - static WORD defaultAttributes = 0; - - // Windows doesn't have ANSI escape sequences and so we use special - // API to change Terminal output color. That means we can't - // manipulate colors by means of "std::stringstream" and hence - // should do nothing in this case. - if (!_internal::is_atty(stream)) - return; - - // get terminal handle - HANDLE hTerminal = INVALID_HANDLE_VALUE; - hTerminal = get_terminal_handle(stream); - - // save default terminal attributes if it unsaved - if (!defaultAttributes) - { - CONSOLE_SCREEN_BUFFER_INFO info; - if (!GetConsoleScreenBufferInfo(hTerminal, &info)) - return; - defaultAttributes = info.wAttributes; - } - - // restore all default settings - if (foreground == -1 && background == -1) - { - SetConsoleTextAttribute(hTerminal, defaultAttributes); - return; - } - - // get current settings - CONSOLE_SCREEN_BUFFER_INFO info; - if (!GetConsoleScreenBufferInfo(hTerminal, &info)) - return; - - if (foreground != -1) - { - info.wAttributes &= ~(info.wAttributes & 0x0F); - info.wAttributes |= static_cast<WORD>(foreground); - } - - if (background != -1) - { - info.wAttributes &= ~(info.wAttributes & 0xF0); - info.wAttributes |= static_cast<WORD>(background); - } - - SetConsoleTextAttribute(hTerminal, info.wAttributes); - } - #endif // TERMCOLOR_TARGET_WINDOWS - - } // namespace _internal - -} // namespace termcolor - - -#undef TERMCOLOR_TARGET_POSIX -#undef TERMCOLOR_TARGET_WINDOWS - -#if defined(TERMCOLOR_AUTODETECTED_IMPLEMENTATION) -# undef TERMCOLOR_USE_ANSI_ESCAPE_SEQUENCES -# undef TERMCOLOR_USE_WINDOWS_API -#endif - -#endif // TERMCOLOR_HPP_
D tinydir.h

@@ -1,838 +0,0 @@

-/* -Copyright (c) 2013-2021, tinydir authors: -- Cong Xu -- Lautis Sun -- Baudouin Feildel -- Andargor <andargor@yahoo.com> -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR -ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ -#ifndef TINYDIR_H -#define TINYDIR_H - -#ifdef __cplusplus -extern "C" { -#endif - -#if ((defined _UNICODE) && !(defined UNICODE)) -#define UNICODE -#endif - -#if ((defined UNICODE) && !(defined _UNICODE)) -#define _UNICODE -#endif - -#include <errno.h> -#include <stdlib.h> -#include <string.h> -#ifdef _MSC_VER -# ifndef WIN32_LEAN_AND_MEAN -# define WIN32_LEAN_AND_MEAN -# endif -# include <windows.h> -# include <tchar.h> -# pragma warning(push) -# pragma warning (disable : 4996) -#else -# include <dirent.h> -# include <libgen.h> -# include <sys/stat.h> -# include <stddef.h> -#endif -#ifdef __MINGW32__ -# include <tchar.h> -#endif - - -/* types */ - -/* Windows UNICODE wide character support */ -#if defined _MSC_VER || defined __MINGW32__ -# define _tinydir_char_t TCHAR -# define TINYDIR_STRING(s) _TEXT(s) -# define _tinydir_strlen _tcslen -# define _tinydir_strcpy _tcscpy -# define _tinydir_strcat _tcscat -# define _tinydir_strcmp _tcscmp -# define _tinydir_strrchr _tcsrchr -# define _tinydir_strncmp _tcsncmp -#else -# define _tinydir_char_t char -# define TINYDIR_STRING(s) s -# define _tinydir_strlen strlen -# define _tinydir_strcpy strcpy -# define _tinydir_strcat strcat -# define _tinydir_strcmp strcmp -# define _tinydir_strrchr strrchr -# define _tinydir_strncmp strncmp -#endif - -#if (defined _MSC_VER || defined __MINGW32__) -# include <windows.h> -# define _TINYDIR_PATH_MAX MAX_PATH -#elif defined __linux__ -# include <limits.h> -# ifdef PATH_MAX -# define _TINYDIR_PATH_MAX PATH_MAX -# endif -#elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) -# include <sys/param.h> -# if defined(BSD) -# include <limits.h> -# ifdef PATH_MAX -# define _TINYDIR_PATH_MAX PATH_MAX -# endif -# endif -#endif - -#ifndef _TINYDIR_PATH_MAX -#define _TINYDIR_PATH_MAX 4096 -#endif - -#ifdef _MSC_VER -/* extra chars for the "\\*" mask */ -# define _TINYDIR_PATH_EXTRA 2 -#else -# define _TINYDIR_PATH_EXTRA 0 -#endif - -#define _TINYDIR_FILENAME_MAX 256 - -#if (defined _MSC_VER || defined __MINGW32__) -#define _TINYDIR_DRIVE_MAX 3 -#endif - -#ifdef _MSC_VER -# define _TINYDIR_FUNC static __inline -#elif !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L -# define _TINYDIR_FUNC static __inline__ -#elif defined(__cplusplus) -# define _TINYDIR_FUNC static inline -#elif defined(__GNUC__) -/* Suppress unused function warning */ -# define _TINYDIR_FUNC __attribute__((unused)) static -#else -# define _TINYDIR_FUNC static -#endif - -/* readdir_r usage; define TINYDIR_USE_READDIR_R to use it (if supported) */ -#ifdef TINYDIR_USE_READDIR_R - -/* readdir_r is a POSIX-only function, and may not be available under various - * environments/settings, e.g. MinGW. Use readdir fallback */ -#if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE ||\ - _POSIX_SOURCE -# define _TINYDIR_HAS_READDIR_R -#endif -#if _POSIX_C_SOURCE >= 200112L -# define _TINYDIR_HAS_FPATHCONF -# include <unistd.h> -#endif -#if _BSD_SOURCE || _SVID_SOURCE || \ - (_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700) -# define _TINYDIR_HAS_DIRFD -# include <sys/types.h> -#endif -#if defined _TINYDIR_HAS_FPATHCONF && defined _TINYDIR_HAS_DIRFD &&\ - defined _PC_NAME_MAX -# define _TINYDIR_USE_FPATHCONF -#endif -#if defined __MINGW32__ || !defined _TINYDIR_HAS_READDIR_R ||\ - !(defined _TINYDIR_USE_FPATHCONF || defined NAME_MAX) -# define _TINYDIR_USE_READDIR -#endif - -/* Use readdir by default */ -#else -# define _TINYDIR_USE_READDIR -#endif - -/* MINGW32 has two versions of dirent, ASCII and UNICODE*/ -#ifndef _MSC_VER -#if (defined __MINGW32__) && (defined _UNICODE) -#define _TINYDIR_DIR _WDIR -#define _tinydir_dirent _wdirent -#define _tinydir_opendir _wopendir -#define _tinydir_readdir _wreaddir -#define _tinydir_closedir _wclosedir -#else -#define _TINYDIR_DIR DIR -#define _tinydir_dirent dirent -#define _tinydir_opendir opendir -#define _tinydir_readdir readdir -#define _tinydir_closedir closedir -#endif -#endif - -/* Allow user to use a custom allocator by defining _TINYDIR_MALLOC and _TINYDIR_FREE. */ -#if defined(_TINYDIR_MALLOC) && defined(_TINYDIR_FREE) -#elif !defined(_TINYDIR_MALLOC) && !defined(_TINYDIR_FREE) -#else -#error "Either define both alloc and free or none of them!" -#endif - -#if !defined(_TINYDIR_MALLOC) - #define _TINYDIR_MALLOC(_size) malloc(_size) - #define _TINYDIR_FREE(_ptr) free(_ptr) -#endif /* !defined(_TINYDIR_MALLOC) */ - -typedef struct tinydir_file -{ - _tinydir_char_t path[_TINYDIR_PATH_MAX]; - _tinydir_char_t name[_TINYDIR_FILENAME_MAX]; - _tinydir_char_t *extension; - int is_dir; - int is_reg; - -#ifndef _MSC_VER -#ifdef __MINGW32__ - struct _stat _s; -#else - struct stat _s; -#endif -#endif -} tinydir_file; - -typedef struct tinydir_dir -{ - _tinydir_char_t path[_TINYDIR_PATH_MAX]; - int has_next; - size_t n_files; - - tinydir_file *_files; -#ifdef _MSC_VER - HANDLE _h; - WIN32_FIND_DATA _f; -#else - _TINYDIR_DIR *_d; - struct _tinydir_dirent *_e; -#ifndef _TINYDIR_USE_READDIR - struct _tinydir_dirent *_ep; -#endif -#endif -} tinydir_dir; - - -/* declarations */ - -_TINYDIR_FUNC -int tinydir_open(tinydir_dir *dir, const _tinydir_char_t *path); -_TINYDIR_FUNC -int tinydir_open_sorted(tinydir_dir *dir, const _tinydir_char_t *path); -_TINYDIR_FUNC -void tinydir_close(tinydir_dir *dir); - -_TINYDIR_FUNC -int tinydir_next(tinydir_dir *dir); -_TINYDIR_FUNC -int tinydir_readfile(const tinydir_dir *dir, tinydir_file *file); -_TINYDIR_FUNC -int tinydir_readfile_n(const tinydir_dir *dir, tinydir_file *file, size_t i); -_TINYDIR_FUNC -int tinydir_open_subdir_n(tinydir_dir *dir, size_t i); - -_TINYDIR_FUNC -int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path); -_TINYDIR_FUNC -void _tinydir_get_ext(tinydir_file *file); -_TINYDIR_FUNC -int _tinydir_file_cmp(const void *a, const void *b); -#ifndef _MSC_VER -#ifndef _TINYDIR_USE_READDIR -_TINYDIR_FUNC -size_t _tinydir_dirent_buf_size(_TINYDIR_DIR *dirp); -#endif -#endif - - -/* definitions*/ - -_TINYDIR_FUNC -int tinydir_open(tinydir_dir *dir, const _tinydir_char_t *path) -{ -#ifndef _MSC_VER -#ifndef _TINYDIR_USE_READDIR - int error; - int size; /* using int size */ -#endif -#else - _tinydir_char_t path_buf[_TINYDIR_PATH_MAX]; -#endif - _tinydir_char_t *pathp; - - if (dir == NULL || path == NULL || _tinydir_strlen(path) == 0) - { - errno = EINVAL; - return -1; - } - if (_tinydir_strlen(path) + _TINYDIR_PATH_EXTRA >= _TINYDIR_PATH_MAX) - { - errno = ENAMETOOLONG; - return -1; - } - - /* initialise dir */ - dir->_files = NULL; -#ifdef _MSC_VER - dir->_h = INVALID_HANDLE_VALUE; -#else - dir->_d = NULL; -#ifndef _TINYDIR_USE_READDIR - dir->_ep = NULL; -#endif -#endif - tinydir_close(dir); - - _tinydir_strcpy(dir->path, path); - /* Remove trailing slashes */ - pathp = &dir->path[_tinydir_strlen(dir->path) - 1]; - while (pathp != dir->path && (*pathp == TINYDIR_STRING('\\') || *pathp == TINYDIR_STRING('/'))) - { - *pathp = TINYDIR_STRING('\0'); - pathp++; - } -#ifdef _MSC_VER - _tinydir_strcpy(path_buf, dir->path); - _tinydir_strcat(path_buf, TINYDIR_STRING("\\*")); -#if (defined WINAPI_FAMILY) && (WINAPI_FAMILY != WINAPI_FAMILY_DESKTOP_APP) - dir->_h = FindFirstFileEx(path_buf, FindExInfoStandard, &dir->_f, FindExSearchNameMatch, NULL, 0); -#else - dir->_h = FindFirstFile(path_buf, &dir->_f); -#endif - if (dir->_h == INVALID_HANDLE_VALUE) - { - errno = ENOENT; -#else - dir->_d = _tinydir_opendir(path); - if (dir->_d == NULL) - { -#endif - goto bail; - } - - /* read first file */ - dir->has_next = 1; -#ifndef _MSC_VER -#ifdef _TINYDIR_USE_READDIR - dir->_e = _tinydir_readdir(dir->_d); -#else - /* allocate dirent buffer for readdir_r */ - size = _tinydir_dirent_buf_size(dir->_d); /* conversion to int */ - if (size == -1) return -1; - dir->_ep = (struct _tinydir_dirent*)_TINYDIR_MALLOC(size); - if (dir->_ep == NULL) return -1; - - error = readdir_r(dir->_d, dir->_ep, &dir->_e); - if (error != 0) return -1; -#endif - if (dir->_e == NULL) - { - dir->has_next = 0; - } -#endif - - return 0; - -bail: - tinydir_close(dir); - return -1; -} - -_TINYDIR_FUNC -int tinydir_open_sorted(tinydir_dir *dir, const _tinydir_char_t *path) -{ - /* Count the number of files first, to pre-allocate the files array */ - size_t n_files = 0; - if (tinydir_open(dir, path) == -1) - { - return -1; - } - while (dir->has_next) - { - n_files++; - if (tinydir_next(dir) == -1) - { - goto bail; - } - } - tinydir_close(dir); - - if (n_files == 0 || tinydir_open(dir, path) == -1) - { - return -1; - } - - dir->n_files = 0; - dir->_files = (tinydir_file *)_TINYDIR_MALLOC(sizeof *dir->_files * n_files); - if (dir->_files == NULL) - { - goto bail; - } - while (dir->has_next) - { - tinydir_file *p_file; - dir->n_files++; - - p_file = &dir->_files[dir->n_files - 1]; - if (tinydir_readfile(dir, p_file) == -1) - { - goto bail; - } - - if (tinydir_next(dir) == -1) - { - goto bail; - } - - /* Just in case the number of files has changed between the first and - second reads, terminate without writing into unallocated memory */ - if (dir->n_files == n_files) - { - break; - } - } - - qsort(dir->_files, dir->n_files, sizeof(tinydir_file), _tinydir_file_cmp); - - return 0; - -bail: - tinydir_close(dir); - return -1; -} - -_TINYDIR_FUNC -void tinydir_close(tinydir_dir *dir) -{ - if (dir == NULL) - { - return; - } - - memset(dir->path, 0, sizeof(dir->path)); - dir->has_next = 0; - dir->n_files = 0; - _TINYDIR_FREE(dir->_files); - dir->_files = NULL; -#ifdef _MSC_VER - if (dir->_h != INVALID_HANDLE_VALUE) - { - FindClose(dir->_h); - } - dir->_h = INVALID_HANDLE_VALUE; -#else - if (dir->_d) - { - _tinydir_closedir(dir->_d); - } - dir->_d = NULL; - dir->_e = NULL; -#ifndef _TINYDIR_USE_READDIR - _TINYDIR_FREE(dir->_ep); - dir->_ep = NULL; -#endif -#endif -} - -_TINYDIR_FUNC -int tinydir_next(tinydir_dir *dir) -{ - if (dir == NULL) - { - errno = EINVAL; - return -1; - } - if (!dir->has_next) - { - errno = ENOENT; - return -1; - } - -#ifdef _MSC_VER - if (FindNextFile(dir->_h, &dir->_f) == 0) -#else -#ifdef _TINYDIR_USE_READDIR - dir->_e = _tinydir_readdir(dir->_d); -#else - if (dir->_ep == NULL) - { - return -1; - } - if (readdir_r(dir->_d, dir->_ep, &dir->_e) != 0) - { - return -1; - } -#endif - if (dir->_e == NULL) -#endif - { - dir->has_next = 0; -#ifdef _MSC_VER - if (GetLastError() != ERROR_SUCCESS && - GetLastError() != ERROR_NO_MORE_FILES) - { - tinydir_close(dir); - errno = EIO; - return -1; - } -#endif - } - - return 0; -} - -_TINYDIR_FUNC -int tinydir_readfile(const tinydir_dir *dir, tinydir_file *file) -{ - const _tinydir_char_t *filename; - if (dir == NULL || file == NULL) - { - errno = EINVAL; - return -1; - } -#ifdef _MSC_VER - if (dir->_h == INVALID_HANDLE_VALUE) -#else - if (dir->_e == NULL) -#endif - { - errno = ENOENT; - return -1; - } - filename = -#ifdef _MSC_VER - dir->_f.cFileName; -#else - dir->_e->d_name; -#endif - if (_tinydir_strlen(dir->path) + - _tinydir_strlen(filename) + 1 + _TINYDIR_PATH_EXTRA >= - _TINYDIR_PATH_MAX) - { - /* the path for the file will be too long */ - errno = ENAMETOOLONG; - return -1; - } - if (_tinydir_strlen(filename) >= _TINYDIR_FILENAME_MAX) - { - errno = ENAMETOOLONG; - return -1; - } - - _tinydir_strcpy(file->path, dir->path); - if (_tinydir_strcmp(dir->path, TINYDIR_STRING("/")) != 0) - _tinydir_strcat(file->path, TINYDIR_STRING("/")); - _tinydir_strcpy(file->name, filename); - _tinydir_strcat(file->path, filename); -#ifndef _MSC_VER -#ifdef __MINGW32__ - if (_tstat( -#elif (defined _BSD_SOURCE) || (defined _DEFAULT_SOURCE) \ - || ((defined _XOPEN_SOURCE) && (_XOPEN_SOURCE >= 500)) \ - || ((defined _POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200112L)) \ - || ((defined __APPLE__) && (defined __MACH__)) \ - || (defined BSD) - if (lstat( -#else - if (stat( -#endif - file->path, &file->_s) == -1) - { - return -1; - } -#endif - _tinydir_get_ext(file); - - file->is_dir = -#ifdef _MSC_VER - !!(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); -#else - S_ISDIR(file->_s.st_mode); -#endif - file->is_reg = -#ifdef _MSC_VER - !!(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || - ( - !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_DEVICE) && - !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && - !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_ENCRYPTED) && -#ifdef FILE_ATTRIBUTE_INTEGRITY_STREAM - !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_INTEGRITY_STREAM) && -#endif -#ifdef FILE_ATTRIBUTE_NO_SCRUB_DATA - !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_NO_SCRUB_DATA) && -#endif - !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_OFFLINE) && - !(dir->_f.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)); -#else - S_ISREG(file->_s.st_mode); -#endif - - return 0; -} - -_TINYDIR_FUNC -int tinydir_readfile_n(const tinydir_dir *dir, tinydir_file *file, size_t i) -{ - if (dir == NULL || file == NULL) - { - errno = EINVAL; - return -1; - } - if (i >= dir->n_files) - { - errno = ENOENT; - return -1; - } - - memcpy(file, &dir->_files[i], sizeof(tinydir_file)); - _tinydir_get_ext(file); - - return 0; -} - -_TINYDIR_FUNC -int tinydir_open_subdir_n(tinydir_dir *dir, size_t i) -{ - _tinydir_char_t path[_TINYDIR_PATH_MAX]; - if (dir == NULL) - { - errno = EINVAL; - return -1; - } - if (i >= dir->n_files || !dir->_files[i].is_dir) - { - errno = ENOENT; - return -1; - } - - _tinydir_strcpy(path, dir->_files[i].path); - tinydir_close(dir); - if (tinydir_open_sorted(dir, path) == -1) - { - return -1; - } - - return 0; -} - -/* Open a single file given its path */ -_TINYDIR_FUNC -int tinydir_file_open(tinydir_file *file, const _tinydir_char_t *path) -{ - tinydir_dir dir; - int result = 0; - int found = 0; - _tinydir_char_t dir_name_buf[_TINYDIR_PATH_MAX]; - _tinydir_char_t file_name_buf[_TINYDIR_FILENAME_MAX]; - _tinydir_char_t *dir_name; - _tinydir_char_t *base_name; -#if (defined _MSC_VER || defined __MINGW32__) - _tinydir_char_t drive_buf[_TINYDIR_PATH_MAX]; - _tinydir_char_t ext_buf[_TINYDIR_FILENAME_MAX]; -#endif - - if (file == NULL || path == NULL || _tinydir_strlen(path) == 0) - { - errno = EINVAL; - return -1; - } - if (_tinydir_strlen(path) + _TINYDIR_PATH_EXTRA >= _TINYDIR_PATH_MAX) - { - errno = ENAMETOOLONG; - return -1; - } - - /* Get the parent path */ -#if (defined _MSC_VER || defined __MINGW32__) -#if ((defined _MSC_VER) && (_MSC_VER >= 1400)) - errno = _tsplitpath_s( - path, - drive_buf, _TINYDIR_DRIVE_MAX, - dir_name_buf, _TINYDIR_FILENAME_MAX, - file_name_buf, _TINYDIR_FILENAME_MAX, - ext_buf, _TINYDIR_FILENAME_MAX); -#else - _tsplitpath( - path, - drive_buf, - dir_name_buf, - file_name_buf, - ext_buf); -#endif - - if (errno) - { - return -1; - } - -/* _splitpath_s not work fine with only filename and widechar support */ -#ifdef _UNICODE - if (drive_buf[0] == L'\xFEFE') - drive_buf[0] = '\0'; - if (dir_name_buf[0] == L'\xFEFE') - dir_name_buf[0] = '\0'; -#endif - - /* Emulate the behavior of dirname by returning "." for dir name if it's - empty */ - if (drive_buf[0] == '\0' && dir_name_buf[0] == '\0') - { - _tinydir_strcpy(dir_name_buf, TINYDIR_STRING(".")); - } - /* Concatenate the drive letter and dir name to form full dir name */ - _tinydir_strcat(drive_buf, dir_name_buf); - dir_name = drive_buf; - /* Concatenate the file name and extension to form base name */ - _tinydir_strcat(file_name_buf, ext_buf); - base_name = file_name_buf; -#else - _tinydir_strcpy(dir_name_buf, path); - dir_name = dirname(dir_name_buf); - _tinydir_strcpy(file_name_buf, path); - base_name = basename(file_name_buf); -#endif - - /* Special case: if the path is a root dir, open the parent dir as the file */ -#if (defined _MSC_VER || defined __MINGW32__) - if (_tinydir_strlen(base_name) == 0) -#else - if ((_tinydir_strcmp(base_name, TINYDIR_STRING("/"))) == 0) -#endif - { - memset(file, 0, sizeof * file); - file->is_dir = 1; - file->is_reg = 0; - _tinydir_strcpy(file->path, dir_name); - file->extension = file->path + _tinydir_strlen(file->path); - return 0; - } - - /* Open the parent directory */ - if (tinydir_open(&dir, dir_name) == -1) - { - return -1; - } - - /* Read through the parent directory and look for the file */ - while (dir.has_next) - { - if (tinydir_readfile(&dir, file) == -1) - { - result = -1; - goto bail; - } - if (_tinydir_strcmp(file->name, base_name) == 0) - { - /* File found */ - found = 1; - break; - } - tinydir_next(&dir); - } - if (!found) - { - result = -1; - errno = ENOENT; - } - -bail: - tinydir_close(&dir); - return result; -} - -_TINYDIR_FUNC -void _tinydir_get_ext(tinydir_file *file) -{ - _tinydir_char_t *period = _tinydir_strrchr(file->name, TINYDIR_STRING('.')); - if (period == NULL) - { - file->extension = &(file->name[_tinydir_strlen(file->name)]); - } - else - { - file->extension = period + 1; - } -} - -_TINYDIR_FUNC -int _tinydir_file_cmp(const void *a, const void *b) -{ - const tinydir_file *fa = (const tinydir_file *)a; - const tinydir_file *fb = (const tinydir_file *)b; - if (fa->is_dir != fb->is_dir) - { - return -(fa->is_dir - fb->is_dir); - } - return _tinydir_strncmp(fa->name, fb->name, _TINYDIR_FILENAME_MAX); -} - -#ifndef _MSC_VER -#ifndef _TINYDIR_USE_READDIR -/* -The following authored by Ben Hutchings <ben@decadent.org.uk> -from https://womble.decadent.org.uk/readdir_r-advisory.html -*/ -/* Calculate the required buffer size (in bytes) for directory * -* entries read from the given directory handle. Return -1 if this * -* this cannot be done. * -* * -* This code does not trust values of NAME_MAX that are less than * -* 255, since some systems (including at least HP-UX) incorrectly * -* define it to be a smaller value. */ -_TINYDIR_FUNC -size_t _tinydir_dirent_buf_size(_TINYDIR_DIR *dirp) -{ - long name_max; - size_t name_end; - /* parameter may be unused */ - (void)dirp; - -#if defined _TINYDIR_USE_FPATHCONF - name_max = fpathconf(dirfd(dirp), _PC_NAME_MAX); - if (name_max == -1) -#if defined(NAME_MAX) - name_max = (NAME_MAX > 255) ? NAME_MAX : 255; -#else - return (size_t)(-1); -#endif -#elif defined(NAME_MAX) - name_max = (NAME_MAX > 255) ? NAME_MAX : 255; -#else -#error "buffer size for readdir_r cannot be determined" -#endif - name_end = (size_t)offsetof(struct _tinydir_dirent, d_name) + name_max + 1; - return (name_end > sizeof(struct _tinydir_dirent) ? - name_end : sizeof(struct _tinydir_dirent)); -} -#endif -#endif - -#ifdef __cplusplus -} -#endif - -# if defined (_MSC_VER) -# pragma warning(pop) -# endif - -#endif