1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/*
PString.h - Lightweight printable string class
Copyright (c) 2009-2012 Mikal Hart. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef PString_h
#define PString_h
#include "Print.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#define PSTRING_LIBRARY_VERSION 3
class PString : public Print
{
private:
char *_buf, *_cur;
size_t _size;
public:
#if defined(ARDUINO) && ARDUINO >= 100
virtual size_t write(uint8_t);
#else
virtual void write(uint8_t);
#endif
public:
// Basic constructor requires a preallocated buffer
PString(char *buf, size_t size) : _buf(buf), _size(size)
{ begin(); }
// Constructor that doesn't clear the supplied buffer
PString(char *buf, size_t size, bool)
{ begin( buf, size ); }
// templated constructors allow inline renderings of this type: PString(buf, size, myfloat[, modifier]);
template<class T> PString(char *buf, size_t size, T arg) : _buf(buf), _size(size)
{ begin(); print(arg); }
template<class T> PString(char *buf, size_t size, T arg, int modifier) : _buf(buf), _size(size)
{ begin(); print(arg, modifier); }
// returns the length of the current string, not counting the 0 terminator
inline const size_t length()
{ return _cur - _buf; }
// returns the capacity of the string
inline const size_t capacity()
{ return _size; }
// gives access to the internal string
inline operator const char *()
{ return _buf; }
// compare to another string
bool operator==(const char *str)
{ return _size > 0 && !strcmp(_buf, str); }
// call this to re-use an existing string
void begin();
// call this to re-use an existing string and change where the buffer lives
void begin(char *, size_t size);
// This function allows assignment to an arbitrary scalar value like str = myfloat;
template<class T> inline PString &operator =(T arg)
{ begin(); print(arg); return *this; }
// Concatenation str += myfloat;
template<class T> inline PString &operator +=(T arg)
{ print(arg); return *this; }
// Safe access to sprintf-like formatting, e.g. str.format("Hi, my name is %s and I'm %d years old", name, age);
int format(char *str, ...);
};
#endif
|