Sorry guys, database rework.. Reimport is is really necessary

This commit is contained in:
Mark Watkins 2011-07-21 04:32:47 +10:00
parent 54b31431f6
commit 45a15ec38f
7 changed files with 183 additions and 585 deletions

View File

@ -1,332 +0,0 @@
/*
SleepLib BinaryFile Implementation
Author: Mark Watkins <jedimark64@users.sourceforge.net>
License: GPL
*/
//#include <wx/filename.h>
#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<i16; i++) {
Unpack(t16);
c=t16;
data+=c;
}
return true;
}
bool BinaryFile::Unpack(QDateTime & data)
{
qint32 i32;
if (!Unpack(i32)) return false;
time_t t=i32;
data.setTime_t(t);
return true;
}
bool BinaryFile::Pack(const qint8 &data)
{
f.write((char *)&data,1);
pos++;
return true;
}
bool BinaryFile::Pack(const bool &data)
{
f.write((char*)&data,1);
pos++;
return true;
}
bool BinaryFile::Pack(const qint16 & data)
{
qint8 *d=(qint8*)&data;
f.write((char *)&d[0],1);
f.write((char *)&d[1],1);
pos+=2;
return true;
}
bool BinaryFile::Pack(const qint32 & data)
{
qint8 *d=(qint8*)&data;
for (int i=0; i<4; i++) f.write((char *)&d[i],1);
pos+=4;
return true;
}
bool BinaryFile::Pack(const qint64 & data)
{
qint8 *d=(qint8 *)&data;
for (int i=0; i<8; i++) f.write((char *)&d[i],1);
pos+=8;
return true;
}
bool BinaryFile::Pack(const quint8 & data)
{
f.write((char *)&data,1);
pos++;
return true;
}
bool BinaryFile::Pack(const quint16 & data)
{
quint8 *d=(quint8 *)&data;
f.write((char *)&d[0],1);
f.write((char *)&d[1],1);
pos+=2;
return true;
}
bool BinaryFile::Pack(const quint32 & data)
{
quint8 *d=(quint8 *)&data;
for (int i=0; i<4; i++) f.write((char *)&d[i],1);
pos+=4;
return true;
}
bool BinaryFile::Pack(const quint64 & data)
{
quint8 *d=(quint8 *)&data;
for (int i=0; i<8; i++) f.write((char *)&d[i],1);
pos+=8;
return true;
}
bool BinaryFile::Pack(const float & data)
{
quint8 *d=(quint8 *)&data;
for (int i=0; i<4; i++) f.write((char *)&d[i],1);
pos+=4;
return true;
}
bool BinaryFile::Pack(const double & data)
{
quint8 *d=(quint8 *)&data;
for (int i=0; i<8; i++) f.write((char *)&d[i],1);
pos+=8;
return true;
}
bool BinaryFile::Pack(const QString & data)
{
const QChar *s=data.data();
qint16 i16=data.length();
Pack(i16);
qint16 t;
for (int i=0; i<i16; i++) {
t=s[i].unicode();
Pack(t);
}
return true;
}
bool BinaryFile::Pack(const QDateTime & data)
{
time_t t=data.toTime_t();
qint32 i32=t;
Pack(i32);
return true;
}

View File

@ -1,87 +0,0 @@
/*
SleepLib BinaryFile Header
Author: Mark Watkins <jedimark64@users.sourceforge.net>
License: GPL
*/
#ifndef BINARY_FILE_H
#define BINARY_FILE_H
//#include <wx/ffile.h>
//#include <wx/utils.h>
#include <QString>
#include <QFile>
#include <QDateTime>
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

View File

@ -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 (duration<diff) {
duration+=diff;
if (duration>abs(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;

View File

@ -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;
//
//********************************************************************************************

View File

@ -11,7 +11,6 @@
#include <QObject>
#include <tr1/random>
#include <sys/time.h>
#include "binary_file.h"
#include "machine.h"
#include "profiles.h"
#include <algorithm>
@ -337,8 +336,9 @@ bool Machine::Purge(int secret)
for (int i=0;i<list.size();++i) {
QFileInfo fi=list.at(i);
QString fullpath=fi.canonicalFilePath();
int j=fullpath.lastIndexOf(".");
QString ext_s=*(fullpath.rightRef(j+1).string());
//int j=fullpath.lastIndexOf(".");
QString ext_s=fullpath.section('.',-1);//right(j);
bool ok;
ext_s.toInt(&ok,10);
if (ok) {
@ -399,16 +399,11 @@ bool Machine::Load()
Session *sess=new Session(this,s->first);
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;
}

View File

@ -1,15 +1,14 @@
/********************************************************************
/*
SleepLib Session Implementation
This stuff contains the base calculation smarts
Copyright (c)2011 Mark Watkins <jedimark@users.sourceforge.net>
License: GPL
*********************************************************************/
*/
#include "session.h"
#include "math.h"
#include <QDir>
#include <QDebug>
#include <SleepLib/binary_file.h>
#include <vector>
#include <algorithm>
@ -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<MachineCode,MCDataType> 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<MachineCode,MCDataType> mctype;
vector<MachineCode> 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<sumsize; i++) {
if (!f.Unpack(t16)) throw UnpackError(); // Machine Code
in >> 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<MachineCode,vector<Event *> >::iterator i;
vector<Event *>::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<MachineCode,qint16> mcsize;
map<MachineCode,qint16> mcfields;
@ -551,12 +573,12 @@ bool Session::LoadEvents(QString filename)
MachineCode mc;
for (int i=0; i<evsize; i++) {
if (!f.Unpack(t16)) throw UnpackError(); // MachineID
in >> 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<mcsize[mc]; e++) {
if (first) {
if (!f.Unpack(d)) throw UnpackError(); // Timestamp
in >> 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<mcfields[mc]; c++) {
if (!f.Unpack(fl)) {}; //throw UnpackError(); // Data Fields in float format
ED[c]=fl;
in >> 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<MachineCode,vector<Waveform *> >::iterator i;
vector<Waveform *>::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<wvsize; i++) {
if (!f.Unpack(t16)) throw UnpackError(); // Machine Code
in >> 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<chunks; i++) {
if (!f.Unpack(date)) throw UnpackError(); // Waveform DateTime
if (!f.Unpack(samples)) throw UnpackError(); // Number of samples
if (!f.Unpack(seconds)) throw UnpackError(); // Duration in seconds
if (!f.Unpack(min)) throw UnpackError(); // Min value
if (!f.Unpack(max)) throw UnpackError(); // Min value
if (!f.Unpack(t8)) throw UnpackError(); // sample size in bytes
if (!f.Unpack(t8)) throw UnpackError(); // Number of samples
in >> 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<samples; k++) {
if (!f.Unpack(t16)) throw UnpackError(); // Number of samples
data[k]=t16;
in >> data[k]; // Individual Samples;
}
Waveform *w=new Waveform(date,mc,data,samples,seconds,min,max);
AddWaveform(w);

View File

@ -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 \