Orocos Real-Time Toolkit  2.6.0
rtstreambufs.hpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jan 19 14:11:19 CET 2004  rtstreambufs.hpp 
00003 
00004                         rtstreambufs.hpp -  description
00005                            -------------------
00006     begin                : Mon January 19 2004
00007     copyright            : (C) 2004 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.be
00009  
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/ 
00037  
00038 #ifndef RTSTREAMBUFS_HPP
00039 #define RTSTREAMBUFS_HPP
00040 
00041 #include <string>
00042 #include "fosi.h"
00043 
00044 namespace RTT
00045 {namespace os
00046 {
00047 
00048 
00052     class RTT_API streambufs
00053     {
00054         public:
00055             typedef unsigned int streamsize;
00056         
00057             virtual ~streambufs() {}
00058             virtual int sgetc() = 0;
00059             virtual streamsize sgetn(char* s, streamsize n ) = 0;
00060             virtual int sputc(char c) = 0;
00061             virtual streamsize sputn(const char* s, streamsize n ) = 0;
00062     };
00063 
00064 
00070     class RTT_API stringbufs
00071         : public streambufs
00072     {
00073         public:
00074             typedef streambufs::streamsize streamsize;
00075             
00076             static const streamsize buf_size = 512;
00077             
00078             stringbufs(const std::string& c="") : _s(c), ptr(0), end(0) { _s.reserve( buf_size ); }
00079 
00080             virtual ~stringbufs() {}
00081             
00082             virtual int sgetc()
00083             {
00084                 if ( _s.empty() || ptr == _s.length() ) return EOF;
00085                 return _s[ptr++];
00086             }
00087             
00088             virtual streamsize sgetn( char* s, streamsize n )
00089             {
00090                 if ( _s.empty() || ptr ==  _s.length() ) return 0;
00091                 streamsize len = (n <= _s.length() ? n : _s.length() );
00092                 _s.copy(s, len, ptr);
00093                 ptr += len;
00094                 return len;
00095             }
00096             
00097             virtual int sputc(char c)
00098             {
00099                 cleanup();
00100                 
00101                 if ( ptr == _s.capacity() )
00102                     return EOF;
00103                 _s.push_back(c);
00104                 return 1;
00105             }
00106             
00107             virtual streamsize sputn(const char* s, streamsize n )
00108             {
00109                 cleanup(n);
00110                 if ( ptr == _s.capacity() )
00111                     return 0;
00112                 _s.append(s,n); // possibly expand _s !.
00113                 return n;
00114             }
00115             
00116             std::string str() const
00117             { 
00118                 return _s.substr(ptr, _s.length() - ptr);
00119             }
00120 
00121             void str( std::string& new_str )
00122             {
00123                 ptr = 0;
00124                 _s = new_str;
00125             }
00126         private:
00127             void clear()
00128             {
00129                 ptr = 0;
00130                 end = 0;
00131                 _s.erase();
00132             }
00133 
00137             void cleanup(int free = 1)
00138             {
00139                 if (ptr == _s.length() && !_s.empty() )
00140                     clear(); // when all is read, clear all
00141                 if ( ptr != 0 && _s.length() + free >= _s.capacity() )
00142                 {
00143                     _s.erase(0, ptr);// when some _must_ and _can_ be freed.
00144                     ptr = 0;
00145                 }
00146             }
00147 
00148             std::string _s;
00149             streamsize ptr;
00150             streamsize end;
00151    };
00152 
00153     class RTT_API printbufs
00154         : public streambufs
00155     {
00156         public:
00157             typedef streambufs::streamsize streamsize;
00158             
00159             virtual int sgetc()
00160             {
00161                 return EOF;
00162             }
00163             
00164         virtual streamsize sgetn( char* /*s*/, streamsize /*n*/ )
00165             {
00166                 return 0;
00167             }
00168             
00169             virtual int sputc(char c)
00170             {
00171                 rtos_printf("%c",c);
00172                 return 1;
00173             }
00174             
00175             virtual streamsize sputn(const char* s, streamsize n )
00176             {
00177                 rtos_printf("%*s", n, s);
00178                 return n;
00179             }
00180             
00181         private:
00182    };
00183 
00184 }}
00185 
00186 
00187 #endif