UniSet  2.6.0
DebugExtBuf.h
1 #ifndef DEBUGEXTBUF_H
2 #define DEBUGEXTBUF_H
3 
4 // Created by Lars Gullik Bj�nnes
5 // Copyright 1999 Lars Gullik Bj�nnes (larsbj@lyx.org)
6 // Released into the public domain.
7 
8 // Primarily developed for use in the LyX Project http://www.lyx.org/
9 // but should be adaptable to any project.
10 
11 // (c) 2002 adapted for UniSet by Lav, GNU LGPL license
12 
13 //#define TEST_DEBUGSTREAM
14 
15 
16 #ifdef __GNUG__
17 #pragma implementation
18 #endif
19 
20 //#include "DebugStream.h"
21 #include "Debug.h"
22 #include "Mutex.h"
23 
24 //�Since the current C++ lib in egcs does not have a standard implementation
25 // of basic_streambuf and basic_filebuf we don't have to include this
26 // header.
27 //#define MODERN_STL_STREAMS
28 #ifdef MODERN_STL_STREAMS
29 #include <fstream>
30 #endif
31 #include <iostream>
32 #include <sstream>
33 #include <iomanip>
34 #include <time.h>
35 #include <iomanip>
36 
37 using std::ostream;
38 using std::streambuf;
39 using std::streamsize;
40 using std::filebuf;
41 using std::stringbuf;
42 using std::cerr;
43 using std::ios;
44 
45 /*
46 ostream & operator<<(ostream & o, Debug::type t)
47 {
48  return o << int(t);
49 }
50 */
51 
56 class nullbuf : public streambuf
57 {
58  protected:
59 #ifndef MODERN_STL_STREAMS
60  typedef char char_type;
61  typedef int int_type;
63  virtual int sync()
64  {
65  return 0;
66  }
67 #endif
68 
69  virtual streamsize xsputn(char_type const*, streamsize n)
70  {
71  // fakes a purge of the buffer by returning n
72  return n;
73  }
74 #ifdef MODERN_STL_STREAMS
75 
76  virtual int_type overflow(int_type c = traits_type::eof())
77  {
78  // fakes success by returning c
79  return c == traits_type::eof() ? ' ' : c;
80  }
81 #else
82 
83  virtual int_type overflow(int_type c = EOF)
84  {
85  // fakes success by returning c
86  return c == EOF ? ' ' : c;
87  }
88 #endif
89 };
90 
94 class teebuf : public streambuf
95 {
96  public:
98  teebuf(streambuf* b1, streambuf* b2)
99  : streambuf(), sb1(b1), sb2(b2) {}
100  protected:
101 #ifdef MODERN_STL_STREAMS
102 
103  virtual int sync()
104  {
105  sb2->pubsync();
106  return sb1->pubsync();
107  }
109  virtual streamsize xsputn(char_type const* p, streamsize n)
110  {
111  sb2->sputn(p, n);
112  return sb1->sputn(p, n);
113  }
115  virtual int_type overflow(int_type c = traits_type::eof())
116  {
117  sb2->sputc(c);
118  return sb1->sputc(c);
119  }
120 #else
121  typedef char char_type;
122  typedef int int_type;
124  virtual int sync()
125  {
126  sb2->sync();
127  return sb1->sync();
128  }
130  virtual streamsize xsputn(char_type const* p, streamsize n)
131  {
132  sb2->xsputn(p, n);
133  return sb1->xsputn(p, n);
134  }
136  virtual int_type overflow(int_type c = EOF)
137  {
138  sb2->overflow(c);
139  return sb1->overflow(c);
140  }
141 #endif
142  private:
144  streambuf* sb1;
146  streambuf* sb2;
147 };
148 
152 class threebuf : public streambuf
153 {
154  public:
156  threebuf(streambuf* b1, streambuf* b2, streambuf* b3)
157  : streambuf(), sb1(b1), sb2(b2), sb3(b3) {}
158  protected:
159 #ifdef MODERN_STL_STREAMS
160 
161  virtual int sync()
162  {
163  sb2->pubsync();
164  return sb1->pubsync();
165  }
167  virtual streamsize xsputn(char_type const* p, streamsize n)
168  {
169  sb2->sputn(p, n);
170  sb3->sputn(p, n);
171  return sb1->sputn(p, n);
172  }
174  virtual int_type overflow(int_type c = traits_type::eof())
175  {
176  sb2->sputc(c);
177  sb3->sputc(c);
178  return sb1->sputc(c);
179  }
180 #else
181  typedef char char_type;
182  typedef int int_type;
184  virtual int sync()
185  {
186  sb2->sync();
187  sb3->sync();
188  return sb1->sync();
189  }
191  virtual streamsize xsputn(char_type const* p, streamsize n)
192  {
193  sb2->xsputn(p, n);
194  sb2->xsputn(p, n);
195  return sb1->xsputn(p, n);
196  }
198  virtual int_type overflow(int_type c = EOF)
199  {
200  sb2->overflow(c);
201  sb3->owerflow(c);
202  return sb1->overflow(c);
203  }
204 #endif
205  private:
207  streambuf* sb1;
209  streambuf* sb2;
211  streambuf* sb3;
212 };
213 
215 class debugbuf : public streambuf
216 {
217  public:
219  explicit debugbuf(streambuf* b)
220  : streambuf(), sb(b) {}
221  protected:
222 #ifdef MODERN_STL_STREAMS
223 
224  virtual int sync()
225  {
226  return sb->pubsync();
227  }
229  virtual streamsize xsputn(char_type const* p, streamsize n)
230  {
231  return sb->sputn(p, n);
232  }
234  virtual int_type overflow(int_type c = traits_type::eof())
235  {
236  return sb->sputc(c);
237  }
238 #else
239  typedef char char_type;
240  typedef int int_type;
242  virtual int sync()
243  {
244  return sb->sync();
245  }
247  virtual streamsize xsputn(char_type const* p, streamsize n)
248  {
249  return sb->xsputn(p, n);
250  }
252  virtual int_type overflow(int_type c = EOF)
253  {
254  return sb->overflow(c);
255  }
256 #endif
257  private:
259  streambuf* sb;
260 };
261 
263 class stringsigbuf : public streambuf
264 {
265  public:
266  stringsigbuf(): sb(new stringbuf())
267  {
268  }
269 
270  ~stringsigbuf()
271  {
272  if( sb )
273  {
274  delete sb;
275  sb = 0;
276  }
277  }
278 
280  explicit stringsigbuf( stringbuf* b )
281  : streambuf(), sb(b) {}
282 
283  typedef sigc::signal<void, const std::string&> StrBufOverflow_Signal;
284  inline StrBufOverflow_Signal signal_overflow()
285  {
286  return s_overflow;
287  }
288 
289  protected:
290 #ifdef MODERN_STL_STREAMS
291 
292  virtual int sync()
293  {
295  return sb->pubsync();
296  }
297 
299  virtual streamsize xsputn(char_type const* p, streamsize n)
300  {
302  streamsize r = sb->sputn(p, n);
303  s_overflow.emit( sb->str() );
304  sb->str("");
305  return r;
306  }
308  virtual int_type overflow(int_type c = traits_type::eof())
309  {
310  int_type r = sb->sputc(c);
311 
312  if( r == '\n' )
313  {
315  s_overflow.emit( sb->str() );
316  sb->str("");
317  }
318 
319  return r;
320  }
321 #else
322  typedef char char_type;
323  typedef int int_type;
325  virtual int sync()
326  {
327  return sb->sync();
328  }
330  virtual streamsize xsputn(char_type const* p, streamsize n)
331  {
332  return sb->xsputn(p, n);
333  }
334 
335  virtual int_type overflow(int_type c = EOF)
336  {
337  int_type r = sb->overflow(c);
338 
339  if( r == '\n' )
340  {
342  s_overflow.emit( sb->str() );
343  sb->str("");
344  }
345 
346  return r;
347  }
348 #endif
349  private:
351  StrBufOverflow_Signal s_overflow;
352  stringbuf* sb;
354 };
355 //--------------------------------------------------------------------------
358 {
360  filebuf fbuf;
361  stringsigbuf sbuf;
362  nullbuf nbuf;
363 };
364 //--------------------------------------------------------------------------
365 #endif