1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
/************************************************************************/
/* */
/* Simple io streams using the C stdio. */
/* */
/************************************************************************/
# include "appUtilConfig.h"
# include <stdlib.h>
# include <string.h>
# include <sioMemory.h>
# include <appDebugon.h>
typedef struct SioMemoryPrivate
{
MemoryBuffer * smpBuffer;
int smpPosition;
} SioMemoryPrivate;
static int sioMemoryClose( void * voidsmp )
{ free( voidsmp ); return 0; }
static int sioInMemoryReadBytes( void * voidsmp,
unsigned char * buffer,
int count )
{
SioMemoryPrivate * smp= (SioMemoryPrivate *)voidsmp;
MemoryBuffer * mb= smp->smpBuffer;
if ( smp->smpPosition >= mb->mbSize )
{ return -1; }
if ( smp->smpPosition+ count > mb->mbSize )
{ count= mb->mbSize- smp->smpPosition; }
memcpy( buffer, mb->mbBytes+ smp->smpPosition, count );
smp->smpPosition += count;
return count;
}
static int sioMemorySeek( void * voidsmp,
long pos )
{
SioMemoryPrivate * smp= (SioMemoryPrivate *)voidsmp;
smp->smpPosition= pos;
return 0;
}
SimpleInputStream * sioInMemoryOpen( const MemoryBuffer * mb )
{
SimpleInputStream * sis;
SioMemoryPrivate * smp;
smp= (SioMemoryPrivate *)malloc( sizeof(SioMemoryPrivate) );
if ( ! smp )
{ XDEB(smp); return (SimpleInputStream *)0; }
smp->smpBuffer= (MemoryBuffer *)mb;
smp->smpPosition= 0;
sis= sioInOpen( (void *)smp,
sioInMemoryReadBytes, sioMemorySeek, sioMemoryClose );
if ( ! sis )
{ XDEB(sis); free( smp ); return (SimpleInputStream *)0; }
return sis;
}
static int sioOutMemoryWriteBytes( void * voidsmp,
const unsigned char * buffer,
int count )
{
SioMemoryPrivate * smp= (SioMemoryPrivate *)voidsmp;
MemoryBuffer * mb= smp->smpBuffer;
if ( smp->smpPosition+ count > mb->mbSize )
{
unsigned char * fresh;
fresh= (unsigned char *)realloc( mb->mbBytes, smp->smpPosition+ count );
if ( ! fresh )
{ LXDEB(smp->smpPosition+ count,fresh); return -1; }
mb->mbBytes= fresh;
}
memcpy( mb->mbBytes+ smp->smpPosition, buffer, count );
smp->smpPosition += count;
if ( mb->mbSize < smp->smpPosition )
{ mb->mbSize= smp->smpPosition; }
return count;
}
SimpleOutputStream * sioOutMemoryOpen( MemoryBuffer * mb )
{
SimpleOutputStream * sos;
SioMemoryPrivate * smp;
smp= (SioMemoryPrivate *)malloc( sizeof(SioMemoryPrivate) );
if ( ! smp )
{ XDEB(smp); return (SimpleOutputStream *)0; }
smp->smpBuffer= mb;
smp->smpPosition= 0;
sos= sioOutOpen( (void *)smp, sioOutMemoryWriteBytes,
sioMemorySeek, sioMemoryClose );
if ( ! sos )
{ XDEB(sos); free( smp ); return (SimpleOutputStream *)0; }
mb->mbSize= 0;
return sos;
}
|