00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <iostream>
00024 #include <fstream>
00025 #include <streambuf>
00026 #include <cstdio>
00027 #include "mfstream.h"
00028
00029 using namespace std;
00030
00031 void mfstream::open(const char *name,openmode mode)
00032 {
00033
00034 char cmode[10];
00035
00036 if (strchr(name,' ')!=0) {
00037 if (mode & ios::in)
00038 strcpy(cmode,"r");
00039 else if (mode & ios::out)
00040 strcpy(cmode,"w");
00041 else if (mode & ios::app)
00042 strcpy(cmode,"a");
00043 else {
00044 cerr << "cannot open file\n";
00045 exit(1);
00046 }
00047 _cmd=1;
00048 strcpy(_cmdname,name);
00049 _FILE=popen(name,cmode);
00050 buf=new fdbuf(fileno(_FILE));
00051 iostream::rdbuf((streambuf*) buf);
00052 } else {
00053 _cmd=0;
00054 fstream::open(name,mode);
00055 }
00056
00057 }
00058
00059
00060 void mfstream::close()
00061 {
00062 if (_cmd==1) {
00063 pclose(_FILE);
00064 delete buf;
00065 } else {
00066 fstream::clear();
00067 fstream::close();
00068 }
00069 _cmd=2;
00070 }
00071
00072
00073
00074 int mfstream::swapbytes(char *p, int sz, int n)
00075 {
00076 char c,
00077 *l,
00078 *h;
00079
00080 if((n<1) ||(sz<2)) return 0;
00081 for(; n--; p+=sz) for(h=(l=p)+sz; --h>l; l++) {
00082 c=*h;
00083 *h=*l;
00084 *l=c;
00085 }
00086 return 0;
00087
00088 };
00089
00090
00091 mfstream& mfstream::iwritex(streampos loc,void *ptr,int size,int n)
00092 {
00093 streampos pos=tellp();
00094
00095 seekp(loc);
00096
00097 writex(ptr,size,n);
00098
00099 seekp(pos);
00100
00101 return *this;
00102
00103 }
00104
00105
00106 mfstream& mfstream::readx(void *p, int sz,int n)
00107 {
00108 if(!read((char *)p, sz * n)) return *this;
00109
00110 if(*(short *)"AB"==0x4241) {
00111 swapbytes((char*)p, sz,n);
00112 }
00113
00114 return *this;
00115 }
00116
00117 mfstream& mfstream::writex(void *p, int sz,int n)
00118 {
00119 if(*(short *)"AB"==0x4241) {
00120 swapbytes((char*)p, sz,n);
00121 }
00122
00123 write((char *)p, sz * n);
00124
00125 if(*(short *)"AB"==0x4241) swapbytes((char*)p, sz,n);
00126
00127 return *this;
00128 }
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150