| 1187 | *******************************************************************/ |
| 1188 | |
| 1189 | int |
| 1190 | FileDatastore::openFile(char *fileName, FileDatastoreOutputFile *theFileStruct, int dataSize) |
| 1191 | { |
| 1192 | fstream *res = new fstream(); |
| 1193 | if (res == 0) { |
| 1194 | opserr << "FileDatastore::openFile - out of memory; failed to open file: " << fileName << endln; |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | res->open(fileName, ios::in | ios::out | ios::binary); |
| 1199 | |
| 1200 | // if file did not exist, need to pass trunc flag to open it |
| 1201 | if (res->bad() == true || res->is_open() == false) { |
| 1202 | // delete & new again for unix gcc compiler to work! |
| 1203 | delete res; |
| 1204 | res = new fstream(); |
| 1205 | if (res == 0) { |
| 1206 | opserr << "FileDatastore::openFile - out of memory; failed to open file: " << fileName << endln; |
| 1207 | theFileStruct->theFile = res; |
| 1208 | return -1; |
| 1209 | } |
| 1210 | res->open(fileName, ios::in | ios::out | ios::trunc | ios::binary); |
| 1211 | } |
| 1212 | |
| 1213 | if (res->bad() == true || res->is_open() == false) { |
| 1214 | opserr << "FATAL - FileDatastore::openFile() - could not open file " << fileName << endln; |
| 1215 | delete res; |
| 1216 | theFileStruct->theFile = 0; |
| 1217 | return -1; |
| 1218 | } |
| 1219 | |
| 1220 | // set the position for writing to eof |
| 1221 | res->seekp(0,ios::end); |
| 1222 | STREAM_POSITION_TYPE fileEnd = res->tellp(); |
| 1223 | int maxDataTag = 0; |
| 1224 | |
| 1225 | if (fileEnd == 0 || fileEnd == -1) { |
| 1226 | *(theIntData.dbTag) = maxDataTag; |
| 1227 | res->write(data, sizeof(int)); |
| 1228 | fileEnd = sizeof(int); |
| 1229 | maxDataTag = -1; |
| 1230 | } else { |
| 1231 | res->seekg(0, ios::beg); |
| 1232 | res->read(data, sizeof(int)); |
| 1233 | maxDataTag = *(theIntData.dbTag); |
| 1234 | } |
| 1235 | |
| 1236 | // move to start of data part |
| 1237 | res->seekp(sizeof(int), ios::beg); |
| 1238 | res->seekg(sizeof(int), ios::beg); |
| 1239 | |
| 1240 | // fill in the structure data |
| 1241 | theFileStruct->theFile = res; |
| 1242 | theFileStruct->fileEnd = fileEnd; |
| 1243 | |
| 1244 | theFileStruct->maxDbTag = maxDataTag; |
| 1245 | |
| 1246 | return 0; |
no test coverage detected