diff --git a/.gitignore b/.gitignore index 4cdb46707f622e46a93208d5e20fe4a9d2161a74..3dd39fe45e042ce69bdcd19e5ca9032371190c96 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 0000000000000000000000000000000000000000..7274de3c89bf18aeb1652922b1b56dd8ad69637b --- /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 0000000000000000000000000000000000000000..dadbaacbb9414f4994e8119a2c08e216af0852ef --- /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 0000000000000000000000000000000000000000..e4edac112ffedc1e73080145dc3616c22b8f9e15 --- /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 0000000000000000000000000000000000000000..d02d6c048cd5913c3df86b213b984019c30a6091 --- /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); +}