scide: implement selectionLength for openDocument
[supercollider.git] / external_libraries / yaml-cpp-0.3.0 / src / emitterstate.cpp
blob562e82c9cf4c6fb71d40112db733e6b65fcb494e
1 #include "emitterstate.h"
2 #include "yaml-cpp/exceptions.h"
3 #include <limits>
5 namespace YAML
7 EmitterState::EmitterState(): m_isGood(true), m_curIndent(0), m_requiresSoftSeparation(false), m_requiresHardSeparation(false)
9 // start up
10 m_stateStack.push(ES_WAITING_FOR_DOC);
12 // set default global manipulators
13 m_charset.set(EmitNonAscii);
14 m_strFmt.set(Auto);
15 m_boolFmt.set(TrueFalseBool);
16 m_boolLengthFmt.set(LongBool);
17 m_boolCaseFmt.set(LowerCase);
18 m_intFmt.set(Dec);
19 m_indent.set(2);
20 m_preCommentIndent.set(2);
21 m_postCommentIndent.set(1);
22 m_seqFmt.set(Block);
23 m_mapFmt.set(Block);
24 m_mapKeyFmt.set(Auto);
25 m_floatPrecision.set(6);
26 m_doublePrecision.set(15);
29 EmitterState::~EmitterState()
33 // SetLocalValue
34 // . We blindly tries to set all possible formatters to this value
35 // . Only the ones that make sense will be accepted
36 void EmitterState::SetLocalValue(EMITTER_MANIP value)
38 SetOutputCharset(value, LOCAL);
39 SetStringFormat(value, LOCAL);
40 SetBoolFormat(value, LOCAL);
41 SetBoolCaseFormat(value, LOCAL);
42 SetBoolLengthFormat(value, LOCAL);
43 SetIntFormat(value, LOCAL);
44 SetFlowType(GT_SEQ, value, LOCAL);
45 SetFlowType(GT_MAP, value, LOCAL);
46 SetMapKeyFormat(value, LOCAL);
49 void EmitterState::BeginGroup(GROUP_TYPE type)
51 unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top().indent);
52 m_curIndent += lastIndent;
54 std::auto_ptr<Group> pGroup(new Group(type));
56 // transfer settings (which last until this group is done)
57 pGroup->modifiedSettings = m_modifiedSettings;
59 // set up group
60 pGroup->flow = GetFlowType(type);
61 pGroup->indent = GetIndent();
62 pGroup->usingLongKey = (GetMapKeyFormat() == LongKey ? true : false);
64 m_groups.push(pGroup);
67 void EmitterState::EndGroup(GROUP_TYPE type)
69 if(m_groups.empty())
70 return SetError(ErrorMsg::UNMATCHED_GROUP_TAG);
72 // get rid of the current group
74 std::auto_ptr<Group> pFinishedGroup = m_groups.pop();
75 if(pFinishedGroup->type != type)
76 return SetError(ErrorMsg::UNMATCHED_GROUP_TAG);
79 // reset old settings
80 unsigned lastIndent = (m_groups.empty() ? 0 : m_groups.top().indent);
81 assert(m_curIndent >= lastIndent);
82 m_curIndent -= lastIndent;
84 // some global settings that we changed may have been overridden
85 // by a local setting we just popped, so we need to restore them
86 m_globalModifiedSettings.restore();
89 GROUP_TYPE EmitterState::GetCurGroupType() const
91 if(m_groups.empty())
92 return GT_NONE;
94 return m_groups.top().type;
97 FLOW_TYPE EmitterState::GetCurGroupFlowType() const
99 if(m_groups.empty())
100 return FT_NONE;
102 return (m_groups.top().flow == Flow ? FT_FLOW : FT_BLOCK);
105 bool EmitterState::CurrentlyInLongKey()
107 if(m_groups.empty())
108 return false;
109 return m_groups.top().usingLongKey;
112 void EmitterState::StartLongKey()
114 if(!m_groups.empty())
115 m_groups.top().usingLongKey = true;
118 void EmitterState::StartSimpleKey()
120 if(!m_groups.empty())
121 m_groups.top().usingLongKey = false;
124 void EmitterState::ClearModifiedSettings()
126 m_modifiedSettings.clear();
129 bool EmitterState::SetOutputCharset(EMITTER_MANIP value, FMT_SCOPE scope)
131 switch(value) {
132 case EmitNonAscii:
133 case EscapeNonAscii:
134 _Set(m_charset, value, scope);
135 return true;
136 default:
137 return false;
141 bool EmitterState::SetStringFormat(EMITTER_MANIP value, FMT_SCOPE scope)
143 switch(value) {
144 case Auto:
145 case SingleQuoted:
146 case DoubleQuoted:
147 case Literal:
148 _Set(m_strFmt, value, scope);
149 return true;
150 default:
151 return false;
155 bool EmitterState::SetBoolFormat(EMITTER_MANIP value, FMT_SCOPE scope)
157 switch(value) {
158 case OnOffBool:
159 case TrueFalseBool:
160 case YesNoBool:
161 _Set(m_boolFmt, value, scope);
162 return true;
163 default:
164 return false;
168 bool EmitterState::SetBoolLengthFormat(EMITTER_MANIP value, FMT_SCOPE scope)
170 switch(value) {
171 case LongBool:
172 case ShortBool:
173 _Set(m_boolLengthFmt, value, scope);
174 return true;
175 default:
176 return false;
180 bool EmitterState::SetBoolCaseFormat(EMITTER_MANIP value, FMT_SCOPE scope)
182 switch(value) {
183 case UpperCase:
184 case LowerCase:
185 case CamelCase:
186 _Set(m_boolCaseFmt, value, scope);
187 return true;
188 default:
189 return false;
193 bool EmitterState::SetIntFormat(EMITTER_MANIP value, FMT_SCOPE scope)
195 switch(value) {
196 case Dec:
197 case Hex:
198 case Oct:
199 _Set(m_intFmt, value, scope);
200 return true;
201 default:
202 return false;
206 bool EmitterState::SetIndent(unsigned value, FMT_SCOPE scope)
208 if(value == 0)
209 return false;
211 _Set(m_indent, value, scope);
212 return true;
215 bool EmitterState::SetPreCommentIndent(unsigned value, FMT_SCOPE scope)
217 if(value == 0)
218 return false;
220 _Set(m_preCommentIndent, value, scope);
221 return true;
224 bool EmitterState::SetPostCommentIndent(unsigned value, FMT_SCOPE scope)
226 if(value == 0)
227 return false;
229 _Set(m_postCommentIndent, value, scope);
230 return true;
233 bool EmitterState::SetFlowType(GROUP_TYPE groupType, EMITTER_MANIP value, FMT_SCOPE scope)
235 switch(value) {
236 case Block:
237 case Flow:
238 _Set(groupType == GT_SEQ ? m_seqFmt : m_mapFmt, value, scope);
239 return true;
240 default:
241 return false;
245 EMITTER_MANIP EmitterState::GetFlowType(GROUP_TYPE groupType) const
247 // force flow style if we're currently in a flow
248 FLOW_TYPE flowType = GetCurGroupFlowType();
249 if(flowType == FT_FLOW)
250 return Flow;
252 // otherwise, go with what's asked of use
253 return (groupType == GT_SEQ ? m_seqFmt.get() : m_mapFmt.get());
256 bool EmitterState::SetMapKeyFormat(EMITTER_MANIP value, FMT_SCOPE scope)
258 switch(value) {
259 case Auto:
260 case LongKey:
261 _Set(m_mapKeyFmt, value, scope);
262 return true;
263 default:
264 return false;
268 bool EmitterState::SetFloatPrecision(int value, FMT_SCOPE scope)
270 if(value < 0 || value > std::numeric_limits<float>::digits10)
271 return false;
272 _Set(m_floatPrecision, value, scope);
273 return true;
276 bool EmitterState::SetDoublePrecision(int value, FMT_SCOPE scope)
278 if(value < 0 || value > std::numeric_limits<double>::digits10)
279 return false;
280 _Set(m_doublePrecision, value, scope);
281 return true;