Logo Search packages:      
Sourcecode: edfbrowser version File versions  Download package

emsa2edf.cpp

/*
***************************************************************************
*
* Author: Teunis van Beelen
*
* Copyright (C) 2009 Teunis van Beelen
*
* teuniz@gmail.com
*
***************************************************************************
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
***************************************************************************
*
* This version of GPL is at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
*
***************************************************************************
*/




#include "emsa2edf.h"



UI_EMSA2EDFwindow::UI_EMSA2EDFwindow()
{
  char txt_string[2048];

  myobjectDialog = new QDialog;

  myobjectDialog->setMinimumSize(QSize(600, 480));
  myobjectDialog->setMaximumSize(QSize(600, 480));
  myobjectDialog->setWindowTitle("Emsa to EDF+ converter");
  myobjectDialog->setModal(TRUE);
  myobjectDialog->setAttribute(Qt::WA_DeleteOnClose, TRUE);

  pushButton1 = new QPushButton(myobjectDialog);
  pushButton1->setGeometry(QRect(20, 430, 100, 26));
  pushButton1->setText("Select File");

  pushButton2 = new QPushButton(myobjectDialog);
  pushButton2->setGeometry(QRect(480, 430, 100, 26));
  pushButton2->setText("Close");

  textEdit1 = new QTextEdit(myobjectDialog);
  textEdit1->setGeometry(QRect(20, 20, 560, 380));
  textEdit1->setFrameStyle(QFrame::Panel | QFrame::Sunken);
  textEdit1->setReadOnly(TRUE);
  textEdit1->setLineWrapMode(QTextEdit::NoWrap);
  sprintf(txt_string, "Emsa to EDF+ converter.\n");
  textEdit1->append(txt_string);

  fchooser = new QFileDialog(myobjectDialog);
  fchooser->setConfirmOverwrite(1);
  fchooser->setFileMode(QFileDialog::ExistingFile);
  fchooser->setAcceptMode(QFileDialog::AcceptOpen);
  fchooser->setWindowTitle("Select inputfile");
  fchooser->setLabelText(QFileDialog::FileName, "input file");
#ifdef Q_WS_X11
  fchooser->setDirectory(getenv("HOME"));
#endif
#ifdef Q_WS_MAC
  fchooser->setDirectory(getenv("HOME"));
#endif
  fchooser->setDefaultSuffix("plg");
  fchooser->setFilter("PLG files (*.plg *.PLG)");

  QObject::connect(pushButton1, SIGNAL(clicked()), this, SLOT(SelectFileButton()));
  QObject::connect(pushButton2, SIGNAL(clicked()), myobjectDialog, SLOT(close()));

  myobjectDialog->exec();
}






void UI_EMSA2EDFwindow::SelectFileButton()
{
  FILE *inputfile=NULL,
       *outputfile=NULL,
       *logfile=NULL;

  const char *fileName;

  int i, j, k,
      pathlen,
      fname_len,
      channels,
      record_size,
      samplefrequency,
      hdrsize,
      samples_in_record,
      bufsize,
      max_buf_records,
      raster,
      data_records,
      records_in_buf,
      error,
      len,
      seq_nr,
      year,
      b_year,
      b_date,
      seconds,
      deci_seconds,
      datrecduris1,
      gender,
      tallen=0,
      annot_cnt=0,
      patientcode=0,
      annots_written=0,
      time_on,
      time_off,
      duration_present=0;

  long long filesize;

  char path[1024],
       scratchpad[256],
       *buf,
       date[9],
       time[9],
       month[4],
       patientname[128],
       b_month[4],
       *p,
       equipment[41],
       handedness[16],
       weight[16],
       birthdate[16],
       txt_string[2048];

  union{
         int number;
         char str[4];
       } var;

  QStringList  input_fileNames;

  QString input_fileName;


  pushButton1->setEnabled(FALSE);

  if(!(fchooser->exec() == QDialog::Accepted))
  {
    pushButton1->setEnabled(TRUE);
    return;
  }
  input_fileNames = fchooser->selectedFiles();
  input_fileName = input_fileNames.at(0);
  strcpy(path, input_fileName.toLatin1().data());

  pathlen = strlen(path);

  if(pathlen<5)
  {
    textEdit1->append("error, filename must contain at least five characters.");
    pushButton1->setEnabled(TRUE);
    return;
  }

  fname_len = 0;
  for(i=pathlen; i>0; i--)
  {
       if((path[i-1]=='/')||(path[i-1]=='\\'))  break;
       fname_len++;
  }
  fileName = path + pathlen - fname_len;

  for(i=0; fileName[i]!=0; i++) ;
  if(i==0)
  {
    textEdit1->append("error, filename must contain at least five characters.");
    pushButton1->setEnabled(TRUE);
    return;
  }

  i -= 4;
  if((strcmp((const char *)fileName + i, ".plg"))&&(strcmp((const char *)fileName + i, ".PLG")))
  {
    textEdit1->append("error, filename extension must have the form \".plg\" or \".PLG\"");
    pushButton1->setEnabled(TRUE);
    return;
  }

/***************** check header ******************************/

  inputfile = fopen64(path, "rb");
  if(inputfile==NULL)
  {
    snprintf(txt_string, 2048, "Can not open file %s for reading.\n", path);
    textEdit1->append(txt_string);
    pushButton1->setEnabled(TRUE);
    return;
  }

  fseeko64(inputfile, 0, SEEK_END);
  filesize = ftello64(inputfile);
  if(filesize<1538)
  {
    textEdit1->append("Error, filesize is too small.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }

  rewind(inputfile);
  if(fread(scratchpad, 3, 1, inputfile)!=1)
  {
    textEdit1->append("Read error.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }
  if((strncmp(scratchpad, "PBJ", 3))&&(strncmp(scratchpad, "BPC", 3)))
  {
    textEdit1->append("Error, file id does not match.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }

  fseeko64(inputfile, 3, SEEK_SET);
  channels = fgetc(inputfile);
  if(channels==EOF)
  {
    textEdit1->append("Read error.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }
  if(!channels)
  {
    textEdit1->append("Error, number of channels is 0.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }

  hdrsize = 1024 + (512 * channels);

  if(filesize<(hdrsize + (2 * channels)))
  {
    textEdit1->append("Error, filesize is too small.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }

  fseeko64(inputfile, 10, SEEK_SET);
  samplefrequency = 256 * fgetc(inputfile);
  fseeko64(inputfile, 9, SEEK_SET);
  samplefrequency += fgetc(inputfile);
  if(!samplefrequency)
  {
    textEdit1->append("Error, samplefrequency is 0 Hz.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }

  if(filesize<(hdrsize + (2 * channels * samplefrequency)))
  {
    textEdit1->append("Error, filesize is too small.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }

  if(get_string(scratchpad, inputfile, 205, 32)!=8)
  {
    textEdit1->append("Error, can not read startdate.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }
  error = 0;
  if((scratchpad[0]<'0')||(scratchpad[0]>'9'))  error = 1;
  if((scratchpad[1]<'0')||(scratchpad[1]>'9'))  error = 1;
  date[6] = scratchpad[2];
  date[7] = scratchpad[3];
  date[3] = scratchpad[4];
  date[4] = scratchpad[5];
  date[0] = scratchpad[6];
  date[1] = scratchpad[7];
  date[2] = 0;
  date[5] = 0;
  date[8] = 0;
  if((date[0]<'0')||(date[0]>'9'))  error = 1;
  if((date[1]<'0')||(date[1]>'9'))  error = 1;
  if((date[3]<'0')||(date[3]>'9'))  error = 1;
  if((date[4]<'0')||(date[4]>'9'))  error = 1;
  if((date[6]<'0')||(date[6]>'9'))  error = 1;
  if((date[7]<'0')||(date[7]>'9'))  error = 1;
  scratchpad[4] = 0;
  year = atoi(scratchpad);

  if(get_string(scratchpad, inputfile, 214, 32)!=8)
  {
    textEdit1->append("Error, can not read starttime.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }
  strcpy(time, scratchpad);
  if(time[2] != ':')  error = 1;
  if(time[5] != ':')  error = 1;
  time[2] = 0;
  time[5] = 0;
  time[8] = 0;
  if((time[0]<'0')||(time[0]>'9'))  error = 1;
  if((time[1]<'0')||(time[1]>'9'))  error = 1;
  if((time[3]<'0')||(time[3]>'9'))  error = 1;
  if((time[4]<'0')||(time[4]>'9'))  error = 1;
  if((time[6]<'0')||(time[6]>'9'))  error = 1;
  if((time[7]<'0')||(time[7]>'9'))  error = 1;
  if((atoi(date)<1)||(atoi(date)>31))  error = 1;
  if((atoi(date+3)<1)||(atoi(date+3)>12))  error = 1;
  if(atoi(time)>23)  error = 1;
  if(atoi(time+3)>59)  error = 1;
  if(atoi(time+6)>59)  error = 1;
  if(error)
  {
    textEdit1->append("Error, can not read startdate or starttime.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }
  date[2] = '.';
  date[5] = '.';
  time[2] = '.';
  time[5] = '.';

  i = atoi(date+3);
  switch(i)
  {
    case  1 : strcpy(month, "JAN");
              break;
    case  2 : strcpy(month, "FEB");
              break;
    case  3 : strcpy(month, "MAR");
              break;
    case  4 : strcpy(month, "APR");
              break;
    case  5 : strcpy(month, "MAY");
              break;
    case  6 : strcpy(month, "JUN");
              break;
    case  7 : strcpy(month, "JUL");
              break;
    case  8 : strcpy(month, "AUG");
              break;
    case  9 : strcpy(month, "SEP");
              break;
    case 10 : strcpy(month, "OCT");
              break;
    case 11 : strcpy(month, "NOV");
              break;
    case 12 : strcpy(month, "DEC");
              break;
    default : strcpy(month, "ERR");
              break;
  }

  if(get_string(scratchpad, inputfile, 169, 32)!=8)
  {
    strcpy(birthdate, "X");
  }
  else
  {
    error = 0;
    for(i=0; i<8; i++)
    {
      if((scratchpad[i]<'0')||(scratchpad[i]>'9'))  error = 1;
    }
    scratchpad[8] = 0;
    b_date = atoi(scratchpad+6);
    if((b_date<1)||(b_date>31))  error = 1;
    scratchpad[6] = 0;
    i = atoi(scratchpad+4);
    if((i<1)||(i>12))  error = 1;
    switch(i)
    {
      case  1 : strcpy(b_month, "JAN");
                break;
      case  2 : strcpy(b_month, "FEB");
                break;
      case  3 : strcpy(b_month, "MAR");
                break;
      case  4 : strcpy(b_month, "APR");
                break;
      case  5 : strcpy(b_month, "MAY");
                break;
      case  6 : strcpy(b_month, "JUN");
                break;
      case  7 : strcpy(b_month, "JUL");
                break;
      case  8 : strcpy(b_month, "AUG");
                break;
      case  9 : strcpy(b_month, "SEP");
                break;
      case 10 : strcpy(b_month, "OCT");
                break;
      case 11 : strcpy(b_month, "NOV");
                break;
      case 12 : strcpy(b_month, "DEC");
                break;
      default : strcpy(b_month, "ERR");
                break;
    }
    scratchpad[4] = 0;
    b_year = atoi(scratchpad);
    if(error)
    {
      strcpy(birthdate, "X");
    }
    else
    {
      sprintf(birthdate, "%02i-%s-%04i", b_date, b_month, b_year);
    }
  }

  len = get_string(patientname, inputfile, 11, 30);
  if(len)
  {
    latin1_to_ascii(patientname, len);
    for(i=0; i<len; i++)
    {
      if(patientname[i]==' ')
      {
        patientname[i] = '_';
      }
    }
  }
  else
  {
    strcpy(patientname, "X");
  }

  fseeko64(inputfile, 4, SEEK_SET);
  if(fread(var.str, 4, 1, inputfile)!=1)
  {
    textEdit1->append("Read error.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }

  fseeko64(inputfile, 8, SEEK_SET);
  seq_nr = fgetc(inputfile);
  if(seq_nr==EOF)
  {
    textEdit1->append("Read error.\n");
    fclose(inputfile);
    pushButton1->setEnabled(TRUE);
    return;
  }

  fseeko64(inputfile, 203, SEEK_SET);
  gender = fgetc(inputfile);
  if((gender!='M')&&(gender!='F'))
  {
    gender = 'X';
  }

  len = fgetc(inputfile);
  switch(len)
  {
    case 'E' : strcpy(handedness, " lefthanded");  break;
    case 'D' : strcpy(handedness, " righthanded");  break;
    default  : handedness[0] = 0;  break;
  }

  len = get_string(weight + 1, inputfile, 351, 6);
  if(len)
  {
    latin1_to_ascii(weight + 1, len);

    weight[0] = ' ';

    strcat(weight, " Kg");
  }
  else
  {
    weight[0] = 0;
  }

  len = get_string(equipment, inputfile, 309, 40);
  if(len)
  {
    latin1_to_ascii(equipment, len);
    for(i=0; i<len; i++)
    {
      if(equipment[i]==' ')
      {
        equipment[i] = '_';
      }
    }
  }
  else
  {
    strcpy(equipment, "X");
  }

  if(samplefrequency%10)
  {
    samples_in_record = samplefrequency;

    raster = samplefrequency * 2;

    datrecduris1 = 1;
  }
  else
  {
    samples_in_record = samplefrequency / 10;

    raster = (samplefrequency / 10) * 2;

    datrecduris1 = 0;
  }

  record_size = samples_in_record * channels * 2;

  data_records = (filesize - hdrsize) / record_size;

/***************** read logfile *****************************************/

  sprintf(path + strlen(path) - 4, ".LBK");

  logfile = fopen64(path, "rb");
  if(logfile==NULL)
  {
    sprintf(path + strlen(path) - 4, ".lbk");

    logfile = fopen64(path, "rb");
  }

  if(logfile!=NULL)
  {
    fseeko64(logfile, 0, SEEK_END);
    annot_cnt = ftello64(logfile) / 69;

    if(annot_cnt)
    {
      logbuf = (struct event_struct *)calloc(1, sizeof(struct event_struct[annot_cnt]));
      if(logbuf==NULL)
      {
        textEdit1->append("Malloc error. (logbuf)\n");
        fclose(inputfile);
        fclose(logfile);
        pushButton1->setEnabled(TRUE);
        return;
      }

      for(i=0; i<annot_cnt; i++)
      {
        if(get_string(scratchpad, logfile, i * 69, 10)!=8)
        {
          textEdit1->append("Error, can not read event starttime in .LBK file.\n");
          fclose(inputfile);
          fclose(logfile);
          free(logbuf);
          return;
        }

        if(scratchpad[2] != ':')  error = 1;
        if(scratchpad[5] != ':')  error = 1;
        scratchpad[2] = 0;
        scratchpad[5] = 0;
        scratchpad[8] = 0;
        if((scratchpad[0]<'0')||(scratchpad[0]>'9'))  error = 1;
        if((scratchpad[1]<'0')||(scratchpad[1]>'9'))  error = 1;
        if((scratchpad[3]<'0')||(scratchpad[3]>'9'))  error = 1;
        if((scratchpad[4]<'0')||(scratchpad[4]>'9'))  error = 1;
        if((scratchpad[6]<'0')||(scratchpad[6]>'9'))  error = 1;
        if((scratchpad[7]<'0')||(scratchpad[7]>'9'))  error = 1;

        if(atoi(scratchpad)>23)  error = 1;
        if(atoi(scratchpad+3)>59)  error = 1;
        if(atoi(scratchpad+6)>59)  error = 1;
        if(error)
        {
          textEdit1->append("Error, can not read event starttime in .LBK file.\n");
          fclose(inputfile);
          fclose(logfile);
          free(logbuf);
          return;
        }

        time_on = atoi(scratchpad) * 3600;
        time_on += atoi(scratchpad+3) * 60;
        time_on += atoi(scratchpad+6);

        if(get_string(scratchpad, logfile, i * 69 + 9, 10)!=8)
        {
          textEdit1->append("Error, can not read event stoptime in .LBK file.\n");
          fclose(inputfile);
          fclose(logfile);
          free(logbuf);
          return;
        }

        if(scratchpad[2] != ':')  error = 1;
        if(scratchpad[5] != ':')  error = 1;
        scratchpad[2] = 0;
        scratchpad[5] = 0;
        scratchpad[8] = 0;
        if((scratchpad[0]<'0')||(scratchpad[0]>'9'))  error = 1;
        if((scratchpad[1]<'0')||(scratchpad[1]>'9'))  error = 1;
        if((scratchpad[3]<'0')||(scratchpad[3]>'9'))  error = 1;
        if((scratchpad[4]<'0')||(scratchpad[4]>'9'))  error = 1;
        if((scratchpad[6]<'0')||(scratchpad[6]>'9'))  error = 1;
        if((scratchpad[7]<'0')||(scratchpad[7]>'9'))  error = 1;

        if(atoi(scratchpad)>23)  error = 1;
        if(atoi(scratchpad+3)>59)  error = 1;
        if(atoi(scratchpad+6)>59)  error = 1;
        if(error)
        {
          textEdit1->append("Error, can not read event stoptime in .LBK file.\n");
          fclose(inputfile);
          fclose(logfile);
          free(logbuf);
          return;
        }

        time_off = atoi(scratchpad) * 3600;
        time_off += atoi(scratchpad+3) * 60;
        time_off += atoi(scratchpad+6);

        logbuf[i].duration = time_off - time_on;

        if(logbuf[i].duration<0)
        {
          textEdit1->append("Error, event duration in .LBK file is < 0.\n");
          fclose(inputfile);
          fclose(logfile);
          free(logbuf);
          return;
        }

        if(logbuf[i].duration)
        {
          duration_present = 1;
        }

        k = (atoi(time) * 3600);
        k += (atoi(time+3) * 60);
        k += atoi(time+6);
        logbuf[i].onset = time_on - k;
        if(logbuf[i].onset < 0)
        {
          logbuf[i].onset += 86400;
        }

        len = get_string(logbuf[i].description, logfile, i * 69 + 18, 40);

        if(len>2)
        {
          for(j=len-1; j>0; j--)
          {
            if(logbuf[i].description[j]==' ')
            {
              logbuf[i].description[j] = 0;
              len--;
            }
            else  break;
          }
        }

        latin12utf8(logbuf[i].description, 80);

        len = strlen(logbuf[i].description);
        if(len>tallen)
        {
          tallen = len;
        }
      }

    }

    fclose(logfile);
  }
  else
  {
    textEdit1->append("Can not open LBK file, will continue without events.");
  }

  tallen += 14;

  if(annot_cnt)
  {
    tallen += 14;
  }

  if(duration_present)
  {
    tallen += 14;
  }

  if(tallen%2)
  {
    tallen++;
  }

/***************** write header *****************************************/

  sprintf(path + strlen(path) - 4, ".edf");

  outputfile = fopen(path, "wb");
  if(outputfile==NULL)
  {
    snprintf(txt_string, 2048, "Error, can not open file %s for writing.\n", path);
    textEdit1->append(txt_string);
    fclose(inputfile);
    free(logbuf);
    pushButton1->setEnabled(TRUE);
    return;
  }

  fprintf(outputfile, "0       ");

  i = fprintf(outputfile, "%05i %c %s %s%s%s",
                          patientcode,
                          gender,
                          birthdate,
                          patientname,
                          handedness,
                          weight);
  for(; i<80; i++)
  {
    fputc(' ', outputfile);
  }

  i = fprintf(outputfile, "Startdate %02i-%s-%04i %05i%02i X %s",
                          atoi(date),
                          month,
                          year,
                          patientcode,
                          seq_nr,
                          equipment);
  for(; i<80; i++)
  {
    fputc(' ', outputfile);
  }

  if(fwrite(date, 8, 1, outputfile)!=1)
  {
    textEdit1->append("Write error.\n");
    fclose(inputfile);
    fclose(outputfile);
    free(logbuf);
    pushButton1->setEnabled(TRUE);
    return;
  }

  if(fwrite(time, 8, 1, outputfile)!=1)
  {
    textEdit1->append("Write error.\n");
    fclose(inputfile);
    fclose(outputfile);
    free(logbuf);
    pushButton1->setEnabled(TRUE);
    return;
  }

  fprintf(outputfile, "%-8i", (256 * (channels + 2)));

  fprintf(outputfile, "EDF+C                                       ");

  fprintf(outputfile, "%-8u", data_records);

  if(samplefrequency%10)
  {
    fprintf(outputfile, "1       ");
  }
  else
  {
    fprintf(outputfile, "0.1     ");
  }

  fprintf(outputfile, "%-4i", channels + 1);

  for(i=0; i<channels; i++)
  {
    len = get_string(scratchpad, inputfile, 1034 + (i * 512), 16);
    for (j=len; j<16; j++)
    {
      scratchpad[j] = ' ';
    }
    latin1_to_ascii(scratchpad, 16);
    fseeko64(inputfile, 1025 + (i * 512), SEEK_SET);
    if((fgetc(inputfile)=='E')&&(len<13))
    {
      fprintf(outputfile, "EEG ");
      if(fwrite(scratchpad, 12, 1, outputfile)!=1)
      {
        textEdit1->append("Write error.\n");
        fclose(inputfile);
        fclose(outputfile);
        free(logbuf);
        pushButton1->setEnabled(TRUE);
        return;
      }
    }
    else
    {
      if(fwrite(scratchpad, 16, 1, outputfile)!=1)
      {
        textEdit1->append("Write error.\n");
        fclose(inputfile);
        fclose(outputfile);
        free(logbuf);
        pushButton1->setEnabled(TRUE);
        return;
      }
    }
  }

  if(fwrite("EDF Annotations ", 16, 1, outputfile)!=1)
  {
    textEdit1->append("Write error.\n");
    fclose(inputfile);
    fclose(outputfile);
    free(logbuf);
    pushButton1->setEnabled(TRUE);
    return;
  }

  for(i=0; i<(80*(channels+1)); i++)
  {
    fputc(' ', outputfile);
  }

  for(i=0; i<channels; i++)
  {
    fprintf(outputfile, "uV      ");
  }
  fprintf(outputfile, "        ");

  for(i=0; i<channels; i++)
  {
    fprintf(outputfile, "-3276.8 ");
  }
  fprintf(outputfile, "-1      ");

  for(i=0; i<channels; i++)
  {
    fprintf(outputfile, "3276.7  ");
  }
  fprintf(outputfile, "1       ");

  for(i=0; i<(channels+1); i++)
  {
    fprintf(outputfile, "-32768  ");
  }

  for(i=0; i<(channels+1); i++)
  {
    fprintf(outputfile, "32767   ");
  }

  for(i=0; i<(80*(channels+1)); i++)
  {
    fputc(' ', outputfile);
  }

  for(i=0; i<channels; i++)
  {
    fprintf(outputfile, "%-8i", samples_in_record);
  }
  fprintf(outputfile, "%-8i", tallen / 2);

  for(i=0; i<(32*(channels+1)); i++)
  {
    fputc(' ', outputfile);
  }

/****************** start conversion *****************/

  seconds = 0;

  deci_seconds = 0;

  record_size += tallen;

  bufsize = 4194304;

  if(record_size>bufsize)
  {
    bufsize = record_size;
  }

  buf = (char *)calloc(1, bufsize);
  if(buf==NULL)
  {
    textEdit1->append("Malloc error. (buf)\n");
    fclose(inputfile);
    fclose(outputfile);
    free(logbuf);
    pushButton1->setEnabled(TRUE);
    return;
  }

  max_buf_records = bufsize / record_size;

  fseeko64(inputfile, hdrsize, SEEK_SET);

  while(data_records)
  {
    if(data_records>max_buf_records)  records_in_buf = max_buf_records;
    else  records_in_buf = data_records;

    for(i=0; i<records_in_buf; i++)
    {
      for(j=0; j<raster; j+=2)
      {
        for(k=0; k<channels; k++)
        {
          buf[j+(k*raster)+(i*record_size)] = fgetc(inputfile);
          buf[j+(k*raster)+(i*record_size)+1] = fgetc(inputfile);
        }
      }

      p = buf + (i * record_size) + (raster * channels);
      if(datrecduris1)
      {
        len = sprintf(p, "%+i", seconds);
        seconds++;
      }
      else
      {
        len = sprintf(p, "%+i.%i", seconds, deci_seconds);
        if(++deci_seconds>9)
        {
          deci_seconds = 0;
          seconds++;
        }
      }
      p[len++] = 20;
      p[len++] = 20;
      if(annots_written<annot_cnt)
      {
        p[len++] = 0;
        len += sprintf(p + len, "%+i", logbuf[annots_written].onset);
        if(logbuf[annots_written].duration)
        {
          p[len++] = 21;
          len += sprintf(p + len, "%i", logbuf[annots_written].duration);
        }
        p[len++] = 20;
        len += sprintf(p + len, "%s", logbuf[annots_written].description);
        p[len++] = 20;
        annots_written++;
      }
      for(; len<tallen; len++)
      {
        p[len] = 0;
      }
    }

    if(fwrite(buf, records_in_buf * record_size, 1, outputfile)!=1)
    {
      textEdit1->append("Write error.\n");
      fclose(inputfile);
      fclose(outputfile);
      free(buf);
      free(logbuf);
      pushButton1->setEnabled(TRUE);
      return;
    }

    data_records -= records_in_buf;
  }

  free(logbuf);
  free(buf);
  fclose(inputfile);
  fclose(outputfile);

  snprintf(txt_string, 2048, "Done, your EDF+ file is %s\n", path);
  textEdit1->append(txt_string);

  pushButton1->setEnabled(TRUE);
}




int UI_EMSA2EDFwindow::get_string(char *str, FILE *file, int offset, int maxlen)
{
  int len;

  if(maxlen<1)
  {
    str[0] = 0;

    return(0);
  }

  fseeko64(file, offset, SEEK_SET);

  len = fgetc(file);

  if(len==EOF)
  {
    str[0] = 0;

    return(0);
  }

  if(len>maxlen)
  {
    len = maxlen;
  }

  if(fread(str, len, 1, file)!=1)
  {
    str[0] = 0;

    return(0);
  }

  str[len] = 0;

  return(len);
}




void UI_EMSA2EDFwindow::latin1_to_ascii(char *str, int len)
{
  int i, value;

  for(i=0; i<len; i++)
  {
    value = *((unsigned char *)(str + i));

    if((value>31)&&(value<127))
    {
      continue;
    }

    switch(value)
    {
      case 128 : str[i] = 'E';  break;

      case 130 : str[i] = ',';  break;

      case 131 : str[i] = 'F';  break;

      case 132 : str[i] = '\"';  break;

      case 133 : str[i] = '.';  break;

      case 134 : str[i] = '+';  break;

      case 135 : str[i] = '+';  break;

      case 136 : str[i] = '^';  break;

      case 137 : str[i] = 'm';  break;

      case 138 : str[i] = 'S';  break;

      case 139 : str[i] = '<';  break;

      case 140 : str[i] = 'E';  break;

      case 142 : str[i] = 'Z';  break;

      case 145 : str[i] = '`';  break;

      case 146 : str[i] = '\'';  break;

      case 147 : str[i] = '\"';  break;

      case 148 : str[i] = '\"';  break;

      case 149 : str[i] = '.';  break;

      case 150 : str[i] = '-';  break;

      case 151 : str[i] = '-';  break;

      case 152 : str[i] = '~';  break;

      case 154 : str[i] = 's';  break;

      case 155 : str[i] = '>';  break;

      case 156 : str[i] = 'e';  break;

      case 158 : str[i] = 'z';  break;

      case 159 : str[i] = 'Y';  break;

      case 171 : str[i] = '<';  break;

      case 180 : str[i] = '\'';  break;

      case 181 : str[i] = 'u';  break;

      case 187 : str[i] = '>';  break;

      case 191 : str[i] = '\?';  break;

      case 192 : str[i] = 'A';  break;

      case 193 : str[i] = 'A';  break;

      case 194 : str[i] = 'A';  break;

      case 195 : str[i] = 'A';  break;

      case 196 : str[i] = 'A';  break;

      case 197 : str[i] = 'A';  break;

      case 198 : str[i] = 'E';  break;

      case 199 : str[i] = 'C';  break;

      case 200 : str[i] = 'E';  break;

      case 201 : str[i] = 'E';  break;

      case 202 : str[i] = 'E';  break;

      case 203 : str[i] = 'E';  break;

      case 204 : str[i] = 'I';  break;

      case 205 : str[i] = 'I';  break;

      case 206 : str[i] = 'I';  break;

      case 207 : str[i] = 'I';  break;

      case 208 : str[i] = 'D';  break;

      case 209 : str[i] = 'N';  break;

      case 210 : str[i] = 'O';  break;

      case 211 : str[i] = 'O';  break;

      case 212 : str[i] = 'O';  break;

      case 213 : str[i] = 'O';  break;

      case 214 : str[i] = 'O';  break;

      case 215 : str[i] = 'x';  break;

      case 216 : str[i] = 'O';  break;

      case 217 : str[i] = 'U';  break;

      case 218 : str[i] = 'U';  break;

      case 219 : str[i] = 'U';  break;

      case 220 : str[i] = 'U';  break;

      case 221 : str[i] = 'Y';  break;

      case 222 : str[i] = 'I';  break;

      case 223 : str[i] = 's';  break;

      case 224 : str[i] = 'a';  break;

      case 225 : str[i] = 'a';  break;

      case 226 : str[i] = 'a';  break;

      case 227 : str[i] = 'a';  break;

      case 228 : str[i] = 'a';  break;

      case 229 : str[i] = 'a';  break;

      case 230 : str[i] = 'e';  break;

      case 231 : str[i] = 'c';  break;

      case 232 : str[i] = 'e';  break;

      case 233 : str[i] = 'e';  break;

      case 234 : str[i] = 'e';  break;

      case 235 : str[i] = 'e';  break;

      case 236 : str[i] = 'i';  break;

      case 237 : str[i] = 'i';  break;

      case 238 : str[i] = 'i';  break;

      case 239 : str[i] = 'i';  break;

      case 240 : str[i] = 'd';  break;

      case 241 : str[i] = 'n';  break;

      case 242 : str[i] = 'o';  break;

      case 243 : str[i] = 'o';  break;

      case 244 : str[i] = 'o';  break;

      case 245 : str[i] = 'o';  break;

      case 246 : str[i] = 'o';  break;

      case 247 : str[i] = '-';  break;

      case 248 : str[i] = '0';  break;

      case 249 : str[i] = 'u';  break;

      case 250 : str[i] = 'u';  break;

      case 251 : str[i] = 'u';  break;

      case 252 : str[i] = 'u';  break;

      case 253 : str[i] = 'y';  break;

      case 254 : str[i] = 't';  break;

      case 255 : str[i] = 'y';  break;

      default  : str[i] = ' ';  break;
    }
  }
}




void UI_EMSA2EDFwindow::latin12utf8(char *latin1_str, int len)
{
  int i, j;

  unsigned char *str, tmp_str[512];


  str = (unsigned char *)latin1_str;

  j = 0;

  for(i=0; i<len; i++)
  {
    tmp_str[j] = str[i];

    if(str[i]==0)  break;

    if(str[i]<32) tmp_str[j] = '.';

    if((str[i]>126)&&(str[i]<160))  tmp_str[j] = '.';

    if(str[i]>159)
    {
      if((len-j)<2)
      {
        tmp_str[j] = ' ';
      }
      else
      {
        tmp_str[j] = 192 + (str[i]>>6);
        j++;
        tmp_str[j] = 128 + (str[i]&63);
      }
    }

    j++;

    if(j>=len)  break;
  }

  for(i=0; i<len; i++)
  {
    str[i] = tmp_str[i];

    if(str[i]==0)  return;
  }
}












Generated by  Doxygen 1.6.0   Back to index