Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
TransformInputStream.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 TransformInputStream.h
4
5 Qore Programming Language
6
7 Copyright (C) 2016 - 2023 Qore Technologies, s.r.o.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30*/
31
32#ifndef _QORE_TRANSFORMINPUTSTREAM_H
33#define _QORE_TRANSFORMINPUTSTREAM_H
34
35#include <cassert>
36#include "qore/InputStream.h"
37#include "qore/Transform.h"
38
39class TransformInputStream : public InputStream {
40public:
41 DLLLOCAL TransformInputStream(InputStream *is, Transform *t) :
42 is(is),
43 t(t),
44 inBufSize(t->inputBufferSize()),
45 outBufSize(t->outputBufferSize()),
46 buf(new char[inBufSize]),
47 outBuf(new char[outBufSize]) {
48 }
49
50 DLLLOCAL ~TransformInputStream() {
51 delete [] buf;
52 delete [] outBuf;
53 }
54
55 DLLLOCAL const char *getName() override {
56 return "TransformInputStream";
57 }
58
59 DLLLOCAL int64 read(void *ptr, int64 limit, ExceptionSink *xsink) override {
60 if (outBufCount > 0) {
61 return readFromBuffer(ptr, limit);
62 }
63 while (true) {
64 if (!eof && inBufSize - bufCount > 0) {
65 int64 r = is->read(buf + bufCount, inBufSize - bufCount, xsink);
66 if (*xsink) {
67 return 0;
68 }
69 if (!r) {
70 eof = true;
71 } else {
72 bufCount += r;
73 }
74 }
75
76 assert(eof || bufCount > 0);
77 // issue #3111: do not read directly into output buffer, transformations have a fixed output size;
78 // read into the object's output buffer and then read this into ptr
79 std::pair<int64, int64> r = t->apply(bufCount ? buf : nullptr, bufCount, outBuf, outBufSize, xsink);
80 //printd(5, "TransformInputStream::read() ptr: %p limit: %lld xsink: %d buf: %p bufCount: %lu r: {%lld, %lld}\n", ptr, limit, (bool)*xsink, bufCount ? buf : nullptr, bufCount, r.first, r.second);
81 if (*xsink) {
82 return 0;
83 }
84 if (r.first) {
85 bufCount -= r.first;
86 memmove(buf, buf + r.first, bufCount);
87 }
88 if (r.second) {
89 outBufCount = r.second;
90 return readFromBuffer(ptr, limit);
91 }
92 if (!r.first) {
93 //did not produce anything and did not read anything
94 assert(eof);
95 assert(!bufCount);
96 return 0;
97 }
98 }
99 }
100
101 DLLLOCAL int64 peek(ExceptionSink* xsink) override {
102 if (outBufCount > 0)
103 return outBuf[outBufOffset];
104 int64 rc = read(outBuf, outBufSize, xsink);
105 if (*xsink)
106 return -2;
107 if (rc == 0) {
108 eof = true;
109 return -1;
110 }
111 outBufCount += rc;
112 return outBuf[0];
113 }
114
115private:
118 size_t inBufSize;
119 size_t outBufSize;
120 size_t outBufOffset = 0;
121 char* buf;
122 char* outBuf;
123 size_t bufCount = 0;
124 size_t outBufCount = 0;
125 bool eof = false;
126
127 DLLLOCAL int64 readFromBuffer(void *ptr, int64 limit) {
128 assert(outBufCount);
129 int64 toCopy = QORE_MIN(static_cast<int64>(outBufCount), limit);
130 memcpy(ptr, outBuf + outBufOffset, toCopy);
131 // shift out buffer
132 outBufCount -= toCopy;
133 if (!outBufCount) {
134 outBufOffset = 0;
135 } else {
136 outBufOffset += toCopy;
137 }
138 return toCopy;
139 }
140};
141
142#endif // _QORE_TRANSFORMINPUTSTREAM_H
#define QORE_MIN(a, b)
macro to return the minimum of 2 numbers
Definition: QoreLib.h:616
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
Interface for private data of input streams.
Definition: InputStream.h:44
virtual int64 peek(ExceptionSink *xsink)=0
Peeks the next byte from the input stream.
virtual int64 read(void *ptr, int64 limit, ExceptionSink *xsink)=0
Reads up to `limit` bytes from the input stream.
manages a reference count of a pointer to a class that takes a simple "deref()" call with no argument...
Definition: ReferenceHolder.h:127
virtual DLLLOCAL const char * getName()=0
Returns the name of the class.
Interface for private data of transformations.
Definition: Transform.h:40
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260