00001 #include <IORawData/CSCTFCommissioning/src/FileReaderSP.h>
00002
00003 #include <iostream>
00004 #include <fstream>
00005 #include <cstdio>
00006 #include <unistd.h>
00007
00008
00009
00010 FileReaderSP::FileReaderSP()
00011 {
00012 pointer = 0;
00013 key[0] = 0xf000;
00014 key[1] = 0x7fff;
00015 key[2] = 0x7fff;
00016 }
00017
00018 void FileReaderSP::Configure() {
00019 }
00020
00021
00022 int FileReaderSP::chunkSize() {
00023
00024
00025
00026 return 0;
00027 }
00028
00029
00030 int FileReaderSP::readSP(unsigned short **buf, const bool debug) {
00031
00032 int counter=0;
00033 unsigned short tmp[1];
00034 unsigned short holder =0;
00035 unsigned short a[6000];
00036 int bytes_read=0;
00037
00038 do{
00039 if(pointer == 0)
00040 {
00041 bytes_read = read(fd_schar,tmp,2);
00042 if(bytes_read != 2)
00043 {
00044 cout<<"FileReaderSP :: Reached end of file!!!\n";
00045 return 0;
00046 }
00047 a[counter]=tmp[0];
00048 }
00049 else
00050 {
00051 holder = miniBuf.front();
00052 a[counter] = holder;
00053 miniBuf.pop();
00054 holder = 0;
00055 }
00056
00057 counter++;
00058 if((bytes_read = fillMiniBuf())== -1)
00059 {
00060 while(miniBuf.size()>0)
00061 {
00062 holder = miniBuf.front();
00063 a[counter] = holder;
00064 miniBuf.pop();
00065 holder = 0;
00066 counter++;
00067 }
00068 void * last = malloc((counter-1)*sizeof(unsigned short));
00069 memset(last,0,(counter-1)*sizeof(unsigned short));
00070 memcpy(last,a,(counter-1)*sizeof(unsigned short));
00071 *buf=reinterpret_cast<unsigned short*>(last);
00072 pointer = 0;
00073 return (counter-1)*sizeof(unsigned short);
00074 }
00075 pointer += bytes_read;
00076
00077 }while(!isHeader());
00078
00079 void * event = malloc(counter*sizeof(unsigned short));
00080 memset(event,0,counter*sizeof(unsigned short));
00081 memcpy(event,a,counter*sizeof(unsigned short));
00082 *buf=reinterpret_cast<unsigned short*>(event);
00083
00084
00085
00086
00087
00088 return counter*sizeof(unsigned short);
00089
00090 }
00091
00092 int FileReaderSP::fillMiniBuf()
00093 {
00094 int count=0;
00095 unsigned short temp[1];
00096 while(miniBuf.size() < 6)
00097 {
00098 if(read(fd_schar,temp,2)==2)
00099 {
00100 miniBuf.push(temp[0]);
00101 count++;
00102 }
00103 else
00104 {
00105 return -1;
00106 }
00107 }
00108 return count;
00109 }
00110
00111 bool FileReaderSP::isHeader()
00112 {
00113 int holder = 0;
00114 for(int i=0;i<MAXBUFSIZE;i++)
00115 {
00116 holder = miniBuf.front();
00117 refBuf[i] = holder;
00118 miniBuf.pop();
00119 miniBuf.push(refBuf[i]);
00120 }
00121
00122 return ((refBuf[0]&key[0])==key[0] && (refBuf[1]&key[0])==key[0] && (refBuf[2]&key[0])==key[0]
00123 && (refBuf[3]&key[0])==key[0] && (refBuf[4]|key[1])==key[1] && (refBuf[5]|key[2])==key[2]);
00124 }