6 #define _TR_(a) QObject::tr(a)
9 #define DbgLv(a) if(dbg_level>=a)qDebug()
40 unsigned char vbuf[ 8 ];
41 int* vint = (
int*)vbuf;
49 double timeinc,
double ftime )
62 if ( !
fileo->open( QIODevice::WriteOnly ) )
98 QString fl1 = QString( format ).left( 1 );
100 if ( QString(
"FIC" ).indexOf( fl1 ) < 0 )
105 DbgLv(1) <<
"TS: key format" << key << format <<
"stat errm" << status <<
error_msg;
109 int flen = QString( format ).mid( 1 ).toInt();
116 DbgLv(1) <<
"TS: nval" <<
nvalues <<
"key format" << key << format
117 <<
"offs flen" << offset << flen <<
"rsize" <<
rec_size;
129 if ( ffmts.count() !=
nvalues )
135 for (
int jj = 0; jj <
nvalues; jj++ )
137 status =
set_key( fkeys[ jj ], ffmts[ jj ] );
151 for (
int jj = 0; jj < fkeys.count(); jj++ )
153 QString fkey = fkeys[ jj ];
154 QString ffmt = fkey.section(
" ", -1, -1 ).simplified();
155 fkey = fkey.section(
" ", 0, -2 ).simplified();
168 DbgLv(1) <<
"DtsF: sival: key ival" << key << ivalue;
179 char* cval = (
char*)cwrk;
191 sprintf( cval,
"%d", ivalue );
192 cwrk[ 0 ] = cwrk[ strlen( cval ) - 1 ] && 15;
193 memcpy (
cdata + roff, cval + coff, 1 );
196 dvalue = (double)ivalue;
200 dvalue = (double)ivalue;
204 sprintf( cval,
"%d", ivalue );
206 memcpy (
cdata + roff, cval + coff, rlen );
220 DbgLv(1) <<
"DtsF: sdval: key dval" << key << dvalue;
231 char* cval = (
char*)cwrk;
237 jvalue = qRound( dvalue );
241 jvalue = qRound( dvalue );
245 jvalue = qRound( dvalue );
246 sprintf( cval,
"%d", jvalue );
247 cwrk[ 0 ] = cwrk[ strlen( cval ) - 1 ] && 15;
248 memcpy (
cdata + roff, cval + coff, 1 );
257 sprintf( cval,
"%f", dvalue );
259 memcpy (
cdata + roff, cval + coff, rlen );
273 DbgLv(1) <<
"DtsF: ssval: key sval" << key << svalue;
286 char* cval = (
char*)cwrk;
292 jvalue = svalue.toInt();
296 jvalue = svalue.toInt();
300 jvalue = svalue.toInt();
301 sprintf( cval,
"%d", jvalue );
302 cwrk[ 0 ] = cwrk[ strlen( cval ) - 1 ] && 15;
303 memcpy (
cdata + roff, cval + coff, 1 );
306 dvalue = svalue.toDouble();
310 dvalue = svalue.toDouble();
316 if ( svalue.length() < rlen )
318 for (
int jj = svalue.length(); jj < rlen; jj++ )
319 wstr += QString(
" " );
322 memcpy(
cdata + roff, wstr.toAscii().constData(), rlen );
365 QString xfname = QString(
filename ).section(
".", 0, -2 ) +
".xml";
366 QString xfpath = QString(
filepath ).section(
".", 0, -2 ) +
".xml";
370 if ( !xfo.open( QIODevice::WriteOnly | QIODevice::Text ) )
377 QXmlStreamWriter xml( &xfo );
378 xml.setAutoFormatting(
true );
379 xml.writeStartDocument();
380 xml.writeDTD (
"<!DOCTYPE US_TimeState>" );
381 xml.writeStartElement(
"TimeState" );
382 xml.writeAttribute (
"version", QString(
_TMST_VERS_ ) );
384 xml.writeStartElement(
"file" );
385 xml.writeAttribute (
"time_count", QString::number(
ntimes ) );
386 xml.writeAttribute (
"constant_incr", (
const_ti ?
"1" :
"0" ) );
387 xml.writeAttribute (
"time_increment", QString::number(
time_inc ) );
388 xml.writeAttribute (
"first_time", QString::number(
time_first ) );
390 for (
int jj = 0; jj <
nvalues; jj++ )
392 xml.writeStartElement(
"value" );
393 xml.writeAttribute (
"key",
keys[ jj ] );
394 xml.writeAttribute (
"format",
fmts[ jj ] );
395 xml.writeEndElement ( );
398 xml.writeEndElement ( );
399 xml.writeEndElement ( );
400 xml.writeEndDocument ( );
413 if ( !
filei->open( QIODevice::ReadOnly ) )
439 + QString(
cdata ).left( 4 );
469 QString xfname = QString(
filename ).section(
".", 0, -2 ) +
".xml";
470 QString xfpath = QString(
filepath ).section(
".", 0, -2 ) +
".xml";
474 if ( !xfi.open( QIODevice::ReadOnly | QIODevice::Text ) )
481 QXmlStreamReader xml( &xfi );
483 while( ! xml.atEnd() )
487 QXmlStreamAttributes attr;
490 if ( xml.isStartElement() )
492 xname = xml.name().toString();
493 attr = xml.attributes();
495 if ( xname ==
"TimeState" )
497 fvers = attr.value(
"version" ).toString();
500 else if ( xname ==
"file" )
502 attv = attr.value(
"time_count" ).toString();
505 attv = attr.value(
"constant_incr" ).toString();
508 attv = attr.value(
"time_increment" ).toString();
511 attv = attr.value(
"first_time" ).toString();
515 else if ( xname ==
"value" )
517 QString ky = attr.value(
"key" ).toString();
518 QString fm = attr.value(
"format" ).toString();
523 koff += fm.mid( 1 ).toInt();
544 if ( constti != NULL ) *constti =
const_ti;
545 if ( timeinc != NULL ) *timeinc =
time_inc;
571 QString fm =
fmts[ lstv ];
575 if ( rtimex < 0 || ( rtimex -
timex ) == 1 )
581 else if ( rtimex >
timex )
583 while (
timex < rtimex )
609 char* rdata =
cdata + roff;
614 char* cval = (
char*)cwrk;
615 char* eval = cval + rlen;
621 ivalue =
iword( rdata );
624 ivalue =
hword( rdata );
627 ivalue = (
unsigned int)rdata[ 0 ];
630 dvalue =
dword( rdata );
631 ivalue = qRound( dvalue );
635 ivalue = qRound( dvalue );
638 memcpy( cval, rdata, rlen );
640 ivalue = QString( cval ).left( rlen ).toInt();
662 char* rdata =
cdata + roff;
667 char* cval = (
char*)cwrk;
668 char* eval = cval + rlen;
674 ivalue =
iword( rdata );
675 dvalue = (double)ivalue;
678 ivalue =
hword( rdata );
679 dvalue = (double)ivalue;
682 ivalue = (
unsigned int)rdata[ 0 ];
683 dvalue = (double)ivalue;
686 dvalue =
dword( rdata );
692 memcpy( cval, rdata, rlen );
694 dvalue = QString( cval ).left( rlen ).toDouble();
718 char* rdata =
cdata + roff;
725 ivalue =
iword( rdata );
726 svalue = QString::number( ivalue );
729 ivalue =
hword( rdata );
730 svalue = QString::number( ivalue );
733 ivalue = (
unsigned int)rdata[ 0 ];
734 svalue = QString::number( ivalue );
737 dvalue =
dword( rdata );
738 svalue = QString::number( dvalue );
742 svalue = QString::number( dvalue );
745 svalue = QString( rdata ).left( rlen );
779 const errmap emap[] =
781 { 100,
_TR_(
"Write-file open error: " ) },
782 { 101,
_TR_(
"Invalid field format: " ) },
783 { 102,
_TR_(
"Keys count out of sync." ) },
784 { 700,
_TR_(
"Write-XML-file open error" ) },
785 { 500,
_TR_(
"Read-file open error: " ) },
786 { 501,
_TR_(
"Not the TMST file magic number: " ) },
787 { 502,
_TR_(
"Incompatible file format version: " ) },
788 { 505,
_TR_(
"Read-XML-file open error" ) },
789 { 510,
_TR_(
"Attempt to access previous time record" ) },
790 { 901,
_TR_(
"Invalid key parameters (key,fmt,len,off): " ) },
791 { 999,
_TR_(
"UNKNOWN" ) }
794 const int netypes =
sizeof( emap ) /
sizeof( emap[ 0 ] );
795 QString errmsg =
_TR_(
"UNKNOWN" );
797 for (
int ii = 0; ii < netypes; ii++ )
799 if ( emap[ ii ].estat == status )
801 errmsg = emap[ ii ].emsg;
806 DbgLv(1) <<
"DtsF: egmsg: status" << status <<
"message" << errmsg;
818 const int expID,
const QString fpath )
822 if ( dbP == NULL || fpath.isEmpty() || expID < 1 )
825 QString tmst_fpath = fpath;
826 QString defs_fpath = QString( fpath ).section(
".", 0, -2 ) +
".xml";
828 QFile dfi( tmst_fpath );
829 QFile xfi( defs_fpath );
830 if ( !dfi.exists() || !xfi.exists() )
833 if ( ! xfi.open( QIODevice::ReadOnly ) )
839 QString tmst_fname = QString( fpath ).section(
"/", -1, -1 );
841 DbgLv(1) <<
"dbCreate: dbP fn ck" << dbP << tmst_fname << tmst_cksm;
843 QByteArray defs_da = xfi.readAll();
845 QString idTmst = QString::number( tmstID );
846 QString idExp = QString::number( expID );
854 int stat =
dbExamine( dbP, &tmstID, &expIDdb, &fnamedb,
856 DbgLv(1) <<
"dbCreate: dbExam stat" << stat <<
"tmstID expID"
857 << tmstID << expIDdb <<
"cksumdb" << cksumdb;
860 if ( cksumdb == tmst_cksm )
862 QByteArray defsdb = xdefs.toUtf8();
863 QString cksumxd = QString( QCryptographicHash::hash(
864 defsdb, QCryptographicHash::Md5 ).toHex() )
865 +
" " + QString::number( defsdb.size() );
867 if ( tmstID > 0 && cksumxd == defs_cksm )
869 if ( fnamedb != tmst_fname )
873 query <<
"update_timestate" << idTmst << idExp
874 << tmst_fname << defs_ld;
892 query <<
"new_timestate" << idExp << tmst_fname << defs_ld;
897 DbgLv(1) <<
"dbCreate: new_timestate status" << stat
898 <<
"idExp tmstID nsrtID dsiz" << idExp << tmstID << nsrtID << defs_da.size();
907 DbgLv(1) <<
"dbDelete: dbP tmstID" << dbP << tmstID;
909 query <<
"delete_timestate" << QString::number( tmstID );
917 QString* fnameP, QString* xdefsP, QString* cksumP, QDateTime* lastupdP )
920 DbgLv(1) <<
"dbExamine: dbP tmstID expID fname xdefs cksum lastupd"
921 << dbP << tmstIdP << expIdP << fnameP << xdefsP << cksumP << lastupdP;
923 int tmstID = ( tmstIdP == NULL ) ? 0 : *tmstIdP;
924 int expID = ( expIdP == NULL ) ? 0 : *expIdP;
926 DbgLv(1) <<
"dbExamine: tmstID expID" << tmstID << expID;
931 query <<
"get_timestate" << QString::number( tmstID );
934 DbgLv(1) <<
"dbExamine: get_timestate tmstID" << tmstID <<
"status" << status;
940 expID = dbP->
value( 0 ).toString().toInt();
941 DbgLv(1) <<
"dbExamine: get_timestate expID" << expID <<
"nrows" << nrows;
942 if ( expIdP != NULL )
946 else if ( expID > 0 )
954 query <<
"get_experiment_timestate" << QString::number( expID );
957 DbgLv(1) <<
"dbExamine: get_experiment_timestate expID status" << expID << status;
962 DbgLv(1) <<
"dbExamine: get_experiment_timestate nrows" << nrows;
964 bool havenx=dbP->
next();
966 DbgLv(1) <<
"dbExamine: get_experiment_timestate next status" << status
967 <<
"have_next" << havenx;
968 tmstID = dbP->
value( 0 ).toString().toInt();
969 DbgLv(1) <<
"dbExamine: get_experiment_timestate tmstID" << tmstID
970 << dbP->
value(0).toString() << dbP->
value(1).toString();
971 if ( tmstIdP != NULL )
980 if ( fnameP != NULL )
981 *fnameP = dbP->
value( 1 ).toString();
983 if ( xdefsP != NULL )
984 *xdefsP = dbP->
value( 2 ).toString();
986 if ( cksumP != NULL )
987 *cksumP = dbP->
value( 3 ).toString() +
" " +
988 dbP->
value( 4 ).toString();
990 DbgLv(1) <<
"dbExamine: cksum-db" << *cksumP;
992 if ( lastupdP != NULL )
993 *lastupdP = QDateTime::fromString( dbP->
value( 5 ).toString(),
994 Qt::ISODate ).toUTC();
1001 const QString fpath )
1004 DbgLv(1) <<
"dbDownload: dbP tmstID fpath" << dbP << tmstID << fpath;
1005 int status = dbP->
readBlobFromDB( fpath, QString(
"download_timestate" ),
1012 const QString fpath )
1015 DbgLv(1) <<
"dbUpload: dbP tmstID fpath" << dbP << tmstID << fpath;
1016 int status = dbP->
writeBlobToDB( fpath, QString(
"upload_timestate" ),
1029 unsigned char* cbuf = (
unsigned char*)dba;
1030 int j0 = (int)cbuf[ 0 ] & 255;
1031 int j1 = (int)cbuf[ 1 ] & 255;
1032 return ( ( j0 << 8 ) | j1 );
1039 return ( ( iword < 32768 ) ? iword : ( iword - 65536 ) );
1046 int j1 =
uhword( cbuf + 2 );
1047 return ( ( j0 << 16 ) | j1 );
1053 int ival =
iword( cbuf );
1055 float* fptr = (
float*)iptr;
1062 float fval =
fword( cbuf );
1063 return (
double)fval;
1070 double *dptr = (
double*)iwork;
1071 int ival =
iword( cbuf );
1074 int ratio =
sizeof( double ) /
int_size;
1078 iwork[ lx ] =
iword( cbuf + 4 );
1089 unsigned char* cbuf = (
unsigned char*)dba;
1090 unsigned char vbuf[ 8 ];
1091 int* vint = (
int*)vbuf;
1096 cbuf[ 0 ] = vbuf[ 1 ];
1097 cbuf[ 1 ] = vbuf[ 0 ];
1110 unsigned char* cbuf = (
unsigned char*)dba;
1111 unsigned char vbuf[ 8 ];
1112 int* vint = (
int*)vbuf;
1117 cbuf[ 0 ] = vbuf[ 3 ];
1118 cbuf[ 1 ] = vbuf[ 2 ];
1119 cbuf[ 2 ] = vbuf[ 1 ];
1120 cbuf[ 3 ] = vbuf[ 0 ];
1135 float fwrk = (float)dval;
1136 float* fptr = (
float*)&fwrk;
1137 int* iptr = (
int*)fptr;
1146 int* iptr = (
int*)&dwrk;
1151 int ratio =
sizeof( double ) /
int_size;
1168 for (
int ii = 0; ii < nvals; ii++, cptr+=2 )
1179 for (
int ii = 0; ii < nvals; ii++, cptr+=4 )
1190 for (
int ii = 0; ii < nvals; ii++, cptr+=4 )
1209 int* rfmtP,
int* rlenP,
int* roffP )
1213 const QString sfmts(
"I4I2I2F4F8C1C2C3C4C5C6C7C8C9" );
1215 int keyx =
keys.indexOf( key );
1216 QString fmt = ( keyx >= 0 ) ?
fmts[ keyx ] :
"";
1217 QString ftst = fmt.left( 2 );
1218 int rfmt = sfmts.indexOf( ftst ) / 2;
1219 rfmt = qMin( rfmt, 5 );
1220 int rlen = QString( fmt ).mid( 1 ).toInt();
1221 int roff = ( keyx >= 0 ) ?
offs[ keyx ] : -1;
1223 if ( rfmt < 0 || rlen < 1 || roff < 0 )
1227 error_msg += key + QString().sprintf(
" %d %d %d", rfmt, rlen, roff );
1230 if ( rfmtP != NULL ) *rfmtP = rfmt;
1231 if ( rlenP != NULL ) *rlenP = rlen;
1232 if ( roffP != NULL ) *roffP = roff;