00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <unistd.h>
00025 #include <string.h>
00026 #include <iostream>
00027 #include <cstring>
00028 #include <cstdlib>
00029 #include <fstream>
00030 #include <streambuf>
00031 #include <cstdio>
00032
00033 using namespace std;
00034
00035 #ifndef MF_STREAM_H
00036 #define MF_STREAM_H
00037
00038 extern "C" {
00039 ssize_t write (int fd, const void* buf, size_t num);
00040 ssize_t read (int fd, void* buf, size_t num);
00041 FILE *popen(const char *command, const char *type);
00042 int pclose(FILE *stream);
00043 int fseek( FILE *stream, long offset, int whence);
00044 long ftell( FILE *stream);
00045 };
00046
00047
00049 class fdbuf : public std::streambuf {
00050
00051 protected:
00052 int fd;
00053
00054
00055 virtual int_type overflow (int_type c) {
00056 char z = c;
00057 if (c != EOF) {
00058 if (write (fd, &z, 1) != 1) {
00059 return EOF;
00060 }
00061 }
00062
00063
00064 return c;
00065 }
00066
00067
00068 virtual
00069 std::streamsize xsputn (const char* s,
00070 std::streamsize num) {
00071 return write(fd,s,num);
00072
00073 }
00074
00075 virtual streampos seekpos ( streampos sp, ios_base::openmode which = ios_base::in | ios_base::out ){
00076 cerr << "seekpos is nt implemented\n";
00077 return (streampos) 0;
00078 }
00079
00080
00081 virtual int_type underflow () {
00082
00083 if (gptr() < egptr()) {
00084 return traits_type::to_int_type(*gptr());
00085 }
00086
00087
00088
00089
00090
00091 int numPutback;
00092 numPutback = gptr() - eback();
00093 if (numPutback > 4) {
00094 numPutback = 4;
00095 }
00096
00097
00098
00099
00100 std::memmove (buffer+(4-numPutback), gptr()-numPutback,
00101 numPutback);
00102
00103
00104 int num;
00105 num = read (fd, buffer+4, bufferSize-4);
00106 if (num <= 0) {
00107
00108 return EOF;
00109 }
00110
00111
00112 setg (buffer+(4-numPutback),
00113 buffer+4,
00114 buffer+4+num);
00115
00116
00117 return traits_type::to_int_type(*gptr());
00118 }
00119
00120
00121
00122 virtual
00123 std::streamsize xsgetn (char* s,
00124 std::streamsize num) {
00125 return read(fd,s,num);
00126 }
00127
00128 static const int bufferSize = 10;
00129 char buffer[bufferSize];
00130
00131 public:
00132
00133
00134 fdbuf (int _fd) : fd(_fd) {
00135 setg (buffer+4,
00136 buffer+4,
00137 buffer+4);
00138 }
00139
00140 };
00141
00142
00143
00145
00146 class mfstream : public std::fstream{
00147
00148 protected:
00149 fdbuf* buf;
00150 int _cmd;
00151 openmode _mode;
00152 FILE* _FILE;
00153
00154
00155 int swapbytes(char *p, int sz, int n);
00156
00157 public:
00158
00159 char _cmdname[500];
00160
00162 mfstream (const char* name,openmode mode) : std::fstream() {
00163 _cmdname[0]='\0';
00164 _mode=mode;
00165 open(name,mode);
00166 }
00167
00169 ~mfstream(){
00170 if (_cmd<2) close();
00171 }
00172
00174 void open(const char *name,openmode mode);
00175
00177 void close();
00178
00180 mfstream& writex(void *p, int sz,int n=1);
00181
00183 mfstream& readx(void *p, int sz,int n=1);
00184
00186 mfstream& iwritex(streampos loc,void *ptr,int size,int n=1);
00187
00189 streampos tellp(){
00190 if (_cmd==0) return (streampos) fstream::tellg();
00191 cerr << "tellp not allowed on commands\n";
00192 exit(1);
00193 }
00194
00196 mfstream& seekp(streampos loc){
00197 if (_cmd==0)
00198 fstream::seekg(loc);
00199 else{
00200 cerr << "seekp not allowed on commands\n";
00201 exit(1);
00202 }
00203 return *this;
00204 }
00205
00207
00208 mfstream& reopen(){
00209
00210 if (_mode != in){
00211 cerr << "mfstream::reopen() openmode must be ios:in\n";
00212 exit(1);
00213 }
00214
00215 if (strlen(_cmdname)>0){
00216 char *a=new char[strlen(_cmdname)+1];
00217 strcpy(a,_cmdname);
00218 cerr << "close/open " << a <<"\n";
00219 close();
00220 open(a,ios::in);
00221 }
00222 else
00223 seekp(0);
00224
00225 return *this;
00226 }
00227
00228 };
00229
00230
00231 #endif