Пример #1
0
/*
 * Write a sequence to a FILE *fp of format "format". If "format" is 0,
 * we choose our favourite - SCF.
 *
 * Returns:
 *   0 for success
 *  -1 for failure
 */
int mfwrite_reading(mFILE *fp, Read *read, int format) {
    int r = -1;
    int no_compress = 0;

#ifdef _WIN32
    /*
     * jkb 09/06/00 comment below
     *
     * On windows "prog > file.scf" will work wrongly (compared to
     * "prog file.scf") because windows is rather stupid. It treats ascii
     * and binary streams differently, it considers stdout to be ascii unless
     * told otherwise, and it can only be told otherwise by using non-ansi
     * windows-specific function calls.
     */
    if (format != TT_EXP && format != TT_PLN && fp->fp)
	_setmode(_fileno(fp->fp), _O_BINARY);
#endif

    switch (format) {
    default:
	/* Defaults to ZTR type */

#ifdef IOLIB_ZTR
    case TT_ZTR:
    case TT_ZTR2: {
        ztr_t *ztr;
	ztr = read2ztr(read);
	compress_ztr(ztr, 2);
	r = mfwrite_ztr(fp, ztr); 
	delete_ztr(ztr);
	no_compress = 1;
	break;
    }
    case TT_ZTR1: {
        ztr_t *ztr;
	ztr = read2ztr(read);
	compress_ztr(ztr, 1);
	r = mfwrite_ztr(fp, ztr); 
	delete_ztr(ztr);
	break;
    }
    case TT_ZTR3: {
        ztr_t *ztr;
	ztr = read2ztr(read);
	compress_ztr(ztr, 3);
	r = mfwrite_ztr(fp, ztr); 
	delete_ztr(ztr);
	no_compress = 1;
	break;
    }
#endif

#ifdef IOLIB_SCF
    case TT_SCF: {
        Scf *scf;
	scf = read2scf(read);
	r = mfwrite_scf(scf, fp);
	scf_deallocate(scf);
	break;
    }
#endif

#ifdef IOLIB_CTF
    case TT_CTF:
	r = mfwrite_ctf(fp, read); 
	break;
#endif

#ifdef IOLIB_ABI
    case TT_ABI:
	/*return mfwrite_abi(fp, read); */
	break;
#endif

#ifdef IOLIB_ALF
    case TT_ALF:
	/* return mfwrite_alf(fp, read); */
	break;
#endif

#ifdef IOLIB_EXP
    case TT_EXP: {
	Exp_info *e = read2exp(read, read->ident ? read->ident : "unknown");
	
	if (NULL == e) {
	    fprintf(stderr, "Failed to create experiment file.\n");
	    r = -1;
	} else {
	    exp_print_mfile(fp, e);
	    exp_destroy_info(e);
	    r = 0;
	}
	break;
    }
#endif

#ifdef IOLIB_PLN
    case TT_PLN:
	r = mfwrite_pln(fp, read);
	break;
#endif
    }

    mftruncate(fp, -1);
    if (r == 0 && !no_compress) {
	fcompress_file(fp);
    }
    mfflush(fp);

    return r;
}
Пример #2
0
int main(void)
{
  MEMFILE *file, *file2, *file3;
  char test[] = "This is a very long string that just goes on and on, but it can surely tell us a few things about how this code is working.";
  char test2[] = "This is the second string, and should get cat'd right up behind the first string.";
  char buffer[512];

  printf("\n--------- open should fail (file doesn't exist)\n");
  file = mfopen( "dummy", "r+b" );
  printf("fopen(\"dummy\",\"r+b\") -> 0x%x\n", file );
  printf("#of open memstreams: %d\n", count_open_streams() );

  printf("\n--------- open should succeed (truncated on open)\n");
  file = mfopen( "dummy", "w+b" );
  printf("fopen(\"dummy\",\"w+b\") -> 0x%x\n", file );
  printf("#of open memstreams: %d\n", count_open_streams() );
  printf("growth increment was %d\n", _mfsetgrowincrement( file, 64 ) );
  printf("growth increment is now %d\n", _mfsetgrowincrement( file, 0 ) );

  printf("\n--------- should be zero offset, and eof == false\n");
  printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) );

  printf("\n--------- should write %d bytes\n", strlen(test));
  printf( "fwrite( [], %d, %d, file )  -> %d\n", strlen(test), sizeof(char),
          mfwrite( test, strlen(test), sizeof(char), file ) );
  printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) );

  printf("\n--------- should read the string back in\n");
  printf( "rewind(file) \n" ); mrewind(file);
  printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) );
  memset( buffer, 0, sizeof( buffer ));
  printf( "fread( [], %d, %d, file )  -> %d\n", sizeof(buffer), sizeof(char),
          mfread( buffer, sizeof(buffer), sizeof(char), file ) );
  printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) );

  printf("\n--------- should cat the second string to the first\n");
  printf( "fwrite( [], %d, %d, file )  -> %d\n", sizeof(char), strlen(test2),
          mfwrite( test2, strlen(test2), sizeof(char), file ) );
  printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) );

  printf("\n--------- should read zero (fp is at eof)\n");
  memset( buffer, 0, sizeof( buffer ));
  printf( "fread( [], %d, %d, file )  -> %d\n", sizeof(buffer), sizeof(char),
          mfread( buffer, sizeof(buffer), sizeof(char), file ) );
  printf( "ftell(file) -> %ld  feof(file)-> %d\n", mftell( file ), mfeof( file ) );

  printf("\n--------- seek test\n");
  printf( "filelength( fileno( file ) )-> %ld\n", mfilelength( mfileno( file ) ) );
  printf( "seek(file,64,0)-> %d\n", mfseek( file, 64, SEEK_SET ) );
  printf( "ftell(file) -> %ld  feof(file)-> %d\n", mftell( file ), mfeof( file ) );

  printf("\n--------- read after seek test, should read filelen-64 bytes\n");
  memset( buffer, 0, sizeof( buffer ));
  printf( "fread( [], %d, %d, file )  -> %d\n", sizeof(buffer), sizeof(char),
          mfread( buffer, sizeof(buffer), sizeof(char), file ) );
  printf( "-> \"%s\"\n", buffer );

  printf("\n--------- change size test\n");
  printf( "filelength( fileno( file ) )-> %ld\n", mfilelength( mfileno( file ) ) );
  printf( "ftell(file) -> %ld  feof(file)-> %d\n", mftell( file ), mfeof( file ) );
  printf( "ftruncate(fileno(file),10)   ->  %d\n", mftruncate( mfileno( file ), 10 ) );
  printf( "filelength( fileno( file ) )-> %ld\n", mfilelength( mfileno( file ) ) );
  printf( "ftell(file) -> %ld  feof(file)-> %d\n", mftell( file ), mfeof( file ) );

  printf("\n--------- negative seek test\n");
  printf( "fseek(file,-5,2)->%d\n", mfseek( file, -5, SEEK_END ) );
  printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) );

  printf("\n--------- read after seek test, should read 5 bytes\n");
  memset( buffer, 0, sizeof( buffer ));
  printf( "fread( [], %d, %d, file )  -> %d\n", sizeof(buffer), sizeof(char),
          mfread( buffer, sizeof(buffer), sizeof(char), file ) );
  printf( "-> \"%s\"\n", buffer );

  printf("\n--------- open and write a second file\n");
  file2 = mfopen( "dummy2", "w+b" );
  printf("fopen(\"dummy2\",\"w+b\") -> 0x%x\n", file2 );
  printf( "fwrite( [], %d, %d, file2 )  -> %d\n", strlen("TEST"), sizeof(char),
          mfwrite( "TEST", strlen("TEST"), sizeof(char), file2 ) );
  printf( "filelength( fileno( file2 ) )-> %ld\n", mfilelength( mfileno( file2 ) ) );
  printf( "ftell(file2) -> %ld feof(file2)-> %d\n", mftell( file2 ), mfeof( file2 ) );
  printf("#of open memstreams: %d\n", count_open_streams() );

  printf("\n--------- open and write a third file\n");
  file3 = mfopen( "dummy3", "w+b" );
  printf("fopen(\"dummy3\",\"w+b\") -> 0x%x\n", file3 );
  printf( "fwrite( [], %d, %d, file3 )  -> %d\n", strlen("BLAHBLAH"), sizeof(char),
          mfwrite( "BLAHBLAH", strlen("BLAHBLAH"), sizeof(char), file3 ) );
  printf( "filelength( fileno( file3 ) )-> %ld\n", mfilelength( mfileno( file3 ) ) );
  printf( "ftell(file3) -> %ld feof(file3)-> %d\n", mftell( file3 ), mfeof( file3 ) );
  printf("#of open memstreams: %d\n", count_open_streams() );

  printf("\n--------- close second\n");
  printf( "fclose(file2)    -> %d\n", mfclose( file2 ) );
  printf( "filelength( fileno( file2 ) )-> %ld\n", mfilelength( mfileno( file2 ) ) );
  printf( "ftell(file2) -> %ld feof(file2)-> %d\n", mftell( file2 ), mfeof( file2 ) );
  printf("#of open memstreams: %d\n", count_open_streams() );

  printf("\n--------- check first\n");
  printf( "filelength( fileno( file ) )-> %ld\n", mfilelength( mfileno( file ) ) );
  printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) );
  printf("#of open memstreams: %d\n", count_open_streams() );

  printf("\n--------- truncate to zero test\n");
  printf( "ftruncate(fileno(file),0)   ->  %d\n", mftruncate( mfileno( file ), 0 ) );
  printf( "filelength( fileno( file ) )-> %ld\n", mfilelength( mfileno( file ) ) );
  printf("#of open memstreams: %d\n", count_open_streams() );

  printf("\n--------- close first\n");
  printf( "fclose(file)    -> %d\n", mfclose( file ) );
  printf( "filelength( fileno( file ) )-> %ld\n", mfilelength( mfileno( file ) ) );
  printf( "ftell(file) -> %ld feof(file)-> %d\n", mftell( file ), mfeof( file ) );
  printf("#of open memstreams: %d\n", count_open_streams() );

  printf("\n--------- close third\n");
  printf( "fclose(file3)    -> %d\n", mfclose( file3 ) );
  printf( "filelength( fileno( file3 ) )-> %ld\n", mfilelength( mfileno( file3 ) ) );
  printf( "ftell(file3) -> %ld feof(file3)-> %d\n", mftell( file3 ), mfeof( file3 ) );
  printf("#of open memstreams: %d\n", count_open_streams() );

  return 0;
}
Пример #3
0
int check_trace_body(Settings *opts, Trace_reader *trc, srf_t *srf) {
  char         name[512];
  Spot_info    spot = { NULL, 0, 0, 0, 0, 0, 0 };
  ztr_t       *ztr = NULL;
  ztr_chunk_t *chunk;
  int          start_chunk = 0;
  int          i;

  if (-1 == construct_trace_name(srf->th.id_prefix,
				 (unsigned char *)srf->tb.read_id,
				 srf->tb.read_id_length,
				 name, sizeof(name))) {
    printf("Couldn't construct read name\n");
    return -1;
  }

  if (0 != decode_name(name, &spot)) {
    printf("Couldn't decode read name.\n");
    return -1;
  }

  if (opts->verbosity > 0) {
    printf("%s\n", name);
  }

  if (0 != read_trace_data(opts, trc, &spot)) {
    printf("Couldn't get trace data for %s\n", name);
    return -1;
  }

  mfseek(srf->mf, srf->mf_end, SEEK_SET);
  if (srf->tb.trace_size) {
    mfwrite(srf->tb.trace, 1, srf->tb.trace_size, srf->mf);
    free(srf->tb.trace);
    srf->tb.trace = NULL;
  }
  mftruncate(srf->mf, mftell(srf->mf));
  mfseek(srf->mf, srf->mf_pos, SEEK_SET);
  
  if (srf->ztr) {
    start_chunk = srf->ztr->nchunks;
    ztr = ztr_dup(srf->ztr);
  }

  if (NULL == partial_decode_ztr(srf, srf->mf, ztr)) {
    printf("Couldn't decode ZTR data for %s\n", name);
    return -1;
  }

  for (i = start_chunk; i < ztr->nchunks; i++) {
    chunk = &ztr->chunk[i];
    if (opts->verbosity > 1) {
      printf(" Chunk %d type %.4s\n", i,
	      (char *) &chunk->type);
    }
    switch (chunk->type) {
    case ZTR_TYPE_BASE:
      if (0 != uncompress_chunk(ztr, chunk)) {
	printf("Couldn't uncompress BASE chunk\n");
	return -1;
      }
      if (trc->seq_len + 1 != chunk->dlength
	  || 0 != memcmp(trc->seq, chunk->data + 1, trc->seq_len)) {
	printf("Sequence differs for %s\n", name);
	return -1;
      }
      break;
    case ZTR_TYPE_CNF1:
      if (0 != uncompress_chunk(ztr, chunk)) {
	printf("Couldn't uncompress CNF1 chunk\n");
	return -1;
      }
      if (trc->seq_len + 1 != chunk->dlength
	  || 0 != memcmp(trc->qual, chunk->data + 1, trc->seq_len)) {
	printf("CNF1 confidence values differ for %s\n", name);
	return -1;
      }
      break;
    case ZTR_TYPE_CNF4:
      if (0 != uncompress_chunk(ztr, chunk)) {
	printf("Couldn't uncompress CNF4 chunk\n");
	return -1;
      }
      if (0 != check_cnf4(ztr, chunk, trc)) {
	printf("CNF4 confidence values differ for %s\n", name);
	return -1;
      }
      break;
    case ZTR_TYPE_SMP4:
      if (0 != uncompress_chunk(ztr, chunk)) {
	printf("Couldn't uncompress SMP4 chunk\n");
	return -1;
      }
      if (0 != check_trace(ztr, chunk, trc)) {
	printf("Trace doesn't match for %s\n", name);
	return -1;
      }
      break;
    default:
      printf("Found unexpected chunk type in trace body for %s\n",
	      name);
      return -1;
    }
  }

  delete_ztr(ztr);

  return 0;
}
Пример #4
0
/*
 * Given the archive name and the level_mode
 * generate information about the archive
 *
 * Note the generated srf file is NOT indexed
 *
 * Returns 0 on success.
 */
int srf_info(char *input, int level_mode, long *read_count, long *chunk_count,
	     uint64_t *chunk_size, long key_count[NCHUNKS][NKEYS],
	     long type_count[NCHUNKS][NTYPES], HashTable *regn_hash,
	     uint64_t *base_count) {
    srf_t *srf;
    off_t pos;
    int type;
    int count = 0;
    long trace_body_count = 0;
    char name[1024];

    if (NULL == (srf = srf_open(input, "rb"))) {
	perror(input);
	return 1;
    }

    while ((type = srf_next_block_type(srf)) >= 0) {

      switch (type) {
	case SRFB_CONTAINER:
	    if( trace_body_count ){
		if( level_mode & LEVEL_NAME )
		    printf( " ... %s x%ld\n", name+strlen(srf->th.id_prefix), trace_body_count);
		trace_body_count = 0;
	    }
	    if (0 != srf_read_cont_hdr(srf, &srf->ch)) {
		fprintf(stderr, "Error reading container header.\nExiting.\n");
		exit(1);
	    }
	    break;

        case SRFB_XML:
	    if( trace_body_count ){
		if( level_mode & LEVEL_NAME )
		    printf( " ... %s x%ld\n", name+strlen(srf->th.id_prefix), trace_body_count);
		trace_body_count = 0;
	    }
	    if (0 != srf_read_xml(srf, &srf->xml)) {
		fprintf(stderr, "Error reading XML.\nExiting.\n");
		exit(1);
	    }
	    break;

	case SRFB_TRACE_HEADER:
	    if( trace_body_count ){
		if( level_mode & LEVEL_NAME )
		    printf( " ... %s x%ld\n", name+strlen(srf->th.id_prefix), trace_body_count);
		trace_body_count = 0;
	    }
	    if (0 != srf_read_trace_hdr(srf, &srf->th)) {
		fprintf(stderr, "Error reading trace header.\nExiting.\n");
		exit(1);
	    }

	    if( 0 == (level_mode & (LEVEL_CHUNK | LEVEL_BASE)) )
		break;

	    /* Decode ZTR chunks in the header */
	    if (srf->mf)
		mfdestroy(srf->mf);

	    srf->mf = mfcreate(NULL, 0);
	    if (srf->th.trace_hdr_size)
		mfwrite(srf->th.trace_hdr, 1, srf->th.trace_hdr_size, srf->mf);
	    if (srf->ztr)
		delete_ztr(srf->ztr);
	    mrewind(srf->mf);

	    if (NULL != (srf->ztr = partial_decode_ztr(srf, srf->mf, NULL))) {
		srf->mf_pos = mftell(srf->mf);
	    } else {
		/* Maybe not enough to decode or no headerBlob. */
		/* So delay until decoding the body. */
		srf->mf_pos = 0;
	    }
	    mfseek(srf->mf, 0, SEEK_END);
	    srf->mf_end = mftell(srf->mf);

	    break;

	case SRFB_TRACE_BODY: {
	    srf_trace_body_t old_tb;
	    ztr_t *ztr_tmp;
            int no_trace = (level_mode & (LEVEL_CHUNK | LEVEL_BASE) ? 0 : 1);

	    if (0 != srf_read_trace_body(srf, &old_tb, no_trace)) {
		fprintf(stderr, "Error reading trace body.\nExiting.\n");
		exit(1);
	    }

	    if (-1 == construct_trace_name(srf->th.id_prefix,
					   (unsigned char *)old_tb.read_id,
					   old_tb.read_id_length,
					   name, 512)) {
		fprintf(stderr, "Error constructing trace name.\nExiting.\n");
		exit(1);
	    }

	    trace_body_count++;
	    if( 1 == trace_body_count ){
		if( level_mode & LEVEL_NAME )
		    printf( "trace_name: %s + %s", srf->th.id_prefix, name+strlen(srf->th.id_prefix));
	    }
          
	    read_count[READ_TOTAL]++;

	    if (old_tb.flags & SRF_READ_FLAG_BAD_MASK ){
		read_count[READ_BAD]++;
	    } else {
		read_count[READ_GOOD]++;
	    }
          
	    if( 0 == (level_mode & (LEVEL_CHUNK | LEVEL_BASE)) )
		break;

	    if (!srf->mf) {
		fprintf(stderr, "Error reading trace body.\nExiting.\n");
		exit(1);
	    }

	    mfseek(srf->mf, srf->mf_end, SEEK_SET);
	    if (old_tb.trace_size) {
		mfwrite(old_tb.trace, 1, old_tb.trace_size, srf->mf);
		free(old_tb.trace);
		old_tb.trace = NULL;
	    }
          
	    mftruncate(srf->mf, mftell(srf->mf));
	    mfseek(srf->mf, srf->mf_pos, SEEK_SET);

	    if (srf->ztr)
		ztr_tmp = ztr_dup(srf->ztr); /* inefficient, but simple */
	    else
		ztr_tmp = NULL;

	    if ((ztr_tmp = partial_decode_ztr(srf, srf->mf, ztr_tmp))) {
		int i;
		for (i=0; i<ztr_tmp->nchunks; i++) {
		    int ichunk = -1;

		    switch (ztr_tmp->chunk[i].type) {
		    case ZTR_TYPE_BASE:
			ichunk = CHUNK_BASE;
			chunk_size[ichunk] += ztr_tmp->chunk[i].dlength;
			if( parse_base(ztr_tmp, &ztr_tmp->chunk[i], base_count) ){
			    delete_ztr(ztr_tmp);
			    return 1;
			}
			break;
		    case ZTR_TYPE_CNF1:
			ichunk = CHUNK_CNF1;
			chunk_size[ichunk] += ztr_tmp->chunk[i].dlength;
			break;
		    case ZTR_TYPE_CNF4:
			ichunk = CHUNK_CNF4;
			chunk_size[ichunk] += ztr_tmp->chunk[i].dlength;
			break;
		    case ZTR_TYPE_SAMP:
			ichunk = CHUNK_SAMP;
			chunk_size[ichunk] += ztr_tmp->chunk[i].dlength;
			break;
		    case ZTR_TYPE_SMP4:
			ichunk = CHUNK_SMP4;
			chunk_size[ichunk] += ztr_tmp->chunk[i].dlength;
			break;
		    case ZTR_TYPE_REGN:
			ichunk = CHUNK_REGN;
			chunk_size[ichunk] += ztr_tmp->chunk[i].dlength;
			if( NULL == parse_regn(ztr_tmp, &ztr_tmp->chunk[i], regn_hash) ){
			    delete_ztr(ztr_tmp);
			    return 1;
			}
			break;
		    default:
			break;
		    }

		    if( ichunk > -1 ) {
			chunk_count[ichunk]++;
			count_mdata_keys(ztr_tmp, &ztr_tmp->chunk[i], ichunk, key_count, type_count);
		    }
		}

	    }

	    if( ztr_tmp )
		delete_ztr(ztr_tmp);

	    count++;
	    if( (level_mode == LEVEL_CHECK) && (count == 10) ){
		printf( " ... %s x%ld\n", name+strlen(srf->th.id_prefix), trace_body_count);
		srf_destroy(srf, 1);
		return 0;
	    }
          
	    break;
        }

	case SRFB_INDEX: {
            off_t pos = ftell(srf->fp);
	    if( trace_body_count ){
		if( level_mode & LEVEL_NAME )
		    printf( " ... %s x%ld\n", name+strlen(srf->th.id_prefix), trace_body_count);
		trace_body_count = 0;
	    }
            printf( "Reading srf index block\n");
	    if (0 != srf_read_index_hdr(srf, &srf->hdr, 1)) {
		srf_destroy(srf, 1);
		fprintf(stderr, "Error reading srf index block header.\nExiting.\n");
		exit(1);
	    }
            /* Skip the index body */
	    fseeko(srf->fp, pos + srf->hdr.size, SEEK_SET);

            break;
        }
        
	case SRFB_NULL_INDEX: {
            uint64_t ilen;
	    if( trace_body_count ){
		if( level_mode & LEVEL_NAME )
		    printf( " ... %s x%ld\n", name+strlen(srf->th.id_prefix), trace_body_count);
		trace_body_count = 0;
	    }
            printf( "Reading srf null index block\n");
	    /*
	     * Maybe the last 8 bytes of a the file (or previously was
	     * last 8 bytes prior to concatenating SRF files together).
	     * If so it's the index length and should always be 8 zeros.
	     */
            if (1 != fread(&ilen, 8, 1, srf->fp)) {
		srf_destroy(srf, 1);
		fprintf(stderr, "Error reading srf null index block.\nExiting.\n");
		exit(1);
            }
            if (ilen != 0) {
		srf_destroy(srf, 1);
		fprintf(stderr, "Invalid srf null index block.\nExiting.\n");
		exit(1);
            }

            break;
        }
        
        default:
            srf_destroy(srf, 1);
	    fprintf(stderr, "Block of unknown type '%c'\nExiting.\n", type);
	    exit(1);
	}

    }

    if( trace_body_count ){
        if( level_mode & LEVEL_NAME )
            printf( " ... %s x%ld\n", name+strlen(srf->th.id_prefix), trace_body_count);
        trace_body_count = 0;
    }

    /* the type should be -1 (EOF) */
    if( type != -1 ) {
        fprintf(stderr, "Block of unknown type '%c'\nExiting.\n", type);
	exit(1);
    }

    /* are we really at the end of the srf file */
    pos = ftell(srf->fp);
    fseek(srf->fp, 0, SEEK_END);
    if( pos != ftell(srf->fp) ){
        fprintf(stderr, "srf file is corrupt\n");
	exit(1);
    }
    
    srf_destroy(srf, 1);
    return 0;
}