// This will modify read_file and write_file
// to save and load all data in little endian notation.const static char BIG_ADDE[2] = { 0xAD, 0xDE };
char file_compressorst::read_file(void *read_var,long read_size,bool raw)
{
//---------------SNIP1
// {raw} is an endian dependant type if true
unsigned short ELVIS=*(unsigned short *)BIG_ADDE;
bool BIG_ENDIAN=(ELVIS!=0xDEAD);
bool endianSwap=!raw; // endian swap if it is not going to read in the data raw (i.e. strings)
if(!raw)
{
else if(BIG_ENDIAN)
{
// swap if big endian
endianSwap = true;
}
}
//---------------SNIP1
if(h==INVALID_HANDLE_VALUE)return 0;
//NOW LOAD INTO read_var UNTIL DONE
while(read_size>0)
{
//GET A BUFFER IF NECESSARY
if(in_buffer_amount_loaded==0||
in_buffer_position>=in_buffer_amount_loaded)
{
if(!load_new_in_buffer())return 0;
}
//BAIL IF STILL NO BUFFER LEFT
if(in_buffer_amount_loaded==0)return 0;
//SET THE AMOUNT TO COPY
long copy_size=in_buffer_amount_loaded-in_buffer_position;
if(read_size<copy_size)copy_size=read_size;
//COPY
//memmove(read_var,in_buffer+in_buffer_position,copy_size);
//---------------SNIP2
char *data = (char *)read_var;
if (endianSwap)
{
for (long read=0;read<copy_size;read++)
{
// read in reverse
data[read_size-read-1]=in_buffer[in_buffer_position+read];
}
}
else
{
for (long read=0;read<copy_size;read++)
{
data[read]=in_buffer[in_buffer_position+read];
}
}
//---------------SNIP2
read_var=((char *)read_var) + copy_size;
read_size-=copy_size;
in_buffer_position+=copy_size;
}
return 1;
}
char file_compressorst::write_file(void *write_var,long write_size,bool raw)
{
//---------------SNIP3
// {raw} is an endian dependant type if true
// this does not have backwards compatability because it really isn't necessary
unsigned short ELVIS=*(unsigned short *)BIG_ADDE;
bool BIG_ENDIAN=(ELVIS!=0xDEAD);
bool endianSwap=!raw; // endian swap if it is not going to read in the data raw (i.e. strings)
if(!raw)
{
if(BIG_ENDIAN)
{
//swap if big endian
endianSwap = true;
}
}
//---------------SNIP3
if(h==INVALID_HANDLE_VALUE)return 0;
//WRITE OUT THE VARIABLE CHUNK BY CHUNK
while(write_size>0)
{
//FLUSH THE BUFFER IF NECESSARY
if(in_buffer_amount_loaded>=in_buffersize)
{
if(!flush_in_buffer())return 0;
}
//SET THE AMOUNT TO COPY
long copy_size=in_buffersize-in_buffer_amount_loaded;
if(write_size<copy_size)copy_size=write_size;
//COPY THE NEXT CHUNK INTO THE BUFFER
//memmove(in_buffer+in_buffer_amount_loaded,write_var,copy_size);
//---------------SNIP4
char *data = (char *)write_var;
if (endianSwap)
{
for (long write=0;write<copy_size;write++)
{
// read in reverse
in_buffer[in_buffer_amount_loaded+write]=data[read_size-read-1];
}
}
else
{
for (long write=0;write<copy_size;write++)
{
in_buffer[in_buffer_position+write]=data[read];
}
}
//---------------SNIP4
write_var=((char *)write_var) + copy_size;
write_size-=copy_size;
in_buffer_amount_loaded+=copy_size;
}
return 1;
}