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

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