Qore Programming Language  0.9.1
BinaryInputStream.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  BinaryInputStream.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2016 - 2018 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 
42 public:
43  DLLLOCAL BinaryInputStream(BinaryNode* src) : src(src), offset(0) {
44  }
45 
46  DLLLOCAL const char* getName() override {
47  return "BinaryInputStream";
48  }
49 
50  DLLLOCAL int64 read(void* ptr, int64 limit, ExceptionSink* xsink) override {
51  assert(limit > 0);
52  qore_size_t count = src->size() - offset;
53  if (count == 0) {
54  return 0;
55  }
56  if (count > static_cast<qore_size_t>(limit)) {
57  count = limit;
58  }
59  memcpy(ptr, static_cast<const uint8_t*>(src->getPtr()) + offset, count);
60  offset += count;
61  return count;
62  }
63 
64  DLLLOCAL int64 peek(ExceptionSink* xsink) override {
65  if ((src->size() - offset) == 0) // No more data.
66  return -1;
67  return static_cast<const char*>(src->getPtr())[offset];
68  }
69 
70 private:
72  qore_size_t offset;
73 };
74 
75 #endif // _QORE_BINARYINPUTSTREAM_H
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:73
DLLLOCAL int64 peek(ExceptionSink *xsink) override
Peeks the next byte from the input stream.
Definition: BinaryInputStream.h:64
DLLLOCAL const char * getName() override
Returns the name of the class.
Definition: BinaryInputStream.h:46
DLLLOCAL int64 read(void *ptr, int64 limit, ExceptionSink *xsink) override
Reads up to `limit` bytes from the input stream.
Definition: BinaryInputStream.h:50
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
Interface for private data of input streams.
Definition: InputStream.h:44
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
DLLEXPORT const void * getPtr() const
returns the pointer to the data
Private data for the Qore::BinaryInputStream class.
Definition: BinaryInputStream.h:41
DLLEXPORT qore_size_t size() const
returns the number of bytes in the object
holds arbitrary binary data
Definition: BinaryNode.h:41