diff --git a/SleepLib/binary_file.cpp b/SleepLib/binary_file.cpp deleted file mode 100644 index 94ffa553..00000000 --- a/SleepLib/binary_file.cpp +++ /dev/null @@ -1,332 +0,0 @@ -/* - -SleepLib BinaryFile Implementation - -Author: Mark Watkins -License: GPL - -*/ - -//#include -#include "binary_file.h" - -bool IsPlatformLittleEndian() -{ - quint32 j=1; - *((char*)&j) = 0; - return j!=1; -} - -BinaryFile::BinaryFile() -{ - size=pos=0; - buffer=NULL; -} -void BinaryFile::Close() -{ - if (bf_mode==BF_WRITE) { - //f.Write(buffer,pos); - } - f.close(); - if (buffer) delete [] buffer; - size=pos=0; -} -BinaryFile::~BinaryFile() -{ - Close(); -} -size_t BinaryFile::Write(const char * buffer, size_t count) -{ - return f.write(buffer,count); -} - -bool BinaryFile::Open(QString filename,BFOpenMode mode) -{ - pos=size=0; - bf_mode=mode; - f.setFileName(filename); - if (f.exists()) { - if (mode==BF_WRITE) { - // hmm.. file exists. unsure of best course of action here.. - // return false; - } - size=f.size(); - } - QIODevice::OpenMode om; - if (mode==BF_READ) om=QIODevice::ReadOnly; - else if (mode==BF_WRITE) om=QIODevice::WriteOnly; - if (!f.open(om)) return false; - - if (mode==BF_READ) { - buffer=new char [size]; - size=f.read(buffer,size); - } - return true; -} -bool BinaryFile::Unpack(qint8 & data) -{ - if (pos>=size) return false; - data=((qint8 *)buffer)[pos++]; - return true; -} -bool BinaryFile::Unpack(bool & data) -{ - if (pos>=size) return false; - data=((qint8 *)buffer)[pos++]; - return true; -} - -bool BinaryFile::Unpack(qint16 & data) -{ - if (pos+1>=size) return false; - data=((quint8 *)buffer)[pos++]; - data|=((qint8 *)buffer)[pos++] << 8; - return true; -} -bool BinaryFile::Unpack(qint32 & data) -{ - if (pos+3>=size) return false; - if (IsPlatformLittleEndian()) { - data=*((qint32 *)(&buffer[pos])); - pos+=4; - } else { - data=((quint8 *)buffer)[pos++]; - data|=((quint8 *)buffer)[pos++] << 8; - data|=((quint8 *)buffer)[pos++] << 16; - data|=((qint8 *)buffer)[pos++] << 24; - - } - return true; -} -bool BinaryFile::Unpack(qint64 & data) -{ - if (pos+7>=size) return false; - - if (IsPlatformLittleEndian()) { - data=*((qint64 *)(&buffer[pos])); - pos+=8; - } else { - //for (int i=7;i>=0;i--) data=(data << 8) | ((quint8 *)buffer))[pos+i]; - // pos+=8; - data=qint64(((quint8 *)buffer)[pos++]); - data|=qint64(((quint8 *)buffer)[pos++]) << 8; - data|=qint64(((quint8 *)buffer)[pos++]) << 16; - data|=qint64(((quint8 *)buffer)[pos++]) << 24; - data|=qint64(((quint8 *)buffer)[pos++]) << 32; - data|=qint64(((quint8 *)buffer)[pos++]) << 40; - data|=qint64(((quint8 *)buffer)[pos++]) << 48; - data|=qint64(((qint8 *)buffer)[pos++]) << 56; - - } - - return true; -} -bool BinaryFile::Unpack(quint8 & data) -{ - if (pos>=size) return false; - data=((qint8 *)buffer)[pos++]; - return true; -} -bool BinaryFile::Unpack(quint16 & data) -{ - if (pos+1>=size) return false; - data=quint16(((quint8 *)buffer)[pos++]); - data|=quint16(((quint8 *)buffer)[pos++]) << 8; - - return true; -} -bool BinaryFile::Unpack(quint32 & data) -{ - if (pos>=size) return false; - if (IsPlatformLittleEndian()) { - data=*((quint32 *)(&buffer[pos])); - pos+=4; - } else { - data=quint32(((quint8 *)buffer)[pos++]); - data|=quint32(((quint8 *)buffer)[pos++]) << 8; - data|=quint32(((quint8 *)buffer)[pos++]) << 16; - data|=quint32(((quint8 *)buffer)[pos++]) << 24; - } - - return true; -} -bool BinaryFile::Unpack(quint64 & data) -{ - if (pos>=size) return false; - if (IsPlatformLittleEndian()) { - data=*((qint64 *)(&buffer[pos])); - pos+=8; - } else { - //for (int i=7;i>=0;i--) data=(data << 8) | ((quint8 *)buffer))[pos+i]; - // pos+=8; - data=quint64(((quint8 *)buffer)[pos++]); - data|=quint64(((quint8 *)buffer)[pos++]) << 8; - data|=quint64(((quint8 *)buffer)[pos++]) << 16; - data|=quint64(((quint8 *)buffer)[pos++]) << 24; - data|=quint64(((quint8 *)buffer)[pos++]) << 32; - data|=quint64(((quint8 *)buffer)[pos++]) << 40; - data|=quint64(((quint8 *)buffer)[pos++]) << 48; - data|=quint64(((quint8 *)buffer)[pos++]) << 56; - } - - return true; -} -bool BinaryFile::Unpack(float & data) -{ - if ((pos+4)>=size) return false; - if (IsPlatformLittleEndian()) { - data=*((float *)(&buffer[pos])); - pos+=4; - } else { - unsigned char b[4]; - for (int i=0; i<4; i++) { - b[3-i]=buffer[pos+i]; - } - data=*((float *)b); - pos+=4; - } - - return true; -} -bool BinaryFile::Unpack(double & data) -{ - if ((pos+7)>=size) return false; - if (IsPlatformLittleEndian()) { - data=*((double *)(&buffer[pos])); - pos+=8; - } else { - unsigned char b[8]; - for (int i=0; i<8; i++) { - b[7-i]=buffer[pos+i]; - } - data=*((double *)b); - pos+=8; - } - - return true; -} -bool BinaryFile::Unpack(QString & data) -{ - qint16 i16,t16; - if (!Unpack(i16)) return false; - if ((pos+(i16*2))>size) return false; - data=""; - QChar c; - for (int i=0; i -License: GPL - -*/ - -#ifndef BINARY_FILE_H -#define BINARY_FILE_H - -//#include -//#include - -#include -#include -#include - -enum BFOpenMode { BF_READ, BF_WRITE }; -const long max_buffer_size=1048576*2; -bool IsPlatformLittleEndian(); - -class UnpackError -{ - -}; - -class PackBuffer -{ -public: - PackBuffer(); - ~PackBuffer(); - bool Open(QString filename,BFOpenMode mode); - void Close(); - -}; - -class BinaryFile -{ -public: - BinaryFile(); - ~BinaryFile(); - bool Open(QString filename,BFOpenMode mode); - void Close(); - - bool Unpack(bool & data); - bool Unpack(qint8 & data); - bool Unpack(qint16 & data); - bool Unpack(qint32 & data); - bool Unpack(qint64 & data); - bool Unpack(quint8 & data); - bool Unpack(quint16 & data); - bool Unpack(quint32 & data); - bool Unpack(quint64 & data); - bool Unpack(float & data); - bool Unpack(double & data); - bool Unpack(QString & data); - bool Unpack(QDateTime & data); - - bool Pack(const bool &data); - bool Pack(const qint8 &data); - bool Pack(const qint16 & data); - bool Pack(const qint32 & data); - bool Pack(const qint64 & data); - bool Pack(const quint8 & data); - bool Pack(const quint16 & data); - bool Pack(const quint32 & data); - bool Pack(const quint64 & data); - bool Pack(const float & data); - bool Pack(const double & data); - bool Pack(const QString & data); - bool Pack(const QDateTime & data); - - size_t Write(const char * buffer, size_t count); - -protected: - BFOpenMode bf_mode; - QFile f; - char * buffer; - int buff_read; - long pos; - QString bf_filename; - long size; -}; - -#endif //BINARY_FILE_H diff --git a/SleepLib/loader_plugins/prs1_loader.cpp b/SleepLib/loader_plugins/prs1_loader.cpp index 81551b7a..05084530 100644 --- a/SleepLib/loader_plugins/prs1_loader.cpp +++ b/SleepLib/loader_plugins/prs1_loader.cpp @@ -932,7 +932,7 @@ bool PRS1Loader::OpenWaveforms(Session *session,QString filename) if (header[0]!=PRS1_MAGIC_NUMBER) { if (cnt==0) return false; - qWarning() << "Corrupt waveform, trying to recover"; + qWarning() << "Corrupt waveform, trying to recover" << sequence; // read the damn bytes anyway.. br=f.read((char *)header,lasthl-hl+1); // last bit of the header @@ -1015,11 +1015,11 @@ bool PRS1Loader::OpenWaveforms(Session *session,QString filename) } else { qint32 diff=timestamp-expected_timestamp; if (diff<0) { - if (durationabs(diff)) { + duration+=diff; // really Subtracting.. samples+=diff*5; } else { - qWarning() << "Waveform out of sync beyond the first entry" << sequence; + qWarning() << "Waveform out of sync beyond the first entry" << sequence << duration << diff; } } else if (diff>0) { qDebug() << "Fixing up Waveform sync" << sequence; diff --git a/SleepLib/loader_plugins/prs1_loader.h b/SleepLib/loader_plugins/prs1_loader.h index f98aa19b..435edf0c 100644 --- a/SleepLib/loader_plugins/prs1_loader.h +++ b/SleepLib/loader_plugins/prs1_loader.h @@ -21,7 +21,7 @@ License: GPL //******************************************************************************************** // Please INCREMENT the following value when making changes to this loaders implementation. // -const int prs1_data_version=1; +const int prs1_data_version=2; // //******************************************************************************************** diff --git a/SleepLib/machine.cpp b/SleepLib/machine.cpp index afd3e69d..44138fd0 100644 --- a/SleepLib/machine.cpp +++ b/SleepLib/machine.cpp @@ -11,7 +11,6 @@ #include #include #include -#include "binary_file.h" #include "machine.h" #include "profiles.h" #include @@ -337,8 +336,9 @@ bool Machine::Purge(int secret) for (int i=0;ifirst); - try { - if (sess->LoadSummary(s->second[0])) { - sess->SetEventFile(s->second[1]); - sess->SetWaveFile(s->second[2]); - - AddSession(sess,profile); - } else { - delete sess; - } - } catch(UnpackError e) { + if (sess->LoadSummary(s->second[0])) { + sess->SetEventFile(s->second[1]); + sess->SetWaveFile(s->second[2]); + AddSession(sess,profile); + } else { qWarning() << "Error unpacking summary data"; delete sess; } diff --git a/SleepLib/session.cpp b/SleepLib/session.cpp index 728f262a..8ad8769a 100644 --- a/SleepLib/session.cpp +++ b/SleepLib/session.cpp @@ -1,15 +1,14 @@ -/******************************************************************** +/* SleepLib Session Implementation This stuff contains the base calculation smarts Copyright (c)2011 Mark Watkins License: GPL -*********************************************************************/ +*/ #include "session.h" #include "math.h" #include #include -#include #include #include @@ -248,11 +247,9 @@ bool Session::OpenEvents() { if(s_events_loaded) return true; bool b; - try { - b=LoadEvents(s_eventfile); - } catch (UnpackError e) { + b=LoadEvents(s_eventfile); + if (!b) { qWarning() << "Error Unkpacking Events" << s_eventfile; - b=false; } s_events_loaded=b; @@ -262,11 +259,9 @@ bool Session::OpenWaveforms() { if (s_waves_loaded) return true; bool b; - try { - b=LoadWaveforms(s_wavefile); - } catch (UnpackError e) { + b=LoadWaveforms(s_wavefile); + if (!b) { qWarning() << "Error Unkpacking Wavefile" << s_wavefile; - b=false; } s_waves_loaded=b; return b; @@ -300,24 +295,32 @@ bool Session::Store(QString path) const quint32 magic=0xC73216AB; +bool IsPlatformLittleEndian() +{ + quint32 j=1; + *((char*)&j) = 0; + return j!=1; +} + bool Session::StoreSummary(QString filename) { - BinaryFile f; - f.Open(filename,BF_WRITE); + QFile file(filename); + file.open(QIODevice::WriteOnly); - f.Pack((quint32)magic); // Magic Number - f.Pack((quint32)s_machine->id()); // Machine ID - f.Pack((quint32)s_session); // Session ID - f.Pack((quint16)0); // File Type 0 == Summary File - f.Pack((quint16)0); // File Version + QDataStream out(&file); + out.setVersion(QDataStream::Qt_4_6); + + out << (quint32)magic; + out << (quint32)s_machine->id(); + out << (quint32)s_session; + out << (quint16)0 << (quint16)0; quint32 starttime=s_first/1000L; quint32 duration=(s_last-s_first)/1000L; - f.Pack(starttime); // Session Start Time - f.Pack(duration); // Duration of sesion in seconds. - - f.Pack((quint16)summary.size()); + out << (quint32)starttime; // Session Start Time + out << (quint32)duration; // Duration of sesion in seconds. + out << (quint16)summary.size(); map mctype; @@ -342,29 +345,30 @@ bool Session::StoreSummary(QString filename) } else { QString t=i->second.typeToName(type); qWarning() << "Error in Session->StoreSummary: Can't pack variant type " << t; - exit(1); + return false; + //exit(1); } - f.Pack((qint16)mc); - f.Pack((qint8)mctype[mc]); + out << (qint16)mc; + out << (qint8)mctype[mc]; } // Then dump out the actual data, according to format. for (i=summary.begin(); i!=summary.end(); i++) { MachineCode mc=i->first; if (mctype[mc]==MC_bool) { - f.Pack((qint8)i->second.toBool()); + out << i->second.toBool(); } else if (mctype[mc]==MC_int) { - f.Pack((qint32)i->second.toInt()); + out << (qint32)i->second.toInt(); } else if (mctype[mc]==MC_long) { - f.Pack((qint64)i->second.toLongLong()); + out << (qint64)i->second.toLongLong(); } else if (mctype[mc]==MC_double) { - f.Pack((double)i->second.toDouble()); + out << (double)i->second.toDouble(); } else if (mctype[mc]==MC_string) { - f.Pack(i->second.toString()); + out << i->second.toString(); } else if (mctype[mc]==MC_datetime) { - f.Pack(i->second.toDateTime()); + out << i->second.toDateTime(); } } - f.Close(); + file.close(); return true; } @@ -372,13 +376,16 @@ bool Session::LoadSummary(QString filename) { if (filename.isEmpty()) return false; //qDebug(("Loading Summary "+filename).toLatin1()); - BinaryFile f; - if (!f.Open(filename,BF_READ)) { + QFile file(filename); + if (!file.open(QIODevice::ReadOnly)) { qDebug() << "Couldn't open file" << filename; return false; } + QDataStream in(&file); + in.setVersion(QDataStream::Qt_4_6); + quint64 t64; quint32 t32; quint16 t16; @@ -387,34 +394,38 @@ bool Session::LoadSummary(QString filename) qint16 sumsize; map mctype; vector mcorder; + in >> t32; + if (t32!=magic) { + qDebug() << "Wrong magic number in " << filename; + return false; + } - if (!f.Unpack(t32)) throw UnpackError(); // Magic Number - if (t32!=magic) throw UnpackError(); + in >> t32; // MachineID (dont need this result) - if (!f.Unpack(t32)) throw UnpackError(); // MachineID - - if (!f.Unpack(t32)) throw UnpackError(); // Sessionid; + in >> t32; // Sessionid; s_session=t32; - if (!f.Unpack(t16)) throw UnpackError(); // File Type - if (t16!=0) throw UnpackError(); //wrong file type + in >> t16; // File Type + if (t16!=0) { + qDebug() << "Wrong file type"; //wrong file type + return false; + } - if (!f.Unpack(t16)) throw UnpackError(); // File Version + in >> t16; // File Version // dont care yet - - if (!f.Unpack(t32)) throw UnpackError(); // Start time + in >> t32; // Start time s_first=qint64(t32)*1000L; - if (!f.Unpack(t32)) throw UnpackError(); // Duration // (16bit==Limited to 18 hours) + in >> t32; // Duration // (16bit==Limited to 18 hours) s_last=s_first+qint64(t32)*1000L; - if (!f.Unpack(sumsize)) throw UnpackError(); // Summary size (number of Machine Code lists) + in >> sumsize; // Summary size (number of Machine Code lists) for (int i=0; i> t16; // Machine Code MachineCode mc=(MachineCode)t16; - if (!f.Unpack(t8)) throw UnpackError(); // Data Type + in >> t8; // Data Type mctype[mc]=(MCDataType)t8; mcorder.push_back(mc); } @@ -423,25 +434,25 @@ bool Session::LoadSummary(QString filename) MachineCode mc=mcorder[i]; if (mctype[mc]==MC_bool) { bool b; - if (!f.Unpack(b)) throw UnpackError(); + in >> b; summary[mc]=b; } else if (mctype[mc]==MC_int) { - if (!f.Unpack(t32)) throw UnpackError(); + in >> t32; summary[mc]=(qint32)t32; } else if (mctype[mc]==MC_long) { - if (!f.Unpack(t64)) throw UnpackError(); + in >> t64; summary[mc]=(qint64)t64; } else if (mctype[mc]==MC_double) { double dl; - if (!f.Unpack(dl)) throw UnpackError(); + in >> dl; summary[mc]=(double)dl; } else if (mctype[mc]==MC_string) { QString s; - if (!f.Unpack(s)) throw UnpackError(); + in >> s; summary[mc]=s; } else if (mctype[mc]==MC_datetime) { QDateTime dt; - if (!f.Unpack(dt)) throw UnpackError(); + in >> dt; summary[mc]=dt; } @@ -451,35 +462,38 @@ bool Session::LoadSummary(QString filename) bool Session::StoreEvents(QString filename) { - BinaryFile f; - f.Open(filename,BF_WRITE); + QFile file(filename); + file.open(QIODevice::WriteOnly); - f.Pack((quint32)magic); // Magic Number - f.Pack((quint32)s_machine->id()); // Machine ID - f.Pack((quint32)s_session); // This session's ID - f.Pack((quint16)1); // File type 1 == Event - f.Pack((quint16)0); // File Version + QDataStream out(&file); + out.setVersion(QDataStream::Qt_4_6); + + out << (quint32)magic; // Magic Number + out << (quint32)s_machine->id(); // Machine ID + out << (quint32)s_session; // This session's ID + out << (quint16)1; // File type 1 == Event + out << (quint16)0; // File Version quint32 starttime=s_first/1000L; quint32 duration=(s_last-s_first)/1000L; - f.Pack(starttime); - f.Pack(duration); + out << starttime; + out << duration; - f.Pack((qint16)events.size()); // Number of event categories + out << (qint16)events.size(); // Number of event categories map >::iterator i; vector::iterator j; for (i=events.begin(); i!=events.end(); i++) { - f.Pack((qint16)i->first); // MachineID - f.Pack((qint16)i->second.size()); // count of events in this category + out << (qint16)i->first; // MachineID + out << (qint16)i->second.size(); // count of events in this category j=i->second.begin(); - f.Pack((qint8)(*j)->fields()); // number of data fields in this event type + out << (qint8)(*j)->fields(); // number of data fields in this event type } bool first; - float tf; + EventDataType tf; qint64 last=0,eventtime,delta; for (i=events.begin(); i!=events.end(); i++) { @@ -487,34 +501,37 @@ bool Session::StoreEvents(QString filename) for (j=i->second.begin(); j!=i->second.end(); j++) { eventtime=(*j)->time(); if (first) { - f.Pack((*j)->time()); + out << (*j)->time(); first=false; } else { delta=eventtime-last; if (delta>0xffffffff) { qDebug("StoreEvent: Delta too big.. needed to use bigger value"); - exit(1); + return false; } - f.Pack((quint32)delta); + out << (quint32)delta; } for (int k=0; k<(*j)->fields(); k++) { tf=(*(*j))[k]; - f.Pack((float)tf); + out << tf; } last=eventtime; } } - f.Close(); + //file.close(); return true; } bool Session::LoadEvents(QString filename) { if (filename.isEmpty()) return false; - BinaryFile f; - if (!f.Open(filename,BF_READ)) { - qDebug() << "Couldn't open events file" << filename; + + QFile file(filename); + if (!file.open(QIODevice::ReadOnly)) { + qDebug() << "Couldn't open file" << filename; return false; } + QDataStream in(&file); + in.setVersion(QDataStream::Qt_4_6); quint32 t32; quint16 t16; @@ -523,27 +540,32 @@ bool Session::LoadEvents(QString filename) // qint16 sumsize; - if (!f.Unpack(t32)) throw UnpackError(); // Magic Number - if (t32!=magic) throw UnpackError(); + in >> t32; // Magic Number + if (t32!=magic) { + qWarning() << "Wrong Magic number in " << filename; + return false; + } + in >> t32; // MachineID - if (!f.Unpack(t32)) throw UnpackError(); // MachineID - - if (!f.Unpack(t32)) throw UnpackError(); // Sessionid; + in >> t32; // Sessionid; s_session=t32; - if (!f.Unpack(t16)) throw UnpackError(); // File Type - if (t16!=1) throw UnpackError(); //wrong file type + in >> t16; // File Type + if (t16!=1) { + qDebug() << "Wrong File Type in " << filename; + return false; + } - if (!f.Unpack(t16)) throw UnpackError(); // File Version + in >> t16; // File Version // dont give a crap yet.. - if (!f.Unpack(t32)) throw UnpackError(); // Start time + in >> t32; // Start time s_first=qint64(t32)*1000L; - if (!f.Unpack(t32)) throw UnpackError(); // Duration // (16bit==Limited to 18 hours) + in >> t32; // Duration // (16bit==Limited to 18 hours) s_last=s_first+qint64(t32)*1000L; qint16 evsize; - if (!f.Unpack(evsize)) throw UnpackError(); // Summary size (number of Machine Code lists) + in >> evsize; // Summary size (number of Machine Code lists) map mcsize; map mcfields; @@ -551,12 +573,12 @@ bool Session::LoadEvents(QString filename) MachineCode mc; for (int i=0; i> t16; // MachineCode mc=(MachineCode)t16; mcorder.push_back(mc); - if (!f.Unpack(t16)) throw UnpackError(); // Count of this Event + in >> t16; // Count of this Event mcsize[mc]=t16; - if (!f.Unpack(t8)) throw UnpackError(); // Count of this Event + in >> t8; // Type mcfields[mc]=t8; } @@ -564,20 +586,18 @@ bool Session::LoadEvents(QString filename) mc=mcorder[i]; bool first=true; qint64 d; - float fl; events[mc].clear(); for (int e=0; e> d; // Timestamp first=false; } else { - if (!f.Unpack(t32)) throw UnpackError(); // Time Delta + in >> t32; // Time Delta d=d+t32; } EventDataType ED[max_number_event_fields]; for (int c=0; c> ED[c]; // Data Fields in float format } Event *ev=new Event(d,mc,ED,mcfields[mc]); @@ -590,55 +610,53 @@ bool Session::LoadEvents(QString filename) bool Session::StoreWaveforms(QString filename) { - BinaryFile f; - f.Open(filename,BF_WRITE); + QFile file(filename); + file.open(QIODevice::WriteOnly); + QDataStream out(&file); + out.setVersion(QDataStream::Qt_4_6); quint16 t16; - f.Pack((quint32)magic); // Magic Number - f.Pack((quint32)s_machine->id()); // Machine ID - f.Pack((quint32)s_session); // This session's ID - f.Pack((quint16)2); // File type 2 == Waveform - f.Pack((quint16)0); // File Version + out << (quint32)magic; // Magic Number + out << (quint32)s_machine->id(); // Machine ID + out << (quint32)s_session; // This session's ID + out << (quint16)2; // File type 2 == Waveform + out << (quint16)0; // File Version quint32 starttime=s_first/1000L; quint32 duration=(s_last-s_first)/1000L; - f.Pack(starttime); - f.Pack(duration); + out << starttime; + out << duration; - f.Pack((qint16)waveforms.size()); // Number of different waveforms + out << (qint16)waveforms.size(); // Number of different waveforms map >::iterator i; vector::iterator j; for (i=waveforms.begin(); i!=waveforms.end(); i++) { - f.Pack((qint16)i->first); // Machine Code + out << (quint16)i->first; // Machine Code t16=i->second.size(); - f.Pack(t16); // Number of (hopefully non-linear) waveform chunks + out << t16; // Number of (hopefully non-linear) waveform chunks for (j=i->second.begin(); j!=i->second.end(); j++) { Waveform &w=*(*j); // 64bit number.. - f.Pack(w.start()); // Start time of first waveform chunk + out << (qint64)w.start(); // Start time of first waveform chunk //qint32 samples; //double seconds; - f.Pack((qint32)w.samples()); // Total number of samples - f.Pack((qint64)w.duration()); // Total number of seconds - f.Pack((qint16)w.min()); - f.Pack((qint16)w.max()); - f.Pack((qint8)sizeof(SampleFormat)); // Bytes per sample - f.Pack((qint8)0); // signed.. all samples for now are signed 16bit. + out << (qint32)w.samples(); // Total number of samples + out << (qint64)w.duration(); // Total number of seconds + out << (SampleFormat)w.min(); + out << (SampleFormat)w.max(); + out << (qint8)sizeof(SampleFormat); // Bytes per sample + out << (qint8)0; // signed.. all samples for now are signed 16bit. //t8=0; // 0=signed, 1=unsigned, 2=float // followed by sample data. - if (IsPlatformLittleEndian()) { - f.Write((const char *)w.GetBuffer(),w.samples()*sizeof(SampleFormat)); - } else { - for (int k=0; k<(*j)->samples(); k++) f.Pack((qint16)w[k]); - } + for (int k=0; k<(*j)->samples(); k++) out << w[k]; } } return true; @@ -648,39 +666,46 @@ bool Session::StoreWaveforms(QString filename) bool Session::LoadWaveforms(QString filename) { if (filename.isEmpty()) return false; - - BinaryFile f; - if (!f.Open(filename,BF_READ)) { - qDebug() << "Couldn't open waveform file " << filename; + QFile file(filename); + if (!file.open(QIODevice::ReadOnly)) { + qDebug() << "Couldn't open file" << filename; return false; } + QDataStream in(&file); + in.setVersion(QDataStream::Qt_4_6); quint32 t32; quint16 t16; quint8 t8; - if (!f.Unpack(t32)) throw UnpackError(); // Magic Number - if (t32!=magic) throw UnpackError(); + in >>t32; // Magic Number + if (t32!=magic) { + qDebug() << "Wrong magic in " << filename; + return false; + } - if (!f.Unpack(t32)) throw UnpackError(); // MachineID + in >> t32; // MachineID - if (!f.Unpack(t32)) throw UnpackError(); // Sessionid; + in >> t32; // Sessionid; s_session=t32; - if (!f.Unpack(t16)) throw UnpackError(); // File Type - if (t16!=2) throw UnpackError(); //wrong file type? + in >> t16; // File Type + if (t16!=2) { + qDebug() << "Wrong File Type in " << filename; + return false; + } - if (!f.Unpack(t16)) throw UnpackError(); // File Version + in >> t16; // File Version // dont give a crap yet.. - if (!f.Unpack(t32)) throw UnpackError(); // Start time + in >> t32; // Start time s_first=qint64(t32)*1000; - if (!f.Unpack(t32)) throw UnpackError(); // Duration // (16bit==Limited to 18 hours) + in >> t32; // Duration // (16bit==Limited to 18 hours) s_last=s_first+qint64(t32)*1000; - qint16 wvsize; - if (!f.Unpack(wvsize)) throw UnpackError(); // Summary size (number of Machine Code lists) + quint16 wvsize; + in >> wvsize; // Number of waveforms MachineCode mc; qint64 date; @@ -689,26 +714,25 @@ bool Session::LoadWaveforms(QString filename) int chunks; SampleFormat min,max; for (int i=0; i> t16; // Machine Code mc=(MachineCode)t16; - if (!f.Unpack(t16)) throw UnpackError(); // Machine Code + in >> t16; // Number of waveform Chunks chunks=t16; for (int i=0; i> date; // Waveform DateTime + in >> samples; // Number of samples + in >> seconds; // Duration in seconds + in >> min; // Min value + in >> max; // Min value + in >> t8; // sample size in bytes + in >> t8; // Number of samples (?? what did I mean by this) SampleFormat *data=new SampleFormat [samples]; + for (int k=0; k> data[k]; // Individual Samples; } Waveform *w=new Waveform(date,mc,data,samples,seconds,min,max); AddWaveform(w); diff --git a/SleepyHeadQT.pro b/SleepyHeadQT.pro index 666cdc40..bcae847d 100644 --- a/SleepyHeadQT.pro +++ b/SleepyHeadQT.pro @@ -24,7 +24,6 @@ TEMPLATE = app SOURCES += main.cpp\ - SleepLib/binary_file.cpp \ SleepLib/machine.cpp \ SleepLib/machine_loader.cpp \ SleepLib/preferences.cpp \ @@ -79,7 +78,6 @@ win32 { } HEADERS += \ - SleepLib/binary_file.h \ SleepLib/machine.h \ SleepLib/machine_loader.h \ SleepLib/preferences.h \