AceRoutine  0.1
A low-memory, fast-switching, cooperative multitasking library using stackless coroutines on Arduino platforms.
StreamReader.cpp
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #include <ctype.h> // atoi()
26 #include "StreamReader.h"
27 
28 namespace ace_routine {
29 namespace cli {
30 
31 bool StreamReader::getChar(char* c) {
32  if (mPushback != 0) {
33  *c = mPushback;
34  mPushback = 0;
35  return true;
36  }
37  if (mStream.available() == 0) return false;
38  *c = mStream.read();
39  mPushback = 0;
40  return true;
41 }
42 
43 bool StreamReader::getWordString(bool* isError, char** word) {
44  char c;
45  while (getChar(&c)) {
46  if (isspace(c)) {
47  if (mIndex == 0) {
48  *isError = true;
49  } else {
50  pushback(c);
51  resetBuffer();
52  *isError = false;
53  *word = mBuf;
54  }
55  return true;
56  }
57  bool error = addToBuffer(c);
58  if (error) {
59  *isError = error;
60  return true;
61  }
62  }
63  return false;
64 }
65 
66 bool StreamReader::getInteger(bool* isError, int* value) {
67  char c;
68  while (getChar(&c)) {
69  if (!isdigit(c)) {
70  if (mIndex == 0) {
71  resetBuffer();
72  *isError = true;
73  } else {
74  pushback(c);
75  resetBuffer();
76  // NOTE: What happens if the intger in mBuf is too big?
77  *value = atoi(mBuf);
78  *isError = false;
79  }
80  return true;
81  }
82  bool error = addToBuffer(c);
83  if (error) {
84  *isError = error;
85  return true;
86  }
87  }
88  return false;
89 }
90 
91 bool StreamReader::getComma(bool* isError) {
92  char c;
93  if (!getChar(&c)) return false;
94  *isError = (c != ',');
95  return true;
96 }
97 
99  char c;
100  while (getChar(&c)) {
101  if (!isspace(c)) {
102  pushback(c);
103  return true;
104  }
105  }
106  return false;
107 }
108 
109 bool StreamReader::getLine(bool* isError, char** line) {
110  while (mStream.available() > 0) {
111  char c = mStream.read();
112  mBuf[mIndex] = c;
113  mIndex++;
114  if (mIndex >= mBufSize - 1) {
115  resetBuffer();
116  *isError = true;
117  *line = mBuf;
118  return true;
119  }
120  if (c == '\n') {
121  resetBuffer();
122  *isError = false;
123  *line = mBuf;
124  return true;
125  }
126  }
127  return false;
128 }
129 
130 bool StreamReader::addToBuffer(char c) {
131  mBuf[mIndex] = c;
132  mIndex++;
133  if (mIndex >= mBufSize - 1) {
134  resetBuffer();
135  return true;
136  } else {
137  return false;
138  }
139 }
140 
141 }
142 }
bool getChar(char *c)
Get a character from serial port.
bool getLine(bool *isError, char **line)
Get a line.
bool getInteger(bool *isError, int *value)
Get an integer.
bool skipWhiteSpace()
Skip whitespace.
bool getComma(bool *isError)
Parse a comma.
bool getWordString(bool *isError, char **word)
Get a string bounded by whitespace, i.e.