/* * Reads a read data block from an mFILE given a count of the number of * flows and basecalls (from the common header and read headers). * * Returns the a pointer to sff_read_data on success * NULL on failure */ sff_read_data *read_sff_read_data(mFILE *mf, int nflows, int nbases) { sff_read_data *d; int i; if (NULL == (d = (sff_read_data *)xcalloc(1, sizeof(*d)))) return NULL; if (NULL == (d->flowgram = (uint16_t *)xcalloc(nflows, 2))) return free_sff_read_data(d), NULL; if (nflows != mfread(d->flowgram, 2, nflows, mf)) return free_sff_read_data(d), NULL; for (i = 0; i < nflows; i++) d->flowgram[i] = be_int2(d->flowgram[i]); if (NULL == (d->flow_index = (uint8_t *)xmalloc(nbases))) return free_sff_read_data(d), NULL; if (nbases != mfread(d->flow_index, 1, nbases, mf)) return free_sff_read_data(d), NULL; if (NULL == (d->bases = (char *)xmalloc(nbases))) return free_sff_read_data(d), NULL; if (nbases != mfread(d->bases, 1, nbases, mf)) return free_sff_read_data(d), NULL; if (NULL == (d->quality = (uint8_t *)xmalloc(nbases))) return free_sff_read_data(d), NULL; if (nbases != mfread(d->quality, 1, nbases, mf)) return free_sff_read_data(d), NULL; /* Pad to 8 chars */ mfseek(mf, (mftell(mf) + 7)& ~7, SEEK_SET); return d; }
/* * Reads a read header (including variable length components) from an mFILE. * * Returns the a pointer to the header on success * NULL on failure */ sff_read_header *read_sff_read_header(mFILE *mf) { sff_read_header *h; unsigned char rhdr[16]; if (16 != mfread(rhdr, 1, 16, mf)) return NULL; h = decode_sff_read_header(rhdr); if (h->name_len != mfread(h->name, 1, h->name_len, mf)) return free_sff_read_header(h), NULL; /* Pad to 8 chars */ mfseek(mf, (mftell(mf) + 7)& ~7, SEEK_SET); return h; }
static int audio_get_frame(void *ctx, TCFrameAudio *ptr) { transfer_t import_para; TCImportData *data = ctx; int ret = TC_OK; if (data->fd != NULL) { if (data->bytes && (ret = mfread(ptr->audio_buf, data->bytes, 1, data->fd)) != 1) ret = TC_ERROR; ptr->audio_len = data->bytes; ptr->audio_size = data->bytes; } else { import_para.fd = NULL; import_para.buffer = ptr->audio_buf; import_para.size = data->bytes; import_para.flag = TC_AUDIO; import_para.attributes = ptr->attributes; ret = tca_import(TC_IMPORT_DECODE, &import_para, data->vob); ptr->audio_len = import_para.size; ptr->audio_size = import_para.size; } return ret; }
/* * Reads a common header (including variable length components) from an mFILE. * * Returns the a pointer to the header on success * NULL on failure */ sff_common_header *read_sff_common_header(mFILE *mf) { sff_common_header *h; unsigned char chdr[31]; if (31 != mfread(chdr, 1, 31, mf)) return NULL; h = decode_sff_common_header(chdr); if (h->flow_len != mfread(h->flow, 1, h->flow_len, mf)) return free_sff_common_header(h), NULL; if (h->key_len != mfread(h->key , 1, h->key_len, mf)) return free_sff_common_header(h), NULL; /* Pad to 8 chars */ mfseek(mf, (mftell(mf) + 7)& ~7, SEEK_SET); return h; }
char *mfgets(MEMFILE *mf,char *string,unsigned int size) { char *ptr; unsigned int read; long seek; assert(mf!=NULL); read=mfread(mf,(unsigned char *)string,size); if (read==0) return NULL; seek=0L; /* make sure that the string is zero-terminated */ assert(read<=size); if (read<size) { string[read]='\0'; } else { string[size-1]='\0'; seek=-1; /* undo reading the character that gets overwritten */ } /* if */ /* find the first '\n' */ ptr=strchr(string,'\n'); if (ptr!=NULL) { *(ptr+1)='\0'; seek=(long)(ptr-string)+1-(long)read; } /* if */ /* undo over-read */ assert(seek<=0); /* should seek backward only */ if (seek!=0) mfseek(mf,seek,SEEK_CUR); return string; }
/* for backward compatibility with the original lib */ void midifile() { mfread(); }
/* for backward compatibility with the original lib */ DECLSPEC void midifile(void) { mfread(); }
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; }
/* * As per partial_decode_ztr in srf.c, but without uncompress_ztr. */ ztr_t *partial_decode_ztr2(srf_t *srf, mFILE *mf, ztr_t *z) { ztr_t *ztr; ztr_chunk_t *chunk; long pos = 0; if (z) { /* Use existing ZTR object => already loaded header */ ztr = z; } else { /* Allocate or use existing ztr */ if (NULL == (ztr = new_ztr())) return NULL; /* Read the header */ if (-1 == ztr_read_header(mf, &ztr->header)) { if (!z) delete_ztr(ztr); mrewind(mf); return NULL; } /* Check magic number and version */ if (memcmp(ztr->header.magic, ZTR_MAGIC, 8) != 0) { if (!z) delete_ztr(ztr); mrewind(mf); return NULL; } if (ztr->header.version_major != ZTR_VERSION_MAJOR) { if (!z) delete_ztr(ztr); mrewind(mf); return NULL; } } /* Load chunks */ pos = mftell(mf); while ((chunk = ztr_read_chunk_hdr(mf))) { chunk->data = (char *)xmalloc(chunk->dlength); if (chunk->dlength != mfread(chunk->data, 1, chunk->dlength, mf)) break; ztr->nchunks++; ztr->chunk = (ztr_chunk_t *)xrealloc(ztr->chunk, ztr->nchunks * sizeof(ztr_chunk_t)); memcpy(&ztr->chunk[ztr->nchunks-1], chunk, sizeof(*chunk)); xfree(chunk); pos = mftell(mf); } /* * At this stage we're 'pos' into the mFILE mf with any remainder being * a partial block. */ if (0 == ztr->nchunks) { if (!z) delete_ztr(ztr); mrewind(mf); return NULL; } /* Ensure we exit at the start of a ztr CHUNK */ mfseek(mf, pos, SEEK_SET); /* If this is the header part, ensure we uncompress and init. data */ if (!z) { /* Force caching of huffman code_sets */ ztr_find_hcode(ztr, CODE_USER); /* And uncompress the rest */ uncompress_ztr(ztr); } return ztr; }