//IMPORTANT ******* //I have not checked the code in this file to ensure it works! //It is a cut down version of the code used in ANALOOK. //Use it at your own risk! //***************** //LOADFILE() Loads an ANABAT sequence file stored in BUFFER //into global arrays T, F and S //Before using it, the file must have already been loaded //into BUFFER, the global variable FILELEN set to //the file length in bytes and the string FILENAME //set to the filename //Loadfile() loads the arrays with the data from the file. //If the file is corrupt, LOADFILE() returns 0, //otherwise it returns the number of data points, //which value is also stored in the global N. //For making use of the data in the arrays, //use an index j such that 0 <= j < N //WORD is 2 byte unsigned //DWORD is 4 byte unsigned //BYTE is 1 byte unsigned //global variables int FILELEN;// already set to length of file BYTE BUFFER[32768];//32768 is max file size //BUFFER already filled with file contents DWORD F[16384]; DWORD T[16384]; BYTE S[16384]; //F will hold frequencies in Herz //T will hold times in microseconds //S will hold status byte. // if S=0 dot is out of range // if S=1 dot switched off // if S=2 dot normal // if S=3 dot is maindot (has been selected as part of call body) int DIVRAT;// holds division ratio DWORD RES1; int N;// holds number of data points int YEAR; int MON; int DAY; int HOURS; int MINS; int SECS; int HUNDS; int MICROS; int FTYPE;// holds file TYPE (must be 129 to 132) char FILENAME[13];//Already contains zero-terminated string of filename //getfiledatetime(int) fetches the date and time into //global variables. These values are obtained from the //data information table buried in the file structure //for those file types which support this. //Otherwise, the date and time are extracted from //the filename, provided the filename is based //on date and time. void getfiledatetime(int p) { char tem[32]; int t; if(FTYPE>=132) { YEAR=BUFFER[p+6]+256*BUFFER[p+7]; MON=BUFFER[p+8]; DAY=BUFFER[p+9]; HOURS=BUFFER[p+10]; MINS=BUFFER[p+11]; SECS=BUFFER[p+12]; HUNDS=BUFFER[p+13]; MICROS=BUFFER[p+14]+256*BUFFER[p+15]; return; } //for filetypes before 132, get date and time from filename t=FILENAME[0]; if((t<='9')&&(t>='0')) { YEAR=t+1990-'0'; goto month; } t=toupper(t); if((t>='A')&&(t<='Z')) { YEAR=t+2000-'A'; goto month; } goto baddate; month: t=FILENAME[1]; if((t<='9')&&(t>'0')) { MON=t-'0'; goto day; } t=toupper(t); if((t>='A')&&(t<='C')) { MON=t+10-'A'; goto day; } goto baddate; day: tem[0]=FILENAME[2]; if(!isdigit(tem[0]))goto baddate; tem[1]=FILENAME[3]; if(!isdigit(tem[1]))goto baddate; tem[2]=0; t=atoi(tem); DAY=t; tem[0]=FILENAME[4]; if(!isdigit(tem[0]))goto baddate; tem[1]=FILENAME[5]; if(!isdigit(tem[1]))goto baddate; tem[2]=0; t=atoi(tem); HOURS=t; tem[0]=FILENAME[6]; if(!isdigit(tem[0]))goto baddate; tem[1]=FILENAME[7]; if(!isdigit(tem[1]))goto baddate; tem[2]=0; t=atoi(tem); MINS=t; tem[0]=FILENAME[9]; if(!isdigit(tem[0]))goto baddate; tem[1]=FILENAME[10]; if(!isdigit(tem[1]))goto baddate; tem[2]=0; t=atoi(tem); SECS=t; HUNDS=0; MICROS=0; return; baddate: YEAR=0; MON=0; DAY=0; HOURS=0; MINS=0; SECS=0; HUNDS=0; MICROS=0; } //calcfreq() calculates the frequency data from the time data //filling F[] with data derived from DIVRAT and T[] void calcfreq() { int t=2; long td; double A; F[0]=0; F[1]=0; long n; Tmin=(long)ceil(DIVRAT*4.0);//corresponds to 250 KHz Tmax=(long)floor(DIVRAT*250.0);//corresponds to 4 KHz if(Tmin<48)Tmin=48; if(Tmax>12859)Tmax=12859; while (t=Tmin)&&(td<=Tmax)) { A=((double)DIVRAT)*1.0e6/(double)td; F[t]=(DWORD)A; } else { F[t]=0; S[t]=0; } t++; } } int LOADFILE() { int p,s=0,nshift,j,t; long time=0,lastdif=0,dif=0,fdif,temdif; double timefactor; //clear all arrays for(j=0;j<16384;j++)T[j]=0; for(j=0;j<16384;j++)F[j]=0; for(j=0;j<16384;j++)S[j]=2;//dots on if(BUFFER[3]==0x81) {//type 129 File Structure FTYPE=BUFFER[3]; p=BUFFER[0]+BUFFER[1]*256; if(p!=0x11a)return(0); RES1=BUFFER[p+2]+256*BUFFER[p+3]; if((RES1>60000)||(RES1<10000))return(0); if(RES1!=25000)timefactor=25.0e3/(double)RES1; if((BUFFER[p+4]>64)||(BUFFER[p+4]<1))return(0); getfiledatetime(p); DIVRAT=BUFFER[p+4]; p=BUFFER[p]+BUFFER[p+1]*256; if(p!=0x120)return(0); T[0]=0; S[0]=0; S[1]=0; t=1; while(p63)dif+=0xffffff80; lastdif+=dif; if(RES1!=25000) { dif=(long)floor(timefactor*(double)lastdif+0.5); } else dif=lastdif; time+=dif; T[t]=time; t++; p++; } else { if(BUFFER[p]>0xf8) { s=BUFFER[p]-0xf8; while(s>0) { S[t+s-1]=1; s--; } p++; } else { nshift=BUFFER[p]; nshift=nshift>>3; nshift=nshift&0x0f; dif=(BUFFER[p]&0x07)*256+BUFFER[p+1]; if(nshift>0)dif=dif<60000)||(RES1<10000))return(0); if(RES1!=25000)timefactor=25.0e3/(double)RES1; if((BUFFER[p+4]>64)||(BUFFER[p+4]<1))return(0); DIVRAT=BUFFER[p+4]; getfiledatetime(p); p=BUFFER[p]+BUFFER[p+1]*256; if((p!=0x120)&&(FTYPE<0x84))return(0);//file corrupt if((p!=0x150)&&(FTYPE==0x84))return(0);//file corrupt T[0]=0; S[0]=0; S[1]=0; t=1; while((p63)dif+=0xffffff80; lastdif+=dif; if(RES1!=25000) { dif=(long)floor(timefactor*(double)lastdif+0.5); } else dif=lastdif; time+=dif; T[t]=time; t++; p++; } else { if(BUFFER[p]>=0xe0) { if((FTYPE==131)||(FTYPE==132)) { unsigned char c; if((p+1)>=FILELEN)goto alldone; c=BUFFER[p]&3; s=BUFFER[p+1]; if((t+s-1)>16383)s=16384-t;//limits index to arrays while(s>0) { S[t+s-1]=c; s--; } p+=2; }//if FTYPE==131 or 132 else { s=BUFFER[p]-0xe0; if((t+s-1)>16383)s=16384-t;//limits index to arrays while(s>0) { S[t+s-1]=1; s--; } p++; }//else FTPYE==130 }//BUFFER[p]>0xe0 status change else { switch((BUFFER[p]&0x60)) { case 0: { if((p+1)>=FILELEN)goto alldone; dif=(((unsigned long)BUFFER[p]&0x1f)<<8)+\ ((unsigned long)BUFFER[p+1]); lastdif=dif; if(RES1!=25000) { dif=(long)floor(timefactor*(double)lastdif+0.5); } time+=dif; T[t]=time; t++; p+=2; break; } case 0x20: { if((p+2)>=FILELEN)goto alldone; dif=(((unsigned long)BUFFER[p]&0x1f)<<16)+\ (((unsigned long)BUFFER[p+1])<<8)+\ ((unsigned long)BUFFER[p+2]); lastdif=dif; if(RES1!=25000) { dif=(long)floor(timefactor*(double)lastdif+0.5); } time+=dif; T[t]=time; t++; p+=3; break; } case 0x40: { if((p+3)>=FILELEN)goto alldone; dif=(((unsigned long)BUFFER[p]&0x1f)<<24)+\ (((unsigned long)BUFFER[p+1])<<16)+\ (((unsigned long)BUFFER[p+2])<<8)+\ ((unsigned long)BUFFER[p+3]); lastdif=dif; if(RES1!=25000) { dif=(long)floor(timefactor*(double)lastdif+0.5); } time+=dif; T[t]=time; t++; p+=4; break; } } } } } alldone: N=t; S[t]=-2; RES1=25000;//all data transormed to microseconds calcfreq(); return(t);//returns number of data points } else {/*Invalid File Structure*/ return(0); } }