Qore Programming Language  1.12.0
StreamWriter.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  StreamWriter.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_STREAMWRITER_H
33 #define _QORE_STREAMWRITER_H
34 
35 #include <cstdint>
36 
37 #include "qore/qore_bitopts.h"
38 #include "qore/OutputStream.h"
39 
44 public:
45  DLLLOCAL StreamWriter(ExceptionSink* xsink, OutputStream* os, const QoreEncoding* enc = QCS_DEFAULT) :
46  out(os, xsink),
47  encoding(enc) {
48  }
49 
50  DLLLOCAL const QoreEncoding* getEncoding() const {
51  return encoding;
52  }
53 
54  DLLLOCAL const OutputStream* getOutputStream() const {
55  return *out;
56  }
57 
58  DLLLOCAL int write(const void *ptr, int64 count, ExceptionSink *xsink) {
59  // OutputStream uses assertion but we need rather raise exception not to coredump
60  if (!out->check(xsink)) {
61  return -1;
62  }
63  out->write(ptr, count, xsink);
64  return *xsink ? -1 : 0;
65  }
66 
67  DLLLOCAL int print(const QoreStringNode* str, ExceptionSink* xsink) {
68  TempEncodingHelper stmp(str, encoding, xsink);
69  return write(stmp->getBuffer(), stmp->size(), xsink);
70  }
71 
72  DLLLOCAL int printf(const QoreListNode* args, ExceptionSink* xsink) {
73  SimpleRefHolder<QoreStringNode> str(q_sprintf(args, 0, 0, xsink));
74  return str ? print(*str, xsink) : 0;
75  }
76 
77  DLLLOCAL int vprintf(const QoreListNode* args, ExceptionSink* xsink) {
78  SimpleRefHolder<QoreStringNode> str(q_vsprintf(args, 0, 0, xsink));
79  return str ? print(*str, xsink) : 0;
80  }
81 
82  DLLLOCAL int f_printf(const QoreListNode* args, ExceptionSink* xsink) {
83  SimpleRefHolder<QoreStringNode> str(q_sprintf(args, 1, 0, xsink));
84  return str ? print(*str, xsink) : 0;
85  }
86 
87  DLLLOCAL int f_vprintf(const QoreListNode* args, ExceptionSink* xsink) {
88  SimpleRefHolder<QoreStringNode> str(q_vsprintf(args, 1, 0, xsink));
89  return str ? print(*str, xsink) : 0;
90  }
91 
92  DLLLOCAL int write(const BinaryNode* b, ExceptionSink* xsink) {
93  write(b->getPtr(), b->size(), xsink);
94  return *xsink ? -1 : 0;
95  }
96 
97  DLLLOCAL int writei1(signed char i, ExceptionSink* xsink) {
98  write(&i, 1, xsink);
99  return *xsink ? -1 : 0;
100  }
101 
102  DLLLOCAL int writei2(int16_t i, ExceptionSink* xsink) {
103  i = htons(i);
104  write(&i, 2, xsink);
105  return *xsink ? -1 : 0;
106  }
107 
108  DLLLOCAL int writei4(int32_t i, ExceptionSink* xsink) {
109  i = htonl(i);
110  write(&i, 4, xsink);
111  return *xsink ? -1 : 0;
112  }
113 
114  DLLLOCAL int writei8(int64 i, ExceptionSink* xsink) {
115  i = i8MSB(i);
116  write(&i, 8, xsink);
117  return *xsink ? -1 : 0;
118  }
119 
120  DLLLOCAL int writei2LSB(int16_t i, ExceptionSink* xsink) {
121  i = i2LSB(i);
122  write(&i, 2, xsink);
123  return *xsink ? -1 : 0;
124  }
125 
126  DLLLOCAL int writei4LSB(int32_t i, ExceptionSink* xsink) {
127  i = i4LSB(i);
128  write(&i, 4, xsink);
129  return *xsink ? -1 : 0;
130  }
131 
132  DLLLOCAL int writei8LSB(int64 i, ExceptionSink* xsink) {
133  i = i8LSB(i);
134  write(&i, 8, xsink);
135  return *xsink ? -1 : 0;
136  }
137 
138  DLLLOCAL virtual const char* getName() const { return "StreamWriter"; }
139 
140 private:
142  const QoreEncoding* encoding;
143 };
144 
145 #endif // _QORE_STREAMWRITER_H
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
DLLEXPORT QoreStringNode * q_sprintf(const QoreListNode *params, int field, int offset, ExceptionSink *xsink)
a string formatting function that works with Qore data structures
DLLEXPORT QoreStringNode * q_vsprintf(const QoreListNode *params, int field, int offset, ExceptionSink *xsink)
a string formatting function that works with Qore data structures
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:44
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
Interface for private data of output streams.
Definition: OutputStream.h:44
virtual void write(const void *ptr, int64 count, ExceptionSink *xsink)=0
Writes bytes to the output stream.
DLLLOCAL bool check(ExceptionSink *xsink)
Checks that the current thread is the same as when the instance was created or assigned via unassignT...
Definition: OutputStream.h:54
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLEXPORT size_t size() const
returns number of bytes in the string (not including the null pointer)
DLLEXPORT const char * getBuffer() const
returns the string's buffer; this data should not be changed
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
Private data for the Qore::StreamWriter class.
Definition: StreamWriter.h:43
use this class to manage strings where the character encoding must be specified and may be different ...
Definition: QoreString.h:1104
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