/elec/propeller-clock

To get this branch, use:
bzr branch http://bzr.ed.am/elec/propeller-clock

« back to all changes in this revision

Viewing changes to src/util/PString.h

  • Committer: edam
  • Date: 2012-02-28 16:50:26 UTC
  • Revision ID: edam@waxworlds.org-20120228165026-pwnwo300xx2e2kg6
removed ulibc, fixed button, added text rendering

Show diffs side-by-side

added added

removed removed

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