Dqn/Standalone/dqn_cpp_file.h

267 lines
10 KiB
C
Raw Normal View History

2024-08-01 13:34:36 +10:00
/*
////////////////////////////////////////////////////////////////////////////////
//
// $$$$$$\ $$$$$$$\ $$$$$$$\ $$$$$$$$\ $$$$$$\ $$\ $$$$$$$$\
// $$ __$$\ $$ __$$\ $$ __$$\ $$ _____|\_$$ _|$$ | $$ _____|
// $$ / \__|$$ | $$ |$$ | $$ | $$ | $$ | $$ | $$ |
// $$ | $$$$$$$ |$$$$$$$ | $$$$$\ $$ | $$ | $$$$$\
// $$ | $$ ____/ $$ ____/ $$ __| $$ | $$ | $$ __|
// $$ | $$\ $$ | $$ | $$ | $$ | $$ | $$ |
// \$$$$$$ |$$ | $$ | $$ | $$$$$$\ $$$$$$$$\ $$$$$$$$\
// \______/ \__| \__| \__| \______|\________|\________|
//
2025-02-14 00:27:42 +11:00
// dn_cpp_file.h -- Functions to emit C++ formatted code
//
////////////////////////////////////////////////////////////////////////////////
2024-08-01 13:34:36 +10:00
*/
2025-02-14 00:27:42 +11:00
#if !defined(DN_CPP_FILE_H)
#define DN_CPP_FILE_H
#include <stdio.h> /// printf, fputc
#include <stdarg.h> /// va_list...
#include <assert.h> /// assert
2025-02-14 00:27:42 +11:00
typedef struct DN_CppFile { ///< Maintains state for printing C++ style formatted files
FILE *file; ///< (Write) File to print to
int indent; ///< Current indent level of the printer
int space_per_indent; ///< (Write) Number of spaces per indent
unsigned char if_chain[256]; ///
unsigned char if_chain_size; ///
2025-02-14 00:27:42 +11:00
} DN_CppFile;
2025-02-14 00:27:42 +11:00
#define DN_CppSpacePerIndent(cpp) ((cpp) && (cpp)->space_per_indent) ? ((cpp)->space_per_indent) : 4
/// Print the format string indented and terminate the string with a new-line.
2025-02-14 00:27:42 +11:00
void DN_CppLineV(DN_CppFile *cpp, char const *fmt, va_list args);
void DN_CppLine(DN_CppFile *cpp, char const *fmt, ...);
/// Print the format string indented
2025-02-14 00:27:42 +11:00
void DN_CppPrintV(DN_CppFile *cpp, char const *fmt, va_list args);
void DN_CppPrint(DN_CppFile *cpp, char const *fmt, ...);
/// Print the format string
2025-02-14 00:27:42 +11:00
#define DN_CppAppend(cpp, fmt, ...) fprintf((cpp)->file, fmt, ##__VA_ARGS__)
#define DN_CppAppendV(cpp, fmt, args) vfprintf((cpp)->file, fmt, args)
2023-04-10 13:21:57 +10:00
/// End the current line, useful after CppPrint and CppAppend
2025-02-14 00:27:42 +11:00
#define DN_CppNewLine(cpp) fputc('\n', (cpp)->file)
/// Manually modify the indent level
2025-02-14 00:27:42 +11:00
#define DN_CppIndent(cpp) (cpp)->indent++
#define DN_CppUnindent(cpp) (cpp)->indent--; assert((cpp)->indent >= 0)
2023-04-10 13:21:57 +10:00
/// Block scope functions that execute a function on entry and exit of the
/// scope by exploiting the comma operator and a for loop.
///
/// @code
2025-02-14 00:27:42 +11:00
/// DN_CppEnumBlock(cpp, "abc") {
2023-04-10 13:21:57 +10:00
/// printf("Hello world!");
/// }
///
/// // Is equivalent to
///
2025-02-14 00:27:42 +11:00
/// DN_CppBeginBlock(cpp, "abc");
2023-04-10 13:21:57 +10:00
/// printf("Hello world!");
2025-02-14 00:27:42 +11:00
/// DN_CppEndEnumBlock(cpp);
2023-04-10 13:21:57 +10:00
/// @endcode
2025-02-14 00:27:42 +11:00
#define DN_CppEnumBlock(cpp, fmt, ...) \
for (bool DN_CPP_TOKEN_PASTE_(once_, __LINE__) = \
(DN_CppBeginEnumBlock(cpp, fmt, ##__VA_ARGS__), true); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__) = (DN_CppEndEnumBlock(cpp), false))
#define DN_CppForBlock(cpp, fmt, ...) \
for (bool DN_CPP_TOKEN_PASTE_(once_, __LINE__) = \
(DN_CppBeginForBlock(cpp, fmt, ##__VA_ARGS__), true); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__) = (DN_CppEndForBlock(cpp), false))
#define DN_CppWhileBlock(cpp, fmt, ...) \
for (bool DN_CPP_TOKEN_PASTE_(once_, __LINE__) = \
(DN_CppBeginWhileBlock(cpp, fmt, ##__VA_ARGS__), true); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__) = (DN_CppEndWhileBlock(cpp), false))
#define DN_CppIfOrElseIfBlock(cpp, fmt, ...) \
for (bool DN_CPP_TOKEN_PASTE_(once_, __LINE__) = \
(DN_CppBeginIfOrElseIfBlock(cpp, fmt, ##__VA_ARGS__), true); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__) = (DN_CppEndIfOrElseIfBlock(cpp), false))
#define DN_CppElseBlock(cpp) \
for (bool DN_CPP_TOKEN_PASTE_(once_, __LINE__) = \
(DN_CppBeginElseBlock(cpp), true); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__) = (DN_CppEndElseBlock(cpp), false))
#define DN_CppFuncBlock(cpp, fmt, ...) \
for (bool DN_CPP_TOKEN_PASTE_(once_, __LINE__) = \
(DN_CppBeginFuncBlock(cpp, fmt, ##__VA_ARGS__), true); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__) = (DN_CppEndFuncBlock(cpp), false))
#define DN_CppStructBlock(cpp, fmt, ...) \
for (bool DN_CPP_TOKEN_PASTE_(once_, __LINE__) = \
(DN_CppBeginStructBlock(cpp, fmt, ##__VA_ARGS__), true); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__) = (DN_CppEndStructBlock(cpp), false))
#define DN_CppSwitchBlock(cpp, fmt, ...) \
for (bool DN_CPP_TOKEN_PASTE_(once_, __LINE__) = \
(DN_CppBeginSwitchBlock(cpp, fmt, ##__VA_ARGS__), true); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__) = (DN_CppEndSwitchBlock(cpp), false))
#define DN_CppBlock(cpp, ending, fmt, ...) \
for (bool DN_CPP_TOKEN_PASTE_(once_, __LINE__) = \
(DN_CppBeginBlock(cpp, false /*append*/, fmt, ##__VA_ARGS__), true); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__) = (DN_CppEndBlock(cpp, ending), false))
#define DN_CppIfChain(cpp) \
for (bool DN_CPP_TOKEN_PASTE_(once_, __LINE__) = (DN_CppBeginIfChain(cpp), true); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__); \
DN_CPP_TOKEN_PASTE_(once_, __LINE__) = (DN_CppEndIfChain(cpp), false))
/// Print the format string followed by a "{" and enter a new line whilst
/// increasing the indent level after the brace.
2025-02-14 00:27:42 +11:00
void DN_CppBeginBlock (DN_CppFile *cpp, bool append, char const *fmt, ...);
void DN_CppBeginBlockV(DN_CppFile *cpp, bool append, char const *fmt, va_list args);
void DN_CppEndBlock (DN_CppFile *cpp, char const *ending);
2023-04-10 13:21:57 +10:00
/// Begin/End a block, specifically for the following language constructs.
2025-02-14 00:27:42 +11:00
#define DN_CppBeginEnumBlock(cpp, fmt, ...) DN_CppBeginBlock(cpp, false /*append*/, "enum " fmt, ##__VA_ARGS__)
#define DN_CppEndEnumBlock(cpp) DN_CppEndBlock(cpp, ";\n")
2023-04-10 13:21:57 +10:00
2025-02-14 00:27:42 +11:00
#define DN_CppBeginWhileBlock(cpp, fmt, ...) DN_CppBeginBlock(cpp, false /*append*/, "while (" fmt ")", ##__VA_ARGS__)
#define DN_CppEndWhileBlock(cpp) DN_CppEndBlock(cpp, "\n")
2025-02-14 00:27:42 +11:00
#define DN_CppBeginForBlock(cpp, fmt, ...) DN_CppBeginBlock(cpp, false /*append*/, "for (" fmt ")", ##__VA_ARGS__)
#define DN_CppEndForBlock(cpp) DN_CppEndBlock(cpp, "\n")
2023-04-10 13:21:57 +10:00
2025-02-14 00:27:42 +11:00
#define DN_CppBeginFuncBlock(cpp, fmt, ...) DN_CppBeginBlock(cpp, false /*append*/, fmt, ##__VA_ARGS__)
#define DN_CppEndFuncBlock(cpp) DN_CppEndBlock(cpp, "\n")
2023-04-10 13:21:57 +10:00
2025-02-14 00:27:42 +11:00
#define DN_CppBeginStructBlock(cpp, fmt, ...) DN_CppBeginBlock(cpp, false /*append*/, "struct " fmt, ##__VA_ARGS__)
#define DN_CppEndStructBlock(cpp) DN_CppEndBlock(cpp, ";\n")
2023-04-10 13:21:57 +10:00
2025-02-14 00:27:42 +11:00
#define DN_CppBeginSwitchBlock(cpp, fmt, ...) DN_CppBeginBlock(cpp, false /*append*/, "switch (" fmt ")", ##__VA_ARGS__)
#define DN_CppEndSwitchBlock(cpp) DN_CppEndBlock(cpp, "\n")
2023-04-10 13:21:57 +10:00
2025-02-14 00:27:42 +11:00
void DN_CppBeginIfOrElseIfBlock (DN_CppFile *cpp, char const *fmt, ...);
#define DN_CppEndIfOrElseIfBlock(cpp) DN_CppEndBlock(cpp, "")
2023-04-10 13:21:57 +10:00
2025-02-14 00:27:42 +11:00
void DN_CppBeginElseBlock (DN_CppFile *cpp);
void DN_CppEndElseBlock (DN_CppFile *cpp);
2023-04-10 13:21:57 +10:00
2025-02-14 00:27:42 +11:00
#define DN_CPP_TOKEN_PASTE2_(x, y) x ## y
#define DN_CPP_TOKEN_PASTE_(x, y) DN_CPP_TOKEN_PASTE2_(x, y)
#endif // DN_CPP_FILE_H
2025-02-14 00:27:42 +11:00
#if defined(DN_CPP_FILE_IMPLEMENTATION)
void DN_CppLineV(DN_CppFile *cpp, char const *fmt, va_list args)
{
2025-02-14 00:27:42 +11:00
DN_CppPrintV(cpp, fmt, args);
DN_CppNewLine(cpp);
}
2025-02-14 00:27:42 +11:00
void DN_CppLine(DN_CppFile *cpp, char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
2025-02-14 00:27:42 +11:00
DN_CppLineV(cpp, fmt, args);
va_end(args);
}
2025-02-14 00:27:42 +11:00
void DN_CppPrintV(DN_CppFile *cpp, char const *fmt, va_list args)
{
2025-02-14 00:27:42 +11:00
int space_per_indent = DN_CppSpacePerIndent(cpp);
int spaces = fmt ? (cpp->indent * space_per_indent) : 0;
fprintf(cpp->file, "%*s", spaces, "");
vfprintf(cpp->file, fmt, args);
}
2025-02-14 00:27:42 +11:00
void DN_CppPrint(DN_CppFile *cpp, char const *fmt, ...)
2021-09-24 22:29:34 +10:00
{
va_list args;
va_start(args, fmt);
2025-02-14 00:27:42 +11:00
DN_CppPrintV(cpp, fmt, args);
2021-09-24 22:29:34 +10:00
va_end(args);
}
2025-02-14 00:27:42 +11:00
void DN_CppBeginBlock(DN_CppFile *cpp, bool append, char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
2025-02-14 00:27:42 +11:00
DN_CppBeginBlockV(cpp, append, fmt, args);
va_end(args);
}
2025-02-14 00:27:42 +11:00
void DN_CppBeginBlockV(DN_CppFile *cpp, bool append, char const *fmt, va_list args)
{
2023-04-10 13:21:57 +10:00
if (append)
2025-02-14 00:27:42 +11:00
DN_CppAppendV(cpp, fmt, args);
2023-04-10 13:21:57 +10:00
else
2025-02-14 00:27:42 +11:00
DN_CppPrintV(cpp, fmt, args);
bool empty_fmt = fmt == nullptr || strlen(fmt) == 0;
2025-02-14 00:27:42 +11:00
DN_CppAppend(cpp, "%s{\n", empty_fmt ? "" : " ");
DN_CppIndent(cpp);
}
2023-04-10 13:21:57 +10:00
2025-02-14 00:27:42 +11:00
void DN_CppEndBlock(DN_CppFile *cpp, char const *ending)
2023-04-10 13:21:57 +10:00
{
2025-02-14 00:27:42 +11:00
DN_CppUnindent(cpp);
DN_CppPrint(cpp, "}%s", ending);
2023-04-10 13:21:57 +10:00
}
2025-02-14 00:27:42 +11:00
void DN_CppBeginIfOrElseIfBlock(DN_CppFile *cpp, char const *fmt, ...)
2023-04-10 13:21:57 +10:00
{
va_list args;
va_start(args, fmt);
assert(cpp->if_chain_size);
2023-04-10 13:21:57 +10:00
if (cpp->if_chain[cpp->if_chain_size - 1] == 0)
2025-02-14 00:27:42 +11:00
DN_CppPrint(cpp, "if");
2023-04-10 13:21:57 +10:00
else
2025-02-14 00:27:42 +11:00
DN_CppAppend(cpp, " else if");
2023-04-10 13:21:57 +10:00
2025-02-14 00:27:42 +11:00
DN_CppAppend(cpp, " (");
DN_CppAppendV(cpp, fmt, args);
DN_CppAppend(cpp, ") {\n");
DN_CppIndent(cpp);
2023-04-10 13:21:57 +10:00
va_end(args);
cpp->if_chain[cpp->if_chain_size - 1]++;
}
2025-02-14 00:27:42 +11:00
void DN_CppBeginElseBlock(DN_CppFile *cpp)
2023-04-10 13:21:57 +10:00
{
assert(cpp->if_chain_size);
2023-04-10 13:21:57 +10:00
if (cpp->if_chain[cpp->if_chain_size - 1] >= 1)
2025-02-14 00:27:42 +11:00
DN_CppBeginBlock(cpp, true /*append*/, " else");
2023-04-10 13:21:57 +10:00
}
2025-02-14 00:27:42 +11:00
void DN_CppEndElseBlock(DN_CppFile *cpp)
{
if (cpp->if_chain[cpp->if_chain_size - 1] >= 1)
2025-02-14 00:27:42 +11:00
DN_CppEndBlock(cpp, "");
}
2025-02-14 00:27:42 +11:00
void DN_CppBeginIfChain(DN_CppFile *cpp)
2023-04-10 13:21:57 +10:00
{
assert(cpp->if_chain_size < sizeof(cpp->if_chain)/sizeof(cpp->if_chain[0]));
cpp->if_chain_size++;
}
2025-02-14 00:27:42 +11:00
void DN_CppEndIfChain(DN_CppFile *cpp)
2023-04-10 13:21:57 +10:00
{
assert(cpp->if_chain_size);
if (cpp->if_chain[cpp->if_chain_size - 1] >= 1) {
2025-02-14 00:27:42 +11:00
DN_CppNewLine(cpp);
}
cpp->if_chain[cpp->if_chain_size - 1] = 0;
cpp->if_chain_size--;
2023-04-10 13:21:57 +10:00
}
2025-02-14 00:27:42 +11:00
#endif // DN_CPP_FILE_IMPLEMENTATION