mootIO.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) 2003-2014 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: mootIO.h
24  * Author: Bryan Jurish <moocow@cpan.org>
25  * Description:
26  * + moot PoS tagger : low-level I/O abstractions
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 
38 #ifndef _MOOT_IO_H
39 #define _MOOT_IO_H
40 
41 #include <mootTypes.h>
42 
43 #include <stdio.h>
44 #include <stdarg.h>
45 #include <string.h>
46 #include <errno.h>
47 #include <assert.h>
48 
50 namespace mootio {
51 
52  /*====================================================================
53  * mootio: types
54  *====================================================================*/
56  typedef int ByteCount;
57 
59  typedef moot::OffsetT ByteOffset;
60 
61  /*====================================================================
62  * mstream: base class
63  *====================================================================*/
84  class mstream {
85  public:
86  std::string name;
87 
88  public:
89  /*----------------------------------------------------------*/
91 
92 
93  mstream(const std::string &myname="") : name(myname) {};
94 
96  virtual ~mstream(void) {};
98 
99  /*----------------------------------------------------------*/
101 
103  virtual bool valid(void) { return false; };
104 
106  inline operator bool(void) { return valid(); };
107 
109  virtual bool eof(void) { return true; };
110 
112  virtual std::string errmsg(void) {
113  return std::string(valid() ? "" : "Invalid stream");
114  };
116 
117  /*----------------------------------------------------------*/
119 
121  virtual bool reopen(void) { return true; };
122 
124  virtual bool close(void) { return true; };
126  }; //-- /mstream
127 
128 
129  /*====================================================================
130  * mistream
131  *====================================================================*/
133  class mistream : virtual public mstream {
134  public:
135  /*----------------------------------------------------------
136  * mistream: constructors
137  */
139 
141  mistream(void) : mstream() {};
142 
144  virtual ~mistream(void) {};
146 
147  /*----------------------------------------------------------
148  * mistream: input
149  */
151 
152 
154  virtual int getbyte(void) { return EOF; }
155 
158  virtual ByteCount read(char *buf, size_t n) {
159  size_t nread = 0;
160  int c;
161  for (c = this->getbyte(); nread < n && c != EOF; nread++, c = this->getbyte()) {
162  *buf++ = c;
163  }
164  return static_cast<ByteCount>(nread);
165  };
166 
172  virtual ByteCount getline(std::string &s, const std::string &delim="\n\r") {
173  ByteCount nread = 0;
174  int c;
175  s.clear();
176  for (c = this->getbyte(); c != EOF; nread++, c = this->getbyte()) {
177  s.push_back(static_cast<char>(c));
178  if (delim.find(c) != delim.npos) break;
179  }
180  return nread ? nread : EOF;
181  };
183  };
184 
185 
186  /*====================================================================
187  * mostream
188  *====================================================================*/
190  class mostream : virtual public mstream {
191  public:
192  /*----------------------------------------------------------
193  * mostream: constructors
194  */
196 
197 
198  mostream(void) : mstream() {};
199 
201  virtual ~mostream(void) {};
203 
204  /*----------------------------------------------------------
205  * mostream: output
206  */
208 
209 
211  virtual bool flush(void) { return false; };
212 
214  virtual bool write(const char *buf, size_t n) { return false; };
220  virtual bool putbyte(unsigned char c) { return false; };
221 
223  virtual bool puts(const char *s) { return false; };
226  virtual bool puts(const std::string &s) { return false; };
232  virtual bool vprintf(const char *fmt, va_list &ap);
233 
238  inline bool printf(const char *fmt, ...) {
239  va_list ap;
240  va_start(ap,fmt);
241  bool rc = this->vprintf(fmt,ap);
242  va_end(ap);
243  return rc;
244  };
246  };
247 
248 }; //-- /namespace mootio
249 
250 
251 #endif //_MOOT_IO_H
virtual bool eof(void)
Definition: mootIO.h:105
virtual bool close(void)
Definition: mootIO.h:120
Abstract base class for output stream wrappers.
Definition: mootIO.h:194
Abstract base class for I/O stream wrappers.
Definition: mootIO.h:80
std::string name
symbolic name of this stream
Definition: mootIO.h:82
mstream(const std::string &myname="")
Definition: mootIO.h:89
moot::OffsetT ByteOffset
typedef for (byte) offsets (may be unsigned)
Definition: mootIO.h:55
Namespace for I/O stream wrappers.
Definition: mootBufferIO.h:45
virtual bool valid(void)
Definition: mootIO.h:99
virtual ByteCount getline(std::string &s, const std::string &delim="\")
Definition: mootIO.h:172
virtual bool reopen(void)
Definition: mootIO.h:117
Common typedefs and constants.
virtual std::string errmsg(void)
Definition: mootIO.h:108
int ByteCount
typedef for byte counts (should be signed, for compatibility)
Definition: mootIO.h:52
long unsigned int OffsetT
Definition: mootTypes.h:70
virtual ~mstream(void)
Definition: mootIO.h:92
Abstract base class for input stream wrappers.
Definition: mootIO.h:129