FreeUSP: Algorithm Implementation and Testing

C Trace Template


/* -----------------  Main Routine -----------------------

     copyright 2001-6, Amoco Production Company 
              All rights reserved
        an affiliate of BP America Inc.

 -----------------  ------------ -----------------------

     Program Description:

     Quick C template as an example of how a USP program in C may
     be written using the arg handlers and USP I/O package.

     Author[s]:  J.Wade, P.Garossino

*/

#include <stdio.h>
#include <save_defs.h>
#include <cu_defs.h>
#include <ut_defs.h>
#include <io_defs.h>
#include <size_defs.h>

int main(argc,argv)
int argc;
char **argv;

{
int seqrec,seqtrc,rec_start,rec_end,trc_start,trc_end;
int help;
int luin,luout;
int nbytes,nbytes2,nrecs,ntrcs,nsmps;
int recnum_fmt,recnum_indx,recnum_lng;
int trcnum_fmt,trcnum_indx,trcnum_lng;
char ntap[512],otap[512];
char *buffer,*date;
short recnum,trcnum;

    help = C_ARGIS("-h",&argc,argv);
    help += C_ARGIS("-?",&argc,argv);
    help += C_ARGIS("-help",&argc,argv);

        if (help != 0) {
            fprintf(stderr,"c_prgm is template to demonstrate");
            fprintf(stderr,"how to code a USP program in C\n\n");
            fprintf(stderr,"Usage: c_prgm -N [ntap] -O [otap]");
            fprintf(stderr," -rs [starting_record] -re [ending_record]\n");
            fprintf(stderr," -ns [starting_trace] -ne [ending_trace]\n");
            exit(0);
        }

	save_cmd_args(argc,argv);

/*
   Fetch the name of the input/output files from the command line. If not 
   specified, stdin and/or stdout are used.
*/

	C_ARGSTR("-N",ntap," "," ",&argc,argv);
	if (strcmp(ntap," ") != 0) 
	  C_LBOPEN(&luin,ntap,"r");
	else
	  luin = 0;

	C_ARGSTR("-O",otap," "," ",&argc,argv);
	if (strcmp(otap," ") != 0) 
	  C_LBOPEN(&luout,otap,"w");
	else
	  luout = 1;


/*
   Grab a buffer for the line header and then reallocate it down to 
   the size of a trace after we get the info from the line header.
*/

	buffer = (char *)malloc(12000*sizeof(char));
	nbytes = 0;
	C_RTAPE(luin,buffer,&nbytes);
	get_hw_val(buffer,"NumRec",LINEHEADER,&nrecs);
	get_hw_val(buffer,"NumTrc",LINEHEADER,&ntrcs);
	get_hw_val(buffer,"NumSmp",LINEHEADER,&nsmps);
	fprintf(stderr,"recs = %d, trcs = %d, smps = %d\n",nrecs,ntrcs,nsmps);

/*
   Read in the value for the starting and ending record and trace values.
*/ 

	C_ARGI4("-rs",&rec_start,1,1,&argc,argv);
	C_ARGI4("-re",&rec_end,nrecs,nrecs,&argc,argv);
	fprintf(stderr,"starting record = %d\nending record = %d\n",
		rec_start,rec_end);

	C_ARGI4("-ns",&trc_start,1,1,&argc,argv);
	C_ARGI4("-ne",&trc_end,ntrcs,ntrcs,&argc,argv);
	fprintf(stderr,"starting trace = %d\nending trace = %d\n",
		trc_start,trc_end);

/*
   Update for date and current command line then write output line header
*/ 


	date = (char *) cgdate();
	fprintf(stderr,"date = %s\n",date);
	put_hw_val(buffer,"PrcDat",LINEHEADER,date);
	save_hlh(buffer,nbytes,&nbytes2);
	C_WRTAPE(luout,buffer,nbytes2);

/*
   reallocate buffer to deal with incoming and outgoing traces
*/ 

	fprintf(stderr,"size of trace buffer = %d\n",SZTRHD+(nsmps*SZSMPD));
	buffer = (char *)realloc(buffer,SZTRHD+(nsmps*SZSMPD));

/*
   assign formats, lenghts and pointers associated with trace header entries
   to be used later
*/ 

	C_SAVELU("RecNum",&recnum_fmt,&recnum_indx,&recnum_lng,TRACEHEADER);
	C_SAVELU("TrcNum",&trcnum_fmt,&trcnum_indx,&trcnum_lng,TRACEHEADER);

/*
   skip down to user defined start record
*/ 

	seqrec = rec_start-1;
	skipt(luin,seqrec*ntrcs);


	do {


/*
   skip down to user defined start trace
*/ 
	  seqtrc = trc_start-1;
	  skipt(luin,seqtrc);

/*
   loop over the selected input traces ( or until we hit nbytes = 0 )
*/ 

	  do {
	    nbytes = 0;
	    C_RTAPE(luin,buffer,&nbytes);
	    if (nbytes == 0) break;
	    get_indexed_hw_val(buffer,recnum_fmt,recnum_indx,recnum_lng,
		TRACEHEADER,&recnum);
	    get_indexed_hw_val(buffer,trcnum_fmt,trcnum_indx,trcnum_lng,
		TRACEHEADER,&trcnum);
	    seqtrc++;
	    fprintf(stderr,"rec  = %d, trc = %d\n",recnum,trcnum);
	    C_WRTAPE(luout,buffer,nbytes);

/*
   end of trace loop
*/ 

	    } while ((seqtrc < trc_end) && (nbytes != 0));

/*
   skip over remaining unwanted traces in a record
*/ 

	    skipt(luin,ntrcs-trc_end);
	    seqrec++;

/*
   end of record loop
*/ 

	  } while ((seqrec < rec_end) && (nbytes != 0));


/*
   close input and output streams and exit routine
*/ 

	C_LBCLOS(luin);
	C_LBCLOS(luout);

	exit(0);
}