1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
25 #if (defined(_WIN32) || defined(__IBMC__))
33 static char wbuf
[4 * OBS
];
34 static char *wbp
= wbuf
;
37 maketokenrow(int size
, Tokenrow
* trp
)
41 trp
->bp
= (Token
*) domalloc(size
* sizeof(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
];
63 * Compare a row of tokens, ignoring the content of WS; return !=0 if different
66 comparetokens(Tokenrow
* tr1
, Tokenrow
* tr2
)
72 if (tr1
->lp
- tp1
!= tr2
->lp
- tp2
)
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)
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.
91 insertrow(Tokenrow
* dtr
, int ntok
, Tokenrow
const * str
)
93 int nrtok
= (int)rowlen(str
);
96 adjustrow(dtr
, nrtok
- ntok
);
98 movetokenrow(dtr
, str
);
103 * make sure there is WS before trp->tp, if tokens might merge in the output
106 makespace(Tokenrow
* trp
, Token
const * ntp
)
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.
129 movetokenrow(Tokenrow
* dtr
, Tokenrow
const * str
)
133 nby
= (char *) str
->lp
- (char *) str
->bp
;
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
145 adjustrow(Tokenrow
* trp
, int nt
)
151 size
= (trp
->lp
- trp
->bp
) + nt
;
152 while (size
> trp
->max
)
154 nby
= (char *) trp
->lp
- (char *) trp
->tp
;
156 memmove(trp
->tp
+ nt
, trp
->tp
, nby
);
161 * Copy a row of tokens into the destination holder, allocating
162 * the space for the contents. Return the destination.
165 copytokenrow(Tokenrow
* dtr
, Tokenrow
const * str
)
167 int len
= (int)rowlen(str
);
169 maketokenrow(len
, dtr
);
170 movetokenrow(dtr
, str
);
177 * Produce a copy of a row of tokens. Start at trp->tp.
178 * The value strings are copied as well. The first token
182 normtokenrow(Tokenrow
* trp
)
185 Tokenrow
*ntrp
= new(Tokenrow
);
188 len
= (int)(trp
->lp
- trp
->tp
);
191 maketokenrow(len
, ntrp
);
192 for (tp
= trp
->tp
; tp
< trp
->lp
; tp
++)
197 ntrp
->lp
->t
= newstring(tp
->t
, tp
->len
, 1);
198 *ntrp
->lp
->t
++ = ' ';
204 if (ntrp
->lp
> ntrp
->bp
)
213 peektokens(Tokenrow
* trp
, char *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
++)
227 int c
= tp
->t
[tp
->len
];
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");
240 puttokens(Tokenrow
* trp
)
249 for (; tp
< trp
->lp
; tp
++)
253 len
= (int)(tp
->len
+ tp
->wslen
);
254 p
= tp
->t
- tp
->wslen
;
256 /* add parameter check to delete operator? */
259 if( (tp
->type
== NAME
) && (strncmp( (char*)p
, "delete", len
) == 0) )
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 );
271 memcpy(wbp
, np
, nlen
);
273 memcpy(wbp
, ")", 1 );
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
]);
300 if (cursource
->fd
== 0)
309 if ( write(1, wbuf
, (int)(wbp
- wbuf
)) != -1)
317 * turn a row into just a newline
320 setempty(Tokenrow
* trp
)
323 trp
->lp
= trp
->bp
+ 1;
331 outnum(char *p
, int n
)
334 p
= outnum(p
, n
/ 10);
335 *p
++ = (char) (n
% 10 + '0');
340 * allocate and initialize a new string from s, of length l, at offset o
344 newstring(uchar
const * s
, size_t l
, size_t o
)
346 uchar
*ns
= (uchar
*) domalloc(l
+ o
+ 1);
349 return (uchar
*) strncpy((char *) ns
+ o
, (char *) s
, l
) - o
;
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */