Codebase list edfbrowser / upstream/1.88+dfsg edf_helper.c
upstream/1.88+dfsg

Tree @upstream/1.88+dfsg (Download .tar.gz)

edf_helper.c @upstream/1.88+dfsgraw · history · blame

/*
***************************************************************************
*
* Author: Teunis van Beelen
*
* Copyright (C) 2010 - 2021 Teunis van Beelen
*
* Email: teuniz@protonmail.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 3 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, see <http://www.gnu.org/licenses/>.
*
***************************************************************************
*/



#include "edf_helper.h"

static const char edf_label_types[7][32]=
{
  "EEG             ",
  "ECG             ",
  "EOG             ",
  "ERG             ",
  "EMG             ",
  "MEG             ",
  "MCG             "
};


int edfplus_annotation_get_tal_timestamp_digit_cnt(struct edfhdrblock *hdr)
{
  int timestamp_digits;

  char scratchpad[256];

  long long time;


  if(hdr==NULL)
  {
    return -1;
  }

  time = (hdr->datarecords * hdr->long_data_record_duration) / TIME_DIMENSION;

#ifdef _WIN32
  timestamp_digits = __mingw_snprintf(scratchpad, 256, "%lli", time);
#else
  timestamp_digits = snprintf(scratchpad, 256, "%lli", time);
#endif

  return timestamp_digits;
}


int edfplus_annotation_get_tal_timestamp_decimal_cnt(struct edfhdrblock *hdr)
{
  int i, j,
      timestamp_decimals;


  if(hdr==NULL)
  {
    return -1;
  }

  j = 10;

  for(timestamp_decimals=7; timestamp_decimals>0; timestamp_decimals--)
  {
    if(hdr->long_data_record_duration % j)
    {
      break;
    }

    j *= 10;
  }

  if((hdr->edfplus==1)||(hdr->bdfplus==1))
  {
    j = 10;

    for(i=7; i>0; i--)
    {
      if(hdr->starttime_offset % j)
      {
        break;
      }

      j *= 10;
    }

    if(i > timestamp_decimals)
    {
      timestamp_decimals = i;
    }
  }

  return timestamp_decimals;
}


int strip_types_from_label(char *label)
{
  int i, type, len;

  len = strlen(label);
  if(len<5)
  {
    return 0;
  }

  for(type=0; type<7; type++)
  {
    if(!strncmp(label, edf_label_types[type], 4))
    {
      break;
    }
  }
  if(type == 7)
  {
    return 0;
  }

  if(label[4] == ' ')
  {
    return 0;
  }

  for(i=0; i<(len-4); i++)
  {
    label[i] = label[i+4];
  }

  for(; i<len; i++)
  {
    label[i] = ' ';
  }

  return (type + 1);
}