00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
00125 os << *this;
00126 } else {
00127
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
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 }