"driver/driver.cpp" did not exist on "96ee9571e2c96ba6eb6972da1be75453d6c6e9fa"
emitter.cpp 6.42 KB
Newer Older
1
#include "yaml-cpp/emitter.h"
2
3
4
#include "emitterstate.h"
#include "emitterutils.h"
#include "indentation.h"
5
#include "yaml-cpp/exceptions.h"
6
7
8
9
10
11
12
13
14
15
16
17
#include <sstream>

namespace YAML
{	
	Emitter::Emitter(): m_pState(new EmitterState)
	{
	}
	
	Emitter::~Emitter()
	{
	}
	
18
	const char *Emitter::c_str() const
19
	{
20
		return m_stream.str();
21
22
	}
	
23
	unsigned Emitter::size() const
24
	{
25
		return m_stream.pos();
26
27
28
29
30
31
32
33
34
35
36
37
38
39
	}
	
	// state checking
	bool Emitter::good() const
	{
		return m_pState->good();
	}
	
	const std::string Emitter::GetLastError() const
	{
		return m_pState->GetLastError();
	}

	// global setters
40
41
	bool Emitter::SetOutputCharset(EMITTER_MANIP value)
	{
42
		return m_pState->SetOutputCharset(value, FmtScope::Global);
43
44
	}

45
46
	bool Emitter::SetStringFormat(EMITTER_MANIP value)
	{
47
		return m_pState->SetStringFormat(value, FmtScope::Global);
48
49
50
51
52
	}
	
	bool Emitter::SetBoolFormat(EMITTER_MANIP value)
	{
		bool ok = false;
53
		if(m_pState->SetBoolFormat(value, FmtScope::Global))
54
			ok = true;
55
		if(m_pState->SetBoolCaseFormat(value, FmtScope::Global))
56
			ok = true;
57
		if(m_pState->SetBoolLengthFormat(value, FmtScope::Global))
58
59
60
61
62
63
			ok = true;
		return ok;
	}
	
	bool Emitter::SetIntBase(EMITTER_MANIP value)
	{
64
		return m_pState->SetIntFormat(value, FmtScope::Global);
65
66
67
68
	}
	
	bool Emitter::SetSeqFormat(EMITTER_MANIP value)
	{
69
		return m_pState->SetFlowType(GroupType::Seq, value, FmtScope::Global);
70
71
72
73
74
	}
	
	bool Emitter::SetMapFormat(EMITTER_MANIP value)
	{
		bool ok = false;
75
		if(m_pState->SetFlowType(GroupType::Map, value, FmtScope::Global))
76
			ok = true;
77
		if(m_pState->SetMapKeyFormat(value, FmtScope::Global))
78
79
80
81
82
83
			ok = true;
		return ok;
	}
	
	bool Emitter::SetIndent(unsigned n)
	{
84
		return m_pState->SetIndent(n, FmtScope::Global);
85
86
87
88
	}
	
	bool Emitter::SetPreCommentIndent(unsigned n)
	{
89
		return m_pState->SetPreCommentIndent(n, FmtScope::Global);
90
91
92
93
	}
	
	bool Emitter::SetPostCommentIndent(unsigned n)
	{
94
		return m_pState->SetPostCommentIndent(n, FmtScope::Global);
95
	}
96
97
98
    
    bool Emitter::SetFloatPrecision(unsigned n)
    {
99
        return m_pState->SetFloatPrecision(n, FmtScope::Global);
100
101
102
103
    }

    bool Emitter::SetDoublePrecision(unsigned n)
    {
104
        return m_pState->SetDoublePrecision(n, FmtScope::Global);
105
    }
106
107
108
109
110
111
112
113
114

	// SetLocalValue
	// . Either start/end a group, or set a modifier locally
	Emitter& Emitter::SetLocalValue(EMITTER_MANIP value)
	{
		if(!good())
			return *this;
		
		switch(value) {
115
116
117
118
119
120
			case BeginDoc:
				EmitBeginDoc();
				break;
			case EndDoc:
				EmitEndDoc();
				break;
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
			case BeginSeq:
				EmitBeginSeq();
				break;
			case EndSeq:
				EmitEndSeq();
				break;
			case BeginMap:
				EmitBeginMap();
				break;
			case EndMap:
				EmitEndMap();
				break;
			case Key:
				EmitKey();
				break;
			case Value:
				EmitValue();
				break;
139
140
141
			case TagByKind:
				EmitKindTag();
				break;
142
143
144
			case Newline:
				EmitNewline();
				break;
145
146
147
148
149
150
151
152
153
			default:
				m_pState->SetLocalValue(value);
				break;
		}
		return *this;
	}
	
	Emitter& Emitter::SetLocalIndent(const _Indent& indent)
	{
154
		m_pState->SetIndent(indent.value, FmtScope::Local);
155
156
157
		return *this;
	}

158
159
160
    Emitter& Emitter::SetLocalPrecision(const _Precision& precision)
    {
        if(precision.floatPrecision >= 0)
161
            m_pState->SetFloatPrecision(precision.floatPrecision, FmtScope::Local);
162
        if(precision.doublePrecision >= 0)
163
            m_pState->SetDoublePrecision(precision.doublePrecision, FmtScope::Local);
164
165
166
        return *this;
    }

167
168
169
170
171
172
173
174
175
176
177
178
179
180
	// EmitBeginDoc
	void Emitter::EmitBeginDoc()
	{
		if(!good())
			return;
	}
	
	// EmitEndDoc
	void Emitter::EmitEndDoc()
	{
		if(!good())
			return;
	}

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
	// EmitBeginSeq
	void Emitter::EmitBeginSeq()
	{
		if(!good())
			return;
	}
	
	// EmitEndSeq
	void Emitter::EmitEndSeq()
	{
		if(!good())
			return;
	}
	
	// EmitBeginMap
	void Emitter::EmitBeginMap()
	{
		if(!good())
			return;
	}
	
	// EmitEndMap
	void Emitter::EmitEndMap()
	{
		if(!good())
206
			return;	}
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
	
	// EmitKey
	void Emitter::EmitKey()
	{
		if(!good())
			return;
	}
	
	// EmitValue
	void Emitter::EmitValue()
	{
		if(!good())
			return;
	}

222
223
224
225
226
	// EmitNewline
	void Emitter::EmitNewline()
	{
		if(!good())
			return;
227
228
229
230
	}

	bool Emitter::CanEmitNewline() const
	{
231
        return false;
232
233
	}

234
235
236
237
238
239
240
241
242
	// *******************************************************************************************
	// overloads of Write
	
	Emitter& Emitter::Write(const std::string& str)
	{
		if(!good())
			return *this;
		return *this;
	}
243

244
245
246
247
248
249
250
251
252
253
    unsigned Emitter::GetFloatPrecision() const
    {
        return m_pState->GetFloatPrecision();
    }
    
    unsigned Emitter::GetDoublePrecision() const
    {
        return m_pState->GetDoublePrecision();
    }

254
255
256
257
258
259
260
	const char *Emitter::ComputeFullBoolName(bool b) const
	{
		const EMITTER_MANIP mainFmt = (m_pState->GetBoolLengthFormat() == ShortBool ? YesNoBool : m_pState->GetBoolFormat());
		const EMITTER_MANIP caseFmt = m_pState->GetBoolCaseFormat();
		switch(mainFmt) {
			case YesNoBool:
				switch(caseFmt) {
261
262
263
264
					case UpperCase: return b ? "YES" : "NO";
					case CamelCase: return b ? "Yes" : "No";
					case LowerCase: return b ? "yes" : "no";
					default: break;
265
				}
266
				break;
267
268
			case OnOffBool:
				switch(caseFmt) {
269
270
271
272
					case UpperCase: return b ? "ON" : "OFF";
					case CamelCase: return b ? "On" : "Off";
					case LowerCase: return b ? "on" : "off";
					default: break;
273
				}
274
275
				break;
			case TrueFalseBool:
276
				switch(caseFmt) {
277
278
279
280
					case UpperCase: return b ? "TRUE" : "FALSE";
					case CamelCase: return b ? "True" : "False";
					case LowerCase: return b ? "true" : "false";
					default: break;
281
				}
282
283
284
				break;
			default:
				break;
285
		}
286
		return b ? "y" : "n"; // should never get here, but it can't hurt to give these answers
287
	}
288

289
290
291
292
	Emitter& Emitter::Write(bool b)
	{
		if(!good())
			return *this;
293

294
295
296
		return *this;
	}

Jesse Beder's avatar
Jesse Beder committed
297
298
299
300
	Emitter& Emitter::Write(char ch)
	{
		if(!good())
			return *this;
301

Jesse Beder's avatar
Jesse Beder committed
302
303
304
		return *this;
	}

305
306
307
308
	Emitter& Emitter::Write(const _Alias& alias)
	{
		if(!good())
			return *this;
309

310
311
312
313
314
315
316
		return *this;
	}
	
	Emitter& Emitter::Write(const _Anchor& anchor)
	{
		if(!good())
			return *this;
317

318
319
320
		return *this;
	}
	
321
322
323
324
	Emitter& Emitter::Write(const _Tag& tag)
	{
		if(!good())
			return *this;
325

326
        
327
		return *this;
328
	}
329

330
331
	void Emitter::EmitKindTag()
	{
332
		Write(LocalTag(""));
333
334
	}

335
336
337
338
	Emitter& Emitter::Write(const _Comment& comment)
	{
		if(!good())
			return *this;
339

340
341
		return *this;
	}
342
343
344
345
346

	Emitter& Emitter::Write(const _Null& /*null*/)
	{
		if(!good())
			return *this;
347

348
349
		return *this;
	}
350

351
	Emitter& Emitter::Write(const Binary& binary)
352
	{
353
354
		Write(SecondaryTag("binary"));

355
356
		if(!good())
			return *this;
357

358
359
		return *this;
	}
360
}
361