Functions

global.cpp File Reference

#include "global.h"
#include <QString>
#include <QDateTime>
#include <QVariant>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlField>
Include dependency graph for global.cpp:

Functions

QString generateKey (const QDateTime &timestamp, const QString &component)
bool copyFile (char *dest, char *src)
QVariant formulaRead (const QVariant &data)
QVariant formulaWrite (const QVariant &data)
quint64 cantor (const int &x, const int &y)
int seekRow (QSqlQuery query, const QSqlRecord &record)

Function Documentation

quint64 cantor ( const int &  x,
const int &  y 
)

                                          {
    return ((x+y)*(x+y)+x-y)/2;
}

bool copyFile ( char *  dest,
char *  src 
)

{
    FILE *dp,*sp;
    int c;
    sp = fopen(src,"rb");
    if (!sp)
    {
        qWarning() << "Can't open " << src;
        return FALSE;
    }
    dp = fopen(dest,"wb");
    if (!dp)
    {
        qWarning() << "Can't open " << dest;
        return FALSE;
    }
    while (1)
    { c = getc(sp);
        if (feof(sp))
            break;
        fputc(c,dp);
    }
    fclose(sp);
    fclose(dp);
    return TRUE;
}

QVariant formulaRead ( const QVariant data  ) 

Referenced by CalibrationsModel::data().

                                           {
    return data.toString().replace("$1", "RAW");
}

QVariant formulaWrite ( const QVariant data  ) 

                                            {
    return data.toString().replace("RAW", "$1");
}

QString generateKey ( const QDateTime timestamp,
const QString component 
)

                                                                          {
    int p = 0;
    // Our key is the left part of the ISODate, cut off on the component.
    // So for month, we take the first 7 characters of YYYY-MM-DDTHH:MM:SS (i.e.
    // ISO date format)
    if (component == QString()) return QString();
    else if (component == "Week") {
        int year;
        int week = timestamp.date().weekNumber(&year);
        return QString::number(year).append("-w").append(QString::number(week));
    }
    else if (component == "Year") p = 4;
    else if (component == "Month") p = 7;
    else if (component == "Day") p = 10;
    else if (component == "Hour") p = 13;
    else if (component == "Minute") p = 16;
    else if (component == "Second") p = 19;
    Q_ASSERT(p);
    return timestamp.toString(Qt::ISODate).left(p);
}

int seekRow ( QSqlQuery  query,
const QSqlRecord record 
)

Referenced by MappedBaseEditor::onQueryChange(), MappedBaseEditor::onSubmitButtonPress(), and MappedBaseEditor::setCurrent().

                                                       {
    // note that query is passed by value: this might be a performance bottleneck for large queries.

    if (!query.isActive() || record.isEmpty())
        return -1;

    // check whether query is empty
    Q_ASSERT(query.isSelect());
    query.first();
    if (!query.isValid())
        return -1;

    // check equal fields exist and their data types are the same, and in mean time,
    // set up the FieldMap.
    struct FieldMap {
        FieldMap() : query(0), record(0) {}
        int query;
        int record;
    } map[record.count()];
    for(int i = 0; i < record.count(); i++){
        map[i].record = i;
        map[i].query = query.record().indexOf(record.fieldName(i));
        if (map[i].query == -1 ||
            query.record().field(map[i].query).type() != record.field(map[i].record).type())
            return -1;
    }
    // seek row
    int i = 0; //field number
    int count = record.count();
    int max = record.count() - 1;
    do {
        for (i = 0; i < count; i++) {
            //qDebug() << /*j*/ << "of" << query.size() << i << query.value(map[i].query) << record.value(map[i].record);
            if (query.value(map[i].query) != record.value(map[i].record))
                break;
            if (i == max) return query.at();
        }
    } while (query.next());

    return -1;
}