DataOutputFileHandler.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.2 $
00022 // $Date: 2004/11/24 22:40:16 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/handler/DataOutputFileHandler.cpp,v $
00024                                                                         
00025 // Written: fmk 
00026 // Date: 10/04
00027 //
00028 // Description: This file contains the class implementation for
00029 // DataOutputFileHandler. 
00030 //
00031 // What: "@(#) DataOutputFileHandler.C, revA"
00032 
00033 #include "DataOutputFileHandler.h"
00034 #include <Vector.h>
00035 #include <ID.h>
00036 #include <Channel.h>
00037 #include <Message.h>
00038 
00039 DataOutputFileHandler::DataOutputFileHandler(const char *theFileName, 
00040                                              echoMode theEMode, 
00041                                              openMode theOMode)
00042   :DataOutputHandler(DATAHANDLER_TAGS_DataOutputFileHandler),
00043    fileName(0), theEchoMode(theEMode), theOpenMode(theOMode), numColumns(-1)
00044 {
00045   if (theFileName != 0) {
00046     fileName = new char [strlen(theFileName)+1];
00047     if (fileName == 0) {
00048     opserr << "DataOutputFileHandler::DataOutputFileHandler() - out of memory\n";      
00049     } else 
00050       strcpy(fileName, theFileName);
00051   }
00052 
00053   if (fileName != 0 && outputFile.setFile(fileName, theOpenMode) < 0) {
00054     opserr << "DataOutputFileHandler::DataOutputFileHandler() - setFile() failed\n";
00055     if (fileName != 0) {
00056       delete [] fileName;
00057       fileName = 0;
00058     }
00059   }
00060 }
00061 
00062 DataOutputFileHandler::~DataOutputFileHandler()
00063 {
00064   if (fileName != 0)
00065     delete [] fileName;
00066 }
00067 
00068 int 
00069 DataOutputFileHandler::open(char **dataDescription, int numData)
00070 {
00071   if (fileName == 0) {
00072     opserr << "DataOutputFileHandler::open() - no filename passed in constructor\n";
00073     return -1;
00074   }
00075 
00076   if (dataDescription == 0) {
00077     opserr << "DataOutputFileHandler::open() - no column description data passed\n";
00078     return -1;
00079   }
00080 
00081   if (numData < 0) {
00082     opserr << "DataOutputFileHandler::open() - numColumns (" << numData << ") < 0\n";
00083     return -1;
00084   } else
00085     numColumns = numData;
00086 
00087 
00088   if (theEchoMode == DATA_FILE) {
00089     for (int i=0; i<numData; i++)
00090       outputFile << dataDescription[i] << " ";
00091     outputFile << endln;
00092 
00093   } else if (theEchoMode == XML_FILE) {
00094 
00095     // create a copy of the  file name
00096     int res = 0;
00097     const char *name = outputFile.getFileName();
00098     int fileNameLength = strlen(name);
00099     char *xmlFileName = new char[fileNameLength + 5];
00100     
00101     if (xmlFileName == 0) {
00102       opserr << "DataOutputFileHandler::open - out of memory creating copy of string " << xmlFileName << endln;
00103       return -1;
00104   }
00105     
00106     strcpy(xmlFileName, name);
00107     if (fileNameLength > 4 && strcmp(".out", &name[fileNameLength-4]) == 0) {
00108       xmlFileName[fileNameLength-4] = '\0';
00109     }
00110     
00111     strcat(xmlFileName,".xml");
00112     
00113     FileStream xmlFile;
00114     if (xmlFile.setFile(xmlFileName, OVERWRITE) == 0) {
00115       
00116       // write the xml data
00117       xmlFile << "<?xml version=\"1.0\"?>\n";
00118       xmlFile << "<NumericalFileDataDescription>\n";
00119       xmlFile << "\t<DataFile>\n";
00120       xmlFile << "\t\t<DataFileName> " << name << "</DataFileName>\n";
00121       xmlFile << "\t\t<NumberDataColumns> " << numData << "</NumberDataColumns>\n";
00122       xmlFile << "\t</DataFile>\n";
00123       for (int i=0; i<numData; i++) {
00124         xmlFile << "\t<DataColumnDescription>\n";
00125         xmlFile << "\t\t<ColumnLocation> " << i+1 << "</ColumnLocation>\n";      
00126         xmlFile << "\t\t<Description> " << dataDescription[i] << "</Description>\n";
00127         xmlFile << "\t</DataColumnDescription>\n";
00128       }
00129       xmlFile << "</NumericalFileDataDescription>\n";
00130       xmlFile.close();
00131       
00132     } else {
00133       opserr << "DataOutputFileHandler::open - failed to open cml file: " << xmlFileName << endln;
00134       delete [] xmlFileName;
00135       res = -1;
00136     }
00137     
00138     // no longer need xmlFileName
00139     delete [] xmlFileName;
00140   }
00141 
00142   return 0;
00143 }
00144 
00145 int 
00146 DataOutputFileHandler::write(Vector &data) 
00147 {
00148   if (fileName == 0 || numColumns < 0) {
00149     opserr << "DataOutputFileHandler::write() - no filename or data description has been set\n";
00150     return -1;
00151   }
00152 
00153   if (data.Size() == numColumns)
00154     outputFile << data;
00155   else {
00156     opserr << fileName;
00157     opserr << "DataOutputStreamHandler::write() - Vector not of correct size\n";
00158     return -1;
00159   }
00160 
00161   return 0;
00162 }
00163  
00164 int 
00165 DataOutputFileHandler::sendSelf(int commitTag, Channel &theChannel)
00166 {
00167   static ID idData(3);
00168   int fileNameLength = 0;
00169   if (fileName != 0)
00170     fileNameLength = strlen(fileName);
00171 
00172   idData(0) = fileNameLength;
00173 
00174   if (theOpenMode == OVERWRITE)
00175     idData(1) = 0;
00176   else
00177     idData(1) = 1;
00178 
00179   if (theEchoMode == NONE)
00180     idData(2) = 0;
00181   else if (theEchoMode == DATA_FILE)
00182     idData(2) = 1;
00183   else
00184     idData(2) = 2;
00185 
00186   if (theChannel.sendID(0, commitTag, idData) < 0) {
00187     opserr << "DataOutputFileHandler::sendSelf() - failed to send id data\n";
00188     return -1;
00189   }
00190 
00191   if (fileNameLength != 0) {
00192     Message theMessage(fileName, fileNameLength);
00193     if (theChannel.sendMsg(0, commitTag, theMessage) < 0) {
00194       opserr << "DataOutputFileHandler::sendSelf() - failed to send message\n";
00195       return -1;
00196     }
00197   }
00198 
00199   return 0;
00200 }
00201 
00202 int 
00203 DataOutputFileHandler::recvSelf(int commitTag, Channel &theChannel, FEM_ObjectBroker &theBroker)
00204 {
00205   static ID idData(3);
00206 
00207   if (theChannel.recvID(0, commitTag, idData) < 0) {
00208     opserr << "DataOutputFileHandler::recvSelf() - failed to recv id data\n";
00209     return -1;
00210   }
00211 
00212   int fileNameLength = idData(0);
00213   if (idData(1) == 0)
00214     theOpenMode = OVERWRITE;
00215   else
00216     theOpenMode = APPEND;
00217 
00218 
00219   if (idData(2) == 0)
00220     theEchoMode = NONE;
00221   else if (idData(2) == 1)
00222     theEchoMode = DATA_FILE;
00223   else
00224     theEchoMode = XML_FILE;
00225 
00226   if (fileNameLength != 0) {
00227     if (fileName != 0)
00228       delete [] fileName;
00229     fileName = new char[fileNameLength+5];
00230     if (fileName == 0) {
00231       opserr << "DataOutputFileHandler::recvSelf() - out of memory\n";
00232       return -1;
00233     }
00234 
00235     Message theMessage(fileName, fileNameLength);
00236     if (theChannel.recvMsg(0, commitTag, theMessage) < 0) {
00237       opserr << "DataOutputFileHandler::recvSelf() - failed to recv message\n";
00238       return -1;
00239     }
00240     sprintf(&fileName[fileNameLength],".%d",commitTag);
00241 
00242     if (fileName != 0 && outputFile.setFile(fileName, theOpenMode) < 0) {
00243       opserr << "DataOutputFileHandler::DataOutputFileHandler() - setFile() failed\n";
00244       if (fileName != 0) {
00245         delete [] fileName;
00246         fileName = 0;
00247       }
00248     }
00249   }
00250   return 0;
00251 }

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