Qore Programming Language  0.9.16
InputStreamWrapper.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  InputStreamWrapper.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2016 - 2020 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_INPUTSTREAMWRAPPER_H
33 #define _QORE_INPUTSTREAMWRAPPER_H
34 
35 #include "qore/InputStream.h"
36 
41 
42 public:
47  InputStreamWrapper(QoreObject* self) : self(self) {
48  }
49 
50  DLLLOCAL virtual int64 read(void* ptr, int64 limit, ExceptionSink* xsink) override {
51  assert(limit > 0);
52  ReferenceHolder<QoreListNode> args(new QoreListNode(autoTypeInfo), xsink);
53  args->push(limit, xsink);
54  ValueHolder bufHolder(self->evalMethod("read", *args, xsink), xsink);
55  if (!bufHolder) {
56  return 0;
57  }
58  BinaryNode *buf = bufHolder->get<BinaryNode>();
59  qore_size_t count = buf->size();
60  if (count == 0) {
61  xsink->raiseException("INPUT-STREAM-ERROR",
62  "%s::read() returned an empty binary; NOTHING should be used to indicate the end of the stream",
63  self->getClassName());
64  return 0;
65  }
66  if (count > static_cast<qore_size_t>(limit)) {
67  xsink->raiseException("INPUT-STREAM-ERROR",
68  "%s::rRead() returned %lu bytes which is more than the specified limit of %lu",
69  self->getClassName(), count, static_cast<qore_size_t>(limit));
70  return 0;
71  }
72  memcpy(ptr, buf->getPtr(), count);
73  return count;
74  }
75 
76  DLLLOCAL virtual int64 peek(ExceptionSink *xsink) override {
77  ValueHolder resHolder(self->evalMethod("peek", nullptr, xsink), xsink);
78  if (!resHolder) {
79  return -2;
80  }
81  return resHolder->getAsBigInt();
82  }
83 
88  DLLLOCAL virtual const char* getName() override {
89  return "InputStreamWrapper";
90  }
91 
92 private:
93  QoreObject *self;
94 };
95 
96 #endif // _QORE_INPUTSTREAMWRAPPER_H
InputStreamWrapper::getName
virtual const DLLLOCAL char * getName() override
Returns the name of the class.
Definition: InputStreamWrapper.h:88
ReferenceHolder< QoreListNode >
qore_size_t
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:73
QoreListNode
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
int64
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
QoreObject::getClassName
const DLLEXPORT char * getClassName() const
returns the name of the class
BinaryNode::size
DLLEXPORT qore_size_t size() const
returns the number of bytes in the object
ExceptionSink::raiseException
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
QoreListNode::push
DLLEXPORT int push(QoreValue val, ExceptionSink *xsink)
adds a value to the list
InputStream
Interface for private data of input streams.
Definition: InputStream.h:44
QoreObject
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:61
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
InputStreamWrapper::read
virtual DLLLOCAL int64 read(void *ptr, int64 limit, ExceptionSink *xsink) override
Reads up to `limit` bytes from the input stream.
Definition: InputStreamWrapper.h:50
InputStreamWrapper::InputStreamWrapper
InputStreamWrapper(QoreObject *self)
Constructor.
Definition: InputStreamWrapper.h:47
BinaryNode::getPtr
const DLLEXPORT void * getPtr() const
returns the pointer to the data
ValueHolder
holds an object and dereferences it in the destructor
Definition: QoreValue.h:452
BinaryNode
holds arbitrary binary data
Definition: BinaryNode.h:41
InputStreamWrapper
Implements the private data for all subclasses of Qore::InputStream implemented in the Qore language.
Definition: InputStreamWrapper.h:40
QoreObject::evalMethod
DLLEXPORT QoreValue evalMethod(const QoreString *name, const QoreListNode *args, ExceptionSink *xsink)
evaluates the given method with the arguments passed and returns the return value,...
InputStreamWrapper::peek
virtual DLLLOCAL int64 peek(ExceptionSink *xsink) override
Peeks the next byte from the input stream.
Definition: InputStreamWrapper.h:76