summaryrefslogtreecommitdiffabout
path: root/mimetypes/prloc.c
blob: 8eab11ad0236f671e4706e605f16e26ef99500ad (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
/* 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"

size_t
nprlen (unsigned x)
{
  size_t len = 1;
  while (x /= 10)
    len++;
  return len;
}

size_t
lpprlen (struct locus_point const *lp)
{
  size_t len = 0;

  if (lp->file)
    {
      len = strlen (lp->file) + 1 + nprlen (lp->line);
      if (lp->col)
	len += 1 + nprlen (lp->col);
    }
  return len;
}

size_t
lrprlen (struct locus_range const *lr)
{
  size_t len;

  len = lpprlen (&lr->end);
  if (len)
    len++;
  len += lpprlen (&lr->beg);
  return len;
}

enum
  {
    PR_NON = 0x00,
    PR_FIL = 0x01,
    PR_LIN = 0x02,
    PR_COL = 0x04,
    PR_ALL = PR_FIL|PR_LIN|PR_COL
  };

static ssize_t
_numprint (char *p, unsigned num)
{
  size_t len = 0;
  char *q;
  do
    {
      p[len++] = (num % 10) + '0';
    }
  while (num /= 10);
  for (q = p + len - 1; q > p; p++, q--)
    {
      char t = *p;
      *p = *q;
      *q = t;
    }
  return len;
}
      
ssize_t
_lpprint (char *buf, struct locus_point const *lp, int pr)
{
  size_t n = 0;

  if (!lp->file)
    pr &= ~PR_FIL;
  if (!lp->col)
    pr &= ~PR_COL;
  
  if (pr & PR_FIL)
    {
      n = strlen (lp->file);
      memcpy (buf, lp->file, n);
    }
  if (pr & PR_LIN)
    {
      if (pr & PR_FIL)
	buf[n++] = ':';
      n += _numprint (buf + n, lp->line);
    }
  if (pr & PR_COL)
    {
      if (pr & PR_LIN)
	buf[n++] = '.';
      n += _numprint (buf + n, lp->col);
    }
  return n;
}

ssize_t
locus_point_format (char **pbuf, size_t *psize, struct locus_point const *lp)
{
  char *buf;
  size_t size;
  size_t prlen;
  
  if (!pbuf || !psize || !lp)
    {
      errno = EINVAL;
      return -1;
    }
  buf = *pbuf;
  size = *psize;

  prlen = lpprlen (lp);
  if (prlen + 1 > size)
    {
      size = prlen + 1;
      buf = realloc (buf, size);
      if (!buf)
	return -1;
      *pbuf = buf;
      *psize = size;
    }
  return _lpprint (buf, lp, PR_ALL);
}

ssize_t
locus_range_format (char **pbuf, size_t *psize, struct locus_range const *lr)
{
  char *buf;
  size_t size;
  size_t prlen;
  size_t n;
  
  if (!pbuf || !psize || !lr)
    {
      errno = EINVAL;
      return -1;
    }
  buf = *pbuf;
  size = *psize;

  prlen = lrprlen (lr);
  if (prlen + 1 > size)
    {
      size = prlen + 1;
      buf = realloc (buf, size);
      if (!buf)
	return -1;
      *pbuf = buf;
      *psize = size;
    }
  n = _lpprint (buf, &lr->beg, PR_ALL);
  if (lr->end.file)
    {
      int flag = PR_NON;
      
      if (!locus_point_same_file (&lr->beg, &lr->end))
	flag = PR_ALL;
      else if (lr->beg.line != lr->end.line)
	flag = PR_LIN|PR_COL;
      else if (lr->beg.col && lr->beg.col != lr->end.col)
	flag = PR_COL;

      if (flag != PR_NON)
	{
	  buf[n++] = '-';
	  n += _lpprint (buf + n, &lr->end, flag);
	}
    }
  buf[n] = 0;
  return n;
}

Return to:

Send suggestions and report system problems to the System administrator.