2021-09-19 11:49:56 +00:00
|
|
|
#if !defined(DQN_CPP_FILE_H)
|
|
|
|
#define DQN_CPP_FILE_H
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// NOTE: Dqn_CppFile: Helper functions to generate formatted CPP files
|
|
|
|
// -----------------------------------------------------------------------------
|
2022-06-28 14:08:35 +00:00
|
|
|
#define DQN_CPPF_ASSERT(expr) do { if (!(expr)) { *((int volatile *)0) = 0; } } while (0)
|
2021-09-19 11:49:56 +00:00
|
|
|
|
|
|
|
struct Dqn_CppFile
|
|
|
|
{
|
2021-09-24 12:29:34 +00:00
|
|
|
FILE *file;
|
|
|
|
int indent;
|
|
|
|
int space_per_indent;
|
|
|
|
bool append_extra_new_line;
|
2021-09-19 11:49:56 +00:00
|
|
|
};
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
int Dqn_CppFSpacePerIndent(Dqn_CppFile *cpp);
|
2021-09-19 11:49:56 +00:00
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFLineBeginV (Dqn_CppFile *cpp, char const *fmt, va_list args);
|
|
|
|
void Dqn_CppFLineBegin (Dqn_CppFile *cpp, char const *fmt, ...);
|
|
|
|
void Dqn_CppFLineEnd (Dqn_CppFile *cpp, char const *fmt, ...);
|
|
|
|
void Dqn_CppFLineAdd (Dqn_CppFile *cpp, char const *fmt, ...);
|
|
|
|
void Dqn_CppFLineV (Dqn_CppFile *cpp, char const *fmt, va_list args);
|
|
|
|
void Dqn_CppFLine (Dqn_CppFile *cpp, char const *fmt, ...);
|
2021-09-19 11:49:56 +00:00
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFNewLine (Dqn_CppFile *cpp);
|
|
|
|
void Dqn_CppFIndent (Dqn_CppFile *cpp);
|
|
|
|
void Dqn_CppFUnindent (Dqn_CppFile *cpp);
|
2021-09-19 11:49:56 +00:00
|
|
|
|
|
|
|
// fmt: (Optional) The format string to print at the beginning of the block.
|
|
|
|
// When the fmt string is given, it will place a new-line at the end of the fmt
|
|
|
|
// string. When fmt is nullptr, no new line will be appended.
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFBeginBlock (Dqn_CppFile *cpp, char const *fmt, ...);
|
|
|
|
void Dqn_CppFEndBlock (Dqn_CppFile *cpp, bool trailing_semicolon, bool new_line_on_next_block);
|
|
|
|
|
|
|
|
#define Dqn_CppFEndEnumBlock(cpp) Dqn_CppFEndBlock(cpp, true /*trailing_semicolon*/, true /*new_line_on_next_block*/)
|
|
|
|
#define Dqn_CppFEndForBlock(cpp) Dqn_CppFEndBlock(cpp, false /*trailing_semicolon*/, false /*new_line_on_next_block*/)
|
|
|
|
#define Dqn_CppFEndIfBlock(cpp) Dqn_CppFEndBlock(cpp, false /*trailing_semicolon*/, false /*new_line_on_next_block*/)
|
|
|
|
#define Dqn_CppFEndFuncBlock(cpp) Dqn_CppFEndBlock(cpp, false /*trailing_semicolon*/, true /*new_line_on_next_block*/)
|
|
|
|
#define Dqn_CppFEndStructBlock(cpp) Dqn_CppFEndBlock(cpp, true /*trailing_semicolon*/, true /*new_line_on_next_block*/)
|
2021-09-19 11:49:56 +00:00
|
|
|
#endif // DQN_CPP_FILE_H
|
|
|
|
|
|
|
|
#if defined(DQN_CPP_FILE_IMPLEMENTATION)
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// NOTE: Dqn_CppFile Implementation
|
|
|
|
// -----------------------------------------------------------------------------
|
2021-12-16 13:07:33 +00:00
|
|
|
int Dqn_CppFSpacePerIndent(Dqn_CppFile *cpp)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
|
|
|
int result = cpp->space_per_indent == 0 ? 4 : cpp->space_per_indent;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFLineBeginV(Dqn_CppFile *cpp, char const *fmt, va_list args)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
2021-12-16 13:07:33 +00:00
|
|
|
int spaces = cpp->indent * Dqn_CppFSpacePerIndent(cpp);
|
2021-09-19 11:49:56 +00:00
|
|
|
fprintf(cpp->file, "%*s", spaces, "");
|
|
|
|
vfprintf(cpp->file, fmt, args);
|
|
|
|
}
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFLineBegin(Dqn_CppFile *cpp, char const *fmt, ...)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_CppFLineBeginV(cpp, fmt, args);
|
2021-09-19 11:49:56 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFLineEnd(Dqn_CppFile *cpp, char const *fmt, ...)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
|
|
|
if (fmt)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vfprintf(cpp->file, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc('\n', cpp->file);
|
|
|
|
}
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFLineAdd(Dqn_CppFile *cpp, char const *fmt, ...)
|
2021-09-24 12:29:34 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vfprintf(cpp->file, fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFLineV(Dqn_CppFile *cpp, char const *fmt, va_list args)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_CppFLineBeginV(cpp, fmt, args);
|
|
|
|
Dqn_CppFLineEnd(cpp, nullptr);
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFLine(Dqn_CppFile *cpp, char const *fmt, ...)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_CppFLineBeginV(cpp, fmt, args);
|
|
|
|
Dqn_CppFLineEnd(cpp, nullptr);
|
2021-09-19 11:49:56 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFNewLine(Dqn_CppFile *cpp)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
|
|
|
fputc('\n', cpp->file);
|
|
|
|
}
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFIndent(Dqn_CppFile *cpp)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
|
|
|
cpp->indent++;
|
|
|
|
}
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFUnindent(Dqn_CppFile *cpp)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
|
|
|
cpp->indent--;
|
|
|
|
DQN_CPPF_ASSERT(cpp->indent >= 0);
|
|
|
|
}
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFBeginBlock(Dqn_CppFile *cpp, char const *fmt, ...)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
|
|
|
if (fmt)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_CppFLineV(cpp, fmt, args);
|
2021-09-19 11:49:56 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_CppFLine(cpp, "{");
|
|
|
|
Dqn_CppFIndent(cpp);
|
2021-09-19 11:49:56 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 13:07:33 +00:00
|
|
|
void Dqn_CppFEndBlock(Dqn_CppFile *cpp, bool trailing_semicolon, bool new_line_on_next_block)
|
2021-09-19 11:49:56 +00:00
|
|
|
{
|
2021-12-16 13:07:33 +00:00
|
|
|
Dqn_CppFUnindent(cpp);
|
|
|
|
Dqn_CppFLine(cpp, trailing_semicolon ? "};" : "}");
|
2021-09-19 11:49:56 +00:00
|
|
|
if (new_line_on_next_block) fputc('\n', cpp->file);
|
|
|
|
}
|
|
|
|
#endif // DQN_CPP_FILE_IMPLEMENTATION
|