90 typedef struct evfilestruct {
103 typedef struct evBinarySearch{
111 #define EVBLOCKSIZE 8192
115 #define EV_PIPEWRITE 3
117 #define EV_MAGIC 0xc0da0100
121 #define EV_HD_BLKSIZ 0
122 #define EV_HD_BLKNUM 1
123 #define EV_HD_HDSIZ 2
124 #define EV_HD_START 3
127 #define EV_HD_RESVD 6
128 #define EV_HD_MAGIC 7
130 #define evGetStructure() (EVFILE *)malloc(sizeof(EVFILE))
147 #if defined(__osf__) && defined(__alpha)
152 #define MAXHANDLES 10
153 EVFILE *handle_list[10]={0,0,0,0,0,0,0,0,0,0};
156 #ifdef AbsoftUNIXFortran
161 (
char *filename,
char *flags,
int *handle,
int fnlen,
int flen)
165 fn = (
char *) malloc(fnlen+1);
166 strncpy(fn,filename,fnlen);
168 fl = (
char *) malloc(flen+1);
169 strncpy(fl,flags,flen);
171 status =
evOpen(fn,fl,handle);
182 while (e>s && *--e==t);
187 int evOpen(
char *fname,
char *flags,
int *handle)
199 filename = malloc(strlen(fname)+1);
200 strcpy(filename,fname);
206 while (*filename==
' ') filename++;
209 for (cp=filename;*cp!=
NULL;cp++) {
210 if ((*cp==
' ') || !(isprint(*cp))) *cp=
'\0';
218 case 'r':
case 'R': {
220 if(strcmp(filename,
"-")==0) {
222 }
else if(filename[0] ==
'|') {
223 a->file = popen(filename+1,
"r");
226 a->file = fopen(filename,
"r");
230 fread(bytes,2,1,a->file);
231 if(bytes[0]==
'\037' && (bytes[1]==
'\213' || bytes[1]==
'\235')) {
234 pipe_command = (
char *)malloc(strlen(filename)+strlen(
"gunzip<")+1);
235 strcpy(pipe_command,
"gunzip<");
236 strcat(pipe_command,filename);
237 a->file = popen(pipe_command,
"r");
242 a->file = fopen(filename,
"r");
247 fread(header,
sizeof(header),1,a->file);
264 a->buf = (
int *)malloc(blk_size*4);
289 if(strcmp(filename,
"-")==0) {
291 }
else if(filename[0] ==
'|') {
292 a->file = popen(filename+1,
"r");
295 a->file = fopen(filename,
"w");
327 for(ihandle=0;ihandle<MAXHANDLES;ihandle++){
328 if(handle_list[ihandle]==0) {
329 handle_list[ihandle] = a;
347 fprintf(stderr,
"evOpen: Error opening file %s, flag %s\n",
359 #ifdef AbsoftUNIXFortran
364 (
int *handle,
int *buffer,
int *buflen)
366 return(
evRead(*handle,buffer,*buflen));
369 int evRead(
int handle,
int *buffer,
int buflen)
372 int nleft,ncopy,error,status;
373 int *temp_buffer,*temp_ptr;
376 a = handle_list[handle-1];
380 if (a->byte_swapped){
381 temp_buffer = (
int *)malloc(buflen*
sizeof(
int));
383 temp_ptr = temp_buffer;
388 if (error)
return(error);
393 nleft = *(a->next) + 1;
394 if (nleft < buflen) {
403 if (error)
return(error);
405 ncopy = (nleft <= a->left) ? nleft : a->left;
406 if (a->byte_swapped){
407 memcpy(temp_buffer,a->next,ncopy*4);
408 temp_buffer += ncopy;
411 memcpy(buffer,a->next,ncopy*4);
418 if (a->byte_swapped){
419 swapped_memcpy((
char *)buffer,(
char *)temp_ptr,buflen*
sizeof(
int));
430 if (feof(a->file))
return(EOF);
433 nread = fread(a->buf,4,a->blksiz,a->file);
434 if (a->byte_swapped){
438 if (feof(a->file))
return(EOF);
439 if (ferror(a->file))
return(ferror(a->file));
440 if (nread != a->blksiz)
return(errno);
459 #ifdef AbsoftUNIXFortran
464 (
int *handle,
int *buffer)
466 return(
evWrite(*handle,buffer));
472 int nleft,ncopy,error;
474 a = handle_list[handle-1];
480 a->evnum = a->evnum + 1;
481 nleft = buffer[0] + 1;
483 ncopy = (nleft <= a->left) ? nleft : a->left;
484 memcpy(a->next,buffer,ncopy*4);
491 if (error)
return(error);
504 nwrite = fwrite(a->buf,4,a->blksiz,a->file);
505 if (ferror(a->file))
return(ferror(a->file));
506 if (nwrite != a->blksiz)
return(errno);
521 #ifdef AbsoftUNIXFortran
526 (
int *handle,
char *request,
void *argp,
int reqlen)
530 req = (
char *)malloc(reqlen+1);
531 strncpy(req,request,reqlen);
533 status =
evIoctl(*handle,req,argp);
538 int evIoctl(
int handle,
char *request,
void *argp)
542 a = handle_list[handle-1];
553 a->blksiz = *(
int *) argp;
555 a->buf = (
int *) malloc(a->blksiz*4);
576 #ifdef AbsoftUNIXFortran
591 a = handle_list[handle-1];
599 status2 = pclose(a->file);
601 status2 = fclose(a->file);
604 handle_list[handle-1] = 0;
606 free((
char *)(a->buf));
608 if (status==0) status = status2;
626 int found = 0, temp, status, i = 1;
627 int last_evn, ev_type, bknum;
631 a = handle_list[handle-1];
637 fprintf(stderr,
"Cannot allocate memory for EVBSEARCH structure!\n");
640 fseek(a->file, 0L, SEEK_SET);
641 fread(header,
sizeof(header), 1, a->file);
648 fseek(a->file, 0L, SEEK_END);
651 fseek(a->file, (-1)*a->blksiz*4*i, SEEK_END);
663 b->last_evn = last_evn;
665 for(ihandle=0;ihandle<MAXHANDLES;ihandle++){
666 if(handle_list[ihandle]==0) {
667 handle_list[ihandle] = (
EVFILE *)b;
668 *b_handle = ihandle+1;
691 int header, t_header, found = 0;
692 int ev_size, temp, evn = 0, last_evn = 0;
697 fread(&header,
sizeof(
int), 1, a->file);
701 ev_size = header + 1;
704 a->left = a->left - ev_size;
708 fseek(a->file, 3*4, SEEK_CUR);
709 fread(&header,
sizeof(
int), 1, a->file);
717 if (first_time == 0){
730 fseek(a->file, 3*4, SEEK_CUR);
731 fread(&header,
sizeof(
int), 1, a->file);
737 fseek(a->file,(ev_size - 5)*4, SEEK_CUR);
740 fseek(a->file, (ev_size - 1)*4, SEEK_CUR);
757 int evSearch(
int handle,
int b_handle,
int evn,
int *buffer,
int buflen,
int *size)
765 a = handle_list[handle-1];
766 b = (
EVBSEARCH *)handle_list[b_handle-1];
772 if(evn > b->last_evn)
778 mid = (start + end)/2.0;
781 if(evn >= b->found_evn){
784 mid = (start + end)/2.0;
789 mid = (start + end)/2.0;
796 mid = (start + end)/2.0;
800 mid = (start + end)/2.0;
831 int evn,
int *buffer,
int buflen,
int *size)
833 int header[
EV_HDSIZ], temp, ev_size;
835 int found = 0, t_evn, block_num;
846 fread(&temp,
sizeof(
int),1,a->file);
852 a->left = a->left - ev_size;
856 fseek(a->file, (-1)*4, SEEK_CUR);
867 fseek(a->file, (ev_size-1)*4, SEEK_CUR);
868 while(!found && a->left > 0){
869 fread(&temp,
sizeof(
int), 1, a->file);
877 a->left = a->left - ev_size;
883 fseek(a->file, (-1)*4, SEEK_CUR);
899 fseek(a->file, (-1)*4, SEEK_CUR);
905 fseek(a->file, (ev_size-1)*4, SEEK_CUR);
909 fseek(a->file, (ev_size - 1)*4, SEEK_CUR);
927 int found = 0, temp, nleft;
930 while(block_num <= b->ebk){
931 fseek(a->file, a->blksiz*block_num*4, SEEK_SET);
932 fread(header,
sizeof(header), 1, a->file);
951 while(block_num >= b->sbk){
952 fseek(a->file, a->blksiz*block_num*4, SEEK_SET);
953 fread(header,
sizeof(header), 1, a->file);
957 memcpy((
char *)buf, (
char *)header,
EV_HDSIZ*4);
970 fprintf(stderr,
"Cannot find out event offset in any of the blocks, Exit!\n");
982 int nleft = old_left;
983 int ev_size, temp, ev_type;
986 fread(&temp,
sizeof(
int), 1, a->file);
994 fseek(a->file, (-1)*
sizeof(
int), SEEK_CUR);
998 nleft = nleft - ev_size;
999 fseek(a->file, 4*(ev_size - 1), SEEK_CUR);
1018 int *temp_buffer, *temp_ptr, *ptr;
1019 int status, nleft, temp, block_left;
1023 if(a->byte_swapped){
1024 temp_buffer = (
int *)malloc(buflen*
sizeof(
int));
1025 temp_ptr = temp_buffer;
1031 if(buflen < ev_size){
1040 block_left = ev_size + a->left;
1041 if(nleft <= block_left){
1042 if(a->byte_swapped){
1043 fread((
char *)temp_ptr, nleft*4, 1, a->file);
1046 fread((
char *)ptr, nleft*4, 1, a->file);
1051 if(a->byte_swapped){
1052 fread((
char *)temp_ptr, ncopy*4, 1, a->file);
1053 temp_ptr = temp_ptr + ncopy;
1056 fread((
char *)ptr, ncopy*4, 1, a->file);
1059 nleft = nleft - ncopy;
1061 fseek(a->file,
EV_HDSIZ*4, SEEK_CUR);
1065 fseek(a->file,
EV_HDSIZ*4,SEEK_CUR);
1070 temp_ptr = temp_buffer;
1076 if(a->byte_swapped){
1077 fread(temp_ptr, ev_size*4, 1, a->file);
1080 fread(ptr, ev_size*4, 1, a->file);
1084 if(a->byte_swapped){
1085 swapped_memcpy((
char *)buffer, (
char *)temp_ptr, buflen*
sizeof(
int));
1100 b = (
EVBSEARCH *)handle_list[b_handle-1];
1101 handle_list[b_handle-1] = 0;
1115 int temp, evn, nleft;
1117 nleft = a->left + ev_size;
1119 fseek(a->file, 3*4, SEEK_CUR);
1121 fseek(a->file, (
EV_HDSIZ+3)*4, SEEK_CUR);
1122 fread(&temp,
sizeof(
int), 1, a->file);
1129 fseek(a->file, (-1)*4*4, SEEK_CUR);
1131 fseek(a->file,(-1)*(
EV_HDSIZ + 4)*4, SEEK_CUR);
1138 int ev_type, temp, t_temp;
1141 fseek(a->file, (
EV_HDSIZ)*4,SEEK_CUR);
1142 if(a->byte_swapped){
1143 fread(&t_temp,
sizeof(
int), 1, a->file);
1147 fread(&temp,
sizeof(
int), 1, a->file);
1148 ev_type = (temp >> 16)&(0x0000ffff);
1151 fseek(a->file, (-1)*(
EV_HDSIZ + 1)*4, SEEK_CUR);
1153 fseek(a->file, (-1)*4, SEEK_CUR);
1172 int nleft, temp, ev_size, ev_type;
1175 if(a->byte_swapped){
1176 fread(header,
sizeof(header), 1, a->file);
1180 fread(buf,
sizeof(buf), 1, a->file);
1189 fread(&temp,
sizeof(
int), 1, a->file);
1197 fseek(a->file, (-1)*
sizeof(
int), SEEK_CUR);
1202 nleft = nleft - ev_size;
1203 fseek(a->file, 4*(ev_size - 1), SEEK_CUR);
#define S_EVFILE_UNKOPTION
#define S_EVFILE_BADSIZEREQ
#define S_EVFILE_BADBLOCK
#define S_EVFILE_UNXPTDEOF
#define S_EVFILE_BADHANDLE
#define S_EVFILE_ALLOCFAIL
int evSearch(int handle, int b_handle, int evn, int *buffer, int buflen, int *size)
int evwrite_(int *handle, int *buffer)
static int evGetEventType(EVFILE *)
int evOpenSearch(int handle, int *b_handle)
int evopen_(char *filename, char *flags, int *handle, int fnlen, int flen)
struct evBinarySearch EVBSEARCH
static int isRealEventsInsideBlock(EVFILE *, int, int)
int evCloseSearch(int b_handle)
static void evFindEventBlockNum(EVFILE *, EVBSEARCH *, int *)
int evioctl_(int *handle, char *request, void *argp, int reqlen)
int evWrite(int handle, int *buffer)
static int findLastEventWithinBlock(EVFILE *)
static int evSearchWithinBlock(EVFILE *, EVBSEARCH *, int *, int, int *, int, int *)
static int physicsEventsInsideBlock(EVFILE *)
int evOpen(char *fname, char *flags, int *handle)
static int copySingleEvent(EVFILE *, int *, int, int)
static char * kill_trailing(char *s, char t)
int evRead(int handle, int *buffer, int buflen)
int evclose_(int *handle)
int evread_(int *handle, int *buffer, int *buflen)
int evIoctl(int handle, char *request, void *argp)
static int evGetEventNumber(EVFILE *, int)
int evGetNewBuffer(EVFILE *a)
struct evfilestruct EVFILE