-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.c
75 lines (53 loc) · 1.98 KB
/
error.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "error.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
struct PositionData {
unsigned int line;
unsigned int line_position;
char* line_content;
size_t line_amount;
};
static struct PositionData get_position(FILE *fptr) {
long position = ftell(fptr);
unsigned int line_position = 0;
char c;
fseek(fptr, -2, SEEK_CUR);
while(line_position < position && (c = getc(fptr)) != '\n') {
++line_position;
fseek(fptr, -2, SEEK_CUR);
}
size_t bufsize = line_position + 10;
char* buf = calloc(bufsize - 1, sizeof(char));
size_t characters = getline(&buf, &bufsize, fptr);
rewind(fptr);
unsigned int lines = 0;
while ((c = getc(fptr)) != EOF && ftell(fptr) != position) {
if(c == '\n') ++lines;
}
fseek(fptr, position, SEEK_SET);
return (struct PositionData) {
.line = lines,
.line_position = line_position,
.line_content = buf,
.line_amount = characters
};
}
#define calc_chars(number) (int)((ceil(log10(number))+1)*sizeof(char))
void print_parser_error(FILE* fptr, const char* file_name, char* error_type, const char* const error_message) {
fprintf(stderr, "ERROR: %s\n", error_type);
struct PositionData position = get_position(fptr);
fprintf(stderr, "--> %s:%u:%u\n", file_name, position.line + 1, position.line_position + 1);
int chars = calc_chars(position.line + 2);
chars = chars > 1 ? chars : 2;
char start_whitespace[chars];
memset(start_whitespace, ' ', chars);
start_whitespace[chars - 1] = '\0';
fprintf(stderr, "%s |\n", start_whitespace);
fprintf(stderr, "%u | %s", position.line + 1, position.line_content);
char position_whitespace[position.line_position + 1];
memset(position_whitespace, ' ', position.line_position);
position_whitespace[position.line_position] = '\0';
fprintf(stderr, "%s | %s^ %s\n", start_whitespace, position_whitespace, error_message);
}