00001 // -*- mode: C++; c-file-style: "stroustrup"; c-basic-offset: 4; indent-tabs-mode: nil; -*- 00002 //////////////////////////////////////////////////////////////////// 00003 // 00004 // Filename : FileStreamBuffer.h (base) 00005 // 00006 // Wrap (C) FILE* to std::streambuf (very simple wrap). 00007 // 00008 // This file is a part of the UPPAAL toolkit. 00009 // Copyright (c) 1995 - 2003, Uppsala University and Aalborg University. 00010 // All right reserved. 00011 // 00012 // $Id: FileStreamBuffer.h,v 1.1 2004/06/14 07:36:54 adavid Exp $ 00013 // 00014 /////////////////////////////////////////////////////////////////// 00015 00016 #ifndef INCLUDE_IO_FILESTREAMBUFFER_H 00017 #define INCLUDE_IO_FILESTREAMBUFFER_H 00018 00019 #include <stdio.h> 00020 #include <iostream> 00021 00022 namespace io 00023 { 00024 /** Simple wrapper of FILE* to streambuf to use C-style API. 00025 * This class does not open or close the file, it only uses it. 00026 */ 00027 class FileStreamBuffer : public std::streambuf 00028 { 00029 public: 00030 00031 /** Constructor: 00032 * @param theFile: file stream (in C) to wrapp. 00033 * @pre theFile != NULL 00034 */ 00035 FileStreamBuffer(FILE *theFile) 00036 : wrappedFile(theFile) {} 00037 00038 /** Destructor: flush. 00039 */ 00040 virtual ~FileStreamBuffer() 00041 { 00042 fflush(wrappedFile); 00043 } 00044 00045 /** overrides default overflow. 00046 */ 00047 virtual int overflow(int c) 00048 { 00049 return fputc(c, wrappedFile); 00050 } 00051 00052 private: 00053 FILE *wrappedFile; 00054 }; 00055 } 00056 00057 00058 /** A macro to wrap print calls based on FILE* to 00059 * calls using ostream. 00060 * @param F: file* to wrap 00061 * @param OBJ: object to print 00062 */ 00063 #define PRINT_OSTREAM(F,OBJ) \ 00064 FileStreamBuffer local_fsb(F);\ 00065 std::ostream(&local_fsb) << OBJ 00066 00067 00068 #endif // INCLUDE_IO_FILESTREAMBUFFER_H