linetrack.c
5.23 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <stdlib.h>
#include <errno.h>
#include <mailutils/types.h>
#include <mailutils/locus.h>
#include <mailutils/error.h>
/* The line-tracker structure keeps track of the last N lines read from a
text input file. For each line read it keeps the number of characters
in that line including the newline. This information is stored in a
syclic stack of N elements. Top of stack always represents the current
line. For the purpose of line tracker, current line is the line that is
being visited, such that its final newline character has not yet been
seen. Once the newline is seen, the line is pushed on stack, and a new
current line is assumed.
The value of N must not be less than 2.
*/
struct mu_linetrack
{
char const *file_name; /* Name of the source file */
size_t max_lines; /* Max. number of lines history kept by tracker (N) */
size_t head; /* Index of the eldest element on stack */
size_t tos; /* Index of the most recent element on stack
(< max_lines) */
unsigned hline; /* Number of line corresponding to cols[head] */
unsigned *cols; /* Cyclic stack or character counts.
Number of characters in line (hline + n) is
cols[head + n] (0 <= n <= tos). */
};
static inline unsigned *
cols_ptr (mu_linetrack_t trk, size_t n)
{
return &trk->cols[(trk->head + n) % trk->max_lines];
}
static inline unsigned *
cols_tos_ptr (mu_linetrack_t trk)
{
return cols_ptr (trk, trk->tos);
}
static inline unsigned
cols_peek (mu_linetrack_t trk, size_t n)
{
return *cols_ptr (trk, n);
}
int
mu_linetrack_create (mu_linetrack_t *ret,
char const *file_name, size_t max_lines)
{
int rc;
struct mu_linetrack *trk;
trk = malloc (sizeof *trk);
if (!trk)
return errno;
trk->cols = calloc (max_lines, sizeof (trk->cols[0]));
if (!trk->cols)
{
rc = errno;
free (trk);
return rc;
}
rc = mu_ident_ref (file_name, &trk->file_name);
if (rc)
{
free (trk->cols);
free (trk);
return rc;
}
if (max_lines < 2)
max_lines = 2;
trk->max_lines = max_lines;
trk->head = 0;
trk->tos = 0;
trk->hline = 1;
trk->cols[0] = 0;
*ret = trk;
return 0;
}
int
mu_linetrack_rebase (mu_linetrack_t trk, struct mu_locus_point const *pt)
{
char const *file_name;
int rc = mu_ident_ref (pt->mu_file, &file_name);
if (rc)
return rc;
mu_ident_deref (trk->file_name);
trk->file_name = file_name;
trk->hline = pt->mu_line;
*cols_ptr (trk, 0) = pt->mu_col;
return 0;
}
void
mu_linetrack_free (mu_linetrack_t trk)
{
if (trk)
{
mu_ident_deref (trk->file_name);
free (trk->cols);
free (trk);
}
}
void
mu_linetrack_destroy (mu_linetrack_t *trk)
{
if (trk)
{
mu_linetrack_free (*trk);
*trk = NULL;
}
}
static inline unsigned *
push (mu_linetrack_t trk)
{
unsigned *ptr;
if (trk->tos == trk->max_lines - 1)
{
trk->head++;
trk->hline++;
}
else
trk->tos++;
*(ptr = cols_tos_ptr (trk)) = 0;
return ptr;
}
static inline unsigned *
pop (mu_linetrack_t trk)
{
if (trk->tos == 0)
return NULL;
trk->tos--;
return cols_tos_ptr (trk);
}
#ifndef SIZE_MAX
# define SIZE_MAX (~((size_t)0))
#endif
int
mu_linetrack_stat (struct mu_linetrack *trk, struct mu_linetrack_stat *st)
{
size_t i, nch = 0;
for (i = 0; i <= trk->tos; i++)
{
unsigned n = cols_peek (trk, i);
if (SIZE_MAX - nch < n)
return ERANGE;
nch += n;
}
st->start_line = trk->hline;
st->n_lines = trk->tos + 1;
st->n_chars = nch;
return 0;
}
int
mu_linetrack_at_bol (struct mu_linetrack *trk)
{
return *cols_tos_ptr (trk) == 0;
}
void
mu_linetrack_advance (struct mu_linetrack *trk,
struct mu_locus_range *loc,
char const *text, size_t leng)
{
unsigned *ptr;
if (text == NULL || leng == 0)
return;
mu_locus_point_set_file (&loc->beg, trk->file_name);
mu_locus_point_set_file (&loc->end, trk->file_name);
loc->beg.mu_line = trk->hline + trk->tos;
ptr = cols_tos_ptr (trk);
loc->beg.mu_col = *ptr + 1;
while (leng--)
{
(*ptr)++;
if (*text == '\n')
ptr = push (trk);
text++;
}
if (*ptr)
{
loc->end.mu_line = trk->hline + trk->tos;
loc->end.mu_col = *ptr;
}
else
{
/* Text ends with a newline. Keep the previous line number. */
loc->end.mu_line = trk->hline + trk->tos - 1;
loc->end.mu_col = cols_peek (trk, trk->tos - 1) - 1;
if (loc->end.mu_col + 1 == loc->beg.mu_col)
{
/* This happens if the previous line contained only newline. */
loc->beg.mu_col = loc->end.mu_col;
}
}
}
int
mu_linetrack_locus (struct mu_linetrack *trk, struct mu_locus_point *lp)
{
lp->mu_line = trk->hline + trk->tos;
return mu_locus_point_set_file (lp, trk->file_name);
}
int
mu_linetrack_retreat (struct mu_linetrack *trk, size_t n)
{
struct mu_linetrack_stat st;
mu_linetrack_stat (trk, &st);
if (n > st.n_chars)
return ERANGE;
else
{
unsigned *ptr = cols_tos_ptr (trk);
while (n--)
{
if (*ptr == 0)
{
ptr = pop (trk);
if (!ptr || *ptr == 0)
{
mu_error ("%s:%d: INTERNAL ERROR: out of pop back\n",
__FILE__, __LINE__);
return ERANGE;
}
}
--*ptr;
}
}
return 0;
}