commit 5d4ab82bdb1c6e02a1b930dd4d5ea66c78179dfd
parent 0f4d26c460c139747d165b6b26fe6764a34f55b1
Author: Morel Bérenger <berengermorel76@gmail.com>
Date:   Fri, 26 Jun 2020 17:16:11 +0200
make optpaser.hpp less C-incompatible
* still needs actual testing
* adds some comments about the C++ helpers
* adds external linkage for the error message list
* adds C linkage for C-compatible symbols
* use a hack to avoid forcing stdio.h inclusion (requires #warning)
Diffstat:
3 files changed, 45 insertions(+), 6 deletions(-)
diff --git a/btl/src/optparser.cpp b/btl/src/optparser.cpp
@@ -101,7 +101,7 @@ parse_error_t parse_cmd_opt( char const* arg, opt_desc_t* start, opt_desc_t cons
 	return opt->count == UINT32_MAX ? MAX_COUNT : NONE;
 }
 
-void print_opts( FILE* target, opt_desc_t const* start, opt_desc_t const* end )
+void print_opts( FILE_PTR target, opt_desc_t const* start, opt_desc_t const* end )
 {
 	char short_name[] = ",-????";
 	for( ; start != end; ++start )
diff --git a/btl/src/optparser.hpp b/btl/src/optparser.hpp
@@ -1,6 +1,20 @@
 #ifndef OPTPARSER_HPP
 #define OPTPARSER_HPP
 
+#include <stdbool.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#else
+#define nullptr 0
+#endif
+
+#ifndef FILE_PTR
+#warning "FILE_PTR was not defined, defaulting to void*"
+#define FILE_PTR void*
+#endif
+
 // this could probably be optimized, but considering it should not be used that
 // often, I think it's better to just keep it as easy to use as possible.
 // Some doc:
@@ -37,7 +51,7 @@ struct opt_desc_t
 	uint32_t count;
 	void* value;
 	bool (*set) ( void* val, char const * const arg );
-	bool (*show)( void const* val, FILE* target );
+	bool (*show)( void const* val, FILE_PTR target );
 };
 
 //see parse_error_msgs in implementation for descriptions
@@ -66,7 +80,28 @@ extern char const *parse_error_msgs[];
 parse_error_t parse_cmd_opt( char const* arg, opt_desc_t* start, opt_desc_t const* end );
 
 // prints a message describing options and their current value if applicable.
-void print_opts( FILE* target, opt_desc_t const* start, opt_desc_t const* end );
+void print_opts( FILE_PTR target, opt_desc_t const* start, opt_desc_t const* end );
+
+#ifdef __cplusplus
+} //end of extern "C"
+
+// add some pretty generic and useful code for C++ users.
+// C users will have to implement their owns wrappers (and maybe contribute it?).
+//
+// This list is wished to grow with time, but should never depend on things that
+// have runtime cost (containers, exceptions, RTTI, etc).
+// It is hoped that it's performance will be improved when possible.
+
+// Depending on what you really use, you may have to define the following functions:
+// * strncpy   for static C string set()
+// * fputs     for C string show()
+// * strtoull  for unsigned integers show()
+// * strtoll   for signed integets show()
+// * strtold   for floating numbers show()
+// * fprintf   for all number related show()
+
+// stdio.h is *not* included, to fasten build time and to let user use
+// unlocked functions, which should be faster but not thread-safe.
 
 #include <type_traits>
 #include <limits>
@@ -112,7 +147,9 @@ bool show(
 		return true;
 	}
 
-	fprintf( target, "(current value: \"%s\")", static_cast<char const*>( val ) );
+	fputs( "(current value: \"", target );
+	fputs( static_cast<char const*>( val ), target );
+	fputs( "\")", target );
 	return false;
 }
 
@@ -218,3 +255,5 @@ bool  set(
 }
 
 #endif
+
+#endif
diff --git a/lmerge/Makefile b/lmerge/Makefile
@@ -8,10 +8,10 @@ all: manpages lmerge.1 lmerge
 	pandoc -s --to=man $< -o $@
 
 lmerge.o: src/lmerge.cpp
-	$(CXX) -I ../btl/src $(CXXFLAGS) -c $< -o $@
+	$(CXX) -I ../btl/src -DFILE_PTR='FILE*' $(CXXFLAGS) -c $< -o $@
 
 %.o: ../btl/src/%.cpp
-	$(CXX) $(CXXFLAGS) -c $< -o $@
+	$(CXX) $(CXXFLAGS)  -DFILE_PTR='FILE*' -c $< -o $@
 
 lmerge: lmerge.o optparser.o
 	$(CXX) -o $@ $^