Main Page   Class Hierarchy   Compound List   File List   Compound Members  

marchive.cc

00001 #include <mobject.h>
00002 #include "magic/marchive.h"
00003 
00007 
00008 CArchive::CArchive (const char* filnam, int md) {
00009     in=NULL;
00010     out=NULL;
00011     indent=0;
00012     arcfile=filnam;
00013     mode = md;
00014     attribs = NULL;
00015     if (mode&ARC_SAVE)
00016         opensave();
00017     else
00018         if (mode&ARC_RECORD)
00019             openrecord ();
00020         else
00021             openload();
00022 }
00023 
00024 CArchive::~CArchive () {
00025     delete in;
00026     delete out;
00027     delete attribs;
00028 }
00029 
00030 int CArchive::open (const char* filename, int flag) {
00031     delete out;
00032     delete in;
00033     if (fbuf.is_open())
00034         fbuf.close ();
00035 
00036     filebuf* succ=NULL;
00037     String filname = arcfile;
00038     if (filename)
00039         filname = filename;
00040     succ = fbuf.open (filname, ios::IOSBINARY | ((flag==ARC_LOAD)? (ios::in|ios::nocreate) : ios::out));
00041     if (!succ || !fbuf.is_open ()) {
00042         // FAILBY("Could not open file for reading");
00043         errst = 1;
00044         return 0;
00045     }
00046     return 1;
00047 }
00048 
00049 int CArchive::opensave (const char* filename) {
00050     if (!open (filename, ARC_SAVE)) {
00051         errst = 1;
00052         return 0;
00053     }
00054     out = new ostream (&fbuf);
00055     if (!out) {
00056         errst = 1;
00057         // FAILBY ("Unnable to open output stream");
00058         return 0;
00059     }
00060     return 1;
00061 }
00062 
00063 int CArchive::openload (const char* filename) {
00064     if (!open (filename, ARC_LOAD)) {
00065         errst=1;
00066         return 0;
00067     }
00068     in = new istream (&fbuf);
00069     if (!in) {
00070         errst = 1;
00071         // FAILBY ("Unable to open input stream");
00072         return 0;
00073     }
00074     return 1;
00075 }
00076 
00077 int CArchive::openrecord () {
00078     attribs = new Array<String>;
00079 }
00080 
00081 void CArchive::close () {
00082     fbuf.close ();
00083     delete in;
00084     delete out;
00085     in = NULL;
00086     out = NULL;
00087 }
00088 
00089 #define CHECKRECORD(rtype,var) \
00090     if (mode&ARC_RECORD) {\
00091         if (nextname.isempty())\
00092             nextname = "?";\
00093         attribs->add (new String (format ("%s %s", #rtype, (CONSTR)nextname)));\
00094         return *this;\
00095     }
00096 //      nextname += format (" = %s", (CONSTR) String (var));\
00097 
00098 CArchive& CArchive::operator<< (int i) {
00099     CHECKRECORD (int, i);
00100 
00101     operator<< ((long) i);
00102     return *this;
00103 }
00104 
00105 CArchive& CArchive::operator<< (long i) {
00106     CHECKRECORD (long, int (i));
00107 
00108     if (mode&ARC_TEXT) {
00109         for (int j=0; j<indent; j++) *out << "\t";
00110         *out << i << "\n";
00111     } else
00112         out->write ((char*) &i, sizeof(i));
00113     return *this;
00114 }
00115 
00116 CArchive& CArchive::operator<<  (char i)                {
00117     CHECKRECORD (char, i);
00118     if (out) out->write ((char*) &i, sizeof(i)); return *this;
00119 }
00120 
00121 CArchive& CArchive::operator<< (float i) {
00122     CHECKRECORD (float, i);
00123     operator<< ((double) i);
00124     return *this;
00125 }
00126 
00127 CArchive& CArchive::operator<<  (double i)              {
00128     CHECKRECORD (double, float(i));
00129     if (mode&ARC_TEXT) {
00130         for (int j=0; j<indent; j++) *out << "\t";
00131         *out << i << "\n";
00132     } else
00133         out->write ((char*) &i, sizeof(i));
00134     return *this;
00135 }
00136 
00137 CArchive& CArchive::write       (const char* p, int n)  {
00138     CHECKRECORD (char*, p);
00139     if (mode&ARC_TEXT) {
00140         for (int i=0; i<indent; i++) *out << "\t";
00141         String str, str2 (p, n);
00142         str.hexcode (str2);
00143         *out << str << "\n";
00144     } else
00145         out->write (p, n);
00146     return *this;
00147 }
00148 
00149 CArchive& CArchive::operator>>  (long& i)           {
00150     if (in) in->read ((char*) &i, sizeof(i)); return *this;
00151 }
00152 
00153 CArchive& CArchive::operator>>  (char& i)           {
00154     if (mode&ARC_TEXT) {
00155         ;
00156     } else
00157         in->read ((char*) &i, sizeof(i));
00158     return *this;
00159 }
00160 
00161 CArchive& CArchive::operator>>  (double& i)         {
00162     if (in) in->read ((char*) &i, sizeof(i)); return *this;
00163 }
00164 CArchive& CArchive::read        (char* p, int n)    {
00165     if (in) in->read (p, n); return *this;
00166 }
00167 
00168 CArchive& operator<< (CArchive& out, const char* str) {
00169     out.name (str);
00170     return out;
00171 }

Generated at Tue Dec 4 19:53:24 2001 for MagiC++ by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001