00001
00002
00003
00004 #include "RodPrimList.h"
00005
00006 namespace SctPixelRod {
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 PrimListException::PrimListException( std::string descriptor, unsigned long data1,
00018 unsigned long data2) : BaseException(descriptor) {
00019 m_data1 = data1;
00020 m_data2 = data2;
00021 setType(PRIMLIST);
00022 }
00023
00024 std::ostream& PrimListException::what(std::ostream& os, PrimListException& primEx) {
00025 os << "PrimListException: " << primEx.getDescriptor() << std::endl;
00026 os << "Data1:" << primEx.getData1() << std::endl;
00027 os << "Data2:" << primEx.getData2() << std::endl;
00028 return os;
00029 }
00030
00031
00032
00033
00034
00035
00036
00037
00038 RodPrimList::RodPrimList(const RodPrimList &rhs) :
00039 std::list<RodPrimitive>(rhs) {
00040 m_index=rhs.getIndex();
00041 m_version=rhs.getVersion();
00042 }
00043
00044 RodPrimList &RodPrimList::operator=(const RodPrimList &rhs) {
00045 if (this == &rhs)
00046 return *this;
00047 std::list<RodPrimitive>::operator=(rhs);
00048 m_index=rhs.getIndex();
00049 m_version=rhs.getVersion();
00050 if (rhs.m_buffer) {
00051 m_bufferSize=rhs.m_bufferSize;
00052 std::copy(rhs.m_buffer, rhs.m_buffer+rhs.m_bufferSize, m_buffer);
00053 } else {
00054 m_bufferSize=0;
00055 m_buffer=0;
00056 }
00057 return *this;
00058 }
00059
00060 RodPrimList::~RodPrimList() {
00061 if (m_buffer)
00062 delete [] m_buffer;
00063 }
00064
00065 unsigned long RodPrimList::numWords() {
00066 unsigned long n=6;
00067
00068 RodPrimList::iterator i;
00069 for (i = begin(); i != end(); i++)
00070 n += i->getLength();
00071 return n;
00072 }
00073
00074 unsigned long RodPrimList::checkSum() {
00075 unsigned long c = m_buffer[0];;
00076 for (unsigned long i = 1; i < m_bufferSize-1; i++) {
00077 c ^= m_buffer[i];
00078 }
00079 return c;
00080 }
00081
00082 void RodPrimList::bufferBuild() throw(PrimListException &) {
00083 m_bufferSize = numWords();
00084 m_buffer = new unsigned long[m_bufferSize];
00085 if (0 == m_buffer) throw PrimListException(
00086 "Bufferbuild unable to allocate buffer, bufferSize, index:", m_bufferSize,
00087 m_index);
00088 m_buffer[0] = m_bufferSize;
00089 m_buffer[1] = m_index;
00090 m_buffer[2] = this->size();
00091 m_buffer[3] = m_version;
00092
00093 long bufPos=4;
00094 long primLength;
00095 long *bodyBegin, *bodyEnd;
00096
00097 for (RodPrimList::iterator i = this->begin(); i != this->end(); i++) {
00098 primLength = i->getLength();
00099 m_buffer[bufPos++] = primLength;
00100 m_buffer[bufPos++] = i->getIndex();
00101 m_buffer[bufPos++] = i->getId();
00102 m_buffer[bufPos++] = i->getVersion();
00103 bodyBegin=i->getBody(); bodyEnd=bodyBegin+primLength-4;
00104 for(long *j=bodyBegin; j < bodyEnd; j++, bufPos++)
00105 m_buffer[bufPos]=(unsigned long) *j;
00106 }
00107 m_buffer[bufPos++] = m_bufferSize;
00108 m_buffer[bufPos] = checkSum();
00109 }
00110
00111 void RodPrimList::writeToXml(std::string& xmlFile) throw(PrimListException &) {
00112 long numPrims, primLength, bufPos;
00113 unsigned long i;
00114 long j;
00115 std::string currentLine;
00116 std::ofstream fout;
00117
00118
00119 fout.open(xmlFile.c_str(), std::ios::out);
00120 if (!fout.is_open()) throw PrimListException("Unable to open XML file for output ", 0, 0);
00121 fout << "<?xml version=\"1.0\"?>" << std::endl;
00122 fout << "<!DOCTYPE PRIMLIST SYSTEM \"PrimList.dtd\">" << std::endl;
00123 fout << "<?xml-stylesheet type=\"text/css\" href=\"PrimList.css\"?>" << std::endl;
00124 fout << "<PRIMLIST>" << std::endl;
00125 fout << " <LISTSIZE> " << m_bufferSize << " </LISTSIZE>" << std::endl;
00126 fout << " <LISTINDEX> " << m_index << " </LISTINDEX>" << std::endl;
00127 numPrims = this->size();
00128 fout << " <NUMPRIMS> " << numPrims << " </NUMPRIMS>" << std::endl;
00129 fout << " <LISTVERSION> " << m_version << " </LISTVERSION>" << std::endl;
00130 bufPos = 4;
00131 for (i=0; i<m_buffer[2]; i++) {
00132 fout << " <PRIMITIVE>" << std::endl;
00133 primLength = m_buffer[bufPos++];
00134 fout << " <PRIMLENGTH> " << primLength << " </PRIMLENGTH>" << std::endl;
00135 fout << " <PRIMINDEX> " << m_buffer[bufPos++] << " </PRIMINDEX>" << std::endl;
00136 fout << " <PRIMNAME> " << "undefined" << " </PRIMNAME>" << std::endl;
00137 fout << " <PRIMID> " << m_buffer[bufPos++] << " </PRIMID>" << std::endl;
00138 fout << " <PRIMVERSION> " << m_buffer[bufPos++] << " </PRIMVERSION>" << std::endl;
00139 fout << " <PRIMDATA TYPE=""HEX"">" << std::endl;
00140 for (j=0; j<primLength-4; j++) {
00141 fout << " " << std::hex << m_buffer[bufPos++] << std::endl;
00142 }
00143 fout << " </PRIMDATA>" << std::endl;
00144 fout << " </PRIMITIVE>" << std::endl;
00145 }
00146 fout << "</PRIMLIST>" << std::endl;
00147 }
00148
00149 void RodPrimList::buildFromXml(std::string& xmlFile) throw(PrimListException &) {
00150 long index, numPrims, primLength;
00151 std::string currentLine;
00152 std::ifstream fin;
00153
00154
00155 fin.open(xmlFile.c_str());
00156 if (!fin.is_open()) throw PrimListException("Unable to open XML file for input.", 0, 0);
00157 getline(fin, currentLine);
00158 if (currentLine.find("<?xml version=")==std::string::npos) throw PrimListException(
00159 "First line of file does not have XML tag", 0, 0);
00160 getline(fin, currentLine);
00161 if (currentLine.find("<?xml-stylesheet")<std::string::npos) {
00162 fin.ignore();
00163 getline(fin, currentLine);
00164 }
00165
00166
00167 if (currentLine.find("PRIMLIST")==std::string::npos) throw
00168 PrimListException("PrimList not found.", 0, 0);
00169 getline(fin, currentLine, '>');
00170 if (currentLine.find("LISTSIZE")==std::string::npos) throw PrimListException("PrimList size not found.", 0, 0);
00171 fin >> m_bufferSize;
00172 m_buffer = new unsigned long[m_bufferSize];
00173 m_buffer[0] = m_bufferSize;
00174 fin.ignore(256, '\n');
00175 index = 1;
00176
00177
00178 getline(fin, currentLine, '>');
00179 if (currentLine.find("LISTINDEX")==std::string::npos) throw PrimListException("PrimList index not found.", 0, 0);
00180 fin >> m_index;
00181 m_buffer[index++] = m_index;
00182 fin.ignore(256, '\n');
00183 getline(fin, currentLine, '>');
00184 if (currentLine.find("NUMPRIMS")==std::string::npos) throw PrimListException("PrimList index not found.", 0, 0);
00185 fin >> numPrims;
00186 m_buffer[index++] = numPrims;
00187 fin.ignore(256, '\n');
00188 getline(fin, currentLine, '>');
00189 if (currentLine.find("LISTVERSION")==std::string::npos) throw
00190 PrimListException("PrimList version not found.", 0, 0);
00191 fin >> m_version;
00192 m_buffer[index++] = m_version;
00193 fin.ignore(256, '\n');
00194
00195
00196 for (int iprim=0; iprim<numPrims; iprim++) {
00197 getline(fin, currentLine, '>');
00198 if (currentLine.find("PRIMITIVE")==std::string::npos) throw
00199 PrimListException("Primitive not found.", iprim, 0);
00200 getline(fin, currentLine, '>');
00201 if (currentLine.find("PRIMLENGTH")==std::string::npos) throw
00202 PrimListException("Primitive length not found.", 0, 0);
00203 fin >> primLength;
00204 m_buffer[index++] = primLength;
00205 fin.ignore(256, '\n');
00206 getline(fin, currentLine, '>');
00207 if (currentLine.find("PRIMINDEX")==std::string::npos) throw
00208 PrimListException("Primitive index not found.", 0, 0);
00209 fin >> m_buffer[index++];
00210 fin.ignore(256, '\n');
00211 getline(fin, currentLine, '>');
00212 if (currentLine.find("PRIMNAME")<std::string::npos) fin.ignore(256,'\n');
00213 getline(fin, currentLine, '>');
00214 if (currentLine.find("PRIMID")==std::string::npos) throw
00215 PrimListException("Primitive ID not found.", 0, 0);
00216 fin >> m_buffer[index++];
00217 fin.ignore(256, '\n');
00218 getline(fin, currentLine, '>');
00219 if (currentLine.find("PRIMVERSION")==std::string::npos) throw
00220 PrimListException("Primitive version not found.", 0, 0);
00221 fin >> m_buffer[index++];
00222 fin.ignore(256, '\n');
00223 getline(fin, currentLine, '>');
00224 if (currentLine.find("REPLYLENGTH")<std::string::npos) fin.ignore(256, '\n');
00225 getline(fin, currentLine);
00226 if (currentLine.find("PRIMDATA")==std::string::npos) throw
00227 PrimListException("Primitive data not found.", 0, 0);
00228
00229 long indata;
00230 if (currentLine.find("DEC") < std::string::npos) {
00231 for (int idata=0; idata< primLength-4; idata++) {
00232 fin >> indata;
00233 m_buffer[index++] = indata;
00234 dec(fin);
00235 }
00236 }
00237 else {
00238 for (int idata=0; idata< primLength-4; idata++) {
00239 fin >> std::hex >> indata;
00240 m_buffer[index++] = indata;
00241 dec(fin);
00242 }
00243 }
00244 getline(fin, currentLine, '>');
00245 if (currentLine.find("/PRIMDATA")==std::string::npos) throw
00246 PrimListException("Primitive data close not found.", 0, 0);
00247 fin.ignore(256, '\n');
00248 getline(fin, currentLine, '>');
00249 if (currentLine.find("/PRIMITIVE")==std::string::npos) throw
00250 PrimListException("Primitive close not found.", 0, 0);
00251 fin.ignore(256, '\n');
00252 }
00253
00254
00255 m_buffer[index++] = m_bufferSize;
00256 m_buffer[index] = checkSum();
00257
00258 fin.close();
00259 }
00260
00261 void RodPrimList::clear() {
00262 this->erase(begin(), end());
00263 m_bufferSize=0;
00264 if (m_buffer)
00265 delete [] m_buffer;
00266 m_buffer = 0;
00267 }
00268
00269 void RodPrimList::print() {
00270 if (!m_buffer)
00271 std::cerr << "Please call bufferBuild before printing.\n";
00272 for (unsigned int i=0; i<m_bufferSize; i++)
00273 std::cout << m_buffer[i] << std::endl;
00274 }
00275 }
00276
00277
00278
00279 std::ostream& operator<<(std::ostream& os, SctPixelRod::PrimListException& primEx) {
00280 return primEx.what(os, primEx);
00281 }
00282