Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

f_evt.c

Go to the documentation of this file.
00001 //-------------------------------------------------------------
00002 //        Go4 Release Package v3.04-01 (build 30401)
00003 //                      28-November-2008
00004 //---------------------------------------------------------------
00005 //   The GSI Online Offline Object Oriented (Go4) Project
00006 //   Experiment Data Processing at EE department, GSI
00007 //---------------------------------------------------------------
00008 //
00009 //Copyright (C) 2000- Gesellschaft f. Schwerionenforschung, GSI
00010 //                    Planckstr. 1, 64291 Darmstadt, Germany
00011 //Contact:            http://go4.gsi.de
00012 //----------------------------------------------------------------
00013 //This software can be used under the license agreements as stated
00014 //in Go4License.txt file which is part of the distribution.
00015 //----------------------------------------------------------------
00016 #include "typedefs.h"
00017 #include "f_stccomm.h"
00018 
00019 #ifdef RFIO
00020 #define RFIO_open  rfio_open
00021 #define RFIO_close rfio_close
00022 #define RFIO_read  rfio_read
00023 #define RFIO_lseek rfio_lseek
00024 #include "rawapin.h"        /* RFIO stuff */
00025 #else
00026 #define RFIO_open  open
00027 #define RFIO_close close
00028 #define RFIO_read  read
00029 #define RFIO_lseek lseek
00030 #endif
00031 
00032 #ifdef GSI__LYNX
00033 #undef unix
00034 #endif
00035 
00036 #ifdef GSI__LINUX   /* Linux */
00037 #undef unix
00038 #endif
00039 
00040 #ifdef GSI__SOLARIS   /* Solaris */
00041 #undef unix
00042 #endif
00043 
00044 #ifdef GSI__WINNT           /* Windows NT */
00045 #endif
00046 
00047 #ifdef GSI__AIX
00048 #define AIX_DEC
00049 #endif
00050 
00051 #ifdef unix   /* DEC OSF/1 */
00052 #define AIX_DEC
00053 #endif
00054 #ifdef GSI__VMS   /* open vms */
00055 #include <descrip.h>
00056 static struct dsc$descriptor_vs sd_string;
00057 static INTS4 l_st_ichannel[5]={0,0,0,0,0};
00058 static INTS4 l_st_ochannel[5]={0,0,0,0,0};
00059 #endif
00060 
00061 #ifdef GSI__WINNT
00062 #include <time.h>
00063 #include <memory.h>
00064 #include <sys/timeb.h>
00065 #include <sys/types.h>
00066 #include <stdio.h>
00067 #include <stdlib.h>
00068 #include <fcntl.h>
00069 #include <string.h>
00070 #include <sys/stat.h>
00071 #include <fcntl.h>
00072 #define DEF_FILE_ACCE S_IREAD|S_IWRITE  /* rw */
00073 #define GET__OPEN_FLAG O_RDONLY
00074 #define PUT__OPEN_APD_FLAG O_RDWR|O_APPEND
00075 #define PUT__CRT_FLAG O_CREAT|O_RDWR
00076 #define PUT__CRT_OPT ""
00077 
00078 #include <winsock.h>
00079 #define    WS_VERSION_REQD     0x0101
00080 #define    WS_VERSION_MAJOR    HIBYTE(WS_VERSION_REQD)
00081 #define    WS_VERSION_MINOR    LOBYTE(WS_VERSION_REQD)
00082 #define    MIN_SOCKETS_REQD    6
00083 // WSADATA    wsaData;
00084 #endif
00085 
00086 #ifdef Linux /* Linux */
00087 #include <stdio.h>
00088 #include <stdlib.h>
00089 #include <fcntl.h>
00090 #include <string.h>
00091 #include <unistd.h>
00092 #include <memory.h>
00093 #include <pwd.h>
00094 #include <sys/time.h>
00095 #include <sys/timeb.h>
00096 #include <sys/types.h>
00097 #include <sys/stat.h>
00098 #define DEF_FILE_ACCE S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH  /* rw-r--r-- */
00099 #define GET__OPEN_FLAG O_RDONLY
00100 #define PUT__OPEN_APD_FLAG O_RDWR|O_APPEND
00101 #define PUT__CRT_FLAG O_CREAT|O_RDWR
00102 #define PUT__CRT_OPT ""
00103 #endif
00104 
00105 #ifdef Solaris /* Linux */
00106 #include <stdio.h>
00107 #include <stdlib.h>
00108 #include <fcntl.h>
00109 #include <string.h>
00110 #include <unistd.h>
00111 #include <memory.h>
00112 #include <pwd.h>
00113 #include <sys/time.h>
00114 #include <sys/timeb.h>
00115 #include <sys/types.h>
00116 #include <sys/stat.h>
00117 #define DEF_FILE_ACCE S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH  /* rw-r--r-- */
00118 #define GET__OPEN_FLAG O_RDONLY
00119 #define PUT__OPEN_APD_FLAG O_RDWR|O_APPEND
00120 #define PUT__CRT_FLAG O_CREAT|O_RDWR
00121 #define PUT__CRT_OPT ""
00122 #endif
00123 
00124 #ifdef _AIX
00125 #include <stdio.h>
00126 #include <stdlib.h>
00127 #include <fcntl.h>
00128 #include <string.h>
00129 #include <unistd.h>
00130 #include <memory.h>
00131 #include <pwd.h>
00132 #include <sys/time.h>
00133 #include <sys/timeb.h>
00134 #include <sys/types.h>
00135 #include <sys/mode.h>
00136 #define DEF_FILE_ACCE S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH  /* rw-r--r-- */
00137 #define GET__OPEN_FLAG O_RDONLY
00138 #define PUT__OPEN_APD_FLAG O_RDWR|O_APPEND
00139 #define PUT__CRT_FLAG O_CREAT|O_RDWR
00140 #define PUT__CRT_OPT ""
00141 #endif
00142 
00143 #ifdef VMS
00144 #include <unixio.h>
00145 #include <unixlib.h>
00146 #include <file.h>
00147 #include <stdio.h>
00148 #include <stdlib.h>
00149 #include <fcntl.h>
00150 #include <string.h>
00151 #include <sys/time.h>
00152 #include <sys/timeb.h>
00153 #include <sys/types.h>
00154 #include <unistd.h>
00155 #define DEF_FILE_ACCE 644          /* rw-r--r-- */
00156 #define GET__OPEN_FLAG O_RDONLY
00157 #define PUT__OPEN_APD_FLAG O_RDWR|O_APPEND
00158 #define PUT__CRT_FLAG O_CREAT|O_WRONLY
00159 #define PUT__CRT_OPT "rfm=fix"
00160 #endif
00161 
00162 #if MBS_ENDIAN == 1     
00163 #include "s_filhe_swap.h"
00164 #else
00165 #include "s_filhe.h"
00166 #endif
00167 
00168 // DABC
00169 #include "fLmd.h"
00170 // -- DABC
00171 
00172 #include "gps_sc_def.h"
00173 #include "f_evt.h"
00174 #include "f_evcli.h"
00175 #include "portnum_def.h"
00176 
00177 INTS4 f_evt_get_newbuf(s_evt_channel *);
00178 INTS4 f_evt_check_buf(CHARS *,INTS4 *, INTS4 *, INTS4 *, INTS4 *);
00179 INTS4 f_evt_ini_bufhe(s_evt_channel *ps_chan);
00180 INTS4 f_evt_swap_filhe(s_bufhe *);
00181 INTS4 f_ut_utime(INTS4, INTS4, CHARS *);
00182 
00183 static struct s_tcpcomm s_tcpcomm_st_evt;
00184 static CHARS c_temp[MAX_BUF_LGTH];
00185 static int l_gl_rev_port = PORT__EVENT_SERV;
00186 static int l_gl_evt_check = 0;
00187 
00188 /*1+ C Procedure *************+****************************************/
00189 /*                                                                    */
00190 /*+ Module      : f_evt__example                                  */
00191 /*                                                                    */
00192 /*--------------------------------------------------------------------*/
00193 /*+ CALLING     : Examples for calling event API */
00194 /*--------------------------------------------------------------------*/
00195 /*                                                                    */
00196 /*+ PURPOSE     : See f_evt_examples.c                    */
00197 /*                                                                    */
00198 /*                                                             */
00199 /*2+Implementation************+****************************************/
00200 /*+ User Example : In m_lea_user.c                                    */
00201 /*+ Channel structure : defined in f_evt.h                            */
00202 /*+ File name   : f_evt.c                                             */
00203 /*+ Version     : 1.01                                                */
00204 /*+ Author      : H.Essel                                             */
00205 /*+ Created     : 16-Feb-2000                                         */
00206 /*+ Updates     : Date        Purpose                                 */
00207 /*1- C Procedure *************+****************************************/
00208 
00209 
00210 /*****************+***********+****************************************/
00211 /*                                                                    */
00212 /*   GSI, Gesellschaft fuer Schwerionenforschung mbH                  */
00213 /*   Postfach 11 05 52                                                */
00214 /*   D-64220 Darmstadt                                               */
00215 /*                                                                    */
00216 /*1+ C Procedure *************+****************************************/
00217 /*                                                                    */
00218 /*+ Module      : f_evt_get_subevent                                  */
00219 /*                                                                    */
00220 /*--------------------------------------------------------------------*/
00221 /*+ CALLING     : l_sts = f_evt_get_subevent(ve10_1 *,subevent,**head,**data,*lwords) */
00222 /*--------------------------------------------------------------------*/
00223 /*                                                                    */
00224 /*+ PURPOSE     : get subevent pointer                                */
00225 /*                                                                    */
00226 /*+ ARGUMENTS   :                                                     */
00227 /*                                                                    */
00228 /*+   ve10_1    : (s_ve10_1 *) event header pointer                   */
00229 /*+  subevent   : subevent number (1,2,3...)                          */
00230 /*                If = 0, f_evt_get_subevent returns the number of    */
00231 /*                subevents. In this case the following arguments     */
00232 /*                might be NULL.                                      */
00233 /*+   head      : Address of s_ves10_1 subevent header pointer        */
00234 /*+   data      : Address of INTS4 event data pointer                 */
00235 /*+   lwords    : Address of INTS4 to get number of data longwords    */
00236 /*                                                                    */
00237 /*+ Return type : int                                                 */
00238 /*-               GETEVT__SUCCESS   : Found subevent.                 */
00239 /*-               GETEVT__NOMORE    : No more subevents.              */
00240 /*+ Declaration :                                                     */
00241 /*                INTS4 f_evt_get_subevent( */
00242 /*                   s_ve10_1 *, INTS4, INTS4 **, INTS4 **, INTS4 *); */
00243 /*1- C Procedure *************+****************************************/
00244 INTS4 f_evt_get_subevent(s_ve10_1 *ps_ve10_1, INTS4 l_subevent, INTS4 **pl_se, INTS4 **pl_d,INTS4 *pl_lwords)
00245 {
00246   s_ves10_1 *ps_ves10_1;
00247   INTS4      l_total,l_sub,l_sum;
00248   INTS4      l,ll,l_status,*pl_next;
00249 
00250   if(ps_ve10_1 == NULL) return(GETEVT__FAILURE);
00251   pl_next = (INTS4 *) (ps_ve10_1 + 1);
00252   l_total    = ps_ve10_1->l_dlen-4;     /* total words in subevents */
00253   l_sum      = 0;
00254   ll=0;
00255   while(l_sum < l_total)
00256   {
00257     ps_ves10_1 = (s_ves10_1 *) pl_next;
00258     ll++;
00259     if(ll == l_subevent)
00260     {
00261       if(pl_lwords != NULL) *pl_lwords  = ps_ves10_1->l_dlen/2-1;
00262       if(pl_se     != NULL) *pl_se      = (INTS4 *) ps_ves10_1;
00263       if(pl_d      != NULL) *pl_d       = (INTS4 *) (ps_ves10_1+1);
00264       return(GETEVT__SUCCESS);
00265     }
00266     l_sub      = ps_ves10_1->l_dlen+4; /* total words of subevent */
00267     l_sum     += l_sub;
00268     pl_next    = (INTS4 *)(ps_ves10_1);
00269     pl_next   += l_sub/2;
00270   }
00271   if(pl_lwords != NULL) *pl_lwords  = ll;
00272   if(pl_se     != NULL) *pl_se      = NULL;
00273   if(pl_d      != NULL) *pl_d       = NULL;
00274   if(l_subevent == 0) return(ll);
00275   else                return(GETEVT__NOMORE);
00276 }
00277 /*****************+***********+****************************************/
00278 /*                                                                    */
00279 /*   GSI, Gesellschaft fuer Schwerionenforschung mbH                  */
00280 /*   Postfach 11 05 52                                                */
00281 /*   D-64220 Darmstadt                                               */
00282 /*                                                                    */
00283 /*1+ C Procedure *************+****************************************/
00284 /*                                                                    */
00285 /*+ Module      : f_evt_type                                          */
00286 /*                                                                    */
00287 /*--------------------------------------------------------------------*/
00288 /*+ CALLING     : l_status = f_evt_type(bufhe,evhe,sid,long,hex,data) */
00289 /*--------------------------------------------------------------------*/
00290 /*                                                                    */
00291 /*+ PURPOSE     : print event                                         */
00292 /*                                                                    */
00293 /*+ ARGUMENTS   :                                                     */
00294 /*                                                                    */
00295 /*+   bufhe     : (s_bufhe *) buffer header pointer (=NULL no output) */
00296 /*+   evhe      : (s_evhe *) event header pointer (=NULL no output)   */
00297 /*+    sid      :  subevent ID (-1 is all)                            */
00298 /*+   long      :  output longwords                                   */
00299 /*+   hex       :  output hex longwords                               */
00300 /*+   data      :  output data                                        */
00301 /*                                                                    */
00302 /*+ Return type : int                                                 */
00303 /*+ Declaration :                                                     */
00304 /*                INTS4 f_evt_type( */
00305 /*                      s_bufhe *,s_evhe *, INTS4, INTS4, INTS4, INTS4); */
00306 /*1- C Procedure *************+****************************************/
00307 INTS4 f_evt_type(s_bufhe *ps_bufhe,s_evhe *ps_evhe, INTS4 l_subid,INTS4 l_long,INTS4 l_hex,INTS4 l_data)
00308 {
00309   s_ves10_1 *ps_ves10_1;
00310   s_ve10_1  *ps_ve10_1;
00311   s_filhe *ps_filhe;
00312   INTS2     *pi_data;
00313   INTS4     *pl_data;
00314   INTS4      l_mode,l_s;
00315   INTS4      l,ll,l_status,l_ldata,l_used;
00316   CHARS c_line[132];
00317   CHARS c_full[132];
00318   CHARS c_time[32];
00319 
00320   strcpy(c_full,"  ");
00321   l_ldata=l_data;
00322   if((l_hex+l_long) > 0) l_ldata=1;
00323 
00324 /* Print buffer header (file header) */
00325 if(ps_bufhe != NULL)
00326 {
00327   sprintf(c_line,"--------------------------------------------------------");
00328   printf("%s\n",c_line);
00329   /* calculate real buffer size */
00330   ll=ps_bufhe->l_dlen*2;
00331   if(ll%512 > 0)ll += 512-ll%512;
00332   /* file header */
00333   l_status = f_ut_utime(ps_bufhe->l_time[0],ps_bufhe->l_time[1],c_time);
00334   if(ps_bufhe->i_type == 2000)
00335   {
00336     ps_filhe=(s_filhe *)ps_bufhe;
00337     sprintf(c_line,"File header info:");
00338     printf("%s\n",c_line);
00339     sprintf(c_line,"Size:    %d [%d b], used %d [b]",ps_filhe->filhe_dlen,ll,ps_filhe->filhe_used*2);
00340     printf("%s\n",c_line);
00341     sprintf(c_line,"Label:   %s",ps_filhe->filhe_label);
00342     printf("%s\n",c_line);
00343     sprintf(c_line,"File:    %s",ps_filhe->filhe_file);
00344     printf("%s\n",c_line);
00345     sprintf(c_line,"User:    %s",ps_filhe->filhe_user);
00346     printf("%s\n",c_line);
00347     sprintf(c_line,"Time:    %s",ps_filhe->filhe_time);
00348     printf("%s\n",c_line);
00349     sprintf(c_line,"Run:     %s",ps_filhe->filhe_run);
00350     printf("%s\n",c_line);
00351     sprintf(c_line,"Exp:     %s",ps_filhe->filhe_exp);
00352     printf("%s\n",c_line);
00353     for(ll=0;ll<ps_filhe->filhe_lines;ll++)
00354     {
00355       sprintf(c_line,"comment: %s",ps_filhe->s_strings[ll].string);
00356       printf("%s\n",c_line);
00357     }
00358   }
00359   else
00360   {
00361    l_used=ps_bufhe->i_used;
00362    if(ps_bufhe->l_dlen > MAX__DLEN)l_used=ps_bufhe->l_free[2];
00363    sprintf(c_line,"Buffer  %9d, Length %5d[w] Size %5d[b] used %5d[w] %s",
00364         ps_bufhe->l_buf,
00365         ps_bufhe->l_dlen,
00366          ll,l_used,
00367         c_time);
00368     printf("%s\n",c_line);
00369     sprintf(c_line,"       Events %3d Type/Subtype %5d %5d FragEnd=%d FragBegin=%d Total %5d[w]",
00370         ps_bufhe->l_evt,
00371         ps_bufhe->i_type,
00372         ps_bufhe->i_subtype,
00373         ps_bufhe->h_end,
00374         ps_bufhe->h_begin,
00375         ps_bufhe->l_free[1]);
00376     printf("%s\n",c_line);
00377   }
00378   sprintf(c_line,"--------------------------------------------------------");
00379   printf("%s\n",c_line);
00380 }
00381 
00382 if(ps_evhe == NULL) return(0);
00383 
00384 /* print event 4,x or 6,x */
00385 if(ps_evhe->i_type != 10)
00386 {
00387   sprintf(c_line,"Event type %d, subtype %d, data longwords %d",
00388           ps_evhe->i_type,ps_evhe->i_subtype,ps_evhe->l_dlen/2);
00389   printf("%s\n",c_line);
00390   if((l_ldata != 0) & ((ps_evhe->i_type == 4)|(ps_evhe->i_type == 6)))
00391   { /* output data, assume data as longword */
00392     pl_data = (INTS4 *)ps_evhe;
00393     pl_data += 2;
00394     for(l=0;l<ps_evhe->l_dlen/2;l++)
00395     {
00396       sprintf(c_line,"%08x ",*pl_data);
00397       strcat(c_full,c_line);
00398       pl_data++;
00399       if(l%8 == 7)
00400       {
00401         printf("%s\n",c_full);
00402         strcpy(c_full,"  ");
00403       }
00404     }
00405     if(strlen(c_full) > 2)printf("%s\n",c_full);
00406    }
00407    return(0);
00408 }
00409 
00410 /* Print event 10,1 */
00411   ps_ve10_1 = (s_ve10_1 *)ps_evhe;
00412 /* Print event header */
00413   sprintf(c_line,"Event   %9d Type/Subtype %5d %5d Length %5d[w] Trigger %2d",
00414         ps_ve10_1->l_count,
00415         ps_ve10_1->i_type,
00416         ps_ve10_1->i_subtype,
00417         ps_ve10_1->l_dlen,
00418         ps_ve10_1->i_trigger);
00419   printf("%s\n",c_line);
00420 
00421 /********************/
00422 l_s=0;
00423 l_status=0;
00424 while(l_status == 0)
00425 {
00426   l_s++;
00427   l_status=f_evt_get_subevent(ps_ve10_1,l_s,(INTS4 **)&ps_ves10_1,(INTS4 **)&pl_data,(INTS4 *)&ll);
00428   if(l_status == 0)
00429     {
00430     if((l_subid < 0)|(l_subid == ps_ves10_1->i_procid))
00431     {
00432       sprintf(c_line,"  SubEv ID %6d Type/Subtype %5d %5d Length %5d[w] Control %2d Subcrate %2d",
00433             ps_ves10_1->i_procid,
00434             ps_ves10_1->i_type,
00435             ps_ves10_1->i_subtype,
00436             ps_ves10_1->l_dlen,
00437             ps_ves10_1->h_control,
00438             ps_ves10_1->h_subcrate);
00439     printf("%s\n",c_line);
00440       if(l_ldata != 0)
00441       { /* output data  */
00442         if((l_long != 0) | (l_hex != 0))
00443         { /* In this case we assume data as one longword per channel */
00444           for(l=0;l<ll;l++)
00445           {
00446             if(l_hex != 0) sprintf(c_line,"%04x.%04x ",(*pl_data>>16)&0xffff,*pl_data&0xffff);
00447             else           sprintf(c_line,"%8d ",*pl_data);
00448        strcat(c_full,c_line);
00449             pl_data++;
00450             if(l%8 == 7)
00451        {
00452               printf("%s\n",c_full);
00453               strcpy(c_full,"  ");
00454        }
00455           }
00456           if(strlen(c_full) > 2)printf("%s\n",c_full);
00457           strcpy(c_full,"  ");
00458         }
00459         else
00460         { /* In this case we assume data as two words per channel */
00461           for(l=0;l<ll;l++)
00462           {
00463             sprintf(c_line,"%8d%8d",*pl_data&0xffff,(*pl_data>>16)&0xffff);
00464             strcat(c_full,c_line);
00465             pl_data++;
00466             if(l%4 == 3)
00467        {
00468               printf("%s\n",c_full);
00469               strcpy(c_full,"  ");
00470        }
00471           }
00472           if(strlen(c_full) > 2)printf("%s\n",c_full);
00473           strcpy(c_full,"  ");
00474    }}}
00475 }}
00476 return(0);
00477 }
00478 /*1+ C Main ****************+******************************************/
00479 /*+ Module      : f_evt_rev_port                                      */
00480 /*--------------------------------------------------------------------*/
00481 /*+ CALLING     : f_evt_rev_port(long l_port)                         */
00482 /*--------------------------------------------------------------------*/
00483 /*                                                                    */
00484 /*+ PURPOSE     : f_evt_rev_port sets port number for event server    */
00485 /*+ ARGUMENTS   :                                                     */
00486 /*+   l_port    : Port number:                                        */
00487 /*+ Return type : int.                                                */
00488 /*+ Status codes:                                                     */
00489 /*-               GETEVT__SUCCESS   : success.                        */
00490 /*+ Declaration :                                                     */
00491 /*                INTS4 f_evt_rev_port(INTS4); */
00492 /*1- C Main ****************+******************************************/
00493 INTS4 f_evt_rev_port(INTS4 l_port)
00494 {
00495   l_gl_rev_port=l_port;
00496   printf("Use remote event server port %d\n",l_port);
00497   return 0;
00498 }
00499 /*1+ C Main ****************+******************************************/
00500 /*+ Module      : f_evt_get_open                                      */
00501 /*--------------------------------------------------------------------*/
00502 /*+ CALLING     : f_evt_get_open(long l_mode, char &c_server[], s_evt_channel &s_chan, */
00503 /*                               char **ps_info, long l_sample,l_para)*/
00504 /*--------------------------------------------------------------------*/
00505 /*                                                                    */
00506 /*+ PURPOSE     : f_evt_get_open  opens an event stream from specified*/
00507 /*                channel.                                            */
00508 /*+ ARGUMENTS   :                                                     */
00509 /*+   l_mode    : Type of server:                                     */
00510 /*-               GETEVT__FILE   : Input from file                    */
00511 /*-               GETEVT__STREAM : Input from MBS stream server       */
00512 /*-               GETEVT__TRANS  : Input from MBS transport           */
00513 /*-               GETEVT__EVENT  : Input from MBS event server        */
00514 /*-               GETEVT__REVSERV: Input from remote event server     */
00515 /*+   c_server  : Node of server or file name.                        */
00516 /*+   s_chan    : structure s_evt_channel, must be allocated.         */
00517 /*+   ps_info   : address of pointer. If it is not NULL, then         */
00518 /*                try to return file header or other information      */
00519 /*                about server. If it is NULL, then returns nothing.  */
00520 /*+   l_sample  : used by event server to send only every             */
00521 /*                'l_sample' event.                                   */
00522 /*+   l_para    : currently not used                                  */
00523 /*+ Return type : int.                                                */
00524 /*+ Status codes:                                                     */
00525 /*-               GETEVT__SUCCESS   : success.                        */
00526 /*-               GETEVT__NOFILE    : file does not exist.            */
00527 /*-               GETEVT__RDERR     : read server error.              */
00528 /*-               GETEVT__NOSERVER  : can not connect server.         */
00529 /*+ Declaration :                                                     */
00530 /*                INTS4 f_evt_get_open( */
00531 /*                INTS4, CHARS *, s_evt_channel *, CHARS **, INTS4, INTS4); */
00532 /*+ FUNCTION    : Opens the input channel and save context in s_chan. */
00533 /*+ NOTE        : Up to four input channels can be opened.            */
00534 /*1- C Main ****************+******************************************/
00535 INTS4 f_evt_get_open(INTS4 l_mode, CHARS *pc_server, s_evt_channel *ps_chan,
00536    CHARS **ps_info, INTS4 l_sample, INTS4 l_param)
00537 {
00538 
00539    INTS4 l_swap, l_swap_head, l_is_goosybuf, l_filehead=0, l_size, l_size_head, l_dummy, l_header_size;
00540    INTS2 *pi;
00541    CHARS c_file[256], *pc_temp;
00542    s_filhe *ps_filhe;
00543    struct s_varstr
00544    {
00545      INTS2 i_varstr;
00546      CHARS c_varstr[128];
00547    } s_varstr_file;
00548    INTS4 l_status,ll;
00549 
00550    if((pc_temp=strchr(pc_server,':')) != NULL)
00551      {
00552        l_mode=GETEVT__RFIO;
00553        /*       printf("rfio file %s\n",pc_server);*/
00554      }
00555 #ifndef RFIO
00556    if(l_mode == GETEVT__RFIO)
00557      {
00558        printf("rfio not supported!\n");
00559        return(GETEVT__NOFILE);
00560      }
00561 #endif
00562 
00563    if(ps_info != NULL) *ps_info=NULL;
00564    // when timeout is already set by f_evt_timeout(), do not overwrite
00565    if(ps_chan->l_timeout==0)ps_chan->l_timeout=-1; /* no timeout */
00566    strcpy(ps_chan->c_channel,pc_server);
00567    switch(l_mode) {
00568      case GETEVT__FILE :
00569        strcpy(c_file,pc_server);
00570        if(strlen(c_file) < 5) strcat(c_file,".lmd");
00571        else {
00572           pc_temp = (CHARS *) &c_file[strlen(c_file)-4];
00573           if((strcmp(pc_temp,".LMD") != 0) &&
00574              (strcmp(pc_temp,".lmd") != 0)) strcat(c_file,".lmd");
00575        }
00576 #ifdef VMS
00577         pc_temp = c_temp;
00578    sd_string.dsc$w_maxstrlen = 128;
00579    sd_string.dsc$b_dtype = DSC$K_DTYPE_VT;
00580    sd_string.dsc$b_class = DSC$K_CLASS_VS;
00581    sd_string.dsc$a_pointer = (char *) &s_varstr_file;
00582    strcpy(s_varstr_file.c_varstr,c_file);
00583    s_varstr_file.i_varstr = strlen(c_file);
00584    ps_chan->l_buf_size = MAX_BUF_LGTH;
00585    ps_chan->l_channel_no=-1;
00586    for(ll=1;ll<5;ll++)
00587    {
00588      if(l_st_ichannel[ll] == 0)
00589      {
00590        ps_chan->l_channel_no=ll;
00591        l_st_ichannel[ll] = 1;
00592        break;
00593      }
00594    }
00595    if(ps_chan->l_channel_no==-1)return(GETEVT__NOCHANNEL);
00596    l_status = i$opilf(&ps_chan->l_channel_no,&sd_string,&ps_chan->l_buf_size);
00597    if((l_status&1) != 1)
00598    {
00599        printf("%s: open error %d\n",pc_server,l_status);
00600        l_st_ichannel[ps_chan->l_channel_no]=-1;
00601        return(GETEVT__NOFILE);
00602     }
00603          l_status = i$gtslf(&ps_chan->l_channel_no,&pc_temp,&ps_chan->l_buf_size);
00604      if((l_status&1) != 1)
00605     {
00606        printf("read error %d\n",l_status);
00607        l_status = i$clilf(&ps_chan->l_channel_no);
00608        l_st_ichannel[ps_chan->l_channel_no]=-1;
00609        return(GETEVT__RDERR);
00610     }
00611          l_is_goosybuf = -1;
00612          f_evt_check_buf(c_temp, &l_size, &l_is_goosybuf, &l_swap, &l_filehead);
00613          if(ps_info != NULL) /* if user want file header be returned */
00614          {
00615            if( l_is_goosybuf == 0) /* file header */
00616            {
00617              if(l_swap)
00618              {
00619          l_status = i$clilf(&ps_chan->l_channel_no);
00620            l_status = i$opilf(&ps_chan->l_channel_no,&sd_string,&ps_chan->l_buf_size);
00621               l_status = i$gtslf(&ps_chan->l_channel_no,&pc_temp,&ps_chan->l_buf_size);
00622          ps_filhe=(s_filhe *)c_temp;
00623               f_evt_swap_filhe((s_bufhe *)ps_filhe);
00624              }
00625              *ps_info=c_temp; /* now , get file header and return */
00626       }
00627            else
00628       {
00629         *ps_info=NULL; /* not find header and return nothing */
00630       }
00631         }
00632         if( l_is_goosybuf == 1) /* no file header, reopen */
00633         {
00634      l_status = i$clilf(&ps_chan->l_channel_no);
00635        l_status = i$opilf(&ps_chan->l_channel_no,&sd_string,&ps_chan->l_buf_size);
00636         }
00637 #else
00638        if((ps_chan->l_channel_no=open(c_file,GET__OPEN_FLAG))== -1)
00639        {
00640           return(GETEVT__NOFILE);
00641        }
00642        /* read first 512 bytes */
00643        if(read(ps_chan->l_channel_no,c_temp,MIN_BUF_LGTH)!=MIN_BUF_LGTH) {
00644          printf("LMD format error: no LMD file: %s\n",c_file);
00645          close(ps_chan->l_channel_no);
00646          ps_chan->l_channel_no=-1;
00647          return(GETEVT__NOLMDFILE);
00648        }
00649 // DABC
00650         ps_chan->pLmd=NULL;
00651       if((*((INTS4 *)(c_temp+4)) == LMD__TYPE_FILE_HEADER_101_1)||
00652          (*((INTS4 *)(c_temp+4)) == 0x65000100)){
00653             close(ps_chan->l_channel_no);
00654             ps_chan->pLmd=fLmdAllocateControl();
00655             fLmdGetOpen(ps_chan->pLmd,c_file,NULL,LMD__BUFFER,LMD__NO_INDEX);
00656         ps_chan->l_server_type=l_mode;
00657         return GETEVT__SUCCESS;
00658       }
00659 // -- DABC
00660        /* check for file header, return size and swap */
00661        f_evt_check_buf(c_temp, &l_size_head, &l_is_goosybuf, &l_swap_head, &l_filehead);
00662        if(((l_is_goosybuf==0)&(l_filehead==0))|(l_size_head==0)) {
00663          printf("LMD format error: swap=%d, header=%d, isLMD=%d, size=%d\n",l_swap_head,l_filehead,l_is_goosybuf,l_size_head);
00664          close(ps_chan->l_channel_no);
00665          ps_chan->l_channel_no=-1;
00666          return(GETEVT__NOLMDFILE);
00667        }
00668        
00669        /* read file header and first buffer and check for goosy header */
00670        if(l_filehead == 1) {
00671          lseek(ps_chan->l_channel_no, 0, SEEK_SET);  /* rewind file */
00672          l_header_size=l_size_head;
00673          if(((s_filhe *)c_temp)->filhe_dlen > MAX__DLEN){
00674            l_header_size=((s_filhe *)c_temp)->filhe_used*2+48;
00675          //  printf("Large buffer, read short header %d bytes\n",l_header_size);
00676        }
00677          if(read(ps_chan->l_channel_no,c_temp,l_header_size)!=l_header_size){
00678            printf("LMD format error: no LMD file: %s\n",c_file);
00679            close(ps_chan->l_channel_no);
00680            ps_chan->l_channel_no=-1;
00681            return(GETEVT__NOLMDFILE);
00682          }
00683          if(read(ps_chan->l_channel_no,c_temp,MIN_BUF_LGTH)!=MIN_BUF_LGTH) {
00684            close(ps_chan->l_channel_no);
00685            ps_chan->l_channel_no=-1;
00686            return(GETEVT__RDERR);
00687          }
00688          f_evt_check_buf(c_temp, &l_size, &l_is_goosybuf, &l_swap, &l_dummy);
00689          if((l_is_goosybuf==0)|(l_size!=l_size_head)|(l_swap!=l_swap_head)) {
00690            printf("LMD format error: swap=%d, isLMD=%d, size=%d\n",l_swap,l_is_goosybuf,l_size);
00691            close(ps_chan->l_channel_no);
00692            ps_chan->l_channel_no=-1;
00693            return(GETEVT__NOLMDFILE);
00694          }
00695        }/* check buffer behind header */
00696        ps_chan->l_buf_size=l_size_head;
00697        lseek(ps_chan->l_channel_no, 0, SEEK_SET);  /* rewind file */
00698        if(ps_info != NULL) *ps_info=NULL;
00699        /* found file header */
00700        if(l_filehead == 1) {
00701          if(read(ps_chan->l_channel_no,c_temp,l_header_size)!=l_header_size) {
00702            printf("LMD format error: no LMD file: %s\n",c_file);
00703            close(ps_chan->l_channel_no);
00704            ps_chan->l_channel_no=-1;
00705            return(GETEVT__NOLMDFILE);
00706          }
00707          ps_filhe = (s_filhe *) c_temp;
00708          if(ps_info != NULL) {/* if user want file header be returned */
00709             if( l_swap_head ==1) f_evt_swap_filhe((s_bufhe *)ps_filhe);
00710             *ps_info=c_temp; /* now , get file header and return */
00711          }
00712          /*
00713          printf("type %d, subtype %d\n",ps_filhe->filhe_type,ps_filhe->filhe_subtype);
00714          printf("strings %d %d %d %d %d %x\n",
00715          ps_filhe->filhe_label_l,
00716          ps_filhe->filhe_file_l,
00717          ps_filhe->filhe_user_l,
00718          ps_filhe->filhe_run_l,
00719          ps_filhe->filhe_exp_l,
00720          ps_filhe->filhe_lines);
00721          pi=(INTS2 *)&ps_filhe->s_strings;
00722          for(l_dummy=0;l_dummy<ps_filhe->filhe_lines;l_dummy++)
00723          {
00724            printf("comment %d, %s\n",*pi,(c_temp+366+l_dummy*80));
00725            pi += 40;
00726          }
00727          */
00728        } /* file header */
00729 
00730       /* points to a real buffer start */
00731       /* and read header buffer, if there */
00732 #endif
00733        ps_chan->l_io_buf_size=ps_chan->l_buf_size;
00734        /* may larger, but must multiplexed */
00735        break;
00736     case GETEVT__STREAM :
00737       /* initialize connection with stream server                  */
00738       if(f_stc_connectserver(pc_server,PORT__STREAM_SERV,&ps_chan->l_channel_no,
00739          &s_tcpcomm_st_evt)!=STC__SUCCESS)
00740       {
00741          return(GETEVT__NOSERVER);
00742       }
00743 
00744       l_status=f_stc_read(c_temp,16,ps_chan->l_channel_no,ps_chan->l_timeout);
00745       if(l_status == STC__TIMEOUT) return(GETEVT__TIMEOUT);
00746       if(l_status != STC__SUCCESS) return(GETEVT__RDERR);
00747 
00748       if( *((INTS4 *)(c_temp))!=1)f_evt_swap(c_temp, 16);
00749       ps_chan->l_buf_size=*((INTS4 *)(c_temp+4)); /* buffer size */
00750       ps_chan->l_bufs_in_stream=*((INTS4 *)(c_temp+8));
00751       /* # buffers per stream */
00752       ps_chan->l_stream_bufs = 0; /* counter */
00753 
00754       ps_chan->l_io_buf_size=(ps_chan->l_buf_size)*(ps_chan->l_bufs_in_stream);
00755 // DABC
00756         ps_chan->pLmd=NULL;
00757       if(*((INTS4 *)(c_temp+12)) == 0) {
00758         ps_chan->pLmd=fLmdAllocateControl();
00759         ps_chan->pLmd->pTCP=&s_tcpcomm_st_evt;
00760         fLmdInitMbs(ps_chan->pLmd,pc_server,ps_chan->l_buf_size,ps_chan->l_bufs_in_stream,0,PORT__STREAM_SERV,ps_chan->l_timeout);
00761         ps_chan->l_server_type=l_mode;
00762         return GETEVT__SUCCESS;
00763       }
00764 // -- DABC
00765       break;
00766    case GETEVT__TRANS  :
00767       /* initialize connection with stream server                  */
00768       if(f_stc_connectserver(pc_server,PORT__TRANSPORT,&ps_chan->l_channel_no,
00769          &s_tcpcomm_st_evt)!=STC__SUCCESS)
00770       {
00771          return(GETEVT__NOSERVER);
00772       }
00773 
00774       l_status=f_stc_read(c_temp,16,ps_chan->l_channel_no,ps_chan->l_timeout);
00775       if(l_status == STC__TIMEOUT) return(GETEVT__TIMEOUT);
00776       if(l_status != STC__SUCCESS) return(GETEVT__RDERR);
00777 
00778       if( *((INTS4 *)(c_temp))!=1)f_evt_swap(c_temp, 16);
00779       ps_chan->l_buf_size=*((INTS4 *)(c_temp+4)); /* buffer size */
00780       ps_chan->l_bufs_in_stream=*((INTS4 *)(c_temp+8));
00781       /* # buffers per stream */
00782       ps_chan->l_io_buf_size=ps_chan->l_buf_size;
00783 // DABC
00784         ps_chan->pLmd=NULL;
00785       if(*((INTS4 *)(c_temp+12)) == 0) {
00786         ps_chan->pLmd=fLmdAllocateControl();
00787         ps_chan->pLmd->pTCP=&s_tcpcomm_st_evt;
00788         fLmdInitMbs(ps_chan->pLmd,pc_server,ps_chan->l_buf_size,ps_chan->l_bufs_in_stream,0,PORT__TRANSPORT,ps_chan->l_timeout);
00789         ps_chan->l_server_type=l_mode;
00790         return GETEVT__SUCCESS;
00791       }
00792 // -- DABC
00793       break;
00794    case GETEVT__REVSERV  :
00795       if(f_evcli_con(ps_chan, pc_server, l_gl_rev_port, -1, l_sample)!=STC__SUCCESS)
00796       {
00797          return(GETEVT__NOSERVER);
00798       }
00799       break;
00800    case GETEVT__EVENT  :
00801       if(f_evcli_con(ps_chan, pc_server, l_gl_rev_port, -1, l_sample)!=STC__SUCCESS)
00802       {
00803          return(GETEVT__NOSERVER);
00804       }
00805       break;
00806    case GETEVT__RFIO   :
00807      ps_chan->l_channel_no=-1;
00808      ps_chan->l_channel_no=RFIO_open(pc_server,GET__OPEN_FLAG,0);
00809      if(ps_chan->l_channel_no < 0) return(GETEVT__NOSERVER);
00810       /* read first 512 bytes */
00811       if(RFIO_read(ps_chan->l_channel_no,c_temp,MIN_BUF_LGTH)!=MIN_BUF_LGTH)
00812    {
00813      printf("LMD format error: no LMD file: %s\n",pc_server);
00814      RFIO_close(ps_chan->l_channel_no);
00815      ps_chan->l_channel_no=-1;
00816           return(GETEVT__NOLMDFILE);
00817    }
00818       /* check for file header, return size and swap */
00819       f_evt_check_buf(c_temp, &l_size_head, &l_is_goosybuf, &l_swap_head, &l_filehead);
00820       if(((l_is_goosybuf==0)&(l_filehead==0))|(l_size_head==0))
00821    {
00822      printf("LMD format error: swap=%d, header=%d, isLMD=%d, size=%d\n",l_swap_head,l_filehead,l_is_goosybuf,l_size_head);
00823      RFIO_close(ps_chan->l_channel_no);
00824      ps_chan->l_channel_no=-1;
00825           return(GETEVT__NOLMDFILE);
00826    }
00827       /* read file header and first buffer and check for goosy header */
00828       if(l_filehead == 1)
00829       {
00830         RFIO_lseek(ps_chan->l_channel_no, 0, SEEK_SET);  /* rewind file */
00831       if(RFIO_read(ps_chan->l_channel_no,c_temp,l_size_head)!=l_size_head)
00832         {
00833      printf("LMD format error: no LMD file: %s\n",pc_server);
00834      RFIO_close(ps_chan->l_channel_no);
00835      ps_chan->l_channel_no=-1;
00836           return(GETEVT__NOLMDFILE);
00837         }
00838       if(RFIO_read(ps_chan->l_channel_no,c_temp,MIN_BUF_LGTH)!=MIN_BUF_LGTH)
00839    {
00840      RFIO_close(ps_chan->l_channel_no);
00841      ps_chan->l_channel_no=-1;
00842           return(GETEVT__RDERR);
00843    }
00844         f_evt_check_buf(c_temp, &l_size, &l_is_goosybuf, &l_swap, &l_dummy);
00845         if((l_is_goosybuf==0)|(l_size!=l_size_head)|(l_swap!=l_swap_head))
00846    {
00847      printf("LMD format error: swap=%d, isLMD=%d, size=%d\n",l_swap,l_is_goosybuf,l_size);
00848      RFIO_close(ps_chan->l_channel_no);
00849      ps_chan->l_channel_no=-1;
00850           return(GETEVT__NOLMDFILE);
00851    }
00852       }/* check buffer behind header */
00853       ps_chan->l_buf_size=l_size_head;
00854       RFIO_lseek(ps_chan->l_channel_no, 0, SEEK_SET);  /* rewind file */
00855       if(ps_info != NULL)*ps_info=NULL;
00856       /* found file header */
00857       if(l_filehead == 1)
00858       {
00859          if(RFIO_read(ps_chan->l_channel_no,c_temp,l_size_head)!=l_size_head)
00860          {
00861      printf("LMD format error: no LMD file: %s\n",pc_server);
00862      RFIO_close(ps_chan->l_channel_no);
00863      ps_chan->l_channel_no=-1;
00864           return(GETEVT__NOLMDFILE);
00865          }
00866    ps_filhe=(s_filhe *)c_temp;
00867         if(ps_info != NULL) /* if user want file header be returned */
00868         {
00869            if( l_swap_head ==1)f_evt_swap_filhe((s_bufhe *)ps_filhe);
00870            *ps_info=c_temp; /* now , get file header and return */
00871         }
00872    /*
00873    printf("type %d, subtype %d\n",ps_filhe->filhe_type,ps_filhe->filhe_subtype);
00874    printf("strings %d %d %d %d %d %x\n",
00875    ps_filhe->filhe_label_l,
00876    ps_filhe->filhe_file_l,
00877    ps_filhe->filhe_user_l,
00878    ps_filhe->filhe_run_l,
00879    ps_filhe->filhe_exp_l,
00880    ps_filhe->filhe_lines);
00881    pi=(INTS2 *)&ps_filhe->s_strings;
00882    for(l_dummy=0;l_dummy<ps_filhe->filhe_lines;l_dummy++)
00883    {
00884      printf("comment %d, %s\n",*pi,(c_temp+366+l_dummy*80));
00885      pi += 40;
00886    }
00887    */
00888       }/* file header */
00889       ps_chan->l_io_buf_size=ps_chan->l_buf_size;
00890      break;
00891    default             :
00892       if(ps_info != NULL)         *ps_info=NULL;
00893       return(GETEVT__NOSERVER);
00894    }       /* end of switch */
00895 
00896    if((l_mode != GETEVT__EVENT)&(l_mode != GETEVT__REVSERV))
00897    {
00898      if( (ps_chan->pc_io_buf=malloc(ps_chan->l_io_buf_size))==NULL)
00899      {
00900         printf("Memory allocation error\n");
00901         exit(2);
00902      }
00903      ps_chan->l_evt_buf_size=ps_chan->l_io_buf_size;
00904      if( (ps_chan->pc_evt_buf=malloc(ps_chan->l_evt_buf_size))==NULL)
00905      {
00906         printf("Memory allocation error\n");
00907         exit(2);
00908      }
00909    } /* l_mode != GETEVT__EVENT */
00910    ps_chan->l_server_type=l_mode;
00911    ps_chan->l_first_get=1;       /* so we will first call f_getvet_get */
00912    return GETEVT__SUCCESS;
00913 }  /* end of f_evt_get_open */
00914 
00915 /*1+ C Main ****************+******************************************/
00916 /*+ Module      : f_evt_get_event                                     */
00917 /*--------------------------------------------------------------------*/
00918 /*+ CALLING     : f_evt_get_event(s_evt_channel &s_chan, long **ppl_buffer, long **ppl_goobuf) */
00919 /*--------------------------------------------------------------------*/
00920 /*                                                                    */
00921 /*+ PURPOSE     : f_evt_get_event  returnes address of event          */
00922 /*+ ARGUMENTS   :                                                     */
00923 /*+   s_chan    : Input channel from open.                            */
00924 /*+   ppl_buffer: Address of pointer. Returns address of event.       */
00925 /*+   ppl_goobuf: Address of pointer. Returns address of buffer.      */
00926 /*+ Return type : int.                                                */
00927 /*+ Status codes:                                                     */
00928 /*-               GETEVT__SUCCESS   : success.                        */
00929 /*-               GETEVT__FRAGMENT  : Event fragment found.           */
00930 /*-               GETEVT__NOMORE    : No more events.                 */
00931 /*-               GETEVT__RDERR     : read server or file error       */
00932 /*-               GETEVT__TIMEOUT   : when enabled by f_evt_timeout   */
00933 /*+ Declaration :                                                     */
00934 /*                INTS4 f_evt_get_event(  */
00935 /*                     s_evt_channel *, INTS4 **, INTS4 **); */
00936 /*+ FUNCTION    : Get next event and returnes pointer. The pointer    */
00937 /*                may point to the event in the buffer or internal    */
00938 /*                event buffer (spanned events). The content of the   */
00939 /*                pointer may be destroyed by next call.              */
00940 /*1- C Main ****************+******************************************/
00941 INTS4 f_evt_get_event(s_evt_channel *ps_chan, INTS4 **ppl_buffer, INTS4 **ppl_goobuf)
00942 {
00943    INTS4 l_temp,l_prev_ok=1, l_stat, l_used;
00944    s_bufhe *ps_bufhe_cur;
00945    s_ve10_1 *ps_ve10_1;
00946    sMbsHeader *pevt;
00947 
00948 // DABC
00949    if(ps_chan->pLmd != NULL){
00950      if(ps_chan->l_server_type == GETEVT__TRANS)
00951        l_stat=fLmdGetMbsEvent(ps_chan->pLmd, &pevt);
00952      else if(ps_chan->l_server_type == GETEVT__STREAM)
00953        l_stat=fLmdGetMbsEvent(ps_chan->pLmd, &pevt);
00954      else if(ps_chan->l_server_type == GETEVT__FILE)
00955        l_stat=fLmdGetElement(ps_chan->pLmd,LMD__NO_INDEX, &pevt);
00956        
00957 // any error, then pointer is null
00958 if(pevt==NULL){
00959      if (ps_chan->l_server_type == GETEVT__FILE){
00960          if(l_stat == GETLMD__NOMORE) return(GETEVT__NOMORE);
00961          if(l_stat == GETLMD__EOFILE) return(GETEVT__NOMORE);
00962          if(l_stat == GETLMD__NOBUFFER) return(GETEVT__FAILURE);
00963              return(GETEVT__RDERR);
00964      } else {
00965         if(l_stat == LMD__TIMEOUT) return(GETEVT__TIMEOUT);
00966         else return (GETEVT__RDERR);
00967      }
00968 }
00969 // OK
00970      if(ppl_goobuf)*ppl_goobuf = NULL;
00971      *ppl_buffer = (INTS4 *)pevt;
00972      return(GETEVT__SUCCESS);
00973    }
00974 // -- DABC
00975    if((ps_chan->l_server_type == GETEVT__EVENT)|(ps_chan->l_server_type == GETEVT__REVSERV))
00976    {
00977       *ppl_goobuf = NULL;
00978       if(f_evcli_evt(ps_chan) != STC__SUCCESS) /* no more event, get new buffer */
00979       {
00980       l_stat=f_evcli_buf(ps_chan);
00981       if(l_stat == STC__TIMEOUT) return(GETEVT__TIMEOUT);
00982       if(l_stat != STC__SUCCESS) return(GETEVT__FAILURE);
00983       }
00984       *ppl_buffer = (INTS4 *) ps_chan->pc_evt_buf;
00985       return(GETEVT__SUCCESS);
00986    }
00987 
00988    /* e.g. read_buffer is 16384, GOOSY buffer may be only 2048,   *
00989     * this ps_chan->l_io_buf_posi indicats when the read_buffer is finished */
00990 
00991    if(ps_chan->l_first_get==1)
00992    {
00993       ps_chan->l_buf_posi=0; /* goosy buffer position */
00994       ps_chan->l_buf_lmt=0;  /* end of this goosy buffer */
00995       ps_chan->l_io_buf_posi=MAX_LONG; /* Iuput buffer(for read file) position */
00996       ps_chan->l_first_buf=1; /* boolean */
00997       ps_chan->l_first_get=0; /* boolean */
00998    }
00999    /* why we need this part codes? beacause we may (open, get, close) *
01000     * and (open, get, close) again!!! Especially when we use m-histo  *
01001     * so the second or later (open,get,close) should reset these      *
01002     * static value                                                    */
01003 
01004    ps_chan->l_evt_buf_posi=0;
01005 /* above, internal event buffer position, internal buffer will be returned */
01006 
01007    while(1)
01008    {
01009       /* if previous goosy buffer has finished, read a new buffer from i/o buffer,
01010         or from file if i/o buffer is all readout */
01011       while (ps_chan->l_buf_posi >= ps_chan->l_buf_lmt)
01012       {
01013          /* if this i/o buffer is read to end */
01014          /* end of this read_buffer which may contain several GOOSY buffers*/ 
01015          if(ps_chan->l_io_buf_posi>=ps_chan->l_io_buf_size)
01016          {
01017             if((l_temp=f_evt_get_newbuf(ps_chan))!=GETEVT__SUCCESS) return(l_temp);
01018             ps_chan->l_io_buf_posi=0;
01019          } /* end of real read server */
01020          ps_chan->ps_bufhe = (s_bufhe*) (ps_chan->pc_io_buf+ps_chan->l_io_buf_posi);
01021          l_prev_ok = (ps_chan->l_buf_no == (ps_chan->ps_bufhe->l_buf-1));
01022          ps_chan->l_buf_no = ps_chan->ps_bufhe->l_buf;
01023          if(ps_chan->ps_bufhe->i_type == 2000) {   /* file header */
01024                 printf("Unsolicited file header found!\n");
01025             ps_chan->l_io_buf_posi += ps_chan->l_buf_size;
01026             ps_chan->l_buf_posi = ps_chan->l_io_buf_posi;
01027             ps_chan->l_buf_lmt  = ps_chan->l_io_buf_posi;
01028          } else {
01029             l_used=ps_chan->ps_bufhe->l_free[2]; // large buffers HE, Oct 2007
01030             if(ps_chan->ps_bufhe->l_dlen <= MAX__DLEN)l_used=ps_chan->ps_bufhe->i_used;
01031             ps_chan->l_buf_posi = ps_chan->l_io_buf_posi + sizeof(s_bufhe);
01032             ps_chan->l_buf_lmt = ps_chan->l_buf_posi + l_used*2;
01033             ps_chan->l_io_buf_posi += ps_chan->l_buf_size;
01034          }
01035       } /* end of read file while loop */
01036 
01037       /* now, ps_chan->l_buf_posi points to start of an event or spanned event */
01038       ps_chan->ps_ve10_1 = (s_ve10_1 *) (ps_chan->pc_io_buf + ps_chan->l_buf_posi);
01039       if( (ps_chan->l_evt_buf_posi != 0) || ((ps_chan->ps_bufhe->h_end==1) && (ps_chan->l_first_buf==1)) ) {      /* if this is a spanned part of an event */
01040          ps_chan->l_buf_posi += sizeof(s_evhe);
01041          ps_chan->l_frag_len = ps_chan->ps_ve10_1->l_dlen*2;
01042       } else  /* if this is a real start of an event */
01043          ps_chan->l_frag_len = (ps_chan->ps_ve10_1->l_dlen-4)*2 + sizeof(s_ve10_1);
01044 
01045       if(ps_chan->l_frag_len + ps_chan->l_buf_posi > ps_chan->l_buf_lmt)
01046       {
01047          return(GETEVT__FRAGMENT);
01048       }
01049 
01050       /* if ps_chan->l_buf_posi is not start of an event and ps_chan->l_first_buf =1  *
01051        * then skip to next event                                   */
01052       if((ps_chan->ps_bufhe->h_end==1)&&((ps_chan->l_first_buf==1) || (l_prev_ok == 0))) {      
01053          /* if the first buffer is spanned at begin, then skip */
01054          ps_chan->l_first_buf=0; /* 24-Apr-1996 */
01055          l_prev_ok=1; /* 2001 HE */
01056          ps_chan->l_evt_buf_posi=0; /* 2001 HE stuff in event buffer obsolete */
01057          ps_chan->l_buf_posi += ps_chan->l_frag_len;
01058          /* now,ps_chan->l_buf_posi points to start of an event or spanned event */
01059          continue;       /* continue "while" loop */
01060       }
01061 
01062       ps_chan->l_first_buf=0;
01063 
01064       /* if ps_chan->l_buf_posi is start of an event and the event is not *
01065        * spanned, then return pointer                           */
01066       if( (ps_chan->l_evt_buf_posi==0) &&
01067           ( (ps_chan->ps_bufhe->h_begin==0) || ((ps_chan->l_buf_posi+ps_chan->l_frag_len) < ps_chan->l_buf_lmt) ) )
01068       {
01069          *ppl_buffer = (INTS4 *)(ps_chan->pc_io_buf+ps_chan->l_buf_posi);
01070          ps_chan->l_buf_posi += ps_chan->l_frag_len;
01071          ps_chan->l_evt_buf_posi = 0;
01072          if(ppl_goobuf) *ppl_goobuf = (INTS4 *) (ps_chan->ps_bufhe);
01073          return(GETEVT__SUCCESS);
01074       }
01075 
01076       /* 2001 HE if we start a spanned event, save buffer header of first buffer */
01077       if(ps_chan->l_evt_buf_posi==0)
01078       {
01079         memcpy((CHARS *)&ps_chan->s_bufhe_1,(CHARS *)ps_chan->ps_bufhe,sizeof(s_bufhe));
01080         ps_bufhe_cur=(s_bufhe *)&ps_chan->s_bufhe_1;
01081       }
01082      /* copy the part of this event which in this buffer into     *
01083       * internal buffer, data will be moved by realloc  function  */
01084       if(ps_chan->l_evt_buf_size<ps_chan->l_evt_buf_posi+ps_chan->l_frag_len)
01085       {
01086          ps_chan->l_evt_buf_size=ps_chan->l_evt_buf_posi+ps_chan->l_frag_len;
01087          if( (ps_chan->pc_evt_buf=realloc
01088             (ps_chan->pc_evt_buf, ps_chan->l_evt_buf_size))==NULL)
01089          {
01090             printf("Memory allocation error\n");
01091             exit(2);
01092          }
01093       }
01094       memcpy(ps_chan->pc_evt_buf+ps_chan->l_evt_buf_posi,ps_chan->pc_io_buf+ps_chan->l_buf_posi,
01095          ps_chan->l_frag_len);
01096       ps_chan->l_buf_posi += ps_chan->l_frag_len;
01097       if(ps_chan->l_evt_buf_posi == 0)
01098    {
01099    }
01100       ps_chan->l_evt_buf_posi += ps_chan->l_frag_len;
01101 
01102       if((ps_chan->ps_bufhe->h_begin!=1)||(ps_chan->l_buf_posi<ps_chan->l_buf_lmt))
01103       {
01104          /* change event header's l_dlen */
01105          ((s_ve10_1 *)(ps_chan->pc_evt_buf))->l_dlen=ps_chan->l_evt_buf_posi/2-4;
01106          *ppl_buffer=(INTS4 *)(ps_chan->pc_evt_buf);
01107          if(ppl_goobuf)*ppl_goobuf=(INTS4 *)(ps_bufhe_cur);
01108          return(GETEVT__SUCCESS);
01109       }
01110    }/* if this event spanned to next buffer, then loop */
01111 } /* end of f_evt_get_event */
01112 
01113 /*1+ C Main ******************+****************************************/
01114 /*+ Module      : f_evt_get_close                                     */
01115 /*--------------------------------------------------------------------*/
01116 /*+ CALLING     : f_evt_get_close( s_evt_channel &s_chan)             */
01117 /*--------------------------------------------------------------------*/
01118 /*                                                                    */
01119 /*+ PURPOSE     : f_evt_get_close  closes event stream of specified   */
01120 /*                channel.                                            */
01121 /*+ ARGUMENTS   :                                                     */
01122 /*+   s_chan    : Input channel from open.                            */
01123 /*+ Return type : int.                                                */
01124 /*+ Status codes:                                                     */
01125 /*-               GETEVT__SUCCESS   : success.                        */
01126 /*-               GETEVT__CLOSE_ERR : close server or file error      */
01127 /*+ Declaration :                                                     */
01128 /*                INTS4 f_evt_get_close(s_evt_channel *);  */
01129 /*+ FUNCTION    : Closes the specified input channel.                 */
01130 /*1- C Main ******************+****************************************/
01131 INTS4 f_evt_get_close(s_evt_channel * ps_chan)
01132 {
01133    INTS4 l_close_failure;
01134    INTS4 l_status;
01135 
01136 // DABC
01137    if(ps_chan->pLmd != NULL){
01138      if(ps_chan->l_server_type == GETEVT__TRANS)fLmdCloseMbs(ps_chan->pLmd);
01139      else if(ps_chan->l_server_type == GETEVT__STREAM)fLmdCloseMbs(ps_chan->pLmd);
01140      else if(ps_chan->l_server_type == GETEVT__FILE) fLmdGetClose(ps_chan->pLmd);
01141      free(ps_chan->pLmd);
01142      ps_chan->pLmd=NULL;
01143      return GETEVT__SUCCESS;
01144    }
01145 // -- DABC
01146    l_close_failure=0;
01147    if(ps_chan->l_channel_no >= 0)
01148    {
01149    switch(ps_chan->l_server_type)
01150    {
01151    case GETEVT__FILE :
01152 #ifdef VMS
01153       l_status = i$clilf(&ps_chan->l_channel_no);
01154       l_st_ichannel[ps_chan->l_channel_no]=0;
01155 #else
01156       if(close(ps_chan->l_channel_no)==-1)                     l_close_failure=1;
01157 #endif
01158       if(ps_chan->pc_io_buf  != NULL)free(ps_chan->pc_io_buf);
01159       if(ps_chan->pc_evt_buf != NULL)free(ps_chan->pc_evt_buf);
01160       break;
01161    case GETEVT__STREAM :
01162       /* disconnect with stream server                              */
01163       f_stc_write("CLOSE", 12, ps_chan->l_channel_no);
01164       if(f_stc_discclient(ps_chan->l_channel_no)!=STC__SUCCESS)l_close_failure=1;
01165       if(f_stc_close(&s_tcpcomm_st_evt)!=STC__SUCCESS)         l_close_failure=1;
01166       if(ps_chan->pc_io_buf  != NULL)free(ps_chan->pc_io_buf);
01167       if(ps_chan->pc_evt_buf != NULL)free(ps_chan->pc_evt_buf);
01168       break;
01169    case GETEVT__TRANS  :
01170       /* disconnect with stream server                              */
01171       if(f_stc_discclient(ps_chan->l_channel_no)!=STC__SUCCESS)l_close_failure=1;
01172       if(f_stc_close(&s_tcpcomm_st_evt)!=STC__SUCCESS)         l_close_failure=1;
01173       if(ps_chan->pc_io_buf  != NULL)free(ps_chan->pc_io_buf);
01174       if(ps_chan->pc_evt_buf != NULL)free(ps_chan->pc_evt_buf);
01175       break;
01176    case GETEVT__REVSERV  :
01177    case GETEVT__EVENT  :
01178       if(f_evcli_close(ps_chan)!=STC__SUCCESS)                l_close_failure=1;
01179       break;
01180    case GETEVT__RFIO :
01181      RFIO_close(ps_chan->l_channel_no);
01182      ps_chan->l_channel_no=-1;
01183       if(ps_chan->pc_io_buf  != NULL)free(ps_chan->pc_io_buf);
01184       if(ps_chan->pc_evt_buf != NULL)free(ps_chan->pc_evt_buf);
01185      break;
01186    default             :
01187          l_close_failure=1;
01188    }       /* end of switch */
01189    ps_chan->pc_io_buf=NULL;
01190    ps_chan->l_channel_no=-1;
01191    if(l_close_failure==1)  return(GETEVT__CLOSE_ERR);
01192    return GETEVT__SUCCESS;
01193    }
01194 return GETEVT__SUCCESS;
01195 
01196 }  /* end of f_evt_get_close */
01197 
01198 
01199 /*1+ C Main ****************+******************************************/
01200 /*+ Module      : f_evt_put_open                                      */
01201 /*--------------------------------------------------------------------*/
01202 /*+ CALLING     : f_evt_put_open(char *c_file[], long l_size, long l_stream, long l_type, */
01203 /*                         long l_subtype, s_evt_channel *ps_chan, char *ps_filhe) */
01204 /*--------------------------------------------------------------------*/
01205 /*                                                                    */
01206 /*+ PURPOSE     : f_evt_put_open  opens an event output stream.       */
01207 /*+ ARGUMENTS   :                                                     */
01208 /*+  c_file     : Name of file.                                       */
01209 /*+  l_size     : Size of output buffers in bytes.                    */
01210 /*+  l_stream   : Number of buffers with spanning events.             */
01211 /*+  l_type     : Buffer type number                                  */
01212 /*+  l_subtype  : Buffer subtype number                               */
01213 /*+  ps_chan    : Address of channel structure which will be returned.*/
01214 /*+  ps_filhe   : Address of user specified file header               */
01215 /*+ Return type : int.                                                */
01216 /*+ Status codes:                                                     */
01217 /*-               PUTEVT__SUCCESS   : success.                        */
01218 /*-               PUTEVT__FILE_EXIST: file already exists.            */
01219 /*-               PUTEVT__FAILURE   : failure.                        */
01220 /*+ Declaration :                                                     */
01221 /*                INTS4 f_evt_put_open(  */
01222 /*                 CHARS *,INTS4,INTS4,INTS4,INTS4,s_evt_channel *,CHARS *); */
01223 /*+ FUNCTION    : Opens the output channel and save context in        */
01224 /*                s_evt_channel structure.                            */
01225 /*+ NOTE        : Up to four output channels can be opened.           */
01226 /* User Example : In m_lea_user.c                                     */
01227 /*1- C Main ****************+******************************************/
01228 INTS4 f_evt_put_open(CHARS *pc_file, INTS4 l_size, INTS4 l_stream,
01229       INTS4 l_type, INTS4 l_subtype, s_evt_channel *ps_chan, CHARS *ps_filhe)
01230 {
01231    s_filhe *ps_file_head;
01232    INTS4 l_write_size;
01233    INTS4 l_status,ll;
01234    time_t s_timet;
01235    struct timeb s_timeb;
01236    /* because "timeb" is not "typedef", so we must use "struct" */
01237    CHARS c_mode[80];
01238    CHARS c_file[256], *pc_temp;
01239    struct s_varstr
01240    {
01241      INTS2 i_varstr;
01242      CHARS c_varstr[128];
01243    } s_varstr_file;
01244 
01245 // DABC
01246    ps_chan->pLmd=NULL;
01247    if(l_stream==0) {
01248      ps_chan->pLmd=fLmdAllocateControl();
01249      l_status=fLmdPutOpen(ps_chan->pLmd,pc_file,NULL,l_size,
01250                  LMD__NO_OVERWRITE,LMD__INDEX,LMD__LARGE_FILE);
01251      return(l_status);
01252    }
01253 // -- DABC
01254 #ifdef VMS
01255 sd_string.dsc$w_maxstrlen = 128;
01256 sd_string.dsc$b_dtype = DSC$K_DTYPE_VT;
01257 sd_string.dsc$b_class = DSC$K_CLASS_VS;
01258 sd_string.dsc$a_pointer = (char *) &s_varstr_file;
01259 #endif
01260 
01261    ps_chan->l_first_put=1;
01262 
01263    ps_chan->l_buf_size=l_size;
01264    ps_chan->l_bufs_in_stream=l_stream;
01265    ps_chan->l_buf_type=l_type;
01266    ps_chan->l_buf_subtype=l_subtype;
01267    ps_chan->l_io_buf_size=ps_chan->l_buf_size * ps_chan->l_bufs_in_stream;
01268    if( (ps_chan->pc_io_buf=malloc(ps_chan->l_io_buf_size))==NULL)
01269    {
01270       printf("Memory allocation error\n");
01271       exit(2);
01272    }
01273 
01274   strcpy(c_file,pc_file);
01275   if(strlen(c_file) < 5) strcat(c_file,".lmd");
01276   else
01277   {
01278     pc_temp = (CHARS *) &c_file[strlen(c_file)-4];
01279     if((strcmp(pc_temp,".LMD") != 0) &&
01280       (strcmp(pc_temp,".lmd") != 0)) strcat(c_file,".lmd");
01281   }
01282 #ifdef VMS
01283    strcpy(s_varstr_file.c_varstr,c_file);
01284    s_varstr_file.i_varstr = strlen(c_file);
01285    ps_chan->l_buf_size = (INTS2) l_size;
01286    ps_chan->l_channel_no=-1;
01287    for(ll=1;ll<5;ll++)
01288    {
01289      if(l_st_ochannel[ll] == 0)
01290      {
01291        ps_chan->l_channel_no=ll;
01292        l_st_ochannel[ll] = 1;
01293        break;
01294      }
01295    }
01296    if(ps_chan->l_channel_no==-1)return(PUTEVT__NOCHANNEL);
01297    l_status = i$opolf(&ps_chan->l_channel_no,&sd_string,&ps_chan->l_buf_size);
01298    if((l_status&1) != 1)   return(PUTEVT__FAILURE);
01299    else
01300    {
01301      if((l_status&1) != 1)   return(PUTEVT__FAILURE);
01302 #else
01303    if((ps_chan->l_channel_no=open(c_file,PUT__OPEN_APD_FLAG) )!= -1)
01304       return(PUTEVT__FILE_EXIST);
01305    else
01306    {      
01307       if((ps_chan->l_channel_no=open(c_file,PUT__CRT_FLAG, DEF_FILE_ACCE))== -1)
01308          return(PUTEVT__FAILURE);
01309 #endif
01310       /* open OK */
01311       else
01312       {
01313          /* output file header */
01314          ps_file_head=(s_filhe *)ps_chan->pc_io_buf;
01315          /* if user specify file header */
01316     if(ps_filhe != NULL)
01317     {
01318             memcpy(ps_file_head, ps_filhe,ps_chan->l_buf_size );
01319          }
01320          else
01321     {
01322             memset( ps_file_head, '\0', ps_chan->l_buf_size);
01323             sprintf(ps_file_head->filhe_run, "Pid %d\0", getpid());
01324             ps_file_head->filhe_run_l=strlen(ps_file_head->filhe_run);
01325          }
01326             ps_file_head->filhe_dlen=ps_chan->l_buf_size/2;
01327             ps_file_head->filhe_subtype=1;
01328             ps_file_head->filhe_type=2000;
01329             ftime(&s_timeb);
01330             ps_file_head->filhe_stime[0]=(INTS4)s_timeb.time;
01331             ps_file_head->filhe_stime[1]=(INTS4)s_timeb.millitm;
01332             ps_file_head->filhe_free[0]=1;
01333             ps_file_head->filhe_file_l=strlen(c_file);/* not include \0 */
01334             strcpy(ps_file_head->filhe_file, c_file);
01335             strcpy(ps_file_head->filhe_user, getenv("USER"));/* user name */
01336             ps_file_head->filhe_user_l=strlen(ps_file_head->filhe_user);
01337             time(&s_timet);/* get calender time */
01338             strcpy(c_mode,ctime(&s_timet));
01339             strcpy(ps_file_head->filhe_time, &c_mode[4]);
01340             ps_file_head->filhe_time[20]=' ';
01341 #ifdef VMS
01342          l_status = i$ptslf(&ps_chan->l_channel_no,&ps_file_head,&ps_chan->l_buf_size);
01343          l_write_size=l_size;
01344 #else
01345          l_write_size=write(ps_chan->l_channel_no,(CHARS *)ps_file_head,
01346             ps_chan->l_buf_size);
01347 #endif
01348          if(l_write_size==-1)
01349          {
01350             return(PUTEVT__WRERR);
01351          }
01352          if(l_write_size!=ps_chan->l_buf_size)
01353          {
01354             return(PUTEVT__TOOBIG);
01355          }
01356          return(PUTEVT__SUCCESS);
01357       }
01358   }
01359 } /* end of f_evt_put_open */
01360 
01361 /*1+ C Main ****************+******************************************/
01362 /*+ Module      : f_evt_put_event                                     */
01363 /*--------------------------------------------------------------------*/
01364 /*+ CALLING     : f_evt_put_event(s_evt_channel *ps_chan, long &la_evt_buf[]) */
01365 /*--------------------------------------------------------------------*/
01366 /*                                                                    */
01367 /*+ PURPOSE     : f_evt_put_event outputs event                       */
01368 /*+ ARGUMENTS   :                                                     */
01369 /*+  ps_chan    : Address of channel structure as returned from       */
01370 /*                f_evt_put_open.                                     */
01371 /*+  la_evt_buf  : event data array. Standard GSI event structure.    */
01372 /*+ Return type : int.                                                */
01373 /*+ Status codes:                                                     */
01374 /*-               PUTEVT__SUCCESS   : success.                        */
01375 /*-               PUTEVT__WRERR     : read server or file error       */
01376 /*+ Declaration :                                                     */
01377 /*                INTS4 f_evt_put_event(s_evt_channel *, INTS4 *); */
01378 /*+ FUNCTION    : Copies current event into output buffer. Writes     */
01379 /*                buffer to file, when full.                          */
01380 /*1- C Main ****************+******************************************/
01381 
01382 INTS4 f_evt_put_event(s_evt_channel *ps_chan, INTS4 *pl_evt_buf)
01383 {
01384    INTS4 l_evt_buf_posi;
01385    INTS4 l_buf_remain_size;  /* net free space of I/O buffer       */
01386    INTS4 l_write_size, l_temp, l_free;
01387    INTS4 l_status;
01388    CHARS *pc_addr;
01389    s_bufhe *ps_bufhe;
01390 
01391 // DABC
01392    if(ps_chan->pLmd != NULL){
01393      l_status=fLmdPutElement(ps_chan->pLmd,(sMbsHeader *)pl_evt_buf);
01394      return(l_status);
01395    }
01396 // -- DABC
01397    if(ps_chan->l_first_put==1)
01398    {
01399       ps_chan->l_first_put=0;
01400       ps_chan->l_io_buf_posi=0;
01401       ps_chan->l_buf_no=1;
01402    }
01403    l_evt_buf_posi=0;
01404    ps_chan->l_evt_size=( ((s_ve10_1 *)(pl_evt_buf))->l_dlen)*2 + sizeof(s_evhe);
01405 
01406    /* get l_buf_remain_size is available size in stream */
01407    l_buf_remain_size = ps_chan->l_io_buf_size - ps_chan->l_io_buf_posi;
01408    l_temp = (l_buf_remain_size / ps_chan->l_buf_size)*(sizeof(s_bufhe)+sizeof(s_evhe) );
01409    l_buf_remain_size -= l_temp;/* minus space occupied by buffer header and spanned event header */
01410 
01411    if(ps_chan->l_evt_size>l_buf_remain_size)/* output this i/o buffer    */
01412    {
01413       memset(ps_chan->pc_io_buf+ps_chan->l_io_buf_posi, '\0', ps_chan->l_io_buf_size-ps_chan->l_io_buf_posi);
01414       /* clear rest of this GOOSY buffer */
01415       l_temp=(ps_chan->l_io_buf_posi/ps_chan->l_buf_size)*ps_chan->l_buf_size;
01416       if(ps_chan->l_io_buf_posi%ps_chan->l_buf_size == 0) l_temp -= ps_chan->l_buf_size;
01417       /* l_temp points to start of last GOOSY buf in i/o buf area  */
01418       ps_chan->ps_bufhe=(s_bufhe *)(ps_chan->pc_io_buf + l_temp);
01419       if(l_temp==0)ps_chan->ps_bufhe->h_end = 0; /* first buf in stream, no end */
01420       else         ps_chan->ps_bufhe->h_end = ((s_bufhe *)((CHARS *)ps_chan->ps_bufhe - ps_chan->l_buf_size))->h_begin;
01421       ps_chan->ps_bufhe->h_begin = 0;     /* evt has end, so not spanned to next buf */
01422       for(l_temp=0;l_temp<ps_chan->l_io_buf_size;l_temp+=ps_chan->l_buf_size)
01423       {
01424          pc_addr = ps_chan->pc_io_buf+l_temp;
01425     ps_bufhe=(s_bufhe *)pc_addr;
01426     if(ps_bufhe->l_evt>0)/* do not write empty buffers */
01427     {
01428 #ifdef VMS
01429            l_status = i$ptslf(&ps_chan->l_channel_no,&pc_addr,&ps_chan->l_buf_size);
01430            l_write_size = (INTS4)ps_chan->l_buf_size;
01431 #else
01432            l_write_size=write(ps_chan->l_channel_no, pc_addr,ps_chan->l_buf_size);
01433 #endif
01434            if(l_write_size==-1)                  return(PUTEVT__WRERR);
01435            if(l_write_size!=ps_chan->l_buf_size) return(PUTEVT__TOOBIG);
01436     }
01437     else ps_chan->l_buf_no--; /* decrement buffer number for not written buffer */
01438       } /* buffer output loop */
01439       ps_chan->l_io_buf_posi=0;
01440       ps_chan->ps_bufhe=(s_bufhe *)ps_chan->pc_io_buf;
01441       /* get l_buf_remain_size of new stream */
01442       l_buf_remain_size = ps_chan->l_io_buf_size;
01443       l_temp = (l_buf_remain_size / ps_chan->l_buf_size) * (sizeof(s_bufhe)+sizeof(s_evhe) );
01444       l_buf_remain_size -= l_temp; /* minus space occupied by buf header and spanned event header */
01445       l_buf_remain_size += sizeof(s_evhe);/* 1st buf always no span */
01446       /* if event can not fit in a new stream, then error */
01447       if(ps_chan->l_evt_size>l_buf_remain_size)  return(PUTEVT__TOO_SMALLS);
01448    }
01449 
01450  if(ps_chan->l_io_buf_posi==0)f_evt_ini_bufhe(ps_chan);/* init all buffer headers */
01451 
01452    /* write event into i/o buf till end of event, change ps_chan->l_io_buf_posi   */
01453    while(l_evt_buf_posi<ps_chan->l_evt_size)
01454    {
01455       ps_chan->ps_ve10_1 = (s_ve10_1 *)(ps_chan->pc_io_buf + ps_chan->l_io_buf_posi);
01456       if((ps_chan->l_io_buf_posi%ps_chan->l_buf_size)==0)
01457       {
01458          ps_chan->l_io_buf_posi += sizeof(s_bufhe);
01459          ps_chan->ps_ve10_1 = (s_ve10_1 *)(ps_chan->pc_io_buf + ps_chan->l_io_buf_posi);/* behind header */
01460          if(l_evt_buf_posi != 0)
01461          {
01462            ps_chan->l_io_buf_posi += sizeof(s_evhe); /* behind element header */
01463            l_write_size=(ps_chan->l_io_buf_posi/ps_chan->l_buf_size)*ps_chan->l_buf_size;
01464            /* l_write_size points to start of this GOOSY buf in i/o buf area  */
01465            ps_chan->ps_bufhe=(s_bufhe *)(ps_chan->pc_io_buf + l_write_size);
01466            ps_chan->ps_bufhe->i_used += sizeof(s_evhe)/2;
01467          }
01468       }
01469       l_write_size=(ps_chan->l_io_buf_posi/ps_chan->l_buf_size)*ps_chan->l_buf_size;
01470       /* l_write_size points to start of this GOOSY buf in i/o buf area  */
01471       ps_chan->ps_bufhe=(s_bufhe *)(ps_chan->pc_io_buf + l_write_size);
01472       l_write_size = l_write_size + ps_chan->l_buf_size - ps_chan->l_io_buf_posi;
01473       /* l_write_size is remain free space in this GOOSY buf */
01474       if(l_write_size>(ps_chan->l_evt_size-l_evt_buf_posi))
01475          l_write_size=(ps_chan->l_evt_size-l_evt_buf_posi);
01476       memcpy(ps_chan->pc_io_buf + ps_chan->l_io_buf_posi, (CHARS *)pl_evt_buf + l_evt_buf_posi, l_write_size);
01477       ps_chan->ps_bufhe->l_evt ++; /* number of fragments */
01478       ps_chan->l_io_buf_posi += l_write_size;
01479       l_evt_buf_posi         += l_write_size;
01480       ps_chan->ps_bufhe->i_used += l_write_size/2;
01481       /* if the remain free space of GOOSY buffer does not large enough  *
01482        * to hold even a event header, then turn to next GOOSY buffer and *
01483        * fill buffer header. or 8 long words aligned ????????            */
01484       l_free=(ps_chan->l_io_buf_posi/ps_chan->l_buf_size)*ps_chan->l_buf_size;
01485       /* l_free points to start of this GOOSY buf in i/o buf area  */
01486       l_free = l_free + ps_chan->l_buf_size - ps_chan->l_io_buf_posi;
01487       if(l_free==ps_chan->l_buf_size)l_free=0;
01488       /* l_free is remain free space in this GOOSY buf */
01489       if(l_free<sizeof(s_ve10_1))ps_chan->l_io_buf_posi += l_free;
01490       /* change spanned evt header l_dlen */
01491       if(l_evt_buf_posi!=l_write_size)
01492       {
01493          ps_chan->ps_ve10_1->l_dlen= l_write_size/2;
01494          ps_chan->ps_ve10_1->i_subtype = ps_chan->l_buf_subtype;
01495          ps_chan->ps_ve10_1->i_type = ps_chan->l_buf_type;
01496       }
01497       else ps_chan->ps_ve10_1->l_dlen= (l_write_size-sizeof(s_evhe))/2; /* header of first frag */
01498       if((ps_chan->l_io_buf_posi%ps_chan->l_buf_size)==0)
01499       {
01500          if(l_evt_buf_posi<ps_chan->l_evt_size) ps_chan->ps_bufhe->h_begin = 1;
01501          if(ps_chan->l_io_buf_posi > ps_chan->l_buf_size)  /* so first buf in stream */
01502             ps_chan->ps_bufhe->h_end = ((s_bufhe *)((CHARS *)ps_chan->ps_bufhe - ps_chan->l_buf_size))->h_begin;
01503          ps_chan->ps_bufhe->l_free[1] = ((s_ve10_1 *)(pl_evt_buf))->l_dlen; /* lgth of last event in buf */
01504       }  /* end of processs prev buffer head and ... */
01505 
01506    } /* end of write event into i/o buf till end of event */
01507    return(PUTEVT__SUCCESS);
01508 } /* end of f_evt_put_event */
01509 
01510 /*1+ C Main ****************+******************************************/
01511 /*+ Module      : f_evt_put_buffer                                    */
01512 /*--------------------------------------------------------------------*/
01513 /*+ CALLING     : f_evt_put_buffer(s_evt_channel *ps_chan, s_bufhe *ps_bufhe) */
01514 /*--------------------------------------------------------------------*/
01515 /*                                                                    */
01516 /*+ PURPOSE     : f_evt_put_buffer outputs buffer                     */
01517 /*+ ARGUMENTS   :                                                     */
01518 /*+  ps_chan    : Address of channel structure as returned from       */
01519 /*                f_evt_put_open.                                     */
01520 /*+  ps_bufhe   : Buffer.                                             */
01521 /*+ Return type : int.                                                */
01522 /*+ Status codes:                                                     */
01523 /*-               PUTEVT__SUCCESS   : success.                        */
01524 /*-               PUTEVT__WRERR     : read server or file error       */
01525 /*+ Declaration :                                                     */
01526 /*                INTS4 f_evt_put_buffer(s_evt_channel *, s_bufhe *); */
01527 /*+ FUNCTION    : Writes buffer to file.                              */
01528 /*1- C Main ****************+******************************************/
01529 
01530 INTS4 f_evt_put_buffer(s_evt_channel *ps_chan, s_bufhe *ps_bufhe)
01531 {
01532    INTS4 l_write_size, l_temp, l_free;
01533    INTS4 l_status;
01534    CHARS *pc_addr;
01535 
01536 // DABC
01537    if(ps_chan->pLmd != NULL){
01538      l_status=fLmdPutBuffer(ps_chan->pLmd,(sMbsHeader *)(ps_bufhe+1),ps_bufhe->l_evt);
01539      return(l_status);
01540    }
01541 // -- DABC
01542    ps_chan->l_io_buf_posi = ps_chan->l_buf_size;
01543    ps_chan->l_io_buf_size = ps_chan->l_buf_size;
01544 #ifdef VMS
01545          l_status = i$ptslf(&ps_chan->l_channel_no,&ps_bufhe,&ps_chan->l_buf_size);
01546          l_write_size = (INTS4)ps_chan->l_buf_size;
01547 #else
01548          l_write_size=write(ps_chan->l_channel_no, (CHARS *) ps_bufhe, ps_chan->l_buf_size);
01549 #endif
01550          if(l_write_size==-1)
01551          {
01552             return(PUTEVT__WRERR);
01553          }
01554          if(l_write_size!=ps_chan->l_buf_size)
01555          {
01556             return(PUTEVT__TOOBIG);
01557          }
01558    return PUTEVT__SUCCESS;
01559 } /* end of f_evt_put_buffer */
01560 
01561 /*1+ C Main ****************+******************************************/
01562 /*+ Module      : f_evt_put_close                                     */
01563 /*--------------------------------------------------------------------*/
01564 /*+ CALLING     : f_evt_put_close(s_evt_channel *ps_chan)             */
01565 /*--------------------------------------------------------------------*/
01566 /*                                                                    */
01567 /*+ PURPOSE     : f_evt_put_close  closes specified channel.          */
01568 /*+ ARGUMENTS   :                                                     */
01569 /*+   channel   : Channel number.                                     */
01570 /*+ Return type : int.                                                */
01571 /*+ Status codes:                                                     */
01572 /*-               PUTEVT__SUCCESS   : success.                        */
01573 /*-               PUTEVT__FAILURE   : failure.                        */
01574 /*-               PUTEVT__CLOSE_ERR : close server or file error      */
01575 /*+ Declaration :                                                     */
01576 /*                INTS4 f_evt_put_close(s_evt_channel *); */
01577 /*+ FUNCTION    : Closes the specified output channel after writing   */
01578 /*                last buffer.                                        */
01579 /*1- C Main ****************+******************************************/
01580 
01581 INTS4 f_evt_put_close(s_evt_channel *ps_chan)
01582 {
01583    INTS4 l_write_size, l_temp, l_temp2;
01584    INTS4 l_status;
01585    CHARS *pc_addr;
01586 
01587 // DABC
01588    if(ps_chan->pLmd != NULL){
01589      l_status=fLmdPutClose(ps_chan->pLmd);
01590      return(l_status);
01591    }
01592 // -- DABC
01593    if(ps_chan->l_first_put==1) goto g_close;
01594 
01595    /* if not the end of GOOSY buf, fill buffer header  */
01596    if((ps_chan->l_io_buf_posi%ps_chan->l_buf_size)!=0)
01597    {
01598       l_temp=(ps_chan->l_io_buf_posi/ps_chan->l_buf_size)*ps_chan->l_buf_size;
01599       /* l_temp points to start of this GOOSY buf in i/o buf area  */
01600       memset(ps_chan->pc_io_buf+ps_chan->l_io_buf_posi, '\0',
01601          l_temp+ps_chan->l_buf_size-ps_chan->l_io_buf_posi);
01602       /* clear rest of this GOOSY buffer */
01603       ps_chan->ps_bufhe=(s_bufhe *)(ps_chan->pc_io_buf + l_temp);
01604 
01605       ps_chan->ps_bufhe->l_dlen = (ps_chan->l_buf_size - sizeof(s_bufhe))/2;
01606       ps_chan->ps_bufhe->h_begin = 0;     /* evt has end, so not spanned to next buf */
01607       if(l_temp==0)   /* so fisrt buf in stream */
01608          ps_chan->ps_bufhe->h_end = 0;
01609       else
01610          ps_chan->ps_bufhe->h_end = ((s_bufhe *)((CHARS *)ps_chan->ps_bufhe - ps_chan->l_buf_size))
01611             ->h_begin;
01612       ps_chan->ps_bufhe->i_used = (ps_chan->l_io_buf_posi%ps_chan->l_buf_size -
01613          sizeof(s_bufhe))/2;
01614    } /* end of process of infilished buffer header */
01615 
01616    /* if at the end of io buf, need not flush                */
01617    if(ps_chan->l_io_buf_posi!=ps_chan->l_io_buf_size)
01618    {
01619       if((ps_chan->l_io_buf_posi%ps_chan->l_buf_size)!=0)
01620          l_write_size=(ps_chan->l_io_buf_posi/ps_chan->l_buf_size+1) *
01621             ps_chan->l_buf_size;
01622       else
01623          l_write_size=ps_chan->l_io_buf_posi;
01624 
01625       for(l_temp=0;l_temp<l_write_size;l_temp+=ps_chan->l_buf_size){
01626 /* why use this loop instead of write the whole io_buf out? because in
01627    VMS side, the record can only be l_buf_size big, not l_write_size big */
01628 #ifdef VMS
01629          pc_addr = ps_chan->pc_io_buf+l_temp;
01630          l_status = i$ptslf(&ps_chan->l_channel_no,&pc_addr,&ps_chan->l_buf_size);
01631          l_temp2 = (INTS4)ps_chan->l_buf_size;
01632 #else
01633          l_temp2=write(ps_chan->l_channel_no, ps_chan->pc_io_buf+l_temp,
01634             ps_chan->l_buf_size);
01635 #endif
01636          if(l_temp2==-1)
01637          {
01638             return(PUTEVT__WRERR);
01639          }
01640          if(l_temp2!=ps_chan->l_buf_size)
01641          {
01642             return(PUTEVT__TOOBIG);
01643          }
01644       }
01645    } /* end of flush */
01646 
01647 g_close:
01648    free(ps_chan->pc_io_buf);
01649    /* free io buffer Memory */
01650 
01651 #ifdef VMS
01652       l_status = i$clolf(&ps_chan->l_channel_no);
01653       l_st_ochannel[ps_chan->l_channel_no]=0;
01654       ps_chan->l_channel_no=-1;
01655       return PUTEVT__SUCCESS;
01656 #else
01657       l_status=close(ps_chan->l_channel_no);
01658       ps_chan->l_channel_no=-1;
01659    if(l_status == -1) return(PUTEVT__CLOSE_ERR);
01660    else               return PUTEVT__SUCCESS;
01661 #endif
01662 } /* end of f_evt_put_close */
01663 
01664 /*1+ C Main ****************+******************************************/
01665 /*+ Module      : f_evt_error                                         */
01666 /*--------------------------------------------------------------------*/
01667 /*+ CALLING     : f_evt_error( long l_error , char &c_string[], long l_out ) */
01668 /*--------------------------------------------------------------------*/
01669 /*                                                                    */
01670 /*+ PURPOSE     : f_evt_error displays error messages.                */
01671 /*+ ARGUMENTS   :                                                     */
01672 /*+   l_error   : The error id as returned from other calls           */
01673 /*+   c_string  : The string into f_evt_error() copies the message....*/
01674 /*+    l_out    : specifies the output device for the error message.  */
01675 /*-                 out = 1 : error message is copied to string.      */
01676 /*-                 out = 0 : error message is printed on terminal.   */
01677 /*+ Return type : int (longword).                                     */
01678 /*+ Status codes:                                                     */
01679 /*-               GETEVT__SUCCESS   : success.                        */
01680 /*-               GETEVT__FAILURE   : failure                         */
01681 /*+ Declaration :                                                     */
01682 /*                INTS4 f_evt_error( INTS4 , CHARS * , INTS4 ); */
01683 /*+ FUNCTION    : f_evt_error  displays the error message for the     */
01684 /*                error id ( l_error ). If out = 1  the error         */
01685 /*                message is copied into string, else                 */
01686 /*                f_evt_error prints the message on terminal.         */
01687 /*1- C Main ******************+****************************************/
01688 INTS4 f_evt_error( INTS4 l_error , CHARS *pc_dest , INTS4 l_out )
01689 {
01690    CHARS c_line[80];
01691 
01692    switch( l_error )
01693    {
01694    case GETEVT__NOFILE   :
01695      sprintf(c_line,"-I-f_evt: no such input file");
01696      break;
01697    case GETEVT__NOTAGFILE   :
01698      sprintf(c_line,"-I-f_evt: no such tag file");
01699      break;
01700    case GETEVT__NOTAG   :
01701      sprintf(c_line,"-I-f_evt: no such event tag");
01702      break;
01703    case GETEVT__TIMEOUT   :
01704      sprintf(c_line,"-I-f_evt: time out");
01705      break;
01706    case GETEVT__NOSERVER :
01707      sprintf(c_line,"-I-f_evt: no such server");
01708      break;
01709    case GETEVT__RDERR    :
01710      sprintf(c_line,"-I-f_evt: read server error");
01711      break;
01712    case GETEVT__TAGRDERR    :
01713      sprintf(c_line,"-I-f_evt: read tag file error");
01714      break;
01715    case GETEVT__TAGWRERR    :
01716      sprintf(c_line,"-I-f_evt: write tag file error");
01717      break;
01718    case GETEVT__FRAGMENT :
01719      sprintf(c_line,"-I-f_evt: data format error");
01720      break;
01721    case GETEVT__NOMORE   :
01722      sprintf(c_line,"-I-f_evt: no more event");
01723      break;
01724    case GETEVT__CLOSE_ERR:
01725      sprintf(c_line,"-I-f_evt: close server error");
01726      break;
01727    case GETEVT__FAILURE  :
01728      sprintf(c_line,"-I-f_evt: failure");
01729      break;
01730    case GETEVT__NOCHANNEL  :
01731      sprintf(c_line,"-I-f_evt: too many channels");
01732      break;
01733    case GETEVT__NOLMDFILE  :
01734      sprintf(c_line,"-I-f_evt: input file is no LMD file");
01735      break;
01736    case GETEVT__SUCCESS  :
01737      sprintf(c_line,"-I-f_evt: success");
01738      break;
01739    case PUTEVT__FILE_EXIST   :
01740      sprintf(c_line,"-I-f_evt: output file alread exist");
01741      break;
01742    case PUTEVT__WRERR    :
01743      sprintf(c_line,"-I-f_evt: write file error");
01744      break;
01745    case PUTEVT__TOOBIG   :
01746      sprintf(c_line,"-I-f_evt: output file too large");
01747      break;
01748    case PUTEVT__TOO_SMALLS:
01749      sprintf(c_line,"-I-f_evt: event can not fit in a stream");
01750      break;
01751    case PUTEVT__CLOSE_ERR :
01752      sprintf(c_line,"-I-f_evt: close output error");
01753      break;
01754    case PUTEVT__FAILURE  :
01755      sprintf(c_line,"-I-f_evt: output failure");
01756      break;
01757    case PUTEVT__NOCHANNEL  :
01758      sprintf(c_line,"-I-f_evt: too many channels");
01759      break;
01760    default               :
01761      sprintf(c_line,"-I-f_evt: unknown message id %d",l_error);
01762      if(l_out==0)printf("%s\n",c_line);
01763      if(l_out==1)strcpy(pc_dest,c_line);
01764      return GETEVT__FAILURE;
01765    }  /* end switch( l_error )   */
01766 
01767    if(l_out==0)printf("%s\n",c_line);
01768    if(l_out==1)strcpy(pc_dest,c_line);
01769 
01770    return GETEVT__SUCCESS;
01771 }  /* end of f_evt_error */
01772 
01773 
01774 /*1+ C Main ****************+******************************************/
01775 /*+ Module      : f_evt_get_buffer                                    */
01776 /*--------------------------------------------------------------------*/
01777 /*+ CALLING     : f_evt_get_buffer(s_evt_channel &s_chan, INTS4 *pl_buffer) */
01778 /*--------------------------------------------------------------------*/
01779 /*                                                                    */
01780 /*+ PURPOSE     : f_evt_get_buffer  read one buffer from server into  */
01781 /*                user buffer.                                        */
01782 /*+ ARGUMENTS   :                                                     */
01783 /*+   s_chan    : structure s_evt_channel.                            */
01784 /*+   pl_buffer : Address of user buffer                              */
01785 /*+ Return type : int.                                                */
01786 /*+ Status codes:                                                     */
01787 /*-               GETEVT__SUCCESS   : success.                        */
01788 /*-               GETEVT__FAILURE   : failure                         */
01789 /*-               GETEVT__RDERR     : read server or file error       */
01790 /*-               GETEVT__NOMORE    : No more events.                 */
01791 /*-               GETEVT__TIMEOUT   : when enabled by f_evt_timeout   */
01792 /*+ Declaration :                                                     */
01793 /*                INTS4 f_evt_get_buffer(s_evt_channel *, INTS4 *); */
01794 /*1- C Main ****************+******************************************/
01795 INTS4 f_evt_get_buffer(s_evt_channel *ps_chan, INTS4 *ps_buffer)
01796 {
01797    INTS4 l_temp;
01798    CHARS * pc_temp;
01799    INTS4 l_status;
01800 
01801    pc_temp=(CHARS *)ps_chan->pc_io_buf;
01802    switch(ps_chan->l_server_type)
01803    {
01804    case GETEVT__FILE :
01805 #ifdef VMS
01806      l_status = i$gtslf(&ps_chan->l_channel_no,&ps_buffer,&ps_chan->l_buf_size);
01807      if((l_status&1) != 1)
01808      {
01809        ps_chan->l_io_buf_size = 0;
01810        return(GETEVT__NOMORE);
01811      }
01812       ps_chan->l_io_buf_size = (INTS4) ps_chan->l_buf_size;
01813 #else
01814       l_temp=read(ps_chan->l_channel_no,ps_buffer, ps_chan->l_buf_size);
01815       if(l_temp == 0)
01816       /* if end of file, then exit */
01817       {
01818          ps_chan->l_io_buf_size = 0;
01819          return(GETEVT__NOMORE);
01820       }
01821       else if(l_temp == -1)
01822       {
01823          ps_chan->l_io_buf_size = 0;
01824          return(GETEVT__RDERR);
01825       }
01826 #endif
01827      break;
01828    case GETEVT__RFIO     :
01829       l_temp=RFIO_read(ps_chan->l_channel_no,(CHARS *)ps_buffer, ps_chan->l_buf_size);
01830       if(l_temp == 0)
01831       {
01832          ps_chan->l_io_buf_size = 0;
01833          return(GETEVT__NOMORE);
01834       }
01835       else if(l_temp == -1)
01836       {
01837          ps_chan->l_io_buf_size = 0;
01838          return(GETEVT__RDERR);
01839       }
01840      break;
01841    case GETEVT__STREAM :
01842       if(ps_chan->l_stream_bufs == 0)
01843       if(f_stc_write("GETEVT", 12, ps_chan->l_channel_no)!=STC__SUCCESS)
01844       {
01845          return(GETEVT__FAILURE);
01846       }
01847 
01848       l_status=f_stc_read(ps_buffer, ps_chan->l_buf_size, ps_chan->l_channel_no,ps_chan->l_timeout);
01849       if(l_status == STC__TIMEOUT) return(GETEVT__TIMEOUT);
01850       if(l_status != STC__SUCCESS) return(GETEVT__RDERR);
01851       ps_chan->l_stream_bufs++;
01852       if(ps_chan->l_stream_bufs == ps_chan->l_bufs_in_stream)ps_chan->l_stream_bufs = 0;
01853       break;
01854    case GETEVT__TRANS  :
01855       l_status=f_stc_read(ps_buffer, ps_chan->l_buf_size, ps_chan->l_channel_no,ps_chan->l_timeout);
01856       if(l_status == STC__TIMEOUT) return(GETEVT__TIMEOUT);
01857       if(l_status != STC__SUCCESS) return(GETEVT__RDERR);
01858       break;
01859    case GETEVT__REVSERV  :
01860    case GETEVT__EVENT  :
01861    default             :
01862       return(GETEVT__FAILURE);
01863    }    /* end of switch */
01864 
01865    /* swap */
01866    if( ((s_bufhe *)(ps_buffer))->l_free[0] !=1)
01867       f_evt_swap((CHARS *)ps_buffer, ps_chan->l_buf_size);
01868 
01869    return(GETEVT__SUCCESS);
01870 } /* end of f_evt_get_buffer */
01871 /*1+ C Main ****************+******************************************/
01872 /*+ Module      : f_evt_skip_buffer                                    */
01873 /*--------------------------------------------------------------------*/
01874 /*+ CALLING     : f_evt_skip_buffer(s_evt_channel &s_chan, INTS4 l_buffer) */
01875 /*--------------------------------------------------------------------*/
01876 /*                                                                    */
01877 /*+ PURPOSE     : Skip buffers in file.     */
01878 /*+ ARGUMENTS   :                                                     */
01879 /*+   s_chan    : structure s_evt_channel.                            */
01880 /*+   l_buffer  : buffers to skip                                     */
01881 /*+ Return type : int.                                                */
01882 /*+ Status codes:                                                     */
01883 /*-               GETEVT__SUCCESS   : success.                        */
01884 /*-               GETEVT__FAILURE   : failure                         */
01885 /*-               GETEVT__RDERR     : read server or file error       */
01886 /*-               GETEVT__NOMORE    : No more events.                 */
01887 /*-               GETEVT__TIMEOUT   : when enabled by f_evt_timeout   */
01888 /*+ Declaration :                                                     */
01889 /*                INTS4 f_evt_skip_buffer(s_evt_channel *, INTS4); */
01890 /*1- C Main ****************+******************************************/
01891 INTS4 f_evt_skip_buffer(s_evt_channel *ps_chan, INTS4 l_buffer)
01892 {
01893    INTS4 l_temp;
01894    CHARS * pc_temp;
01895    INTS4 l_status,ii;
01896 
01897    pc_temp=(CHARS *)ps_chan->pc_io_buf;
01898    switch(ps_chan->l_server_type)
01899    {
01900    case GETEVT__FILE :
01901 #ifdef VMS
01902      for(ii=0;ii<l_buffer;ii++)
01903      {
01904        l_status = i$gtslf(&ps_chan->l_channel_no,&ps_chan->pc_io_buf,&ps_chan->l_buf_size);
01905        if((l_status&1) != 1) return(GETEVT__NOMORE);
01906      }
01907      ps_chan->l_io_buf_size = (INTS4) ps_chan->l_buf_size;
01908 #else
01909       l_temp=lseek(ps_chan->l_channel_no,l_buffer*ps_chan->l_buf_size,SEEK_CUR);
01910       if(l_temp == -1)
01911       /* if end of file, then exit */
01912       {
01913          return(GETEVT__NOMORE);
01914       }
01915 #endif
01916      break;
01917    case GETEVT__RFIO     :
01918       l_temp=RFIO_lseek(ps_chan->l_channel_no,l_buffer*ps_chan->l_buf_size,SEEK_CUR);
01919       if(l_temp == -1)
01920       {
01921          return(GETEVT__NOMORE);
01922       }
01923      break;
01924    case GETEVT__STREAM  :
01925    case GETEVT__TRANS   :
01926    case GETEVT__REVSERV :
01927    case GETEVT__EVENT   :
01928    default              : return(GETEVT__FAILURE);
01929    }    /* end of switch */
01930 
01931    ps_chan->l_first_get=1;       /* so we will first call f_getevt_get */
01932    return(GETEVT__SUCCESS);
01933 } /* end of f_evt_skip_buffer */
01934 
01935 /*1+ C Main ****************+******************************************/
01936 /*+ Module      : f_evt_timeout                                       */
01937 /*--------------------------------------------------------------------*/
01938 /*+ CALLING     : f_evt_timeout(s_evt_channel *ps_chan, seconds)      */
01939 /*--------------------------------------------------------------------*/
01940 /*                                                                    */
01941 /*+ PURPOSE     : Set a timeout for TCP read operations               */
01942 /*+ ARGUMENTS   :                                                     */
01943 /*+  ps_chan    : Address of channel structure.                       */
01944 /*+  seconds    :  -1: wait (default)                                 */
01945 /*                 >0: if after n seconds no data arrived,            */
01946 /*                 read functions return GETEVT__TIMEOUT.             */
01947 /*+ Return type : INTS4                                               */
01948 /*+ Declaration :                                                     */
01949 /*                INTS4 f_evt_timeout(s_evt_channel *, INTS4 ); */
01950 /*+ FUNCTION    : Set a timeout for TCP read operations.              */
01951 /*                The calls of f_evt_get_event, f_evt_get_buffer      */
01952 /*               will return GETEVT__TIMEOUT when seconds have been   */
01953 /*               set to positive value.                               */
01954 /*1- C Main ****************+******************************************/
01955 INTS4 f_evt_timeout(s_evt_channel *ps_chan, INTS4 l_sec)
01956 {
01957     ps_chan->l_timeout = l_sec;
01958    return(GETEVT__SUCCESS);
01959 } /* end of f_evt_timeout */
01960 
01961 /*1- C Main ****************+******************************************/
01962 /*+ Module      : f_evt_swap                                          */
01963 /*--------------------------------------------------------------------*/
01964 /*+ CALLING     : sts = f_evt_swap(char &c_source[], long l_length)   */
01965 /*--------------------------------------------------------------------*/
01966 /*                                                                    */
01967 /*+ PURPOSE     : Long word byte swap.                                */
01968 /*+ ARGUMENTS   :                                                     */
01969 /*+    c_source    : source data array.                               */
01970 /*+    l_length    : length (in bytes)                                */
01971 /*+ FUNCTION    : Long word byte swap. Works on the source field.     */
01972 /*+ Return type : int                                                 */
01973 /*+ Status codes: bit 0: success                                      */
01974 /*1- C Procedure ***********+******************************************/
01975 INTS4 f_evt_swap(CHARS * pc_source, INTS4 l_length)
01976 {
01977    CHARS * p_s, * p_d;
01978    INTS4 l_save;
01979    CHARS ch_temp;
01980 
01981    if(l_length%4 == 2){
01982       ch_temp=*(pc_source+l_length-2);
01983       *(pc_source+l_length-2)=*(pc_source+l_length-1);
01984       *(pc_source+l_length-1)=ch_temp;
01985    }
01986    l_length=(l_length/4)*4;       /* aligned */
01987    for(p_d=pc_source,p_s=(CHARS *)&l_save; p_d<pc_source+l_length;)
01988    {
01989       l_save = *((INTS4 *)p_d);
01990       p_s += 4;                 /* increment source               */
01991       *(p_d++) = *(--p_s);
01992       *(p_d++) = *(--p_s);
01993       *(p_d++) = *(--p_s);
01994       *(p_d++) = *(--p_s);
01995    }
01996    return(0);
01997 } /* end of f_evt_swap */
01998 /*1- C Main ****************+******************************************/
01999 /*+ Module      : f_evt_swap                                          */
02000 /*--------------------------------------------------------------------*/
02001 /*+ CALLING     : sts = f_evt_swap_filhe(s_filhe *)   */
02002 /*--------------------------------------------------------------------*/
02003 /*                                                                    */
02004 /*+ PURPOSE     : Swap file header.                                */
02005 /*+ ARGUMENTS   :                                                     */
02006 /*+    c_source    : source data array.                               */
02007 /*+    l_length    : length (in bytes)                                */
02008 /*+ FUNCTION    : Long word byte swap. Works on the source field.     */
02009 /*+ Return type : int                                                 */
02010 /*+ Status codes: bit 0: success                                      */
02011 /*1- C Procedure ***********+******************************************/
02012 INTS4 f_evt_swap_filhe(s_bufhe *ps_bufhe)
02013 {
02014   CHARS * p_s, * p_d;
02015   INTS4 ii;
02016   INTS2 *pi;
02017   s_filhe *ps_filhe;
02018 
02019   f_evt_swap((CHARS *)ps_bufhe,48);
02020   if(ps_bufhe->i_type == 2000)
02021     {
02022       ps_filhe=(s_filhe *)ps_bufhe;
02023       f_evt_swap((CHARS *)&ps_filhe->filhe_lines,4);/* number of comment lines ps_filhe->filhe_lines*/
02024       ps_filhe->filhe_label_l=ps_filhe->filhe_label_l>>8;
02025       ps_filhe->filhe_file_l =ps_filhe->filhe_file_l >>8;
02026       ps_filhe->filhe_user_l =ps_filhe->filhe_user_l >>8;
02027       ps_filhe->filhe_run_l  =ps_filhe->filhe_run_l  >>8;
02028       ps_filhe->filhe_exp_l  =ps_filhe->filhe_exp_l  >>8;
02029       pi=(INTS2 *)&ps_filhe->s_strings;
02030       for(ii=0;ii<ps_filhe->filhe_lines;ii++)
02031       {
02032    *pi=*pi>>8;
02033    pi += 40;
02034       }
02035       }
02036 
02037       return(0);
02038     } /* end of f_evt_swap */
02039 
02040 /*1- C Main ****************+******************************************/
02041 /*+ Module      : f_evt_get_buffer_ptr                                    */
02042 /*--------------------------------------------------------------------*/
02043 /*+ CALLING     : f_evt_get_buffer_ptr(s_evt_channel &s_chan)             */
02044 /*--------------------------------------------------------------------*/
02045 /*                                                                    */
02046 /*+ PURPOSE     : f_evt_get_buffer_ptr returns buffer pointer         */
02047 /*+ ARGUMENTS   :                                                     */
02048 /*+   s_chan    : structure s_evt_channel.                            */
02049 /*+ Return type : int.                                                */
02050 /*+ Status codes:                                                     */
02051 /*1- C Main ****************+******************************************/
02052 CHARS * f_evt_get_buffer_ptr(s_evt_channel *ps_chan)
02053 {
02054    return(ps_chan->pc_io_buf);
02055 }
02056 /*1- C Main ****************+******************************************/
02057 /*+ Module      : f_evt_get_newbuf                                    */
02058 /*--------------------------------------------------------------------*/
02059 /*+ CALLING     : f_evt_get_newbuf(s_evt_channel &s_chan)             */
02060 /*--------------------------------------------------------------------*/
02061 /*                                                                    */
02062 /*+ PURPOSE     : f_evt_get_newbuf  read a buffer from server         */
02063 /*+ ARGUMENTS   :                                                     */
02064 /*+   s_chan    : structure s_evt_channel.                            */
02065 /*+ Return type : int.                                                */
02066 /*+ Status codes:                                                     */
02067 /*-               GETEVT__SUCCESS   : success.                        */
02068 /*-               GETEVT__FAILURE   : failure                         */
02069 /*-               GETEVT__RDERR     : read server or file error       */
02070 /*-               GETEVT__NOMORE    : No more events.                 */
02071 /*-               GETEVT__TIMEOUT   : when enabled by f_evt_timeout   */
02072 /*1- C Main ****************+******************************************/
02073 INTS4 f_evt_get_newbuf(s_evt_channel *ps_chan)
02074 {
02075    INTS4 l_temp;
02076    CHARS * pc_temp;
02077    INTS4 l_status;
02078 
02079    pc_temp=(CHARS *)ps_chan->pc_io_buf;
02080    switch(ps_chan->l_server_type)
02081    {
02082    case GETEVT__FILE :
02083 #ifdef VMS
02084      l_status = i$gtslf(&ps_chan->l_channel_no,&ps_chan->pc_io_buf,&ps_chan->l_buf_size);
02085      if((l_status&1) != 1)
02086      {
02087        ps_chan->l_io_buf_size = 0;
02088        return(GETEVT__NOMORE);
02089      }
02090       ps_chan->l_io_buf_size = (INTS4) ps_chan->l_buf_size;
02091 #else
02092      while(1){
02093       l_temp=read(ps_chan->l_channel_no,pc_temp, ps_chan->l_io_buf_size);
02094       if(l_temp == 0)                    return(GETEVT__NOMORE);
02095       if(l_temp == -1)                   return(GETEVT__RDERR);
02096       if(l_temp < ps_chan->l_io_buf_size)return(GETEVT__RDERR);
02097       break; /* skip out while(1) */
02098      } /* end of while(1) */
02099 #endif
02100      break;
02101    case GETEVT__STREAM :
02102       if(f_stc_write("GETEVT", 12, ps_chan->l_channel_no)!=STC__SUCCESS)
02103       {
02104          return(GETEVT__FAILURE);
02105       }
02106 
02107       for(l_temp=0; l_temp<ps_chan->l_bufs_in_stream; l_temp++)
02108       {
02109          l_status=f_stc_read(pc_temp, ps_chan->l_buf_size, ps_chan->l_channel_no,ps_chan->l_timeout);
02110          if(l_status == STC__TIMEOUT) return(GETEVT__TIMEOUT);
02111          if(l_status != STC__SUCCESS) return(GETEVT__RDERR);
02112          pc_temp+=ps_chan->l_buf_size;
02113       }
02114       /* if first buffer is empty, all are empty */
02115       if( ((s_bufhe *)(ps_chan->pc_io_buf))->l_evt == 0) {printf("empy stream\n");return(GETEVT__TIMEOUT);}
02116       break;
02117    case GETEVT__TRANS  :
02118          l_status=f_stc_read(pc_temp, ps_chan->l_buf_size, ps_chan->l_channel_no,ps_chan->l_timeout);
02119          if(l_status == STC__TIMEOUT) return(GETEVT__TIMEOUT);
02120          if(l_status != STC__SUCCESS) return(GETEVT__RDERR);
02121       break;
02122    case GETEVT__RFIO     :
02123       l_temp=RFIO_read(ps_chan->l_channel_no,pc_temp, ps_chan->l_io_buf_size);
02124       if(l_temp == 0)                    return(GETEVT__NOMORE);
02125       if(l_temp == -1)                   return(GETEVT__RDERR);
02126       if(l_temp < ps_chan->l_io_buf_size)return(GETEVT__RDERR);
02127      break;
02128    case GETEVT__REVSERV  :
02129    case GETEVT__EVENT  :
02130    default             :
02131       return(GETEVT__FAILURE);
02132    }    /* end of switch */
02133 
02134    /* swap */
02135    if( ((s_bufhe *)(ps_chan->pc_io_buf))->l_free[0] !=1)
02136       f_evt_swap(ps_chan->pc_io_buf, ps_chan->l_io_buf_size);
02137 
02138    return(GETEVT__SUCCESS);
02139 } /* end of f_evt_get_newbuf */
02140 
02141 /*1- C Main ****************+******************************************/
02142 /*+ Module      : f_evt_check_buf                                     */
02143 /*--------------------------------------------------------------------*/
02144 /*+ CALLING     : f_evt_check_buf(CHARS *pc_head, INTS4 *pl_goosybuf, */
02145 /*                                INTS4 *pl_swap, INTS4 *pl_filehead) */
02146 /*--------------------------------------------------------------------*/
02147 /*                                                                    */
02148 /*+ PURPOSE     : check a buffer is a file header or GOOSY buffer,    */
02149 /*                and whether it's swaped.                            */
02150 /*+ ARGUMENTS   :                                                     */
02151 /*+ pc_head     : pointer to the buffer to be checked.                */
02152 /*+ pl_goosybuf : 1 is goosy buffer, 0 is header                      */
02153 /*+ pl_swap     : 1 means different endian, 0 means same endian.      */
02154 /*+ pl_filehead : 1 means first buffer is header                      */
02155 /*+ Return type : void                                                */
02156 /*+ Status codes:                                                     */
02157 /*+ FUNCTION    : check a buffer is a file header or GOOSY buffer,    */
02158 /*                and whether it's swaped.                            */
02159 /*1- C Main ****************+******************************************/
02160 INTS4 f_evt_check_buf(CHARS *pc_head, INTS4 *pl_size, INTS4 *pl_is_goosybuf, INTS4 *pl_swap, INTS4 *pl_filehead)
02161 {
02162   INTU4 l_size;
02163 
02164       *pl_is_goosybuf=0;
02165       *pl_filehead=0;
02166       *pl_size=0;
02167       *pl_swap=0;
02168    /* first check if it's file header */
02169    if( (((s_filhe *)(pc_head))->filhe_subtype==1)&&
02170        (((s_filhe *)(pc_head))->filhe_type==2000) )
02171    {
02172       *pl_swap=0;
02173       *pl_is_goosybuf=0;
02174       *pl_filehead=1;
02175       l_size=((s_bufhe *)(pc_head))->l_dlen*2;
02176       if(l_size%512 > 0) l_size += sizeof(s_bufhe);
02177       if((l_size>>24) > 0) *pl_size=0;
02178                       else *pl_size=l_size;
02179       return(PUTEVT__SUCCESS);
02180    }
02181    f_evt_swap(pc_head, sizeof(s_filhe));
02182    if( (((s_filhe *)(pc_head))->filhe_subtype==1)&&
02183        (((s_filhe *)(pc_head))->filhe_type==2000) )
02184    {
02185       *pl_swap=1;
02186       *pl_is_goosybuf=0;
02187       *pl_filehead=1;
02188       l_size=((s_bufhe *)(pc_head))->l_dlen*2;
02189       if(l_size%512 > 0) l_size += sizeof(s_bufhe);
02190       if((l_size>>24) > 0) *pl_size=0;
02191                       else *pl_size=l_size;
02192       return(PUTEVT__SUCCESS);
02193    }
02194 
02195    /* if not file header, check if it's goosy buffer header */
02196       l_size=((s_bufhe *)(pc_head))->l_dlen*2;
02197       if(l_size%512 > 0) l_size += sizeof(s_bufhe);
02198    if( (((l_size>>24)== 0)&&(l_size > 0))&&
02199        (((s_bufhe *)(pc_head))->h_begin<2)&&
02200        (((s_bufhe *)(pc_head))->h_begin>=0)&&
02201        (((s_bufhe *)(pc_head))->h_end<2)&&
02202        (((s_bufhe *)(pc_head))->h_end>=0)&&
02203        //(((s_bufhe *)(pc_head))->i_used<=(MAX_BUF_LGTH-sizeof(s_bufhe))/2)&&
02204         ((((s_bufhe *)(pc_head))->i_used>0)||(((s_bufhe *)(pc_head))->l_free[2]>0))&&
02205        //(((s_bufhe *)(pc_head))->i_used>0)&&
02206        ( (((s_bufhe *)(pc_head))->l_free[0]==1)||
02207          (((s_bufhe *)(pc_head))->l_free[0]==0)||
02208 /* above, because some old lsm file forgot to set this bit, so it's zero */
02209          (((s_bufhe *)(pc_head))->l_free[0]==256*256*256) ) )
02210    {
02211       *pl_swap=1; /* !!!, because already swaped once */
02212       *pl_is_goosybuf=1;
02213       *pl_size=l_size;
02214       return(PUTEVT__SUCCESS);
02215    }
02216    f_evt_swap(pc_head, sizeof(s_filhe));
02217    l_size=((s_bufhe *)(pc_head))->l_dlen*2+sizeof(s_bufhe);
02218    if( (((l_size>>24)== 0)&&(l_size > 0))&&
02219        (((s_bufhe *)(pc_head))->l_dlen>0)&&
02220        (((s_bufhe *)(pc_head))->h_begin<2)&&
02221        (((s_bufhe *)(pc_head))->h_begin>=0)&&
02222        (((s_bufhe *)(pc_head))->h_end<2)&&
02223        (((s_bufhe *)(pc_head))->h_end>=0)&&
02224        //(((s_bufhe *)(pc_head))->i_used<=(MAX_BUF_LGTH-sizeof(s_bufhe))/2)&&
02225         ((((s_bufhe *)(pc_head))->i_used>0)||(((s_bufhe *)(pc_head))->l_free[2]>0))&&
02226        //(((s_bufhe *)(pc_head))->i_used>0)&&
02227        ( (((s_bufhe *)(pc_head))->l_free[0]==1)||
02228          (((s_bufhe *)(pc_head))->l_free[0]==0)||
02229 /* above, because some old lsm file forgot to set this bit, so it's zero */
02230          (((s_bufhe *)(pc_head))->l_free[0]==256*256*256) ) )
02231    {
02232       *pl_swap=0; /* !!!, because already swaped 2 times */
02233       *pl_is_goosybuf=1;
02234       *pl_size=l_size;
02235       return(PUTEVT__SUCCESS);
02236    }
02237    return(PUTEVT__SUCCESS);
02238 } /* end of f_evt_check_buf */
02239 
02240 /*1- C Main ****************+******************************************/
02241 /*+ Module      : f_evt_ini_bufhe                                     */
02242 /*--------------------------------------------------------------------*/
02243 /*+ CALLING     : f_evt_ini_bufhe(s_evt_channel *ps_chan)             */
02244 /*--------------------------------------------------------------------*/
02245 /*                                                                    */
02246 /*+ PURPOSE     : pre-fill each GOOSY buffer header in a stream       */
02247 /*+ ARGUMENTS   :                                                     */
02248 /*+  ps_chan    : Address of channel structure.                       */
02249 /*+ Return type : void          .                                     */
02250 /*1- C Main ****************+******************************************/
02251 INTS4 f_evt_ini_bufhe(s_evt_channel *ps_chan)
02252 {
02253    INTS4 l_temp;
02254    struct timeb s_timeb;
02255 
02256    /* because "timeb" is not "typedef", so we must use "struct" */
02257 
02258    for(l_temp=0; l_temp<ps_chan->l_io_buf_size; l_temp += ps_chan->l_buf_size)
02259    {
02260       ps_chan->ps_bufhe=(s_bufhe *)(ps_chan->pc_io_buf + l_temp);
02261       ps_chan->ps_bufhe->l_dlen = (ps_chan->l_buf_size - sizeof(s_bufhe))/2;
02262       ps_chan->ps_bufhe->i_subtype = ps_chan->l_buf_subtype;
02263       ps_chan->ps_bufhe->i_type = ps_chan->l_buf_type;
02264       ps_chan->ps_bufhe->h_begin = 0;
02265       ps_chan->ps_bufhe->h_end = 0;
02266       ps_chan->ps_bufhe->i_used = 0;
02267       ps_chan->ps_bufhe->l_buf = ps_chan->l_buf_no; ps_chan->l_buf_no++;
02268       ps_chan->ps_bufhe->l_evt = 0;
02269       ps_chan->ps_bufhe->l_current_i = 0;
02270       ftime(&s_timeb);
02271       ps_chan->ps_bufhe->l_time[0]=(INTS4)s_timeb.time;
02272       ps_chan->ps_bufhe->l_time[1]=(INTS4)s_timeb.millitm;
02273       ps_chan->ps_bufhe->l_free[0] = 1; /* for swap flag */
02274       ps_chan->ps_bufhe->l_free[1] = 0;
02275       ps_chan->ps_bufhe->l_free[2] = 0;
02276       ps_chan->ps_bufhe->l_free[3] = 0;
02277    }
02278    return(PUTEVT__SUCCESS);
02279 } /* end of f_evt_ini_bufhe */
02280 
02281 /*1+ C Main ****************+******************************************/
02282 /*+ Module      : f_evt_cre_tagfile                                     */
02283 /*--------------------------------------------------------------------*/
02284 /*+ CALLING     : f_evt_cre_tagfile(lmdfile,tagfile,filter)             */
02285 /*--------------------------------------------------------------------*/
02286 /*                                                                    */
02287 /*+ PURPOSE     : Create a tag file from lmd file                     */
02288 /*+ ARGUMENTS   :                                                     */
02289 /*+  lmdfile    : LMD file name                                       */
02290 /*+  tagfile    : tag file name                                       */
02291 /*+  filter     : optional function for filter                        */
02292 /*+ Return type : INTS4         .                                     */
02293 /*+ Status codes:                                                     */
02294 /*-               GETEVT__SUCCESS   : success.                        */
02295 /*-               GETEVT__NOFILE    : file does not exist.            */
02296 /*-               GETEVT__TAGWRERR  : tag file write error.            */
02297 /*-               GETEVT__RDERR     : lmd read error.              */
02298 /*+ Declaration :                                                     */
02299 /*                INTS4 f_evt_cre_tagfile(CHARS *,CHARS *, INTS4 (*)()); */
02300 /*+ FUNCTION    : Create a tag file from lmd file       */
02301 /*+   filter    : The filter function is called at the beginning */
02302 /*                with a NULL as argument, then for each event */
02303 /*                with the event pointer. Returning 0 skips the event, */
02304 /*                1 takes the event into the tag file. */
02305 /*                Different tag files can be created from one lmd file. */
02306 /*1- C Main ****************+******************************************/
02307 INTS4 f_evt_tag_filter(s_ve10_1 *ps_ve10_1)
02308 {
02309   /* take it */
02310 return(1);
02311 //if(ps_ve10_1 != NULL)
02312 //{
02313 //  printf("Filter   %9d Type/Subtype %5d %5d Length %5d[w] Trigger %2d\n",
02314 //                               ps_ve10_1->l_count,
02315 //                               ps_ve10_1->i_type,
02316 //                               ps_ve10_1->i_subtype,
02317 //                               ps_ve10_1->l_dlen,
02318 //                               ps_ve10_1->i_trigger);
02319 //  if(ps_ve10_1->l_count%2) return(1); /* take it */
02320 //  else                     return(0); /* skip it */
02321 //}
02322 //else                  printf("Initialized filter function\n");
02323 //  return(0);
02324 }
02325 INTS4 f_evt_cre_tagfile(CHARS *pc_lmd, CHARS *pc_tag,INTS4 (*e_filter)())
02326 {
02327   INTS4 ii,l_take_it,l_temp,l_chan,l_out,l_file_pos=0,l_bufnr=0,l_events=0;
02328   INTS4 l_firste,*pl,l_len,l_last=-1,l_lin=0,l_fragsize, la_head[2];
02329   INTS4 l_swap, l_swap_head, l_is_goosybuf, l_filehead=0, l_size, l_size_head, l_dummy, l_evsize,l_evt_buf_size=0;
02330   INTS2 *pi;
02331   INTU4 *ps,*pd;
02332   CHARS c_lmd[128], c_tag[128], *pc_temp,*pc_evt_buf=NULL;
02333   s_ve10_1 *ps_ve10_1;
02334   s_bufhe *ps_bufhe;
02335   s_taghe s_taghe;
02336   s_tag s_tag;
02337 
02338   ps_bufhe = (s_bufhe *)c_temp;
02339   printf("LMD file %s, TAG file %s\n",pc_lmd,pc_tag);
02340   /* get file attributes */
02341   if((l_chan=open(pc_lmd,GET__OPEN_FLAG))== -1)  return(GETEVT__NOFILE);
02342   /* read first 512 bytes */
02343   if(read(l_chan,c_temp,MIN_BUF_LGTH)!=MIN_BUF_LGTH)
02344     {
02345       close(l_chan);
02346       return(GETEVT__RDERR);
02347     }
02348   /* check for file header, return size and swap */
02349   f_evt_check_buf(c_temp, &l_size_head, &l_is_goosybuf, &l_swap_head, &l_filehead);
02350   if(((l_is_goosybuf==0)&(l_filehead==0))|(l_size_head==0))
02351     {
02352      printf("LMD format error: swap=%d, header=%d, isLMD=%d, size=%d\n",l_swap_head,l_filehead,l_is_goosybuf,l_size_head);
02353      close(l_chan);
02354           return(GETEVT__NOLMDFILE);
02355     }
02356   /* read file header and first buffer and check for goosy header */
02357   if(l_filehead == 1)
02358     {
02359       lseek(l_chan, 0, SEEK_SET);  /* rewind file */
02360       if(read(l_chan,c_temp,l_size_head)!=l_size_head)
02361    {
02362      close(l_chan);
02363      return(GETEVT__RDERR);
02364    }
02365       if(read(l_chan,c_temp,MIN_BUF_LGTH)!=MIN_BUF_LGTH)
02366    {
02367      close(l_chan);
02368      return(GETEVT__RDERR);
02369    }
02370       f_evt_check_buf(c_temp, &l_size, &l_is_goosybuf, &l_swap, &l_dummy);
02371       if((l_is_goosybuf==0)|(l_size!=l_size_head)|(l_swap!=l_swap_head))
02372    {
02373      printf("LMD format error: swap=%d, isLMD=%d, size=%d\n",l_swap,l_is_goosybuf,l_size);
02374      close(l_chan);
02375           return(GETEVT__NOLMDFILE);
02376    }
02377     }/* check buffer behind header */
02378 
02379   lseek(l_chan, 0, SEEK_SET);  /* rewind file */
02380 
02381   printf("Buffer swap %d, File header %d, LMD buffer %d, size %d[b]\n",l_swap,l_filehead,l_is_goosybuf,l_size);
02382 
02383   /* found file header, skip */
02384   if(l_filehead == 1)
02385     {
02386       if(read(l_chan,c_temp,l_size)!=l_size)
02387    {
02388      close(l_chan);
02389      return(GETEVT__RDERR);
02390    }
02391       l_file_pos += l_size;
02392       l_bufnr++;
02393     }
02394 
02395   /* Open and create tag file */
02396   if((l_out=open(pc_tag,PUT__CRT_FLAG,DEF_FILE_ACCE))== -1)  return(GETEVT__NOFILE);
02397   write(l_out,(CHARS *)&s_taghe,sizeof(s_taghe));
02398 
02399   /* Initialize filter function */
02400   if(e_filter != NULL) ii=(*e_filter)(NULL);
02401 
02402   while(read(l_chan,c_temp,l_size)==l_size)
02403     {
02404       l_file_pos=l_bufnr*l_size;
02405       if(l_swap) f_evt_swap(c_temp,l_size);
02406       if((ps_bufhe->h_end)&(ps_bufhe->h_begin)&(ps_bufhe->l_evt==1))
02407    {
02408      /* only fragment, next buffer */
02409      /* printf("Event fragment skipped Buffer %6d\n",l_bufnr);*/
02410      l_bufnr++;
02411      continue;
02412    }
02413       ps_ve10_1=(s_ve10_1 *)(ps_bufhe + 1);
02414       pl = (INTS4 *)ps_ve10_1;
02415       l_file_pos += sizeof(s_bufhe);
02416       /* skip fragment at begin */
02417       if(ps_bufhe->h_end)
02418    {
02419      l_len = (*pl>>1)+2;
02420      l_file_pos += (l_len<<2); /* bytes */
02421      ps_ve10_1=(s_ve10_1 *)(pl+l_len);
02422      pl = (INTS4 *)ps_ve10_1;
02423      ps_bufhe->l_evt--;
02424      /*     printf("Event fragment end skipped Buffer %6d\n",l_bufnr);*/
02425    }
02426       for(ii=1;ii<ps_bufhe->l_evt;ii++) /* except last element */
02427    {
02428      /*printf("Event %10d pos %10d Buffer %6d\n",ps_ve10_1->l_count, l_file_pos,l_bufnr);*/
02429           if(e_filter != NULL)l_take_it=(*e_filter)(ps_ve10_1);
02430      else l_take_it=1;
02431      l_len = (*pl>>1)+2;
02432      s_tag.l_event=ps_ve10_1->l_count;
02433      s_tag.l_offset=l_file_pos;
02434      if(l_take_it)
02435      {
02436        l_events++;
02437          if(ps_ve10_1->l_count != (l_last+1))
02438        {
02439          l_lin++;
02440          if(l_lin == 1)l_firste=ps_ve10_1->l_count;
02441          /*              printf("New event number offset %d, index %d\n",ps_ve10_1->l_count,l_events);*/
02442             }
02443        l_last=ps_ve10_1->l_count;
02444             if(write(l_out,(CHARS *)&s_tag,sizeof(s_tag)) != sizeof(s_tag))
02445        {
02446               close(l_chan);
02447               close(l_out);
02448          return(GETEVT__TAGWRERR);
02449        }
02450      }
02451      l_file_pos += (l_len<<2); /* bytes */
02452      ps_ve10_1=(s_ve10_1 *)(pl+l_len);
02453      pl = (INTS4 *)ps_ve10_1;
02454    }
02455 
02456       if(ps_bufhe->l_evt > 0)
02457    {
02458      if(ps_ve10_1->l_dlen < 4) printf("Buffer %d Event fragment %10d dlen %d ts %d %d trigger %d\n"
02459                   ,l_bufnr,ps_ve10_1->l_count,ps_ve10_1->l_dlen,ps_ve10_1->i_type,
02460                                            ps_ve10_1->i_subtype,ps_ve10_1->i_trigger);
02461      s_tag.l_event=ps_ve10_1->l_count;
02462      if(ps_bufhe->h_begin)
02463        {
02464          /* copy event to event buffer for filter function */
02465          if(e_filter != NULL)
02466          {
02467         /* is event buffer big enough? */
02468         l_evsize=ps_bufhe->l_free[1]+4; /* total words */
02469         if(l_evt_buf_size < l_evsize*2)
02470         {
02471             if(pc_evt_buf != NULL)free(pc_evt_buf);
02472             l_evt_buf_size=l_evsize*2;
02473             pc_evt_buf=(CHARS *)malloc(l_evt_buf_size);
02474         }
02475         /* copy event fragment to buffer */
02476         ps=(INTU4 *)ps_ve10_1;
02477         pd=(INTU4 *)pc_evt_buf;
02478         l_fragsize = ps_ve10_1->l_dlen+4; /* fragment size + header [w] */
02479         for(ii=0;ii<l_fragsize/2;ii++) *pd++ = *ps++;
02480         /* loop over fragments */
02481         while(l_fragsize < l_evsize)
02482              {
02483             if(read(l_chan,c_temp,l_size) != l_size)break;
02484             l_bufnr++;
02485             if(l_swap) f_evt_swap(c_temp,l_size);
02486             ps = (INTU4 *) (ps_bufhe+1); /* behind buffer header, fragment header */
02487             l_temp=(INTS4)*ps; /* fragment size [w] */
02488             ps += 2; /* to data behind fragment header */
02489             for(ii=0;ii<l_temp/2;ii++) *pd++ = *ps++;
02490             l_fragsize += l_temp;
02491              }
02492         if(l_fragsize < l_evsize)break;
02493         ps_ve10_1 = (s_ve10_1 *)pc_evt_buf;
02494         ps_ve10_1->l_dlen=l_evsize-4;
02495         /* rewind last buffer, will be read again by while loop */
02496         l_bufnr--;
02497                   lseek(l_chan, -l_size, SEEK_CUR);
02498          }
02499          s_tag.l_offset=-l_file_pos;
02500          /* printf("Event %10d pos %10d Buffer -%6d\n",ps_ve10_1->l_count, l_file_pos,l_bufnr);*/
02501        }
02502      else
02503        {
02504          s_tag.l_offset=l_file_pos;
02505          /* printf("Event %10d pos %10d Buffer  %6d\n",ps_ve10_1->l_count, l_file_pos,l_bufnr);*/
02506        }
02507           if(e_filter != NULL)l_take_it=(*e_filter)(ps_ve10_1);
02508      else l_take_it=1;
02509      if(l_take_it)
02510      {
02511        l_events++;
02512        if(ps_ve10_1->l_count != (l_last+1))
02513        {
02514          l_lin++;
02515          if(l_lin == 1)l_firste=ps_ve10_1->l_count;
02516          /*              printf("New event number offset %d, index %d\n",ps_ve10_1->l_count,l_events);*/
02517             }
02518        l_last=ps_ve10_1->l_count;
02519             if(write(l_out,(CHARS *)&s_tag,sizeof(s_tag)) != sizeof(s_tag))
02520        {
02521               close(l_chan);
02522               close(l_out);
02523          return(GETEVT__TAGWRERR);
02524        }
02525      }
02526    }
02527       l_bufnr++;
02528       /*      if(l_bufnr > 3) break; */
02529     }
02530   lseek(l_out, 0, SEEK_SET);  /* rewind file */
02531   s_taghe.l_endian   = 1;
02532   s_taghe.l_version  = 1;
02533   s_taghe.l_bufsize  = l_size;
02534   s_taghe.l_buffers  = l_bufnr;
02535   s_taghe.l_events   = l_events;
02536   s_taghe.l_filesize = sizeof(s_tag)*l_events;
02537   s_taghe.l_linear   = (l_lin == 1);
02538   s_taghe.l_first    = l_firste;
02539   s_taghe.l_last     = s_tag.l_event;
02540   if(write(l_out,(CHARS *)&s_taghe,sizeof(s_taghe)) != sizeof(s_taghe))
02541   {
02542     close(l_chan);
02543     close(l_out);
02544     return(GETEVT__TAGWRERR);
02545   }
02546   close(l_chan);
02547   close(l_out);
02548   printf("Buffers %6d, of size %d, Events %10d, first %d, last %d "
02549         ,s_taghe.l_buffers
02550         ,s_taghe.l_bufsize
02551         ,s_taghe.l_events
02552         ,s_taghe.l_first
02553         ,s_taghe.l_last);
02554   if(s_taghe.l_linear)printf("linear\n");
02555   else printf("not linear\n");
02556 
02557   return(GETEVT__SUCCESS);
02558 } /* end of f_evt_cre_tagfile */
02559 /*1+ C Main ****************+******************************************/
02560 /*+ Module      : f_evt_get_tagopen                                     */
02561 /*--------------------------------------------------------------------*/
02562 /*+ CALLING     : f_evt_get_tagopen(channel,tagfile,lmdfile,header,print)             */
02563 /*--------------------------------------------------------------------*/
02564 /*                                                                    */
02565 /*+ PURPOSE     : Open tag and lmd file                    */
02566 /*+ ARGUMENTS   :                                                     */
02567 /*+  channel    : s_evt_channel* , must be allocated. */
02568 /*+  tagfile    : Name of tag file */
02569 /*+  filename   : LMD file name                                       */
02570 /*+  header     : address of CHARS pointer. If it is not NULL, then         */
02571 /*                try to return file header or other information      */
02572 /*                about server. If it is NULL, then returns nothing.  */
02573 /*+  print      : Print flag: 1=verbose */
02574 /*+ Return type : INTS4         .                                     */
02575 /*+ Status codes:                                                     */
02576 /*-               GETEVT__SUCCESS   : success.                        */
02577 /*-               GETEVT__NOFILE    : file does not exist.            */
02578 /*-               GETEVT__TAGRDERR  : tag file read error.            */
02579 /*-               GETEVT__RDERR     : read server error.              */
02580 /*+ Declaration :                                                     */
02581 /*                INTS4 f_evt_get_tagopen( */
02582 /*                     s_evt_channel *,CHARS *,CHARS *,CHARS **,INTS4); */
02583 /*+ FUNCTION    : Open tag file and lmd file. If no tag file is found,       */
02584 /*                a standard f_evt_get_open is called. In this case */
02585 /*                following tag functions call standard f_evt_get functions. */
02586 /*1- C Main ****************+******************************************/
02587 INTS4 f_evt_get_tagopen(s_evt_channel *ps_chan,CHARS *pc_tag,CHARS *pc_lmd, CHARS **ps_head, INTS4 l_prihe)
02588 {
02589   INTS4 l_temp,ii;
02590   s_bufhe *ps_bufhe;
02591 
02592   ps_chan->ps_tag = NULL; /* tagfile buffer */
02593   ps_chan->ps_taghe = NULL;
02594   if(l_prihe)printf("LMD file %s, TAG file %s\n",pc_lmd,pc_tag);
02595   /* open tag file and read header */
02596   if((ps_chan->l_tagfile_no=open(pc_tag,GET__OPEN_FLAG))== -1)
02597   {
02598     /* no tag file, use normal open */
02599   /*=============================================*/
02600     ii=f_evt_get_open(GETEVT__FILE, pc_lmd, ps_chan, (CHARS **) &ps_bufhe,0,0);
02601     *ps_head = (CHARS *) ps_bufhe;
02602     if(ii!=GETEVT__SUCCESS)
02603     {
02604       printf("Error opening input file %s\n",pc_lmd);
02605       return (ii);
02606     }
02607     /*    printf("No tag file for %s\n",pc_lmd); */
02608     if(l_prihe) ii = f_evt_type(ps_bufhe,NULL,0,0,0,0);
02609     return(GETEVT__SUCCESS);
02610   }
02611   /*=============================================*/
02612   /* tag file found */
02613   ps_chan->ps_taghe = (s_taghe *)malloc(sizeof(s_taghe));
02614   if(read(ps_chan->l_tagfile_no,(CHARS *)ps_chan->ps_taghe,sizeof(s_taghe))!=sizeof(s_taghe))
02615     {
02616       free(ps_chan->ps_taghe);
02617       ps_chan->ps_taghe = NULL;
02618       close(ps_chan->l_tagfile_no);
02619       return(GETEVT__TAGRDERR);
02620     }
02621   if(ps_chan->ps_taghe->l_endian != 1)ps_chan->l_tagswap=1;
02622   if(ps_chan->l_tagswap)f_evt_swap((CHARS *)ps_chan->ps_taghe,sizeof(s_taghe));
02623   if(l_prihe)
02624     {
02625       printf("Buffers %6d, of size %d, Events %10d, first %d, last %d"
02626         ,ps_chan->ps_taghe->l_buffers
02627         ,ps_chan->ps_taghe->l_bufsize
02628         ,ps_chan->ps_taghe->l_events
02629         ,ps_chan->ps_taghe->l_first
02630         ,ps_chan->ps_taghe->l_last);
02631       if(ps_chan->ps_taghe->l_linear)printf(" linear\n");
02632       else                printf(" not linear\n");
02633     }
02634   if((ps_chan->l_channel_no=open(pc_lmd,GET__OPEN_FLAG))== -1)
02635     {
02636       if(ps_chan->ps_taghe != NULL) free(ps_chan->ps_taghe);
02637       ps_chan->ps_taghe = NULL;
02638       close(ps_chan->l_tagfile_no);
02639       return(GETEVT__NOFILE);
02640     }
02641   /* read buffer header to check if we have to swap */
02642   ps_bufhe  = (s_bufhe  *)c_temp;
02643   if(read(ps_chan->l_channel_no,c_temp,ps_chan->ps_taghe->l_bufsize)!=ps_chan->ps_taghe->l_bufsize)
02644     {
02645       if(ps_chan->ps_taghe != NULL) free(ps_chan->ps_taghe);
02646       ps_chan->ps_taghe = NULL;
02647       if(ps_chan->ps_tag != NULL) free(ps_chan->ps_tag);
02648       ps_chan->ps_tag = NULL;
02649       close(ps_chan->l_tagfile_no);
02650       close(ps_chan->l_channel_no);
02651       return(GETEVT__RDERR);
02652     }
02653   if(ps_chan->ps_taghe->l_linear == 0)
02654     {
02655   ps_chan->ps_tag = (s_tag *)malloc(ps_chan->ps_taghe->l_filesize);
02656   if(read(ps_chan->l_tagfile_no,(CHARS *)ps_chan->ps_tag,ps_chan->ps_taghe->l_filesize)!=ps_chan->ps_taghe->l_filesize)
02657     {
02658       if(ps_chan->ps_taghe != NULL) free(ps_chan->ps_taghe);
02659       ps_chan->ps_taghe = NULL;
02660       if(ps_chan->ps_tag != NULL) free(ps_chan->ps_tag);
02661       ps_chan->ps_tag = NULL;
02662       close(ps_chan->l_tagfile_no);
02663       close(ps_chan->l_channel_no);
02664       return(GETEVT__TAGRDERR);
02665     }
02666     }
02667   if(ps_bufhe->l_free[0] != 1)ps_chan->l_lmdswap=1;
02668   if(ps_chan->l_lmdswap)f_evt_swap_filhe(ps_bufhe);
02669   if(l_prihe) ii = f_evt_type(ps_bufhe,NULL,0,0,0,0);
02670   if(ps_head != NULL)
02671     {
02672       if(ps_bufhe->i_type == 2000) *ps_head = (CHARS *)ps_bufhe;
02673     }
02674   ps_chan->l_evt_buf_size=0;
02675   ps_chan->pc_evt_buf=NULL;
02676   ps_chan->l_io_buf_posi=0; /* keeps index of last event */
02677   return(GETEVT__SUCCESS);
02678 }
02679 
02680 /*1+ C Main ****************+******************************************/
02681 /*+ Module      : f_evt_get_tagnext                                     */
02682 /*--------------------------------------------------------------------*/
02683 /*+ CALLING     : f_evt_get_tagnext(channel,skip,**event)             */
02684 /*--------------------------------------------------------------------*/
02685 /*                                                                    */
02686 /*+ PURPOSE     : Get tagged event from lmd file                    */
02687 /*+ ARGUMENTS   :                                                     */
02688 /*+  channel    : s_evt_channel* , must be allocated. */
02689 /*+  event      : address of pointer. If it is not NULL, then         */
02690 /*                return event pointer.      */
02691 /*+ Return type : INTS4         .                                     */
02692 /*+ Status codes:                                                     */
02693 /*-               GETEVT__SUCCESS   : success.                        */
02694 /*-               GETEVT__NOFILE    : file does not exist.            */
02695 /*-               GETEVT__TAGRDERR  : tag file read error.            */
02696 /*-               GETEVT__RDERR     : lmd read error.              */
02697 /*-               GETEVT__FRAGMENT  : Event fragment found.           */
02698 /*-               GETEVT__NOMORE    : No more events.                 */
02699 /*+ Declaration :                                                     */
02700 /*                INTS4 f_evt_get_tagnext(s_evt_channel *,INTS4,INTS4 **); */
02701 /*+ FUNCTION    : Get next event at current position, either in tag      */
02702 /*                file, or in lmd file. Optional <skip> events are skipped. */
02703 /*1- C Main ****************+******************************************/
02704 INTS4 f_evt_get_tagnext(s_evt_channel *ps_chan,INTS4 l_skip, INTS4 **pl_event)
02705 {
02706   INTS4 ii,*pl,kk;
02707   /* no tagfile */
02708   /*=============================================*/
02709   if(ps_chan->ps_taghe == NULL)
02710     {
02711       for(kk=0;kk<=l_skip;kk++)
02712        {
02713          ii=f_evt_get_event(ps_chan, (INTS4 **) &pl, NULL);
02714     if(ii != GETEVT__SUCCESS) break;
02715        }
02716     }
02717   /*=============================================*/
02718   else
02719     {
02720       /* l_io_buf_posi is the index of last event */
02721       ii=f_evt_get_tagevent(ps_chan,ps_chan->l_io_buf_posi+l_skip+1,1,(INTS4 **)&pl);
02722     }
02723   if(pl_event != NULL) *pl_event=pl;
02724   return(ii);
02725 }
02726 /*1+ C Main ****************+******************************************/
02727 /*+ Module      : f_evt_get_tagevent                                     */
02728 /*--------------------------------------------------------------------*/
02729 /*+ CALLING     : f_evt_get_tagevent(*channel, value, mode, **event)             */
02730 /*--------------------------------------------------------------------*/
02731 /*                                                                    */
02732 /*+ PURPOSE     : Get tagged event from lmd file                    */
02733 /*+ ARGUMENTS   :                                                     */
02734 /*+  channel    : s_evt_channel* , must be allocated. */
02735 /*+   value     : event number or index */
02736 /*+   mode      : 0: value is number, 1: value is index */
02737 /*+   event     : address of pointer. If it is not NULL, then         */
02738 /*                return event pointer.      */
02739 /*+ Return type : INTS4         .                                     */
02740 /*+ Status codes:                                                     */
02741 /*-               GETEVT__SUCCESS   : success.                        */
02742 /*-               GETEVT__TAGRDERR  : tag file read error.            */
02743 /*-               GETEVT__RDERR     : lmd read error.              */
02744 /*-               GETEVT__FRAGMENT  : Event fragment found.           */
02745 /*-               GETEVT__NOMORE    : No more events.                 */
02746 /*-               GETEVT__NOTAG     : Specified event not in tag file.              */
02747 /*+ Declaration :                                                     */
02748 /*                INTS4 f_evt_get_tagevent( */
02749 /*                           s_evt_channel *,INTS4,INTS4,INTS4 **); */
02750 /*+ FUNCTION    : Get tag event. If no tag file is there, skip <value> */
02751 /*                events, or look for event number <value>     */
02752 /*1- C Main ****************+******************************************/
02753 INTS4 f_evt_get_tagevent(s_evt_channel *ps_chan,INTS4 l_value, INTS4 l_type, INTS4 **pl_event)
02754 {
02755   INTS4 l_temp,l_chan,l_tag,ii,kk,lb,l_tagbufs,l_tagrest,l_evt,l_off,l_typ,l_val,l_evsize,l_fragsize;
02756   INTS4 la_head[2],*pl;
02757   CHARS *pc;
02758   s_ve10_1 *ps_ve10_1;
02759   s_bufhe *ps_bufhe;
02760   s_tag *ps_tag;
02761   s_tag s_tag_l;
02762   s_filhe *ps_filhe;
02763   s_evt_channel s_chan_i;
02764 
02765   l_typ=l_type;
02766   l_val=l_value;
02767   /* no tagfile */
02768   /*=============================================*/
02769   if(ps_chan->ps_taghe == NULL)
02770     {
02771       /* search by index */
02772       if(l_type == 1)
02773    {
02774           if(l_val == 0) l_val=1;
02775           for(kk=0;kk<l_val;kk++)
02776        {
02777          ii=f_evt_get_event(ps_chan, (INTS4 **) &pl, NULL);
02778          if(ii != GETEVT__SUCCESS)return(ii);
02779        }
02780      *pl_event=pl;
02781    }
02782       else /* search by event number */
02783    {
02784      ii=f_evt_get_event(ps_chan, (INTS4 **) &ps_ve10_1, NULL);
02785      if(ii != GETEVT__SUCCESS)return(ii);
02786      while(ps_ve10_1->l_count != l_val)
02787        {
02788          ii=f_evt_get_event(ps_chan, (INTS4 **) &ps_ve10_1, NULL);
02789          if(ii != GETEVT__SUCCESS)return(ii);
02790        }
02791      *pl_event = (INTS4 *)ps_ve10_1;
02792    }
02793       return(GETEVT__SUCCESS);
02794     }
02795   /*=============================================*/
02796   ps_ve10_1 = (s_ve10_1 *)c_temp;
02797   ps_bufhe  = (s_bufhe  *)c_temp;
02798 
02799   /* linear==1 means that event numbers are subsequent. */
02800   /* in that case we can calculate index from number */
02801   if(ps_chan->ps_taghe->l_linear)
02802     {
02803       if(l_typ == 0)l_val=l_value-ps_chan->ps_taghe->l_first+1;
02804       l_typ=1;
02805     }
02806   /* search by index or by value */
02807   if(l_typ == 1)
02808     {
02809       if(l_val > ps_chan->ps_taghe->l_events)
02810    {
02811      printf("Event index %d not found\n",l_val);
02812      return(GETEVT__NOTAG);
02813    }
02814       if(l_val == 0) l_val=1;
02815       ps_tag=(s_tag *)&s_tag_l;
02816       lseek(ps_chan->l_tagfile_no, (l_val-1)*sizeof(s_tag)+sizeof(s_taghe), SEEK_SET);  /* set file offset*/
02817       if(read(ps_chan->l_tagfile_no,(CHARS *)ps_tag,sizeof(s_tag))!=sizeof(s_tag))
02818    {
02819      return(GETEVT__TAGRDERR);
02820    }
02821       if(ps_chan->l_tagswap)f_evt_swap((CHARS *)ps_tag,sizeof(s_tag));
02822       ps_chan->l_io_buf_posi=l_val; /* keeps index */
02823     }
02824   else
02825     {
02826       ps_tag=ps_chan->ps_tag;
02827       for(ii=1;ii<=ps_chan->ps_taghe->l_events;ii++)
02828    {
02829      if(ps_tag->l_event == l_val) break;
02830      ps_tag++;
02831    }
02832       if(ps_tag->l_event != l_val)
02833    {
02834      printf("Event number %d not found\n",l_val);
02835      return(GETEVT__NOTAG);
02836    }
02837       ps_chan->l_io_buf_posi=ii; /* keeps index of last event */
02838     }
02839   /* now we have the requested event in ps_tag */
02840   l_off=ps_tag->l_offset;
02841   if(l_off < 0) l_off=((-l_off)/ps_chan->ps_taghe->l_bufsize)*ps_chan->ps_taghe->l_bufsize;
02842   l_evt=ps_tag->l_event;
02843   /* full event in buffer, read */
02844   if(ps_tag->l_offset > 0)
02845     {
02846       ps_ve10_1 = (s_ve10_1 *)c_temp;
02847       lseek(ps_chan->l_channel_no, l_off, SEEK_SET);  /* set file offset*/
02848       if(read(ps_chan->l_channel_no,c_temp,8)!=8) return(GETEVT__RDERR);
02849       if(ps_chan->l_lmdswap)f_evt_swap(c_temp,8);
02850       if(read(ps_chan->l_channel_no,(CHARS *)&c_temp[8],ps_ve10_1->l_dlen*2)!=ps_ve10_1->l_dlen*2)return(GETEVT__RDERR);
02851       if(ps_chan->l_lmdswap)f_evt_swap((CHARS *)&c_temp[8],ps_ve10_1->l_dlen*2);
02852       /*ii=f_evt_type(NULL,(s_evhe *)ps_ve10_1,-1,0,1,1);*/
02853     }
02854   else
02855     /* spanning event begin, read to event buffer */
02856     {
02857       lseek(ps_chan->l_channel_no, l_off, SEEK_SET);  /* set file offset to buffer begin */
02858       if(read(ps_chan->l_channel_no,c_temp,sizeof(s_bufhe))!=sizeof(s_bufhe)) return(GETEVT__RDERR);
02859       if(ps_chan->l_lmdswap)f_evt_swap(c_temp,sizeof(s_bufhe));
02860       /* is event buffer big enough? */
02861       l_evsize=ps_bufhe->l_free[1]+4; /* total words */
02862       if(ps_chan->l_evt_buf_size < l_evsize*2)
02863       {
02864          if(ps_chan->pc_evt_buf != NULL)free(ps_chan->pc_evt_buf);
02865          ps_chan->l_evt_buf_size=l_evsize*2;
02866          ps_chan->pc_evt_buf=(CHARS *)malloc(ps_chan->l_evt_buf_size);
02867       }
02868       l_fragsize=0;
02869       ps_ve10_1 = (s_ve10_1 *)ps_chan->pc_evt_buf;
02870       lseek(ps_chan->l_channel_no, -ps_tag->l_offset, SEEK_SET);  /* set file offset*/
02871       if(read(ps_chan->l_channel_no,ps_chan->pc_evt_buf,8)!=8) return(GETEVT__RDERR);
02872       if(ps_chan->l_lmdswap)f_evt_swap(ps_chan->pc_evt_buf,8);
02873       pc=ps_chan->pc_evt_buf+8;
02874       /* read fragment */
02875       if(read(ps_chan->l_channel_no,pc,ps_ve10_1->l_dlen*2)!=ps_ve10_1->l_dlen*2)return(GETEVT__RDERR);
02876       l_fragsize += ps_ve10_1->l_dlen+4; /* fragment size + header [w] */
02877       pc += ps_ve10_1->l_dlen*2; /* next in event buffer */
02878       ps_ve10_1->l_dlen=l_evsize-4; /* set correct event size */
02879       /* loop over fragments */
02880       while(l_fragsize < l_evsize)
02881    {
02882      l_off += ps_chan->ps_taghe->l_bufsize; /* next buffer absolut address */
02883           lseek(ps_chan->l_channel_no,l_off+sizeof(s_bufhe), SEEK_SET);  /* set file offset*/
02884           if(read(ps_chan->l_channel_no,(CHARS *)&la_head,8)!=8) return(GETEVT__RDERR);
02885           if(ps_chan->l_lmdswap)f_evt_swap((CHARS *)&la_head,8);
02886           if(read(ps_chan->l_channel_no,pc,la_head[0]*2)!=la_head[0]*2) return(GETEVT__RDERR);
02887      pc += la_head[0]*2;
02888      l_fragsize += la_head[0];
02889    }
02890       /* now swap whole event in buffer */
02891       pc=ps_chan->pc_evt_buf+8; /* header already swapped */
02892       ps_ve10_1 = (s_ve10_1 *)ps_chan->pc_evt_buf;
02893       if(ps_chan->l_lmdswap)f_evt_swap(pc,ps_ve10_1->l_dlen*2);
02894     }
02895   *pl_event = (INTS4 *)ps_ve10_1;
02896   return(GETEVT__SUCCESS);
02897 } /* end of f_evt_ini_bufhe */
02898 
02899 /*1+ C Main ****************+******************************************/
02900 /*+ Module      : f_evt_get_tagclose                                     */
02901 /*--------------------------------------------------------------------*/
02902 /*+ CALLING     : f_evt_get_tagclose(s_evt_channel)             */
02903 /*--------------------------------------------------------------------*/
02904 /*                                                                    */
02905 /*+ PURPOSE     : Close tag and lmd file, cleanup s_evt_channel                   */
02906 /*+ ARGUMENTS   :                                                     */
02907 /*+  channel    : s_evt_channel* , must be allocated. */
02908 /*+ Return type : INTS4                                              */
02909 /*+ Declaration :                                                     */
02910 /*                INTS4 f_evt_get_tagclose(s_evt_channel *); */
02911 /*+ FUNCTION    : Create a tag file from lmd file       */
02912 /*1- C Main ****************+******************************************/
02913 INTS4 f_evt_get_tagclose(s_evt_channel *ps_chan)
02914 {
02915   /*=============================================*/
02916   if(ps_chan->ps_taghe == NULL)
02917   {
02918     f_evt_get_close(ps_chan);
02919     return(GETEVT__SUCCESS);
02920   }
02921   /*=============================================*/
02922   free(ps_chan->ps_taghe);
02923   if(ps_chan->ps_tag     != NULL) free(ps_chan->ps_tag);
02924   if(ps_chan->pc_evt_buf != NULL) free(ps_chan->pc_evt_buf);
02925   close(ps_chan->l_tagfile_no);
02926   close(ps_chan->l_channel_no);
02927   memset((void *)ps_chan, 0, sizeof(s_evt_channel));            /* clear memory                 */
02928   return(GETEVT__SUCCESS);
02929 }
02930  s_evt_channel *f_evt_control(){
02931    s_evt_channel *x;
02932    x=(s_evt_channel *)malloc(sizeof(s_evt_channel));
02933    memset(x,0,sizeof(s_evt_channel));
02934    return(x);
02935  }
02936 
02937 //----------------------------END OF GO4 SOURCE FILE ---------------------

Generated on Fri Nov 28 12:59:32 2008 for Go4-v3.04-1 by  doxygen 1.4.2