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 <stddef.h>
|
---|
24 | #include <avr/pgmspace.h>
|
---|
25 | #include "ChoreoOutputFormatter.h"
|
---|
26 | #include "ChoreoOutputSet.h"
|
---|
27 |
|
---|
28 | static const char TAG_OUTPUTS_START[] PROGMEM = "\"outputs\":[";
|
---|
29 | static const char TAG_NAME[] PROGMEM = "\"name\":";
|
---|
30 | static const char TAG_PATH[] PROGMEM = "\"path\":";
|
---|
31 | static const char TAG_VAR[] PROGMEM = "\"variable\":";
|
---|
32 |
|
---|
33 |
|
---|
34 | ChoreoOutputFormatter::ChoreoOutputFormatter(const ChoreoOutputSet* outputSet) {
|
---|
35 | m_outputSet = outputSet;
|
---|
36 | reset();
|
---|
37 | }
|
---|
38 |
|
---|
39 | void ChoreoOutputFormatter::reset() {
|
---|
40 | m_currentOutput = NULL;
|
---|
41 | m_nextChar = NULL;
|
---|
42 | if (m_outputSet == NULL || m_outputSet->isEmpty()) {
|
---|
43 | m_nextState = END;
|
---|
44 | } else {
|
---|
45 | m_nextState = START;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | bool ChoreoOutputFormatter::hasNext() {
|
---|
50 | return m_nextState != END;
|
---|
51 | }
|
---|
52 |
|
---|
53 | char ChoreoOutputFormatter::next() {
|
---|
54 | char c = '\0';
|
---|
55 | switch(m_nextState) {
|
---|
56 | case START:
|
---|
57 | c = readStartTagChar(TAG_OUTPUTS_START, OUTPUTS_TAG);
|
---|
58 | break;
|
---|
59 |
|
---|
60 | case OUTPUTS_TAG:
|
---|
61 | c = readTagChar(OUTPUT_START);
|
---|
62 | if (m_nextState == OUTPUT_START) {
|
---|
63 | m_currentOutput = m_outputSet->getFirstOutput();
|
---|
64 | }
|
---|
65 | break;
|
---|
66 |
|
---|
67 | case OUTPUT_START:
|
---|
68 | c = '{';
|
---|
69 | m_nextChar = TAG_NAME;
|
---|
70 | m_nextState = NAME_TAG;
|
---|
71 | break;
|
---|
72 |
|
---|
73 | case NAME_TAG:
|
---|
74 | c = readTagChar(NAME_START);
|
---|
75 | break;
|
---|
76 |
|
---|
77 | case NAME_START:
|
---|
78 | c = '"';
|
---|
79 | m_nextChar = m_currentOutput->getName();
|
---|
80 | if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) {
|
---|
81 | m_nextState = NAME_END;
|
---|
82 | } else {
|
---|
83 | m_nextState = NAME;
|
---|
84 | }
|
---|
85 | break;
|
---|
86 |
|
---|
87 | case NAME:
|
---|
88 | c = readValueChar(NAME_END);
|
---|
89 | break;
|
---|
90 |
|
---|
91 | case NAME_END:
|
---|
92 | c = '"';
|
---|
93 | m_nextState = NAME_PATH_SEPARATOR;
|
---|
94 | break;
|
---|
95 |
|
---|
96 | case NAME_PATH_SEPARATOR:
|
---|
97 | c = ',';
|
---|
98 | m_nextState = PATH_TAG;
|
---|
99 | m_nextChar = TAG_PATH;
|
---|
100 | break;
|
---|
101 |
|
---|
102 | case PATH_TAG:
|
---|
103 | c = readTagChar(PATH_START);
|
---|
104 | break;
|
---|
105 |
|
---|
106 | case PATH_START:
|
---|
107 | c = '"';
|
---|
108 | m_nextChar = m_currentOutput->getPath();
|
---|
109 | if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) {
|
---|
110 | m_nextState = PATH_END;
|
---|
111 | } else {
|
---|
112 | m_nextState = PATH;
|
---|
113 | }
|
---|
114 | break;
|
---|
115 |
|
---|
116 | case PATH:
|
---|
117 | c = readValueChar(PATH_END);
|
---|
118 | break;
|
---|
119 |
|
---|
120 | case PATH_END:
|
---|
121 | c = '"';
|
---|
122 | m_nextState = PATH_VAR_SEPARATOR;
|
---|
123 | break;
|
---|
124 |
|
---|
125 | case PATH_VAR_SEPARATOR:
|
---|
126 | c = ',';
|
---|
127 | m_nextState = VAR_TAG;
|
---|
128 | m_nextChar = TAG_VAR;
|
---|
129 | break;
|
---|
130 |
|
---|
131 | case VAR_TAG:
|
---|
132 | c = readTagChar(VAR_START);
|
---|
133 | break;
|
---|
134 |
|
---|
135 | case VAR_START:
|
---|
136 | c = '"';
|
---|
137 | m_nextChar = m_currentOutput->getVariable();
|
---|
138 | if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) {
|
---|
139 | m_nextState = VAR_END;
|
---|
140 | } else {
|
---|
141 | m_nextState = VAR;
|
---|
142 | }
|
---|
143 | break;
|
---|
144 |
|
---|
145 | case VAR:
|
---|
146 | c = readValueChar(VAR_END);
|
---|
147 | break;
|
---|
148 |
|
---|
149 | case VAR_END:
|
---|
150 | c = '"';
|
---|
151 | m_nextState = OUTPUT_END;
|
---|
152 | break;
|
---|
153 |
|
---|
154 | case OUTPUT_END:
|
---|
155 | c = '}';
|
---|
156 | m_currentOutput = m_currentOutput->getNext();
|
---|
157 | if (m_currentOutput != NULL) {
|
---|
158 | m_nextState = NEXT_OUTPUT;
|
---|
159 | } else {
|
---|
160 | m_nextState = OUTPUTS_END;
|
---|
161 | }
|
---|
162 | break;
|
---|
163 |
|
---|
164 | case NEXT_OUTPUT:
|
---|
165 | c = ',';
|
---|
166 | m_nextChar = m_currentOutput->getName();
|
---|
167 | m_nextState = OUTPUT_START;
|
---|
168 | break;
|
---|
169 |
|
---|
170 | case OUTPUTS_END:
|
---|
171 | c = ']';
|
---|
172 | m_nextState = END;
|
---|
173 | break;
|
---|
174 | case END:
|
---|
175 | default:
|
---|
176 | c = '\0';
|
---|
177 | }
|
---|
178 |
|
---|
179 | return c;
|
---|
180 | }
|
---|
181 |
|
---|