Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

gul_std.cpp

Go to the documentation of this file.
00001 /* LIBGUL - Geometry Utility Library
00002  * Copyright (C) 1998-1999 Norbert Irmer
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 #include "stdafx.h"
00021 
00022 #include <iostream>
00023 #include <string>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 
00027 #include "gul_types.h"
00028 #include "gul_std.h"
00029 
00030 #ifdef __BORLANDC__
00031   #include "string.cc"
00032 #endif
00033 
00034 template 
00035 class std::basic_string<char,std::char_traits<char>,gul::poolalloc<char> >;
00036 
00037 
00038 namespace gul {
00039 
00040 /*--------------------------------------------------------------------------
00041   class for redirecting cout/cerr to a debugger window and/or stream.
00042   this class registers an 'atexit' function to restore the original
00043   'streambufs'  
00044 ---------------------------------------------------------------------------*/
00045 static List<streambufrec> OldStreambufs;  
00046 
00047 GULAPI debug_streambuf::debug_streambuf( FILE *logfile )
00048 {
00049   m_bptr = 0;
00050   m_logfile = logfile; 
00051 
00052   m_oldsb = new streambufrec;
00053   OldStreambufs.Append(m_oldsb); 
00054 
00055   m_oldsb->cerr_buf = std::cerr.rdbuf();
00056   m_oldsb->cout_buf = std::cout.rdbuf();
00057   std::cerr.rdbuf( this );
00058   std::cout.rdbuf( this );
00059   if( !initialized )
00060   {
00061     atexit(debug_streambuf::atexit_func);
00062     initialized = true;
00063   }
00064 }
00065 
00066 int debug_streambuf::overflow( int c )
00067 {
00068   m_buf[m_bptr++] = c;
00069 
00070   if( (c == '\n') || (m_bptr == 1023) )
00071   {
00072     m_buf[m_bptr] = 0;
00073 
00074     if( m_logfile )
00075       fprintf(m_logfile,m_buf);
00076     printf(m_buf);
00077     #ifdef _MSC_VER
00078       OutputDebugStringA(m_buf);
00079     #endif
00080     m_bptr = 0;
00081   }
00082   return(c);
00083 }
00084 
00085 void debug_streambuf::atexit_func( void )
00086 {
00087   streambufrec *oldsb,*oldsb_next;
00088 
00089   oldsb = OldStreambufs.First(); 
00090   OldStreambufs.ReleaseElems();
00091 
00092   while( oldsb )
00093   {
00094     std::cerr.rdbuf( oldsb->cerr_buf );
00095     std::cout.rdbuf( oldsb->cout_buf );
00096     oldsb_next = oldsb->next;
00097     delete oldsb;
00098     oldsb = oldsb_next;
00099   }
00100 }
00101 
00102 GULAPI debug_streambuf::~debug_streambuf()
00103 {
00104   if( OldStreambufs.nElems ) // atexit function wasn't called
00105   {
00106     OldStreambufs.Remove(m_oldsb);
00107     std::cerr.rdbuf( m_oldsb->cerr_buf );
00108     std::cout.rdbuf( m_oldsb->cout_buf );
00109     delete m_oldsb;
00110   }
00111 }
00112 
00113 bool debug_streambuf::initialized = false;
00114 
00115 }
00116 
00117 
00118 

Generated on Mon Jan 21 04:17:32 2002 for GUL 0.6 - Geometry Utility Library by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001