Ejemplo n.º 1
0
int limeReaderReadData(void *dest, n_uint64_t *nbytes, LimeReader *r)
{
  n_uint64_t bytes_to_read;
  n_uint64_t bytes_read;
  char myname[] = "limeReaderReadData";

  /* Check if we are at the end of the record */
  if( r->rec_ptr == r->bytes_total ) {
    return LIME_EOR;
  }

  /* If we are not at the end then read how much we have to */
  if( *nbytes > 0 ) { 
    if( *nbytes + r->rec_ptr > r->bytes_total ) {
      bytes_to_read = r->bytes_total - r->rec_ptr;
    }
    else { 
      bytes_to_read = *nbytes;
    }

    if((size_t)bytes_to_read != bytes_to_read){
      printf("%s Can't read %llu bytes\n",
	     myname,(unsigned long long)bytes_to_read);
      return LIME_ERR_READ;
    }

    /* Actually read */
    bytes_read = DCAP(fread)(dest, sizeof(unsigned char), 
		       (size_t)bytes_to_read, r->fp);
    *nbytes = bytes_read;
    
    if( bytes_read != bytes_to_read ){
      printf("%s tried to read %llu bytes but got %llu bytes\n",
	     myname,
	     (unsigned long long)bytes_to_read,
	     (unsigned long long)bytes_read);
      if(feof(r->fp))
	printf("Unexpected EOF encountered\n");
      return LIME_ERR_READ;
    }

    r->bytes_left -= bytes_read;
    r->rec_ptr += bytes_read;
  }

  return LIME_SUCCESS;
}
Ejemplo n.º 2
0
/* Entrance assumption to this function is that:
   i) The stream pointer is pointing to the beginning of 
      a record header.

   ii) There is a header about to come 
*/
int readAndParseHeader(LimeReader *r)
{

  unsigned int i_version;
  int i_MB, i_ME;
  n_uint32_t i_magic_no;
  n_uint64_t  i_data_length;
  unsigned char *typebuf;
  int status;
  //int msg_begin = 1;
  char myname[] = "lime::readAndParseHeader";

  /* Destroy any old header structure kicking about */
  if( r->curr_header != (LimeRecordHeader *)NULL ) { 
    //msg_begin = r->is_last;   /* Remember whether we finished a message */
    limeDestroyHeader(r->curr_header);
    r->curr_header = (LimeRecordHeader *)NULL;
  }

  /* Read the entire header */

  status = DCAP(fread)((void *)lime_header.int64, 
		 sizeof(n_uint64_t), MAX_HDR64, r->fp);
  if( status != MAX_HDR64 ) {
    if( DCAP(feof)( r->fp ) ) return LIME_EOF;
    fprintf(stderr,"%s read %d but wanted %d\n",myname,status,MAX_HDR64);
    return LIME_ERR_READ;
  }

  /* Check magic number */

  i_magic_no = big_endian_long(*lime_hdr_magic_no);
  if(i_magic_no != LIME_MAGIC_NO){
    fprintf(stderr,"%s: wrong magic number: read %x but wanted %x\n",
	    myname,i_magic_no,LIME_MAGIC_NO);
    return LIME_ERR_READ;
  }

#ifdef LIME_DEBUG
  fprintf(stderr, "%s Magic number OK: %d\n ", myname, i_magic_no);
  fflush(stderr);
#endif

  /* LIME version number */

  i_version = big_endian_short(*lime_hdr_version);

#ifdef LIME_DEBUG
  fprintf(stderr, "%s Input Version: %d\n ", myname,(int)i_version);
  fflush(stderr);
#endif

  /* MB flag */

  if ( *lime_hdr_mbme & MB_MASK ) { 
    i_MB = 1; 
  } 
  else {
    i_MB = 0;
  }

#ifdef LIME_DEBUG
  fprintf(stderr, "%s MB Flag: %d\n ", myname, (int)i_MB);
  fflush(stderr);
#endif
  
  /* ME Flag */

  if( *lime_hdr_mbme & ME_MASK ) {
    i_ME = 1;
  }
  else { 
    i_ME = 0;
  }

#ifdef LIME_DEBUG
  fprintf(stderr, "%s ME Flag: %d\n ", myname, (int)i_ME);
  fflush(stderr);
#endif

  /* Data length */

  i_data_length = big_endian_long_long(*lime_hdr_data_len);

#ifdef LIME_DEBUG
  fprintf(stderr, "%s Data Length: %llu\n ", myname, (unsigned long long)i_data_length);
  fflush(stderr);  
#endif

  /* Record type. */
  typebuf = (unsigned char *)lime_hdr_rec_type;

  /* Sanity Checking */
  /* Check Version */
  if( i_version != LIME_VERSION ) { 
    fprintf(stderr, "%s Unknown Lime Version\n",myname);
    exit(EXIT_FAILURE);
  }

#ifdef LIME_DEBUG
  printf("%s: type %s\n",myname,typebuf);
#endif

  /* If we are the first packet we MUST have MB flag set */
  /**  suppressed to allow seeking withing file without penalty
  if( msg_begin != i_MB ) { 
    fprintf(stderr, "%s MB Flag incorrect: last ME = %d but current MB=%d \n",
	    myname, msg_begin, i_MB);
    exit(EXIT_FAILURE);
  }
  **/

  r->curr_header = limeCreateHeader(i_MB, i_ME,
				    (char*)typebuf,
				    i_data_length);


  if (r->curr_header == (LimeRecordHeader *)NULL ) { 
    fprintf(stderr, "%s ERROR; Couldn't create header\n",myname);
    exit(EXIT_FAILURE);
  }

  return LIME_SUCCESS;
}
Ejemplo n.º 3
0
int read_qdppp_scidac_binary_data(char *buff_out, long int buff_size, char *fin)
{

  char buf[MAXBUF];  /* auxilary buffer for using a buffer copy. Bytes first copied to buf then from buf to buff_out*/
  LimeReader *reader;
  int status;
  n_uint64_t nbytes, bytes_left, bytes_to_copy, read_bytes;
  int rec_seek,msg_seek;
  int rec, msg;
  char *lime_type;
  size_t bytes_pad;
  int MB_flag, ME_flag;

  int i;
  /* Open file */
  FILE *fp = fopen(fin, "r");
  if(fp == NULL) { 
    fprintf(stderr,"Unable to open file %s for reading\n", fin);
    return EXIT_FAILURE;
  }



  /* Open LIME reader */
  reader = limeCreateReader(fp);
  if( reader == (LimeReader *)NULL ) { 
    fprintf(stderr, "Unable to open LimeReader\n");
    return EXIT_FAILURE;
  }


  /* Loop over records */
  msg=0;
  rec=0; 
  while( (status = limeReaderNextRecord(reader)) != LIME_EOF ){
    
    if( status != LIME_SUCCESS ) { 
      fprintf(stderr, "limeReaderNextRecord returned status = %d\n", 
	      status);
      return EXIT_FAILURE;
    }

    nbytes    = limeReaderBytes(reader);
    lime_type = limeReaderType(reader);
    bytes_pad = limeReaderPadBytes(reader);
    MB_flag   = limeReaderMBFlag(reader);
    ME_flag   = limeReaderMEFlag(reader);

    /* Update message and record numbers */
    if(MB_flag == 1){
      msg++;
      rec = 0;
    }

    rec++;

#if 1
    printf("\n\n");
    printf("Type:           %s\n",   lime_type);
    printf("Data Length:    %ld\n",  nbytes);
    printf("Padding Length: %zd\n",   bytes_pad);
    printf("MB flag:        %d\n",   MB_flag);
    printf("ME flag:        %d\n",   ME_flag);
#endif


    /* Skip to next record until target record is reached */
    /*if (msg != msg_seek || rec != rec_seek) continue;*/
    if (strcmp(lime_type,"scidac-binary-data") != 0) continue;
    
    /*check that the data record has the expected number of byutes*/
    if(nbytes != buff_size)
    {
      fprintf(stderr,"Error, expecting %ld bytes but data has %ld bytes\n",buff_size,nbytes);
      return EXIT_FAILURE;
    }


    /* Buffered copy */
    
    bytes_left = nbytes;
    int bcopied=0;
    while(bytes_left > (n_uint64_t)0){
      bytes_to_copy = mino((n_uint64_t)MAXBUF,bytes_left);
      read_bytes = bytes_to_copy;
      status = limeReaderReadData((void *)buf, &read_bytes, reader);
    
      if( status < 0 && status != LIME_EOR ) { 
	fprintf(stderr, "LIME read error occurred: status= %d\n", status);
	exit(EXIT_FAILURE);
      }
      if (read_bytes != bytes_to_copy) {
	fprintf(stderr, "Read error %lld bytes wanted,%lld read\n", 
		(unsigned long long)nbytes, (unsigned long long)read_bytes);
	return EXIT_FAILURE;
      }
    
      /* copy to the output buffer */
      for(i=0; i<read_bytes; i++)
        buff_out[bcopied+i]=buf[i];

      bcopied += read_bytes;
      bytes_left -= bytes_to_copy;
    }

    /* Quit at this record */
    break;
  }

  limeDestroyReader(reader);
  DCAP(fclose)(fp);

  return EXIT_SUCCESS;
}   
Ejemplo n.º 4
0
int main(int argc, char *argv[]) 
{
  char* data_buf;
  
  LimeReader *reader;
  FILE *fp;
  int status;
  n_uint64_t nbytes, read_bytes;
  int msg,rec,first;
  char *lime_type;
  size_t bytes_pad;
  int MB_flag, ME_flag;
  
  if( argc != 2 ) { 
    fprintf(stderr, "Usage: %s <lime_file>\n", argv[0]);
    return EXIT_FAILURE;
  }
  

  fp = DCAPL(fopen)(argv[1], "r");
  if(fp == (FILE *)NULL) { 
    fprintf(stderr,"Unable to open file %s for reading\n", argv[1]);
    return EXIT_FAILURE;
  }

  reader = limeCreateReader(fp);
  if( reader == (LimeReader *)NULL ) { 
    fprintf(stderr, "Unable to open LimeReader\n");
    return EXIT_FAILURE;
  }

  msg = 0; first = 1; rec = 0;
  while( (status = limeReaderNextRecord(reader)) != LIME_EOF ){
    
    if( status != LIME_SUCCESS ) { 
      fprintf(stderr, "limeReaderNextRecord returned status = %d\n", 
	      status);
      return EXIT_FAILURE;
    }

    nbytes    = limeReaderBytes(reader);
    lime_type = limeReaderType(reader);
    bytes_pad = limeReaderPadBytes(reader);
    MB_flag   = limeReaderMBFlag(reader);
    ME_flag   = limeReaderMEFlag(reader);
    
    if (MB_flag == 1 || first)
      {
	first = 0;
	rec = 0;
	msg++;
      }

    rec++;

    printf("\n\n");
    printf("Message:        %d\n", msg);
    printf("Record:         %d\n", rec);
    printf("Type:           %s\n", lime_type);
    printf("Data Length:    %llu\n", (unsigned long long)nbytes);
    printf("Padding Length: %lu\n", (unsigned long)bytes_pad);
    printf("MB flag:        %d\n", MB_flag);
    printf("ME flag:        %d\n", ME_flag);
    
    /* TO DO: Buffer the input */
    if(nbytes < MAX_BYTES){
      data_buf = (char *)malloc((size_t)nbytes+1);
      if( data_buf == (char *)NULL) { 
	fprintf(stderr, "Couldn't malloc data buf\n");
	return EXIT_FAILURE;
      }
      
      read_bytes = nbytes;
      status = limeReaderReadData((void *)data_buf, &read_bytes, reader);
      
      if( status < 0 ) { 
	if( status != LIME_EOR ) { 
	  fprintf(stderr, "LIME read error occurred: status= %d  %llu bytes wanted, %llu read\n", 
		  status, (unsigned long long)nbytes, 
		  (unsigned long long)read_bytes);
	  return EXIT_FAILURE;
	}
      }
      
      data_buf[nbytes]='\0';
      if(!all_ascii(data_buf, nbytes))
	printf("Data:           [Binary data]\n");
      else
	printf("Data:           \"%s\" \n", data_buf);
      
      free(data_buf);
    }
    else{
	printf("Data:           [Long record skipped]\n");
    }

  }

  limeDestroyReader(reader);
  DCAP(fclose)(fp);

  return EXIT_SUCCESS;

}