00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "FileReadoutFormat/FileNameStrings.h"
00010
00011 #include "DaqReadoutFormat/ByteBuffer.h"
00012 #include "FileReadoutFormat/FileTraits.h"
00013 #include <cstring>
00014
00015 using DybDaq::ByteBuffer;
00016 using DybDaq::FileNameStrings;
00017 using DybDaq::FileTraits;
00018 using std::string;
00019
00020 FileNameStrings::FileNameStrings(const std::string application,
00021 const std::string fileBase,
00022 const FileTraits& traits) :
00023 FileBuffer(new char[traits.recordSize(FileTraits::kFileNameStrings) * kBytesInInt],
00024 traits,
00025 FileTraits::kFileNameStrings),
00026 m_application(0),
00027 m_fileBase(0),
00028 m_stringBuffer(0) {
00029 setNameStrings(application,
00030 fileBase);
00031 }
00032
00033
00034
00035 FileNameStrings::FileNameStrings(const ByteBuffer& byteBuffer,
00036 const FileTraits& traits) :
00037 FileBuffer(byteBuffer,
00038 traits),
00039 m_application(0),
00040 m_fileBase(0),
00041 m_stringBuffer(0) {
00042 }
00043
00044 FileNameStrings::~FileNameStrings() {
00045 if (0 != m_stringBuffer) {
00046 delete m_stringBuffer;
00047 }
00048 if (0 != m_fileBase) {
00049 delete m_fileBase;
00050 }
00051 if (0 != m_application) {
00052 delete m_application;
00053 }
00054 }
00055
00056 bool FileNameStrings::isMarked(unsigned int marker) const {
00057 return FileTraits::kFileNameStrings == marker;
00058 }
00059
00060 unsigned int FileNameStrings::firstLength() const {
00061 return readUnsignedInt(FileTraits::kFirstNameLength);
00062 }
00063
00064 unsigned int FileNameStrings::formatComponent() const {
00065 return FileTraits::kFileNameStrings;
00066 }
00067
00068 string FileNameStrings::application() const {
00069 if (0 == m_application) {
00070 const char* start;
00071 if (hasByteBuffer()) {
00072 start = buffer() + (fileTraits().firstNameLengthOffset() * kBytesInInt);
00073 } else {
00074 start = m_stringBuffer;
00075 }
00076 m_application = new string(readString(start));
00077
00078 unsigned int appLength = m_application->size();
00079 unsigned int paddedAppLength = appLength + (kBytesInInt - 1) - ((appLength - 1) % kBytesInInt );
00080 start += paddedAppLength + kBytesInInt;
00081 m_fileBase = new string(readString(start));
00082 }
00083 return *m_application;
00084 }
00085
00086 string FileNameStrings::fileBase() const {
00087 if (0 == m_application) {
00088 application();
00089 }
00090 return *m_fileBase;
00091 }
00092
00093 unsigned int FileNameStrings::gather(OutputBufferList& outputBuffers) const {
00094 if (hasByteBuffer()) {
00095 return FileBuffer::gather(outputBuffers);
00096 }
00097 const unsigned int fixedLength = fileTraits().recordSize(FileTraits::kFileNameStrings) * kBytesInInt;
00098 outputBuffers.push_back(OutputBuffer(buffer(),
00099 fixedLength));
00100 unsigned int stringsLength = (size() * kBytesInInt) - fixedLength;
00101 outputBuffers.push_back(OutputBuffer(m_stringBuffer,
00102 stringsLength));
00103 return fixedLength + stringsLength;
00104 }
00105
00106 void FileNameStrings::setNameStrings(const string application,
00107 const string fileBase) {
00108 unsigned int stringsLength = calculateStringsLength(application,
00109 fileBase);
00110 if (0 != m_stringBuffer) {
00111 delete m_stringBuffer;
00112 }
00113 m_stringBuffer = new char[stringsLength];
00114 setSize(fileTraits().recordSize(FileTraits::kFileNameStrings) + (stringsLength / kBytesInInt));
00115 char* start = m_stringBuffer;
00116 start += writeString(application,
00117 start);
00118 writeString(fileBase,
00119 start);
00120 if (0 != m_fileBase) {
00121 delete m_fileBase;
00122 m_fileBase = 0;
00123 }
00124 if (0 != m_application) {
00125 delete m_application;
00126 m_application = 0;
00127 }
00128 }
00129
00130 string FileNameStrings::readString(const char* buffer) {
00131 unsigned int length = *((unsigned int*)(buffer));
00132 char* base = new char[length + 1];
00133 memcpy(base,
00134 buffer + kBytesInInt,
00135 length);
00136 base[length] = 0;
00137 string result(base);
00138 delete base;
00139 return result;
00140 }
00141
00142 unsigned int FileNameStrings::calculateStringsLength(const string application,
00143 const string fileBase) {
00144 unsigned int result = 0;
00145 unsigned int length = application.size();
00146 unsigned int paddedLength = length + (kBytesInInt - 1) - ((length - 1) % kBytesInInt );
00147 result += paddedLength + kBytesInInt;
00148 length = fileBase.size();
00149 paddedLength = length + (kBytesInInt - 1) - ((length - 1) % kBytesInInt );
00150 result += paddedLength + kBytesInInt;
00151 return result;
00152 }
00153
00154 unsigned int FileNameStrings::writeString(const string& value,
00155 char* buffer) {
00156 unsigned int length = value.size();
00157 char* start = buffer;
00158 *((unsigned int*)(start)) = length;
00159 start += kBytesInInt;
00160 memcpy(start,
00161 value.data(),
00162 length);
00163 unsigned int padding = (kBytesInInt - 1) - ((length - 1) % kBytesInInt );
00164 start += length;
00165 for (unsigned int index = 0;
00166 index != padding;
00167 ++index) {
00168 *start = ' ';
00169 ++start;
00170 }
00171 return kBytesInInt + length + padding;
00172 }
00173