Beispiel #1
0
/* Fill the dev_id and blk_1000 structures from the given record (lptr)
   Returns 1 if 1000 blockette was found, 0 if not
*/
int parse_record( t_dev_id *dev_id,
		  t_blk_1000 *blk_1000,
		  char *lptr, int rec_size)
{
  
  static struct s_fsdh_data *fsdh_data = NULL;
  static struct s_blk_head *blk_head = NULL;

  int found_1000b = 0;            /* found the 1000 blockette? */
  char swap_flag = 0;             /* is swapping needed? */
  unsigned short begin_blockette; /* byte offset for next blockette */

  /* Make sure there is room for these structs */
  fsdh_data = (struct s_fsdh_data *) realloc(fsdh_data, sizeof(struct s_fsdh_data));
  blk_head  = (struct s_blk_head *) realloc(blk_head, sizeof(struct s_blk_head));

  /* copy fixed section data header data into memory */
  memcpy((void *)dev_id,lptr+8,sizeof(struct s_dev_id));
  memcpy((void *)fsdh_data,lptr+20,sizeof(struct s_fsdh_data));

  /* check to see if word swapping is needed (bogus year makes good test) */
  if ( (fsdh_data->start_time.year < 1960) || 
       (fsdh_data->start_time.year > 2050) )
    swap_flag = 1;

  /* change word order?  Most are unused, but leave it for now */
  swap_2bytes(&fsdh_data->start_time.year,swap_flag);
  swap_2bytes(&fsdh_data->start_time.day,swap_flag);
  swap_2bytes(&fsdh_data->start_time.fracts,swap_flag);
  swap_2bytes(&fsdh_data->num_samples,swap_flag);
  swap_2bytes((uint16_t *)&fsdh_data->sample_rate,swap_flag);
  swap_2bytes((uint16_t *)&fsdh_data->multiplier,swap_flag);
  swap_4bytes((uint32_t *)&fsdh_data->time_correct,swap_flag);
  swap_2bytes(&fsdh_data->begin_data,swap_flag);
  swap_2bytes(&fsdh_data->begin_blockette,swap_flag);

  begin_blockette = fsdh_data->begin_blockette;

  /* loop through blockettes as long as number is non-zero and viable */
  while ((begin_blockette != 0) &&
	 (begin_blockette <= (uint16_t) rec_size)) {

    memcpy((void *)blk_head,lptr+begin_blockette,sizeof(struct s_blk_head));
    swap_2bytes(&blk_head->blk_type,swap_flag);
    swap_2bytes(&blk_head->next_blk,swap_flag);

    if (blk_head->blk_type == 1000) { /* found the 1000 blockette */

      memcpy((void *)blk_1000,lptr+begin_blockette,sizeof(struct s_blk_1000));
      found_1000b = 1;
    }

    if (begin_blockette != 0)  /* check not useful now, but for the future */ 
      begin_blockette = blk_head->next_blk;
  }

  return found_1000b;
}
/***************************************************************************
 * parse_record:
 *
 * Parse and populate the fsdh and blk_1000 structures from the given
 * record (recptr).
 *
 * Returns 1 if 1000 blockette was found, 0 if not.
 ***************************************************************************/
int
parse_record( char *recptr, int reclen,
	      t_fsdh *fsdh, t_blk_1000 *blk_1000)
{
  
  static t_blk_head *blk_head = NULL;
  
  int found_1000b = 0;            /* found the 1000 blockette? */
  char swap_flag = 0;             /* is swapping needed? */
  unsigned short begin_blockette; /* byte offset for next blockette */
  
  /* Make sure there is room for this struct */
  blk_head  = (t_blk_head *) realloc(blk_head, sizeof(t_blk_head));
  
  /* Copy fixed section data header */
  memcpy((void *)fsdh,recptr,sizeof(t_fsdh));
  
  /* check to see if word swapping is needed (bogus year makes good test) */
  if ( (fsdh->start_time.year < 1960) || 
       (fsdh->start_time.year > 2050) )
    swap_flag = 1;
  
  /* Swap bytes in FSDH */
  if ( swap_flag )
    {
      swap_2bytes(&fsdh->start_time.year);
      swap_2bytes(&fsdh->start_time.day);
      swap_2bytes(&fsdh->start_time.fracts);
      swap_2bytes(&fsdh->num_samples);
      swap_2bytes((unsigned short *)&fsdh->sample_rate);
      swap_2bytes((unsigned short *)&fsdh->multiplier);
      swap_4bytes((unsigned int *)&fsdh->time_correct);
      swap_2bytes(&fsdh->begin_data);
      swap_2bytes(&fsdh->begin_blockette);
    }
  
  begin_blockette = fsdh->begin_blockette;
  
  /* loop through blockettes as long as number is non-zero and viable */
  while ((begin_blockette != 0) &&
	 (begin_blockette <= (unsigned short) reclen)) {
    
    memcpy((void *)blk_head,recptr+begin_blockette,sizeof(t_blk_head));
    if ( swap_flag )
      {
	swap_2bytes(&blk_head->blk_type);
	swap_2bytes(&blk_head->next_blk);
      }
    
    if (blk_head->blk_type == 1000) /* found the 1000 blockette */
      {
	memcpy((void *)blk_1000,recptr+begin_blockette,sizeof(t_blk_1000));
	found_1000b = 1;
      }
    
    if (begin_blockette != 0)  /* check not useful now, but for the future */ 
      begin_blockette = blk_head->next_blk;
  }
  
  return found_1000b;
}  /* End of parse_record() */