2023-08-28 21:03:08 -07:00
|
|
|
#ifdef GEN_INTELLISENSE_DIRECTIVES
|
|
|
|
# include "builder.hpp"
|
|
|
|
#endif
|
2023-08-21 18:40:23 -07:00
|
|
|
|
2024-12-10 13:13:14 -08:00
|
|
|
#pragma region Builder
|
|
|
|
|
2024-12-10 04:24:32 -08:00
|
|
|
Builder builder_open( char const* path )
|
2023-08-03 08:01:43 -07:00
|
|
|
{
|
|
|
|
Builder result;
|
2024-10-24 22:04:17 -07:00
|
|
|
|
2023-08-03 08:01:43 -07:00
|
|
|
FileError error = file_open_mode( & result.File, EFileMode_WRITE, path );
|
|
|
|
if ( error != EFileError_NONE )
|
|
|
|
{
|
|
|
|
log_failure( "gen::File::open - Could not open file: %s", path);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-12-13 16:16:52 -08:00
|
|
|
result.Buffer = strbuilder_make_reserve( _ctx->Allocator_Temp, _ctx->InitSize_BuilderBuffer );
|
2023-08-03 08:01:43 -07:00
|
|
|
|
2023-08-19 05:21:28 -07:00
|
|
|
// log_fmt("$Builder - Opened file: %s\n", result.File.filename );
|
2023-08-03 08:01:43 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2024-12-10 04:24:32 -08:00
|
|
|
void builder_pad_lines( Builder* builder, s32 num )
|
2023-08-03 08:01:43 -07:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_str( & builder->Buffer, txt("\n") );
|
2023-08-03 08:01:43 -07:00
|
|
|
}
|
|
|
|
|
2024-12-10 04:24:32 -08:00
|
|
|
void builder_print( Builder* builder, Code code )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2024-12-13 11:38:27 -08:00
|
|
|
StrBuilder str = code_to_strbuilder(code);
|
2024-10-27 15:58:37 -07:00
|
|
|
// const ssize len = str.length();
|
2023-08-19 05:21:28 -07:00
|
|
|
// log_fmt( "%s - print: %.*s\n", File.filename, len > 80 ? 80 : len, str.Data );
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_string( & builder->Buffer, str );
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
2024-12-10 04:24:32 -08:00
|
|
|
void builder_print_fmt_va( Builder* builder, char const* fmt, va_list va )
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2024-10-27 15:58:37 -07:00
|
|
|
ssize res;
|
2023-07-24 14:45:27 -07:00
|
|
|
char buf[ GEN_PRINTF_MAXLEN ] = { 0 };
|
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
res = c_str_fmt_va( buf, count_of( buf ) - 1, fmt, va ) - 1;
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_append_c_str_len( (StrBuilder*) & (builder->Buffer), (char const*)buf, res);
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
|
|
|
|
2024-12-10 04:24:32 -08:00
|
|
|
void builder_write(Builder* builder)
|
2023-07-24 14:45:27 -07:00
|
|
|
{
|
2024-12-12 09:55:15 -08:00
|
|
|
b32 result = file_write( & builder->File, builder->Buffer, strbuilder_length(builder->Buffer) );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
|
|
|
if ( result == false )
|
2024-12-10 04:24:32 -08:00
|
|
|
log_failure("gen::File::write - Failed to write to file: %s\n", file_name( & builder->File ) );
|
2023-07-24 14:45:27 -07:00
|
|
|
|
2024-12-10 04:24:32 -08:00
|
|
|
log_fmt( "Generated: %s\n", builder->File.filename );
|
|
|
|
file_close( & builder->File );
|
2024-12-12 09:55:15 -08:00
|
|
|
strbuilder_free(& builder->Buffer);
|
2023-07-24 14:45:27 -07:00
|
|
|
}
|
2024-12-10 04:24:32 -08:00
|
|
|
|
2024-12-10 13:13:14 -08:00
|
|
|
#pragma endregion Builder
|