Main Page | Directories | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

mootIO.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) 2003-2004 by Bryan Jurish <moocow@ling.uni-potsdam.de>
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011    
00012    This library 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 GNU
00015    Lesser General Public License for more details.
00016    
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with this library; 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: mootIO.h
00024  * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
00025  * Description:
00026  *   + moot PoS tagger : low-level I/O abstractions
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_IO_H
00034 #define _MOOT_IO_H
00035 
00036 #include <stdio.h>
00037 #include <stdarg.h>
00038 #include <string.h>
00039 #include <errno.h>
00040 #include <assert.h>
00041 
00042 #include <string>
00043 
00045 namespace mootio {
00046 
00047   /*====================================================================
00048    * mootio: types
00049    *====================================================================*/
00051   typedef int ByteCount;
00052 
00053   /*====================================================================
00054    * mstream: base class
00055    *====================================================================*/
00076   class mstream {
00077   public:
00078     std::string name;  
00079 
00080   public:
00081     /*----------------------------------------------------------*/
00083 
00084 
00085     mstream(const std::string &myname="") : name(myname) {};
00086 
00088     virtual ~mstream(void) {};
00090 
00091     /*----------------------------------------------------------*/
00093 
00094 
00095     virtual bool valid(void) { return false; };
00096 
00098     inline operator bool(void) { return valid(); };
00099 
00101     virtual bool eof(void) { return true; };
00102 
00104     virtual std::string errmsg(void) {
00105       return std::string(valid() ? "" : "Invalid stream");
00106     };
00108 
00109     /*----------------------------------------------------------*/
00111 
00112 
00113     virtual bool reopen(void) { return true; };
00114 
00116     virtual bool close(void) { return true; };
00118   }; //-- /mstream
00119 
00120 
00121   /*====================================================================
00122    * mistream
00123    *====================================================================*/
00125   class mistream : virtual public mstream {
00126   public:
00127     /*----------------------------------------------------------
00128      * mistream: constructors
00129      */
00131 
00132 
00133     mistream(void) {};
00134 
00136     virtual ~mistream(void) {};
00138 
00139     /*----------------------------------------------------------
00140      * mistream: input
00141      */
00143 
00144 
00146     virtual int getc(void) { return EOF; }
00147 
00150     virtual ByteCount read(char *buf, size_t n) {
00151       size_t nread = 0;
00152       int c;
00153       for (c = this->getc(); nread < n && c != EOF; nread++, c = this->getc()) {
00154         *buf++ = c;
00155       }
00156       return (ByteCount)nread;
00157     };
00158 
00164     virtual ByteCount getline(std::string &s, const std::string &delim="\n\r") {
00165       ByteCount nread = 0;
00166       int c;
00167       s.clear();
00168       for (c = this->getc(); c != EOF; nread++, c = this->getc()) {
00169         s.push_back((char)c);
00170         if (delim.find(c) != delim.npos) break;
00171       }
00172       return nread ? nread : EOF;
00173     };
00175   };
00176 
00177 
00178   /*====================================================================
00179    * mostream
00180    *====================================================================*/
00182   class mostream : virtual public mstream {
00183   public:
00184     /*----------------------------------------------------------
00185      * mostream: constructors
00186      */
00188 
00189 
00190     mostream(void) {};
00191 
00193     virtual ~mostream(void) {};
00195 
00196     /*----------------------------------------------------------
00197      * mostream: output
00198      */
00200 
00201 
00203     virtual bool flush(void) { return false; };
00204 
00206     virtual bool write(const char *buf, size_t n) { return false; };
00207 
00209     virtual bool putc(unsigned char c) { return false; };
00210 
00212     virtual bool puts(const char *s) { return false; };
00214     virtual bool puts(const std::string &s) { return false; };
00215 
00220     virtual bool vprintf(const char *fmt, va_list &ap) {
00221       char *obuf = NULL;
00222       size_t len = 0;
00223       len = vasprintf(&obuf, fmt, ap);
00224       bool rc = len >= 0 && write(obuf, len);
00225       if (obuf) free(obuf);
00226       return rc;
00227     };
00232     inline bool printf(const char *fmt, ...) {
00233       va_list ap;
00234       va_start(ap,fmt);
00235       bool rc = this->vprintf(fmt,ap);
00236       va_end(ap);
00237       return rc;
00238     };
00240   };
00241 
00242 }; //-- /namespace mootio
00243 
00244 
00245 #endif //_MOOT_IO_H

Generated on Mon Jun 27 13:05:25 2005 for libmoot by  doxygen 1.3.8-20040913