c-template
errors.c
Go to the documentation of this file.
1
#include <string.h>
2
#include <stdlib.h>
3
#include <stdio.h>
4
#include <stdatomic.h>
5
#include "../../include/utils/errors.h"
6
7
cg_error
*
new_cg_error
(
char
*message) {
8
cg_error
*err = calloc(
sizeof
(
cg_error
),
sizeof
(
cg_error
) + strlen(message) + 1);
9
if
(err == NULL) {
10
printf(
"failed to calloc cg_error\n"
);
11
return
NULL;
12
}
13
err->
message
=
cg_error_string
(message);
14
return
err;
15
}
16
17
char
*
cg_error_string
(
char
*message) {
18
char
*msg = calloc(
sizeof
(
char
), strlen(message) + 1);
19
if
(msg == NULL) {
20
printf(
"failed to calloc msg\n"
);
21
return
NULL;
22
}
23
// TODO(bonedaddy): should we use strcpy??
24
memmove(msg, message, strlen(message) + 1);
25
return
msg;
26
}
27
28
int
wrap_cg_error
(
cg_error
*error,
char
*message) {
29
// 4 for delimiter of ` | `
30
error->
message
= realloc(error->
message
, strlen(error->
message
) + strlen(message) + 4);
31
if
(error->
message
== NULL) {
32
return
-1;
33
}
34
strcat(error->
message
,
" | "
);
35
strcat(error->
message
, message);
36
return
0;
37
}
38
39
void
free_cg_error
(
cg_error
*error) {
40
free(error->
message
);
41
free(error);
42
}
cg_error_string
char * cg_error_string(char *message)
creates a new variable and moves memory of message into that variable returning it this appears to be...
Definition:
errors.c:17
new_cg_error
cg_error * new_cg_error(char *message)
returns a cg_error struct containing message
Definition:
errors.c:7
wrap_cg_error
int wrap_cg_error(cg_error *error, char *message)
appends message to the end of error->message uses realloc to maximize memory efficiency
Definition:
errors.c:28
free_cg_error
void free_cg_error(cg_error *error)
frees up resources associated with error
Definition:
errors.c:39
cg_error::message
char * message
Definition:
errors.h:15
cg_error
Definition:
errors.h:14
src
utils
errors.c
Generated by
1.8.17