Skip to content

Commit

Permalink
fix cmake bugs and add complete examples
Browse files Browse the repository at this point in the history
  • Loading branch information
chitalu committed Jun 2, 2017
1 parent 09aa90d commit b35bad5
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 52 deletions.
11 changes: 7 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
Expand All @@ -36,6 +36,8 @@ elseif(UNIX)

endif()

MESSAGE(STATUS "${CPF_PREPROC_DEFS}")

add_library(${PROJECT_NAME}
${library_type}
"${cprintf_include_directory}/cprintf/cprintf.h"
Expand All @@ -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}")

Expand Down
2 changes: 0 additions & 2 deletions cprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 17 additions & 15 deletions cprintf/cprintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<unicode_string_t> attr_esc_seqs;

Expand Down Expand Up @@ -427,31 +425,31 @@ void write_variadic_argument_to_file_stream(file_stream_t file_stream,
}

template <>
void write_variadic_argument_to_file_stream<unicode_string_t>(
CPF_API void write_variadic_argument_to_file_stream<unicode_string_t>(
file_stream_t file_stream, unicode_string_t const &format,
unicode_string_t &&arg);

template <>
void write_variadic_argument_to_file_stream<ascii_string_t>(
CPF_API void write_variadic_argument_to_file_stream<ascii_string_t>(
file_stream_t file_stream, unicode_string_t const &format,
ascii_string_t &&arg);

template <>
void write_variadic_argument_to_file_stream<char *>(
CPF_API void write_variadic_argument_to_file_stream<char *>(
file_stream_t file_stream, unicode_string_t const &format, char *&&arg);

template <>
void write_variadic_argument_to_file_stream<signed char *>(
CPF_API void write_variadic_argument_to_file_stream<signed char *>(
file_stream_t file_stream, unicode_string_t const &format,
signed char *&&arg);

template <>
void write_variadic_argument_to_file_stream<const char *>(
CPF_API void write_variadic_argument_to_file_stream<const char *>(
file_stream_t file_stream, unicode_string_t const &format,
const char *&&arg);

template <>
void write_variadic_argument_to_file_stream<const signed char *>(
CPF_API void write_variadic_argument_to_file_stream<const signed char *>(
file_stream_t file_stream, unicode_string_t const &format,
const signed char *&&arg);

Expand Down Expand Up @@ -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<std::mutex> lock(_cprintf_::mutex_lock);

print_format_string_layout(
file_stream, format_string_layout_end_iterator,
format_string_layout_begin_iterator,
Expand All @@ -585,6 +581,12 @@ template <typename... Types> inline int cprintf(Types... arguments) {
return cprintf(stdout, std::forward<Types>(arguments)...);
}

template <typename FormatType, unsigned N, typename... Types>
inline int cprintf_s(FormatType(&format)[N], Types... arguments) {
static_assert(N >= 2, "invalid string-literal");
return cprintf(format, std::forward<Types>(arguments)...);
}

#if CPF_DBG_CONFIG

#include <cstdarg>
Expand Down Expand Up @@ -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" {

Expand Down
130 changes: 99 additions & 31 deletions cprintf_examples.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,124 @@

#include <cinttypes> // 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<std::uint32_t>::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<std::string> 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<std::string>::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;
}

0 comments on commit b35bad5

Please sign in to comment.