emitter.cpp 10.6 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
			case BeginSeq:
				EmitBeginSeq();
				break;
			case EndSeq:
				EmitEndSeq();
				break;
			case BeginMap:
				EmitBeginMap();
				break;
			case EndMap:
				EmitEndMap();
				break;
			case Key:
			case Value:
Jesse Beder's avatar
Jesse Beder committed
135
                // deprecated (these can be deduced by the parity of nodes in a map)
136
				break;
137
138
139
			case TagByKind:
				EmitKindTag();
				break;
140
141
142
			case Newline:
				EmitNewline();
				break;
143
144
145
146
147
148
149
150
151
			default:
				m_pState->SetLocalValue(value);
				break;
		}
		return *this;
	}
	
	Emitter& Emitter::SetLocalIndent(const _Indent& indent)
	{
152
		m_pState->SetIndent(indent.value, FmtScope::Local);
153
154
155
		return *this;
	}

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

165
166
167
168
169
	// EmitBeginDoc
	void Emitter::EmitBeginDoc()
	{
		if(!good())
			return;
Jesse Beder's avatar
Jesse Beder committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
        
        if(m_pState->CurGroupType() != GroupType::None) {
			m_pState->SetError("Unexpected begin document");
			return;
        }
        
        if(m_pState->HasAnchor() || m_pState->HasTag()) {
			m_pState->SetError("Unexpected begin document");
			return;
        }
        
        if(m_stream.col() > 0)
            m_stream << "\n";
        m_stream << "---\n";
184
185
186
187
188
189
190
	}
	
	// EmitEndDoc
	void Emitter::EmitEndDoc()
	{
		if(!good())
			return;
Jesse Beder's avatar
Jesse Beder committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
        
        if(m_pState->CurGroupType() != GroupType::None) {
			m_pState->SetError("Unexpected begin document");
			return;
        }
        
        if(m_pState->HasAnchor() || m_pState->HasTag()) {
			m_pState->SetError("Unexpected begin document");
			return;
        }
        
        if(m_stream.col() > 0)
            m_stream << "\n";
        m_stream << "...\n";
205
206
	}

207
208
209
210
211
	// EmitBeginSeq
	void Emitter::EmitBeginSeq()
	{
		if(!good())
			return;
Jesse Beder's avatar
Jesse Beder committed
212
        
Jesse Beder's avatar
Jesse Beder committed
213
214
        PrepareNode();
        
Jesse Beder's avatar
Jesse Beder committed
215
        m_pState->BeginGroup(GroupType::Seq);
216
217
218
219
220
221
222
	}
	
	// EmitEndSeq
	void Emitter::EmitEndSeq()
	{
		if(!good())
			return;
Jesse Beder's avatar
Jesse Beder committed
223
224
        
        m_pState->EndGroup(GroupType::Seq);
225
226
227
228
229
230
231
	}
	
	// EmitBeginMap
	void Emitter::EmitBeginMap()
	{
		if(!good())
			return;
Jesse Beder's avatar
Jesse Beder committed
232

Jesse Beder's avatar
Jesse Beder committed
233
234
        PrepareNode();
        
Jesse Beder's avatar
Jesse Beder committed
235
        m_pState->BeginGroup(GroupType::Map);
236
237
238
239
240
241
242
	}
	
	// EmitEndMap
	void Emitter::EmitEndMap()
	{
		if(!good())
			return;
Jesse Beder's avatar
Jesse Beder committed
243
244
245

        m_pState->EndGroup(GroupType::Map);
    }
246

247
248
249
250
251
	// EmitNewline
	void Emitter::EmitNewline()
	{
		if(!good())
			return;
252
253
254
255
	}

	bool Emitter::CanEmitNewline() const
	{
256
        return false;
257
258
	}

Jesse Beder's avatar
Jesse Beder committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
    // Put the stream in a state so we can simply write the next node
    // E.g., if we're in a sequence, write the "- "
    void Emitter::PrepareNode()
    {
        switch(m_pState->CurGroupType()) {
            case GroupType::None:
                PrepareTopNode();
                break;
            case GroupType::Seq:
                switch(m_pState->CurGroupFlowType()) {
                    case FlowType::Flow:
                        FlowSeqPrepareNode();
                        break;
                    case FlowType::Block:
                        BlockSeqPrepareNode();
                        break;
                    case FlowType::None:
                        assert(false);
                }
                break;
            case GroupType::Map:
                switch(m_pState->CurGroupFlowType()) {
                    case FlowType::Flow:
                        FlowMapPrepareNode();
                        break;
                    case FlowType::Block:
                        BlockMapPrepareNode();
                        break;
                    case FlowType::None:
                        assert(false);
                }
                break;
        }
    }
    
    void Emitter::PrepareTopNode()
    {
296
297
298
299
300
301
302
303
304
305
306
        const bool hasAnchor = m_pState->HasAnchor();
        const bool hasTag = m_pState->HasTag();
        
        if(!hasAnchor && !hasTag && m_stream.pos() > 0) {
            EmitBeginDoc();
        }
        
        // TODO: if we were writing null, and
        // we wanted it blank, we wouldn't want a space
        if(hasAnchor || hasTag)
            m_stream << " ";
Jesse Beder's avatar
Jesse Beder committed
307
308
309
310
311
312
313
314
    }
    
    void Emitter::FlowSeqPrepareNode()
    {
    }

    void Emitter::BlockSeqPrepareNode()
    {
Jesse Beder's avatar
Jesse Beder committed
315
        const unsigned curIndent = m_pState->CurIndent();
316
317
318
319
320
321
322
        if(!m_pState->HasTag() && !m_pState->HasAnchor()) {
            if(m_pState->CurGroupChildCount() > 0) {
                m_stream << "\n";
            }
            m_stream << IndentTo(curIndent);
            m_stream << "-";
            m_stream << IndentTo(curIndent + m_pState->CurGroupIndent());
Jesse Beder's avatar
Jesse Beder committed
323
        }
Jesse Beder's avatar
Jesse Beder committed
324
325
326
327
328
329
330
331
332
333
    }
    
    void Emitter::FlowMapPrepareNode()
    {
    }

    void Emitter::BlockMapPrepareNode()
    {
    }

334
335
336
337
338
339
340
	// *******************************************************************************************
	// overloads of Write
	
	Emitter& Emitter::Write(const std::string& str)
	{
		if(!good())
			return *this;
Jesse Beder's avatar
Jesse Beder committed
341
        
Jesse Beder's avatar
Jesse Beder committed
342
343
        PrepareNode();
        
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
		const bool escapeNonAscii = m_pState->GetOutputCharset() == EscapeNonAscii;
        const StringFormat::value strFormat = Utils::ComputeStringFormat(str, m_pState->GetStringFormat(), m_pState->CurGroupFlowType(), escapeNonAscii);
        
        switch(strFormat) {
            case StringFormat::Plain:
                m_stream << str;
                break;
            case StringFormat::SingleQuoted:
                Utils::WriteSingleQuotedString(m_stream, str);
                break;
            case StringFormat::DoubleQuoted:
                Utils::WriteDoubleQuotedString(m_stream, str, escapeNonAscii);
                break;
            case StringFormat::Literal:
                Utils::WriteLiteralString(m_stream, str, m_pState->CurIndent() + m_pState->GetIndent());
                break;
        }

Jesse Beder's avatar
Jesse Beder committed
362
363
        m_pState->BeginScalar();
        
364
365
		return *this;
	}
366

367
368
369
370
371
372
373
374
375
376
    unsigned Emitter::GetFloatPrecision() const
    {
        return m_pState->GetFloatPrecision();
    }
    
    unsigned Emitter::GetDoublePrecision() const
    {
        return m_pState->GetDoublePrecision();
    }

377
378
379
380
381
382
383
	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) {
384
385
386
387
					case UpperCase: return b ? "YES" : "NO";
					case CamelCase: return b ? "Yes" : "No";
					case LowerCase: return b ? "yes" : "no";
					default: break;
388
				}
389
				break;
390
391
			case OnOffBool:
				switch(caseFmt) {
392
393
394
395
					case UpperCase: return b ? "ON" : "OFF";
					case CamelCase: return b ? "On" : "Off";
					case LowerCase: return b ? "on" : "off";
					default: break;
396
				}
397
398
				break;
			case TrueFalseBool:
399
				switch(caseFmt) {
400
401
402
403
					case UpperCase: return b ? "TRUE" : "FALSE";
					case CamelCase: return b ? "True" : "False";
					case LowerCase: return b ? "true" : "false";
					default: break;
404
				}
405
406
407
				break;
			default:
				break;
408
		}
409
		return b ? "y" : "n"; // should never get here, but it can't hurt to give these answers
410
	}
411

412
413
414
415
	Emitter& Emitter::Write(bool b)
	{
		if(!good())
			return *this;
416

Jesse Beder's avatar
Jesse Beder committed
417
418
        m_pState->BeginScalar();

419
420
421
		return *this;
	}

Jesse Beder's avatar
Jesse Beder committed
422
423
424
425
	Emitter& Emitter::Write(char ch)
	{
		if(!good())
			return *this;
426

Jesse Beder's avatar
Jesse Beder committed
427
428
        m_pState->BeginScalar();

Jesse Beder's avatar
Jesse Beder committed
429
430
431
		return *this;
	}

432
433
434
435
	Emitter& Emitter::Write(const _Alias& alias)
	{
		if(!good())
			return *this;
436

Jesse Beder's avatar
Jesse Beder committed
437
438
        m_pState->BeginScalar();

439
440
441
442
443
444
445
		return *this;
	}
	
	Emitter& Emitter::Write(const _Anchor& anchor)
	{
		if(!good())
			return *this;
446

Jesse Beder's avatar
Jesse Beder committed
447
448
        m_pState->BeginScalar();

449
450
451
		return *this;
	}
	
452
453
454
455
	Emitter& Emitter::Write(const _Tag& tag)
	{
		if(!good())
			return *this;
456

Jesse Beder's avatar
Jesse Beder committed
457
        m_pState->BeginScalar();
458
        
459
		return *this;
460
	}
461

462
463
	void Emitter::EmitKindTag()
	{
464
		Write(LocalTag(""));
465
466
	}

467
468
469
470
	Emitter& Emitter::Write(const _Comment& comment)
	{
		if(!good())
			return *this;
471

Jesse Beder's avatar
Jesse Beder committed
472
473
        m_pState->BeginScalar();

474
475
		return *this;
	}
476
477
478
479
480

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

Jesse Beder's avatar
Jesse Beder committed
482
483
        m_pState->BeginScalar();

484
485
		return *this;
	}
486

487
	Emitter& Emitter::Write(const Binary& binary)
488
	{
489
490
		Write(SecondaryTag("binary"));

491
492
		if(!good())
			return *this;
493

Jesse Beder's avatar
Jesse Beder committed
494
495
        m_pState->BeginScalar();

496
497
		return *this;
	}
498
}
499