00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
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
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
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 }