Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Derived.C

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Created on Fri Nov  7 13:27:50 PST 1997 by heydon
00020 // Last modified on Fri Apr 22 17:21:41 EDT 2005 by ken@xorian.net        
00021 //      modified on Sun Jul 28 11:57:12 EDT 2002 by lken@remote.xorian.net
00022 //      modified on Mon Nov 10 12:52:54 PST 1997 by heydon
00023 
00024 #include <Basics.H>
00025 #include <FS.H>
00026 #include <SRPC.H>
00027 #include <VestaLog.H>
00028 #include <Recovery.H>
00029 #include <SourceOrDerived.H>
00030 #include "Derived.H"
00031 
00032 using std::ostream;
00033 using std::istream;
00034 using std::ios;
00035 using std::hex;
00036 
00037 inline void Indent(ostream &os, int indent) throw ()
00038 {
00039     for (int i = 0; i < indent; i++) os << " ";
00040 }
00041 
00042 // Derived::Indices -----------------------------------------------------------
00043 
00044 void Derived::Indices::Log(VestaLog &log) const
00045   throw (VestaLog::Error)
00046 {
00047     log.write((char *)(&(this->len)), sizeof_assert(this->len, 4));
00048     if (this->len > 0) {
00049         log.write((char *)(this->index),
00050           this->len * sizeof_assert(*(this->index), 4));
00051     }
00052 }
00053 
00054 void Derived::Indices::Recover(RecoveryReader &rd)
00055   throw (VestaLog::Error, VestaLog::Eof)
00056 {
00057     rd.readAll((char *)(&(this->len)), sizeof_assert(this->len, 4));
00058     if (this->len > 0) {
00059         this->index = NEW_PTRFREE_ARRAY(Derived::Index, this->len);
00060         rd.readAll((char *)(this->index),
00061           this->len * sizeof_assert(*(this->index), 4));
00062     } else {
00063         this->index = (Derived::Index *)NULL;
00064     }
00065 }
00066 
00067 void Derived::Indices::Write(ostream &ofs) const
00068   throw (FS::Failure)
00069 {
00070     FS::Write(ofs, (char *)(&(this->len)), sizeof_assert(this->len, 4));
00071     if (this->len > 0) {
00072         FS::Write(ofs, (char *)(this->index),
00073           this->len * sizeof_assert(*(this->index), 4));
00074     }
00075 }
00076 
00077 void Derived::Indices::Read(istream &ifs)
00078   throw (FS::EndOfFile, FS::Failure)
00079 {
00080     FS::Read(ifs, (char *)(&(this->len)), sizeof_assert(this->len, 4));
00081     if (this->len > 0) {
00082         this->index = NEW_PTRFREE_ARRAY(Derived::Index, this->len);
00083         FS::Read(ifs, (char *)(this->index),
00084           this->len * sizeof_assert(*(this->index), 4));
00085     } else {
00086         this->index = (Derived::Index *)NULL;
00087     }
00088 }
00089 
00090 int Derived::Indices::Skip(istream &ifs)
00091   throw (FS::EndOfFile, FS::Failure)
00092 {
00093     int len;
00094     FS::Read(ifs, (char *)(&len), sizeof(len));
00095     int dataLen = len * sizeof(Derived::Index);
00096     FS::Seek(ifs, dataLen, ios::cur);
00097     return (sizeof(len) + dataLen);
00098 }
00099 
00100 void Derived::Indices::Send(SRPC &srpc) const throw (SRPC::failure)
00101 {
00102     srpc.send_int(this->len);
00103     if (this->len > 0) {
00104       srpc.send_int32_array((const Basics::int32 *) this->index, this->len);
00105     }
00106 }
00107 
00108 void Derived::Indices::Recv(SRPC &srpc) throw (SRPC::failure)
00109 {
00110   this->len = srpc.recv_int();
00111   if (this->len > 0) {
00112     int len;
00113     this->index = (Derived::Index *) srpc.recv_int32_array(len);
00114     assert(this->len == len);
00115   } else {
00116     this->index = (Derived::Index *)NULL;
00117   }
00118 }
00119 
00120 void Derived::Indices::Print(ostream &os, int indent) const throw ()
00121 {
00122   const int NumDIsPerLine = 5;
00123   if (this->len < NumDIsPerLine) {
00124     // print in 1-line format
00125     os << *this;
00126   } else {
00127     // multi-line format
00128     ios::fmtflags baseVal = os.flags();
00129     os << '{' << hex;
00130     for (int i = 0; i < this->len; i++) {
00131       if (i % NumDIsPerLine == 0) {
00132         os << '\n'; Indent(os, indent);
00133       }
00134       os << "0x" << this->index[i];
00135       if (i < this->len - 1) os << ", ";
00136     }
00137     os << " }";
00138     (void) os.setf(baseVal, ios::basefield);
00139   }
00140   os << '\n';
00141 }
00142 
00143 ostream& operator << (ostream &os, const Derived::Indices& dis) throw ()
00144 {
00145   ios::fmtflags baseVal = os.flags();
00146   os << "{ " << hex;
00147   for (int i = 0; i < dis.len; i++) {
00148     os << "0x" << dis.index[i];
00149     if (i < dis.len - 1) os << ", ";
00150   }
00151   os << " }";
00152   (void) os.setf(baseVal, ios::basefield);
00153   return os;
00154 }
00155 
00156 // Derived::IndicesApp --------------------------------------------------------
00157 
00158 void Derived::IndicesApp::Append(Derived::Index di) throw ()
00159 {
00160     if (this->len == this->maxLen) {
00161         this->maxLen = max(2, 2 * this->maxLen);
00162         assert(this->maxLen > this->len);
00163         Derived::Index *newIndex =
00164           NEW_PTRFREE_ARRAY(Derived::Index, this->maxLen);
00165         if (this->index != (Derived::Index *)NULL) {
00166             for (int i = 0; i < this->len; i++) {
00167                 newIndex[i] = this->index[i];
00168             }
00169         }
00170         this->index = newIndex;
00171     }
00172     this->index[this->len++] = di;
00173 }

Generated on Mon May 8 00:48:31 2006 for Vesta by  doxygen 1.4.2