summaryrefslogtreecommitdiffabout
path: root/mimetypes/linetrack.c
blob: e020782b16c5b4635581f002eb01bd5acd0c73b4 (plain)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* This file is part of fileserv.
   Copyright (C) 2017-2019 Sergey Poznyakoff

   Fileserv is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   Fileserv is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with fileserv.  If not, see <http://www.gnu.org/licenses/>. */

#include <stdlib.h>
#include <errno.h>
#include "locus.h"

/* The line-tracker structure keeps track of the last N lines read from one
   or more input files.  For each line read it keeps the number of characters
   in that line including the newline.  This information is stored in a
   cyclic stack of N elements (N >= 2).  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.

   Each input file is represented by a directory entry keeping its name,
   the number of the first line that is stored in the tracker and the index of
   that line in the cols stack.  Entries form a doubly-linked list, with
   head pointing to the most recent (current) source.  When a new line is
   being added to the stack which is full, its eldest entry is discarded
   and re-assigned to that line; at the same time the directory of the
   eldest source is updated accordingly.  If the entry to be discarded
   represents the only line of the source, the source is discarded.   
*/

struct source
{
  char const *file_name; /* Name of the source file */
  size_t idx;            /* Index of the first element on stack */
  unsigned line;         /* Number of line corresponding to cols[idx] */
  struct source *next, *prev;
};

struct linetrack
{
  struct source *s_head, *s_tail;
                         /* Directory of source files.  Most recent one is
			    s_head */

  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 *cols;        /* Cyclic stack or character counts.
			    Number of characters in line (head + n) is
			    cols[head + n] (0 <= n <= tos). */
};

static inline size_t
trk_incr (struct linetrack *trk, size_t a)
{
  return (a + 1) % trk->max_lines;
}

static inline size_t
trk_decr (struct linetrack *trk, size_t a)
{
  return (a + trk->max_lines - 1) % trk->max_lines;
}
	 
static inline unsigned
count_lines (linetrack_t trk, size_t from)
{
  return (trk->tos + trk->max_lines - from) % trk->max_lines + 1;
}

#ifndef SIZE_MAX
# define SIZE_MAX (~((size_t)0))
#endif

static int
count_chars (struct linetrack *trk, size_t i, size_t *ret)
{
  size_t nch = 0;

  while (1)
    {
      unsigned n = trk->cols[i];
      if (SIZE_MAX - nch < n)
	return ERANGE;
      nch += n;
      if (i == trk->tos)
	break;
      i = trk_incr (trk, i);
    }
  *ret = nch;
  return 0;
}

static size_t
count_files (struct linetrack *trk)
{
  struct source *sp;
  size_t n = 0;
  for (sp = trk->s_head; sp; sp = sp->next)
    n++;
  return n;
}

static void
del_source (linetrack_t trk, struct source *sp)
{
  if (sp->prev)
    sp->prev->next = sp->next;
  else
    trk->s_head = sp->next;
  if (sp->next)
    sp->next->prev = sp->prev;
  else
    trk->s_tail = sp->prev;
  ident_deref (sp->file_name);
  free (sp);
}
  
static inline unsigned *
push (linetrack_t trk)
{
  trk->tos = trk_incr (trk, trk->tos);
  if (trk->tos == trk->head)
    {
      trk->head = trk_incr (trk, trk->head);
      trk->s_tail->idx = trk->head;
      trk->s_tail->line++;
    }
  if (trk->s_tail->prev && trk->s_tail->idx == trk->s_tail->prev->idx)
    del_source (trk, trk->s_tail);
  trk->cols[trk->tos] = 0;
  return &trk->cols[trk->tos];
}

static inline unsigned *
pop (linetrack_t trk)
{
  if (trk->tos == trk->head)
    return NULL;
  if (trk->tos == trk->s_head->idx)
    del_source (trk, trk->s_head);
  
  trk->tos = trk_decr (trk, trk->tos);
  
  return &trk->cols[trk->tos];
}

int
linetrack_origin (linetrack_t trk, struct locus_point const *pt)
{
  int rc;
  struct source *sp;
  char const *file_name;
  
  if (!trk || !pt || pt->line == 0)
    return EINVAL;
  if (pt->file)
    file_name = pt->file;
  else if (trk->s_head)
    file_name = trk->s_head->file_name;
  else
    return EINVAL;
  sp = malloc (sizeof *sp);
  if (!sp)
    return errno;
  rc = ident_ref (file_name, &sp->file_name);
  if (rc)
    {
      free (sp);
      return rc;
    }

  if (trk->cols[trk->tos])
    push (trk);
  
  sp->idx = trk->tos;
  sp->line = pt->line;
  trk->cols[sp->idx] = pt->col;

  sp->prev = NULL;
  sp->next = trk->s_head;
  if (trk->s_head)
    trk->s_head->prev = sp;
  else
    trk->s_tail = sp;
  trk->s_head = sp;
  return 0;
}

int
linetrack_create (linetrack_t *ret, char const *file_name, size_t max_lines)
{
  int rc;
  struct linetrack *trk;
  struct locus_point pt;
  
  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;
    }
  trk->s_head = trk->s_tail = NULL;
  
  if (max_lines < 2)
    max_lines = 2;
  trk->max_lines = max_lines;
  trk->head = 0;
  trk->tos = 0;
  trk->cols[0] = 0;

  pt.file = file_name;
  pt.line = 1;
  pt.col = 0;
  rc = linetrack_origin (trk, &pt);
  if (rc)
    {
      free (trk->cols);
      free (trk);
      return rc;
    }
  
  *ret = trk;
  return 0;
}

int
linetrack_rebase (linetrack_t trk, struct locus_point const *pt)
{
  char const *file_name;
  int rc = ident_ref (pt->file, &file_name);
  if (rc)
    return rc;
  ident_deref (trk->s_head->file_name);
  trk->s_head->file_name = file_name;
  trk->s_head->line = pt->line;
  trk->cols[trk->s_head->idx] = pt->col;
  return 0;
}
  
void
linetrack_free (linetrack_t trk)
{
  if (trk)
    {
      while (trk->s_head)
	del_source (trk, trk->s_head);
      free (trk->cols);
      free (trk);
    }
}

void
linetrack_destroy (linetrack_t *trk)
{
  if (trk)
    {
      linetrack_free (*trk);
      *trk = NULL;
    }
}   

int
linetrack_stat (struct linetrack *trk, struct linetrack_stat *st)
{
  if (count_chars (trk, trk->head, &st->n_chars))
    return ERANGE;
  st->n_files = count_files (trk);
  st->n_lines = count_lines (trk, trk->head);
  return 0;
}

int
linetrack_at_bol (struct linetrack *trk)
{
  return trk->cols[trk->tos] == 0;
}

void
linetrack_advance (struct linetrack *trk,
		      struct locus_range *loc,
		      char const *text, size_t leng)
{
  unsigned *ptr;

  if (text == NULL || leng == 0)
    return;

  locus_point_set_file (&loc->beg, trk->s_head->file_name);
  locus_point_set_file (&loc->end, trk->s_head->file_name);
  loc->beg.line =
    trk->s_head->line + count_lines (trk, trk->s_head->idx) - 1;
  ptr = &trk->cols[trk->tos];
  loc->beg.col = *ptr + 1;
  while (leng--)
    {
      (*ptr)++;
      if (*text == '\n')
	ptr = push (trk);
      text++;
    }

  loc->end.line =
    trk->s_head->line + count_lines (trk, trk->s_head->idx) - 1;
  if (*ptr)
    {
      loc->end.col = *ptr;
    }
  else
    {
      /* Text ends with a newline.  Keep the previous line number. */
      loc->end.line--;
      loc->end.col = trk->cols[trk_decr (trk, trk->tos)] - 1;
      if (loc->end.col + 1 == loc->beg.col)
	{
	  /* This happens if the previous line contained only newline. */
	  loc->beg.col = loc->end.col;
	}	  
   }
}

int
linetrack_locus (struct linetrack *trk, struct locus_point *lp)
{
  int rc = locus_point_set_file (lp, trk->s_head->file_name);
  if (rc == 0)
    {
      lp->line =
	trk->s_head->line + count_lines (trk, trk->s_head->idx) - 1;
      lp->col = trk->cols[trk->tos];
    }
  return rc;
}

int
linetrack_retreat (struct linetrack *trk, size_t n)
{
  size_t nch;

  if (count_chars (trk, trk->head, &nch))
    return ERANGE;
  if (n > nch)
    return ERANGE;
  else
    {
      unsigned *ptr = &trk->cols[trk->tos];
      while (n--)
	{
	  if (*ptr == 0)
	    {
	      ptr = pop (trk);
	      if (!ptr || *ptr == 0)
		return ERANGE;
	    }
	  --*ptr;
	}
    }
  return 0;
}
    
  

Return to:

Send suggestions and report system problems to the System administrator.