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

mootCIO.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: mootCIO.h
00024  * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
00025  * Description:
00026  *   + moot PoS tagger : low-level I/O abstractions for C FILE *s
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_CIO_H
00034 #define _MOOT_CIO_H
00035 
00036 #include <mootIO.h>
00037 #include <stdio.h>
00038 
00039 namespace mootio {
00040 
00041   /*====================================================================
00042    * mcstream : FILE* i/o
00043    *====================================================================*/
00045   class mcstream
00046     : public mistream,
00047       public mostream
00048   {
00049   public:
00050     FILE *file;  
00051   public:
00052     /*----------------------------------------------------------
00053      * mcstream: constructors
00054      */
00056 
00057 
00058     mcstream(FILE *f=NULL) : file(f) {};
00059 
00061     ~mcstream(void) {};
00063 
00064     /*----------------------------------------------------------
00065      * mcstream: integrity
00066      */
00068 
00069 
00070     virtual bool valid(void) { return file && !ferror(file); };
00071 
00073     virtual bool eof(void) { return !file || feof(file); };
00074 
00076     virtual std::string errmsg(void) {
00077       return std::string(file && !ferror(file) ? "" : strerror(errno));
00078     };
00080 
00081     /*----------------------------------------------------------
00082      * mcstream: open/close
00083      */
00085 
00086 
00087     virtual bool reopen(void) { return true; };
00088 
00090     virtual bool close(void) { file=NULL; return true; };
00092 
00093     /*----------------------------------------------------------
00094      * mcstream: input
00095      */
00097 
00098 
00100     virtual ByteCount read(char *buf, size_t n) {
00101       return file ? fread(buf, 1, n, file) : 0;
00102     };
00103 
00105     virtual int getc(void) {
00106       return file ? fgetc(file) : EOF;
00107     };
00109 
00110     /*----------------------------------------------------------
00111      * mcstream: output
00112      */
00114 
00115 
00116     virtual bool flush(void) { return file && fflush(file) != EOF; };
00117 
00119     virtual bool write(const char *buf, size_t n) {
00120       return file ? (fwrite(buf,1,n,file) == n) : false;
00121     };
00122 
00124     virtual bool putc(unsigned char c) {
00125       return file ? (fputc(c,file) != EOF) : false;
00126     };
00127 
00129     virtual bool puts(const char *s) {
00130       return file ? (fputs(s,file) >= 0) : false;
00131     };
00133     virtual bool puts(const std::string &s) {
00134       return file ? (fwrite(s.data(),1,s.size(),file) == s.size()) : false;
00135     };
00136 
00139     virtual bool vprintf(const char *fmt, va_list &ap) {
00140       return file ? (vfprintf(file,fmt,ap) >= 0) : false;
00141     };
00143   };
00144 
00145 
00146   /*====================================================================
00147    * mfstream : named file i/o
00148    *====================================================================*/
00154   class mfstream : public mcstream {
00155   public:
00156     std::string mode;          
00157     std::string default_mode;  
00158   public:
00159     /*------------------------------------------------------------------
00160      * mfstream: Constructors
00161      */
00163 
00164 
00165     mfstream(void)
00166       : mode("r"),
00167         default_mode("r")
00168     {};
00169 
00171     mfstream(const char *filename, const char *open_mode=NULL)
00172       : default_mode("r")
00173     {
00174       open(filename,open_mode);
00175     };
00176 
00178     virtual ~mfstream(void) { close(); };
00180 
00181     /*------------------------------------------------------------------
00182      * mfstream: open()
00183      */
00185 
00186 
00188     inline bool open(const char *filename, const char *open_mode=NULL) {
00189       name = filename;
00190       if (open_mode) mode = open_mode;
00191       return reopen();
00192     };
00195     inline bool open(const std::string &filename, const std::string &open_mode="") {
00196       mode = open_mode;
00197       name = filename;
00198       return reopen();
00199     };
00200 
00202     virtual bool reopen(void) {
00203       close();
00204       if (mode.empty()) mode = default_mode;
00205       //-- check for standard stream aliases
00206       if (name == "-") {
00207         if (mode.find('w') != mode.npos) file = stdout;
00208         else file = stdin;
00209       } else {
00210         file = fopen(name.c_str(), mode.c_str());
00211       }
00212       return file && !ferror(file);
00213     };
00214 
00216     virtual bool close(void) {
00217       if (!file) return true;
00218       else if (file==stdin || file==stdout || file==stderr) {
00219         //-- don't close standard streams
00220         file = NULL;
00221         return true;
00222       }
00223       bool rc = fclose(file) == 0;
00224       file = NULL;
00225       return rc;
00226     };
00228 
00229     /*------------------------------------------------------------------
00230      * mfstream: params
00231      */
00233 
00234 
00235     inline void setparams(int level=0, int strategy=0) {};
00237   }; //-- /mfstream
00238 
00239   /*====================================================================
00240    * mifstream : named file input
00241    *====================================================================*/
00247   class mifstream : public mfstream {
00248   public:
00249     /*------------------------------------------------------------------
00250      * mifstream: Constructors
00251      */
00253 
00254 
00255     mifstream(void) {
00256       default_mode = "r";
00257     };
00258 
00260     mifstream(const char *filename, const char *mode=NULL) 
00261     {
00262       default_mode = "r";
00263       open(filename,mode);
00264     };
00265 
00267     mifstream(const std::string &filename, const std::string &mode="") 
00268     {
00269       default_mode = "r";
00270       open(filename,mode);
00271     };
00272 
00274     virtual ~mifstream(void) { close(); };
00276   }; //-- /mifstream
00277 
00278   /*====================================================================
00279    * mofstream : named file output
00280    *====================================================================*/
00286   class mofstream : public mfstream {
00287   public:
00288     /*------------------------------------------------------------------
00289      * mofstream: Constructors
00290      */
00292 
00293 
00294     mofstream(void) {
00295       default_mode = "w";
00296     };
00297 
00299     mofstream(const char *filename, const char *mode=NULL)  {
00300       default_mode = "w";
00301       open(filename,mode);
00302     };
00303 
00305     mofstream(const std::string &filename, const std::string &mode="") {
00306       default_mode = "w";
00307       open(filename,mode);
00308     };
00309 
00311     virtual ~mofstream(void) { close(); };
00313   }; //-- /mofstream
00314 
00315 
00316   /*====================================================================
00317    * mootio : typedef aliases
00318    *====================================================================*/
00319   typedef mcstream micstream; 
00320   typedef mcstream mocstream; 
00321 
00322 }; //-- /namespace mootio
00323 
00324 
00325 #endif //_MOOT_CIO_H

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