00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __UT_FPStream__
00023 #define __UT_FPStream__
00024
00025 #include "UT_API.h"
00026
00027 #include <istream>
00028 #include <ostream>
00029 #include <streambuf>
00030 #include <stdio.h>
00031 #include <string.h>
00032 #include "UT_Assert.h"
00033
00034 #ifdef GCC3
00035 using namespace std;
00036 #endif
00037
00038
00039
00040
00041
00042
00043 class UT_API UT_FPOutBuf : public std::streambuf {
00044 public:
00045 UT_FPOutBuf (FILE *_fp)
00046 : myFP(_fp)
00047 {
00048 }
00049 void close()
00050 {
00051 UT_ASSERT(myFP);
00052 ::fclose(myFP);
00053 myFP = 0;
00054 }
00055
00056 protected:
00057
00058 virtual int_type overflow(int_type c)
00059 {
00060 UT_ASSERT(myFP);
00061 return putc(c, myFP);
00062 }
00063
00064
00065 virtual pos_type seekoff(off_type _Off,
00066 ios_base::seekdir _Way,
00067 ios_base::openmode = ios::out)
00068 {
00069 int whence = SEEK_CUR;
00070 UT_ASSERT(myFP);
00071 switch (_Way)
00072 {
00073 case ios_base::beg:
00074 whence = SEEK_SET;
00075 break;
00076 case ios_base::end:
00077 whence = SEEK_END;
00078 break;
00079 case ios_base::cur:
00080 default:
00081 whence = SEEK_CUR;
00082 break;
00083 }
00084 if (fseek(myFP, _Off, whence) < 0)
00085 return -1;
00086 return (pos_type)ftell(myFP);
00087 }
00088 virtual pos_type seekpos(pos_type _Off,
00089 ios_base::openmode = ios::out)
00090 {
00091 if (fseek(myFP, _Off, SEEK_SET) < 0)
00092 return -1;
00093 return (pos_type)ftell(myFP);
00094 }
00095
00096
00097 virtual std::streamsize xsputn (const char* s, std::streamsize num)
00098 {
00099 UT_ASSERT(myFP);
00100 return fwrite(s, 1, num, myFP);
00101 }
00102
00103 protected:
00104 FILE *myFP;
00105 };
00106
00107 class UT_API UT_OFPStream : public std::ostream {
00108 protected:
00109 UT_FPOutBuf buf;
00110 public:
00111 UT_OFPStream (FILE *fp) : std::ostream(0), buf(fp)
00112 {
00113 rdbuf(&buf);
00114 }
00115 void close() { buf.close(); }
00116 };
00117
00118 #endif
00119