XmlFileStream.cpp

Go to the documentation of this file.
00001 /* ****************************************************************** **
00002 **    OpenSees - Open System for Earthquake Engineering Simulation    **
00003 **          Pacific Earthquake Engineering Research Center            **
00004 **                                                                    **
00005 **                                                                    **
00006 ** (C) Copyright 1999, The Regents of the University of California    **
00007 ** All Rights Reserved.                                               **
00008 **                                                                    **
00009 ** Commercial use of this program without express permission of the   **
00010 ** University of California, Berkeley, is strictly prohibited.  See   **
00011 ** file 'COPYRIGHT'  in main directory for information on usage and   **
00012 ** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
00013 **                                                                    **
00014 ** Developed by:                                                      **
00015 **   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
00016 **   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
00017 **   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
00018 **                                                                    **
00019 ** ****************************************************************** */
00020                                                                         
00021 // $Revision: 1.1 $
00022 // $Date: 2006/08/03 23:28:34 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/handler/XmlFileStream.cpp,v $
00024 
00025 #include <XmlFileStream.h>
00026 #include <Vector.h>
00027 #include <iostream>
00028 #include <iomanip>
00029 #include <ID.h>
00030 #include <Channel.h>
00031 #include <Message.h>
00032 
00033 using std::cerr;
00034 using std::ios;
00035 using std::setiosflags;
00036 
00037 XmlFileStream::XmlFileStream(int indent)
00038   :OPS_Stream(OPS_STREAM_TAGS_XmlFileStream), 
00039    fileOpen(0), fileName(0), indentSize(indent), numIndent(-1),
00040    attributeMode(false), numTag(0), sizeTags(0), tags(0)
00041 {
00042   opserr << "XmlFileStream::" << endln;
00043   if (indentSize < 1) indentSize = 1;
00044   indentString = new char[indentSize+1];
00045   //  for (int i=0; i<indentSize; i++)
00046   //    strcpy(indentString, " ");
00047 
00048   indentString = "  ";
00049 
00050 }
00051 
00052 XmlFileStream::XmlFileStream(const char *name, openMode mode, int indent)
00053   :OPS_Stream(OPS_STREAM_TAGS_XmlFileStream), 
00054    fileOpen(0), fileName(0), indentSize(indent), numIndent(-1),
00055    attributeMode(false), numTag(0), sizeTags(0), tags(0)
00056 {
00057   if (indentSize < 1) indentSize = 1;
00058   indentString = new char[indentSize+1];
00059   //  for (int i=0; i<indentSize; i++)
00060   //    strcat(indentString," ");
00061   indentString = "  ";
00062 
00063   this->setFile(name, mode);
00064 
00065 }
00066 
00067 XmlFileStream::~XmlFileStream()
00068 {
00069   for (int i=numTag; i>0; i--) {
00070     this->indent();
00071     theFile << "</" << tags[i] << ">\n";
00072     delete [] tags[i];
00073     numTag--;
00074   }
00075 
00076   delete [] tags;
00077 
00078   if (fileOpen == 1)
00079     theFile.close();
00080   
00081   if (fileName != 0)
00082     delete [] fileName;
00083 }
00084 
00085 int 
00086 XmlFileStream::setFile(const char *name, openMode mode)
00087 {
00088   if (name == 0) {
00089     std::cerr << "XmlFileStream::setFile() - no name passed\n";
00090     return -1;
00091   }
00092 
00093   // first create a copy of the file name
00094   if (fileName != 0) {
00095     if (strcmp(fileName, name) != 0)
00096       delete [] fileName;
00097     fileName = 0;
00098   }
00099   if (fileName == 0) {
00100     fileName = new char[strlen(name)+1];
00101     if (fileName == 0) {
00102       std::cerr << "XmlFileStream::setFile() - out of memory copying name: " << name << std::endl;
00103       return -1;
00104     }
00105     
00106     // copy the strings
00107     strcpy(fileName, name);
00108   }
00109 
00110   // if file already open, close it
00111   if (fileOpen == 1) {
00112     theFile.close();
00113     fileOpen = 0;
00114   }
00115 
00116   if (mode == OVERWRITE) 
00117     theFile.open(fileName, ios::out);
00118   else
00119     theFile.open(fileName, ios::out| ios::app);
00120 
00121   if (theFile.bad()) {
00122     std::cerr << "WARNING - XmlFileStream::setFile()";
00123     std::cerr << " - could not open file " << fileName << std::endl;
00124 
00125     return -1;
00126   } else
00127     fileOpen = 1;
00128 
00129   if (mode == 0)
00130     theOpenMode = OVERWRITE;
00131   else
00132     theOpenMode = APPEND;
00133 
00134   theFile << setiosflags(ios::fixed);
00135 
00136 
00137   theFile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
00138   theFile << " <OpenSees\n";
00139   theFile << "  xmlns:xsi = \"http://www.w3.org/2001/XMLSchema-instance\"\n";
00140   theFile << "  xsi:noNamespaceSchemaLocation = \"http://OpenSees.berkeley.edu//xml-schema/xmlns/OpenSees.xsd\">\n";
00141 
00142   return 0;
00143 }
00144 
00145 int 
00146 XmlFileStream::open(void)
00147 {
00148   // check setFile has been called
00149   if (fileName == 0) {
00150     std::cerr << "XmlFileStream::open(void) - no file name has been set\n";
00151     return -1;
00152   }
00153 
00154   // if file already open, return
00155   if (fileOpen == 1) {
00156     return 0;
00157   }
00158 
00159   // open file
00160   theFile.open(fileName, ios::out| ios::app);
00161   if (theFile.bad()) {
00162     std::cerr << "WARNING - XmlFileStream::open()";
00163     std::cerr << " - could not open file " << fileName << std::endl;
00164 
00165     return -1;
00166   } else
00167     fileOpen = 1;
00168 
00169 
00170 
00171   return 0;
00172 }
00173 
00174 int 
00175 XmlFileStream::close(void)
00176 {
00177   if (fileOpen != 0)
00178     theFile.close();
00179   fileOpen = 0;
00180   return 0;
00181 }
00182 
00183 
00184 int 
00185 XmlFileStream::setPrecision(int prec)
00186 {
00187   if (fileOpen != 0)
00188     theFile << std::setprecision(prec);
00189 
00190   return 0;
00191 }
00192 
00193 int 
00194 XmlFileStream::setFloatField(floatField field)
00195 {
00196   if (field == FIXEDD) {
00197     if (fileOpen != 0)
00198       theFile << setiosflags(ios::fixed);
00199   }
00200   else if (field == SCIENTIFIC) {
00201     if (fileOpen != 0)
00202       theFile << setiosflags(ios::scientific);
00203   }
00204 
00205   return 0;
00206 }
00207 
00208 
00209 int 
00210 XmlFileStream::tag(const char *tagName)
00211 {
00212   //
00213   // copy tagName to end of list of tags
00214   //
00215 
00216   // if tags array not large enough, expand
00217   if (numTag == sizeTags) {
00218     int nextSize = 2*sizeTags;
00219     if (nextSize == 0) nextSize = 32;
00220     char **nextTags = new char *[nextSize];
00221     if (nextTags != 0) {
00222       for (int i=0; i<sizeTags; i++)
00223         nextTags[i] = tags[i];
00224       for (int j=sizeTags+1; j<nextSize; j++)
00225         nextTags[j] = 0;
00226       sizeTags = nextSize;
00227     } else {
00228       sizeTags = 0;
00229       delete [] tags;
00230       tags = 0;
00231       return -1;
00232     }
00233     
00234     if (tags != 0)
00235       delete [] tags;
00236 
00237     tags = nextTags;
00238   } 
00239 
00240   // copy string and assign to end of array
00241   char *newTag = new char[strlen(tagName) +1];
00242   strcpy(newTag, tagName);
00243   tags[numTag++] = newTag;
00244 
00245 
00246   if (attributeMode == true) {
00247     theFile << ">";
00248   }
00249 
00250   // output the xml for it to the file
00251 
00252   numIndent++;
00253   theFile << endln;
00254   this->indent();
00255   theFile << "<" << tagName;
00256   
00257   attributeMode = true;
00258 
00259   return 0;
00260 }
00261 
00262 
00263 int 
00264 XmlFileStream::tag(const char *tagName, const char *value)
00265 {
00266   if (attributeMode == true) {
00267     theFile << ">\n";
00268   }
00269 
00270   // output the xml for it to the file
00271   numIndent++;
00272   this->indent();
00273   theFile << "<" << tagName << ">" << value << "<" << tagName << "/>" << endln;
00274   numIndent--;
00275 
00276   attributeMode = false;
00277 
00278   return 0;
00279 }
00280 
00281 int 
00282 XmlFileStream::endTag()
00283 {
00284   if (attributeMode == true) {
00285     theFile << "/>\n";
00286     delete [] tags[numTag-1];
00287     numTag--;
00288   } else {
00289     this->indent();
00290     theFile << "</" << tags[numTag-1] << ">\n";
00291     delete [] tags[numTag-1];
00292     numTag--;
00293   }    
00294 
00295   attributeMode = false;
00296   numIndent--;
00297 
00298   return 0;
00299 }
00300 
00301 int 
00302 XmlFileStream::attr(const char *name, int value)
00303 {
00304   theFile << " " << name << "=\"" << value << "\"";
00305   
00306   return 0;
00307 }
00308 
00309 int 
00310 XmlFileStream::attr(const char *name, double value)
00311 {
00312   theFile << " " << name << "=\"" << value << "\"";
00313 
00314   return 0;
00315 }
00316 
00317 int 
00318 XmlFileStream::attr(const char *name, const char *value)
00319 {
00320   theFile << " " << name << "=\"" << value << "\"";
00321 
00322   return 0;
00323 }
00324 
00325 int 
00326 XmlFileStream::write(Vector &data)
00327 {
00328   if (attributeMode == true) {
00329     theFile << ">\n";
00330     attributeMode = false;
00331     numIndent++;
00332   }
00333 
00334   this->indent();
00335   (*this) << data;  
00336 
00337   return 0;
00338 }
00339 
00340 
00341 
00342 OPS_Stream& 
00343 XmlFileStream::write(const char *s,int n)
00344 {
00345   if (attributeMode == true) {
00346     theFile << "/>\n";
00347     attributeMode = false;
00348   }
00349 
00350   if (fileOpen != 0)
00351     theFile.write(s, n);
00352 
00353   return *this;
00354 }
00355 
00356 OPS_Stream& 
00357 XmlFileStream::write(const unsigned char*s,int n)
00358 {
00359   if (attributeMode == true) {
00360     theFile << "/>\n";
00361     attributeMode = false;
00362   }
00363 
00364   if (fileOpen != 0)
00365     theFile.write((const char *) s, n);
00366 
00367   return *this;
00368 }
00369 OPS_Stream& 
00370 XmlFileStream::write(const signed char*s,int n)
00371 {
00372   if (attributeMode == true) {
00373     theFile << "/>\n";
00374     attributeMode = false;
00375   }
00376 
00377   if (fileOpen != 0)
00378     theFile.write((const char *) s, n);
00379 
00380   return *this;
00381 }
00382 OPS_Stream& 
00383 XmlFileStream::write(const void *s, int n)
00384 {
00385   if (attributeMode == true) {
00386     theFile << "/>\n";
00387     attributeMode = false;
00388   }
00389 
00390   if (fileOpen != 0)
00391     theFile.write((const char *) s, n);
00392 
00393   return *this;
00394 }
00395 OPS_Stream& 
00396 XmlFileStream::operator<<(char c)
00397 {
00398   if (attributeMode == true) {
00399     theFile << "/>\n";
00400     attributeMode = false;
00401   }
00402 
00403   if (fileOpen != 0)
00404     theFile << c;
00405 
00406   return *this;
00407 }
00408 OPS_Stream& 
00409 XmlFileStream::operator<<(unsigned char c)
00410 {
00411   if (attributeMode == true) {
00412     theFile << "/>\n";
00413     attributeMode = false;
00414   }
00415 
00416   if (fileOpen != 0)
00417     theFile << c;
00418 
00419   return *this;
00420 }
00421 OPS_Stream& 
00422 XmlFileStream::operator<<(signed char c)
00423 {
00424   if (attributeMode == true) {
00425     theFile << "/>\n";
00426     attributeMode = false;
00427   }
00428 
00429   if (fileOpen != 0)
00430     theFile << c;
00431 
00432   return *this;
00433 }
00434 OPS_Stream& 
00435 XmlFileStream::operator<<(const char *s)
00436 {
00437   if (attributeMode == true) {
00438     theFile << "/>\n";
00439     attributeMode = false;
00440   }
00441 
00442   // note that we do the flush so that a "/n" before
00443   // a crash will cause a flush() - similar to what 
00444   if (fileOpen != 0) {
00445     theFile << s;
00446     theFile.flush();
00447   }
00448 
00449   return *this;
00450 }
00451 OPS_Stream& 
00452 XmlFileStream::operator<<(const unsigned char *s)
00453 {
00454   if (attributeMode == true) {
00455     theFile << "/>\n";
00456     attributeMode = false;
00457   }
00458 
00459   if (fileOpen != 0)
00460     theFile << s;
00461 
00462   return *this;
00463 }
00464 OPS_Stream& 
00465 XmlFileStream::operator<<(const signed char *s)
00466 {
00467   if (attributeMode == true) {
00468     theFile << "/>\n";
00469     attributeMode = false;
00470   }
00471 
00472   if (fileOpen != 0)
00473     theFile << s;
00474 
00475   return *this;
00476 }
00477 OPS_Stream& 
00478 XmlFileStream::operator<<(const void *p)
00479 {
00480   if (attributeMode == true) {
00481     theFile << "/>\n";
00482     attributeMode = false;
00483   }
00484 
00485 /*
00486   if (fileOpen != 0)
00487     theFile << p;
00488 */
00489   return *this;
00490 }
00491 OPS_Stream& 
00492 XmlFileStream::operator<<(int n)
00493 {
00494   if (attributeMode == true) {
00495     theFile << "/>\n";
00496     attributeMode = false;
00497   }
00498 
00499   if (fileOpen != 0)
00500     theFile << 1.0*n;
00501 
00502   return *this;
00503 }
00504 OPS_Stream& 
00505 XmlFileStream::operator<<(unsigned int n)
00506 {
00507   if (attributeMode == true) {
00508     theFile << "/>\n";
00509     attributeMode = false;
00510   }
00511 
00512   if (fileOpen != 0)
00513     theFile << 1.0*n;
00514 
00515   return *this;
00516 }
00517 OPS_Stream& 
00518 XmlFileStream::operator<<(long n)
00519 {
00520   if (attributeMode == true) {
00521     theFile << "/>\n";
00522     attributeMode = false;
00523   }
00524 
00525 /*
00526   if (fileOpen != 0)
00527     theFile << n;
00528 */
00529   return *this;
00530 }
00531 OPS_Stream& 
00532 XmlFileStream::operator<<(unsigned long n)
00533 {
00534   if (attributeMode == true) {
00535     theFile << "/>\n";
00536     attributeMode = false;
00537   }
00538 
00539 /*
00540   if (fileOpen != 0)
00541     theFile << n;
00542 */
00543   return *this;
00544 }
00545 OPS_Stream& 
00546 XmlFileStream::operator<<(short n)
00547 {
00548   if (attributeMode == true) {
00549     theFile << "/>\n";
00550     attributeMode = false;
00551   }
00552 
00553 /*
00554   if (fileOpen != 0)
00555     theFile << n;
00556 */
00557   return *this;
00558 }
00559 OPS_Stream& 
00560 XmlFileStream::operator<<(unsigned short n)
00561 {
00562   if (attributeMode == true) {
00563     theFile << "/>\n";
00564     attributeMode = false;
00565   }
00566 
00567 /*
00568   if (fileOpen != 0)
00569     theFile << n;
00570 */
00571   return *this;
00572 }
00573 OPS_Stream& 
00574 XmlFileStream::operator<<(bool b)
00575 {
00576   if (attributeMode == true) {
00577     theFile << "/>\n";
00578     attributeMode = false;
00579   }
00580 
00581 /*
00582   if (fileOpen != 0)
00583     theFile << b;
00584 */
00585   return *this;
00586 }
00587 OPS_Stream& 
00588 XmlFileStream::operator<<(double n)
00589 {
00590   if (attributeMode == true) {
00591     theFile << "/>\n";
00592     attributeMode = false;
00593   }
00594 
00595   if (fileOpen != 0)
00596     theFile << n;
00597 
00598   return *this;
00599 }
00600 OPS_Stream& 
00601 XmlFileStream::operator<<(float n)
00602 {
00603   if (attributeMode == true) {
00604     theFile << "/>\n";
00605     attributeMode = false;
00606   }
00607 
00608   if (fileOpen != 0)
00609     theFile << n;
00610 
00611   return *this;
00612 }
00613 
00614 
00615 int 
00616 XmlFileStream::sendSelf(int commitTag, Channel &theChannel)
00617 {
00618   static ID idData(2);
00619   int fileNameLength = 0;
00620   if (fileName != 0)
00621     fileNameLength = strlen(fileName);
00622 
00623   idData(0) = fileNameLength;
00624 
00625   if (theOpenMode == OVERWRITE)
00626     idData(1) = 0;
00627   else
00628     idData(1) = 1;
00629 
00630   if (theChannel.sendID(0, commitTag, idData) < 0) {
00631     opserr << "XmlFileStream::sendSelf() - failed to send id data\n";
00632     return -1;
00633   }
00634 
00635   if (fileNameLength != 0) {
00636     Message theMessage(fileName, fileNameLength);
00637     if (theChannel.sendMsg(0, commitTag, theMessage) < 0) {
00638       opserr << "XmlFileStream::sendSelf() - failed to send message\n";
00639       return -1;
00640     }
00641   }
00642 
00643   return 0;
00644 }
00645 
00646 int 
00647 XmlFileStream::recvSelf(int commitTag, Channel &theChannel, FEM_ObjectBroker &theBroker)
00648 {
00649   static ID idData(2);
00650 
00651   if (theChannel.recvID(0, commitTag, idData) < 0) {
00652     opserr << "XmlFileStream::recvSelf() - failed to recv id data\n";
00653     return -1;
00654   }
00655 
00656   int fileNameLength = idData(0);
00657   if (idData(1) == 0)
00658     theOpenMode = OVERWRITE;
00659   else
00660     theOpenMode = APPEND;
00661 
00662   if (fileNameLength != 0) {
00663     if (fileName != 0)
00664       delete [] fileName;
00665     fileName = new char[fileNameLength+5];
00666     if (fileName == 0) {
00667       opserr << "XmlFileStream::recvSelf() - out of memory\n";
00668       return -1;
00669     }
00670 
00671     Message theMessage(fileName, fileNameLength);
00672     if (theChannel.recvMsg(0, commitTag, theMessage) < 0) {
00673       opserr << "XmlFileStream::recvSelf() - failed to recv message\n";
00674       return -1;
00675     }
00676     sprintf(&fileName[fileNameLength],".%d",commitTag);
00677 
00678     if (this->setFile(fileName, theOpenMode) < 0) {
00679       opserr << "XmlFileStream::XmlFileStream() - setFile() failed\n";
00680       if (fileName != 0) {
00681         delete [] fileName;
00682         fileName = 0;
00683       }
00684     }
00685   }
00686   
00687   return 0;
00688 }
00689 
00690 void
00691 XmlFileStream::indent(void)
00692 {
00693   if (fileOpen != 0)
00694     for (int i=0; i<numIndent; i++)
00695       theFile << indentString;
00696 }

Generated on Mon Oct 23 15:05:13 2006 for OpenSees by doxygen 1.5.0