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

mootZIO.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: mootZIO.h
00024  * Author: Bryan Jurish <moocow@ling.uni-potsdam.de>
00025  * Description:
00026  *   + moot PoS tagger : low-level I/O abstractions for libz gzFile
00027  *--------------------------------------------------------------------------*/
00028 
00029 #ifndef _MOOT_ZIO_H
00030 #define _MOOT_ZIO_H
00031 
00032 #include <mootIO.h>
00033 
00034 #ifdef MOOT_ZLIB_ENABLED
00035 
00036 #include <zlib.h>
00037 #define MOOT_DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
00038 
00039 namespace mootio {
00040 
00041   /*====================================================================
00042    * mcstream : FILE* i/o
00043    *====================================================================*/
00045   class mzstream
00046     : virtual public mistream,
00047       virtual public mostream
00048   {
00049   public:
00050     gzFile zfile;  
00051   public:
00052     /*----------------------------------------------------------
00053      * mzstream: constructors
00054      */
00056 
00057 
00058     mzstream(gzFile zf=NULL) : zfile(zf) {};
00059 
00061     ~mzstream(void) {};
00063 
00064     /*----------------------------------------------------------
00065      * mcstream: integrity
00066      */
00068 
00069 
00070     virtual bool valid(void) {
00071       if (!zfile) return false;
00072       int errnum;
00073       gzerror(zfile,&errnum);
00074       return errnum == Z_OK;
00075     };
00076 
00078     virtual bool eof(void) { return !zfile || gzeof(zfile); };
00079 
00081     virtual std::string errmsg(void) {
00082       int errnum = Z_ERRNO;
00083       const char *zerrmsg = zfile ? gzerror(zfile,&errnum) : "NULL gzFile";
00084       return std::string(errnum == Z_OK
00085                          ? ""
00086                          : (zerrmsg != NULL ? zerrmsg : "unspecified zlib error"));
00087     };
00089 
00090     /*----------------------------------------------------------
00091      * mcstream: open/close
00092      */
00094 
00095 
00096     virtual bool reopen(void) { return true; };
00097 
00099     virtual bool close(void) { zfile=NULL; return true; };
00101 
00102     /*----------------------------------------------------------
00103      * mcstream: input
00104      */
00106 
00107 
00109     virtual ByteCount read(char *buf, size_t n) {
00110       return zfile ? gzread(zfile, buf, n) : 0;
00111     };
00112 
00114     virtual int getc(void) {
00115       int c = zfile ? gzgetc(zfile) : -1;
00116       return c==-1 ? EOF : c;
00117     };
00119 
00120     /*----------------------------------------------------------
00121      * mzstream: output
00122      */
00124 
00125 
00126     virtual bool flush(void) {
00127       return zfile && gzflush(zfile, Z_SYNC_FLUSH) == Z_OK;
00128     };
00129 
00131     virtual bool write(const char *buf, size_t n) {
00132       return zfile ? (gzwrite(zfile,buf,n) == (int)n) : false;
00133     };
00134 
00136     virtual bool putc(unsigned char c) {
00137       return zfile ? (gzputc(zfile,c) != -1) : false;
00138     };
00139 
00141     virtual bool puts(const char *s) {
00142       return zfile ? (gzputs(zfile,s) >= 0) : false;
00143     };
00145     virtual bool puts(const std::string &s) {
00146       return (zfile
00147               ? (gzwrite(zfile,s.data(),s.size()) == (int)s.size())
00148               : false);
00149     };
00150 
00152     /*
00153     virtual bool vprintf(const char *fmt, va_list &ap) {
00154       return zfile ? (vfprintf(file,fmt,ap) >= 0) : false;
00155     };
00156     */
00158   };
00159 
00160 
00161   /*====================================================================
00162    * mzfstream : named file i/o
00163    *====================================================================*/
00169   class mzfstream : virtual public mzstream {
00170   public:
00171     std::string mode;          
00172     std::string default_mode;  
00173   public:
00174     /*------------------------------------------------------------------
00175      * mfstream: Constructors
00176      */
00178 
00179 
00180     mzfstream(void)
00181       : mode("rb"),
00182         default_mode("rb")
00183     {};
00184 
00186     mzfstream(const char *filename,
00187               const char *open_mode=NULL)
00188       : default_mode("rb")
00189     {
00190       open(filename,open_mode);
00191     };
00192 
00194     virtual ~mzfstream(void) { close(); };
00196 
00197     /*------------------------------------------------------------------
00198      * mzfstream: open()
00199      */
00201 
00202 
00204     inline bool open(const char *filename, const char *open_mode=NULL) {
00205       name = filename;
00206       if (open_mode) mode = open_mode;
00207       return reopen();
00208     };
00211     inline bool open(const std::string &filename, const std::string &open_mode="") {
00212       mode = open_mode;
00213       name = filename;
00214       return reopen();
00215     };
00216 
00218     virtual bool reopen(void) {
00219       close();
00220       if (mode.empty()) mode = default_mode;
00221       //-- check for standard stream aliases
00222       if (name == "-") {
00223         if (mode.find('w') != mode.npos) zfile = gzdopen(fileno(stdout), mode.c_str());
00224         else zfile = gzdopen(fileno(stdin), mode.c_str());
00225       } else {
00226         zfile = gzopen(name.c_str(), mode.c_str());
00227       }
00228       return valid();
00229     };
00230 
00232     virtual bool close(void) {
00233       if (!zfile) return true;
00234       bool rc = gzclose(zfile) == Z_OK;
00235       zfile = NULL;
00236       return rc;
00237     };
00239 
00240     /*------------------------------------------------------------------
00241      * mzfstream: Utilties
00242      */
00244 
00245 
00246     inline void setparams(int level=Z_DEFAULT_COMPRESSION,
00247                           int strategy=Z_DEFAULT_STRATEGY)
00248     {
00249       if (level != Z_DEFAULT_COMPRESSION
00250           && (level > Z_BEST_COMPRESSION || level < Z_NO_COMPRESSION))
00251         {
00252           level = Z_DEFAULT_COMPRESSION;
00253         }
00254       if (zfile) gzsetparams(zfile, level, strategy);
00255     };
00257 
00258   }; //-- /mzfstream
00259 
00260   /*====================================================================
00261    * mizfstream : named file input
00262    *====================================================================*/
00268   class mizfstream : virtual public mzfstream {
00269   public:
00270     /*------------------------------------------------------------------
00271      * mfstream: Constructors
00272      */
00274 
00275 
00276     mizfstream(void) {
00277       default_mode = "rb";
00278     };
00279 
00281     mizfstream(const char *filename, const char *mode=NULL) 
00282     {
00283       default_mode = "rb";
00284       open(filename,mode);
00285     };
00286 
00288     mizfstream(const std::string &filename, const std::string &mode="") 
00289     {
00290       default_mode = "rb";
00291       open(filename,mode);
00292     };
00293 
00295     virtual ~mizfstream(void) { close(); };
00297   }; //-- /mizfstream
00298 
00299   /*====================================================================
00300    * mozfstream : named file output
00301    *====================================================================*/
00307   class mozfstream : virtual public mzfstream {
00308   public:
00309     /*------------------------------------------------------------------
00310      * mozfstream: Constructors
00311      */
00313 
00314 
00315     mozfstream(void) {
00316       default_mode = "wb";
00317     };
00318 
00320     mozfstream(const char *filename, const char *mode=NULL)  {
00321       default_mode = "wb";
00322       open(filename,mode);
00323     };
00324 
00326     mozfstream(const std::string &filename, const std::string &mode="") {
00327       default_mode = "wb";
00328       open(filename,mode);
00329     };
00330 
00332     virtual ~mozfstream(void) { close(); };
00334   }; //-- /mofstream
00335 
00336 }; //-- /namespace mootio
00337 
00338 #else  // _MOOT_ZLIB_ENABLED
00339 
00340 #include <mootCIO.h>
00341 
00342 #define MOOT_DEFAULT_COMPRESSION -1
00343 
00344 namespace mootio {
00345 
00346   typedef mcstream   mzstream;
00347   typedef mfstream   mzfstream;
00348   typedef mifstream  mizfstream;
00349   typedef mofstream  mozfstream;
00350 
00351 }; //-- /namespace mootio
00352 
00353 #endif // _MOOT_ZLIB_ENABLED
00354 
00355 namespace mootio {
00356 
00357   /*====================================================================
00358    * mootio : common typedef aliases
00359    *====================================================================*/
00360   typedef mzstream mizstream; 
00361   typedef mzstream mozstream; 
00362 
00363 }; //-- /namespace mootio
00364 
00365 #endif //_MOOT_ZIO_H

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