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

mootCxxIO.h

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*- */
00002 
00003 /*
00004    libmoot : moocow's part-of-speech tagging library
00005    Copyright (C) 2004 by Bryan Jurish <moocow@ling.uni-potsdam.de>
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020 */
00021 
00022 /*--------------------------------------------------------------------------
00023  * File: mootCxxIO.h
00024  * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
00025  * Description:
00026  *   + moot PoS tagger : low-level I/O abstractions for C++ streams
00027  *   + these are ugly, but there appears to be no better
00028  *     (read "faster in the general case") way to get
00029  *     C FILE*s to jive with C++ streams, and I REALLY
00030  *     like printf() and friends...
00031  *--------------------------------------------------------------------------*/
00032 
00033 #ifndef _MOOT_CXXIO_H
00034 #define _MOOT_CXXIO_H
00035 
00036 #include <iostream>
00037 #include <fstream>
00038 
00039 #include <mootIO.h>
00040 
00041 #include <stdarg.h>
00042 #include <string.h>
00043 #include <assert.h>
00044 
00045 namespace mootio {
00046   using namespace std;
00047 
00048   /*====================================================================
00049    * mcxxstream : std::(i|o)stream i/o
00050    *====================================================================*/
00051   template <class _StreamClass>
00052   class mcxxstream : virtual public mstream {
00053   public:
00054     _StreamClass *streamp;  
00055   public:
00056     /*----------------------------------------------------------
00057      * mcxxstream: constructors
00058      */
00060 
00061 
00062     mcxxstream(_StreamClass *streamptr=NULL) : streamp(streamptr) {};
00063 
00065     mcxxstream(_StreamClass &stream) : streamp(&stream) {};
00066 
00068     virtual ~mcxxstream(void) {};
00070 
00071     /*----------------------------------------------------------
00072      * mcxxstream: integrity
00073      */
00075 
00076 
00077     virtual bool valid(void) { return streamp && streamp->good(); };
00078 
00080     virtual bool eof(void) { return !streamp || streamp->eof(); };
00082   }; //-- /mcxxstream
00083 
00084 
00085   /*====================================================================
00086    * micxxstream : std::istream input
00087    *====================================================================*/
00089   class micxxstream
00090     : public mcxxstream<std::istream>,
00091       public mistream
00092   {
00093   public:
00094     /*----------------------------------------------------------
00095      * micxxstream: constructors
00096      */
00098 
00099 
00100     micxxstream(std::istream *streamptr=NULL)
00101       : mcxxstream<std::istream>(streamptr)
00102     {};
00103 
00105     micxxstream(std::istream &istream)
00106       : mcxxstream<std::istream>(istream)
00107     {};
00108 
00110     ~micxxstream(void) {};
00112 
00113     /*----------------------------------------------------------
00114      * micxxstream: input
00115      */
00117 
00118 
00120     virtual ByteCount read(char *buf, size_t n) {
00121       return streamp ? streamp->read(buf,n).gcount() : 0;
00122     };
00123 
00125     virtual int getc(void) {
00126       return streamp ? streamp->get() : EOF;
00127     };
00129   }; //-- /micxxstream
00130 
00131 
00132   /*====================================================================
00133    * mocxxstream : std::ostream output
00134    *====================================================================*/
00136   class mocxxstream
00137     : public mcxxstream<std::ostream>,
00138       public mostream
00139   {
00140   public:
00141     char      *_printf_buffer; 
00142     ByteCount  _printf_buflen; 
00143 
00144   public:
00145     /*----------------------------------------------------------
00146      * mocxxstream: output
00147      */
00149 
00150 
00151     mocxxstream(std::ostream *ostreamptr)
00152       : mcxxstream<std::ostream>(ostreamptr),
00153         _printf_buffer(NULL),
00154         _printf_buflen(0)
00155     {};
00156 
00158     mocxxstream(std::ostream &ostream)
00159       : mcxxstream<std::ostream>(ostream),
00160         _printf_buffer(NULL),
00161         _printf_buflen(0)
00162     {};
00163 
00165     virtual ~mocxxstream(void)
00166     {
00167       if (_printf_buffer) free(_printf_buffer);
00168     };
00170 
00171     /*----------------------------------------------------------
00172      * mocxxstream: output
00173      */
00175 
00176 
00177     virtual bool mflush(void) { return streamp->flush().good(); };
00178 
00180     virtual bool write(const char *buf, size_t n) {
00181       return streamp ? streamp->write(buf,n).good() : false;
00182     };
00183 
00185     virtual bool putc(unsigned char c) {
00186       return streamp ? streamp->put(c).good() : false;
00187     };
00188 
00190     virtual bool puts(const char *s) {
00191       return streamp ? ((*streamp) << s).good() : false;
00192     };
00194     virtual bool puts(const std::string &s) {
00195       return streamp ? ((*streamp) << s).good() : false;
00196     };
00197 
00200     virtual bool vprintf(const char *fmt, va_list &ap) {
00201       if (!streamp) return false;
00202       ByteCount nchars = vsnprintf(_printf_buffer, _printf_buflen, fmt, ap);
00203       if (nchars >= _printf_buflen) {
00204         //-- oops: reallocate!
00205         _printf_buffer = (char *)realloc(_printf_buffer, nchars+1);
00206         assert(_printf_buffer != NULL);
00207         _printf_buflen = nchars+1;
00208         //-- ... and try again
00209         nchars = vsnprintf(_printf_buffer, _printf_buflen, fmt, ap);
00210       }
00211       //-- we now have a full string buffer: write it
00212       return streamp->write(_printf_buffer, nchars).good();
00213     };
00215   }; //-- /mocxxstream
00216 
00217 }; //-- /namespace mootio
00218 
00219 
00220 #endif //_MOOT_CXXIO_H

Generated on Wed Jul 28 15:48:02 2004 for libmoot by doxygen1.2.15