Qore Programming Language  0.9.1
StringInputStream.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  StringInputStream.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_STRINGINPUTSTREAM_H
33 #define _QORE_STRINGINPUTSTREAM_H
34 
35 #include <stdint.h>
36 #include "qore/InputStream.h"
37 
42 
43 public:
44  DLLLOCAL StringInputStream(const QoreStringNode *str) : src(str->stringRefSelf()), offset(0) {
45  }
46 
47  DLLLOCAL const char *getName() override {
48  return "StringInputStream";
49  }
50 
51  DLLLOCAL int64 read(void *ptr, int64 limit, ExceptionSink *xsink) override {
52  assert(limit > 0);
53  qore_size_t count = src->size() - offset;
54  if (count == 0) {
55  return 0;
56  }
57  if (count > static_cast<qore_size_t>(limit)) {
58  count = limit;
59  }
60  memcpy(ptr, src->getBuffer() + offset, count);
61  offset += count;
62  return count;
63  }
64 
65  DLLLOCAL int64 peek(ExceptionSink* xsink) override {
66  if ((src->size() - offset) == 0) // No more data.
67  return -1;
68  return src->getBuffer()[offset];
69  }
70 
71  DLLLOCAL const QoreEncoding* getEncoding() const {
72  return src->getEncoding();
73  }
74 
75 private:
77  qore_size_t offset;
78 };
79 
80 #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:51
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:47
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:65
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)