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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
/************************************************************************/
/* */
/* Simple io streams using the C stdio. */
/* */
/************************************************************************/
# include "config.h"
# include <stdlib.h>
# include <sioMemory.h>
# include <debugon.h>
void sioMemoryInit( MemoryBuffer * mb )
{
mb->mbCapacity= 0;
mb->mbBytes= (unsigned char *)0;
return;
}
void sioMemoryClean( MemoryBuffer * mb )
{
if ( mb->mbBytes )
{ free( mb->mbBytes ); }
mb->mbCapacity= 0;
mb->mbBytes= (unsigned char *)0;
return;
}
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->mbCapacity )
{ return -1; }
if ( smp->smpPosition+ count > mb->mbCapacity )
{ count= mb->mbCapacity- 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( MemoryBuffer * mb )
{
SimpleInputStream * sis;
SioMemoryPrivate * smp;
smp= (SioMemoryPrivate *)malloc( sizeof(SioMemoryPrivate) );
if ( ! smp )
{ XDEB(smp); return (SimpleInputStream *)0; }
smp->smpBuffer= 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->mbCapacity )
{
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->mbCapacity < smp->smpPosition )
{ mb->mbCapacity= 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->mbCapacity= 0;
return sos;
}
|