Lumenarium/meta/gs_meta_error.h

69 lines
2.0 KiB
C
Raw Normal View History

2019-10-30 14:28:02 +00:00
struct error_buffer
2019-10-30 14:28:02 +00:00
{
char* Backbuffer;
string* Contents;
2019-10-30 14:28:02 +00:00
};
#define ERROR_MAX_LENGTH 256
#define ERROR_BUFFER_SIZE 256
struct errors
2019-10-30 14:28:02 +00:00
{
error_buffer* Buffers;
u32 BuffersCount;
u32 Used;
2019-10-30 14:28:02 +00:00
};
internal void
PushFError (errors* Errors, char* Format, ...)
2019-10-30 14:28:02 +00:00
{
if (Errors->Used >= (Errors->BuffersCount * ERROR_BUFFER_SIZE))
2019-10-30 14:28:02 +00:00
{
Errors->BuffersCount += 1;
Errors->Buffers = (error_buffer*)realloc(Errors->Buffers, sizeof(error_buffer*) * Errors->BuffersCount);
error_buffer* NewBuffer = Errors->Buffers + (Errors->BuffersCount - 1);
NewBuffer->Backbuffer = (char*)malloc(sizeof(char) * ERROR_MAX_LENGTH * ERROR_BUFFER_SIZE);
NewBuffer->Contents = (string*)malloc(sizeof(string) * ERROR_BUFFER_SIZE);
for (u32 i = 0; i < ERROR_BUFFER_SIZE; i++)
{
NewBuffer->Contents[i].Memory = NewBuffer->Backbuffer + (i * ERROR_MAX_LENGTH);
NewBuffer->Contents[i].Max = ERROR_MAX_LENGTH;
NewBuffer->Contents[i].Length = 0;
}
2019-10-30 14:28:02 +00:00
}
u32 NewErrorIndex = Errors->Used++;
u32 BufferIndex = NewErrorIndex / ERROR_BUFFER_SIZE;
u32 IndexInBuffer = NewErrorIndex % ERROR_BUFFER_SIZE;
string* NewError = Errors->Buffers[BufferIndex].Contents + IndexInBuffer;
2019-10-30 14:28:02 +00:00
va_list Args;
va_start(Args, Format);
NewError->Length = PrintFArgsList(NewError->Memory, NewError->Max, Format, Args);
2019-10-30 14:28:02 +00:00
va_end(Args);
}
internal string*
TakeError (errors* Errors)
2019-10-30 14:28:02 +00:00
{
u32 NewErrorIndex = Errors->Used++;
u32 BufferIndex = NewErrorIndex / ERROR_BUFFER_SIZE;
u32 IndexInBuffer = NewErrorIndex % ERROR_BUFFER_SIZE;
string* NewError = Errors->Buffers[BufferIndex].Contents + IndexInBuffer;
return NewError;
2019-10-30 14:28:02 +00:00
}
internal void
PrintAllErrors (errors Errors)
2019-10-30 14:28:02 +00:00
{
for (u32 i = 0; i < Errors.Used; i++)
2019-10-30 14:28:02 +00:00
{
u32 BufferIndex = i / ERROR_BUFFER_SIZE;
u32 IndexInBuffer = i % ERROR_BUFFER_SIZE;
string Error = Errors.Buffers[BufferIndex].Contents[IndexInBuffer];
printf("%.*s\n", StringExpand(Error));
2019-10-30 14:28:02 +00:00
}
}