mootZIO.h
Go to the documentation of this file.
1 /* -*- Mode: C++ -*- */
2 
3 /*
4  libmoot : moocow's part-of-speech tagging library
5  Copyright (C) 2004-2010 by Bryan Jurish <moocow@cpan.org>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 3 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 
22 /*--------------------------------------------------------------------------
23  * File: mootZIO.h
24  * Author: Bryan Jurish <moocow@cpan.org>
25  * Description:
26  * + moot PoS tagger : low-level I/O abstractions for libz gzFile
27  *--------------------------------------------------------------------------*/
28 
33 #ifndef _MOOT_ZIO_H
34 #define _MOOT_ZIO_H
35 
36 #include <mootIO.h>
37 
38 #ifdef MOOT_ZLIB_ENABLED
39 
40 #include <zlib.h>
41 #define MOOT_DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
42 
43 namespace mootio {
44 
45  /*====================================================================
46  * mzstream : gzFile* i/o
47  *====================================================================*/
49  class mzstream
50  : virtual public mistream,
51  virtual public mostream
52  {
53  public:
54  gzFile zfile;
55  public:
56  /*----------------------------------------------------------
57  * mzstream: constructors
58  */
60 
61 
62  mzstream(gzFile zf=NULL) : zfile(zf) {};
63 
65  ~mzstream(void) {};
67 
68  /*----------------------------------------------------------
69  * mcstream: integrity
70  */
72 
73 
74  virtual bool valid(void) {
75  if (!zfile) return false;
76  int errnum;
77  gzerror(zfile,&errnum);
78  return errnum == Z_OK;
79  };
80 
82  virtual bool eof(void) { return !zfile || gzeof(zfile); };
83 
85  virtual std::string errmsg(void) {
86  int errnum = Z_ERRNO;
87  const char *zerrmsg = zfile ? gzerror(zfile,&errnum) : "NULL gzFile";
88  return std::string(errnum == Z_OK
89  ? ""
90  : (zerrmsg != NULL ? zerrmsg : "unspecified zlib error"));
91  };
93 
94  /*----------------------------------------------------------
95  * mcstream: open/close
96  */
98 
99 
100  virtual bool reopen(void) { return true; };
101 
103  virtual bool close(void) { zfile=NULL; return true; };
105 
106  /*----------------------------------------------------------
107  * mcstream: input
108  */
111 
113  virtual ByteCount read(char *buf, size_t n) {
114  return zfile ? gzread(zfile, buf, n) : 0;
115  };
116 
118  virtual int getbyte(void) {
119  int c = zfile ? gzgetc(zfile) : -1;
120  return c==-1 ? EOF : c;
121  };
123 
124  /*----------------------------------------------------------
125  * mzstream: output
126  */
128 
129 
130  virtual bool flush(void) {
131  return zfile && gzflush(zfile, Z_SYNC_FLUSH) == Z_OK;
132  };
133 
135  virtual bool write(const char *buf, size_t n) {
136  return zfile ? (gzwrite(zfile,buf,n) == static_cast<int>(n)) : false;
137  };
138 
140  virtual bool putbyte(unsigned char c) {
141  return zfile ? (gzputc(zfile,c) != -1) : false;
142  };
143 
145  virtual bool puts(const char *s) {
146  return zfile ? (gzputs(zfile,s) >= 0) : false;
147  };
149  virtual bool puts(const std::string &s) {
150  return (zfile
151  ? (gzwrite(zfile,s.data(),s.size()) == static_cast<int>(s.size()))
152  : false);
153  };
154 
156  /*
157  virtual bool vprintf(const char *fmt, va_list &ap) {
158  return zfile ? (vfprintf(file,fmt,ap) >= 0) : false;
159  };
160  */
162  };
163 
164 
165  /*====================================================================
166  * mzfstream : named file i/o
167  *====================================================================*/
173  class mzfstream : virtual public mzstream {
174  public:
175  std::string mode;
176  std::string default_mode;
177  public:
178  /*------------------------------------------------------------------
179  * mfstream: Constructors
180  */
182 
183 
184  mzfstream(void)
185  : mode("rb"),
186  default_mode("rb")
187  {};
188 
190  mzfstream(const char *filename,
191  const char *open_mode=NULL)
192  : default_mode("rb")
193  {
194  open(filename,open_mode);
195  };
196 
198  virtual ~mzfstream(void) { close(); };
200 
201  /*------------------------------------------------------------------
202  * mzfstream: open()
203  */
208  inline bool open(const char *filename, const char *open_mode=NULL) {
209  name = filename;
210  if (open_mode) mode = open_mode;
211  return reopen();
212  };
215  inline bool open(const std::string &filename, const std::string &open_mode="") {
216  mode = open_mode;
217  name = filename;
218  return reopen();
219  };
222  virtual bool reopen(void) {
223  close();
224  if (mode.empty()) mode = default_mode;
225  //-- check for standard stream aliases
226  if (name == "-") {
227  if (mode.find('w') != mode.npos) zfile = gzdopen(fileno(stdout), mode.c_str());
228  else zfile = gzdopen(fileno(stdin), mode.c_str());
229  } else {
230  zfile = gzopen(name.c_str(), mode.c_str());
231  }
232  return valid();
233  };
234 
236  virtual bool close(void) {
237  if (!zfile) return true;
238  bool rc = gzclose(zfile) == Z_OK;
239  zfile = NULL;
240  return rc;
241  };
243 
244  /*------------------------------------------------------------------
245  * mzfstream: Utilties
246  */
248 
250  inline void setparams(int level=Z_DEFAULT_COMPRESSION,
251  int strategy=Z_DEFAULT_STRATEGY)
252  {
253  if (level != Z_DEFAULT_COMPRESSION
254  && (level > Z_BEST_COMPRESSION || level < Z_NO_COMPRESSION))
255  {
256  level = Z_DEFAULT_COMPRESSION;
257  }
258  if (zfile) gzsetparams(zfile, level, strategy);
259  };
261 
262  }; //-- /mzfstream
263 
264  /*====================================================================
265  * mizfstream : named file input
266  *====================================================================*/
272  class mizfstream : virtual public mzfstream {
273  public:
274  /*------------------------------------------------------------------
275  * mfstream: Constructors
276  */
278 
279 
280  mizfstream(void) {
281  default_mode = "rb";
282  };
283 
285  mizfstream(const char *filename, const char *mode=NULL)
286  {
287  default_mode = "rb";
288  open(filename,mode);
289  };
290 
292  mizfstream(const std::string &filename, const std::string &mode="")
293  {
294  default_mode = "rb";
295  open(filename,mode);
296  };
297 
299  virtual ~mizfstream(void) { close(); };
301  }; //-- /mizfstream
302 
303  /*====================================================================
304  * mozfstream : named file output
305  *====================================================================*/
311  class mozfstream : virtual public mzfstream {
312  public:
313  /*------------------------------------------------------------------
314  * mozfstream: Constructors
315  */
317 
318 
319  mozfstream(void) {
320  default_mode = "wb";
321  };
322 
324  mozfstream(const char *filename, const char *mode=NULL) {
325  default_mode = "wb";
326  open(filename,mode);
327  };
328 
330  mozfstream(const std::string &filename, const std::string &mode="") {
331  default_mode = "wb";
332  open(filename,mode);
333  };
336  virtual ~mozfstream(void) { close(); };
338  }; //-- /mofstream
340 }; //-- /namespace mootio
341 
342 #else // _MOOT_ZLIB_ENABLED
343 
344 #include <mootCIO.h>
345 
346 #define MOOT_DEFAULT_COMPRESSION -1
347 
348 namespace mootio {
349 
350  typedef mcstream mzstream;
351  typedef mfstream mzfstream;
352  typedef mifstream mizfstream;
353  typedef mofstream mozfstream;
354 
355 }; //-- /namespace mootio
356 
357 #endif // _MOOT_ZLIB_ENABLED
358 
359 namespace mootio {
360 
361  /*====================================================================
362  * mootio : common typedef aliases
363  *====================================================================*/
364  typedef mzstream mizstream;
365  typedef mzstream mozstream;
366 
367 }; //-- /namespace mootio
368 
369 #endif //_MOOT_ZIO_H
mzstream mizstream
Alias for gzFile input stream wrappers.
Definition: mootZIO.h:404
virtual bool flush(void)
Definition: mootZIO.h:156
virtual bool reopen(void)
Definition: mootZIO.h:110
virtual bool putbyte(unsigned char c)
Definition: mootZIO.h:170
mootio abstraction layer for C FILE*s
virtual int getbyte(void)
Definition: mootZIO.h:130
~mzstream(void)
Definition: mootZIO.h:67
Wrapper class for named file i/o using C FILE*s.
Definition: mootCIO.h:175
gzFile zfile
underlying gzFile
Definition: mootZIO.h:54
virtual bool puts(const char *s)
Definition: mootZIO.h:175
Wrapper class for named file input using C FILE*s.
Definition: mootCIO.h:286
Abstract base class for output stream wrappers.
Definition: mootIO.h:194
virtual ByteCount read(char *buf, size_t n)
Definition: mootZIO.h:125
mzstream mozstream
Alias for gzFile output stream wrappers.
Definition: mootZIO.h:405
std::string name
symbolic name of this stream
Definition: mootIO.h:82
Wrapper class for named file output using gzFile.
Definition: mootZIO.h:367
Wrapper class for named file input using gzFile.
Definition: mootZIO.h:326
Wrapper class for named file output using C FILE*s.
Definition: mootCIO.h:327
virtual bool eof(void)
Definition: mootZIO.h:86
Namespace for I/O stream wrappers.
Definition: mootBufferIO.h:45
virtual bool close(void)
Definition: mootZIO.h:113
generic I/O abstraction layer
virtual bool write(const char *buf, size_t n)
Definition: mootZIO.h:165
Wrapper class for C FILE* streams.
Definition: mootCIO.h:54
Wrapper class for C FILE* streams.
Definition: mootZIO.h:49
int ByteCount
typedef for byte counts (should be signed, for compatibility)
Definition: mootIO.h:52
virtual bool valid(void)
Definition: mootZIO.h:76
virtual std::string errmsg(void)
Definition: mootZIO.h:89
Wrapper class for named file i/o using gzFile.
Definition: mootZIO.h:203
Abstract base class for input stream wrappers.
Definition: mootIO.h:129
mzstream(gzFile zf=__null)
Definition: mootZIO.h:62