Initial commit, basic lexer structure
This commit is contained in:
42
src/error.c
Normal file
42
src/error.c
Normal file
@ -0,0 +1,42 @@
|
||||
#include "error.h"
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
error_t *const err_errorf_alloc = &(error_t){
|
||||
.message = "Allocation failed during formatting of another error"};
|
||||
error_t *const err_errorf_length = &(error_t){
|
||||
.message =
|
||||
"Formatting of another error failed to determine the error length"};
|
||||
|
||||
error_t *errorf(const char *fmt, ...) {
|
||||
error_t *err = calloc(1, sizeof(error_t));
|
||||
if (err == nullptr)
|
||||
return err_errorf_alloc;
|
||||
|
||||
va_list args;
|
||||
va_list args_count;
|
||||
va_start(args, fmt);
|
||||
va_copy(args_count, args);
|
||||
|
||||
int size = vsnprintf(nullptr, 0, fmt, args_count) + 1;
|
||||
va_end(args_count);
|
||||
if (size <= 0) {
|
||||
free(err);
|
||||
va_end(args);
|
||||
return err_errorf_length;
|
||||
}
|
||||
|
||||
err->message = malloc(size);
|
||||
if (err->message == nullptr) {
|
||||
free(err);
|
||||
va_end(args);
|
||||
return err_errorf_alloc;
|
||||
}
|
||||
|
||||
vsnprintf(err->message, size, fmt, args);
|
||||
va_end(args);
|
||||
err->is_heap_allocated = true;
|
||||
return err;
|
||||
}
|
Reference in New Issue
Block a user