Qore Programming Language  1.7.0
InputStreamWrapper.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  InputStreamWrapper.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2016 - 2022 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  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<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<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
holds arbitrary binary data
Definition: BinaryNode.h:41
DLLEXPORT size_t size() const
returns the number of bytes in the object
DLLEXPORT const void * getPtr() const
returns the pointer to the data
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
Interface for private data of input streams.
Definition: InputStream.h:44
Implements the private data for all subclasses of Qore::InputStream implemented in the Qore language.
Definition: InputStreamWrapper.h:40
InputStreamWrapper(QoreObject *self)
Constructor.
Definition: InputStreamWrapper.h:47
virtual DLLLOCAL const char * getName() override
Returns the name of the class.
Definition: InputStreamWrapper.h:88
virtual DLLLOCAL int64 peek(ExceptionSink *xsink) override
Peeks the next byte from the input stream.
Definition: InputStreamWrapper.h:76
virtual DLLLOCAL int64 read(void *ptr, int64 limit, ExceptionSink *xsink) override
Reads up to `limit` bytes from the input stream.
Definition: InputStreamWrapper.h:50
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLEXPORT int push(QoreValue val, ExceptionSink *xsink)
adds a value to the list
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:60
DLLEXPORT const char * getClassName() const
returns the name of the class
DLLEXPORT QoreValue evalMethod(const QoreString *name, const QoreListNode *args, ExceptionSink *xsink)
evaluates the given method with the arguments passed and returns the return value,...
holds an object and dereferences it in the destructor
Definition: QoreValue.h:476
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