#include <stdio.h>
#include <stdlib.h>
#include "plxfunctions.h"
#include "plxstructs.h"
void test()
{
printf("test\n");
}
long int *UnitTimeStamps_ReturnPointer(char *FileName, int Channel, int Unit)
{
long int *UnitTimeStamps_Pointer;
long int UnitTimeStamp;
FILE *PLXFile;
struct PL_FileHeader PLXFileHeader;
struct PL_ChanHeader PLXChannelHeader[128];
struct PL_SlowChannelHeader PLXSlowChannelHeader[384];
struct PL_EventHeader PLXEventHeader[64];
struct PL_DataBlockHeader PLXDataBlockHeader;
int InternalUnitCount = 0;
PLXFile = fopen(FileName, "rb");
if (PLXFile == NULL)
{
printf("ERROR: File Not Found\n");
return NULL;
}
UnitTimeStamps_Pointer = (long int *)malloc(UnitTimeStamps_TimeStampCount(FileName, Channel, Unit) * sizeof(long int)); //allocate space
rewind(PLXFile);
fread(&PLXFileHeader, sizeof(struct PL_FileHeader), 1, PLXFile);
fread(&PLXChannelHeader, sizeof(struct PL_ChanHeader) * PLXFileHeader.NumDSPChannels, 1, PLXFile);
fread(&PLXEventHeader, sizeof(struct PL_EventHeader) * PLXFileHeader.NumEventChannels, 1, PLXFile);
fread(&PLXSlowChannelHeader, sizeof(struct PL_SlowChannelHeader) * PLXFileHeader.NumSlowChannels, 1, PLXFile);
while (1)
{
fread(&PLXDataBlockHeader, sizeof(struct PL_DataBlockHeader), 1, PLXFile);
if (feof(PLXFile)) {break;}
if (PLXDataBlockHeader.Type == 1 && PLXDataBlockHeader.Channel == Channel && PLXDataBlockHeader.Unit == Unit)
{
UnitTimeStamp = ((long int)PLXDataBlockHeader.UpperByteOf5ByteTimestamp << 32) + (long int)PLXDataBlockHeader.TimeStamp;
UnitTimeStamps_Pointer[InternalUnitCount] = UnitTimeStamp;
InternalUnitCount++;
SkipSpikeData(PLXFile, PLXDataBlockHeader.NumberOfWordsInWaveform);
}
else if (PLXDataBlockHeader.Type == 1)
{
SkipSpikeData(PLXFile, PLXDataBlockHeader.NumberOfWordsInWaveform);
}
else if (PLXDataBlockHeader.Type == 4)
{
SkipEventBlock();
}
else if (PLXDataBlockHeader.Type == 5)
{
SkipContinuousData(PLXFile, PLXDataBlockHeader.NumberOfWordsInWaveform);
}
}
fclose(PLXFile);
return UnitTimeStamps_Pointer;
}
int UnitTimeStamps_TimeStampCount(char *FileName, int Channel, int Unit)
{
FILE *PLXFile;
struct PL_FileHeader PLXFileHeader;
struct PL_ChanHeader PLXChannelHeader[128];
struct PL_SlowChannelHeader PLXSlowChannelHeader[384];
struct PL_EventHeader PLXEventHeader[64];
struct PL_DataBlockHeader PLXDataBlockHeader;
int InternalUnitCount = 0;
PLXFile = fopen(FileName, "rb");
if (PLXFile == NULL)
{
printf("ERROR: File Not Found\n");
return 0;
}
//First pass through the file to determine how many units there are
fread(&PLXFileHeader, sizeof(struct PL_FileHeader), 1, PLXFile);
fread(&PLXChannelHeader, sizeof(struct PL_ChanHeader) * PLXFileHeader.NumDSPChannels, 1, PLXFile);
fread(&PLXEventHeader, sizeof(struct PL_EventHeader) * PLXFileHeader.NumEventChannels, 1, PLXFile);
fread(&PLXSlowChannelHeader, sizeof(struct PL_SlowChannelHeader) * PLXFileHeader.NumSlowChannels, 1, PLXFile);
while (1)
{
fread(&PLXDataBlockHeader, sizeof(struct PL_DataBlockHeader), 1, PLXFile);
if (feof(PLXFile)) {break;}
if (PLXDataBlockHeader.Type == 1 && PLXDataBlockHeader.Channel == Channel && PLXDataBlockHeader.Unit == Unit)
{
InternalUnitCount++;
SkipSpikeData(PLXFile, PLXDataBlockHeader.NumberOfWordsInWaveform);
}
else if (PLXDataBlockHeader.Type == 1)
{
SkipSpikeData(PLXFile, PLXDataBlockHeader.NumberOfWordsInWaveform);
}
else if (PLXDataBlockHeader.Type == 4)
{
SkipEventBlock();
}
else if (PLXDataBlockHeader.Type == 5)
{
SkipContinuousData(PLXFile, PLXDataBlockHeader.NumberOfWordsInWaveform);
}
}
return InternalUnitCount;
fclose(PLXFile);
}
void SkipSpikeData (FILE *file, int SkipLength)
{
fseek(file, (SkipLength * sizeof(short int)), SEEK_CUR);
return;
}
void SkipEventBlock()
{
return;
}
void SkipContinuousData(FILE *file, int SkipLength)
{
fseek(file, (SkipLength * sizeof(short int)), SEEK_CUR);
return;
}