00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "FileReadoutFormat/DaqToFStream.h"
00010
00011 #include "DaqReadoutFormat/DaqBuffer.h"
00012 #include "FileReadoutFormat/FileEndRecord.h"
00013 #include "FileReadoutFormat/FileStartRecord.h"
00014 #include "FileReadoutFormat/FileTraits.h"
00015
00016 using DybDaq::FileEndRecord;
00017 using DybDaq::FileStartRecord;
00018 using DybDaq::FileTraits;
00019 using DybDaq::DaqToFStream;
00020 using std::fstream;
00021 using std::ios;
00022 using std::ofstream;
00023 using std::ostream;
00024 using std::string;
00025
00026 DaqToFStream::DaqToFStream(const FileTraits& traits) :
00027 DaqToOStream(traits),
00028 m_ofstream(0),
00029 m_fileDir(0),
00030 m_fileName(0) {
00031 }
00032
00033 DaqToFStream::DaqToFStream(const FileTraits& traits,
00034 std::string& fileDir) :
00035 DaqToOStream(traits),
00036 m_ofstream(0),
00037 m_fileDir(0),
00038 m_fileName(0) {
00039 if (0 != &fileDir) {
00040 m_fileDir = new string(".");
00041 }
00042 }
00043
00044 DaqToFStream::~DaqToFStream() {
00045 if (0 != m_fileDir) {
00046 delete m_fileDir;
00047 }
00048 if (0 != m_fileName) {
00049 delete m_fileName;
00050 }
00051 if (hasOStream()) {
00052 if (m_ofstream->is_open()) {
00053 m_ofstream->close();
00054 }
00055 }
00056 }
00057
00058 bool DaqToFStream::openFile(const string& fileName,
00059 const FileStartRecord& record) {
00060 if (hasFileBase()) {
00061 return false;
00062 }
00063 if (0 != m_fileName) {
00064 delete m_fileName;
00065 }
00066 m_fileName = new string(fileName);
00067 return open(record);
00068 }
00069
00070 const FileStartRecord* DaqToFStream::openFile(const string& fileName,
00071 const unsigned int fileNumber,
00072 const FileTraits::FileDateTime& dateTime,
00073 const unsigned int dataBlockLimit,
00074 const unsigned int megabyteLimit) {
00075 if (hasFileBase()) {
00076 return 0;
00077 }
00078 if (0 != m_fileName) {
00079 delete m_fileName;
00080 }
00081 m_fileName = new string(fileName);
00082 return openElement(fileNumber,
00083 dateTime,
00084 dataBlockLimit,
00085 megabyteLimit);
00086 }
00087
00088 void DaqToFStream::prepareNext() {
00089
00090 const string& fileBase = getFileBase();
00091 if (0 == &fileBase) {
00092 return;
00093 }
00094
00095 unsigned int segmentCount = getFileCount() + 1;
00096 char* segmentSuffix = new char[11];
00097 sprintf(segmentSuffix,
00098 "_%.4d.data",
00099 segmentCount);
00100 segmentSuffix[10] = 0;
00101 string fileName = getFileBase() + segmentSuffix;
00102 delete[] segmentSuffix;
00103 if (0 != m_fileDir) {
00104 fileName = *m_fileDir + "/" + fileName;
00105 }
00106 if (0 != m_fileName) {
00107 delete m_fileName;
00108 }
00109 m_fileName = new string(fileName);
00110 }
00111
00112 ostream* DaqToFStream::openStream() {
00113 if (0 == m_fileName) {
00114 prepareNext();
00115 }
00116 m_ofstream = new std::ofstream(m_fileName->c_str(),
00117 fstream::binary | fstream::out | fstream::trunc);
00118 if (0 != m_fileName) {
00119 delete m_fileName;
00120 }
00121 m_fileName = 0;
00122 return m_ofstream;
00123 }
00124
00125 void DaqToFStream::closeStream(std::ostream& stream) {
00126 ofstream& fileStream = dynamic_cast<ofstream&> (stream);
00127 fileStream.close();
00128 m_ofstream = 0;
00129 }
00130