SFML logo
  • Main Page
  • Namespaces
  • Classes
  • Files
  • File List

Err.cpp

00001 
00002 //
00003 // SFML - Simple and Fast Multimedia Library
00004 // Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
00005 //
00006 // This software is provided 'as-is', without any express or implied warranty.
00007 // In no event will the authors be held liable for any damages arising from the use of this software.
00008 //
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it freely,
00011 // subject to the following restrictions:
00012 //
00013 // 1. The origin of this software must not be misrepresented;
00014 //    you must not claim that you wrote the original software.
00015 //    If you use this software in a product, an acknowledgment
00016 //    in the product documentation would be appreciated but is not required.
00017 //
00018 // 2. Altered source versions must be plainly marked as such,
00019 //    and must not be misrepresented as being the original software.
00020 //
00021 // 3. This notice may not be removed or altered from any source distribution.
00022 //
00024 
00026 // Headers
00028 #include <SFML/System/Err.hpp>
00029 #include <streambuf>
00030 #include <stdio.h>
00031 
00032 
00034 // Private data
00036 namespace
00037 {
00038 // This class will be used as the default streambuf of sf::Err,
00039 // it outputs to stderr by default (to keep the default beaviour)
00040 class DefaultErrStreamBuf : public std::streambuf
00041 {
00042 public :
00043 
00044     DefaultErrStreamBuf() 
00045     {
00046         // Allocate the write buffer
00047         static const int size = 64;
00048         char* buffer = new char[size];
00049         setp(buffer, buffer + size);
00050     }
00051 
00052     ~DefaultErrStreamBuf() 
00053     {
00054         // Synchronize
00055         sync();
00056 
00057         // Delete the write buffer
00058         delete[] pbase();
00059     }
00060 
00061 private :
00062 
00063     virtual int overflow(int character)
00064     {
00065         if ((character != EOF) && (pptr() != epptr()))
00066         {
00067             // Valid character
00068             return sputc(static_cast<char>(character));
00069         }
00070         else
00071         {
00072             // Invalid character, or not enough space in the buffer: synchronize output
00073             return sync();
00074         }
00075     }
00076 
00077     virtual int sync()
00078     {
00079         // Check if there is something into the write buffer
00080         if (pbase() != pptr())
00081         {
00082             // Retrieve the contents of the write buffer
00083             int size = static_cast<int>(pptr() - pbase());
00084             std::string buffer(pbase(), size);
00085 
00086             // Write it into the standard error output
00087             fprintf(stderr, "%s", buffer.c_str());
00088 
00089             // Reset the pointer position to the beginning of the write buffer
00090             setp(pbase(), epptr());
00091         }
00092 
00093         return 0;
00094     }
00095 };
00096 }
00097 
00098 namespace sf
00099 {
00101 std::ostream& Err()
00102 {
00103     static DefaultErrStreamBuf buffer;
00104     static std::ostream stream(&buffer);
00105 
00106     return stream;
00107 }
00108 
00109 
00110 } // namespace sf

 ::  Copyright © 2007-2008 Laurent Gomila, all rights reserved  ::  Documentation generated by doxygen 1.5.2  ::