FileStream.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.8 $
00022 // $Date: 2006/08/03 23:24:56 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/handler/FileStream.cpp,v $
00024 
00025 
00026 #include <FileStream.h>
00027 #include <Vector.h>
00028 #include <iostream>
00029 #include <iomanip>
00030 #include <ID.h>
00031 #include <Channel.h>
00032 #include <Message.h>
00033 
00034 
00035 using std::cerr;
00036 using std::ios;
00037 using std::setiosflags;
00038 
00039 FileStream::FileStream(int indent)
00040   :OPS_Stream(OPS_STREAM_TAGS_FileStream), 
00041    fileOpen(0), fileName(0), indentSize(indent)
00042 {
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 
00049 
00050 FileStream::FileStream(const char *name, openMode mode, int indent)
00051   :OPS_Stream(OPS_STREAM_TAGS_FileStream), 
00052    fileOpen(0), fileName(0), indentSize(indent)
00053 {
00054   if (indentSize < 1) indentSize = 1;
00055   indentString = new char[indentSize+1];
00056   for (int i=0; i<indentSize; i++)
00057     strcpy(indentString, " ");
00058 
00059   this->setFile(name, mode);
00060 }
00061 
00062 
00063 FileStream::~FileStream()
00064 {
00065   if (fileOpen == 1)
00066     theFile.close();
00067   
00068   if (fileName != 0)
00069     delete [] fileName;
00070 }
00071 
00072 int 
00073 FileStream::setFile(const char *name, openMode mode)
00074 {
00075   if (name == 0) {
00076     std::cerr << "FileStream::setFile() - no name passed\n";
00077     return -1;
00078   }
00079 
00080   // first create a copy of the file name
00081   if (fileName != 0) {
00082     if (strcmp(fileName, name) != 0)
00083       delete [] fileName;
00084     fileName = 0;
00085   }
00086   if (fileName == 0) {
00087     fileName = new char[strlen(name)+1];
00088     if (fileName == 0) {
00089       std::cerr << "FileStream::setFile() - out of memory copying name: " << name << std::endl;
00090       return -1;
00091     }
00092     
00093     // copy the strings
00094     strcpy(fileName, name);
00095   }
00096 
00097   // if file already open, close it
00098   if (fileOpen == 1) {
00099     theFile.close();
00100     fileOpen = 0;
00101   }
00102 
00103   if (mode == OVERWRITE) 
00104     theFile.open(fileName, ios::out);
00105   else
00106     theFile.open(fileName, ios::out| ios::app);
00107 
00108   if (theFile.bad()) {
00109     std::cerr << "WARNING - FileStream::setFile()";
00110     std::cerr << " - could not open file " << fileName << std::endl;
00111 
00112     return -1;
00113   } else
00114     fileOpen = 1;
00115 
00116   if (mode == 0)
00117     theOpenMode = OVERWRITE;
00118   else
00119     theOpenMode = APPEND;
00120 
00121   return 0;
00122 }
00123 
00124 int 
00125 FileStream::open(void)
00126 {
00127   // check setFile has been called
00128   if (fileName == 0) {
00129     std::cerr << "FileStream::open(void) - no file name has been set\n";
00130     return -1;
00131   }
00132 
00133   // if file already open, return
00134   if (fileOpen == 1) {
00135     return 0;
00136   }
00137 
00138   // open file
00139   theFile.open(fileName, ios::out| ios::app);
00140   if (theFile.bad()) {
00141     std::cerr << "WARNING - FileStream::open()";
00142     std::cerr << " - could not open file " << fileName << std::endl;
00143 
00144     return -1;
00145   } else
00146     fileOpen = 1;
00147 
00148   return 0;
00149 }
00150 
00151 int 
00152 FileStream::close(void)
00153 {
00154   if (fileOpen != 0)
00155     theFile.close();
00156   fileOpen = 0;
00157   return 0;
00158 }
00159 
00160 
00161 int 
00162 FileStream::setPrecision(int prec)
00163 {
00164   if (fileOpen != 0)
00165     theFile << std::setprecision(prec);
00166 
00167   return 0;
00168 }
00169 
00170 int 
00171 FileStream::setFloatField(floatField field)
00172 {
00173   if (field == FIXEDD) {
00174     if (fileOpen != 0)
00175       theFile << setiosflags(ios::fixed);
00176   }
00177   else if (field == SCIENTIFIC) {
00178     if (fileOpen != 0)
00179       theFile << setiosflags(ios::scientific);
00180   }
00181 
00182   return 0;
00183 }
00184 
00185 
00186 int 
00187 FileStream::tag(const char *tagName)
00188 {
00189   // output the xml for it to the file
00190   this->indent();
00191   (*this) << tagName << endln;
00192 
00193   numIndent++;
00194 
00195   return 0;
00196 }
00197 
00198 int 
00199 FileStream::tag(const char *tagName, const char *value)
00200 {
00201   // output the xml for it to the file
00202   numIndent++;
00203   this->indent();
00204   (*this) << tagName << " = " << value << endln;
00205 
00206   numIndent--;
00207 
00208   return 0;
00209 }
00210 
00211 
00212 int 
00213 FileStream::endTag()
00214 {
00215   numIndent--;
00216 
00217   return 0;
00218 }
00219 
00220 int 
00221 FileStream::attr(const char *name, int value)
00222 {
00223   this->indent();
00224   (*this) << name << " = " << value << endln;
00225   
00226   return 0;
00227 }
00228 
00229 int 
00230 FileStream::attr(const char *name, double value)
00231 {
00232   this->indent();
00233   (*this) << name << " = " << value << endln;
00234 
00235   return 0;
00236 }
00237 
00238 int 
00239 FileStream::attr(const char *name, const char *value)
00240 {
00241   this->indent();
00242   (*this) << name << " = " << value << endln;
00243 
00244   return 0;
00245 }
00246 
00247 int 
00248 FileStream::write(Vector &data)
00249 {
00250   this->indent();
00251   (*this) << data << endln;  
00252 
00253   return 0;
00254 }
00255 
00256 
00257 
00258 OPS_Stream& 
00259 FileStream::write(const char *s,int n)
00260 {
00261   if (fileOpen != 0)
00262     theFile.write(s, n);
00263 
00264   return *this;
00265 }
00266 
00267 OPS_Stream& 
00268 FileStream::write(const unsigned char*s,int n)
00269 {
00270   if (fileOpen != 0)
00271     theFile.write((const char *) s, n);
00272 
00273   return *this;
00274 }
00275 OPS_Stream& 
00276 FileStream::write(const signed char*s,int n)
00277 {
00278   if (fileOpen != 0)
00279     theFile.write((const char *) s, n);
00280 
00281   return *this;
00282 }
00283 OPS_Stream& 
00284 FileStream::write(const void *s, int n)
00285 {
00286   if (fileOpen != 0)
00287     theFile.write((const char *) s, n);
00288 
00289   return *this;
00290 }
00291 OPS_Stream& 
00292 FileStream::operator<<(char c)
00293 {
00294   if (fileOpen != 0)
00295     theFile << c;
00296 
00297   return *this;
00298 }
00299 OPS_Stream& 
00300 FileStream::operator<<(unsigned char c)
00301 {
00302   if (fileOpen != 0)
00303     theFile << c;
00304 
00305   return *this;
00306 }
00307 OPS_Stream& 
00308 FileStream::operator<<(signed char c)
00309 {
00310   if (fileOpen != 0)
00311     theFile << c;
00312 
00313   return *this;
00314 }
00315 OPS_Stream& 
00316 FileStream::operator<<(const char *s)
00317 {
00318   // note that we do the flush so that a "/n" before
00319   // a crash will cause a flush() - similar to what 
00320   if (fileOpen != 0) {
00321     theFile << s;
00322     theFile.flush();
00323   }
00324 
00325   return *this;
00326 }
00327 OPS_Stream& 
00328 FileStream::operator<<(const unsigned char *s)
00329 {
00330   if (fileOpen != 0)
00331     theFile << s;
00332 
00333   return *this;
00334 }
00335 OPS_Stream& 
00336 FileStream::operator<<(const signed char *s)
00337 {
00338   if (fileOpen != 0)
00339     theFile << s;
00340 
00341   return *this;
00342 }
00343 OPS_Stream& 
00344 FileStream::operator<<(const void *p)
00345 {
00346 /*
00347   if (fileOpen != 0)
00348     theFile << p;
00349 */
00350   return *this;
00351 }
00352 OPS_Stream& 
00353 FileStream::operator<<(int n)
00354 {
00355   if (fileOpen != 0)
00356     theFile << 1.0*n;
00357 
00358   return *this;
00359 }
00360 OPS_Stream& 
00361 FileStream::operator<<(unsigned int n)
00362 {
00363   if (fileOpen != 0)
00364     theFile << 1.0*n;
00365 
00366   return *this;
00367 }
00368 OPS_Stream& 
00369 FileStream::operator<<(long n)
00370 {
00371 /*
00372   if (fileOpen != 0)
00373     theFile << n;
00374 */
00375   return *this;
00376 }
00377 OPS_Stream& 
00378 FileStream::operator<<(unsigned long n)
00379 {
00380 /*
00381   if (fileOpen != 0)
00382     theFile << n;
00383 */
00384   return *this;
00385 }
00386 OPS_Stream& 
00387 FileStream::operator<<(short n)
00388 {
00389 /*
00390   if (fileOpen != 0)
00391     theFile << n;
00392 */
00393   return *this;
00394 }
00395 OPS_Stream& 
00396 FileStream::operator<<(unsigned short n)
00397 {
00398 /*
00399   if (fileOpen != 0)
00400     theFile << n;
00401 */
00402   return *this;
00403 }
00404 OPS_Stream& 
00405 FileStream::operator<<(bool b)
00406 {
00407 /*
00408   if (fileOpen != 0)
00409     theFile << b;
00410 */
00411   return *this;
00412 }
00413 OPS_Stream& 
00414 FileStream::operator<<(double n)
00415 {
00416   if (fileOpen != 0)
00417     theFile << n;
00418 
00419   return *this;
00420 }
00421 OPS_Stream& 
00422 FileStream::operator<<(float n)
00423 {
00424   if (fileOpen != 0)
00425     theFile << n;
00426 
00427   return *this;
00428 }
00429 
00430 
00431 int 
00432 FileStream::sendSelf(int commitTag, Channel &theChannel)
00433 {
00434   static ID idData(2);
00435   int fileNameLength = 0;
00436   if (fileName != 0)
00437     fileNameLength = strlen(fileName);
00438 
00439   idData(0) = fileNameLength;
00440 
00441   if (theOpenMode == OVERWRITE)
00442     idData(1) = 0;
00443   else
00444     idData(1) = 1;
00445 
00446   if (theChannel.sendID(0, commitTag, idData) < 0) {
00447     opserr << "FileStream::sendSelf() - failed to send id data\n";
00448     return -1;
00449   }
00450 
00451   if (fileNameLength != 0) {
00452     Message theMessage(fileName, fileNameLength);
00453     if (theChannel.sendMsg(0, commitTag, theMessage) < 0) {
00454       opserr << "FileStream::sendSelf() - failed to send message\n";
00455       return -1;
00456     }
00457   }
00458 
00459   return 0;
00460 }
00461 
00462 int 
00463 FileStream::recvSelf(int commitTag, Channel &theChannel, FEM_ObjectBroker &theBroker)
00464 {
00465   static ID idData(2);
00466 
00467   if (theChannel.recvID(0, commitTag, idData) < 0) {
00468     opserr << "FileStream::recvSelf() - failed to recv id data\n";
00469     return -1;
00470   }
00471 
00472   int fileNameLength = idData(0);
00473   if (idData(1) == 0)
00474     theOpenMode = OVERWRITE;
00475   else
00476     theOpenMode = APPEND;
00477 
00478   if (fileNameLength != 0) {
00479     if (fileName != 0)
00480       delete [] fileName;
00481     fileName = new char[fileNameLength+5];
00482     if (fileName == 0) {
00483       opserr << "FileStream::recvSelf() - out of memory\n";
00484       return -1;
00485     }
00486 
00487     Message theMessage(fileName, fileNameLength);
00488     if (theChannel.recvMsg(0, commitTag, theMessage) < 0) {
00489       opserr << "FileStream::recvSelf() - failed to recv message\n";
00490       return -1;
00491     }
00492     sprintf(&fileName[fileNameLength],".%d",commitTag);
00493 
00494     if (this->setFile(fileName, theOpenMode) < 0) {
00495       opserr << "FileStream::FileStream() - setFile() failed\n";
00496       if (fileName != 0) {
00497         delete [] fileName;
00498         fileName = 0;
00499       }
00500     }
00501   }
00502   
00503   return 0;
00504 }
00505 
00506 
00507 void
00508 FileStream::indent(void)
00509 {
00510   if (fileOpen != 0)
00511     for (int i=0; i<numIndent; i++)
00512       theFile << indentString;
00513 }

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