From: Kevin Day Date: Mon, 17 Jan 2022 03:01:44 +0000 (-0600) Subject: Update: Rewrite f_color functions and use f_string_static_t instead of f_string_t... X-Git-Tag: 0.5.8~101 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=93f66f140f21bea6d8fb617bd44ef3017ca53955;p=fll Update: Rewrite f_color functions and use f_string_static_t instead of f_string_t for global constant strings. Switch to using the f_string_static_t so that the size does not have to be constantly tested via an strnlen() or similar call. The f_string_static_t by default uses 64-bit types for size and used so there may be a performance hit due to this over the normal 32-bit numbers in strnlen(). Originally macros were used to reduce code repition. This was done long before I decided to make f_string an exception that can be depended on by all level 0 projects. The dynamic string functions can now be used, significantly simplifying the code. The macros can be replaced with functions 1 through 5 to allow for only passing the required arguments. This should reduce the cost of the function call by having fewer arguments when not necessary to have them. The documentation comments are now present when they were not before. Change the order of the function parameters to be more consistent with the latest practices. Update the unit tests. --- diff --git a/level_0/f_color/c/color-common.c b/level_0/f_color/c/color-common.c index e461660..9f65c95 100644 --- a/level_0/f_color/c/color-common.c +++ b/level_0/f_color/c/color-common.c @@ -5,48 +5,48 @@ extern "C" { #endif #ifndef _di_f_color_strings_s_ - const f_string_t f_color_string_begin_s = F_color_string_begin_s; - const f_string_t f_color_string_end_s = F_color_string_end_s; - const f_string_t f_color_string_medium_s = F_color_string_medium_s; + const f_string_static_t f_color_string_begin_s = macro_f_string_static_t_initialize2(F_color_string_begin_s, 0, F_color_string_begin_s_length); + const f_string_static_t f_color_string_end_s = macro_f_string_static_t_initialize2(F_color_string_end_s, 0, F_color_string_end_s_length); + const f_string_static_t f_color_string_medium_s = macro_f_string_static_t_initialize2(F_color_string_medium_s, 0, F_color_string_medium_s_length); - const f_string_t f_color_string_code_reset_s = F_color_string_code_reset_s; - const f_string_t f_color_string_code_bold_s = F_color_string_code_bold_s; - const f_string_t f_color_string_code_underline_s = F_color_string_code_underline_s; - const f_string_t f_color_string_code_blink_s = F_color_string_code_blink_s; - const f_string_t f_color_string_code_reverse_s = F_color_string_code_reverse_s; - const f_string_t f_color_string_code_conceal_s = F_color_string_code_conceal_s; - const f_string_t f_color_string_code_black_s = F_color_string_code_black_s; - const f_string_t f_color_string_code_red_s = F_color_string_code_red_s; - const f_string_t f_color_string_code_green_s = F_color_string_code_green_s; - const f_string_t f_color_string_code_yellow_s = F_color_string_code_yellow_s; - const f_string_t f_color_string_code_blue_s = F_color_string_code_blue_s; - const f_string_t f_color_string_code_purple_s = F_color_string_code_purple_s; - const f_string_t f_color_string_code_teal_s = F_color_string_code_teal_s; - const f_string_t f_color_string_code_white_s = F_color_string_code_white_s; - const f_string_t f_color_string_code_black_bg_s = F_color_string_code_black_bg_s; - const f_string_t f_color_string_code_red_bg_s = F_color_string_code_red_bg_s; - const f_string_t f_color_string_code_green_bg_s = F_color_string_code_green_bg_s; - const f_string_t f_color_string_code_yellow_bg_s = F_color_string_code_yellow_bg_s; - const f_string_t f_color_string_code_blue_bg_s = F_color_string_code_blue_bg_s; - const f_string_t f_color_string_code_purple_bg_s = F_color_string_code_purple_bg_s; - const f_string_t f_color_string_code_teal_bg_s = F_color_string_code_teal_bg_s; - const f_string_t f_color_string_code_white_bg_s = F_color_string_code_white_bg_s; - const f_string_t f_color_string_code_bright_black_s = F_color_string_code_bright_black_s; - const f_string_t f_color_string_code_bright_red_s = F_color_string_code_bright_red_s; - const f_string_t f_color_string_code_bright_green_s = F_color_string_code_bright_green_s; - const f_string_t f_color_string_code_bright_yellow_s = F_color_string_code_bright_yellow_s; - const f_string_t f_color_string_code_bright_blue_s = F_color_string_code_bright_blue_s; - const f_string_t f_color_string_code_bright_purple_s = F_color_string_code_bright_purple_s; - const f_string_t f_color_string_code_bright_teal_s = F_color_string_code_bright_teal_s; - const f_string_t f_color_string_code_bright_white_s = F_color_string_code_bright_white_s; - const f_string_t f_color_string_code_bright_black_bg_s = F_color_string_code_bright_black_bg_s; - const f_string_t f_color_string_code_bright_red_bg_s = F_color_string_code_bright_red_bg_s; - const f_string_t f_color_string_code_bright_green_bg_s = F_color_string_code_bright_green_bg_s; - const f_string_t f_color_string_code_bright_yellow_bg_s = F_color_string_code_bright_yellow_bg_s; - const f_string_t f_color_string_code_bright_blue_bg_s = F_color_string_code_bright_blue_bg_s; - const f_string_t f_color_string_code_bright_purple_bg_s = F_color_string_code_bright_purple_bg_s; - const f_string_t f_color_string_code_bright_teal_bg_s = F_color_string_code_bright_teal_bg_s; - const f_string_t f_color_string_code_bright_white_bg_s = F_color_string_code_bright_white_bg_s; + const f_string_static_t f_color_string_code_reset_s = macro_f_string_static_t_initialize2(F_color_string_code_reset_s, 0, F_color_string_code_reset_s_length); + const f_string_static_t f_color_string_code_bold_s = macro_f_string_static_t_initialize2(F_color_string_code_bold_s, 0, F_color_string_code_bold_s_length); + const f_string_static_t f_color_string_code_underline_s = macro_f_string_static_t_initialize2(F_color_string_code_underline_s, 0, F_color_string_code_underline_s_length); + const f_string_static_t f_color_string_code_blink_s = macro_f_string_static_t_initialize2(F_color_string_code_blink_s, 0, F_color_string_code_blink_s_length); + const f_string_static_t f_color_string_code_reverse_s = macro_f_string_static_t_initialize2(F_color_string_code_reverse_s, 0, F_color_string_code_reverse_s_length); + const f_string_static_t f_color_string_code_conceal_s = macro_f_string_static_t_initialize2(F_color_string_code_conceal_s, 0, F_color_string_code_conceal_s_length); + const f_string_static_t f_color_string_code_black_s = macro_f_string_static_t_initialize2(F_color_string_code_black_s, 0, F_color_string_code_black_s_length); + const f_string_static_t f_color_string_code_red_s = macro_f_string_static_t_initialize2(F_color_string_code_red_s, 0, F_color_string_code_red_s_length); + const f_string_static_t f_color_string_code_green_s = macro_f_string_static_t_initialize2(F_color_string_code_green_s, 0, F_color_string_code_green_s_length); + const f_string_static_t f_color_string_code_yellow_s = macro_f_string_static_t_initialize2(F_color_string_code_yellow_s, 0, F_color_string_code_yellow_s_length); + const f_string_static_t f_color_string_code_blue_s = macro_f_string_static_t_initialize2(F_color_string_code_blue_s, 0, F_color_string_code_blue_s_length); + const f_string_static_t f_color_string_code_purple_s = macro_f_string_static_t_initialize2(F_color_string_code_purple_s, 0, F_color_string_code_purple_s_length); + const f_string_static_t f_color_string_code_teal_s = macro_f_string_static_t_initialize2(F_color_string_code_teal_s, 0, F_color_string_code_teal_s_length); + const f_string_static_t f_color_string_code_white_s = macro_f_string_static_t_initialize2(F_color_string_code_white_s, 0, F_color_string_code_white_s_length); + const f_string_static_t f_color_string_code_black_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_black_bg_s, 0, F_color_string_code_black_bg_s_length); + const f_string_static_t f_color_string_code_red_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_red_bg_s, 0, F_color_string_code_red_bg_s_length); + const f_string_static_t f_color_string_code_green_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_green_bg_s, 0, F_color_string_code_green_bg_s_length); + const f_string_static_t f_color_string_code_yellow_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_yellow_bg_s, 0, F_color_string_code_yellow_bg_s_length); + const f_string_static_t f_color_string_code_blue_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_blue_bg_s, 0, F_color_string_code_blue_bg_s_length); + const f_string_static_t f_color_string_code_purple_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_purple_bg_s, 0, F_color_string_code_purple_bg_s_length); + const f_string_static_t f_color_string_code_teal_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_teal_bg_s, 0, F_color_string_code_teal_bg_s_length); + const f_string_static_t f_color_string_code_white_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_white_bg_s, 0, F_color_string_code_white_bg_s_length); + const f_string_static_t f_color_string_code_bright_black_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_black_s, 0, F_color_string_code_bright_black_s_length); + const f_string_static_t f_color_string_code_bright_red_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_red_s, 0, F_color_string_code_bright_red_s_length); + const f_string_static_t f_color_string_code_bright_green_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_green_s, 0, F_color_string_code_bright_green_s_length); + const f_string_static_t f_color_string_code_bright_yellow_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_yellow_s, 0, F_color_string_code_bright_yellow_s_length); + const f_string_static_t f_color_string_code_bright_blue_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_blue_s, 0, F_color_string_code_bright_blue_s_length); + const f_string_static_t f_color_string_code_bright_purple_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_purple_s, 0, F_color_string_code_bright_purple_s_length); + const f_string_static_t f_color_string_code_bright_teal_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_teal_s, 0, F_color_string_code_bright_teal_s_length); + const f_string_static_t f_color_string_code_bright_white_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_white_s, 0, F_color_string_code_bright_white_s_length); + const f_string_static_t f_color_string_code_bright_black_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_black_bg_s, 0, F_color_string_code_bright_black_bg_s_length); + const f_string_static_t f_color_string_code_bright_red_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_red_bg_s, 0, F_color_string_code_bright_red_bg_s_length); + const f_string_static_t f_color_string_code_bright_green_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_green_bg_s, 0, F_color_string_code_bright_green_bg_s_length); + const f_string_static_t f_color_string_code_bright_yellow_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_yellow_bg_s, 0, F_color_string_code_bright_yellow_bg_s_length); + const f_string_static_t f_color_string_code_bright_blue_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_blue_bg_s, 0, F_color_string_code_bright_blue_bg_s_length); + const f_string_static_t f_color_string_code_bright_purple_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_purple_bg_s, 0, F_color_string_code_bright_purple_bg_s_length); + const f_string_static_t f_color_string_code_bright_teal_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_teal_bg_s, 0, F_color_string_code_bright_teal_bg_s_length); + const f_string_static_t f_color_string_code_bright_white_bg_s = macro_f_string_static_t_initialize2(F_color_string_code_bright_white_bg_s, 0, F_color_string_code_bright_white_bg_s_length); #endif // _di_f_color_strings_s_ #ifndef _di_f_color_set_string_empty_s_ diff --git a/level_0/f_color/c/color-common.h b/level_0/f_color/c/color-common.h index c5e20bb..2a6b061 100644 --- a/level_0/f_color/c/color-common.h +++ b/level_0/f_color/c/color-common.h @@ -28,21 +28,74 @@ extern "C" { #define F_color_max_size_terminated_d 8 #endif // _di_f_color_max_size_ -#ifndef _di_f_color_types_t_ +/** + * Define codes representing color sets. + * + * F_color_code_*: + * - none: No color code set defined. + * - linux: Linux terminal color code set. + * - xterminal: X-Terminal color code set. + */ +#ifndef _di_f_color_code_t_ typedef char f_color_code_t; #define F_color_code_none_d 0 #define F_color_code_linux_d 1 #define F_color_code_xterminal_d 2 -#endif // _di_f_color_types_t_ +#endif // _di_f_color_code_t_ /** * Provide a global color related strings. + * + * F_color_string_*: + * - begin: String used to designate the start of a color code sequence. + * - end: String used to designate the end of a color code sequence. + * - medium: String used to designate the separation between two codes in a color code sequence. + * + * F_color:tring_code_*: + * - reset: String used to describe reset color code. + * - bold: String used to describe bold color code. + * - underline: String used to describe underline color code. + * - blink: String used to describe blink color code. + * - reverse: String used to describe revese color code. + * - conceal: String used to describe conceal color code. + * - black: String used to describe black color code. + * - red: String used to describe red color code. + * - green: String used to describe green color code. + * - yellow: String used to describe yellow color code. + * - blue: String used to describe blue color code. + * - purple: String used to describe purple color code. + * - teal: String used to describe teal color code. + * - white: String used to describe white color code. + * - black_bg: String used to describe black background color code. + * - red_bg: String used to describe red backgound color code. + * - green_bg: String used to describe green backgound color code. + * - yellow_bg: String used to describe yellow backgound color code. + * - blue_bg: String used to describe blue backgound color code. + * - purple_bg: String used to describe purple backgound color code. + * - teal_bg: String used to describe teal backgound color code. + * - white_bg: String used to describe white backgound color code. + * - bright_black: String used to describe bright black backgound color code. + * - bright_red: String used to describe bright red backgound color code. + * - bright_green: String used to describe bright green backgound color code. + * - bright_yellow: String used to describe bright yellow backgound color code. + * - bright_blue: String used to describe bright blue backgound color code. + * - bright_purple: String used to describe bright purple backgound color code. + * - bright_teal: String used to describe bright teal backgound color code. + * - bright_white: String used to describe bright white backgound color code. + * - bright_black_bg: String used to describe bright black backgound color code. + * - bright_red_bg: String used to describe bright red backgound color code. + * - bright_green_bg: String used to describe bright green backgound color code. + * - bright_yellow_bg: String used to describe bright yellow backgound color code. + * - bright_blue_bg: String used to describe bright blue backgound color code. + * - bright_purple_bg: String used to describe bright purple backgound color code. + * - bright_teal_bg: String used to describe bright teal backgound color code. + * - bright_white_bg: String used to describe bright white backgound color code. */ -#ifndef _di_f_color_strings_s_ - #define F_color_string_begin_s "\033["; - #define F_color_string_end_s "m"; - #define F_color_string_medium_s ";"; +#ifndef _di_f_color_string_s_ + #define F_color_string_begin_s "\033[" + #define F_color_string_end_s "m" + #define F_color_string_medium_s ";" #define F_color_string_code_reset_s "0" #define F_color_string_code_bold_s "1" @@ -83,49 +136,92 @@ extern "C" { #define F_color_string_code_bright_teal_bg_s "106" #define F_color_string_code_bright_white_bg_s "107" - extern const f_string_t f_color_string_begin_s; - extern const f_string_t f_color_string_end_s; - extern const f_string_t f_color_string_medium_s; - - extern const f_string_t f_color_string_code_reset_s; - extern const f_string_t f_color_string_code_bold_s; - extern const f_string_t f_color_string_code_underline_s; - extern const f_string_t f_color_string_code_blink_s; - extern const f_string_t f_color_string_code_reverse_s; - extern const f_string_t f_color_string_code_conceal_s; - extern const f_string_t f_color_string_code_black_s; - extern const f_string_t f_color_string_code_red_s; - extern const f_string_t f_color_string_code_green_s; - extern const f_string_t f_color_string_code_yellow_s; - extern const f_string_t f_color_string_code_blue_s; - extern const f_string_t f_color_string_code_purple_s; - extern const f_string_t f_color_string_code_teal_s; - extern const f_string_t f_color_string_code_white_s; - extern const f_string_t f_color_string_code_black_bg_s; - extern const f_string_t f_color_string_code_red_bg_s; - extern const f_string_t f_color_string_code_green_bg_s; - extern const f_string_t f_color_string_code_yellow_bg_s; - extern const f_string_t f_color_string_code_blue_bg_s; - extern const f_string_t f_color_string_code_purple_bg_s; - extern const f_string_t f_color_string_code_teal_bg_s; - extern const f_string_t f_color_string_code_white_bg_s; - extern const f_string_t f_color_string_code_bright_black_s; - extern const f_string_t f_color_string_code_bright_red_s; - extern const f_string_t f_color_string_code_bright_green_s; - extern const f_string_t f_color_string_code_bright_yellow_s; - extern const f_string_t f_color_string_code_bright_blue_s; - extern const f_string_t f_color_string_code_bright_purple_s; - extern const f_string_t f_color_string_code_bright_teal_s; - extern const f_string_t f_color_string_code_bright_white_s; - extern const f_string_t f_color_string_code_bright_black_bg_s; - extern const f_string_t f_color_string_code_bright_red_bg_s; - extern const f_string_t f_color_string_code_bright_green_bg_s; - extern const f_string_t f_color_string_code_bright_yellow_bg_s; - extern const f_string_t f_color_string_code_bright_blue_bg_s; - extern const f_string_t f_color_string_code_bright_purple_bg_s; - extern const f_string_t f_color_string_code_bright_teal_bg_s; - extern const f_string_t f_color_string_code_bright_white_bg_s; -#endif // _di_f_color_strings_s_ + #define F_color_string_begin_s_length 2 + #define F_color_string_end_s_length 1 + #define F_color_string_medium_s_length 1 + + #define F_color_string_code_reset_s_length 1 + #define F_color_string_code_bold_s_length 1 + #define F_color_string_code_underline_s_length 1 + #define F_color_string_code_blink_s_length 1 + #define F_color_string_code_reverse_s_length 1 + #define F_color_string_code_conceal_s_length 1 + #define F_color_string_code_black_s_length 2 + #define F_color_string_code_red_s_length 2 + #define F_color_string_code_green_s_length 2 + #define F_color_string_code_yellow_s_length 2 + #define F_color_string_code_blue_s_length 2 + #define F_color_string_code_purple_s_length 2 + #define F_color_string_code_teal_s_length 2 + #define F_color_string_code_white_s_length 2 + #define F_color_string_code_black_bg_s_length 2 + #define F_color_string_code_red_bg_s_length 2 + #define F_color_string_code_green_bg_s_length 2 + #define F_color_string_code_yellow_bg_s_length 2 + #define F_color_string_code_blue_bg_s_length 2 + #define F_color_string_code_purple_bg_s_length 2 + #define F_color_string_code_teal_bg_s_length 2 + #define F_color_string_code_white_bg_s_length 2 + #define F_color_string_code_bright_black_s_length 2 + #define F_color_string_code_bright_red_s_length 2 + #define F_color_string_code_bright_green_s_length 2 + #define F_color_string_code_bright_yellow_s_length 2 + #define F_color_string_code_bright_blue_s_length 2 + #define F_color_string_code_bright_purple_s_length 2 + #define F_color_string_code_bright_teal_s_length 2 + #define F_color_string_code_bright_white_s_length 2 + #define F_color_string_code_bright_black_bg_s_length 3 + #define F_color_string_code_bright_red_bg_s_length 3 + #define F_color_string_code_bright_green_bg_s_length 3 + #define F_color_string_code_bright_yellow_bg_s_length 3 + #define F_color_string_code_bright_blue_bg_s_length 3 + #define F_color_string_code_bright_purple_bg_s_length 3 + #define F_color_string_code_bright_teal_bg_s_length 3 + #define F_color_string_code_bright_white_bg_s_length 3 + + extern const f_string_static_t f_color_string_begin_s; + extern const f_string_static_t f_color_string_end_s; + extern const f_string_static_t f_color_string_medium_s; + + extern const f_string_static_t f_color_string_code_reset_s; + extern const f_string_static_t f_color_string_code_bold_s; + extern const f_string_static_t f_color_string_code_underline_s; + extern const f_string_static_t f_color_string_code_blink_s; + extern const f_string_static_t f_color_string_code_reverse_s; + extern const f_string_static_t f_color_string_code_conceal_s; + extern const f_string_static_t f_color_string_code_black_s; + extern const f_string_static_t f_color_string_code_red_s; + extern const f_string_static_t f_color_string_code_green_s; + extern const f_string_static_t f_color_string_code_yellow_s; + extern const f_string_static_t f_color_string_code_blue_s; + extern const f_string_static_t f_color_string_code_purple_s; + extern const f_string_static_t f_color_string_code_teal_s; + extern const f_string_static_t f_color_string_code_white_s; + extern const f_string_static_t f_color_string_code_black_bg_s; + extern const f_string_static_t f_color_string_code_red_bg_s; + extern const f_string_static_t f_color_string_code_green_bg_s; + extern const f_string_static_t f_color_string_code_yellow_bg_s; + extern const f_string_static_t f_color_string_code_blue_bg_s; + extern const f_string_static_t f_color_string_code_purple_bg_s; + extern const f_string_static_t f_color_string_code_teal_bg_s; + extern const f_string_static_t f_color_string_code_white_bg_s; + extern const f_string_static_t f_color_string_code_bright_black_s; + extern const f_string_static_t f_color_string_code_bright_red_s; + extern const f_string_static_t f_color_string_code_bright_green_s; + extern const f_string_static_t f_color_string_code_bright_yellow_s; + extern const f_string_static_t f_color_string_code_bright_blue_s; + extern const f_string_static_t f_color_string_code_bright_purple_s; + extern const f_string_static_t f_color_string_code_bright_teal_s; + extern const f_string_static_t f_color_string_code_bright_white_s; + extern const f_string_static_t f_color_string_code_bright_black_bg_s; + extern const f_string_static_t f_color_string_code_bright_red_bg_s; + extern const f_string_static_t f_color_string_code_bright_green_bg_s; + extern const f_string_static_t f_color_string_code_bright_yellow_bg_s; + extern const f_string_static_t f_color_string_code_bright_blue_bg_s; + extern const f_string_static_t f_color_string_code_bright_purple_bg_s; + extern const f_string_static_t f_color_string_code_bright_teal_bg_s; + extern const f_string_static_t f_color_string_code_bright_white_bg_s; +#endif // _di_f_color_string_s_ /** * Specify color modes. @@ -141,27 +237,48 @@ extern "C" { #ifndef _di_f_color_mode_t_ typedef uint8_t f_color_mode_t; - #define F_color_mode_none_d 0 - #define F_color_mode_no_color_d 1 - #define F_color_mode_dark_d 2 - #define F_color_mode_light_d 3 + #define F_color_mode_none_d 0 + #define F_color_mode_no_color_d 1 + #define F_color_mode_dark_d 2 + #define F_color_mode_light_d 3 #endif // _di_f_color_mode_t_ +/** + * Provide structure for containing color format control strings. + * + * begin: String used to designate the start of a color code sequence. + * end: String used to designate the end of a color code sequence. + * medium: String used to designate the separation between two codes in a color code sequence. + */ #ifndef _di_f_color_format_t_ typedef struct { - const char *begin; - const char *end; - const char *medium; + f_string_static_t begin; + f_string_static_t end; + f_string_static_t medium; } f_color_format_t; - #define f_color_format_t_initialize { 0, 0, 0 } - #define f_color_format_t_initialize_linux { f_color_string_begin_s, f_color_string_end_s, f_color_string_medium_s } - #define f_color_format_t_initialize_xterminal { f_color_string_begin_s, f_color_string_end_s, f_color_string_medium_s } + #define f_color_format_t_initialize { \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize \ + } + + #define f_color_format_t_initialize_linux { \ + f_color_string_begin_s, \ + f_color_string_end_s, \ + f_color_string_medium_s \ + } + + #define f_color_format_t_initialize_xterminal { \ + f_color_string_begin_s, \ + f_color_string_end_s, \ + f_color_string_medium_s \ + } #define macro_f_color_format_t_clear(format) \ - format.begin = 0; \ - format.end = 0; \ - format.medium = 0; + macro_f_string_static_t_clear(format.begin) \ + macro_f_string_static_t_clear(format.end) \ + macro_f_string_static_t_clear(format.medium) #define macro_f_color_format_t_set_linux(format) \ format.begin = f_color_string_begin_s; \ @@ -182,91 +299,250 @@ extern "C" { * Custom initializers are provided for common defaults: * - f_color_t_initialize_linux: ideal for linux console. * - f_color_t_initialize_xterminal: ideal for linux x-terminal. + * + * reset: String used to describe reset color code. + * bold: String used to describe bold color code. + * underline: String used to describe underline color code. + * blink: String used to describe blink color code. + * reverse: String used to describe revese color code. + * conceal: String used to describe conceal color code. + * black: String used to describe black color code. + * red: String used to describe red color code. + * green: String used to describe green color code. + * yellow: String used to describe yellow color code. + * blue: String used to describe blue color code. + * purple: String used to describe purple color code. + * teal: String used to describe teal color code. + * white: String used to describe white color code. + * black_bg: String used to describe black background color code. + * red_bg: String used to describe red backgound color code. + * green_bg: String used to describe green backgound color code. + * yellow_bg: String used to describe yellow backgound color code. + * blue_bg: String used to describe blue backgound color code. + * purple_bg: String used to describe purple backgound color code. + * teal_bg: String used to describe teal backgound color code. + * white_bg: String used to describe white backgound color code. + * bright_black: String used to describe bright black backgound color code. + * bright_red: String used to describe bright red backgound color code. + * bright_green: String used to describe bright green backgound color code. + * bright_yellow: String used to describe bright yellow backgound color code. + * bright_blue: String used to describe bright blue backgound color code. + * bright_purple: String used to describe bright purple backgound color code. + * bright_teal: String used to describe bright teal backgound color code. + * bright_white: String used to describe bright white backgound color code. + * bright_black_bg: String used to describe bright black backgound color code. + * bright_red_bg: String used to describe bright red backgound color code. + * bright_green_bg: String used to describe bright green backgound color code. + * bright_yellow_bg: String used to describe bright yellow backgound color code. + * bright_blue_bg: String used to describe bright blue backgound color code. + * bright_purple_bg: String used to describe bright purple backgound color code. + * bright_teal_bg: String used to describe bright teal backgound color code. + * bright_white_bg: String used to describe bright white backgound color code. */ #ifndef _di_f_color_t_ typedef struct { - const char *reset; - const char *bold; - const char *underline; - const char *blink; - const char *reverse; - const char *conceal; - const char *black; - const char *red; - const char *green; - const char *yellow; - const char *blue; - const char *purple; - const char *teal; - const char *white; - const char *black_bg; - const char *red_bg; - const char *green_bg; - const char *yellow_bg; - const char *blue_bg; - const char *purple_bg; - const char *teal_bg; - const char *white_bg; - const char *bright_black; - const char *bright_red; - const char *bright_green; - const char *bright_yellow; - const char *bright_blue; - const char *bright_purple; - const char *bright_teal; - const char *bright_white; - const char *bright_black_bg; - const char *bright_red_bg; - const char *bright_green_bg; - const char *bright_yellow_bg; - const char *bright_blue_bg; - const char *bright_purple_bg; - const char *bright_teal_bg; - const char *bright_white_bg; + f_string_static_t reset; + f_string_static_t bold; + f_string_static_t underline; + f_string_static_t blink; + f_string_static_t reverse; + f_string_static_t conceal; + f_string_static_t black; + f_string_static_t red; + f_string_static_t green; + f_string_static_t yellow; + f_string_static_t blue; + f_string_static_t purple; + f_string_static_t teal; + f_string_static_t white; + f_string_static_t black_bg; + f_string_static_t red_bg; + f_string_static_t green_bg; + f_string_static_t yellow_bg; + f_string_static_t blue_bg; + f_string_static_t purple_bg; + f_string_static_t teal_bg; + f_string_static_t white_bg; + f_string_static_t bright_black; + f_string_static_t bright_red; + f_string_static_t bright_green; + f_string_static_t bright_yellow; + f_string_static_t bright_blue; + f_string_static_t bright_purple; + f_string_static_t bright_teal; + f_string_static_t bright_white; + f_string_static_t bright_black_bg; + f_string_static_t bright_red_bg; + f_string_static_t bright_green_bg; + f_string_static_t bright_yellow_bg; + f_string_static_t bright_blue_bg; + f_string_static_t bright_purple_bg; + f_string_static_t bright_teal_bg; + f_string_static_t bright_white_bg; } f_color_t; - #define f_color_t_initialize_linux { f_color_string_code_reset_s, f_color_string_code_bold_s, f_color_string_code_underline_s, f_color_string_code_blink_s, f_color_string_code_reverse_s, f_color_string_code_conceal_s, f_color_string_code_black_s, f_color_string_code_red_s, f_color_string_code_green_s, f_color_string_code_yellow_s, f_color_string_code_blue_s, f_color_string_code_purple_s, f_color_string_code_teal_s, f_color_string_code_white_s, f_color_string_code_black_bg_s, f_color_string_code_red_bg_s, f_color_string_code_green_bg_s, f_color_string_code_yellow_bg_s, f_color_string_code_blue_bg_s, f_color_string_code_purple_bg_s, f_color_string_code_teal_bg_s, f_color_string_code_white_bg_s, f_color_string_code_black_s, f_color_string_code_red_s, f_color_string_code_green_s, f_color_string_code_yellow_s, f_color_string_code_blue_s, f_color_string_code_purple_s, f_color_string_code_teal_s, f_color_string_code_white_s, f_color_string_code_black_bg_s, f_color_string_code_red_bg_s, f_color_string_code_green_bg_s, f_color_string_code_yellow_bg_s, f_color_string_code_blue_bg_s, f_color_string_code_purple_bg_s, f_color_string_code_teal_bg_s, f_color_string_code_white_bg_s } - #define f_color_t_initialize_xterminal { f_color_string_code_reset_s, f_color_string_code_bold_s, f_color_string_code_underline_s, f_color_string_code_blink_s, f_color_string_code_reverse_s, f_color_string_code_conceal_s, f_color_string_code_black_s, f_color_string_code_red_s, f_color_string_code_green_s, f_color_string_code_yellow_s, f_color_string_code_blue_s, f_color_string_code_purple_s, f_color_string_code_teal_s, f_color_string_code_white_s, f_color_string_code_black_bg_s, f_color_string_code_red_bg_s, f_color_string_code_green_bg_s, f_color_string_code_yellow_bg_s, f_color_string_code_blue_bg_s, f_color_string_code_purple_bg_s, f_color_string_code_teal_bg_s, f_color_string_code_white_bg_s, f_color_string_code_bright_black_s, f_color_string_code_bright_red_s, f_color_string_code_bright_green_s, f_color_string_code_bright_yellow_s, f_color_string_code_bright_blue_s, f_color_string_code_bright_purple_s, f_color_string_code_bright_teal_s, f_color_string_code_bright_white_s, f_color_string_code_bright_black_bg_s, f_color_string_code_bright_red_bg_s, f_color_string_code_bright_green_bg_s, f_color_string_code_bright_yellow_bg_s, f_color_string_code_bright_blue_bg_s, f_color_string_code_bright_purple_bg_s, f_color_string_code_bright_teal_bg_s, f_color_string_code_bright_white_bg_s } + #define f_color_t_initialize { \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + f_string_static_t_initialize, \ + } + + #define f_color_t_initialize_linux { \ + f_color_string_code_reset_s, \ + f_color_string_code_bold_s, \ + f_color_string_code_underline_s, \ + f_color_string_code_blink_s, \ + f_color_string_code_reverse_s, \ + f_color_string_code_conceal_s, \ + f_color_string_code_black_s, \ + f_color_string_code_red_s, \ + f_color_string_code_green_s, \ + f_color_string_code_yellow_s, \ + f_color_string_code_blue_s, \ + f_color_string_code_purple_s, \ + f_color_string_code_teal_s, \ + f_color_string_code_white_s, \ + f_color_string_code_black_bg_s, \ + f_color_string_code_red_bg_s, \ + f_color_string_code_green_bg_s, \ + f_color_string_code_yellow_bg_s, \ + f_color_string_code_blue_bg_s, \ + f_color_string_code_purple_bg_s, \ + f_color_string_code_teal_bg_s, \ + f_color_string_code_white_bg_s, \ + f_color_string_code_black_s, \ + f_color_string_code_red_s, \ + f_color_string_code_green_s, \ + f_color_string_code_yellow_s, \ + f_color_string_code_blue_s, \ + f_color_string_code_purple_s, \ + f_color_string_code_teal_s, \ + f_color_string_code_white_s, \ + f_color_string_code_black_bg_s, \ + f_color_string_code_red_bg_s, \ + f_color_string_code_green_bg_s, \ + f_color_string_code_yellow_bg_s, \ + f_color_string_code_blue_bg_s, \ + f_color_string_code_purple_bg_s, \ + f_color_string_code_teal_bg_s, \ + f_color_string_code_white_bg_s, \ + } + + #define f_color_t_initialize_xterminal { \ + f_color_string_code_reset_s, \ + f_color_string_code_bold_s, \ + f_color_string_code_underline_s, \ + f_color_string_code_blink_s, \ + f_color_string_code_reverse_s, \ + f_color_string_code_conceal_s, \ + f_color_string_code_black_s, \ + f_color_string_code_red_s, \ + f_color_string_code_green_s, \ + f_color_string_code_yellow_s, \ + f_color_string_code_blue_s, \ + f_color_string_code_purple_s, \ + f_color_string_code_teal_s, \ + f_color_string_code_white_s, \ + f_color_string_code_black_bg_s, \ + f_color_string_code_red_bg_s, \ + f_color_string_code_green_bg_s, \ + f_color_string_code_yellow_bg_s, \ + f_color_string_code_blue_bg_s, \ + f_color_string_code_purple_bg_s, \ + f_color_string_code_teal_bg_s, \ + f_color_string_code_white_bg_s, \ + f_color_string_code_bright_black_s, \ + f_color_string_code_bright_red_s, \ + f_color_string_code_bright_green_s, \ + f_color_string_code_bright_yellow_s, \ + f_color_string_code_bright_blue_s, \ + f_color_string_code_bright_purple_s, \ + f_color_string_code_bright_teal_s, \ + f_color_string_code_bright_white_s, \ + f_color_string_code_bright_black_bg_s, \ + f_color_string_code_bright_red_bg_s, \ + f_color_string_code_bright_green_bg_s, \ + f_color_string_code_bright_yellow_bg_s, \ + f_color_string_code_bright_blue_bg_s, \ + f_color_string_code_bright_purple_bg_s, \ + f_color_string_code_bright_teal_bg_s, \ + f_color_string_code_bright_white_bg_s, \ + } #define macro_f_color_t_clear(color) \ - color.reset = 0; \ - color.bold = 0; \ - color.underline = 0; \ - color.blink = 0; \ - color.reverse = 0; \ - color.conceal = 0; \ - color.black = 0; \ - color.red = 0; \ - color.green = 0; \ - color.yellow = 0; \ - color.blue = 0; \ - color.purple = 0; \ - color.teal = 0; \ - color.white = 0; \ - color.black_bg = 0; \ - color.red_bg = 0; \ - color.green_bg = 0; \ - color.yellow_bg = 0; \ - color.blue_bg = 0; \ - color.purple_bg = 0; \ - color.teal_bg = 0; \ - color.white_bg = 0; \ - color.bright_black = 0; \ - color.bright_red = 0; \ - color.bright_green = 0; \ - color.bright_yellow = 0; \ - color.bright_blue = 0; \ - color.bright_purple = 0; \ - color.bright_teal = 0; \ - color.bright_white = 0; \ - color.bright_black_bg = 0; \ - color.bright_red_bg = 0; \ - color.bright_green_bg = 0; \ - color.bright_yellow_bg = 0; \ - color.bright_blue_bg = 0; \ - color.bright_purple_bg = 0; \ - color.bright_teal_bg = 0; \ - color.bright_white_bg = 0; + macro_f_string_static_t_clear(color.reset) \ + macro_f_string_static_t_clear(color.bold) \ + macro_f_string_static_t_clear(color.underline) \ + macro_f_string_static_t_clear(color.blink) \ + macro_f_string_static_t_clear(color.reverse) \ + macro_f_string_static_t_clear(color.conceal) \ + macro_f_string_static_t_clear(color.black) \ + macro_f_string_static_t_clear(color.red) \ + macro_f_string_static_t_clear(color.green) \ + macro_f_string_static_t_clear(color.yellow) \ + macro_f_string_static_t_clear(color.blue) \ + macro_f_string_static_t_clear(color.purple) \ + macro_f_string_static_t_clear(color.teal) \ + macro_f_string_static_t_clear(color.white) \ + macro_f_string_static_t_clear(color.black_bg) \ + macro_f_string_static_t_clear(color.red_bg) \ + macro_f_string_static_t_clear(color.green_bg) \ + macro_f_string_static_t_clear(color.yellow_bg) \ + macro_f_string_static_t_clear(color.blue_bg) \ + macro_f_string_static_t_clear(color.purple_bg) \ + macro_f_string_static_t_clear(color.teal_bg) \ + macro_f_string_static_t_clear(color.white_bg) \ + macro_f_string_static_t_clear(color.bright_black) \ + macro_f_string_static_t_clear(color.bright_red) \ + macro_f_string_static_t_clear(color.bright_green) \ + macro_f_string_static_t_clear(color.bright_yellow) \ + macro_f_string_static_t_clear(color.bright_blue) \ + macro_f_string_static_t_clear(color.bright_purple) \ + macro_f_string_static_t_clear(color.bright_teal) \ + macro_f_string_static_t_clear(color.bright_white) \ + macro_f_string_static_t_clear(color.bright_black_bg) \ + macro_f_string_static_t_clear(color.bright_red_bg) \ + macro_f_string_static_t_clear(color.bright_green_bg) \ + macro_f_string_static_t_clear(color.bright_yellow_bg) \ + macro_f_string_static_t_clear(color.bright_blue_bg) \ + macro_f_string_static_t_clear(color.bright_purple_bg) \ + macro_f_string_static_t_clear(color.bright_teal_bg) \ + macro_f_string_static_t_clear(color.bright_white_bg) #define macro_f_color_t_set_linux(color) \ color.reset = f_color_string_code_reset_s; \ @@ -410,7 +686,18 @@ extern "C" { f_color_set_t normal_reset; } f_color_set_context_t; - #define f_color_set_context_t_initialize { f_color_set_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize, f_color_set_t_initialize } + #define f_color_set_context_t_initialize { \ + f_color_set_t_initialize, \ + f_color_set_t_initialize, \ + f_color_set_t_initialize, \ + f_color_set_t_initialize, \ + f_color_set_t_initialize, \ + f_color_set_t_initialize, \ + f_color_set_t_initialize, \ + f_color_set_t_initialize, \ + f_color_set_t_initialize, \ + f_color_set_t_initialize \ + } #define macro_f_color_set_context_t_initialize(reset, warning, error, title, notable, important, standout, success, normal, normal_reset) { reset, warning, error, title, notable, important, standout, success, normal, normal_reset } @@ -466,7 +753,22 @@ extern "C" { f_string_dynamic_t normal_reset; } f_color_context_t; - #define f_color_context_t_initialize { f_color_t_initialize_linux, f_color_format_t_initialize_linux, F_color_mode_none_d, f_color_set_context_t_initialize, f_string_dynamic_t_initialize, f_string_dynamic_t_initialize, f_string_dynamic_t_initialize, f_string_dynamic_t_initialize, f_string_dynamic_t_initialize, f_string_dynamic_t_initialize, f_string_dynamic_t_initialize, f_string_dynamic_t_initialize, f_string_dynamic_t_initialize, f_string_dynamic_t_initialize } + #define f_color_context_t_initialize { \ + f_color_t_initialize_linux, \ + f_color_format_t_initialize_linux, \ + F_color_mode_none_d, \ + f_color_set_context_t_initialize, \ + f_string_dynamic_t_initialize, \ + f_string_dynamic_t_initialize, \ + f_string_dynamic_t_initialize, \ + f_string_dynamic_t_initialize, \ + f_string_dynamic_t_initialize, \ + f_string_dynamic_t_initialize, \ + f_string_dynamic_t_initialize, \ + f_string_dynamic_t_initialize, \ + f_string_dynamic_t_initialize, \ + f_string_dynamic_t_initialize \ + } #define macro_f_color_context_t_clear(context) \ macro_f_color_t_clear(context.list) \ diff --git a/level_0/f_color/c/color.c b/level_0/f_color/c/color.c index 89f6edc..5cd1b01 100644 --- a/level_0/f_color/c/color.c +++ b/level_0/f_color/c/color.c @@ -5,7 +5,7 @@ extern "C" { #endif #ifndef _di_f_color_load_context_ - f_status_t f_color_load_context(f_color_context_t *context, const bool use_light_colors) { + f_status_t f_color_load_context(const bool use_light_colors, f_color_context_t * const context) { #ifndef _di_level_0_parameter_checking_ if (!context) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ @@ -22,35 +22,35 @@ extern "C" { } } - f_status_t status = macro_fl_color_save_1(&context->reset, context->format, context->list.reset); + f_status_t status = f_color_save_1(context->format, context->list.reset, &context->reset); if (F_status_is_error_not(status)) { - status = macro_fl_color_save_1(&context->warning, context->format, context->list.yellow); + status = f_color_save_1(context->format, context->list.yellow, &context->warning); } if (F_status_is_error_not(status)) { - status = macro_fl_color_save_2(&context->error, context->format, context->list.bold, context->list.red); + status = f_color_save_2(context->format, context->list.bold, context->list.red, &context->error); } if (F_status_is_error_not(status)) { - status = macro_fl_color_save_2(&context->success, context->format, context->list.bold, context->list.green); + status = f_color_save_2(context->format, context->list.bold, context->list.green, &context->success); } if (F_status_is_error_not(status)) { - status = macro_fl_color_save_1(&context->notable, context->format, context->list.bold); + status = f_color_save_1(context->format, context->list.bold, &context->notable); } if (use_light_colors) { if (F_status_is_error_not(status)) { - status = macro_fl_color_save_2(&context->title, context->format, context->list.bold, context->list.blue); + status = f_color_save_2(context->format, context->list.bold, context->list.blue, &context->title); } if (F_status_is_error_not(status)) { - status = macro_fl_color_save_1(&context->important, context->format, context->list.blue); + status = f_color_save_1(context->format, context->list.blue, &context->important); } if (F_status_is_error_not(status)) { - status = macro_fl_color_save_1(&context->standout, context->format, context->list.purple); + status = f_color_save_1(context->format, context->list.purple, &context->standout); } if (F_status_is_error_not(status)) { @@ -59,15 +59,15 @@ extern "C" { } else { if (F_status_is_error_not(status)) { - status = macro_fl_color_save_2(&context->title, context->format, context->list.bold, context->list.yellow); + status = f_color_save_2(context->format, context->list.bold, context->list.yellow, &context->title); } if (F_status_is_error_not(status)) { - status = macro_fl_color_save_2(&context->important, context->format, context->list.bold, context->list.green); + status = f_color_save_2(context->format, context->list.bold, context->list.green, &context->important); } if (F_status_is_error_not(status)) { - status = macro_fl_color_save_1(&context->standout, context->format, context->list.green); + status = f_color_save_1(context->format, context->list.green, &context->standout); } if (F_status_is_error_not(status)) { @@ -111,226 +111,195 @@ extern "C" { } #endif // _di_f_color_load_context_ -#ifndef _di_f_color_save_ - f_status_t f_color_save(f_string_dynamic_t * const buffer, const f_color_format_t format, const char *color1, const char *color2, const char *color3, const char *color4, const char *color5) { +#ifndef _di_f_color_save_1_ + f_status_t f_color_save_1(const f_color_format_t format, const f_string_static_t color1, f_string_dynamic_t * const buffer) { #ifndef _di_level_0_parameter_checking_ if (!buffer) return F_status_set_error(F_parameter); - if (!color1) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ + + f_status_t status = F_none; + + status = f_string_dynamic_increase_by(format.begin.used + format.end.used + color1.used + 1, buffer); + if (F_status_is_error(status)) return status; + + status = f_string_dynamic_append(format.begin, buffer); + if (F_status_is_error(status)) return status; + + status = f_string_dynamic_append(color1, buffer); + if (F_status_is_error(status)) return status; - // Require all data to be in the proper order. - if (!color2 && (color3 || color4 || color5)) return F_status_set_error(F_parameter); - if (!color3 && (color4 || color5)) return F_status_set_error(F_parameter); - if (!color4 && color5) return F_status_set_error(F_parameter); + status = f_string_dynamic_append(format.end, buffer); + if (F_status_is_error(status)) return status; + + buffer->string[buffer->used] = 0; + + return F_none; + } +#endif // _di_f_color_save_1_ + +#ifndef _di_f_color_save_1_ + f_status_t f_color_save_2(const f_color_format_t format, const f_string_static_t color1, const f_string_static_t color2, f_string_dynamic_t * const buffer) { + #ifndef _di_level_0_parameter_checking_ + if (!buffer) return F_status_set_error(F_parameter); #endif // _di_level_0_parameter_checking_ - f_array_length_t size_string = 0; - uint8_t size_begin = 0; - uint8_t size_medium = 0; - uint8_t size_end = 0; - uint8_t size_color1 = 0; - uint8_t size_color2 = 0; - uint8_t size_color3 = 0; - uint8_t size_color4 = 0; - uint8_t size_color5 = 0; - - if (format.begin) { - size_begin = strnlen(format.begin, F_color_max_size_d); - size_string += size_begin; - } + f_status_t status = F_none; - if (format.medium) { - size_medium += strnlen(format.medium, F_color_max_size_d); - } + status = f_string_dynamic_increase_by(format.begin.used + format.medium.used + format.end.used + color1.used + color2.used + 1, buffer); + if (F_status_is_error(status)) return status; - if (format.end) { - size_end = strnlen(format.end, F_color_max_size_d); - size_string += size_end; - } + status = f_string_dynamic_append(format.begin, buffer); + if (F_status_is_error(status)) return status; - size_color1 = strnlen(color1, F_color_max_size_d); - size_string += size_color1; + status = f_string_dynamic_append(color1, buffer); + if (F_status_is_error(status)) return status; - if (color2) { - size_color2 = strnlen(color2, F_color_max_size_d); - size_string += size_color2; - } + status = f_string_dynamic_append(format.medium, buffer); + if (F_status_is_error(status)) return status; - if (color3) { - size_color3 = strnlen(color3, F_color_max_size_d); - size_string += size_color3; - } + status = f_string_dynamic_append(color2, buffer); + if (F_status_is_error(status)) return status; - if (color4) { - size_color4 = strnlen(color4, F_color_max_size_d); - size_string += size_color4; - } + status = f_string_dynamic_append(format.end, buffer); + if (F_status_is_error(status)) return status; - if (color5) { - size_color5 = strnlen(color5, F_color_max_size_d); - size_string += size_color5; - } + buffer->string[buffer->used] = 0; - { - const f_status_t status = f_string_dynamic_increase_by(size_string + 1, buffer); - if (F_status_is_error(status)) return status; - } + return F_none; + } +#endif // _di_f_color_save_2_ - if (!color2) { - if (size_begin) { - memcpy(buffer->string + buffer->used, format.begin, size_begin); - buffer->used += size_begin; - } +#ifndef _di_f_color_save_3_ + f_status_t f_color_save_3(const f_color_format_t format, const f_string_static_t color1, const f_string_static_t color2, const f_string_static_t color3, f_string_dynamic_t * const buffer) { + #ifndef _di_level_0_parameter_checking_ + if (!buffer) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ - memcpy(buffer->string + buffer->used, color1, size_color1); - buffer->used += size_color1; + f_status_t status = F_none; - if (size_end) { - memcpy(buffer->string + buffer->used, format.end, size_end); - buffer->used += size_end; - } - } - else if (!color3) { - if (size_begin) { - memcpy(buffer->string + buffer->used, format.begin, size_begin); - buffer->used += size_begin; - } + status = f_string_dynamic_increase_by(format.begin.used + (format.medium.used * 2) + format.end.used + color1.used + color2.used + color3.used + 1, buffer); + if (F_status_is_error(status)) return status; - memcpy(buffer->string + buffer->used, color1, size_color1); - buffer->used += size_color1; + status = f_string_dynamic_append(format.begin, buffer); + if (F_status_is_error(status)) return status; - if (size_medium) { - memcpy(buffer->string + buffer->used, format.medium, size_medium); - buffer->used += size_medium; - } + status = f_string_dynamic_append(color1, buffer); + if (F_status_is_error(status)) return status; - memcpy(buffer->string + buffer->used, color2, size_color2); - buffer->used += size_color2; + status = f_string_dynamic_append(format.medium, buffer); + if (F_status_is_error(status)) return status; - if (size_end) { - memcpy(buffer->string + buffer->used, format.end, size_end); - buffer->used += size_end; - } - } - else if (!color4) { - if (size_begin) { - memcpy(buffer->string + buffer->used, format.begin, size_begin); - buffer->used += size_begin; - } + status = f_string_dynamic_append(color2, buffer); + if (F_status_is_error(status)) return status; - memcpy(buffer->string + buffer->used, color1, size_color1); - buffer->used += size_color1; + status = f_string_dynamic_append(format.medium, buffer); + if (F_status_is_error(status)) return status; - if (size_medium) { - memcpy(buffer->string + buffer->used, format.medium, size_medium); - buffer->used += size_medium; - } + status = f_string_dynamic_append(color3, buffer); + if (F_status_is_error(status)) return status; - memcpy(buffer->string + buffer->used, color2, size_color2); - buffer->used += size_color2; + status = f_string_dynamic_append(format.end, buffer); + if (F_status_is_error(status)) return status; - if (size_medium) { - memcpy(buffer->string + buffer->used, format.medium, size_medium); - buffer->used += size_medium; - } + buffer->string[buffer->used] = 0; - memcpy(buffer->string + buffer->used, color3, size_color3); - buffer->used += size_color3; + return F_none; + } +#endif // _di_f_color_save_3_ - if (size_end) { - memcpy(buffer->string + buffer->used, format.end, size_end); - buffer->used += size_end; - } - } - else if (!color5) { - if (size_begin) { - memcpy(buffer->string + buffer->used, format.begin, size_begin); - buffer->used += size_begin; - } +#ifndef _di_f_color_save_4_ + f_status_t f_color_save_4(const f_color_format_t format, const f_string_static_t color1, const f_string_static_t color2, const f_string_static_t color3, const f_string_static_t color4, f_string_dynamic_t * const buffer) { + #ifndef _di_level_0_parameter_checking_ + if (!buffer) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ - memcpy(buffer->string + buffer->used, color1, size_color1); - buffer->used += size_color1; + f_status_t status = F_none; - if (size_medium) { - memcpy(buffer->string + buffer->used, format.medium, size_medium); - buffer->used += size_medium; - } + status = f_string_dynamic_increase_by(format.begin.used + (format.medium.used * 3) + format.end.used + color1.used + color2.used + color3.used + color4.used + 1, buffer); + if (F_status_is_error(status)) return status; - memcpy(buffer->string + buffer->used, color2, size_color2); - buffer->used += size_color2; + status = f_string_dynamic_append(format.begin, buffer); + if (F_status_is_error(status)) return status; - if (size_medium) { - memcpy(buffer->string + buffer->used, format.medium, size_medium); - buffer->used += size_medium; - } + status = f_string_dynamic_append(color1, buffer); + if (F_status_is_error(status)) return status; - memcpy(buffer->string + buffer->used, color3, size_color3); - buffer->used += size_color3; + status = f_string_dynamic_append(format.medium, buffer); + if (F_status_is_error(status)) return status; - if (size_medium) { - memcpy(buffer->string + buffer->used, format.medium, size_medium); - buffer->used += size_medium; - } + status = f_string_dynamic_append(color2, buffer); + if (F_status_is_error(status)) return status; - memcpy(buffer->string + buffer->used, color4, size_color4); - buffer->used += size_color4; + status = f_string_dynamic_append(format.medium, buffer); + if (F_status_is_error(status)) return status; - if (size_end) { - memcpy(buffer->string + buffer->used, format.end, size_end); - buffer->used += size_end; - } - } - else { - if (size_begin) { - memcpy(buffer->string + buffer->used, format.begin, size_begin); - buffer->used += size_begin; - } + status = f_string_dynamic_append(color3, buffer); + if (F_status_is_error(status)) return status; - memcpy(buffer->string + buffer->used, color1, size_color1); - buffer->used += size_color1; + status = f_string_dynamic_append(format.medium, buffer); + if (F_status_is_error(status)) return status; - if (size_medium) { - memcpy(buffer->string + buffer->used, format.medium, size_medium); - buffer->used += size_medium; - } + status = f_string_dynamic_append(color4, buffer); + if (F_status_is_error(status)) return status; - memcpy(buffer->string + buffer->used, color2, size_color2); - buffer->used += size_color2; + status = f_string_dynamic_append(format.end, buffer); + if (F_status_is_error(status)) return status; - if (size_medium) { - memcpy(buffer->string + buffer->used, format.medium, size_medium); - buffer->used += size_medium; - } + buffer->string[buffer->used] = 0; - memcpy(buffer->string + buffer->used, color3, size_color3); - buffer->used += size_color3; + return F_none; + } +#endif // _di_f_color_save_4_ - if (size_medium) { - memcpy(buffer->string + buffer->used, format.medium, size_medium); - buffer->used += size_medium; - } +#ifndef _di_f_color_save_5_ + f_status_t f_color_save_5(const f_color_format_t format, const f_string_static_t color1, const f_string_static_t color2, const f_string_static_t color3, const f_string_static_t color4, const f_string_static_t color5, f_string_dynamic_t * const buffer) { + #ifndef _di_level_0_parameter_checking_ + if (!buffer) return F_status_set_error(F_parameter); + #endif // _di_level_0_parameter_checking_ - memcpy(buffer->string + buffer->used, color4, size_color4); - buffer->used += size_color4; + f_status_t status = F_none; - if (size_medium) { - memcpy(buffer->string + buffer->used, format.medium, size_medium); - buffer->used += size_medium; - } + status = f_string_dynamic_increase_by(format.begin.used + (format.medium.used * 4) + format.end.used + color1.used + color2.used + color3.used + color4.used + color5.used + 1, buffer); + if (F_status_is_error(status)) return status; - memcpy(buffer->string + buffer->used, color5, size_color5); - buffer->used += size_color5; + status = f_string_dynamic_append(format.begin, buffer); + if (F_status_is_error(status)) return status; - if (size_end) { - memcpy(buffer->string + buffer->used, format.end, size_end); - buffer->used += size_end; - } - } + status = f_string_dynamic_append(color1, buffer); + if (F_status_is_error(status)) return status; + + status = f_string_dynamic_append(format.medium, buffer); + if (F_status_is_error(status)) return status; + + status = f_string_dynamic_append(color2, buffer); + if (F_status_is_error(status)) return status; + + status = f_string_dynamic_append(format.medium, buffer); + if (F_status_is_error(status)) return status; + + status = f_string_dynamic_append(color3, buffer); + if (F_status_is_error(status)) return status; + + status = f_string_dynamic_append(format.medium, buffer); + if (F_status_is_error(status)) return status; + + status = f_string_dynamic_append(color4, buffer); + if (F_status_is_error(status)) return status; + + status = f_string_dynamic_append(format.medium, buffer); + if (F_status_is_error(status)) return status; + + status = f_string_dynamic_append(color5, buffer); + if (F_status_is_error(status)) return status; + + status = f_string_dynamic_append(format.end, buffer); + if (F_status_is_error(status)) return status; buffer->string[buffer->used] = 0; return F_none; } -#endif // _di_f_color_save_ +#endif // _di_f_color_save_5_ #ifdef __cplusplus } // extern "C" diff --git a/level_0/f_color/c/color.h b/level_0/f_color/c/color.h index 658bcef..baaa27e 100644 --- a/level_0/f_color/c/color.h +++ b/level_0/f_color/c/color.h @@ -13,18 +13,18 @@ #ifndef _F_color_h #define _F_color_h -// libc includes +// libc includes. #include #include #include -// fll-0 includes +// fll-0 includes. #include #include #include #include -// fll-0 color includes +// fll-0 color includes. #include #ifdef __cplusplus @@ -39,68 +39,198 @@ extern "C" { * * The default/fallback behavior is f_color_xterminal. * - * @param context - * The color context the load the color codes into. * @param use_light_colors * Set to F_true to use colors for light backgrounds. * Set to F_false to use colors for dark backgrounds. + * @param context + * The color context the load the color codes into. * * @return * F_none on success. * * F_parameter (with error bit) if a parameter is invalid. * - * Errors (with error bit) from: f_color_save() + * Errors (with error bit) from: f_color_save_1() + * Errors (with error bit) from: f_color_save_2() * - * @see f_color_save() + * @see f_color_save_1() + * @see f_color_save_2() */ #ifndef _di_f_color_load_context_ - extern f_status_t f_color_load_context(f_color_context_t *context, const bool use_light_colors); + extern f_status_t f_color_load_context(const bool use_light_colors, f_color_context_t * const context); #endif // _di_f_color_load_context_ /** - * Save color information to some string. + * Save color information to some string using one color code. + * + * This is environment sensitive. + * If TERM is not defined or set to "linux", then macro_f_color_t_set_linux() is used. + * Otherwise macro_f_color_t_set_xterminal() is used. + * + * @param format + * The color format parts. + * @param color1 + * A color to assign. + * @param buffer + * The string to save the colors to. + * The buffer will become NULL terminated after the appended string length. + * + * @return + * F_none on success. + * + * F_parameter (with error bit) if a parameter is invalid. * - * Up to 5 colors may be associted with a single color format block. + * Errors (with error bit) from: f_string_dynamic_append() + * Errors (with error bit) from: f_string_dynamic_increase_by() + * + * @see f_string_dynamic_append() + * @see f_string_dynamic_increase_by() + */ +#ifndef _di_f_color_save_1_ + extern f_status_t f_color_save_1(const f_color_format_t format, const f_string_static_t color1, f_string_dynamic_t * const buffer); +#endif // _di_f_color_save_1_ + +/** + * Save color information to some string using two color codes. * * This is environment sensitive. * If TERM is not defined or set to "linux", then macro_f_color_t_set_linux() is used. * Otherwise macro_f_color_t_set_xterminal() is used. * + * @param format + * The color format parts. + * @param color1 + * A color to assign. + * @param color2 + * A second color to assign. * @param buffer * The string to save the colors to. * The buffer will become NULL terminated after the appended string length. + * + * @return + * F_none on success. + * + * F_parameter (with error bit) if a parameter is invalid. + * + * Errors (with error bit) from: f_string_dynamic_append() + * Errors (with error bit) from: f_string_dynamic_increase_by() + * + * @see f_string_dynamic_append() + * @see f_string_dynamic_increase_by() + */ +#ifndef _di_f_color_save_2_ + extern f_status_t f_color_save_2(const f_color_format_t format, const f_string_static_t color1, const f_string_static_t color2, f_string_dynamic_t * const buffer); +#endif // _di_f_color_save_2_ + +/** + * Save color information to some string using three color codes. + * + * This is environment sensitive. + * If TERM is not defined or set to "linux", then macro_f_color_t_set_linux() is used. + * Otherwise macro_f_color_t_set_xterminal() is used. + * * @param format * The color format parts. * @param color1 - * A color to assign, set to NULL to not use. + * A color to assign. * @param color2 - * A color to assign, set to NULL to not use. + * A second color to assign. * @param color3 - * A color to assign, set to NULL to not use. + * A third color to assign. + * @param buffer + * The string to save the colors to. + * The buffer will become NULL terminated after the appended string length. + * + * @return + * F_none on success. + * + * F_parameter (with error bit) if a parameter is invalid. + * + * Errors (with error bit) from: f_string_dynamic_append() + * Errors (with error bit) from: f_string_dynamic_increase_by() + * + * @see f_string_dynamic_append() + * @see f_string_dynamic_increase_by() + */ +#ifndef _di_f_color_save_3_ + extern f_status_t f_color_save_3(const f_color_format_t format, const f_string_static_t color1, const f_string_static_t color2, const f_string_static_t color3, f_string_dynamic_t * const buffer); +#endif // _di_f_color_save_3_ + +/** + * Save color information to some string using four color codes. + * + * This is environment sensitive. + * If TERM is not defined or set to "linux", then macro_f_color_t_set_linux() is used. + * Otherwise macro_f_color_t_set_xterminal() is used. + * + * @param format + * The color format parts. + * @param color1 + * A color to assign. + * @param color2 + * A second color to assign. + * @param color3 + * A third color to assign. * @param color4 - * A color to assign, set to NULL to not use. - * @param color5 - * A color to assign, set to NULL to not use. + * A fourth color to assign. + * @param buffer + * The string to save the colors to. + * The buffer will become NULL terminated after the appended string length. * * @return * F_none on success. * * F_parameter (with error bit) if a parameter is invalid. * + * Errors (with error bit) from: f_string_dynamic_append() * Errors (with error bit) from: f_string_dynamic_increase_by() * + * @see f_string_dynamic_append() * @see f_string_dynamic_increase_by() */ -#ifndef _di_f_color_save_ - extern f_status_t f_color_save(f_string_dynamic_t * const buffer, const f_color_format_t format, const char *color1, const char *color2, const char *color3, const char *color4, const char *color5); +#ifndef _di_f_color_save_4_ + extern f_status_t f_color_save_4(const f_color_format_t format, const f_string_static_t color1, const f_string_static_t color2, const f_string_static_t color3, const f_string_static_t color4, f_string_dynamic_t * const buffer); +#endif // _di_f_color_save_4_ - #define macro_fl_color_save_1(buffer, format, color1) f_color_save(buffer, format, color1, 0, 0, 0, 0); - #define macro_fl_color_save_2(buffer, format, color1, color2) f_color_save(buffer, format, color1, color2, 0, 0, 0); - #define macro_fl_color_save_3(buffer, format, color1, color2, color3) f_color_save(buffer, format, color1, color2, color3, 0, 0); - #define macro_fl_color_save_4(buffer, format, color1, color2, color3, color4) f_color_save(buffer, format, color1, color2, color3, color4, 0); - #define macro_fl_color_save_5(buffer, format, color1, color2, color3, color4, color5) f_color_save(buffer, format, color1, color2, color3, color4, color5); -#endif // _di_f_color_save_ +/** + * Save color information to some string using five color codes. + * + * Five colors is the max that may be associated with a single color format block. + * + * This is environment sensitive. + * If TERM is not defined or set to "linux", then macro_f_color_t_set_linux() is used. + * Otherwise macro_f_color_t_set_xterminal() is used. + * + * @param format + * The color format parts. + * @param color1 + * A color to assign. + * @param color2 + * A second color to assign. + * @param color3 + * A third color to assign. + * @param color4 + * A fourth color to assign. + * @param color5 + * A fifth color to assign. + * @param buffer + * The string to save the colors to. + * The buffer will become NULL terminated after the appended string length. + * + * @return + * F_none on success. + * + * F_parameter (with error bit) if a parameter is invalid. + * + * Errors (with error bit) from: f_string_dynamic_append() + * Errors (with error bit) from: f_string_dynamic_increase_by() + * + * @see f_string_dynamic_append() + * @see f_string_dynamic_increase_by() + */ +#ifndef _di_f_color_save_5_ + extern f_status_t f_color_save_5(const f_color_format_t format, const f_string_static_t color1, const f_string_static_t color2, const f_string_static_t color3, const f_string_static_t color4, const f_string_static_t color5, f_string_dynamic_t * const buffer); +#endif // _di_f_color_save_5_ #ifdef __cplusplus } // extern "C" diff --git a/level_0/f_color/data/build/settings-tests b/level_0/f_color/data/build/settings-tests index 75ea8f1..e5c1f03 100644 --- a/level_0/f_color/data/build/settings-tests +++ b/level_0/f_color/data/build/settings-tests @@ -19,7 +19,7 @@ build_libraries -lc -lcmocka build_libraries-individual -lf_memory -lf_string -lf_color build_libraries-level -lfll_0 build_libraries-monolithic -lfll -build_sources_program test-color-load_context.c test-color-save.c test-color.c +build_sources_program test-color-load_context.c test-color-save_1.c test-color-save_2.c test-color-save_3.c test-color-save_4.c test-color-save_5.c test-color.c build_script no build_shared yes build_static no diff --git a/level_0/f_color/tests/c/test-color-load_context.c b/level_0/f_color/tests/c/test-color-load_context.c index ff9cdb3..5981117 100644 --- a/level_0/f_color/tests/c/test-color-load_context.c +++ b/level_0/f_color/tests/c/test-color-load_context.c @@ -9,7 +9,7 @@ extern "C" { void test__f_color_load_context__parameter_checking(void **state) { { - const f_status_t status = f_color_load_context(0, F_true); + const f_status_t status = f_color_load_context(F_true, 0); assert_int_equal(F_status_set_fine(status), F_parameter); } @@ -39,7 +39,7 @@ void test__f_color_load_context__works(void **state) { will_return(__wrap_getenv, i); will_return(__wrap_getenv, terms[j]); - const f_status_t status = f_color_load_context(&context, F_true); + const f_status_t status = f_color_load_context(F_true, &context); assert_int_equal(status, F_none); diff --git a/level_0/f_color/tests/c/test-color-save.c b/level_0/f_color/tests/c/test-color-save.c deleted file mode 100644 index 7c3f19e..0000000 --- a/level_0/f_color/tests/c/test-color-save.c +++ /dev/null @@ -1,150 +0,0 @@ -#include "test-color.h" -#include "test-color-save.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef _di_level_0_parameter_checking_ - void test__f_color_save__parameter_checking(void **state) { - - f_string_static_t buffer = f_string_static_t_initialize; - const f_color_format_t format = f_color_format_t_initialize; - const char *color = "color"; - - { - const f_status_t status = f_color_save(0, format, 0, 0, 0, 0, 0); - - assert_int_equal(F_status_set_fine(status), F_parameter); - } - - { - const f_status_t status = f_color_save(&buffer, format, 0, 0, 0, 0, 0); - - assert_int_equal(F_status_set_fine(status), F_parameter); - } - - { - const f_status_t status = f_color_save(&buffer, format, color, 0, color, 0, 0); - - assert_int_equal(F_status_set_fine(status), F_parameter); - } - - { - const f_status_t status = f_color_save(&buffer, format, color, 0, color, color, 0); - - assert_int_equal(F_status_set_fine(status), F_parameter); - } - - { - const f_status_t status = f_color_save(&buffer, format, color, 0, color, 0, color); - - assert_int_equal(F_status_set_fine(status), F_parameter); - } - - { - const f_status_t status = f_color_save(&buffer, format, color, 0, color, color, color); - - assert_int_equal(F_status_set_fine(status), F_parameter); - } - - { - const f_status_t status = f_color_save(&buffer, format, color, 0, 0, color, 0); - - assert_int_equal(F_status_set_fine(status), F_parameter); - } - - { - const f_status_t status = f_color_save(&buffer, format, color, 0, 0, 0, color); - - assert_int_equal(F_status_set_fine(status), F_parameter); - } - - { - const f_status_t status = f_color_save(&buffer, format, color, color, 0, color, 0); - - assert_int_equal(F_status_set_fine(status), F_parameter); - } - - { - const f_status_t status = f_color_save(&buffer, format, color, color, 0, 0, color); - - assert_int_equal(F_status_set_fine(status), F_parameter); - } - - { - const f_status_t status = f_color_save(&buffer, format, color, color, color, 0, color); - - assert_int_equal(F_status_set_fine(status), F_parameter); - } - } -#endif // _di_level_0_parameter_checking_ - -void test__f_color_save__works(void **state) { - - f_color_context_t context = f_color_context_t_initialize; - f_string_dynamic_t buffer = f_string_dynamic_t_initialize; - const f_color_format_t format = f_color_format_t_initialize; - const f_string_t color1 = "color1"; - const f_string_t color2 = "color2"; - const f_string_t color3 = "color3"; - const f_string_t color4 = "color4"; - const f_string_t color5 = "color5"; - - { - f_status_t status = F_none; - - macro_f_color_context_t_new(status, context); - } - - { - const f_status_t status = f_color_save(&buffer, format, color1, 0, 0, 0, 0); - - assert_int_equal(status, F_none); - assert_string_equal(buffer.string, "color1"); - } - - { - buffer.used = 0; - - const f_status_t status = f_color_save(&buffer, format, color1, color2, 0, 0, 0); - - assert_int_equal(status, F_none); - assert_string_equal(buffer.string, "color1color2"); - } - - { - buffer.used = 0; - - const f_status_t status = f_color_save(&buffer, format, color1, color2, color3, 0, 0); - - assert_int_equal(status, F_none); - assert_string_equal(buffer.string, "color1color2color3"); - } - - { - buffer.used = 0; - - const f_status_t status = f_color_save(&buffer, format, color1, color2, color3, color4, 0); - - assert_int_equal(status, F_none); - assert_string_equal(buffer.string, "color1color2color3color4"); - } - - { - buffer.used = 0; - - const f_status_t status = f_color_save(&buffer, format, color1, color2, color3, color4, color5); - - assert_int_equal(status, F_none); - assert_string_equal(buffer.string, "color1color2color3color4color5"); - } - - macro_f_color_context_t_delete_simple(context); - - f_string_dynamic_resize(0, &buffer); -} - -#ifdef __cplusplus -} // extern "C" -#endif diff --git a/level_0/f_color/tests/c/test-color-save.h b/level_0/f_color/tests/c/test-color-save.h deleted file mode 100644 index ead0f63..0000000 --- a/level_0/f_color/tests/c/test-color-save.h +++ /dev/null @@ -1,31 +0,0 @@ -/** - * FLL - Level 0 - * - * Project: Color - * API Version: 0.5 - * Licenses: lgpl-2.1-or-later - * - * Test the color project. - */ -#ifndef _TEST__F_color__save -#define _TEST__F_color__save - -// f_color_save() only returns failures by other functions that have their own tests. - -/** - * Test that parameter checking works as expected. - * - * @see f_color_save() - */ -#ifndef _di_level_0_parameter_checking_ - extern void test__f_color_save__parameter_checking(void **state); -#endif // _di_level_0_parameter_checking_ - -/** - * Test that function works. - * - * @see f_color_save() - */ -extern void test__f_color_save__works(void **state); - -#endif // _TEST__F_color__save diff --git a/level_0/f_color/tests/c/test-color-save_1.c b/level_0/f_color/tests/c/test-color-save_1.c new file mode 100644 index 0000000..b83f1a7 --- /dev/null +++ b/level_0/f_color/tests/c/test-color-save_1.c @@ -0,0 +1,50 @@ +#include "test-color.h" +#include "test-color-save_1.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _di_level_0_parameter_checking_ + void test__f_color_save_1_parameter_checking(void **state) { + + f_string_static_t buffer = f_string_static_t_initialize; + const f_color_format_t format = f_color_format_t_initialize; + const f_string_static_t color = macro_f_string_static_t_initialize("color", 5); + + { + const f_status_t status = f_color_save_1(format, color, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + } +#endif // _di_level_0_parameter_checking_ + +void test__f_color_save_1_works(void **state) { + + f_color_context_t context = f_color_context_t_initialize; + f_string_dynamic_t buffer = f_string_dynamic_t_initialize; + const f_color_format_t format = f_color_format_t_initialize; + const f_string_static_t color1 = macro_f_string_static_t_initialize("color1", 6); + + { + f_status_t status = F_none; + + macro_f_color_context_t_new(status, context); + } + + { + const f_status_t status = f_color_save_1(format, color1, &buffer); + + assert_int_equal(status, F_none); + assert_string_equal(buffer.string, "color1"); + } + + macro_f_color_context_t_delete_simple(context); + + f_string_dynamic_resize(0, &buffer); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_color/tests/c/test-color-save_1.h b/level_0/f_color/tests/c/test-color-save_1.h new file mode 100644 index 0000000..a681d7c --- /dev/null +++ b/level_0/f_color/tests/c/test-color-save_1.h @@ -0,0 +1,31 @@ +/** + * FLL - Level 0 + * + * Project: Color + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the color project. + */ +#ifndef _TEST__F_color__save_1 +#define _TEST__F_color__save_1 + +// f_color_save_1() only returns failures by other functions that have their own tests. + +/** + * Test that parameter checking works as expected. + * + * @see f_color_save_1() + */ +#ifndef _di_level_0_parameter_checking_ + extern void test__f_color_save_1_parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ + +/** + * Test that function works. + * + * @see f_color_save_1() + */ +extern void test__f_color_save_1_works(void **state); + +#endif // _TEST__F_color__save_1 diff --git a/level_0/f_color/tests/c/test-color-save_2.c b/level_0/f_color/tests/c/test-color-save_2.c new file mode 100644 index 0000000..d041db7 --- /dev/null +++ b/level_0/f_color/tests/c/test-color-save_2.c @@ -0,0 +1,51 @@ +#include "test-color.h" +#include "test-color-save_2.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _di_level_0_parameter_checking_ + void test__f_color_save_2_parameter_checking(void **state) { + + f_string_static_t buffer = f_string_static_t_initialize; + const f_color_format_t format = f_color_format_t_initialize; + const f_string_static_t color = macro_f_string_static_t_initialize("color", 5); + + { + const f_status_t status = f_color_save_2(format, color, color, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + } +#endif // _di_level_0_parameter_checking_ + +void test__f_color_save_2_works(void **state) { + + f_color_context_t context = f_color_context_t_initialize; + f_string_dynamic_t buffer = f_string_dynamic_t_initialize; + const f_color_format_t format = f_color_format_t_initialize; + const f_string_static_t color1 = macro_f_string_static_t_initialize("color1", 6); + const f_string_static_t color2 = macro_f_string_static_t_initialize("color2", 6); + + { + f_status_t status = F_none; + + macro_f_color_context_t_new(status, context); + } + + { + const f_status_t status = f_color_save_2(format, color1, color2, &buffer); + + assert_int_equal(status, F_none); + assert_string_equal(buffer.string, "color1color2"); + } + + macro_f_color_context_t_delete_simple(context); + + f_string_dynamic_resize(0, &buffer); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_color/tests/c/test-color-save_2.h b/level_0/f_color/tests/c/test-color-save_2.h new file mode 100644 index 0000000..b17b205 --- /dev/null +++ b/level_0/f_color/tests/c/test-color-save_2.h @@ -0,0 +1,31 @@ +/** + * FLL - Level 0 + * + * Project: Color + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the color project. + */ +#ifndef _TEST__F_color__save_2 +#define _TEST__F_color__save_2 + +// f_color_save_2() only returns failures by other functions that have their own tests. + +/** + * Test that parameter checking works as expected. + * + * @see f_color_save_2() + */ +#ifndef _di_level_0_parameter_checking_ + extern void test__f_color_save_2_parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ + +/** + * Test that function works. + * + * @see f_color_save_2() + */ +extern void test__f_color_save_2_works(void **state); + +#endif // _TEST__F_color__save_2 diff --git a/level_0/f_color/tests/c/test-color-save_3.c b/level_0/f_color/tests/c/test-color-save_3.c new file mode 100644 index 0000000..6e1aafb --- /dev/null +++ b/level_0/f_color/tests/c/test-color-save_3.c @@ -0,0 +1,52 @@ +#include "test-color.h" +#include "test-color-save_3.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _di_level_0_parameter_checking_ + void test__f_color_save_3_parameter_checking(void **state) { + + f_string_static_t buffer = f_string_static_t_initialize; + const f_color_format_t format = f_color_format_t_initialize; + const f_string_static_t color = macro_f_string_static_t_initialize("color", 5); + + { + const f_status_t status = f_color_save_3(format, color, color, color, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + } +#endif // _di_level_0_parameter_checking_ + +void test__f_color_save_3_works(void **state) { + + f_color_context_t context = f_color_context_t_initialize; + f_string_dynamic_t buffer = f_string_dynamic_t_initialize; + const f_color_format_t format = f_color_format_t_initialize; + const f_string_static_t color1 = macro_f_string_static_t_initialize("color1", 6); + const f_string_static_t color2 = macro_f_string_static_t_initialize("color2", 6); + const f_string_static_t color3 = macro_f_string_static_t_initialize("color3", 6); + + { + f_status_t status = F_none; + + macro_f_color_context_t_new(status, context); + } + + { + const f_status_t status = f_color_save_3(format, color1, color2, color3, &buffer); + + assert_int_equal(status, F_none); + assert_string_equal(buffer.string, "color1color2color3"); + } + + macro_f_color_context_t_delete_simple(context); + + f_string_dynamic_resize(0, &buffer); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_color/tests/c/test-color-save_3.h b/level_0/f_color/tests/c/test-color-save_3.h new file mode 100644 index 0000000..135554f --- /dev/null +++ b/level_0/f_color/tests/c/test-color-save_3.h @@ -0,0 +1,31 @@ +/** + * FLL - Level 0 + * + * Project: Color + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the color project. + */ +#ifndef _TEST__F_color__save_3 +#define _TEST__F_color__save_3 + +// f_color_save_3() only returns failures by other functions that have their own tests. + +/** + * Test that parameter checking works as expected. + * + * @see f_color_save_3() + */ +#ifndef _di_level_0_parameter_checking_ + extern void test__f_color_save_3_parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ + +/** + * Test that function works. + * + * @see f_color_save_3() + */ +extern void test__f_color_save_3_works(void **state); + +#endif // _TEST__F_color__save_3 diff --git a/level_0/f_color/tests/c/test-color-save_4.c b/level_0/f_color/tests/c/test-color-save_4.c new file mode 100644 index 0000000..d85b4d9 --- /dev/null +++ b/level_0/f_color/tests/c/test-color-save_4.c @@ -0,0 +1,53 @@ +#include "test-color.h" +#include "test-color-save_4.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _di_level_0_parameter_checking_ + void test__f_color_save_4_parameter_checking(void **state) { + + f_string_static_t buffer = f_string_static_t_initialize; + const f_color_format_t format = f_color_format_t_initialize; + const f_string_static_t color = macro_f_string_static_t_initialize("color", 5); + + { + const f_status_t status = f_color_save_4(format, color, color, color, color, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + } +#endif // _di_level_0_parameter_checking_ + +void test__f_color_save_4_works(void **state) { + + f_color_context_t context = f_color_context_t_initialize; + f_string_dynamic_t buffer = f_string_dynamic_t_initialize; + const f_color_format_t format = f_color_format_t_initialize; + const f_string_static_t color1 = macro_f_string_static_t_initialize("color1", 6); + const f_string_static_t color2 = macro_f_string_static_t_initialize("color2", 6); + const f_string_static_t color3 = macro_f_string_static_t_initialize("color3", 6); + const f_string_static_t color4 = macro_f_string_static_t_initialize("color4", 6); + + { + f_status_t status = F_none; + + macro_f_color_context_t_new(status, context); + } + + { + const f_status_t status = f_color_save_4(format, color1, color2, color3, color4, &buffer); + + assert_int_equal(status, F_none); + assert_string_equal(buffer.string, "color1color2color3color4"); + } + + macro_f_color_context_t_delete_simple(context); + + f_string_dynamic_resize(0, &buffer); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_color/tests/c/test-color-save_4.h b/level_0/f_color/tests/c/test-color-save_4.h new file mode 100644 index 0000000..1adf336 --- /dev/null +++ b/level_0/f_color/tests/c/test-color-save_4.h @@ -0,0 +1,31 @@ +/** + * FLL - Level 0 + * + * Project: Color + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the color project. + */ +#ifndef _TEST__F_color__save_4 +#define _TEST__F_color__save_4 + +// f_color_save_4() only returns failures by other functions that have their own tests. + +/** + * Test that parameter checking works as expected. + * + * @see f_color_save_4() + */ +#ifndef _di_level_0_parameter_checking_ + extern void test__f_color_save_4_parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ + +/** + * Test that function works. + * + * @see f_color_save_4() + */ +extern void test__f_color_save_4_works(void **state); + +#endif // _TEST__F_color__save_4 diff --git a/level_0/f_color/tests/c/test-color-save_5.c b/level_0/f_color/tests/c/test-color-save_5.c new file mode 100644 index 0000000..81444ec --- /dev/null +++ b/level_0/f_color/tests/c/test-color-save_5.c @@ -0,0 +1,54 @@ +#include "test-color.h" +#include "test-color-save_5.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _di_level_0_parameter_checking_ + void test__f_color_save_5_parameter_checking(void **state) { + + f_string_static_t buffer = f_string_static_t_initialize; + const f_color_format_t format = f_color_format_t_initialize; + const f_string_static_t color = macro_f_string_static_t_initialize("color", 5); + + { + const f_status_t status = f_color_save_5(format, color, color, color, color, color, 0); + + assert_int_equal(F_status_set_fine(status), F_parameter); + } + } +#endif // _di_level_0_parameter_checking_ + +void test__f_color_save_5_works(void **state) { + + f_color_context_t context = f_color_context_t_initialize; + f_string_dynamic_t buffer = f_string_dynamic_t_initialize; + const f_color_format_t format = f_color_format_t_initialize; + const f_string_static_t color1 = macro_f_string_static_t_initialize("color1", 6); + const f_string_static_t color2 = macro_f_string_static_t_initialize("color2", 6); + const f_string_static_t color3 = macro_f_string_static_t_initialize("color3", 6); + const f_string_static_t color4 = macro_f_string_static_t_initialize("color4", 6); + const f_string_static_t color5 = macro_f_string_static_t_initialize("color5", 6); + + { + f_status_t status = F_none; + + macro_f_color_context_t_new(status, context); + } + + { + const f_status_t status = f_color_save_5(format, color1, color2, color3, color4, color5, &buffer); + + assert_int_equal(status, F_none); + assert_string_equal(buffer.string, "color1color2color3color4color5"); + } + + macro_f_color_context_t_delete_simple(context); + + f_string_dynamic_resize(0, &buffer); +} + +#ifdef __cplusplus +} // extern "C" +#endif diff --git a/level_0/f_color/tests/c/test-color-save_5.h b/level_0/f_color/tests/c/test-color-save_5.h new file mode 100644 index 0000000..fad16f1 --- /dev/null +++ b/level_0/f_color/tests/c/test-color-save_5.h @@ -0,0 +1,31 @@ +/** + * FLL - Level 0 + * + * Project: Color + * API Version: 0.5 + * Licenses: lgpl-2.1-or-later + * + * Test the color project. + */ +#ifndef _TEST__F_color__save_5 +#define _TEST__F_color__save_5 + +// f_color_save_5() only returns failures by other functions that have their own tests. + +/** + * Test that parameter checking works as expected. + * + * @see f_color_save_5() + */ +#ifndef _di_level_0_parameter_checking_ + extern void test__f_color_save_5_parameter_checking(void **state); +#endif // _di_level_0_parameter_checking_ + +/** + * Test that function works. + * + * @see f_color_save_5() + */ +extern void test__f_color_save_5_works(void **state); + +#endif // _TEST__F_color__save_5 diff --git a/level_0/f_color/tests/c/test-color.c b/level_0/f_color/tests/c/test-color.c index 6aeae66..3eb6d5d 100644 --- a/level_0/f_color/tests/c/test-color.c +++ b/level_0/f_color/tests/c/test-color.c @@ -19,15 +19,32 @@ int setdown(void **state) { int main(void) { const struct CMUnitTest tests[] = { + // f_color_load_context() only returns failures by other functions that have their own tests. cmocka_unit_test(test__f_color_load_context__works), - // f_color_save() only returns failures by other functions that have their own tests. - cmocka_unit_test(test__f_color_save__works), + // f_color_save_1() only returns failures by other functions that have their own tests. + cmocka_unit_test(test__f_color_save_1_works), + + // f_color_save_2() only returns failures by other functions that have their own tests. + cmocka_unit_test(test__f_color_save_2_works), + + // f_color_save_3() only returns failures by other functions that have their own tests. + cmocka_unit_test(test__f_color_save_3_works), + + // f_color_save_4() only returns failures by other functions that have their own tests. + cmocka_unit_test(test__f_color_save_4_works), + + // f_color_save_5() only returns failures by other functions that have their own tests. + cmocka_unit_test(test__f_color_save_5_works), #ifndef _di_level_0_parameter_checking_ cmocka_unit_test(test__f_color_load_context__parameter_checking), - cmocka_unit_test(test__f_color_save__parameter_checking), + cmocka_unit_test(test__f_color_save_1_parameter_checking), + cmocka_unit_test(test__f_color_save_2_parameter_checking), + cmocka_unit_test(test__f_color_save_3_parameter_checking), + cmocka_unit_test(test__f_color_save_4_parameter_checking), + cmocka_unit_test(test__f_color_save_5_parameter_checking), #endif // _di_level_0_parameter_checking_ }; diff --git a/level_0/f_color/tests/c/test-color.h b/level_0/f_color/tests/c/test-color.h index dab50a5..0bd48da 100644 --- a/level_0/f_color/tests/c/test-color.h +++ b/level_0/f_color/tests/c/test-color.h @@ -27,7 +27,11 @@ // test includes. #include "test-color-load_context.h" -#include "test-color-save.h" +#include "test-color-save_1.h" +#include "test-color-save_2.h" +#include "test-color-save_3.h" +#include "test-color-save_4.h" +#include "test-color-save_5.h" #ifdef __cplusplus extern "C" { diff --git a/level_1/fl_print/c/private-print.c b/level_1/fl_print/c/private-print.c index 7df219b..8257b72 100644 --- a/level_1/fl_print/c/private-print.c +++ b/level_1/fl_print/c/private-print.c @@ -424,7 +424,7 @@ extern "C" { const f_color_set_t value = va_arg(*ap, f_color_set_t); if (value.before) { - *status = f_print_terminated(value.before->string, stream); + *status = f_print_dynamic(*value.before, stream); } return string; @@ -433,7 +433,7 @@ extern "C" { const f_color_set_t value = va_arg(*ap, f_color_set_t); if (value.after) { - *status = f_print_terminated(value.after->string, stream); + *status = f_print_dynamic(*value.after, stream); } return string; diff --git a/level_2/fll_program/c/program.c b/level_2/fll_program/c/program.c index b13ca9e..aefba8c 100644 --- a/level_2/fll_program/c/program.c +++ b/level_2/fll_program/c/program.c @@ -96,7 +96,7 @@ extern "C" { if (F_status_is_error(status)) return status; - // load colors unless told not to. + // Load colors unless told not to. if (decision == choices.id[0]) { context->mode = F_color_mode_no_color_d; } @@ -106,7 +106,7 @@ extern "C" { macro_f_color_context_t_new(allocation_status, (*context)); if (F_status_is_error(status)) return status; - status = f_color_load_context(context, decision == choices.id[1]); + status = f_color_load_context(decision == choices.id[1], context); } return status;