blob: 325b01c5e9df6a2eff2866aff4fab4cc0e134e55 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#ifndef __RDR_SUBSTITUTINGINSTREAM_H__
20#define __RDR_SUBSTITUTINGINSTREAM_H__
21
22#include <rdr/InStream.h>
23#include <rdr/Exception.h>
24
25namespace rdr {
26
27 class Substitutor {
28 public:
29 virtual char* substitute(const char* varName) = 0;
30 };
31
32 class SubstitutingInStream : public InStream {
33 public:
34 SubstitutingInStream(InStream* underlying_, Substitutor* s,
35 int maxVarNameLen_)
36 : underlying(underlying_), dollar(0), substitutor(s), subst(0),
37 maxVarNameLen(maxVarNameLen_)
38 {
39 ptr = end = underlying->getptr();
40 varName = new char[maxVarNameLen+1];
41 }
42 ~SubstitutingInStream() {
43 delete underlying;
44 delete [] varName;
45 delete [] subst;
46 }
47
48 int pos() { return underlying->pos(); }
49
50 virtual int overrun(int itemSize, int nItems, bool wait=true) {
51 if (itemSize != 1)
52 throw new rdr::Exception("SubstitutingInStream: itemSize must be 1");
53
54 if (subst) {
55 delete [] subst;
56 subst = 0;
57 } else {
58 underlying->setptr(ptr);
59 }
60
61 underlying->check(1);
62 ptr = underlying->getptr();
63 end = underlying->getend();
64 dollar = (const U8*)memchr(ptr, '$', end-ptr);
65 if (dollar) {
66 if (dollar == ptr) {
67 try {
68 int i = 0;
69 while (i < maxVarNameLen) {
70 varName[i++] = underlying->readS8();
71 varName[i] = 0;
72 subst = substitutor->substitute(varName);
73 if (subst) {
74 ptr = (U8*)subst;
75 end = (U8*)subst + strlen(subst);
76 break;
77 }
78 }
79 } catch (EndOfStream&) {
80 }
81
82 if (!subst)
83 dollar = (const U8*)memchr(ptr+1, '$', end-ptr-1);
84 }
85 if (!subst && dollar) end = dollar;
86 }
87
88 if (itemSize * nItems > end - ptr)
89 nItems = (end - ptr) / itemSize;
90
91 return nItems;
92 }
93
94 InStream* underlying;
95 const U8* dollar;
96 Substitutor* substitutor;
97 char* varName;
98 char* subst;
99 int maxVarNameLen;
100 };
101}
102#endif