You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SubstitutingInStream.h 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* 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. #ifndef __RDR_SUBSTITUTINGINSTREAM_H__
  19. #define __RDR_SUBSTITUTINGINSTREAM_H__
  20. #include <rdr/InStream.h>
  21. #include <rdr/Exception.h>
  22. namespace rdr {
  23. class Substitutor {
  24. public:
  25. virtual char* substitute(const char* varName) = 0;
  26. };
  27. class SubstitutingInStream : public InStream {
  28. public:
  29. SubstitutingInStream(InStream* underlying_, Substitutor* s,
  30. int maxVarNameLen_)
  31. : underlying(underlying_), dollar(0), substitutor(s), subst(0),
  32. maxVarNameLen(maxVarNameLen_)
  33. {
  34. ptr = end = underlying->getptr();
  35. varName = new char[maxVarNameLen+1];
  36. }
  37. ~SubstitutingInStream() {
  38. delete underlying;
  39. delete [] varName;
  40. delete [] subst;
  41. }
  42. int pos() { return underlying->pos(); }
  43. virtual int overrun(int itemSize, int nItems, bool wait=true) {
  44. if (itemSize != 1)
  45. throw new rdr::Exception("SubstitutingInStream: itemSize must be 1");
  46. if (subst) {
  47. delete [] subst;
  48. subst = 0;
  49. } else {
  50. underlying->setptr(ptr);
  51. }
  52. underlying->check(1);
  53. ptr = underlying->getptr();
  54. end = underlying->getend();
  55. dollar = (const U8*)memchr(ptr, '$', end-ptr);
  56. if (dollar) {
  57. if (dollar == ptr) {
  58. try {
  59. int i = 0;
  60. while (i < maxVarNameLen) {
  61. varName[i++] = underlying->readS8();
  62. varName[i] = 0;
  63. subst = substitutor->substitute(varName);
  64. if (subst) {
  65. ptr = (U8*)subst;
  66. end = (U8*)subst + strlen(subst);
  67. break;
  68. }
  69. }
  70. } catch (EndOfStream&) {
  71. }
  72. if (!subst)
  73. dollar = (const U8*)memchr(ptr+1, '$', end-ptr-1);
  74. }
  75. if (!subst && dollar) end = dollar;
  76. }
  77. if (itemSize * nItems > end - ptr)
  78. nItems = (end - ptr) / itemSize;
  79. return nItems;
  80. }
  81. InStream* underlying;
  82. const U8* dollar;
  83. Substitutor* substitutor;
  84. char* varName;
  85. char* subst;
  86. int maxVarNameLen;
  87. };
  88. }
  89. #endif