diff --git a/src/Database.cpp b/src/Database.cpp index 2ffcd6c..ec04738 100644 --- a/src/Database.cpp +++ b/src/Database.cpp @@ -19,6 +19,58 @@ ***************************************************************************/ #include "Database.h" +#include "lib/random.h" + +KpxUuid::KpxUuid(){ + generate(); +} + + +void KpxUuid::generate(){ + char uuid[16]; + getRandomBytes(uuid,16); + quint32 Secs=QDateTime::currentDateTime().toTime_t(); + quint16 mSecs=QTime::currentTime().msec(); + mSecs=(mSecs & 0x3FF) | (*((quint16*)(uuid+4)) & 0xFC00); //msec has only 10 Bits, filling the rest with random data + memcpy((void*)uuid,&Secs,4); + memcpy((void*)(uuid+4),&mSecs,2); + Data=QByteArray(uuid,16); +} + +QString KpxUuid::toString()const{ + QString hex; + Q_ASSERT(Data.length()==16); + for(int i=0;i<16;i++){ + QString HexByte; + HexByte.setNum((unsigned char)*(Data.data()+i),16); + if(HexByte.length()<2)HexByte="0"+HexByte; + hex+=HexByte; + } + return QString("{%1-%2-%3-%4-%5}") + .arg(hex.mid(0,8)) + .arg(hex.mid(8,4)) + .arg(hex.mid(12,4)) + .arg(hex.mid(16,4)) + .arg(hex.mid(20,12)); +} + +void KpxUuid::toRaw(void* dst){ + memcpy(dst,Data.data(),16); +} + +void KpxUuid::fromRaw(void* src){ + Data=QByteArray((char*)src,16); +} + +bool KpxUuid::operator==(const KpxUuid& other)const{ + return other.Data==Data; +} + +bool KpxUuid::operator!=(const KpxUuid& other)const{ + return other.Data!=Data; +} + + QString KpxDateTime::toString(Qt::DateFormat format) const{ if(*this==Date_Never)return QObject::tr("Never"); diff --git a/src/Database.h b/src/Database.h index 3bd289c..5d5ea9c 100644 --- a/src/Database.h +++ b/src/Database.h @@ -24,10 +24,27 @@ #include #include #include +#include #include "lib/SecString.h" using namespace std; extern const QDateTime Date_Never; + +class KpxUuid{ +public: + KpxUuid(); + bool operator==(const KpxUuid&) const; + bool operator!=(const KpxUuid&) const; + QString toString() const; + const unsigned char* data()const + {return (const unsigned char*) Data.data();} + void toRaw(void* dst); + void fromRaw(void* src); +private: + void generate(); + QByteArray Data; +}; + class KpxDateTime:public QDateTime{ public: KpxDateTime(){}; diff --git a/src/main.cpp b/src/main.cpp index d946404..b504011 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -35,6 +35,7 @@ #include "PwmConfig.h" #include "PwManager.h" #include "mainwindow.h" +#include "Database.h" using namespace std; #ifdef Q_WS_X11 @@ -82,6 +83,12 @@ bool loadTranslation(QTranslator* tr,const QString& prefix,const QString& Locale int main(int argc, char **argv) { +for(int i=0;i<100;i++){ + KpxUuid id; + cout << (const char*)id.toString().toAscii() << endl; +} + + QApplication* app=new QApplication(argc,argv); QString ArgFile,ArgCfg,ArgLang,IniFilename;