00001 #include "LogSensorStream.h" 00002 00003 LogSensorStream::LogSensorStream(const LogReader* _reader, const LogWriter* _writer): 00004 m_index(0), 00005 m_logReader(_reader), 00006 m_logWriter(_writer) 00007 { 00008 m_log.reserve(AVG_SIZE_LOG); 00009 } 00010 00011 LogSensorStream::LogSensorStream(const LogSensorStream& _stream): 00012 m_log(_stream.getLog().size()), 00013 m_index(0), 00014 m_logReader(_stream.getReader()), 00015 m_logWriter(_stream.getWriter()) 00016 { 00017 const std::vector<AbstractReading*> log = _stream.getLog(); 00018 for(unsigned int i = 0; i < log.size(); i++){ 00019 m_log[i] = log[i]->clone(); 00020 } 00021 } 00022 00023 LogSensorStream& LogSensorStream::operator=(const LogSensorStream& _stream){ 00024 m_index = 0; 00025 m_logReader = _stream.getReader(); 00026 m_logWriter = _stream.getWriter(); 00027 00028 for(unsigned int i = 0; i < m_log.size(); i++){ 00029 delete m_log[i]; 00030 } 00031 00032 const std::vector<AbstractReading*> log = _stream.getLog(); 00033 m_log.resize(log.size()); 00034 for(unsigned int i = 0; i < log.size(); i++){ 00035 m_log[i] = log[i]->clone(); 00036 } 00037 return *this; 00038 } 00039 00040 LogSensorStream::~LogSensorStream(){ 00041 for(unsigned int i=0; i < m_log.size(); i++){ 00042 delete m_log[i]; 00043 } 00044 } 00045 00046 const AbstractReading* LogSensorStream::next() const{ 00047 return m_log[m_index++]; 00048 } 00049 00050 00051 const AbstractReading* LogSensorStream::current() const{ 00052 return m_log[m_index]; 00053 } 00054 00055 bool LogSensorStream::seek(const unsigned int _position, SensorStreamOffset _offset){ 00056 unsigned int position = (_offset == BEGIN)? _position : m_log.size() -_position - 1; 00057 bool result = position >= 0 && position < m_log.size(); 00058 m_index = result ? position : m_index; 00059 return result; 00060 } 00061 00062 bool LogSensorStream::end() const{ 00063 return m_index >= m_log.size(); 00064 } 00065 00066 00067 void LogSensorStream::load(const std::string& _filename){ 00068 std::ifstream infile; 00069 infile.open(_filename.c_str()); 00070 load(infile); 00071 infile.close(); 00072 } 00073 00074 void LogSensorStream::save(const std::string& _filename){ 00075 std::ofstream outfile; 00076 outfile.open(_filename.c_str()); 00077 save(outfile); 00078 outfile.close(); 00079 } 00080 00081 void LogSensorStream::load(std::istream& _stream){ 00082 m_logReader->readLog(_stream, m_log); 00083 } 00084 00085 void LogSensorStream::save(std::ostream& _stream){ 00086 m_logWriter->writeLog(_stream, m_log); 00087 }