From b35bad5bcc54d3d89a9e5d4a2092b7e7afd4d819 Mon Sep 17 00:00:00 2001 From: chitalu Date: Sat, 3 Jun 2017 00:02:40 +0100 Subject: [PATCH] fix cmake bugs and add complete examples --- CMakeLists.txt | 11 ++-- cprintf.cpp | 2 - cprintf/cprintf.h | 32 ++++++----- cprintf_examples.cpp | 130 ++++++++++++++++++++++++++++++++----------- 4 files changed, 123 insertions(+), 52 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ae6ea11..895bda3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,13 +2,13 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT (cprintf) -option (CPRINTF_BUILD_AS_SHARED_LIB "build as shared library" OFF) +option (CPRINTF_BUILD_AS_SHARED_LIB "build as shared library" ON) SET(cprintf_include_directory "${CMAKE_CURRENT_SOURCE_DIR}" CACHE STRING " ${PROJECT_NAME} include") if (CPRINTF_BUILD_AS_SHARED_LIB) - set (CPF_PREPROC_DEFS CPF_BUILD_AS_SHARED CPF_FUNC_EXPORT ) - set(library_type SHARED) + list (APPEND CPF_PREPROC_DEFS CPF_BUILD_AS_SHARED CPF_FUNC_EXPORT ) + set (library_type SHARED) else() set(library_type STATIC) endif() @@ -21,7 +21,7 @@ set (CPF_FLAGS "") if(WIN32) set (CPF_BUILD_FLAGS "${CPF_BUILD_FLAGS} /WX /EHsc") - LIST (APPEND CPF_PREPROC_DEFS VC_EXTRALEAN WIN32_LEAN_AND_MEAN _CRT_SECURE_NO_WARNINGS) + list (APPEND CPF_PREPROC_DEFS VC_EXTRALEAN WIN32_LEAN_AND_MEAN _CRT_SECURE_NO_WARNINGS) elseif(UNIX) set (CPF_BUILD_FLAGS "${CPF_BUILD_FLAGS} -std=c++11 -Werror") @@ -36,6 +36,8 @@ elseif(UNIX) endif() +MESSAGE(STATUS "${CPF_PREPROC_DEFS}") + add_library(${PROJECT_NAME} ${library_type} "${cprintf_include_directory}/cprintf/cprintf.h" @@ -53,6 +55,7 @@ add_executable(${EXAMPLES_PROJECT_NAME} "${CMAKE_CURRENT_SOURCE_DIR}/${PROJECT_N target_link_libraries( ${EXAMPLES_PROJECT_NAME} ${PROJECT_NAME} ) set_target_properties( ${EXAMPLES_PROJECT_NAME} PROPERTIES + COMPILE_DEFINITIONS "${CPF_PREPROC_DEFS}" COMPILE_FLAGS "${CPF_BUILD_FLAGS}" INCLUDE_DIRECTORIES "${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/cprintf.cpp b/cprintf.cpp index 2591e22..f99e22a 100644 --- a/cprintf.cpp +++ b/cprintf.cpp @@ -849,8 +849,6 @@ CPF_API const unicode_string_t debugging_log_format_string = #endif // CPF_DBG_CONFIG -std::mutex mutex_lock; - // cprintf("Characters:\t%c %%\n", 65); int get_num_format_specifiers_in_string(const unicode_string_t &obj) { int n = 0; diff --git a/cprintf/cprintf.h b/cprintf/cprintf.h index 6a257c9..907256e 100644 --- a/cprintf/cprintf.h +++ b/cprintf/cprintf.h @@ -355,8 +355,6 @@ CPF_API void configure(file_stream_t ustream, const attribute_group_t &attr); CPF_API const unicode_string_t debugging_log_format_string; -CPF_API std::mutex mutex_lock; - // text attribute token escape sequences.. CPF_API const std::initializer_list attr_esc_seqs; @@ -427,31 +425,31 @@ void write_variadic_argument_to_file_stream(file_stream_t file_stream, } template <> -void write_variadic_argument_to_file_stream( +CPF_API void write_variadic_argument_to_file_stream( file_stream_t file_stream, unicode_string_t const &format, unicode_string_t &&arg); template <> -void write_variadic_argument_to_file_stream( +CPF_API void write_variadic_argument_to_file_stream( file_stream_t file_stream, unicode_string_t const &format, ascii_string_t &&arg); template <> -void write_variadic_argument_to_file_stream( +CPF_API void write_variadic_argument_to_file_stream( file_stream_t file_stream, unicode_string_t const &format, char *&&arg); template <> -void write_variadic_argument_to_file_stream( +CPF_API void write_variadic_argument_to_file_stream( file_stream_t file_stream, unicode_string_t const &format, signed char *&&arg); template <> -void write_variadic_argument_to_file_stream( +CPF_API void write_variadic_argument_to_file_stream( file_stream_t file_stream, unicode_string_t const &format, const char *&&arg); template <> -void write_variadic_argument_to_file_stream( +CPF_API void write_variadic_argument_to_file_stream( file_stream_t file_stream, unicode_string_t const &format, const signed char *&&arg); @@ -559,8 +557,6 @@ int dispatch(file_stream_t file_stream, FormatType &&raw_format, format_string_layout_const_iterator_t format_string_layout_end_iterator = format_string_layout.cend(); - std::lock_guard lock(_cprintf_::mutex_lock); - print_format_string_layout( file_stream, format_string_layout_end_iterator, format_string_layout_begin_iterator, @@ -585,6 +581,12 @@ template inline int cprintf(Types... arguments) { return cprintf(stdout, std::forward(arguments)...); } +template +inline int cprintf_s(FormatType(&format)[N], Types... arguments) { + static_assert(N >= 2, "invalid string-literal"); + return cprintf(format, std::forward(arguments)...); +} + #if CPF_DBG_CONFIG #include @@ -626,11 +628,11 @@ typedef const wchar_t* c_wchar_p; int cprintf_##_formattype##_##_argtype(int stream, _formattype format, _argtype arg) #define __CPRINTF_capi_Sig1(_formattype)\ -__CPRINTF_capi_Sig0(_formattype, c_int32);\ -__CPRINTF_capi_Sig0(_formattype, c_long);\ -__CPRINTF_capi_Sig0(_formattype, c_float);\ -__CPRINTF_capi_Sig0(_formattype, c_char_p);\ -__CPRINTF_capi_Sig0(_formattype, c_wchar_p); +CPF_API __CPRINTF_capi_Sig0(_formattype, c_int32);\ +CPF_API __CPRINTF_capi_Sig0(_formattype, c_long);\ +CPF_API __CPRINTF_capi_Sig0(_formattype, c_float);\ +CPF_API __CPRINTF_capi_Sig0(_formattype, c_char_p);\ +CPF_API __CPRINTF_capi_Sig0(_formattype, c_wchar_p); extern "C" { diff --git a/cprintf_examples.cpp b/cprintf_examples.cpp index 4f111c7..5a43c6e 100644 --- a/cprintf_examples.cpp +++ b/cprintf_examples.cpp @@ -2,56 +2,124 @@ #include // PRIu32 -int main() +int main(void) { - cprintf_c_char_p_c_int32(1, "hello world %d\n", 1); - - cprintf("Characters: $R%c %c \n", 'a', 65); - - cprintf("$GDecimals$?: %d %ld\n", 1977, 650000L); - + cprintf(stdout, "Characters: $R%c %c \n", 'a', 65); + cprintf(stderr, "$GDecimals$?: %d %ld\n", 1977, 650000L); cprintf("$BPreceding with blanks: %10d \n", 1977); - - cprintf("Preceding with zeros: $Y%010d \n", 1977); - + cprintf(L"Preceding with zeros: $Y%010d \n", 1977); cprintf("Some different radices: %d %x %o $M%#x$W %#o \n", 100, 100, 100, 100, 100); - cprintf("floats: %4.2f $C%+.0e$? %E \n", 3.1416, 3.1416, 3.1416); - cprintf("$W%s \n", "A string"); - cprintf("$r%-5s\n", "ab"); - cprintf("String: $cR%s\n", "my string arg"); - cprintf("Str$Mi$?ngs:\n"); - const char *s = "Hello"; cprintf("Char$cacters:\t%c %%\n", 65); cprintf("\t$Y.%10s$?.\n\t.%-10s.", s, s); - cprintf("$YIntegers\n"); cprintf("Decimal:\t$R%i $G%d $B%.6i $C%i $Y%.0i $M%+i $W%u\n", 1, 2, 3, 0, 0, 4, -1); cprintf("Hexadecimal:\t$R#%x $G#%x $B#%X $C#%#x\n", 5, 10, 10, 6); cprintf("$b#Octal:\t%o %#o %#o\n", 10, 10, 4); - - cprintf("Floating point\n"); - cprintf("Rounding:\t%f %.0f %.32f\n", 1.5, 1.5, 1.3); - cprintf("Padding:\t%05.2f %.2f %5.2f\n", 1.5, 1.5, 1.5); - cprintf("Scientific:\t%E %e\n", 1.5, 1.5); - cprintf("Hexadecimal:\t%a %A\n", 1.5, 1.5); - + cprintf("$cFloating point\n"); + cprintf("Ro$yunding:\t%f $M%.0f $W%.32f\n", 1.5, 1.5, 1.3); + cprintf("$WPadding:\t%05.2f %.2f %5.2f\n", 1.5, 1.5, 1.5); + cprintf("Scientific:$R\t%E $W%e\n", 1.5, 1.5); + cprintf("$BHexadecimal:\t%a %A\n", 1.5, 1.5); +#ifdef max #undef max +#endif std::uint32_t val = std::numeric_limits::max(); - cprintf("Largest 32-bit value is %" PRIu32 " or %#" PRIx32 "\n", val, val); - - /* - The following are not supported: - + cprintf("Largest 32-bit value is $Y%" PRIu32 "$? or $C%#" PRIx32 "\n", val, val); +#if 0 +#error "Unsupported argument combinations" cprintf("\t.%10s.\n\t.%-10s.\n\t.%*s.\n", s, s, 10, s); cprintf("Special values:\t0/0=%g 1/0=%g\n", 0. / 0, 1. / 0); cprintf("right-justified variable width: '%*c'\n", 5, 'x'); - */ - +#endif + cprintf("std::string: %s\n", std::string("hello")); + cprintf("std::string: %S\n", std::wstring(L"hello")); + cprintf_s("string literal API\n"); +#if 0 +#error "Unsupported argument combinations" + cprintf_s(s); +#endif + cprintf("bar $rw`Red on white\n"); + cprintf("bar $r#ed\n"); + cprintf("bar $r#ed\n"); + cprintf("bar $r`#ed\n"); + cprintf("quux$r.b blue\n"); + cprintf("quux$B.r red\n"); + cprintf("quux$B.R red\n"); + cprintf("quux$r.B blue\n"); +#ifdef CPF_WINDOWS_BUILD + cprintf("note that $bld this text is blue\n"); +#else + // bitmap colour tokens available on XTERM (0 - 255) + cprintf("quux $128f xterm bitmap colour foreground\n"); + cprintf("quux $24b xterm bitmap colour background\n"); + cprintf("quux $128f.94b xterm bitmap colour foreground & background\n"); + cprintf("quux $128f$64b xterm bitmap colour foreground & background\n"); + // applies bitmap colour to both foreground and background + cprintf("quux $64& xterm bitmap colour block\n"); + cprintf("note that $bld this text is bold\n"); + cprintf("note that $b`ld this text is blue\n"); + // reverting text format token effects mid format string + cprintf("drive forward and $rvs reverse $?rvsand the back to normal\n"); +#endif + std::vector xplat_tokens = { + "r", "g", "b", "y", "m", "c", "w", + "r#", "g#", "b#", "y#", "m#", "c#", "w#", + "R", "G", "B", "Y", "M", "C", "W", + "R#", "G#", "B#", "Y#", "M#", "C#", "W#", + "rr", "rb", "rg", "ry", "rm", "rc", "rw", + "gg", "gb", "gr", "gy", "gm", "gc", "gw", + "bb", "br", "bg", "by", "bm", "bc", "bw", + "yy", "yb", "yg", "yr", "ym", "yc", "yw", + "mm", "mr", "mg", "my", "mb", "mc", "mw", + "cc", "cr", "cg", "cy", "cm", "cb", "cw", + "ww", "wr", "wg", "wy", "wm", "wc", "wb", + "Rr", "Rb", "Rg", "Ry", "Rm", "Rc", "Rw", + "Gg", "Gb", "Gr", "Gy", "Gm", "Gc", "Gw", + "Bb", "Br", "Bg", "By", "Bm", "Bc", "Bw", + "Yy", "Yb", "Yg", "Yr", "Ym", "Yc", "Yw", + "Mm", "Mr", "Mg", "My", "Mb", "Mc", "Mw", + "Cc", "Cr", "Cg", "Cy", "Cm", "Cb", "Cw", + "Ww", "Wr", "Wg", "Wy", "Wm", "Wc", "Wb", + "rR", "rB", "rG", "rY", "rM", "rC", "rW", + "gG", "gB", "gR", "gY", "gM", "gC", "gW", + "bB", "bR", "bG", "bY", "bM", "bC", "bW", + "yY", "yB", "yG", "yR", "yM", "yC", "yW", + "mM", "mR", "mG", "mY", "mB", "mC", "mW", + "cC", "cR", "cG", "cY", "cM", "cB", "cW", + "wW", "wR", "wG", "wY", "wM", "wC", "wB", + "RR", "RB", "RG", "RY", "RM", "RC", "RW", + "GG", "GB", "GR", "GY", "GM", "GC", "GW", + "BB", "BR", "BG", "BY", "BM", "BC", "BW", + "YY", "YB", "YG", "YR", "YM", "YC", "YW", + "MM", "MR", "MG", "MY", "MB", "MC", "MW", + "CC", "CR", "CG", "CY", "CM", "CB", "CW", + "WW", "WR", "WG", "WY", "WM", "WC", "WB" + }; + + for (std::vector::const_iterator tokenIter = xplat_tokens.cbegin(); + tokenIter != xplat_tokens.cend(); + ++tokenIter) { + + const std::string format = ("$" + (*tokenIter) + "%s" + "$?\t"); + + cprintf(format, (*tokenIter)); + + std::size_t i = std::distance(xplat_tokens.cbegin(), tokenIter) + 1; + + if ((i % 7) == 0) + { + cprintf("%s", i == xplat_tokens.size() -1 ? "\n\n" : "\n"); + } + } + + cprintf("`$? `$r `$g `$b `$y `$m `$c `$w `$rg `$gB `$By `$YM `$m ...\n"); + cprintf("$? $r`r $g`g $b`b $y`y $m`m $c`c $w`w $rg`rg $gB`gB $By`By $YM`YM $m`m ...\n"); + return 0; } \ No newline at end of file