1 #include "emitterstate.h"
2 #include "yaml-cpp/exceptions.h"
7 EmitterState::EmitterState(): m_isGood(true), m_curIndent(0), m_requiresSoftSeparation(false), m_requiresHardSeparation(false)
10 m_stateStack
.push(ES_WAITING_FOR_DOC
);
12 // set default global manipulators
13 m_charset
.set(EmitNonAscii
);
15 m_boolFmt
.set(TrueFalseBool
);
16 m_boolLengthFmt
.set(LongBool
);
17 m_boolCaseFmt
.set(LowerCase
);
20 m_preCommentIndent
.set(2);
21 m_postCommentIndent
.set(1);
24 m_mapKeyFmt
.set(Auto
);
25 m_floatPrecision
.set(6);
26 m_doublePrecision
.set(15);
29 EmitterState::~EmitterState()
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
;
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
)
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
);
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
94 return m_groups
.top().type
;
97 FLOW_TYPE
EmitterState::GetCurGroupFlowType() const
102 return (m_groups
.top().flow
== Flow
? FT_FLOW
: FT_BLOCK
);
105 bool EmitterState::CurrentlyInLongKey()
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
)
134 _Set(m_charset
, value
, scope
);
141 bool EmitterState::SetStringFormat(EMITTER_MANIP value
, FMT_SCOPE scope
)
148 _Set(m_strFmt
, value
, scope
);
155 bool EmitterState::SetBoolFormat(EMITTER_MANIP value
, FMT_SCOPE scope
)
161 _Set(m_boolFmt
, value
, scope
);
168 bool EmitterState::SetBoolLengthFormat(EMITTER_MANIP value
, FMT_SCOPE scope
)
173 _Set(m_boolLengthFmt
, value
, scope
);
180 bool EmitterState::SetBoolCaseFormat(EMITTER_MANIP value
, FMT_SCOPE scope
)
186 _Set(m_boolCaseFmt
, value
, scope
);
193 bool EmitterState::SetIntFormat(EMITTER_MANIP value
, FMT_SCOPE scope
)
199 _Set(m_intFmt
, value
, scope
);
206 bool EmitterState::SetIndent(unsigned value
, FMT_SCOPE scope
)
211 _Set(m_indent
, value
, scope
);
215 bool EmitterState::SetPreCommentIndent(unsigned value
, FMT_SCOPE scope
)
220 _Set(m_preCommentIndent
, value
, scope
);
224 bool EmitterState::SetPostCommentIndent(unsigned value
, FMT_SCOPE scope
)
229 _Set(m_postCommentIndent
, value
, scope
);
233 bool EmitterState::SetFlowType(GROUP_TYPE groupType
, EMITTER_MANIP value
, FMT_SCOPE scope
)
238 _Set(groupType
== GT_SEQ
? m_seqFmt
: m_mapFmt
, value
, scope
);
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
)
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
)
261 _Set(m_mapKeyFmt
, value
, scope
);
268 bool EmitterState::SetFloatPrecision(int value
, FMT_SCOPE scope
)
270 if(value
< 0 || value
> std::numeric_limits
<float>::digits10
)
272 _Set(m_floatPrecision
, value
, scope
);
276 bool EmitterState::SetDoublePrecision(int value
, FMT_SCOPE scope
)
278 if(value
< 0 || value
> std::numeric_limits
<double>::digits10
)
280 _Set(m_doublePrecision
, value
, scope
);