rtstreambufs.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 19 14:11:19 CET 2004 rtstreambufs.hpp
3 
4  rtstreambufs.hpp - description
5  -------------------
6  begin : Mon January 19 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 #ifndef RTSTREAMBUFS_HPP
39 #define RTSTREAMBUFS_HPP
40 
41 #include <string>
42 #include "fosi.h"
43 
44 namespace RTT
45 {namespace os
46 {
47 
48 
52  class RTT_API streambufs
53  {
54  public:
55  typedef unsigned int streamsize;
56 
57  virtual ~streambufs() {}
58  virtual int sgetc() = 0;
59  virtual streamsize sgetn(char* s, streamsize n ) = 0;
60  virtual int sputc(char c) = 0;
61  virtual streamsize sputn(const char* s, streamsize n ) = 0;
62  };
63 
64 
70  class RTT_API stringbufs
71  : public streambufs
72  {
73  public:
75 
76  static const streamsize buf_size = 512;
77 
78  stringbufs(const std::string& c="") : _s(c), ptr(0), end(0) { _s.reserve( buf_size ); }
79 
80  virtual ~stringbufs() {}
81 
82  virtual int sgetc()
83  {
84  if ( _s.empty() || ptr == _s.length() ) return EOF;
85  return _s[ptr++];
86  }
87 
88  virtual streamsize sgetn( char* s, streamsize n )
89  {
90  if ( _s.empty() || ptr == _s.length() ) return 0;
91  streamsize len = (n <= _s.length() ? n : _s.length() );
92  _s.copy(s, len, ptr);
93  ptr += len;
94  return len;
95  }
96 
97  virtual int sputc(char c)
98  {
99  cleanup();
100 
101  if ( ptr == _s.capacity() )
102  return EOF;
103  _s.push_back(c);
104  return 1;
105  }
106 
107  virtual streamsize sputn(const char* s, streamsize n )
108  {
109  cleanup(n);
110  if ( ptr == _s.capacity() )
111  return 0;
112  _s.append(s,n); // possibly expand _s !.
113  return n;
114  }
115 
116  std::string str() const
117  {
118  return _s.substr(ptr, _s.length() - ptr);
119  }
120 
121  void str( std::string& new_str )
122  {
123  ptr = 0;
124  _s = new_str;
125  }
126  private:
127  void clear()
128  {
129  ptr = 0;
130  end = 0;
131  _s.erase();
132  }
133 
137  void cleanup(int free = 1)
138  {
139  if (ptr == _s.length() && !_s.empty() )
140  clear(); // when all is read, clear all
141  if ( ptr != 0 && _s.length() + free >= _s.capacity() )
142  {
143  _s.erase(0, ptr);// when some _must_ and _can_ be freed.
144  ptr = 0;
145  }
146  }
147 
148  std::string _s;
149  streamsize ptr;
150  streamsize end;
151  };
152 
153  class RTT_API printbufs
154  : public streambufs
155  {
156  public:
158 
159  virtual int sgetc()
160  {
161  return EOF;
162  }
163 
164  virtual streamsize sgetn( char* /*s*/, streamsize /*n*/ )
165  {
166  return 0;
167  }
168 
169  virtual int sputc(char c)
170  {
171  rtos_printf("%c",c);
172  return 1;
173  }
174 
175  virtual streamsize sputn(const char* s, streamsize n )
176  {
177  rtos_printf("%*s", n, s);
178  return n;
179  }
180 
181  private:
182  };
183 
184 }}
185 
186 
187 #endif
streambufs::streamsize streamsize
virtual streamsize sgetn(char *, streamsize)
unsigned int streamsize
virtual int sputc(char c)
std::string str() const
void cleanup(int free=1)
void str(std::string &new_str)
stringbufs(const std::string &c="")
streambufs::streamsize streamsize
virtual int sputc(char c)
virtual streamsize sputn(const char *s, streamsize n)
virtual int sgetc()
#define rtos_printf
Definition: ecos/fosi.h:272
virtual streamsize sgetn(char *s, streamsize n)
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
virtual streamsize sputn(const char *s, streamsize n)
virtual int sgetc()


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:35