calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / soltools / cpp / _tokens.c
blob5cb403c270ed82ae23e1cdb7eb9da3f9e8d5c368
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <ctype.h>
25 #if (defined(_WIN32) || defined(__IBMC__))
26 #include <io.h>
27 #else
28 #include <unistd.h>
29 #endif
30 #include "cpp.h"
33 static char wbuf[4 * OBS];
34 static char *wbp = wbuf;
36 void
37 maketokenrow(int size, Tokenrow * trp)
39 trp->max = size;
40 if (size > 0)
41 trp->bp = (Token *) domalloc(size * sizeof(Token));
42 else
43 trp->bp = NULL;
44 trp->tp = trp->bp;
45 trp->lp = trp->bp;
48 Token *
49 growtokenrow(Tokenrow * trp)
51 size_t ncur = trp->tp - trp->bp;
52 size_t nlast = trp->lp - trp->bp;
54 trp->max = 3 * trp->max / 2 + 1;
55 trp->bp = (Token *) realloc(trp->bp, trp->max * sizeof(Token));
56 assert(trp->bp); // realloc failure is OOM -> no point to handle
57 trp->lp = &trp->bp[nlast];
58 trp->tp = &trp->bp[ncur];
59 return trp->lp;
63 * Compare a row of tokens, ignoring the content of WS; return !=0 if different
65 int
66 comparetokens(Tokenrow * tr1, Tokenrow * tr2)
68 Token *tp1, *tp2;
70 tp1 = tr1->tp;
71 tp2 = tr2->tp;
72 if (tr1->lp - tp1 != tr2->lp - tp2)
73 return 1;
74 for (; tp1 < tr1->lp; tp1++, tp2++)
76 if (tp1->type != tp2->type
77 || (tp1->wslen == 0) != (tp2->wslen == 0)
78 || tp1->len != tp2->len
79 || strncmp((char *) tp1->t, (char *) tp2->t, tp1->len) != 0)
80 return 1;
82 return 0;
86 * replace ntok tokens starting at dtr->tp with the contents of str.
87 * tp ends up pointing just beyond the replacement.
88 * Canonical whitespace is assured on each side.
90 void
91 insertrow(Tokenrow * dtr, int ntok, Tokenrow const * str)
93 int nrtok = (int)rowlen(str);
95 dtr->tp += ntok;
96 adjustrow(dtr, nrtok - ntok);
97 dtr->tp -= ntok;
98 movetokenrow(dtr, str);
99 dtr->tp += nrtok;
103 * make sure there is WS before trp->tp, if tokens might merge in the output
105 void
106 makespace(Tokenrow * trp, Token const * ntp)
108 uchar *tt;
109 Token *tp = trp->tp;
111 if (tp >= trp->lp)
112 return;
114 if (ntp->wslen)
116 tt = newstring(tp->t, tp->len, ntp->wslen);
117 strncpy((char *)tt, (char *)ntp->t - ntp->wslen, ntp->wslen);
118 tp->t = tt + ntp->wslen;
119 tp->wslen = ntp->wslen;
124 * Copy an entire tokenrow into another, at tp.
125 * It is assumed that there is enough space.
126 * Not strictly conforming.
128 void
129 movetokenrow(Tokenrow * dtr, Tokenrow const * str)
131 size_t nby;
133 nby = (char *) str->lp - (char *) str->bp;
134 if (nby)
135 memmove(dtr->tp, str->bp, nby);
139 * Move the tokens in a row, starting at tr->tp, rightward by nt tokens;
140 * nt may be negative (left move).
141 * The row may need to be grown.
142 * Non-strictly conforming because of the (char *), but easily fixed
144 void
145 adjustrow(Tokenrow * trp, int nt)
147 size_t nby, size;
149 if (nt == 0)
150 return;
151 size = (trp->lp - trp->bp) + nt;
152 while (size > trp->max)
153 growtokenrow(trp);
154 nby = (char *) trp->lp - (char *) trp->tp;
155 if (nby)
156 memmove(trp->tp + nt, trp->tp, nby);
157 trp->lp += nt;
161 * Copy a row of tokens into the destination holder, allocating
162 * the space for the contents. Return the destination.
164 Tokenrow *
165 copytokenrow(Tokenrow * dtr, Tokenrow const * str)
167 int len = (int)rowlen(str);
169 maketokenrow(len, dtr);
170 movetokenrow(dtr, str);
171 if (len != 0)
172 dtr->lp += len;
173 return dtr;
177 * Produce a copy of a row of tokens. Start at trp->tp.
178 * The value strings are copied as well. The first token
179 * has WS available.
181 Tokenrow *
182 normtokenrow(Tokenrow * trp)
184 Token *tp;
185 Tokenrow *ntrp = new(Tokenrow);
186 int len;
188 len = (int)(trp->lp - trp->tp);
189 if (len <= 0)
190 len = 1;
191 maketokenrow(len, ntrp);
192 for (tp = trp->tp; tp < trp->lp; tp++)
194 *ntrp->lp = *tp;
195 if (tp->len)
197 ntrp->lp->t = newstring(tp->t, tp->len, 1);
198 *ntrp->lp->t++ = ' ';
199 if (tp->wslen)
200 ntrp->lp->wslen = 1;
202 ntrp->lp++;
204 if (ntrp->lp > ntrp->bp)
205 ntrp->bp->wslen = 0;
206 return ntrp;
210 * Debugging
212 void
213 peektokens(Tokenrow * trp, char *str)
215 Token *tp;
217 tp = trp->tp;
218 flushout();
219 if (str)
220 fprintf(stderr, "%s ", str);
221 if (tp < trp->bp || tp > trp->lp)
222 fprintf(stderr, "(tp offset %ld) ", (long int) (tp - trp->bp));
223 for (tp = trp->bp; tp < trp->lp && tp < trp->bp + 32; tp++)
225 if (tp->type != NL)
227 int c = tp->t[tp->len];
229 tp->t[tp->len] = 0;
230 fprintf(stderr, "%s", tp->t);
231 tp->t[tp->len] = (uchar) c;
233 fprintf(stderr, tp == trp->tp ? "{%x*} " : "{%x} ", tp->type);
235 fprintf(stderr, "\n");
236 fflush(stderr);
239 void
240 puttokens(Tokenrow * trp)
242 Token *tp;
243 int len;
244 uchar *p;
246 if (Vflag)
247 peektokens(trp, "");
248 tp = trp->bp;
249 for (; tp < trp->lp; tp++)
251 if (tp->type != NL)
253 len = (int)(tp->len + tp->wslen);
254 p = tp->t - tp->wslen;
256 /* add parameter check to delete operator? */
257 if( Dflag )
259 if( (tp->type == NAME) && (strncmp( (char*)p, "delete", len ) == 0) )
261 Token* ntp = tp;
262 ntp++;
264 if( ntp->type == NAME )
266 uchar* np = ntp->t - ntp->wslen;
267 int nlen = (int)(ntp->len + ntp->wslen);
269 memcpy(wbp, "if(", 3 );
270 wbp += 4;
271 memcpy(wbp, np, nlen );
272 wbp += nlen;
273 memcpy(wbp, ")", 1 );
274 wbp++;
276 memcpy(wbp, p, len);
281 memcpy(wbp, p, len);
283 wbp += len;
285 else
286 *wbp++ = '\n';
288 if (wbp >= &wbuf[OBS])
290 if ( write(1, wbuf, OBS) != -1 )
292 if (wbp > &wbuf[OBS])
293 memmove(wbuf, wbuf + OBS, wbp - &wbuf[OBS]);
294 wbp -= OBS;
296 else exit(1);
299 trp->tp = tp;
300 if (cursource->fd == 0)
301 flushout();
304 void
305 flushout(void)
307 if (wbp > wbuf)
309 if ( write(1, wbuf, (int)(wbp - wbuf)) != -1)
310 wbp = wbuf;
311 else
312 exit(1);
317 * turn a row into just a newline
319 void
320 setempty(Tokenrow * trp)
322 trp->tp = trp->bp;
323 trp->lp = trp->bp + 1;
324 *trp->bp = nltoken;
328 * generate a number
330 char *
331 outnum(char *p, int n)
333 if (n >= 10)
334 p = outnum(p, n / 10);
335 *p++ = (char) (n % 10 + '0');
336 return p;
340 * allocate and initialize a new string from s, of length l, at offset o
341 * Null terminated.
343 uchar *
344 newstring(uchar const * s, size_t l, size_t o)
346 uchar *ns = (uchar *) domalloc(l + o + 1);
348 ns[l + o] = '\0';
349 return (uchar *) strncpy((char *) ns + o, (char *) s, l) - o;
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */