Example #1
0
void FiffStreamThread::parseCommand(FiffTag::SPtr p_pTag)
{
    if(p_pTag->size() >= 4)
    {
        qint32* t_pInt = (qint32*)p_pTag->data();
        IOUtils::swap_intp(t_pInt);
        qint32 t_iCmd = t_pInt[0];

        if(t_iCmd == MNE_RT_SET_CLIENT_ALIAS)
        {
            //
            // Set Client Alias
            //
            m_sDataClientAlias = QString(p_pTag->mid(4, p_pTag->size()-4));
            printf("FiffStreamClient (ID %d): new alias = '%s'\r\n\n", m_iDataClientId, m_sDataClientAlias.toUtf8().constData());
        }
        else if(t_iCmd == MNE_RT_GET_CLIENT_ID)
        {
            //
            // Send Client ID
            //
            printf("FiffStreamClient (ID %d): send client ID %d\r\n\n", m_iDataClientId, m_iDataClientId);
            writeClientId();
        }
        else
        {
            printf("FiffStreamClient (ID %d): unknown command\r\n\n", m_iDataClientId);
        }
    }
    else
    {
        printf("FiffStreamClient (ID %d): unknown command\r\n\n", m_iDataClientId);
    }
}
Example #2
0
bool FiffTag::read_tag(FiffStream* p_pStream, FiffTag::SPtr& p_pTag, qint64 pos)
{
    if (pos >= 0)
    {
        p_pStream->device()->seek(pos);
    }

    p_pTag = FiffTag::SPtr(new FiffTag());

    //
    // Read fiff tag header from stream
    //
    *p_pStream  >> p_pTag->kind;
    *p_pStream  >> p_pTag->type;
    qint32 size;
    *p_pStream  >> size;
    p_pTag->resize(size);
    *p_pStream  >> p_pTag->next;

//    qDebug() << "read_tag" << "  Kind:" << p_pTag->kind << "  Type:" << p_pTag->type << "  Size:" << p_pTag->size() << "  Next:" << p_pTag->next;

    //
    // Read data when available
    //
    if (p_pTag->size() > 0)
    {
        p_pStream->readRawData(p_pTag->data(), p_pTag->size());
        FiffTag::convert_tag_data(p_pTag,FIFFV_BIG_ENDIAN,FIFFV_NATIVE_ENDIAN);
    }

    if (p_pTag->next != FIFFV_NEXT_SEQ)
        p_pStream->device()->seek(p_pTag->next);//fseek(fid,tag.next,'bof');

    return true;
}
Example #3
0
bool FiffTag::read_tag_info(FiffStream* p_pStream, FiffTag::SPtr &p_pTag, bool p_bDoSkip)
{
    p_pTag = FiffTag::SPtr(new FiffTag());

    //Option 1
//    t_DataStream.readRawData((char *)p_pTag, FIFFC_TAG_INFO_SIZE);
//    p_pTag->kind = Fiff::swap_int(p_pTag->kind);
//    p_pTag->type = Fiff::swap_int(p_pTag->type);
//    p_pTag->size = Fiff::swap_int(p_pTag->size);
//    p_pTag->next = Fiff::swap_int(p_pTag->next);

    //Option 2
    *p_pStream  >> p_pTag->kind;
    *p_pStream  >> p_pTag->type;
    qint32 size;
    *p_pStream  >> size;
    p_pTag->resize(size);
    *p_pStream  >> p_pTag->next;

//    qDebug() << "read_tag_info" << "  Kind:" << p_pTag->kind << "  Type:" << p_pTag->type << "  Size:" << p_pTag->size() << "  Next:" << p_pTag->next;

    if (p_bDoSkip)
    {
        QTcpSocket* t_qTcpSocket = qobject_cast<QTcpSocket*>(p_pStream->device());
        if(t_qTcpSocket)
        {
            p_pStream->skipRawData(p_pTag->size());
        }
        else
        {
            if (p_pTag->next == FIFFV_NEXT_SEQ)
            {
                p_pStream->device()->seek(p_pStream->device()->pos()+p_pTag->size()); //fseek(fid,tag.size,'cof');
            }
            else if (p_pTag->next > 0)
            {
                p_pStream->device()->seek(p_pTag->next); //fseek(fid,tag.next,'bof');
            }
        }
    }

    return true;
}
bool FiffDirTree::find_tag(FiffStream* p_pStream, fiff_int_t findkind, FiffTag::SPtr& p_pTag) const
{
    for (qint32 p = 0; p < this->nent; ++p)
    {
       if (this->dir[p].kind == findkind)
       {
          FiffTag::read_tag(p_pStream,p_pTag,this->dir[p].pos);
          return true;
       }
    }
    if (p_pTag)
        p_pTag.clear();

    return false;
}
Example #5
0
bool FiffTag::read_tag_data(FiffStream* p_pStream, FiffTag::SPtr& p_pTag, qint64 pos)
{
    if(pos >= 0)
    {
        p_pStream->device()->seek(pos);
    }

    if(!p_pTag)
        return false;

    //
    // Read data when available
    //
    if (p_pTag->size() > 0)
    {
        p_pStream->readRawData(p_pTag->data(), p_pTag->size());
        FiffTag::convert_tag_data(p_pTag,FIFFV_BIG_ENDIAN,FIFFV_NATIVE_ENDIAN);
    }

    if (p_pTag->next != FIFFV_NEXT_SEQ)
        p_pStream->device()->seek(p_pTag->next);//fseek(fid,tag.next,'bof');

    return true;
}
Example #6
0
bool FiffTag::read_rt_tag(FiffStream* p_pStream, FiffTag::SPtr& p_pTag)
{
    while(p_pStream->device()->bytesAvailable() < 16)
        p_pStream->device()->waitForReadyRead(10);

    if(!FiffTag::read_tag_info(p_pStream, p_pTag, false))
        return false;

    while(p_pStream->device()->bytesAvailable() < p_pTag->size())
        p_pStream->device()->waitForReadyRead(10);

    if(!FiffTag::read_tag_data(p_pStream, p_pTag))
        return false;

    return true;
}
Example #7
0
int ShmemSocket::receive_tag (FiffTag::SPtr& p_pTag)
{
    struct  sockaddr_un from;	/* Address (not used) */
    socklen_t fromlen;

    dacqDataMessageRec mess;	/* This is the kind of message we receive */
    int rlen;
    int data_ok = 0;

    p_pTag = FiffTag::SPtr(new FiffTag());
    dacqShmBlock  shmem = this->get_shmem();
    dacqShmBlock  shmBlock;
    dacqShmClient shmClient;
    int           k;


    long read_loc = 0;

    if (m_iShmemSock < 0)
        return (OK);

    //
    // read from the socket
    //
    fromlen = sizeof(from);
    rlen = recvfrom(m_iShmemSock, (void *)(&mess), DATA_MESS_SIZE, 0, (sockaddr *)(&from), &fromlen);

//    qDebug() << "Mess Kind: " << mess.kind << " Type: " << mess.type << "Size: " << mess.size;

    //
    // Parse received message
    //
    if (rlen == -1)
    {
        printf("recvfrom");//dacq_perror("recvfrom");
        this->close_socket ();
        return (FAIL);
    }

    /* A sanity check to survive at least some crazy messages */

    if (mess.kind > 20000 || (unsigned long) mess.size > (size_t) 100000000)
    {
        printf("ALERT: Unreasonable data received, skipping! (size=%d)(kind=%d)", mess.kind, mess.size);
        //dacq_log("ALERT: Unreasonable data received, skipping! (size=%d)(kind=%d)", mess.kind, mess.size);
        mess.size = 0;
        mess.kind = FIFF_NOP;
    }

    p_pTag->kind = mess.kind;
    p_pTag->type = mess.type;
    p_pTag->next = 0;

    if ((unsigned long) mess.size > (size_t) 0)
    {
        p_pTag->resize(mess.size);
    }

//    qDebug() << mess.loc << " " << mess.size << " " << mess.shmem_buf << " " << mess.shmem_loc;

    if (mess.loc < 0 && (unsigned long) mess.size > (size_t) 0 && mess.shmem_buf < 0 && mess.shmem_loc < 0)
    {
        fromlen = sizeof(from);
        rlen = recvfrom(m_iShmemSock, (void *)p_pTag->data(), mess.size, 0, (sockaddr *)(&from), &fromlen);
        if (rlen == -1)
        {
            printf("recvfrom");//dacq_perror("recvfrom");
            this->close_socket();
            return (FAIL);
        }
        data_ok = 1;
//        if (mess.type == FIFFT_STRING)
//            data[mess.size] = '\0';
    }
    else if ((unsigned long) mess.size > (size_t) 0) {
        /*
         * Copy data from shared memory
         */
        if (mess.shmem_buf >= 0 && m_iShmemId/10000 > 0)
        {
            shmBlock  = shmem + mess.shmem_buf;
            shmClient = shmBlock->clients;

            if (interesting_data(mess.kind))
            {
                memcpy(p_pTag->data(),shmBlock->data,mess.size);
                data_ok = 1;
            #ifdef DEBUG
                printf("client # %d read shmem buffer # %d\n", m_iShmemId, mess.shmem_buf);//dacq_log("client # %d read shmem buffer # %d\n", id,mess.shmem_buf);
            #endif
            }
            /*
            * Indicate that this client has processed the data
            */
            for (k = 0; k < SHM_MAX_CLIENT; k++,shmClient++)
                if (shmClient->client_id == m_iShmemId)
                    shmClient->done = 1;
        }
        /*
        * Read data from file
        */
        else {
            /*
            * Possibly read from shmem file
            */
            if (m_iShmemId/10000 > 0 && mess.shmem_loc >= 0) {
                read_fd  = shmem_fd;
                read_loc = mess.shmem_loc;
            }
            else {
                read_fd  = fd;
                read_loc = mess.loc;
            }
            if (interesting_data(mess.kind)) {
                if (read_fif (read_fd,read_loc,mess.size,(char *)p_pTag->data()) == -1) {
                    printf("Could not read data (tag = %d, size = %d, pos = %li)!\n", mess.kind,mess.size,read_loc);//dacq_log("Could not read data (tag = %d, size = %d, pos = %d)!\n", mess.kind,mess.size,read_loc);
                    //dacq_log("%s\n",err_get_error());
                }
                else {
                    data_ok = 1;
//                    if (mess.type == FIFFT_STRING)
//                        data[mess.size] = '\0';
                    FiffTag::convert_tag_data(p_pTag,FIFFV_BIG_ENDIAN,FIFFV_NATIVE_ENDIAN);
                }
            }
        }
    }

    /*
    * Special case: close old input file
    */
    if (mess.kind == FIFF_CLOSE_FILE) {
        if (fd != NULL) {
            printf("File to be closed (lib/FIFF_CLOSE_FILE).\n");//dacq_log("File to be closed (lib/FIFF_CLOSE_FILE).\n");
            (void)fclose(fd);
            fd = NULL;
        }
        else
            printf("No file to close (lib/FIFF_CLOSE_FILE).\n");//dacq_log("No file to close (lib/FIFF_CLOSE_FILE).\n");
    }
    /*
    * Another special case: open new input file
    */
    else if (mess.kind == FIFF_NEW_FILE) {
        if (fd != NULL) {
            (void)fclose(fd);
            printf("File closed (lib/FIFF_NEW_FILE).\n");//dacq_log("File closed (lib/FIFF_NEW_FILE).\n");
        }
        fd = open_fif((char *)p_pTag->data());
        free (filename);
        filename = strdup((char *)p_pTag->data());

        if (shmem_fd == NULL)
            shmem_fd = open_fif (SHM_FAIL_FILE);
    }

    if (p_pTag->size() <= 0)
    {
        data_ok  = 0;
        return (FAIL);
    }
    return (OK);
}
Example #8
0
bool MneFiffExpSet::show_fiff_contents(FILE *out, const QString &name, bool verbose, const QList<int> &tags, int indent_step, bool long_strings, bool blocks_only)
{
    QFile file(name);
    FiffStream::SPtr stream(new FiffStream(&file));

    FiffDirEntry::SPtr this_ent;
    FiffTag::SPtr   tag;
    int             day,month,year;
    int             block;
    int             count = 0;
    int             prev_kind;
    int             indent = 0;
    bool            show_it = false;
    QString         s;
    bool            output_taginfo = false;
    QList<MneFiffExp>::const_iterator exp;

    if (!stream->open())
        return false;
    prev_kind = -1;

    if (blocks_only) {
        //        for (auto this_ent : stream->dir()) {//C++11
        for (int i = 0; i < stream->dir().size(); ++i) {
            this_ent = stream->dir()[i];
            if (this_ent->kind == FIFF_BLOCK_START || this_ent->kind == FIFF_BLOCK_END) {
                if (this_ent->kind == FIFF_BLOCK_END)
                    indent = indent - indent_step;
                if (this_ent->kind == FIFF_BLOCK_START) {
                    for (int k = 0; k < indent; k++)
                        fprintf(out," ");
                    if ( stream->read_tag(tag, this_ent->pos) ) {
                        block = *tag->toInt();
                        exp = this->find_fiff_explanation(CLASS_BLOCK,block);
                        if (exp != this->constEnd())
                            fprintf(out,"%-d = %-s\n",exp->kind,exp->text.toUtf8().constData());
                        else
                            fprintf(out,"%-d = %-s\n",block,"Not explained");
                    }
                }
                if (this_ent->kind == FIFF_BLOCK_START)
                    indent = indent + indent_step;
            }
        }
    }
    else {
        //        for (auto this_ent : stream->dir()) {//C++11
        for (int i = 0; i < stream->dir().size(); ++i) {
            this_ent = stream->dir()[i];
            if (tags.size() == 0)
                show_it = true;
            else {
                show_it = false;
                for (int k = 0; k < tags.size(); k++) {
                    if (this_ent->kind == tags[k]) {
                        show_it = true;
                        break;
                    }
                }
            }
            if (show_it) {
                if (this_ent->kind == FIFF_BLOCK_START || this_ent->kind == FIFF_BLOCK_END) {
                    if (!verbose) {
                        if (count > 1)
                            fprintf(out," [%d]\n",count);
                        else if (this_ent != stream->dir()[0])
                            fprintf(out,"\n");
                    }
                    if (this_ent->kind == FIFF_BLOCK_END)
                        indent = indent - indent_step;
                    for (int k = 0; k < indent; k++)
                        fprintf(out," ");
                    exp = this->find_fiff_explanation(CLASS_TAG,this_ent->kind);
                    if (exp != this->constEnd())
                        fprintf(out,"%4d = %-s",exp->kind,exp->text.toUtf8().constData());
                    else
                        fprintf(out,"%4d = %-s",this_ent->kind,"Not explained");
                    if ( stream->read_tag(tag,this_ent->pos)) {
                        block = *tag->toInt();
                        exp = this->find_fiff_explanation(CLASS_BLOCK,block);
                        if (exp != this->constEnd())
                            fprintf(out,"\t%-d = %-s",exp->kind,exp->text.toUtf8().constData());
                        else
                            fprintf(out,"\t%-d = %-s",block,"Not explained");
                    }
                    if ( this_ent->kind == FIFF_BLOCK_START)
                        indent = indent + indent_step;
                    count = 1;
                    if (verbose)
                        fprintf(out,"\n");
                }
                else if (verbose) {
                    for (int k = 0; k < indent; k++)
                        fprintf(out," ");
                    if (output_taginfo) {
                        fprintf(out,"%d %d ",this_ent->size,this_ent->type);
                    }
                    exp = this->find_fiff_explanation(CLASS_TAG,this_ent->kind);
                    if (exp != this->constEnd())
                        fprintf(out,"%4d = %-18s",exp->kind,exp->text.toUtf8().constData());
                    else
                        fprintf(out,"%4d = %-18s",this_ent->kind,"Not explained");
                    if (FiffTag::fiff_type_fundamental(this_ent->type) == FIFFTS_FS_MATRIX) {
                        fprintf(out,"TODO print_matrix");
                        //                        print_matrix(out,in,this_ent);
                    }
                    else {
                        switch (this_ent->type) {
                        case FIFFT_INT :
                            if (stream->read_tag(tag,this_ent->pos)) {
                                if (this_ent->kind == FIFF_BLOCK_START ||
                                        this_ent->kind == FIFF_BLOCK_END) {
                                    block = *tag->toInt();
                                    exp = this->find_fiff_explanation(CLASS_BLOCK,block);
                                    if (exp != this->constEnd())
                                        fprintf(out,"\t%-d = %s",exp->kind,exp->text.toUtf8().constData());
                                    else
                                        fprintf(out,"\t%-d = %-s",block,"Not explained");
                                }
                                else if (this_ent->kind == FIFF_MEAS_DATE) {
                                    QDateTime ltime;
                                    ltime.setTime_t(tag->toInt()[0]);
                                    fprintf(out,"\t%s",ltime.toString().toUtf8().constData());
                                }
                                else if (tag->size() == sizeof(fiff_int_t))
                                    fprintf(out,"\t%d",*tag->toInt());
                                else
                                    fprintf(out,"\t%d ints",(int)(tag->size()/sizeof(fiff_int_t)));
                            }
                            break;
                        case FIFFT_UINT :
                            if (stream->read_tag(tag,this_ent->pos)) {
                                if (tag->size() == sizeof(fiff_int_t))
                                    fprintf(out,"\t%d",*tag->toUnsignedInt());
                                else
                                    fprintf(out,"\t%d u_ints",(int)(tag->size()/sizeof(fiff_int_t)));
                            }
                            break;
                        case FIFFT_JULIAN :
                            if (stream->read_tag(tag,this_ent->pos)) {
                                fprintf(out,"TODO fiff_caldate");
                                //                                fiff_caldate (*(fiff_julian_t *)tag.data,&day,&month,&year);
                                fprintf(out,"\t%d.%d.%d",day,month,year);
                            }
                            break;
                        case FIFFT_STRING :
                            if (stream->read_tag(tag,this_ent->pos)) {
                                s = tag->toString();
                                if (long_strings)
                                    fprintf(out,"\t%s",tag->toString().toUtf8().constData());
                                else {
                                    if ((s.indexOf("\n")) != -1)
                                        s.replace(s.indexOf("\n"), 2, "\0");
                                    else if (s.size() > LONG_LINE) {
                                        s.truncate(LONG_LINE);
                                        s += "...";
                                    }
                                    fprintf(out,"\t%s",s.toUtf8().constData());
                                }
                            }
                            break;
                        case FIFFT_FLOAT :
                            if (stream->read_tag(tag,this_ent->pos)) {
                                if (tag->size() == sizeof(fiff_float_t))
                                    fprintf(out,"\t%g",*tag->toFloat());
                                else
                                    fprintf(out,"\t%d floats",(int)(tag->size()/sizeof(fiff_float_t)));
                            }
                            break;
                        case FIFFT_DOUBLE :
                            if (stream->read_tag(tag,this_ent->pos)) {
                                if (tag->size() == sizeof(fiff_double_t))
                                    fprintf(out,"\t%g",*tag->toDouble());
                                else
                                    fprintf(out,"\t%d doubles",(int)(tag->size()/sizeof(fiff_double_t)));
                            }
                            break;
                        case FIFFT_COMPLEX_FLOAT :
                            if (stream->read_tag(tag,this_ent->pos)) {
                                float *fdata = tag->toFloat();
                                if (tag->size() == 2*sizeof(fiff_float_t))
                                    fprintf(out,"\t(%g %g)",fdata[0],fdata[1]);
                                else
                                    fprintf(out,"\t%d complex numbers",
                                            (int)(tag->size()/(2*sizeof(fiff_float_t))));
                            }
                            break;
                        case FIFFT_COMPLEX_DOUBLE :
                            if (stream->read_tag(tag,this_ent->pos)) {
                                double *ddata = tag->toDouble();
                                if (tag->size() == 2*sizeof(fiff_double_t))
                                    fprintf(out,"\t(%g %g)",ddata[0],ddata[1]);
                                else
                                    fprintf(out,"\t%d double complex numbers",
                                            (int)(tag->size()/(2*sizeof(fiff_double_t))));
                            }
                            break;
                        case FIFFT_CH_INFO_STRUCT :
                            if (stream->read_tag(tag,this_ent->pos))
                                fprintf(out,"TODO print_ch_info");
                            //                                print_ch_info (out,set,(fiff_ch_info_t *)tag.data);
                            break;
                        case FIFFT_ID_STRUCT :
                            if (stream->read_tag(tag,this_ent->pos))
                                fprintf(out,"TODO print_file_id");
                            //                                if (tag.size == sizeof(fiff_id_t))
                            //                                    print_file_id (out,(fiff_id_t *)tag.data);
                            break;
                        case FIFFT_DIG_POINT_STRUCT :
                            if (stream->read_tag(tag,this_ent->pos))
                                fprintf(out,"TODO print_dig_point");
                            //                                if (tag.size == sizeof(fiff_dig_point_t))
                            //                                    print_dig_point (out,(fiff_dig_point_t *)tag.data);
                            break;
                        case FIFFT_DIG_STRING_STRUCT :
                            if (stream->read_tag(tag,this_ent->pos)) {
#ifdef FOO
                                if ((ds = decode_fiff_dig_string(&tag)) != NULL)
                                    print_dig_string (ds);
                                free_fiff_dig_string(ds);
#endif
                            }
                            break;
                        case FIFFT_COORD_TRANS_STRUCT :
                            if (stream->read_tag(tag,this_ent->pos))
                                fprintf(out,"TODO print_transform");
                            //                                if (tag.size == sizeof(fiff_coord_trans_t))
                            //                                    print_transform   (out,(fiff_coord_trans_t *)tag.data);
                            break;
                        default :
                            if (this_ent->kind == FIFF_DIG_STRING)
                                fprintf(out,"type = %d\n",this_ent->type);
                            if (this_ent->size > 0)
                                fprintf(out,"\t%d bytes",this_ent->size);
                            break;
                        }
                    }
                    fprintf(out,"\n");
                    prev_kind = this_ent->kind;
                }
                else {
                    if (this_ent->kind != prev_kind) {
                        if (count > 1)
                            fprintf(out," [%d]\n",count);
                        else if (this_ent != stream->dir()[0])
                            fprintf(out,"\n");
                        for (int k = 0; k < indent; k++)
                            fprintf(out," ");
                        exp = this->find_fiff_explanation(CLASS_TAG,this_ent->kind);
                        if (exp != this->constEnd())
                            fprintf(out,"%4d = %-s",exp->kind,exp->text.toUtf8().constData());
                        else
                            fprintf(out,"%4d = %-s",this_ent->kind,"Not explained");
                        count = 1;
                    }
                    else
                        count++;
                }
            }
            prev_kind = this_ent->kind;
        }
        if (!verbose) {
            if (count > 1)
                fprintf(out," [%d]\n",count);
            else
                fprintf(out,"\n");
        }
    }

    stream->close();

    return true;
}
Example #9
0
//*************************************************************************************************************
//ToDo remove this function by swapping -> define little endian big endian, QByteArray
void FiffTag::convert_tag_data(FiffTag::SPtr tag, int from_endian, int to_endian)
{
    int            np;
    int            k,r;//,c;
    char           *offset;
    fiff_int_t     *ithis;
    fiff_short_t   *sthis;
    fiff_long_t    *lthis;
    float          *fthis;
    double         *dthis;
//    fiffDirEntry   dethis;
//    fiffId         idthis;
//    fiffChInfoRec* chthis;//FiffChInfo*     chthis;//ToDo adapt parsing to the new class
//    fiffChPos      cpthis;
//    fiffCoordTrans ctthis;
//    fiffDigPoint   dpthis;
    fiffDataRef    drthis;

    if (tag->data() == NULL || tag->size() == 0)
        return;

    if (from_endian == FIFFV_NATIVE_ENDIAN)
        from_endian = NATIVE_ENDIAN;
    if (to_endian == FIFFV_NATIVE_ENDIAN)
        to_endian = NATIVE_ENDIAN;

    if (from_endian == to_endian)
        return;

    if (fiff_type_fundamental(tag->type) == FIFFTS_FS_MATRIX) {
        if (from_endian == NATIVE_ENDIAN)
            convert_matrix_to_file_data(tag);
        else
            convert_matrix_from_file_data(tag);
        return;
    }

    switch (tag->type) {

    case FIFFT_INT :
    case FIFFT_JULIAN :
    case FIFFT_UINT :
        np = tag->size()/sizeof(fiff_int_t);
        for (ithis = (fiff_int_t *)tag->data(), k = 0; k < np; k++, ithis++)
            IOUtils::swap_intp(ithis);
        break;

    case FIFFT_LONG :
    case FIFFT_ULONG :
        np = tag->size()/sizeof(fiff_long_t);
        for (lthis = (fiff_long_t *)tag->data(), k = 0; k < np; k++, lthis++)
            IOUtils::swap_longp(lthis);
        break;

    case FIFFT_SHORT :
    case FIFFT_DAU_PACK16 :
    case FIFFT_USHORT :
        np = tag->size()/sizeof(fiff_short_t);
        for (sthis = (fiff_short_t *)tag->data(), k = 0; k < np; k++, sthis++)
            *sthis = IOUtils::swap_short(*sthis);
        break;

    case FIFFT_FLOAT :
    case FIFFT_COMPLEX_FLOAT :
        np = tag->size()/sizeof(fiff_float_t);
        for (fthis = (fiff_float_t *)tag->data(), k = 0; k < np; k++, fthis++)
            IOUtils::swap_floatp(fthis);
        break;

    case FIFFT_DOUBLE :
    case FIFFT_COMPLEX_DOUBLE :
        np = tag->size()/sizeof(fiff_double_t);
        for (dthis = (fiff_double_t *)tag->data(), k = 0; k < np; k++, dthis++)
            IOUtils::swap_doublep(dthis);
        break;

    case FIFFT_OLD_PACK :
        fthis = (float *)tag->data();
    /*
     * Offset and scale...
     */
        IOUtils::swap_floatp(fthis+0);
        IOUtils::swap_floatp(fthis+1);
        sthis = (short *)(fthis+2);
        np = (tag->size() - 2*sizeof(float))/sizeof(short);
        for (k = 0; k < np; k++,sthis++)
            *sthis = IOUtils::swap_short(*sthis);
        break;

    case FIFFT_DIR_ENTRY_STRUCT :
//        np = tag->size/sizeof(fiffDirEntryRec);
//        for (dethis = (fiffDirEntry)tag->data->data(), k = 0; k < np; k++, dethis++) {
//            dethis->kind = swap_int(dethis->kind);
//            dethis->type = swap_int(dethis->type);
//            dethis->size = swap_int(dethis->size);
//            dethis->pos  = swap_int(dethis->pos);
//        }
        np = tag->size()/FiffDirEntry::storageSize();
        for (k = 0; k < np; k++) {
            offset = (char*)tag->data() + k*FiffDirEntry::storageSize();
            ithis = (fiff_int_t*) offset;
            ithis[0] = IOUtils::swap_int(ithis[0]);//kind
            ithis[1] = IOUtils::swap_int(ithis[1]);//type
            ithis[2] = IOUtils::swap_int(ithis[2]);//size
            ithis[3] = IOUtils::swap_int(ithis[3]);//pos
        }
        break;

    case FIFFT_ID_STRUCT :
//        np = tag->size/sizeof(fiffIdRec);
//        for (idthis = (fiffId)tag->data->data(), k = 0; k < np; k++, idthis++) {
//            idthis->version = swap_int(idthis->version);
//            idthis->machid[0] = swap_int(idthis->machid[0]);
//            idthis->machid[1] = swap_int(idthis->machid[1]);
//            idthis->time.secs  = swap_int(idthis->time.secs);
//            idthis->time.usecs = swap_int(idthis->time.usecs);
//        }
        np = tag->size()/FiffId::storageSize();
        for (k = 0; k < np; k++) {
            offset = (char*)tag->data() + k*FiffId::storageSize();
            ithis = (fiff_int_t*) offset;
            ithis[0] = IOUtils::swap_int(ithis[0]);//version
            ithis[1] = IOUtils::swap_int(ithis[1]);//machid[0]
            ithis[2] = IOUtils::swap_int(ithis[2]);//machid[1]
            ithis[3] = IOUtils::swap_int(ithis[3]);//time.secs
            ithis[4] = IOUtils::swap_int(ithis[4]);//time.usecs
        }
        break;

    case FIFFT_CH_INFO_STRUCT :
//        np = tag->size/sizeof(fiffChInfoRec);
//        for (chthis = (fiffChInfoRec*)tag->data->data(), k = 0; k < np; k++, chthis++) {
//            chthis->scanNo    = swap_int(chthis->scanNo);
//            chthis->logNo     = swap_int(chthis->logNo);
//            chthis->kind      = swap_int(chthis->kind);
//            swap_floatp(&chthis->range);
//            swap_floatp(&chthis->cal);
//            chthis->unit      = swap_int(chthis->unit);
//            chthis->unit_mul  = swap_int(chthis->unit_mul);
//            convert_ch_pos(&(chthis->chpos));
//        }

        np = tag->size()/FiffChInfo::storageSize();
        for (k = 0; k < np; k++) {
            offset = (char*)tag->data() + k*FiffChInfo::storageSize();
            ithis = (fiff_int_t*) offset;
            fthis = (float*) offset;

            ithis[0] = IOUtils::swap_int(ithis[0]);  //scanno
            ithis[1] = IOUtils::swap_int(ithis[1]);  //logno
            ithis[2] = IOUtils::swap_int(ithis[2]);  //kind
            IOUtils::swap_floatp(&fthis[3]);         //range
            IOUtils::swap_floatp(&fthis[4]);         //cal
            ithis[5] = IOUtils::swap_int(ithis[5]);  //coil_type
            for (r = 0; r < 12; ++r)
                IOUtils::swap_floatp(&fthis[6+r]);   //loc
            ithis[18] = IOUtils::swap_int(ithis[18]);//unit
            ithis[19] = IOUtils::swap_int(ithis[19]);//unit_mul
        }

        break;

    case FIFFT_CH_POS_STRUCT :
//        np = tag->size/sizeof(fiffChPosRec);
//        for (cpthis = (fiffChPos)tag->data->data(), k = 0; k < np; k++, cpthis++)
//            convert_ch_pos(cpthis);

        np = tag->size()/FiffChPos::storageSize();
        for (k = 0; k < np; ++k)
        {
            offset = (char*)tag->data() + k*FiffChPos::storageSize();
            ithis = (fiff_int_t*) offset;
            fthis = (float*) offset;

            ithis[0] = IOUtils::swap_int(ithis[0]);    //coil_type
            for (r = 0; r < 12; r++)
                IOUtils::swap_floatp(&fthis[1+r]);    //r0, ex, ey, ez
        }

        break;

    case FIFFT_DIG_POINT_STRUCT :
//        np = tag->size/sizeof(fiffDigPointRec);
//        for (dpthis = (fiffDigPoint)tag->data->data(), k = 0; k < np; k++, dpthis++) {
//            dpthis->kind = swap_int(dpthis->kind);
//            dpthis->ident = swap_int(dpthis->ident);
//            for (r = 0; r < 3; r++)
//                swap_floatp(&dpthis->r[r]);
//        }

        np = tag->size()/FiffDigPoint::storageSize();

        for (k = 0; k < np; k++) {
            offset = tag->data() + k*FiffDigPoint::storageSize();
            ithis = (fiff_int_t*) offset;
            fthis = (float*) offset;

            ithis[0] = IOUtils::swap_int(ithis[0]);//kind
            ithis[1] = IOUtils::swap_int(ithis[1]);//ident

            for (r = 0; r < 3; ++r)
                IOUtils::swap_floatp(&fthis[2+r]);        //r
        }
        break;

    case FIFFT_COORD_TRANS_STRUCT :
//        np = tag->size/sizeof(fiffCoordTransRec);
//        for (ctthis = (fiffCoordTrans)tag->data->data(), k = 0; k < np; k++, ctthis++) {
//            ctthis->from = swap_int(ctthis->from);
//            ctthis->to   = swap_int(ctthis->to);
//        for (r = 0; r < 3; r++) {
//             swap_floatp(&ctthis->move[r]);
//             swap_floatp(&ctthis->invmove[r]);
//            for (c = 0; c < 3; c++) {
//                swap_floatp(&ctthis->rot[r][c]);
//                swap_floatp(&ctthis->invrot[r][c]);
//            }
//        }
//    }

        np = tag->size()/FiffCoordTrans::storageSize();

        for( k = 0; k < np; ++k)
        {
            offset = tag->data() + k*FiffCoordTrans::storageSize();
            ithis = (fiff_int_t*)offset;
            fthis = (float*)offset;

            ithis[0] = IOUtils::swap_int(ithis[0]);
            ithis[1] = IOUtils::swap_int(ithis[1]);

            for (r = 0; r < 24; ++r)
                IOUtils::swap_floatp(&fthis[2+r]);
        }
        break;

    case FIFFT_DATA_REF_STRUCT :
        np = tag->size()/sizeof(fiffDataRefRec);
        for (drthis = (fiffDataRef)tag->data(), k = 0; k < np; k++, drthis++) {
            drthis->type   = IOUtils::swap_int(drthis->type);
            drthis->endian = IOUtils::swap_int(drthis->endian);
            drthis->size   = IOUtils::swap_long(drthis->size);
            drthis->offset = IOUtils::swap_long(drthis->offset);
        }
        break;

    default :
        break;
    }
    return;
}
Example #10
0
void FiffTag::convert_matrix_to_file_data(FiffTag::SPtr tag)
/*
 * Assumes that the input is in the NATIVE_ENDIAN byte order and needs to be swapped to the other one
 */
{
    int ndim;
    int k;
    int *dimp,*data,kind,np;
    float *fdata;
    double *ddata;
    unsigned int tsize = tag->size();

    if (fiff_type_fundamental(tag->type) != FIFFTS_FS_MATRIX)
        return;
    if (tag->data() == NULL)
        return;
    if (tsize < sizeof(fiff_int_t))
        return;

    dimp = ((fiff_int_t *)(((char *)tag->data())+tag->size()-sizeof(fiff_int_t)));
    ndim = *dimp;
    IOUtils::swap_intp(dimp);

    if (fiff_type_matrix_coding(tag->type) == FIFFTS_MC_DENSE) {
        if (tsize < (ndim+1)*sizeof(fiff_int_t))
            return;
        dimp = dimp - ndim;
        for (k = 0, np = 1; k < ndim; k++) {
            np = np*dimp[k];
            IOUtils::swap_intp(dimp+k);
        }
    }
    else {
        if (tsize < (ndim+2)*sizeof(fiff_int_t))
            return;
        if (ndim > 2)		/* Not quite sure what to do */
            return;
        dimp = dimp - ndim - 1;
        if (fiff_type_matrix_coding(tag->type) == FIFFTS_MC_CCS)
            np = dimp[0] + dimp[2] + 1; /* nz + n + 1 */
        else if (fiff_type_matrix_coding(tag->type) == FIFFTS_MC_RCS)
            np = dimp[0] + dimp[1] + 1; /* nz + m + 1 */
        else
            return;			/* Don't know what to do */
        for (k = 0; k < ndim+1; k++)
            IOUtils::swap_intp(dimp+k);
    }
    /*
    * Now convert data...
    */
    kind = fiff_type_base(tag->type);
    if (kind == FIFFT_INT) {
        for (data = (int *)(tag->data()), k = 0; k < np; k++)
            IOUtils::swap_intp(data+k);
    }
    else if (kind == FIFFT_FLOAT) {
        for (fdata = (float *)(tag->data()), k = 0; k < np; k++)
            IOUtils::swap_floatp(fdata+k);
    }
    else if (kind == FIFFT_DOUBLE) {
        for (ddata = (double *)(tag->data()), k = 0; k < np; k++)
            IOUtils::swap_doublep(ddata+k);
    }
    else if (kind == FIFFT_COMPLEX_FLOAT) {
        for (fdata = (float *)(tag->data()), k = 0; k < 2*np; k++)
            IOUtils::swap_floatp(fdata+k);
    }
    else if (kind == FIFFT_COMPLEX_DOUBLE) {
        for (ddata = (double *)(tag->data()), k = 0; k < 2*np; k++)
            IOUtils::swap_doublep(ddata+k);
    }
    return;
}