Skip to content

Commit

Permalink
?cxx/Macros.hxx: +SUSUWU_SH_ESC, +SUSUWU_SH_CSI
Browse files Browse the repository at this point in the history
	, +`SUSUWU_SH_BEL`, +`SUSUWU_SH_ST`, +`SUSUWU_SH_OSC`.
	`%s/"\\033\[/SUSUWU_SH_CSI "/` /* Replace magic constants with macros which refer to conventions */
	+`SUSUWU_SH_RUNTIME_COLORS`
	+`SUSUWU_PRAGMA`
	`%s/SUSUWU_USE_STDERR/SUSUWU_SH_STDERR/` /* Use _SH_ for all macro constants which have to do with `bash`/`ash`/`sh` */

?`posts/VirusAnalysis.md`: Include all of above which is used

?`README.md`:
	+How to use `SUSUWU_SH_RUNTIME_COLORS`
	`%s/SUSUWU_USE_STDERR/SUSUWU_SH_STDERR/`

Has to do with #17
  • Loading branch information
SwuduSusuwu committed Oct 29, 2024
1 parent 73ce589 commit c3dd29a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 44 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Targets: Windows/Linux/Android/OSX/iOS; all C++ compilers, requires some extensi

Usage: `./build.sh` produces `*.o` static libraries (for distribution to others,) plus `a.out` to do unit tests (test harnesses). Allowed flags; `--debug` (default; includes frame-pointers/debug symbols (`-g`), includes `valgrind`-replacement tools (such as `-fsanitize=address`), `--release` (excludes `--debug`, strips frame-pointers/symbols, optimizes with `-O2`), `--mingw` (if on _Linux_, can use with `--release` or `--debug`, produces _Portable Executable_ for _Windows_. If on _Windows_, the default is to produce _Portable Executable_'s for _Windows_)
Optional flags (`vim build.sh` to use):
`-DSUSUWU_USE_STDERR` to replace `std::cerr` with `fprintf(stderr, ...)`, default is `!defined(__cplusplus)`.
`-DSUSUWU_SH_RUNTIME_COLORS` to replace `#if ... #else ... #endif` with operating system calls to choose whether or not to use colors, default is undefined.
`-DSUSUWU_SH_STDERR` to replace `std::cerr` with `fprintf(stderr, ...)`, default is `!defined(__cplusplus)`.
`-DSUSUWU_SH_SKIP_BRACKETS = true` sets output format to `WARN_LEVEL: message`, default is `false`.
`-DSUSUWU_SH_FILE = true` sets output format to `[__FILE__: WARN_LEVEL: message]`, default is `!defined(NDEBUG)`.
`-DSUSUWU_SH_LINE = true` sets output format to `[__LINE__: WARN_LEVEL: message]`, default is `!defined(NDEBUG)`.
Expand Down
61 changes: 37 additions & 24 deletions cxx/Macros.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
#define SUSUWU_GLUE2(S, U) S##U /* concatanates 2 macro constants */
#define SUSUWU_GLUE(S, U) SUSUWU_GLUE2(S, U) /* concatanates 2 macro functions or constants */
#define SUSUWU_COMMA , /* to pass to macro functions whose `__VA_ARGS__` is conditional */
#if (!defined _POSIX_VERSION) && (defined _POSIX_C_SOURCE)
# define _POSIX_VERSION _POSIX_C_SOURCE /* "Error: ... ndef _POSIX_VERSION" fix. Now, you can just do `#ifdef _POSIX_VERSION` for POSIX code paths */
#endif /* (!defined _POSIX_VERSION) && (defined _POSIX_C_SOURCE) */

/* `#pragma S` in macro functions is `_Pragma(S)`, but without this wrap gives `error: _Pragma takes a parenthesized string literal`/`expected string literal in pragma message`. Use as `SUSUWU_PRAGMA(message("Message"))` */
#define SUSUWU_PRAGMA(S) _Pragma(#S)

#if !defined(SUSUWU_SH_SKIP_BRACKETS) || SUSUWU_SH_SKIP_BRACKETS == false /* overridable with `-DSUSUWU_SH_SKIP_BRACKETS true` (which you can set to mimic `g++`/`clang++` syntax for outputs) */
# define IF_SUSUWU_SH_BRACKETS(TRUE, FALSE) TRUE
Expand Down Expand Up @@ -55,10 +61,20 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
# define IF_SUSUWU_SH_FILE_LINE_OR_FUNC(U) /* don't printout */
#endif

#define SUSUWU_SH_COLORS_UNSUPPORTED __MINGW32__ /* MinGW + WINE shows the literal ANSI color codes in console */
#if SUSUWU_SH_COLORS_UNSUPPORTED && !defined SUSUWU_SH_SKIP_COLORS
#define SUSUWU_SH_RUNTIME_COLORS
#ifdef SUSUWU_SH_RUNTIME_COLORS
# pragma message("TODO: respond to https://github.com/SwuduSusuwu/SubStack/issues/17 if you want this soon")
#elif !defined(_POSIX_VERSION)
# define SUSUWU_SH_COLORS_UNSUPPORTED /* assume "dumb terminals" (such as __WIN32__ or __MINGW32__ often has) if built without runtime tests for color attributes on non-POSIX systems */
#endif
#if defined(SUSUWU_SH_COLORS_UNSUPPORTED) && !defined(SUSUWU_SH_SKIP_COLORS)
# define SUSUWU_SH_SKIP_COLORS true /* you can use `-DSUSUWU_SH_SKIP_COLORS=false` to force unsupported color use (such as if _MinGW_ will target _Windows_ but the executables aren't for use on _WINE_/`localhost`) */
#endif /* SUSUWU_SH_COLORS_UNSUPPORTED && !defined SUSUWU_SH_SKIP_COLORS */
# define SUSUWU_SH_BEL "\07" /* Bell sound */
# define SUSUWU_SH_ESC "\033" /* Escape */
# define SUSUWU_SH_OSC SUSUWU_SH_ESC "]" /* Operating System Command */
# define SUSUWU_SH_ST SUSUWU_SH_ESC "\\" /* String Terminator of commands */ /* `BEL` reduces this to 1 byte but just functions as `ST` on `xterm` */
# define SUSUWU_SH_CSI SUSUWU_SH_ESC "[" /* Control Sequence Introducer */
#if SUSUWU_SH_SKIP_COLORS /* `-DSUSUWU_SH_SKIP_COLORS=true` to turn colors off */
# define SUSUWU_SH_DEFAULT ""
# define SUSUWU_SH_BLACK ""
Expand All @@ -78,23 +94,23 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
# define SUSUWU_SH_LIGHT_GRAY ""
# define SUSUWU_SH_WHITE ""
#else /* !SUSUWU_SH_SKIP_COLORS */
# define SUSUWU_SH_DEFAULT "\033[0m"
# define SUSUWU_SH_BLACK "\033[0;30m"
# define SUSUWU_SH_DARK_GRAY "\033[1;30m"
# define SUSUWU_SH_RED "\033[0;31m"
# define SUSUWU_SH_LIGHT_RED "\033[1;31m"
# define SUSUWU_SH_GREEN "\033[0;32m"
# define SUSUWU_SH_LIGHT_GREEN "\033[1;32m"
# define SUSUWU_SH_BROWN "\033[0;33m"
# define SUSUWU_SH_YELLOW "\033[1;33m"
# define SUSUWU_SH_BLUE "\033[0;34m"
# define SUSUWU_SH_LIGHT_BLUE "\033[1;34m"
# define SUSUWU_SH_PURPLE "\033[0;35m"
# define SUSUWU_SH_LIGHT_PURPLE "\033[1;35m"
# define SUSUWU_SH_CYAN "\033[0;36m"
# define SUSUWU_SH_LIGHT_CYAN "\033[1;36m"
# define SUSUWU_SH_LIGHT_GRAY "\033[0;37m"
# define SUSUWU_SH_WHITE "\033[1;37m"
# define SUSUWU_SH_DEFAULT SUSUWU_SH_CSI "0m"
# define SUSUWU_SH_BLACK SUSUWU_SH_CSI "0;30m"
# define SUSUWU_SH_DARK_GRAY SUSUWU_SH_CSI "1;30m"
# define SUSUWU_SH_RED SUSUWU_SH_CSI "0;31m"
# define SUSUWU_SH_LIGHT_RED SUSUWU_SH_CSI "1;31m"
# define SUSUWU_SH_GREEN SUSUWU_SH_CSI "0;32m"
# define SUSUWU_SH_LIGHT_GREEN SUSUWU_SH_CSI "1;32m"
# define SUSUWU_SH_BROWN SUSUWU_SH_CSI "0;33m"
# define SUSUWU_SH_YELLOW SUSUWU_SH_CSI "1;33m"
# define SUSUWU_SH_BLUE SUSUWU_SH_CSI "0;34m"
# define SUSUWU_SH_LIGHT_BLUE SUSUWU_SH_CSI "1;34m"
# define SUSUWU_SH_PURPLE SUSUWU_SH_CSI "0;35m"
# define SUSUWU_SH_LIGHT_PURPLE SUSUWU_SH_CSI "1;35m"
# define SUSUWU_SH_CYAN SUSUWU_SH_CSI "0;36m"
# define SUSUWU_SH_LIGHT_CYAN SUSUWU_SH_CSI "1;36m"
# define SUSUWU_SH_LIGHT_GRAY SUSUWU_SH_CSI "0;37m"
# define SUSUWU_SH_WHITE SUSUWU_SH_CSI "1;37m"
#endif /* !SUSUWU_SH_SKIP_COLORS */
#define SUSUWU_SH_FILE __FILE__ ":"
#define SUSUWU_SH_PREFIX IF_SUSUWU_SH_BRACKETS("[", "") SUSUWU_SH_WHITE
Expand All @@ -119,9 +135,9 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
# define IF_SUSUWU_CPLUSPLUS(TRUE, FALSE) TRUE
#else /* !(defined __cplusplus */
# define IF_SUSUWU_CPLUSPLUS(TRUE, FALSE) FALSE
# define SUSUWU_USE_STDERR
# define SUSUWU_SH_STDERR /* `-DSUSUWU_SH_STDERR` to force this. Replaces `std::cerr << x << std::endl;` with `fprintf(stderr, "%s\n", x);` */
#endif /* !(defined __cplusplus */
#ifdef SUSUWU_USE_STDERR
#ifdef SUSUWU_SH_STDERR
# define SUSUWU_PRINT(LEVEL, x) SUSUWU_STDERR(LEVEL, x)
#else
# define SUSUWU_PRINT(LEVEL, x) SUSUWU_CERR(LEVEL, x)
Expand Down Expand Up @@ -155,9 +171,6 @@ namespace Susuwu { /* namespaces do not affect macros. Is just standard practice
#ifndef __has_feature
#define __has_feature(X) false /* `gcc` "error: missing binary operator before token \"(\"" fix */
#endif /* ndef __has_feature */
#if (!defined _POSIX_VERSION) && (_POSIX_C_SOURCE)
#define _POSIX_VERSION _POSIX_C_SOURCE /* "Error: ... ndef _POSIX_VERSION" fix */
#endif /* (!defined _POSIX_VERSION) && (_POSIX_C_SOURCE) */

#if (!defined __WIN32__) && (defined _WIN32 /* || defined __CYGWIN__ should use "#ifdef _POSIX_VERSION" path */ || __MSC_VER)
# define __WIN32__ /* https://stackoverflow.com/questions/430424/are-there-any-macros-to-determine-if-my-code-is-being-compiled-to-windows/430435#430435 says that __WIN32__ is not always defined on Windows targets */
Expand Down
43 changes: 24 additions & 19 deletions posts/VirusAnalysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ For the most new sources (+ static libs), use apps such as [iSH](https://apps.ap
#define SUSUWU_GLUE2(S, U) S##U /* concatanates 2 macro constants */
#define SUSUWU_GLUE(S, U) SUSUWU_GLUE2(S, U) /* concatanates 2 macro functions or constants */
#define SUSUWU_COMMA , /* to pass to macro functions whose `__VA_ARGS__` is conditional */
#if (!defined _POSIX_VERSION) && (defined _POSIX_C_SOURCE)
# define _POSIX_VERSION _POSIX_C_SOURCE /* "Error: ... ndef _POSIX_VERSION" fix. Now, you can just do `#ifdef _POSIX_VERSION` for POSIX code paths */
#endif /* (!defined _POSIX_VERSION) && (defined _POSIX_C_SOURCE) */
#if !defined(SUSUWU_SH_SKIP_BRACKETS) || SUSUWU_SH_SKIP_BRACKETS == false /* overridable with `-DSUSUWU_SH_SKIP_BRACKETS true` (which you can set to mimic `g++`/`clang++` syntax for outputs) */
# define IF_SUSUWU_SH_BRACKETS(TRUE, FALSE) TRUE
Expand Down Expand Up @@ -39,23 +42,25 @@ For the most new sources (+ static libs), use apps such as [iSH](https://apps.ap
# define IF_SUSUWU_SH_FILE_LINE_OR_FUNC(U) /* don't printout */
#endif
#define SUSUWU_SH_DEFAULT "\033[0m"
#define SUSUWU_SH_BLACK "\033[0;30m"
#define SUSUWU_SH_DARK_GRAY "\033[1;30m"
#define SUSUWU_SH_RED "\033[0;31m"
#define SUSUWU_SH_LIGHT_RED "\033[1;31m"
#define SUSUWU_SH_GREEN "\033[0;32m"
#define SUSUWU_SH_LIGHT_GREEN "\033[1;32m"
#define SUSUWU_SH_BROWN "\033[0;33m"
#define SUSUWU_SH_YELLOW "\033[1;33m"
#define SUSUWU_SH_BLUE "\033[0;34m"
#define SUSUWU_SH_LIGHT_BLUE "\033[1;34m"
#define SUSUWU_SH_PURPLE "\033[0;35m"
#define SUSUWU_SH_LIGHT_PURPLE "\033[1;35m"
#define SUSUWU_SH_CYAN "\033[0;36m"
#define SUSUWU_SH_LIGHT_CYAN "\033[1;36m"
#define SUSUWU_SH_LIGHT_GRAY "\033[0;37m"
#define SUSUWU_SH_WHITE "\033[1;37m"
#define SUSUWU_SH_ESC "\033" /* Escape */
#define SUSUWU_SH_CSI SUSUWU_SH_ESC "[" /* Control Sequence Introducer */
#define SUSUWU_SH_DEFAULT SUSUWU_SH_CSI "0m"
#define SUSUWU_SH_BLACK SUSUWU_SH_CSI "0;30m"
#define SUSUWU_SH_DARK_GRAY SUSUWU_SH_CSI "1;30m"
#define SUSUWU_SH_RED SUSUWU_SH_CSI "0;31m"
#define SUSUWU_SH_LIGHT_RED SUSUWU_SH_CSI "1;31m"
#define SUSUWU_SH_GREEN SUSUWU_SH_CSI "0;32m"
#define SUSUWU_SH_LIGHT_GREEN SUSUWU_SH_CSI "1;32m"
#define SUSUWU_SH_BROWN SUSUWU_SH_CSI "0;33m"
#define SUSUWU_SH_YELLOW SUSUWU_SH_CSI "1;33m"
#define SUSUWU_SH_BLUE SUSUWU_SH_CSI "0;34m"
#define SUSUWU_SH_LIGHT_BLUE SUSUWU_SH_CSI "1;34m"
#define SUSUWU_SH_PURPLE SUSUWU_SH_CSI "0;35m"
#define SUSUWU_SH_LIGHT_PURPLE SUSUWU_SH_CSI "1;35m"
#define SUSUWU_SH_CYAN SUSUWU_SH_CSI "0;36m"
#define SUSUWU_SH_LIGHT_CYAN SUSUWU_SH_CSI "1;36m"
#define SUSUWU_SH_LIGHT_GRAY SUSUWU_SH_CSI "0;37m"
#define SUSUWU_SH_WHITE SUSUWU_SH_CSI "1;37m"
#define SUSUWU_SH_FILE __FILE__ ":"
#define SUSUWU_SH_PREFIX IF_SUSUWU_SH_BRACKETS("[", "") SUSUWU_SH_WHITE
#define SUSUWU_SH_ERROR SUSUWU_SH_RED "Error: " SUSUWU_SH_WHITE
Expand All @@ -79,9 +84,9 @@ For the most new sources (+ static libs), use apps such as [iSH](https://apps.ap
# define IF_SUSUWU_CPLUSPLUS(TRUE, FALSE) TRUE
#else /* !(defined __cplusplus */
# define IF_SUSUWU_CPLUSPLUS(TRUE, FALSE) FALSE
# define SUSUWU_USE_STDERR
# define SUSUWU_SH_STDERR /* `-DSUSUWU_SH_STDERR` to force this. Replaces `std::cerr << x << std::endl;` with `fprintf(stderr, "%s\n", x);` */
#endif /* !(defined __cplusplus */
#ifdef SUSUWU_USE_STDERR
#ifdef SUSUWU_SH_STDERR
# define SUSUWU_PRINT(LEVEL, x) SUSUWU_STDERR(LEVEL, x)
#else
# define SUSUWU_PRINT(LEVEL, x) SUSUWU_CERR(LEVEL, x)
Expand Down

0 comments on commit c3dd29a

Please sign in to comment.