forked from vmxdev/tkvdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorst.c
130 lines (100 loc) · 2.15 KB
/
colorst.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "colorst.h"
#include "colorst_impl.h"
static void colorst_free(colorst *c);
static int colorst_prepare(colorst *c, const char *query,
char *message, size_t msgsize);
colorst *
colorst_create(const char *query, tkvdb_tr *tr,
int *retcode, char *message, size_t msgsize)
{
colorst *c;
struct colorst_data *cdt;
c = malloc(sizeof(colorst));
if (!c) {
goto malloc_fail;
}
cdt = malloc(sizeof(struct colorst_data));
if (!cdt) {
goto malloc_data_fail;
}
cdt->tr = tr;
c->data = cdt;
if (!colorst_prepare(c, query, message, msgsize)) {
goto prepare_fail;
}
c->free = &colorst_free;
*retcode = 1;
return c;
prepare_fail:
malloc_data_fail:
free(c);
malloc_fail:
*retcode = 0;
return NULL;
}
static void
colorst_free(colorst *c)
{
free(c->data);
c->data = NULL;
free(c);
}
void
colorst_res_init(colorst_res *cr)
{
cr->errors = cr->warnings = cr->info = 0;
cr->error_messages = NULL;
cr->warning_messages = NULL;
cr->info_messages = NULL;
}
void
colorst_res_free(colorst_res *cr)
{
int i;
#define FREE(N, MSGS) \
do { \
for (i=0; i<N; i++) { \
free(MSGS[i]); \
MSGS[i] = NULL; \
} \
free(MSGS); \
} while (0)
FREE(cr->errors, cr->error_messages);
FREE(cr->warnings, cr->warning_messages);
FREE(cr->info, cr->info_messages);
#undef FREE
}
static int
colorst_prepare(colorst *c, const char *query, char *message, size_t msgsize)
{
struct input i;
/* init input */
i.s = query;
i.eof = 0;
i.line = i.col = 1;
i.error = 0;
i.errmsg = message;
i.msgsize = msgsize;
i.data = c->data;
i.data->fl.fields = NULL;
i.data->fl.nfields = 0;
parse_query(&i);
if (i.error) {
return 0;
}
return 1;
}
void
mkerror(struct input *i, char *msg)
{
i->error = 1;
if (i->line > 1) {
snprintf(i->errmsg, i->msgsize, "Line %d, col %d: %s",
i->line, i->col, msg);
} else {
snprintf(i->errmsg, i->msgsize, "Col: %d: %s", i->col, msg);
}
}