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 "BaseFormatter.h"
|
---|
24 |
|
---|
25 | char BaseFormatter::escape(char c) {
|
---|
26 | char outChar = c;
|
---|
27 | switch(c) {
|
---|
28 | case '\\':
|
---|
29 | case '"':
|
---|
30 | outChar = '\\';
|
---|
31 | m_escapedChar = c;
|
---|
32 | break;
|
---|
33 | case '\b':
|
---|
34 | outChar = '\\';
|
---|
35 | m_escapedChar = 'b';
|
---|
36 | break;
|
---|
37 | case '\f':
|
---|
38 | outChar = '\\';
|
---|
39 | m_escapedChar = 'f';
|
---|
40 | break;
|
---|
41 | case '\n':
|
---|
42 | outChar = '\\';
|
---|
43 | m_escapedChar = 'n';
|
---|
44 | break;
|
---|
45 | case '\r':
|
---|
46 | outChar = '\\';
|
---|
47 | m_escapedChar = 'r';
|
---|
48 | break;
|
---|
49 | case '\t':
|
---|
50 | outChar = '\\';
|
---|
51 | m_escapedChar = 't';
|
---|
52 | break;
|
---|
53 | default:
|
---|
54 | m_escapedChar = '\0';
|
---|
55 | }
|
---|
56 | return outChar;
|
---|
57 | }
|
---|
58 |
|
---|
59 | char BaseFormatter::finishEscape() {
|
---|
60 | char c = m_escapedChar;
|
---|
61 | m_escapedChar = '\0';
|
---|
62 | return c;
|
---|
63 | }
|
---|
64 |
|
---|
65 | char BaseFormatter::readTagChar(int nextState) {
|
---|
66 | char c = pgm_read_byte(m_nextChar++);
|
---|
67 | if (pgm_read_byte(m_nextChar) == '\0') {
|
---|
68 | m_nextState = nextState;
|
---|
69 | }
|
---|
70 | return c;
|
---|
71 | }
|
---|
72 |
|
---|
73 | char BaseFormatter::readValueChar(int nextState) {
|
---|
74 | char c;
|
---|
75 | if (isEscaping()) {
|
---|
76 | c = finishEscape();
|
---|
77 | if (*m_nextChar == '\0') {
|
---|
78 | m_nextState = nextState;
|
---|
79 | }
|
---|
80 | } else {
|
---|
81 | c = escape(*m_nextChar++);
|
---|
82 | if (!isEscaping()) {
|
---|
83 | if(*m_nextChar == '\0') {
|
---|
84 | m_nextState = nextState;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | return c;
|
---|
89 | }
|
---|
90 |
|
---|
91 | char BaseFormatter::readStartTagChar(const char* tag, int nextState) {
|
---|
92 | m_nextChar = tag;
|
---|
93 | char c = pgm_read_byte(m_nextChar++);
|
---|
94 | m_nextState = nextState;
|
---|
95 | return c;
|
---|
96 | }
|
---|