From afa331fdf3cff19500934e143aed226cfef097e4 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Tue, 20 Nov 2018 02:21:51 -0500 Subject: [PATCH] Create variadic macro spike --- .gitignore | 1 + spikes/variadic_macros/makefile | 3 ++ spikes/variadic_macros/readme.md | 5 +++ spikes/variadic_macros/variadic_macro.c | 30 +++++++++++++ spikes/variadic_macros/variadic_macro.h | 58 +++++++++++++++++++++++++ 5 files changed, 97 insertions(+) create mode 100644 spikes/variadic_macros/makefile create mode 100644 spikes/variadic_macros/readme.md create mode 100644 spikes/variadic_macros/variadic_macro.c create mode 100644 spikes/variadic_macros/variadic_macro.h diff --git a/.gitignore b/.gitignore index 4cdb467..3dd39fe 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ spikes/logging/log_with_macro spikes/logging/log_file.txt spikes/make/read_from_file spikes/unit_testing/unit_tests +spikes/variadic_macros/variadic_macro # Ignore vscode files .vscode diff --git a/spikes/variadic_macros/makefile b/spikes/variadic_macros/makefile new file mode 100644 index 0000000..7274de3 --- /dev/null +++ b/spikes/variadic_macros/makefile @@ -0,0 +1,3 @@ + +all: + gcc -Wall -Wpedantic -std=c99 -g *.c -o variadic_macro -lcunit diff --git a/spikes/variadic_macros/readme.md b/spikes/variadic_macros/readme.md new file mode 100644 index 0000000..dadbaac --- /dev/null +++ b/spikes/variadic_macros/readme.md @@ -0,0 +1,5 @@ + +# C - Quadratic Solver > Spikes > Variadic Macros + +## Description +Spikes to test use of variable length input for macros (variadic macros). diff --git a/spikes/variadic_macros/variadic_macro.c b/spikes/variadic_macros/variadic_macro.c new file mode 100644 index 0000000..e4edac1 --- /dev/null +++ b/spikes/variadic_macros/variadic_macro.c @@ -0,0 +1,30 @@ +/** + * CS 4900 + * Variadic Macro spike. + */ + + +/** + * Description: + * Spike to test creating macros with dynamic number of passed args. + */ + + +// Import headers. +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "variadic_macro.h" + + + +/** + * Program's main. + * Initializes and runs program. + */ +int main(int argc, char* argv[]) { + va_printf("%s\n", "Hello world!"); + va_printf("%s %s\n", "Hello", "world!"); + va_printf("%s %d %d\n", "Nov", 20, 2018); + exit(0); +} diff --git a/spikes/variadic_macros/variadic_macro.h b/spikes/variadic_macros/variadic_macro.h new file mode 100644 index 0000000..d02d6c0 --- /dev/null +++ b/spikes/variadic_macros/variadic_macro.h @@ -0,0 +1,58 @@ +/** + * CS 4900 + * Header file for Variadic Macro spike. + */ + + +/** + * Description: + * Header file to hold variadic macro. + */ + + +// Import headers. +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + + +// Function Prototypes. +void va_printf_func (const char *source_file_name, + int source_line_num, + const char *source_format_string, + ...); + + +// Macro definition. +#define va_printf(source_format_string, ...) \ + va_printf_func (__FILE__, __LINE__, source_format_string, __VA_ARGS__) + + +/** + * Function called by va_printf macro. Used for printing output of unknown length args. + * Notes: + * * It seems to just segfault if you use a bad/wrong format string specifier. + * Unlike normal printf, it will not give detailed warnings/errors about such. + * * Due to how va_list and va_start work, calling macro needs at least one variable defined. + * This variable is where the __VA_ARGS__ call will start looking for new args to handle. + */ +void va_printf_func(const char *source_file_name, + int source_line_num, + const char *source_format_string, + ...) { + // Create initial formatted string, with default formatting. + char* format_string = calloc(1024, sizeof(char*)); + sprintf(format_string, "[%s %d] %s", source_file_name, source_line_num, source_format_string); + + // Handle variable length arg input. + va_list va_arglist; + va_start(va_arglist, source_format_string); + + // Format string with variable length values. + vprintf(format_string, va_arglist); + + // Clean up. + va_end(va_arglist); + free(format_string); +} -- GitLab