mootCIO.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: mootCIO.h
24  * Author: Bryan Jurish <moocow@cpan.org>
25  * Description:
26  * + moot PoS tagger : low-level I/O abstractions for C FILE *s
27  * + these are ugly, but there appears to be no better
28  * (read "faster in the general case") way to get
29  * C FILE*s to jive with C++ streams, and I REALLY
30  * like printf() and friends...
31  *--------------------------------------------------------------------------*/
32 
42 #ifndef _MOOT_CIO_H
43 #define _MOOT_CIO_H
44 
45 #include <mootIO.h>
46 /*#include <stdio.h>*/ //-- included by mootIO.h
47 
48 namespace mootio {
49 
50  /*====================================================================
51  * mcstream : FILE* i/o
52  *====================================================================*/
54  class mcstream
55  : public mistream,
56  public mostream
57  {
58  public:
59  FILE *file;
60  public:
61  /*----------------------------------------------------------
62  * mcstream: constructors
63  */
65 
66 
67  mcstream(FILE *f=NULL) : file(f) {};
68 
70  ~mcstream(void) {};
72 
73  /*----------------------------------------------------------
74  * mcstream: integrity
75  */
77 
78 
79  virtual bool valid(void) { return file && !ferror(file); };
80 
82  virtual bool eof(void) { return !file || feof(file); };
83 
85  virtual std::string errmsg(void) {
86  return std::string(file && !ferror(file) ? "" : strerror(errno));
87  };
89 
90  /*----------------------------------------------------------
91  * mcstream: open/close
92  */
94 
95 
96  virtual bool reopen(void) { return true; };
97 
99  virtual bool close(void) { file=NULL; return true; };
101 
102  /*----------------------------------------------------------
103  * mcstream: input
104  */
106 
107 
109  virtual ByteCount read(char *buf, size_t n) {
110  return file ? fread(buf, 1, n, file) : 0;
111  };
112 
114  virtual int getbyte(void) {
115  return file ? fgetc(file) : EOF;
116  };
118 
119  /*----------------------------------------------------------
120  * mcstream: output
121  */
123 
124 
125  virtual bool flush(void) { return file && fflush(file) != EOF; };
126 
128  virtual bool write(const char *buf, size_t n) {
129  return file ? (fwrite(buf,1,n,file) == n) : false;
130  };
131 
133  virtual bool putbyte(unsigned char c) {
134  return file ? (fputc(c,file) != EOF) : false;
135  };
136 
138  virtual bool puts(const char *s) {
139  return file ? (fputs(s,file) >= 0) : false;
140  };
142  virtual bool puts(const std::string &s) {
143  return file ? (fwrite(s.data(),1,s.size(),file) == s.size()) : false;
144  };
145 
148  virtual bool vprintf(const char *fmt, va_list &ap) {
149  return file ? (vfprintf(file,fmt,ap) >= 0) : false;
150  };
152  };
153 
155  /*====================================================================
156  * mfstream : named file i/o
157  *====================================================================*/
163  class mfstream : public mcstream {
164  public:
165  std::string mode;
166  std::string default_mode;
167  public:
168  /*------------------------------------------------------------------
169  * mfstream: Constructors
170  */
172 
173 
174  mfstream(void)
175  : mode("r"),
176  default_mode("r")
177  {};
180  mfstream(const char *filename, const char *open_mode=NULL)
181  : default_mode("r")
182  {
183  open(filename,open_mode);
184  };
185 
187  virtual ~mfstream(void) { close(); };
189 
190  /*------------------------------------------------------------------
191  * mfstream: open()
192  */
194 
195 
197  inline bool open(const char *filename, const char *open_mode=NULL) {
198  name = filename;
199  if (open_mode) mode = open_mode;
200  return reopen();
201  };
204  inline bool open(const std::string &filename, const std::string &open_mode="") {
205  mode = open_mode;
206  name = filename;
207  return reopen();
208  };
209 
211  virtual bool reopen(void) {
212  close();
213  if (mode.empty()) mode = default_mode;
214  //-- check for standard stream aliases
215  if (name == "-") {
216  if (mode.find('w') != mode.npos) file = stdout;
217  else file = stdin;
218  } else {
219  file = fopen(name.c_str(), mode.c_str());
220  }
221  return file && !ferror(file);
222  };
223 
225  virtual bool close(void) {
226  if (!file) return true;
227  else if (file==stdin || file==stdout || file==stderr) {
228  //-- don't close standard streams
229  file = NULL;
230  return true;
231  }
232  bool rc = fclose(file) == 0;
233  file = NULL;
234  return rc;
235  };
237 
238  /*------------------------------------------------------------------
239  * mfstream: params
240  */
242 
243 
244  inline void setparams(int level=0, int strategy=0) {};
246  }; //-- /mfstream
247 
248  /*====================================================================
249  * mifstream : named file input
250  *====================================================================*/
256  class mifstream : public mfstream {
257  public:
258  /*------------------------------------------------------------------
259  * mifstream: Constructors
260  */
262 
263 
264  mifstream(void) {
265  default_mode = "r";
266  };
267 
269  mifstream(const char *filename, const char *mode=NULL)
270  {
271  default_mode = "r";
272  open(filename,mode);
273  };
276  mifstream(const std::string &filename, const std::string &mode="")
277  {
278  default_mode = "r";
279  open(filename,mode);
280  };
281 
283  virtual ~mifstream(void) { close(); };
285  }; //-- /mifstream
287  /*====================================================================
288  * mofstream : named file output
289  *====================================================================*/
295  class mofstream : public mfstream {
296  public:
297  /*------------------------------------------------------------------
298  * mofstream: Constructors
299  */
301 
302 
303  mofstream(void) {
304  default_mode = "w";
305  };
306 
308  mofstream(const char *filename, const char *mode=NULL) {
309  default_mode = "w";
310  open(filename,mode);
311  };
312 
314  mofstream(const std::string &filename, const std::string &mode="") {
315  default_mode = "w";
316  open(filename,mode);
317  };
318 
320  virtual ~mofstream(void) { close(); };
322  }; //-- /mofstream
323 
324 
325  /*====================================================================
326  * mootio : typedef aliases
327  *====================================================================*/
328  typedef mcstream micstream;
329  typedef mcstream mocstream;
330 
331 }; //-- /namespace mootio
332 
333 
334 #endif //_MOOT_CIO_H
virtual bool close(void)
Definition: mootCIO.h:103
virtual bool write(const char *buf, size_t n)
Definition: mootCIO.h:138
virtual bool flush(void)
Definition: mootCIO.h:133
FILE * file
underlying FILE*
Definition: mootCIO.h:59
virtual int getbyte(void)
Definition: mootCIO.h:120
virtual bool eof(void)
Definition: mootCIO.h:84
Wrapper class for named file i/o using C FILE*s.
Definition: mootCIO.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
std::string name
symbolic name of this stream
Definition: mootIO.h:82
mcstream mocstream
Alias for FILE* output stream wrappers.
Definition: mootCIO.h:363
~mcstream(void)
Definition: mootCIO.h:72
virtual bool vprintf(const char *fmt, va_list &ap)
Definition: mootCIO.h:160
virtual bool reopen(void)
Definition: mootCIO.h:100
Wrapper class for named file output using C FILE*s.
Definition: mootCIO.h:327
Namespace for I/O stream wrappers.
Definition: mootBufferIO.h:45
generic I/O abstraction layer
mcstream(FILE *f=__null)
Definition: mootCIO.h:67
virtual bool putbyte(unsigned char c)
Definition: mootCIO.h:143
Wrapper class for C FILE* streams.
Definition: mootCIO.h:54
virtual ByteCount read(char *buf, size_t n)
Definition: mootCIO.h:115
virtual std::string errmsg(void)
Definition: mootCIO.h:87
int ByteCount
typedef for byte counts (should be signed, for compatibility)
Definition: mootIO.h:52
mcstream micstream
Alias for FILE* input stream wrappers.
Definition: mootCIO.h:362
virtual bool puts(const char *s)
Definition: mootCIO.h:150
virtual bool valid(void)
Definition: mootCIO.h:81
Abstract base class for input stream wrappers.
Definition: mootIO.h:129