Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/MbsAPI/f_evt.c

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

Generated on Tue Nov 8 10:56:08 2005 for Go4-v2.10-5 by doxygen1.2.15