1 | /*
|
---|
2 | ###############################################################################
|
---|
3 | #
|
---|
4 | # Temboo Arduino library
|
---|
5 | #
|
---|
6 | # Copyright 2014, Temboo Inc.
|
---|
7 | #
|
---|
8 | # Licensed under the Apache License, Version 2.0 (the "License");
|
---|
9 | # you may not use this file except in compliance with the License.
|
---|
10 | # You may obtain a copy of the License at
|
---|
11 | #
|
---|
12 | # http://www.apache.org/licenses/LICENSE-2.0
|
---|
13 | #
|
---|
14 | # Unless required by applicable law or agreed to in writing,
|
---|
15 | # software distributed under the License is distributed on an
|
---|
16 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
---|
17 | # either express or implied. See the License for the specific
|
---|
18 | # language governing permissions and limitations under the License.
|
---|
19 | #
|
---|
20 | ###############################################################################
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "DataFormatter.h"
|
---|
24 |
|
---|
25 |
|
---|
26 | DataFormatter::DataFormatter(
|
---|
27 | const ChoreoInputSet* inputSet,
|
---|
28 | const ChoreoOutputSet* outputSet,
|
---|
29 | const ChoreoPreset* preset) :
|
---|
30 | m_inputFormatter(inputSet),
|
---|
31 | m_outputFormatter(outputSet),
|
---|
32 | m_presetFormatter(preset) {
|
---|
33 |
|
---|
34 | m_inputSet = inputSet;
|
---|
35 | m_outputSet = outputSet;
|
---|
36 | m_preset = preset;
|
---|
37 |
|
---|
38 | reset();
|
---|
39 | }
|
---|
40 |
|
---|
41 | void DataFormatter::reset() {
|
---|
42 | m_nextState = DATA_START;
|
---|
43 | m_inputFormatter.reset();
|
---|
44 | m_outputFormatter.reset();
|
---|
45 | m_presetFormatter.reset();
|
---|
46 | }
|
---|
47 |
|
---|
48 | bool DataFormatter::hasNext() {
|
---|
49 | return m_nextState != DATA_END;
|
---|
50 | }
|
---|
51 |
|
---|
52 | char DataFormatter::next() {
|
---|
53 | char c;
|
---|
54 | switch(m_nextState) {
|
---|
55 | case DATA_START:
|
---|
56 | c = '{';
|
---|
57 | if (m_inputFormatter.hasNext()) {
|
---|
58 | m_nextState = FORMATTING_INPUTS;
|
---|
59 | } else if (m_outputFormatter.hasNext()) {
|
---|
60 | m_nextState = FORMATTING_OUTPUTS;
|
---|
61 | } else if (m_presetFormatter.hasNext()) {
|
---|
62 | m_nextState = FORMATTING_PRESET;
|
---|
63 | } else {
|
---|
64 | m_nextState = FORMATTING_EMPTY;
|
---|
65 | }
|
---|
66 | break;
|
---|
67 | case FORMATTING_INPUTS:
|
---|
68 | if (m_inputFormatter.hasNext()) {
|
---|
69 | c = m_inputFormatter.next();
|
---|
70 | } else if (m_outputFormatter.hasNext()) {
|
---|
71 | c = ',';
|
---|
72 | m_nextState = FORMATTING_OUTPUTS;
|
---|
73 | } else if (m_presetFormatter.hasNext()) {
|
---|
74 | c = ',';
|
---|
75 | m_nextState = FORMATTING_PRESET;
|
---|
76 | } else {
|
---|
77 | c = '}';
|
---|
78 | m_nextState = DATA_END;
|
---|
79 | }
|
---|
80 | break;
|
---|
81 | case FORMATTING_OUTPUTS:
|
---|
82 | if (m_outputFormatter.hasNext()) {
|
---|
83 | c = m_outputFormatter.next();
|
---|
84 | } else if (m_presetFormatter.hasNext()) {
|
---|
85 | c = ',';
|
---|
86 | m_nextState = FORMATTING_PRESET;
|
---|
87 | } else {
|
---|
88 | c = '}';
|
---|
89 | m_nextState = DATA_END;
|
---|
90 | }
|
---|
91 | break;
|
---|
92 |
|
---|
93 | case FORMATTING_PRESET:
|
---|
94 | if (m_presetFormatter.hasNext()) {
|
---|
95 | c = m_presetFormatter.next();
|
---|
96 | } else {
|
---|
97 | c = '}';
|
---|
98 | m_nextState = DATA_END;
|
---|
99 | }
|
---|
100 | break;
|
---|
101 |
|
---|
102 | case FORMATTING_EMPTY:
|
---|
103 | c = '}';
|
---|
104 | m_nextState = DATA_END;
|
---|
105 | break;
|
---|
106 |
|
---|
107 | case DATA_END:
|
---|
108 | default:
|
---|
109 | c = '\0';
|
---|
110 | break;
|
---|
111 | }
|
---|
112 | return c;
|
---|
113 | }
|
---|
114 |
|
---|