mootCxxIO.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: mootCxxIO.h
24  * Author: Bryan Jurish <moocow@cpan.org>
25  * Description:
26  * + moot PoS tagger : low-level I/O abstractions for C++ streams
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_CXXIO_H
39 #define _MOOT_CXXIO_H
40 
41 #include <iostream>
42 #include <fstream>
43 
44 #include <mootIO.h>
45 #include <mootArgs.h>
46 
47 namespace mootio {
48  using namespace std;
49 
50  /*====================================================================
51  * mcxxstream : std::(i|o)stream i/o
52  *====================================================================*/
53  template <class _StreamClass>
54  class mcxxstream : virtual public mstream {
55  public:
56  _StreamClass *streamp;
57  public:
58  /*----------------------------------------------------------
59  * mcxxstream: constructors
60  */
62 
63 
64  mcxxstream(_StreamClass *streamptr=NULL) : streamp(streamptr) {};
65 
67  mcxxstream(_StreamClass &stream) : streamp(&stream) {};
68 
70  virtual ~mcxxstream(void) {};
72 
73  /*----------------------------------------------------------
74  * mcxxstream: integrity
75  */
77 
78 
79  virtual bool valid(void) { return streamp && streamp->good(); };
80 
82  virtual bool eof(void) { return !streamp || streamp->eof(); };
84  }; //-- /mcxxstream
85 
86 
87  /*====================================================================
88  * micxxstream : std::istream input
89  *====================================================================*/
91  class micxxstream
92  : public mcxxstream<std::istream>,
93  public mistream
94  {
95  public:
96  /*----------------------------------------------------------
97  * micxxstream: constructors
98  */
100 
102  micxxstream(std::istream *streamptr=NULL)
103  : mcxxstream<std::istream>(streamptr)
104  {};
105 
107  micxxstream(std::istream &istream)
108  : mcxxstream<std::istream>(istream)
109  {};
110 
112  ~micxxstream(void) {};
114 
115  /*----------------------------------------------------------
116  * micxxstream: input
117  */
119 
120 
122  virtual ByteCount read(char *buf, size_t n) {
123  return streamp ? streamp->read(buf,n).gcount() : 0;
124  };
125 
127  virtual int getbyte(void) {
128  return streamp ? streamp->get() : EOF;
129  };
131  }; //-- /micxxstream
132 
133 
134  /*====================================================================
135  * mocxxstream : std::ostream output
136  *====================================================================*/
138  class mocxxstream
139  : public mcxxstream<std::ostream>,
140  public mostream
141  {
142  public:
143  char *_printf_buffer;
144  ByteCount _printf_buflen;
145 
146  public:
147  /*----------------------------------------------------------
148  * mocxxstream: output
149  */
151 
152 
153  mocxxstream(std::ostream *ostreamptr)
154  : mcxxstream<std::ostream>(ostreamptr),
155  _printf_buffer(NULL),
156  _printf_buflen(0)
157  {};
158 
160  mocxxstream(std::ostream &ostream)
161  : mcxxstream<std::ostream>(ostream),
162  _printf_buffer(NULL),
163  _printf_buflen(0)
164  {};
167  virtual ~mocxxstream(void)
168  {
169  if (_printf_buffer) free(_printf_buffer);
170  };
172 
173  /*----------------------------------------------------------
174  * mocxxstream: output
175  */
177 
178 
179  virtual bool mflush(void) { return streamp->flush().good(); };
180 
182  virtual bool write(const char *buf, size_t n) {
183  return streamp ? streamp->write(buf,n).good() : false;
184  };
185 
187  virtual bool putbyte(unsigned char c) {
188  return streamp ? streamp->put(c).good() : false;
189  };
190 
192  virtual bool puts(const char *s) {
193  return streamp ? ((*streamp) << s).good() : false;
194  };
196  virtual bool puts(const std::string &s) {
197  return streamp ? ((*streamp) << s).good() : false;
198  };
202  virtual bool vprintf(const char *fmt, va_list &ap) {
203  if (!streamp) return false;
204  va_list ap_tmp;
205  moot_va_copy(ap_tmp,ap);
206  ByteCount nchars = vsnprintf(_printf_buffer, _printf_buflen, fmt, ap);
207  if (nchars >= _printf_buflen) {
208  //-- oops: reallocate!
209  _printf_buffer = reinterpret_cast<char *>(realloc(_printf_buffer, nchars+1));
210  assert(_printf_buffer != NULL);
211  _printf_buflen = nchars+1;
212  //-- ... and try again
213  moot_va_copy(ap,ap_tmp);
214  nchars = vsnprintf(_printf_buffer, _printf_buflen, fmt, ap);
215  }
216  //-- we now have a full string buffer: write it
217  return streamp->write(_printf_buffer, nchars).good();
218  };
220  }; //-- /mocxxstream
221 
222 }; //-- /namespace mootio
223 
224 
225 #endif //_MOOT_CXXIO_H
Definition: mootCxxIO.h:51
Abstract base class for output stream wrappers.
Definition: mootIO.h:194
Definition: mootCxxIO.h:141
Abstract base class for I/O stream wrappers.
Definition: mootIO.h:80
Namespace for I/O stream wrappers.
Definition: mootBufferIO.h:45
utilities for functions taking variable number of arguments
generic I/O abstraction layer
virtual ByteCount read(char *buf, size_t n)
Definition: mootCxxIO.h:123
int ByteCount
typedef for byte counts (should be signed, for compatibility)
Definition: mootIO.h:52
Definition: mootCxxIO.h:90
Abstract base class for input stream wrappers.
Definition: mootIO.h:129
virtual bool eof(void)
Definition: mootCxxIO.h:81