emitterstate.cpp 6.53 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_requiresSoftSeparation(false), m_requiresHardSeparation(false)
8
9
10
11
12
	{
		// start up
		m_stateStack.push(ES_WAITING_FOR_DOC);
		
		// set default global manipulators
13
		m_charset.set(EmitNonAscii);
14
15
16
17
18
19
20
21
22
23
24
		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);
25
26
        m_floatPrecision.set(6);
        m_doublePrecision.set(15);
27
28
29
30
31
32
33
34
35
36
37
	}
	
	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)
	{
38
39
40
41
42
43
44
45
46
		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);
47
48
	}
	
49
	void EmitterState::BeginGroup(GroupType::value type)
50
	{
51
		unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top().indent);
52
53
		m_curIndent += lastIndent;
		
54
		std::auto_ptr<Group> pGroup(new Group(type));
55
56
57
58
59
60
61
62
63
		
		// transfer settings (which last until this group is done)
		pGroup->modifiedSettings = m_modifiedSettings;

		// set up group
		pGroup->flow = GetFlowType(type);
		pGroup->indent = GetIndent();
		pGroup->usingLongKey = (GetMapKeyFormat() == LongKey ? true : false);

64
		m_groups.push(pGroup);
65
66
	}
	
67
	void EmitterState::EndGroup(GroupType::value type)
68
69
70
71
72
73
	{
		if(m_groups.empty())
			return SetError(ErrorMsg::UNMATCHED_GROUP_TAG);
		
		// get rid of the current group
		{
74
			std::auto_ptr<Group> pFinishedGroup = m_groups.pop();
75
76
77
78
79
			if(pFinishedGroup->type != type)
				return SetError(ErrorMsg::UNMATCHED_GROUP_TAG);
		}

		// reset old settings
80
		unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top().indent);
81
82
83
84
85
86
87
88
		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();
	}
		
89
	GroupType::value EmitterState::GetCurGroupType() const
90
91
	{
		if(m_groups.empty())
92
			return GroupType::None;
93
		
94
		return m_groups.top().type;
95
96
	}
	
97
    FlowType::value EmitterState::GetCurGroupFlowType() const
98
99
	{
		if(m_groups.empty())
100
			return FlowType::None;
101
		
102
		return (m_groups.top().flow == Flow ? FlowType::Flow : FlowType::Block);
103
104
105
106
107
108
	}
	
	bool EmitterState::CurrentlyInLongKey()
	{
		if(m_groups.empty())
			return false;
109
		return m_groups.top().usingLongKey;
110
111
112
113
114
	}
	
	void EmitterState::StartLongKey()
	{
		if(!m_groups.empty())
115
			m_groups.top().usingLongKey = true;
116
117
118
119
120
	}
	
	void EmitterState::StartSimpleKey()
	{
		if(!m_groups.empty())
121
			m_groups.top().usingLongKey = false;
122
123
124
125
126
127
	}

	void EmitterState::ClearModifiedSettings()
	{
		m_modifiedSettings.clear();
	}
128

129
	bool EmitterState::SetOutputCharset(EMITTER_MANIP value, FmtScope::value scope)
130
131
132
133
134
135
136
137
138
139
	{
		switch(value) {
			case EmitNonAscii:
			case EscapeNonAscii:
				_Set(m_charset, value, scope);
				return true;
			default:
				return false;
		}
	}
140
	
141
	bool EmitterState::SetStringFormat(EMITTER_MANIP value, FmtScope::value scope)
142
143
144
145
146
147
148
149
150
151
152
153
154
	{
		switch(value) {
			case Auto:
			case SingleQuoted:
			case DoubleQuoted:
			case Literal:
				_Set(m_strFmt, value, scope);
				return true;
			default:
				return false;
		}
	}
	
155
	bool EmitterState::SetBoolFormat(EMITTER_MANIP value, FmtScope::value scope)
156
157
158
159
160
161
162
163
164
165
166
167
	{
		switch(value) {
			case OnOffBool:
			case TrueFalseBool:
			case YesNoBool:
				_Set(m_boolFmt, value, scope);
				return true;
			default:
				return false;
		}
	}

168
	bool EmitterState::SetBoolLengthFormat(EMITTER_MANIP value, FmtScope::value scope)
169
170
171
172
173
174
175
176
177
178
179
	{
		switch(value) {
			case LongBool:
			case ShortBool:
				_Set(m_boolLengthFmt, value, scope);
				return true;
			default:
				return false;
		}
	}

180
	bool EmitterState::SetBoolCaseFormat(EMITTER_MANIP value, FmtScope::value scope)
181
182
183
184
185
186
187
188
189
190
191
192
	{
		switch(value) {
			case UpperCase:
			case LowerCase:
			case CamelCase:
				_Set(m_boolCaseFmt, value, scope);
				return true;
			default:
				return false;
		}
	}

193
	bool EmitterState::SetIntFormat(EMITTER_MANIP value, FmtScope::value scope)
194
195
196
197
198
199
200
201
202
203
204
205
	{
		switch(value) {
			case Dec:
			case Hex:
			case Oct:
				_Set(m_intFmt, value, scope);
				return true;
			default:
				return false;
		}
	}

206
	bool EmitterState::SetIndent(unsigned value, FmtScope::value scope)
207
208
209
210
211
212
213
214
	{
		if(value == 0)
			return false;
		
		_Set(m_indent, value, scope);
		return true;
	}

215
	bool EmitterState::SetPreCommentIndent(unsigned value, FmtScope::value scope)
216
217
218
219
220
221
222
223
	{
		if(value == 0)
			return false;
		
		_Set(m_preCommentIndent, value, scope);
		return true;
	}
	
224
	bool EmitterState::SetPostCommentIndent(unsigned value, FmtScope::value scope)
225
226
227
228
229
230
231
232
	{
		if(value == 0)
			return false;
		
		_Set(m_postCommentIndent, value, scope);
		return true;
	}

233
	bool EmitterState::SetFlowType(GroupType::value groupType, EMITTER_MANIP value, FmtScope::value scope)
234
235
236
237
	{
		switch(value) {
			case Block:
			case Flow:
238
				_Set(groupType == GroupType::Seq ? m_seqFmt : m_mapFmt, value, scope);
239
240
241
242
243
244
				return true;
			default:
				return false;
		}
	}

245
	EMITTER_MANIP EmitterState::GetFlowType(GroupType::value groupType) const
246
247
	{
		// force flow style if we're currently in a flow
248
249
        FlowType::value flowType = GetCurGroupFlowType();
		if(flowType == FlowType::Flow)
250
251
			return Flow;
		
252
253
		// otherwise, go with what's asked of us
		return (groupType == GroupType::Seq ? m_seqFmt.get() : m_mapFmt.get());
254
255
	}
	
256
	bool EmitterState::SetMapKeyFormat(EMITTER_MANIP value, FmtScope::value scope)
257
258
259
260
261
262
263
264
265
266
	{
		switch(value) {
			case Auto:
			case LongKey:
				_Set(m_mapKeyFmt, value, scope);
				return true;
			default:
				return false;
		}
	}
267

268
    bool EmitterState::SetFloatPrecision(int value, FmtScope::value scope)
269
    {
270
        if(value < 0 || value > std::numeric_limits<float>::digits10)
271
272
273
274
275
            return false;
        _Set(m_floatPrecision, value, scope);
        return true;
    }
    
276
    bool EmitterState::SetDoublePrecision(int value, FmtScope::value scope)
277
    {
278
        if(value < 0 || value > std::numeric_limits<double>::digits10)
279
280
281
282
            return false;
        _Set(m_doublePrecision, value, scope);
        return true;
    }
283
284
}