emitterstate.cpp 8.22 KB
Newer Older
1
#include "emitterstate.h"
2
#include "yaml-cpp/exceptions.h"
3
#include <limits>
4
5
6

namespace YAML
{
7
	EmitterState::EmitterState(): m_isGood(true), m_curIndent(0), m_hasAnchor(false), m_hasTag(false), m_hasNonContent(false), m_docCount(0)
8
9
	{
		// set default global manipulators
10
		m_charset.set(EmitNonAscii);
11
12
13
14
15
16
17
18
19
20
21
		m_strFmt.set(Auto);
		m_boolFmt.set(TrueFalseBool);
		m_boolLengthFmt.set(LongBool);
		m_boolCaseFmt.set(LowerCase);
		m_intFmt.set(Dec);
		m_indent.set(2);
		m_preCommentIndent.set(2);
		m_postCommentIndent.set(1);
		m_seqFmt.set(Block);
		m_mapFmt.set(Block);
		m_mapKeyFmt.set(Auto);
22
23
        m_floatPrecision.set(6);
        m_doublePrecision.set(15);
24
25
26
27
28
29
30
31
32
33
34
	}
	
	EmitterState::~EmitterState()
	{
	}

	// SetLocalValue
	// . We blindly tries to set all possible formatters to this value
	// . Only the ones that make sense will be accepted
	void EmitterState::SetLocalValue(EMITTER_MANIP value)
	{
35
36
37
38
39
40
41
42
43
		SetOutputCharset(value, FmtScope::Local);
		SetStringFormat(value, FmtScope::Local);
		SetBoolFormat(value, FmtScope::Local);
		SetBoolCaseFormat(value, FmtScope::Local);
		SetBoolLengthFormat(value, FmtScope::Local);
		SetIntFormat(value, FmtScope::Local);
		SetFlowType(GroupType::Seq, value, FmtScope::Local);
		SetFlowType(GroupType::Map, value, FmtScope::Local);
		SetMapKeyFormat(value, FmtScope::Local);
44
45
	}
	
Jesse Beder's avatar
Jesse Beder committed
46
47
48
49
50
51
52
53
54
55
    void EmitterState::SetAnchor()
    {
        m_hasAnchor = true;
    }
        
    void EmitterState::SetTag()
    {
        m_hasTag = true;
    }

Jesse Beder's avatar
Jesse Beder committed
56
57
58
59
60
    void EmitterState::SetNonContent()
    {
        m_hasNonContent = true;
    }

61
    void EmitterState::SetLongKey()
Jesse Beder's avatar
Jesse Beder committed
62
    {
63
        assert(!m_groups.empty());        
64
        if(m_groups.empty())
65
66
67
68
69
70
71
72
73
74
            return;
        
        assert(m_groups.top().type == GroupType::Map);
        assert(m_groups.top().flowType == FlowType::Block);
        m_groups.top().longKey = true;
    }

    void EmitterState::StartedNode()
    {
        if(m_groups.empty()) {
75
            m_docCount++;
76
        } else {
Jesse Beder's avatar
Jesse Beder committed
77
            m_groups.top().childCount++;
78
79
80
            if(m_groups.top().childCount % 2 == 0)
                m_groups.top().longKey = false;
        }
Jesse Beder's avatar
Jesse Beder committed
81
82
83
        
        m_hasAnchor = false;
        m_hasTag = false;
Jesse Beder's avatar
Jesse Beder committed
84
        m_hasNonContent = false;
Jesse Beder's avatar
Jesse Beder committed
85
86
    }

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    EmitterNodeType::value EmitterState::NextGroupType(GroupType::value type) const
    {
        if(type == GroupType::Seq) {
            if(GetFlowType(type) == Block)
                return EmitterNodeType::BlockSeq;
            else
                return EmitterNodeType::FlowSeq;
        } else {
            if(GetFlowType(type) == Block)
                return EmitterNodeType::BlockMap;
            else
                return EmitterNodeType::FlowMap;
        }
        
        // can't happen
        assert(false);
        return EmitterNodeType::None;
    }
    
106
    void EmitterState::StartedScalar()
Jesse Beder's avatar
Jesse Beder committed
107
    {
108
        StartedNode();
Jesse Beder's avatar
Jesse Beder committed
109
110
    }

111
	void EmitterState::StartedGroup(GroupType::value type)
112
	{
113
        StartedNode();
Jesse Beder's avatar
Jesse Beder committed
114

Jesse Beder's avatar
Jesse Beder committed
115
116
		const int lastGroupIndent = (m_groups.empty() ? 0 : m_groups.top().indent);
		m_curIndent += lastGroupIndent;
117
		
118
		std::auto_ptr<Group> pGroup(new Group(type));
119
120
121
122
123
		
		// transfer settings (which last until this group is done)
		pGroup->modifiedSettings = m_modifiedSettings;

		// set up group
124
125
126
127
        if(GetFlowType(type) == Block)
            pGroup->flowType = FlowType::Block;
        else
            pGroup->flowType = FlowType::Flow;
128
129
		pGroup->indent = GetIndent();

130
		m_groups.push(pGroup);
131
132
	}
	
133
	void EmitterState::EndedGroup(GroupType::value type)
134
135
136
137
138
139
	{
		if(m_groups.empty())
			return SetError(ErrorMsg::UNMATCHED_GROUP_TAG);
		
		// get rid of the current group
		{
140
			std::auto_ptr<Group> pFinishedGroup = m_groups.pop();
141
142
143
144
145
			if(pFinishedGroup->type != type)
				return SetError(ErrorMsg::UNMATCHED_GROUP_TAG);
		}

		// reset old settings
146
		unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top().indent);
147
148
149
150
151
152
153
		assert(m_curIndent >= lastIndent);
		m_curIndent -= lastIndent;
		
		// some global settings that we changed may have been overridden
		// by a local setting we just popped, so we need to restore them
		m_globalModifiedSettings.restore();
	}
154
155
156
157
158
159
160
161
162
163

    EmitterNodeType::value EmitterState::CurGroupNodeType() const
    {
        if(m_groups.empty())
            return EmitterNodeType::None;
        
        return m_groups.top().NodeType();
    }

    GroupType::value EmitterState::CurGroupType() const
164
	{
165
        return m_groups.empty() ? GroupType::None : m_groups.top().type;
166
167
	}
	
Jesse Beder's avatar
Jesse Beder committed
168
    FlowType::value EmitterState::CurGroupFlowType() const
169
	{
170
        return m_groups.empty() ? FlowType::None : m_groups.top().flowType;
171
172
	}

Jesse Beder's avatar
Jesse Beder committed
173
174
175
176
177
    int EmitterState::CurGroupIndent() const
    {
        return m_groups.empty() ? 0 : m_groups.top().indent;
    }

178
179
    std::size_t EmitterState::CurGroupChildCount() const
    {
180
        return m_groups.empty() ? m_docCount : m_groups.top().childCount;
181
182
    }

183
184
185
186
187
    bool EmitterState::CurGroupLongKey() const
    {
        return m_groups.empty() ? false : m_groups.top().longKey;
    }

188
189
190
191
	void EmitterState::ClearModifiedSettings()
	{
		m_modifiedSettings.clear();
	}
192

193
	bool EmitterState::SetOutputCharset(EMITTER_MANIP value, FmtScope::value scope)
194
195
196
197
198
199
200
201
202
203
	{
		switch(value) {
			case EmitNonAscii:
			case EscapeNonAscii:
				_Set(m_charset, value, scope);
				return true;
			default:
				return false;
		}
	}
204
	
205
	bool EmitterState::SetStringFormat(EMITTER_MANIP value, FmtScope::value scope)
206
207
208
209
210
211
212
213
214
215
216
217
218
	{
		switch(value) {
			case Auto:
			case SingleQuoted:
			case DoubleQuoted:
			case Literal:
				_Set(m_strFmt, value, scope);
				return true;
			default:
				return false;
		}
	}
	
219
	bool EmitterState::SetBoolFormat(EMITTER_MANIP value, FmtScope::value scope)
220
221
222
223
224
225
226
227
228
229
230
231
	{
		switch(value) {
			case OnOffBool:
			case TrueFalseBool:
			case YesNoBool:
				_Set(m_boolFmt, value, scope);
				return true;
			default:
				return false;
		}
	}

232
	bool EmitterState::SetBoolLengthFormat(EMITTER_MANIP value, FmtScope::value scope)
233
234
235
236
237
238
239
240
241
242
243
	{
		switch(value) {
			case LongBool:
			case ShortBool:
				_Set(m_boolLengthFmt, value, scope);
				return true;
			default:
				return false;
		}
	}

244
	bool EmitterState::SetBoolCaseFormat(EMITTER_MANIP value, FmtScope::value scope)
245
246
247
248
249
250
251
252
253
254
255
256
	{
		switch(value) {
			case UpperCase:
			case LowerCase:
			case CamelCase:
				_Set(m_boolCaseFmt, value, scope);
				return true;
			default:
				return false;
		}
	}

257
	bool EmitterState::SetIntFormat(EMITTER_MANIP value, FmtScope::value scope)
258
259
260
261
262
263
264
265
266
267
268
269
	{
		switch(value) {
			case Dec:
			case Hex:
			case Oct:
				_Set(m_intFmt, value, scope);
				return true;
			default:
				return false;
		}
	}

270
	bool EmitterState::SetIndent(unsigned value, FmtScope::value scope)
271
272
273
274
275
276
277
278
	{
		if(value == 0)
			return false;
		
		_Set(m_indent, value, scope);
		return true;
	}

279
	bool EmitterState::SetPreCommentIndent(unsigned value, FmtScope::value scope)
280
281
282
283
284
285
286
287
	{
		if(value == 0)
			return false;
		
		_Set(m_preCommentIndent, value, scope);
		return true;
	}
	
288
	bool EmitterState::SetPostCommentIndent(unsigned value, FmtScope::value scope)
289
290
291
292
293
294
295
296
	{
		if(value == 0)
			return false;
		
		_Set(m_postCommentIndent, value, scope);
		return true;
	}

297
	bool EmitterState::SetFlowType(GroupType::value groupType, EMITTER_MANIP value, FmtScope::value scope)
298
299
300
301
	{
		switch(value) {
			case Block:
			case Flow:
302
				_Set(groupType == GroupType::Seq ? m_seqFmt : m_mapFmt, value, scope);
303
304
305
306
307
308
				return true;
			default:
				return false;
		}
	}

309
	EMITTER_MANIP EmitterState::GetFlowType(GroupType::value groupType) const
310
311
	{
		// force flow style if we're currently in a flow
Jesse Beder's avatar
Jesse Beder committed
312
		if(CurGroupFlowType() == FlowType::Flow)
313
314
			return Flow;
		
315
316
		// otherwise, go with what's asked of us
		return (groupType == GroupType::Seq ? m_seqFmt.get() : m_mapFmt.get());
317
318
	}
	
319
	bool EmitterState::SetMapKeyFormat(EMITTER_MANIP value, FmtScope::value scope)
320
321
322
323
324
325
326
327
328
329
	{
		switch(value) {
			case Auto:
			case LongKey:
				_Set(m_mapKeyFmt, value, scope);
				return true;
			default:
				return false;
		}
	}
330

331
    bool EmitterState::SetFloatPrecision(int value, FmtScope::value scope)
332
    {
333
        if(value < 0 || value > std::numeric_limits<float>::digits10)
334
335
336
337
338
            return false;
        _Set(m_floatPrecision, value, scope);
        return true;
    }
    
339
    bool EmitterState::SetDoublePrecision(int value, FmtScope::value scope)
340
    {
341
        if(value < 0 || value > std::numeric_limits<double>::digits10)
342
343
344
345
            return false;
        _Set(m_doublePrecision, value, scope);
        return true;
    }
346
347
}