Qore Programming Language 1.14.0
Loading...
Searching...
No Matches
BinaryInputStream.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 BinaryInputStream.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_BINARYINPUTSTREAM_H
33#define _QORE_BINARYINPUTSTREAM_H
34
35#include <stdint.h>
36#include "qore/InputStream.h"
37
42public:
43 DLLLOCAL BinaryInputStream(const QoreValue& src) {
44 assert(src);
45 if (src.getType() == NT_STRING) {
46 const QoreStringNode* str = src.get<const QoreStringNode>();
47 this->src = str->stringRefSelf();
48 ptr = str->c_str();
49 len = str->size();
50 } else {
51 assert(src.getType() == NT_BINARY);
52 const BinaryNode* bin = src.get<const BinaryNode>();
53 this->src = bin->binRefSelf();
54 ptr = bin->getPtr();
55 len = bin->size();
56 }
57 }
58
59 DLLLOCAL const char* getName() override {
60 return "BinaryInputStream";
61 }
62
63 DLLLOCAL int64 read(void* ptr, int64 limit, ExceptionSink* xsink) override {
64 assert(limit > 0);
65 size_t count = len - offset;
66 if (count == 0) {
67 return 0;
68 }
69 if (count > static_cast<size_t>(limit)) {
70 count = limit;
71 }
72 memcpy(ptr, static_cast<const uint8_t*>(this->ptr) + offset, count);
73 offset += count;
74 return count;
75 }
76
77 DLLLOCAL int64 peek(ExceptionSink* xsink) override {
78 if ((len - offset) == 0) // No more data.
79 return -1;
80 return static_cast<const char*>(ptr)[offset];
81 }
82
83private:
84 const void* ptr;
85 size_t len;
87 size_t offset = 0;
88};
89
90#endif // _QORE_BINARYINPUTSTREAM_H
Private data for the Qore::BinaryInputStream class.
Definition: BinaryInputStream.h:41
DLLLOCAL const char * getName() override
Returns the name of the class.
Definition: BinaryInputStream.h:59
DLLLOCAL int64 peek(ExceptionSink *xsink) override
Peeks the next byte from the input stream.
Definition: BinaryInputStream.h:77
DLLLOCAL int64 read(void *ptr, int64 limit, ExceptionSink *xsink) override
Reads up to `limit` bytes from the input stream.
Definition: BinaryInputStream.h:63
holds arbitrary binary data
Definition: BinaryNode.h:41
DLLEXPORT size_t size() const
returns the number of bytes in the object
DLLEXPORT BinaryNode * binRefSelf() const
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:50
Interface for private data of input streams.
Definition: InputStream.h:44
DLLLOCAL detail::QoreValueCastHelper< T >::Result get()
returns the value as the given type
Definition: QoreValue.h:214
DLLEXPORT qore_type_t getType() const
returns the type of value contained
DLLEXPORT const char * c_str() const
returns the string's buffer; this data should not be changed
DLLEXPORT size_t size() const
returns number of bytes in the string (not including the null pointer)
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
DLLEXPORT QoreStringNode * stringRefSelf() const
references the object and returns a non-const pointer to "this"
manages a reference count of a pointer to a class that takes a simple "deref()" call with no argument...
Definition: ReferenceHolder.h:127
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
const qore_type_t NT_BINARY
type value for BinaryNode
Definition: node_types.h:49
const qore_type_t NT_STRING
type value for QoreStringNode
Definition: node_types.h:45
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:276