You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
4.8 KiB
C++

/***************************************************************************
* Copyright (C) 2012 by gempa GmbH *
* *
* All Rights Reserved. *
* *
* NOTICE: All information contained herein is, and remains *
* the property of gempa GmbH and its suppliers, if any. The intellectual *
* and technical concepts contained herein are proprietary to gempa GmbH *
* and its suppliers. *
* Dissemination of this information or reproduction of this material *
* is strictly forbidden unless prior written permission is obtained *
* from gempa GmbH. *
***************************************************************************/
#include <gempa/caps/anypacket.h>
#include <gempa/caps/riff.h>
#include <gempa/caps/utils.h>
#include <streambuf>
#include <iostream>
#include <cstring>
namespace Gempa {
namespace CAPS {
bool AnyDataRecord::AnyHeader::put(std::streambuf &buf) const {
Endianess::Writer put(buf);
put(type, sizeof(type)-1);
dataHeader.put(buf);
put(endTime.year);
put(endTime.yday);
put(endTime.hour);
put(endTime.minute);
put(endTime.second);
put(endTime.usec);
return put.good;
}
AnyDataRecord::AnyDataRecord() {
strncpy(_header.type, "ANY", sizeof(_header.type));
_header.dataHeader.samplingFrequencyDenominator = 0;
_header.dataHeader.samplingFrequencyNumerator = 0;
// Just a bunch of bytes
_header.dataHeader.dataType = DT_INT8;
}
bool AnyDataRecord::setType(const char *type) {
strncpy(_header.type, type, sizeof(_header.type));
// Input clipped?
if ( _header.type[sizeof(_header.type)-1] != '\0' ) {
_header.type[sizeof(_header.type)-1] = '\0';
return false;
}
return true;
}
const char *AnyDataRecord::type() const {
return _header.type;
}
void AnyDataRecord::setStartTime(const Time &time) {
timeToTimestamp(_header.dataHeader.samplingTime, time);
_startTime = time;
}
void AnyDataRecord::setEndTime(const Time &time) {
timeToTimestamp(_header.endTime, time);
_endTime = time;
}
void AnyDataRecord::setSamplingFrequency(uint16_t numerator, uint16_t denominator) {
_header.dataHeader.samplingFrequencyNumerator = numerator;
_header.dataHeader.samplingFrequencyDenominator = denominator;
}
const char *AnyDataRecord::formatName() const {
return "ANY";
}
bool AnyDataRecord::readMetaData(std::streambuf &buf, int size,
Header &header,
Time &startTime, Time &endTime) {
// Read record type
buf.sgetn(_header.type, 4);
_header.type[sizeof(_header.type)-1] = '\0';
size -= sizeof(_header.type)-1;
if ( !header.get(buf) ) return false;
TimeStamp tmp;
Endianess::Reader get(buf);
get(tmp.year);
get(tmp.yday);
get(tmp.hour);
get(tmp.minute);
get(tmp.second);
get(tmp.usec);
startTime = timestampToTime(header.samplingTime);
endTime = timestampToTime(tmp);
return true;
}
const DataRecord::Header *AnyDataRecord::header() const {
return &_header.dataHeader;
}
Time AnyDataRecord::startTime() const {
return _startTime;
}
Time AnyDataRecord::endTime() const {
return _endTime;
}
bool AnyDataRecord::canTrim() const {
return false;
}
bool AnyDataRecord::canMerge() const {
return false;
}
bool AnyDataRecord::trim(const Time &start,
const Time &end) const {
return false;
}
size_t AnyDataRecord::dataSize(bool withHeader) const {
if ( withHeader )
return _data.size() + _header.dataSize();
else
return _data.size();
}
DataRecord::ReadStatus AnyDataRecord::get(std::streambuf &buf, int size,
const Time &start, const Time &end,
int) {
_data.clear();
size -= _header.dataSize();
if ( size < 0 ) return RS_Error;
if ( !_header.get(buf) ) return RS_Error;
_startTime = timestampToTime(_header.dataHeader.samplingTime);
_endTime = timestampToTime(_header.endTime);
if ( start.valid() ) {
if ( _endTime < start || (_startTime < start && _endTime == start) )
return RS_BeforeTimeWindow;
}
if ( end.valid() ) {
if ( _startTime >= end ) return RS_AfterTimeWindow;
}
RIFF::VectorChunk<1,false> dataChunk(_data, 0, size);
if ( !dataChunk.get(buf, size) ) return RS_Error;
return RS_Complete;
}
bool AnyDataRecord::put(std::streambuf &buf, bool withHeader) const {
if ( withHeader && !_header.put(buf) ) return false;
return (int)buf.sputn(_data.data(), _data.size()) == (int)_data.size();
}
void AnyDataRecord::setData(char *data, size_t size) {
_data.resize(size);
memcpy(_data.data(), data, size);
}
}
}