Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
StreamWriter.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 StreamWriter.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_STREAMWRITER_H
33#define _QORE_STREAMWRITER_H
34
35#include <cstdint>
36
37#include "qore/qore_bitopts.h"
38#include "qore/OutputStream.h"
39
44public:
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 OutputStream* getOutputStream() {
55 return *out;
56 }
57
58 DLLLOCAL const OutputStream* getOutputStream() const {
59 return *out;
60 }
61
62 DLLLOCAL int write(const void* ptr, int64 count, ExceptionSink* xsink) {
63 // OutputStream uses assertion but we need rather raise exception not to coredump
64 if (!out->check(xsink)) {
65 return -1;
66 }
67 out->write(ptr, count, xsink);
68 return *xsink ? -1 : 0;
69 }
70
71 DLLLOCAL int print(const QoreStringNode* str, ExceptionSink* xsink) {
72 TempEncodingHelper stmp(str, encoding, xsink);
73 return write(stmp->getBuffer(), stmp->size(), xsink);
74 }
75
76 DLLLOCAL int printf(const QoreListNode* args, ExceptionSink* xsink) {
77 SimpleRefHolder<QoreStringNode> str(q_sprintf(args, 0, 0, xsink));
78 return str ? print(*str, xsink) : 0;
79 }
80
81 DLLLOCAL int vprintf(const QoreListNode* args, ExceptionSink* xsink) {
82 SimpleRefHolder<QoreStringNode> str(q_vsprintf(args, 0, 0, xsink));
83 return str ? print(*str, xsink) : 0;
84 }
85
86 DLLLOCAL int f_printf(const QoreListNode* args, ExceptionSink* xsink) {
87 SimpleRefHolder<QoreStringNode> str(q_sprintf(args, 1, 0, xsink));
88 return str ? print(*str, xsink) : 0;
89 }
90
91 DLLLOCAL int f_vprintf(const QoreListNode* args, ExceptionSink* xsink) {
92 SimpleRefHolder<QoreStringNode> str(q_vsprintf(args, 1, 0, xsink));
93 return str ? print(*str, xsink) : 0;
94 }
95
96 DLLLOCAL int write(const BinaryNode* b, ExceptionSink* xsink) {
97 write(b->getPtr(), b->size(), xsink);
98 return *xsink ? -1 : 0;
99 }
100
101 DLLLOCAL int writei1(signed char i, ExceptionSink* xsink) {
102 write(&i, 1, xsink);
103 return *xsink ? -1 : 0;
104 }
105
106 DLLLOCAL int writei2(int16_t i, ExceptionSink* xsink) {
107 i = htons(i);
108 write(&i, 2, xsink);
109 return *xsink ? -1 : 0;
110 }
111
112 DLLLOCAL int writei4(int32_t i, ExceptionSink* xsink) {
113 i = htonl(i);
114 write(&i, 4, xsink);
115 return *xsink ? -1 : 0;
116 }
117
118 DLLLOCAL int writei8(int64 i, ExceptionSink* xsink) {
119 i = i8MSB(i);
120 write(&i, 8, xsink);
121 return *xsink ? -1 : 0;
122 }
123
124 DLLLOCAL int writei2LSB(int16_t i, ExceptionSink* xsink) {
125 i = i2LSB(i);
126 write(&i, 2, xsink);
127 return *xsink ? -1 : 0;
128 }
129
130 DLLLOCAL int writei4LSB(int32_t i, ExceptionSink* xsink) {
131 i = i4LSB(i);
132 write(&i, 4, xsink);
133 return *xsink ? -1 : 0;
134 }
135
136 DLLLOCAL int writei8LSB(int64 i, ExceptionSink* xsink) {
137 i = i8LSB(i);
138 write(&i, 8, xsink);
139 return *xsink ? -1 : 0;
140 }
141
142 DLLLOCAL int writeu1(unsigned char i, ExceptionSink* xsink) {
143 return writei1((signed char)i, xsink);
144 }
145
146 DLLLOCAL int writeu2(uint16_t i, ExceptionSink* xsink) {
147 return writei2((signed char)i, xsink);
148 }
149
150 DLLLOCAL int writeu4(uint32_t i, ExceptionSink* xsink) {
151 return writei4((signed char)i, xsink);
152 }
153
154 DLLLOCAL virtual const char* getName() const { return "StreamWriter"; }
155
156private:
158 const QoreEncoding* encoding;
159};
160
161#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:50
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 const char * getBuffer() 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
a templated class to manage a reference count of an object that can throw a Qore-language exception w...
Definition: ReferenceHolder.h:52
manages a reference count of a pointer to a class that takes a simple "deref()" call with no argument...
Definition: ReferenceHolder.h:127
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:1125
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