-
Notifications
You must be signed in to change notification settings - Fork 0
/
QoreYamlEmitter.cpp
200 lines (162 loc) · 5.49 KB
/
QoreYamlEmitter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* indent-tabs-mode: nil -*- */
/*
yaml Qore module
Copyright (C) 2010 - 2017 Qore Technologies, s.r.o.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "yaml-module.h"
const char *QY_EMIT_ERR = "YAML-EMITTER-ERROR";
static int qore_yaml_write_handler(QoreYamlWriteHandler *wh, unsigned char *buffer, size_t size) {
return wh->write(buffer, size);
}
QoreYamlEmitter::QoreYamlEmitter(QoreYamlWriteHandler &n_wh, int flags, int width, int indent, ExceptionSink *n_xsink)
: QoreYamlBase(n_xsink), wh(n_wh), block(flags & QYE_BLOCK_STYLE),
implicit_start_doc(!(flags & QYE_EXPLICIT_START_DOC)),
implicit_end_doc(!(flags & QYE_EXPLICIT_END_DOC)),
emit_sqlnull(flags & QYE_EMIT_SQLNULL),
yaml_ver(0) {
if (!yaml_emitter_initialize(&emitter)) {
err("unknown error initializing yaml emitter");
return;
}
if (flags & QYE_VER_1_1 && flags & QYE_VER_1_2) {
err("cannot set both Yaml1_1 and Yaml1_2 version flags");
return;
}
if (flags & QYE_CANONICAL)
setCanonical();
if (!(flags & QYE_ESCAPE_UNICODE))
setUnicode();
if (flags & QYE_VER_1_2)
yaml_ver = &yaml_ver_1_2;
else if (flags & QYE_VER_1_1)
yaml_ver = &yaml_ver_1_1;
else if (flags & QYE_VER_1_0)
yaml_ver = &yaml_ver_1_0;
yaml_emitter_set_output(&emitter, (yaml_write_handler_t*)qore_yaml_write_handler, &wh);
//printd(5, "QoreYamlEmitter::QoreYamlEmitter() indent=%d width=%d\n", indent, width);
yaml_emitter_set_indent(&emitter, indent);
yaml_emitter_set_width(&emitter, width);
if (!streamStart() && !docStart())
valid = true;
}
int QoreYamlEmitter::emit(const QoreValue& v) {
switch (v.getType()) {
case NT_STRING:
return emitValue(*v.get<const QoreStringNode>());
case NT_INT:
return emitValue(v.getAsBigInt());
case NT_FLOAT:
return emitValue(v.getAsFloat());
case NT_NUMBER:
return emitValue(*v.get<const QoreNumberNode>());
case NT_BOOLEAN:
return emitValue(v.getAsBool());
case NT_LIST:
return emitValue(*v.get<const QoreListNode>());
case NT_HASH:
return emitValue(*v.get<const QoreHashNode>());
case NT_DATE:
return emitValue(*v.get<const DateTimeNode>());
case NT_BINARY:
return emitValue(*v.get<const BinaryNode>());
case NT_NULL:
if (emit_sqlnull)
return emitSqlNull();
// fall down to nothing
case NT_NOTHING:
return emitNull();
default:
err("cannot convert Qore type '%s' to YAML", v.getTypeName());
return -1;
}
return 0;
}
int QoreYamlEmitter::emitValue(const DateTime &d) {
qore_tm info;
d.getInfo(info);
QoreString str(QCS_UTF8);
if (d.isRelative()) {
str.concat('P');
if (d.hasValue()) {
if (info.year)
str.sprintf("%dY", info.year);
if (info.month)
str.sprintf("%dM", info.month);
if (info.day)
str.sprintf("%dD", info.day);
bool has_t = false;
if (info.hour) {
str.sprintf("T%dH", info.hour);
has_t = true;
}
if (info.minute) {
if (!has_t) {
str.concat('T');
has_t = true;
}
str.sprintf("%dM", info.minute);
}
if (info.second) {
if (!has_t) {
str.concat('T');
has_t = true;
}
str.sprintf("%dS", info.second);
}
if (info.us) {
if (!has_t) {
str.concat('T');
has_t = true;
}
str.sprintf("%du", info.us);
}
}
else
str.concat("0D");
return emitScalar(str, QORE_YAML_DURATION_TAG);
}
// shorthand type name
if (emitter.canonical) {
d.format(str, "YYYY-MM-DDTHH:mm:SS.xx");
}
else {
d.format(str, "YYYY-MM-DD");
if (!info.isTimeNull() || info.secsEast()) {
// use spaces for enhanced readability
if (!info.us)
d.format(str, " HH:mm:SS.");
else if (!(info.us % 1000))
d.format(str, " HH:mm:SS.ms");
else
d.format(str, " HH:mm:SS.xx");
}
}
// remove trailing zeros from microseconds time
str.trim_trailing('0');
// remove empty decimal point if us == 0
str.trim_trailing('.');
// if not emitting the canonical format and there is a time zone offset,
// then add a space
if (!emitter.canonical) {
if (!info.isTimeNull() || info.secsEast()) {
str.concat(' ');
// add time zone offset (or "Z")
d.format(str, "Z");
}
}
else
d.format(str, "Z");
// issue #2343: to avoid ambiguity with single quoted strings, we always use the tag here
return emitScalar(str, YAML_TIMESTAMP_TAG);
}