Arduino Radio Library  0.9
A set of Arduino libraries to control diverse FM radio receiver chips in Arduino projects.
StringBuffer.h
Go to the documentation of this file.
1 
2 #define NUL '\0'
3 #define CR '\r'
4 #define LF '\n'
5 #define SPACE ' '
6 #define QUOTE '\"'
7 
8 // StringBuffer is a helper class for building long texts by using an fixed allocated memory region.
9 
10 class StringBuffer {
11  public:
13  StringBuffer(char *buffer, unsigned int bufferSize)
14  {
15  _buf = buffer;
16  _size = bufferSize;
17  clear();
18  };
19 
21  void clear() {
22  *_buf = '\0';
23  _len = 1; // The ending NUL character has to be part of the buffer;
24  };
25 
26  char *getBuffer() {
27  return (_buf);
28  };
29 
30  uint16_t getLength() {
31  return(_len);
32  };
33 
34  void append(char c)
35  {
36  char *t = _buf + _len - 1;
37  if (_len < _size) {
38  *t++ = c;
39  _len++;
40  } // if
41  *t = NUL;
42  }; // append()
43 
44  void append(const char *txt)
45  {
46  char *t = _buf + _len - 1;
47  char *s = (char *)txt;
48  while (_len < _size) {
49  if (! *s) break;
50  *t++ = *s++;
51  _len++;
52  }
53  *t = NUL;
54  }; // append()
55 
56 
57  void append(const __FlashStringHelper *txt)
58  {
59  char *t = _buf + _len - 1;
60  PGM_P s = reinterpret_cast<PGM_P>(txt);
61  while (_len < _size) {
62  unsigned char c = pgm_read_byte(s++);
63  if (! c) break;
64  *t++ = c;
65  _len++;
66  }
67  *t = NUL;
68  }; // append()
69 
70 
72  void append(int num)
73  {
74  char buf[1 + 3 * sizeof(int)];
75  itoa(num, buf, 10);
76  append(buf);
77  } // append()
78 
79 
81  void append(uint32_t num)
82  {
83  char buf[1 + 3 * sizeof(uint32_t)];
84  ultoa(num, buf, 10);
85  append(buf);
86  } // append()
87 
88 
90  void appendQuoted(const char *txt)
91  {
92  append(QUOTE);
93  append(txt);
94  append(QUOTE);
95  } // appendQuoted()
96 
97 
99  void appendJSON(const char *name, char *value) {
100  appendQuoted(name);
101  append(':');
102  appendQuoted(value);
103  } // appendJSON()
104 
105 
107  void appendJSON(const char *name, int value) {
108  appendQuoted(name);
109  append(':');
110  append(value);
111  } // appendJSON()
112 
113  private:
114  char* _buf;
115  unsigned int _size;
116  unsigned int _len;
117 
118 };
119 
uint16_t getLength()
Definition: StringBuffer.h:30
#define QUOTE
Definition: StringBuffer.h:6
unsigned int _len
The actual used len of the buffer.
Definition: StringBuffer.h:116
void append(char c)
Definition: StringBuffer.h:34
void append(uint32_t num)
Append a number.
Definition: StringBuffer.h:81
Definition: StringBuffer.h:10
void clear()
clear the buffer.
Definition: StringBuffer.h:21
void appendJSON(const char *name, char *value)
Append an object with value in the JSON notation to the buffer.
Definition: StringBuffer.h:99
void append(const char *txt)
Definition: StringBuffer.h:44
void append(const __FlashStringHelper *txt)
Definition: StringBuffer.h:57
char * getBuffer()
Definition: StringBuffer.h:26
StringBuffer(char *buffer, unsigned int bufferSize)
setup a StringBuffer by passing a local char[] variable and it's size.
Definition: StringBuffer.h:13
#define NUL
Definition: StringBuffer.h:2
unsigned int _size
The size of the buffer.
Definition: StringBuffer.h:115
void appendJSON(const char *name, int value)
Append an object with value in the JSON notation to the buffer.
Definition: StringBuffer.h:107
void appendQuoted(const char *txt)
Append a string with enclosing quotes at the given buffer.
Definition: StringBuffer.h:90
char * _buf
The allocated buffer.
Definition: StringBuffer.h:114
void append(int num)
Append a number.
Definition: StringBuffer.h:72