Orocos Real-Time Toolkit  2.5.0
rtstreams.cpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Mon May 10 19:10:29 CEST 2004  rtstreams.cxx 
00003 
00004                         rtstreams.cxx -  description
00005                            -------------------
00006     begin                : Mon May 10 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 #include "os/rtstreams.hpp"
00038 #include "os/fosi.h"
00039 #include <cstring>
00040 
00041 namespace RTT
00042 {namespace os
00043 {
00044 
00045     printstream cout;
00046     
00047     basic_streams::~basic_streams()
00048     {}
00049 
00050     basic_ostreams& basic_ostreams::write( const char * c, streamsize n )
00051     {
00052         buf.sputn( c, n);
00053         return *this;
00054     }
00055 
00056     basic_ostreams& basic_ostreams::put( char c )
00057     {
00058         buf.sputc( c );
00059         return *this;
00060     }
00061 
00062     basic_istreams& basic_istreams::read(char* c, streamsize n)
00063     {
00064         buf.sgetn(c,n);
00065         return *this;
00066     }
00067 
00068     basic_istreams::streamsize basic_istreams::readsome(char* c, streamsize n)
00069     {
00070         return buf.sgetn(c,n);
00071     }
00072 
00073     basic_istreams& basic_istreams::get( char& c )
00074     {
00075         c = buf.sgetc();
00076         return *this;
00077     }
00078 
00079     basic_istreams& basic_istreams::get( char*c ,streamsize n, char delim)
00080     {
00081         streamsize i=0;
00082         int res;
00083         do {
00084             res = buf.sgetc();
00085             c[i++] = res;
00086         } while ( res != EOF && c[i-1] != delim && i != n );
00087 
00088         if ( i == n)
00089             c[i] = 0;
00090         else
00091             c[i-1] = 0;
00092 
00093         return *this;
00094     }
00095 
00096     basic_istreams& basic_istreams::get( char*c,  char delim)
00097     {
00098         streamsize i=0;
00099         int res;
00100         do {
00101             res = buf.sgetc();
00102             c[i++] = res;
00103         } while ( res != EOF && c[i-1] != delim );
00104         
00105         c[i-1] = 0;
00106 
00107         return *this;
00108     }
00109     
00110     basic_ostreams & endl( basic_ostreams & s )
00111     {
00112         return s << '\n';
00113     }
00114     
00115     basic_ostreams& basic_ostreams::operator<<( int i )
00116     {
00117         std::string result( int_to_string( i ) );
00118         write(result.c_str(), result.length() );
00119         return *this;
00120     }
00121 
00122     basic_ostreams& basic_ostreams::operator<<( long l )
00123     {
00124         std::string result( int_to_string( l ) );
00125         write(result.c_str(), result.length() );
00126         return *this;
00127     }
00128 
00129     basic_ostreams& basic_ostreams::operator<<( char c )
00130     {
00131         buf.sputc( c );
00132         return *this;
00133     }
00134 
00135     basic_ostreams& basic_ostreams::operator<<( char * c )
00136     {
00137         buf.sputn( c, strlen(c) );
00138         return *this;
00139     }
00140 
00141     basic_ostreams& basic_ostreams::operator<<( basic_ostreams & ( *f ) ( basic_ostreams & ) )
00142     {
00143         return ( *f ) ( *this );
00144     }
00145 
00146     basic_ostreams& basic_ostreams::operator<<( double f )
00147     {
00148         std::string result( float_to_string( float(f) ) );
00149         write( result.c_str(), result.length() );
00150         return *this;
00151     }
00152 
00153     basic_ostreams& basic_ostreams::operator<<( std::string s )
00154     {
00155         write( s.c_str(), s.length() );
00156         return *this;
00157     }
00158 
00159     basic_ostreams& basic_ostreams::operator<<( unsigned int u )
00160     {
00161         std::string result( unsigned_int_to_string( u ) );
00162         write( result.c_str(), result.length() );
00163         return *this;
00164     }
00165 
00166     basic_istreams& basic_istreams::operator>>( int &i )
00167     {
00168         std::string result;
00169         result.reserve(10);
00170         int i_res = buf.sgetc();
00171         while ( (i_res != EOF) && (i_res != ' ') )
00172         {
00173               result += char(i_res);
00174               i_res = buf.sgetc();
00175         }
00176         i = string_to_int( result );
00177         return *this;
00178     }
00179 
00180     basic_istreams& basic_istreams::operator>>( char &c )
00181     {
00182         c = buf.sgetc();
00183         return *this;
00184     }
00185 
00186     basic_istreams& basic_istreams::operator>>( double &f )
00187     {
00188         std::string result;
00189         result.reserve(40);
00190         int i_res = buf.sgetc();
00191         while ( (i_res != EOF) && (i_res != ' ') )
00192         {
00193               result += char(i_res);
00194               i_res = buf.sgetc();
00195         }
00196         //f = string_to_float( result );
00197         f = -1;
00198         return * this;
00199     }
00200 
00201     basic_istreams& basic_istreams::operator>>( std::string &s )
00202     {
00203         std::string result;
00204         int i_res; 
00205         do {
00206               i_res = buf.sgetc();
00207               if (i_res == EOF )
00208                   break;
00209               result += char(i_res);
00210         } while ( i_res != ' ' );
00211         
00212         s = result;
00213         
00214         return *this;
00215     }
00216 
00217     basic_istreams& basic_istreams::operator>>( unsigned int &u )
00218     {
00219         std::string result;
00220         result.reserve(10);
00221         int i_res = buf.sgetc();
00222         while ( (i_res != EOF) && (i_res != ' ') )
00223         {
00224               result += char(i_res);
00225               i_res = buf.sgetc();
00226         }
00227         
00228         u = string_to_unsigned_int( result );
00229         
00230         return *this;
00231     }
00232 
00233     basic_istreams::~basic_istreams()
00234     {}
00235 
00236     basic_ostreams::~basic_ostreams()
00237     {}
00238 
00239 
00240     printstream::~printstream()
00241     {}
00242 
00243     stringstreams::~stringstreams()
00244     {}
00245 
00246 
00247 }}