Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

PrintStream.cpp

00001 /*
00002  * NOTE: "zlib/libpng" style License
00003  *
00004  *                 ----=== s t d n e t ===----
00005  *
00006  * Copyright (c) 2002 Warwick Molloy   w-molloy@users.sf.net
00007  *
00008  * Copyright (c) 2002 Stdnet Community
00009  * HTTP://Sourceforge.net/projects/stdnet
00010  *
00011  * All references to "software" refer to the stdnet.
00012  *
00013  * This software is provided 'as-is', without any express or
00014  * implied warranty. In no event will the authors be held liable
00015  * for any damages arising from the use of this software.
00016  *
00017  * Permission is granted to anyone to use this software for any
00018  * purpose, including commercial applications, and to alter it
00019  * and redistribute it freely, subject to the following
00020  * restrictions:
00021  *
00022  * 1.  The origin of this software must not be misrepresented;
00023  *     you must not claim that you wrote the original software.
00024  *     If you use this software in a product, an acknowledgment
00025  *     in the product documentation would be appreciated but
00026  *     is not required.
00027  *
00028  * 2.  Altered source versions must be plainly marked as such,
00029  *     and must not be misrepresented as being the original
00030  *     software.
00031  *
00032  * 3.  This notice may not be removed or altered from any source
00033  *     distribution.
00034  */
00035 
00036 #include <stdnet/config.h>
00037 #include <stdnet/io/IOException>
00038 #include <stdnet/io/PrintStream>
00039 
00040 #include <stdio.h>
00041 #include <string.h>
00042 
00043 namespace stdbase {
00044     namespace io {
00045 
00046 const system::reference<PrintStream> PrintStream::
00047     create( const OutputStreamRef & ref)
00048 {
00049     const system::reference<PrintStream> object = new PrintStream( ref );
00050     return object;
00051 }
00052 
00053 void PrintStream::
00054     flush()
00055     throw (IOException)
00056 {
00057     try
00058     {
00059         output->flush();
00060     }
00061     catch( IOException e)
00062     {
00063         e = e; // avoid warning
00064         error = true;
00065     }
00066 }
00067 
00068 void PrintStream::
00069     close()
00070     throw (IOException)
00071 {
00072     try
00073     {
00074         output->close();
00075     }
00076     catch( IOException e)
00077     {
00078         e = e; // avoid warning
00079         error = true;
00080     }
00081 }
00082 
00083 void PrintStream::
00084     write( int b )
00085     throw (IOException)
00086 {
00087     try
00088     {   output->write(b); }
00089     catch( IOException e)
00090     {
00091         e = e; // avoid warning
00092         error = true;
00093     }
00094 }
00095 
00096 void PrintStream::
00097     write( byte b[], int len)
00098     throw (IOException)
00099 {
00100     try
00101     {   output->write(b, len); }
00102     catch( IOException e)
00103     {
00104         e = e; // avoid warning
00105         error = true;
00106     }
00107 }
00108 
00109 void PrintStream::
00110     println( system::StringRef str)
00111 {
00112     int len = str->length();
00113     char * buffer = new char[ len + 2];
00114     str-> getChars( 0, len, buffer, 0);
00115     buffer[ len ] = '\n';
00116     buffer[ len + 1] = 0;
00117 
00118     try
00119     {   output->write( (byte *)(buffer), len + 1 ); }
00120     catch( IOException e)
00121     {
00122         e = e; // avoid warning
00123         error = true;
00124     }
00125 
00126     delete [] buffer;
00127 }
00128 
00129 void PrintStream::
00130     println( const char text[] )
00131 {
00132     int len = strlen( text );
00133     try
00134     {
00135         output->write( (byte *)(text), len );
00136         output->write( (byte) '\n' );
00137     }
00138     catch( IOException e)
00139     {
00140         e = e; // avoid warning
00141         error = true;
00142     }
00143 }
00144 
00145 void PrintStream::
00146     println( int i )
00147 {
00148     int len;
00149     const int max = 64;
00150     char buffer[ max ];
00151 
00152     len = snprintf( buffer, max, "%d\n", i );
00153 
00154     try
00155     {   output->write( (byte *)(buffer), len ); }
00156     catch( IOException e)
00157     {
00158         e = e; // avoid warning
00159         error = true;
00160     }
00161 }
00162 
00163 void PrintStream::
00164     print( system::StringRef str)
00165 {
00166     int len = str->length();
00167     char * buffer = new char[ len + 2];
00168     str-> getChars( 0, len, buffer, 0);
00169     buffer[ len ] = 0;
00170 
00171     try
00172     {   output->write( (byte *)(buffer), len ); }
00173     catch( IOException e)
00174     {
00175         e = e; // avoid warning
00176         error = true;
00177     }
00178 
00179     delete [] buffer;
00180 }
00181 
00182 void PrintStream::
00183     print( const char text[] )
00184 {
00185     int len = strlen( text );
00186     try
00187     {
00188         output->write( (byte *)(text), len );
00189     }
00190     catch( IOException e)
00191     {
00192         e = e; // avoid warning
00193         error = true;
00194     }
00195 }
00196 
00197 void PrintStream::
00198     print( int i )
00199 {
00200     int len;
00201     const int max = 64;
00202     char buffer[ max ];
00203 
00204     len = snprintf( buffer, max, "%d", i );
00205 
00206     try
00207     {   output->write( (byte *)(buffer), len ); }
00208     catch( IOException e)
00209     {
00210         e = e; // avoid warning
00211         error = true;
00212     }
00213 }
00214 
00215 bool PrintStream::
00216     checkError()
00217 {
00218     return error;
00219 }
00220 
00221 bool PrintStream::
00222     equals( system::ObjectRef o) const
00223 {
00224     return const_cast<const stdbase::system::Object *>(o.get()) == this;
00225 }
00226 
00227 PrintStream::
00228 PrintStream( const OutputStreamRef & ref)
00229     :   Object(),
00230         output( ref ),
00231         error(false)
00232 {}
00233 
00234 PrintStream::
00235 ~PrintStream()
00236 {
00237     try {
00238         output->close();
00239     }
00240     catch( IOException e)
00241     {
00242         e = e; // avoid warning
00243     }
00244 }
00245 
00246     } // -- end io --
00247 } // ------ end stdbase --
00248 
00249 

Generated at Tue Aug 13 14:19:39 2002 for stdnet2 by doxygen1.2.9.1 written by Dimitri van Heesch, © 1997-2001