📄 bitstr.c
字号:
/*------------------------------------------------------------------------------ File : BITSTR.c Author : St閜hane TAVENARD (C) Copyright 1997-1998 St閜hane TAVENARD All Rights Reserved This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. #Rev| Date | Comment ----|----------|-------------------------------------------------------- 0 |21/02/1997| Initial revision ST 1 |31/03/1997| First Aminet release ST 2 |19/04/1997| Added async I/O ST 3 |03/05/1997| Added file access spec ST 4 |07/05/1997| Added stream_size in stream access spec ST 5 |07/06/1997| Added BSTR_read_bytes ST 6 |04/07/1997| Added stream size calc with fseek & ftell ST 7 |19/07/1997| Change BSTR_seek error handling & defaut Amiga I/O ST 8 |01/11/1997| Added Hooks support (for AMIGA Version) ST 9 |02/05/1998| Added PPC support (for AMIGA Version) ST 10 |16/06/1998| Added BSTR_pos() function ST 11 |23/06/1998| Optimized ppc cache ST ------------------------------------------------------------------------ BitStream files handling Pure C Version------------------------------------------------------------------------------*/#include "defs.h" // #8#include <stdio.h>#include <stdlib.h>#include <string.h>#include "bitstr.h"#ifdef AMIGA // #7#include <dos/stdio.h>#include <proto/dos.h>#ifdef HOOKS#endif#endif/* Default file I/O functions (#3) */static long def_open( char *stream_name, long buffer_size, long *stream_size )/*----------------------------------------------------------------------------*/{#ifdef PPC // #9 Begin BPTR file_ptr; *stream_size = 0; file_ptr = PPCOpen( stream_name, MODE_OLDFILE );/* if( (file_ptr) && (buffer_size > 0) ) { SetVBuf( file_ptr, NULL, BUF_FULL, buffer_size ); }*/ *stream_size = 0; if( file_ptr ) { if( PPCSeek( file_ptr, 0, OFFSET_END ) != -1 ) { *stream_size = PPCSeek( file_ptr, 0, OFFSET_CURRENT ); PPCSeek( file_ptr, 0, OFFSET_BEGINNING ); } }#else#ifdef AMIGA // #7 BPTR file_ptr; *stream_size = 0; file_ptr = Open( stream_name, MODE_OLDFILE ); if( (file_ptr) && (buffer_size > 0) ) { SetVBuf( file_ptr, NULL, BUF_FULL, buffer_size ); } *stream_size = 0; if( file_ptr ) { if( Seek( file_ptr, 0, OFFSET_END ) != -1 ) { *stream_size = Seek( file_ptr, 0, OFFSET_CURRENT ); Seek( file_ptr, 0, OFFSET_BEGINNING ); } }#else FILE *file_ptr; file_ptr = fopen( stream_name, "rb" ); if( (file_ptr) && (buffer_size > 0 ) ) { setvbuf( file_ptr, NULL, _IOFBF, buffer_size ); } *stream_size = 0; if( file_ptr ) { // #6 if( !fseek( file_ptr, 0, SEEK_END ) ) { *stream_size = ftell( file_ptr ); fseek( file_ptr, 0, SEEK_SET ); } }#endif#endif // #9 End return (long)file_ptr;}static void def_close( long handle )/*----------------------------------*/{#ifdef PPC // #9 Begin if( handle ) PPCClose( (BPTR)handle );#else#ifdef AMIGA // #7 if( handle ) Close( (BPTR)handle );#else if( handle ) fclose( (FILE *)handle );#endif#endif // #9 End}static long def_read( long handle, void *buffer, long num_bytes )/*---------------------------------------------------------------*/{ long read_size = -1;#ifdef PPC // #9 Begin if( handle ) { read_size = PPCRead( (BPTR)handle, buffer, num_bytes ); }#else#ifdef AMIGA // #7 if( handle ) { read_size = FRead( (BPTR)handle, buffer, 1, num_bytes ); }#else if( handle ) { read_size = fread( buffer, 1, num_bytes, (FILE *)handle ); }#endif#endif // #9 End return read_size;}static int def_seek( long handle, long abs_byte_seek_pos )/*--------------------------------------------------------*/{ int err = 0;#ifdef PPC // #9 Begin if( handle ) { if( PPCSeek( (BPTR)handle, abs_byte_seek_pos, OFFSET_BEGINNING ) == -1 ) err = -1; }#else#ifdef AMIGA // #7 if( handle ) { if( Seek( (BPTR)handle, abs_byte_seek_pos, OFFSET_BEGINNING ) == -1 ) err = -1; }#else if( handle ) { err = fseek( (FILE *)handle, abs_byte_seek_pos, SEEK_SET ); }#endif#endif // #9 End return err;}#ifdef HOOKS // #8#ifdef PPC // #9 Beginstatic ULONG ppc_call_hook( BITSTREAM *bs, APTR handle, BSTR_ACCESS_PARAM *access ) { if( !bs->caos ) { bs->caos = (struct Caos *)PPCAllocVec( sizeof(struct Caos), MEMF_PUBLIC | MEMF_CLEAR ); if( !bs->caos ) return -1; } bs->caos->caos_Un.Function = (APTR)bs->baccess.h_Entry; bs->caos->M68kCacheMode = IF_CACHEFLUSHALL; bs->caos->PPCCacheMode = IF_CACHEFLUSHALL;// bs->caos->M68kCacheMode = IF_CACHEFLUSHNO;// bs->caos->PPCCacheMode = IF_CACHEFLUSHNO; bs->caos->a0 = (ULONG)&bs->baccess; bs->caos->a2 = (ULONG)handle; bs->caos->a1 = (ULONG)access; bs->caos->d0 = 0; (void)PPCCallM68k( bs->caos ); return bs->caos->d0;}#elsestatic ULONG SAVEDS ASM def_baccess( REG(a0) struct Hook *hook, REG(a2) APTR handle, REG(a1) BSTR_ACCESS_PARAM *access ) {/*-----------------------------------------------------------------------*/ switch( access->func ) { case BSTR_FUNC_OPEN: return (ULONG)def_open( access->data.open.stream_name, access->data.open.buffer_size, &access->data.open.stream_size ); case BSTR_FUNC_CLOSE: def_close( (long)handle ); break; case BSTR_FUNC_READ: return (ULONG)def_read( (long)handle, access->data.read.buffer, access->data.read.num_bytes ); case BSTR_FUNC_SEEK: return (ULONG)def_seek( (long)handle, access->data.seek.abs_byte_seek_pos ); } return 0;}typedef ULONG ASM (*BSTR_HOOK_FUNC)( REG(a0) struct Hook *hook, REG(a2) APTR handle, REG(a1) BSTR_ACCESS_PARAM *access );#endif // #9 End#endif/* End of default file I/O functions */void BSTR_close( BITSTREAM *bitstream ){ if( bitstream ) { if( bitstream->buffer ) { free( bitstream->buffer ); } if( bitstream->file_handle ) { // #3#ifdef HOOKS // #8 BSTR_ACCESS_PARAM param; param.func = BSTR_FUNC_CLOSE;#ifdef PPC // #9 Begin ppc_call_hook( bitstream, (APTR)bitstream->file_handle, ¶m );#else// CallHookPkt( &bitstream->baccess, (APTR)bitstream->file_handle, ¶m ); ((BSTR_HOOK_FUNC)bitstream->baccess.h_Entry)( &bitstream->baccess, (APTR)bitstream->file_handle, ¶m );#endif // #9 End#else if( bitstream->baccess.close ) { bitstream->baccess.close( bitstream->file_handle ); }#endif }#ifdef PPC // #9 if( bitstream->caos ) { PPCFreeVec( bitstream->caos ); bitstream->caos = NULL; }#endif free( bitstream ); }}BITSTREAM *BSTR_open( BITSTREAM_ACCESS *bs_access, char *filename, long buffer_size )/*----------------------------------------------------------------------------------- Open a BitStream for read Inputs: bs_access = specify how to access to the bitstream (functions) if NULL, use standard file i/o filename = name of the bitstream buffer_size = # of bytes read for each access*/{ BITSTREAM *bs; bs = (BITSTREAM *)malloc( sizeof( BITSTREAM ) ); if( !bs ) return NULL; memset( bs, 0, sizeof( BITSTREAM ) );#ifdef HOOKS // #8 if( bs_access ) { bs->baccess = *bs_access; } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -