Qore Programming Language  0.9.3.1
StringInputStream.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  StringInputStream.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2016 - 2019 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_STRINGINPUTSTREAM_H
33 #define _QORE_STRINGINPUTSTREAM_H
34 
35 #include <stdint.h>
36 #include "qore/InputStream.h"
37 
42 public:
43  DLLLOCAL StringInputStream(const QoreStringNode *str) : src(str->stringRefSelf()) {
44  }
45 
46  DLLLOCAL const char *getName() override {
47  return "StringInputStream";
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, src->getBuffer() + 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 src->getBuffer()[offset];
68  }
69 
70  DLLLOCAL const QoreEncoding* getEncoding() const {
71  return src->getEncoding();
72  }
73 
74 private:
76  qore_size_t offset = 0;
77 };
78 
79 #endif // _QORE_STRINGINPUTSTREAM_H
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
DLLEXPORT QoreStringNode * stringRefSelf() const
references the object and returns a non-const pointer to "this"
Private data for the Qore::StringInputStream class.
Definition: StringInputStream.h:41
DLLLOCAL int64 read(void *ptr, int64 limit, ExceptionSink *xsink) override
Reads up to `limit` bytes from the input stream.
Definition: StringInputStream.h:50
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:73
Qore&#39;s string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
DLLLOCAL const char * getName() override
Returns the name of the class.
Definition: StringInputStream.h:46
DLLEXPORT const char * getBuffer() const
returns the string&#39;s buffer; this data should not be changed
DLLLOCAL int64 peek(ExceptionSink *xsink) override
Peeks the next byte from the input stream.
Definition: StringInputStream.h:64
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 QoreEncoding * getEncoding() const
returns the encoding for the string
DLLEXPORT qore_size_t size() const
returns number of bytes in the string (not including the null pointer)