1 | /* |
---|
2 | ############################################################################### |
---|
3 | # |
---|
4 | # Temboo Arduino library |
---|
5 | # |
---|
6 | # Copyright 2015, 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 "ChoreoInputFormatter.h" |
---|
26 | #include "ChoreoInputSet.h" |
---|
27 | |
---|
28 | static const char TAG_INPUTS_START[] PROGMEM = "\"inputs\":{"; |
---|
29 | |
---|
30 | ChoreoInputFormatter::ChoreoInputFormatter(const ChoreoInputSet* inputSet) { |
---|
31 | m_inputSet = inputSet; |
---|
32 | reset(); |
---|
33 | } |
---|
34 | |
---|
35 | void ChoreoInputFormatter::reset() { |
---|
36 | m_currentInput = NULL; |
---|
37 | m_nextChar = NULL; |
---|
38 | if (m_inputSet == NULL || m_inputSet->isEmpty()) { |
---|
39 | m_nextState = END; |
---|
40 | } else { |
---|
41 | m_nextState = START; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | bool ChoreoInputFormatter::hasNext() { |
---|
46 | return m_nextState != END; |
---|
47 | } |
---|
48 | |
---|
49 | char ChoreoInputFormatter::next() { |
---|
50 | char c; |
---|
51 | switch(m_nextState) { |
---|
52 | case START: |
---|
53 | c = readStartTagChar(TAG_INPUTS_START, INPUTS_TAG); |
---|
54 | break; |
---|
55 | |
---|
56 | case INPUTS_TAG: |
---|
57 | c = readTagChar(NAME_START); |
---|
58 | if (m_nextState == NAME_START) { |
---|
59 | m_currentInput= m_inputSet->getFirstInput(); |
---|
60 | } |
---|
61 | break; |
---|
62 | |
---|
63 | case NAME_START: |
---|
64 | c = '"'; |
---|
65 | m_nextChar = m_currentInput->getName(); |
---|
66 | if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) { |
---|
67 | m_nextState = NAME_END; |
---|
68 | } else { |
---|
69 | m_nextState = NAME; |
---|
70 | } |
---|
71 | break; |
---|
72 | |
---|
73 | case NAME: |
---|
74 | c = readValueChar(NAME_END); |
---|
75 | break; |
---|
76 | |
---|
77 | case NAME_END: |
---|
78 | c = '"'; |
---|
79 | m_nextState = NAME_VALUE_SEPARATOR; |
---|
80 | break; |
---|
81 | |
---|
82 | case NAME_VALUE_SEPARATOR: |
---|
83 | c = ':'; |
---|
84 | m_nextState = VALUE_START; |
---|
85 | break; |
---|
86 | |
---|
87 | case VALUE_START: |
---|
88 | c = '"'; |
---|
89 | m_nextChar = m_currentInput->getValue(); |
---|
90 | if ((NULL == m_nextChar) || ('\0' == *m_nextChar)) { |
---|
91 | m_nextState = VALUE_END; |
---|
92 | } else { |
---|
93 | m_nextState = VALUE; |
---|
94 | } |
---|
95 | break; |
---|
96 | |
---|
97 | case VALUE: |
---|
98 | c = readValueChar(VALUE_END); |
---|
99 | break; |
---|
100 | |
---|
101 | case VALUE_END: |
---|
102 | c = '"'; |
---|
103 | m_currentInput = m_currentInput->getNext(); |
---|
104 | if (m_currentInput != NULL) { |
---|
105 | m_nextState = NEXT_INPUT; |
---|
106 | } else { |
---|
107 | m_nextState = INPUTS_END; |
---|
108 | } |
---|
109 | break; |
---|
110 | case NEXT_INPUT: |
---|
111 | c = ','; |
---|
112 | m_nextChar = m_currentInput->getName(); |
---|
113 | m_nextState = NAME_START; |
---|
114 | break; |
---|
115 | |
---|
116 | case INPUTS_END: |
---|
117 | c = '}'; |
---|
118 | m_nextState = END; |
---|
119 | break; |
---|
120 | case END: |
---|
121 | default: |
---|
122 | c = '\0'; |
---|
123 | } |
---|
124 | return c; |
---|
125 | } |
---|