tools

various tools
git clone git://deadbeef.fr/tools.git
Log | Files | Refs | README | LICENSE

commit a237d993b5c97bface28534c1d08216bd580829f
parent f87f5765b28638fef7eef413628a7e0df9776919
Author: Morel BĂ©renger <berengermorel76@gmail.com>
Date:   Sat, 30 May 2020 04:59:56 +0200

allow providing parameters as command-line parameters

Diffstat:
Msrc/lmerge.cpp | 33+++++++++++++++++++--------------
Msrc/optparser.hpp | 15+++++++++++++++
2 files changed, 34 insertions(+), 14 deletions(-)

diff --git a/src/lmerge.cpp b/src/lmerge.cpp @@ -18,7 +18,6 @@ * * check that ENTRY_SEP works as expected; * * fix the fact input needs a "\\n" at end of last line for it to be merged; * * UTF-8 support (field separators); - * * providing FIELDS variable as command-line option; * * -v/--version option; * * allow to customize the memory allocation scheme at runtime; * * allow to not print twice merged fields; @@ -73,11 +72,26 @@ bool allocate_markers( void print_help( char const* pgm, FILE* target, opt_desc_t const* start, opt_desc_t const* end ); -int main( int argc, char const *const * argv ) +int main( int argc, char const *const *argv ) { + char const * SEP_START = getenv( "FIELD_SEP" ); + char const * SEP_ENTRY = getenv( "ENTRY_SEP" ); + char const * FIELDS = getenv( "FIELDS" ); + if( !SEP_START ) + { + SEP_START = " \t"; + } + if( !SEP_ENTRY ) + { + SEP_ENTRY = "\n"; + } + opt_desc_t opts[] = { { "help", "shows this message", 'h', 0, nullptr, nullptr, nullptr }, + { "field_sep", "field separator", 't' , 0, &SEP_START, set<char const**>, show<char*> }, + { "entry_sep", "entry separator", 'l' , 0, &SEP_ENTRY, set<char const**>, show<char*> }, + { "fields" , "fields to compare", 'f', 0, &FIELDS , set<char const**>, show<char*> }, }; auto b_opts = std::begin( opts ); auto e_opts = std::end( opts ); @@ -107,21 +121,12 @@ int main( int argc, char const *const * argv ) } } - char const * const DEFAULT_FIELD_SEP = " \t"; - char const * const DEFAULT_ENTRY_SEP = "\n"; - - char const * SEP_START = getenv( "FIELD_SEP" ); - if( !SEP_START ) - { - SEP_START = DEFAULT_FIELD_SEP; - } - char const * SEP_ENTRY = getenv( "ENTRY_SEP" ); - if( !SEP_ENTRY ) + if( opts[0].count ) { - SEP_ENTRY = DEFAULT_ENTRY_SEP; + print_help( argv[0], stdout, b_opts, e_opts ); + return EXIT_SUCCESS; } - char const * const FIELDS = getenv( "FIELDS" ); if( !FIELDS ) { fputs( "ERROR: FIELDS is not defined\n", stderr ); diff --git a/src/optparser.hpp b/src/optparser.hpp @@ -67,6 +67,21 @@ void print_opts( FILE* target, opt_desc_t const* start, opt_desc_t const* end ); #include <type_traits> #include <limits> +// C "semi-static" strings in pointer +template <typename T> +bool set( + typename std::enable_if <std::is_same<char const**,T>::value, void*>::type val, + char const* arg ) +{ + if( !val ) + { + return true; + } + + *static_cast<char const**>( val ) = arg; + return false; +} + // C strings in statically allocated buffer template <typename T, size_t SZ> bool set(