📄 stream.cpp
字号:
#include "stream.h"
#include <stdio.h>
#include <stdlib.h>
FILE* file=NULL;
BitStream::BitStream():Buffer(0), BufferLength(0), ByteStuffing(false)
{ Name[0]=0; }
BitStream::~BitStream()
{ if( file!=NULL ) Close(); }
bool BitStream::Open(const char* name, const char* attr)
{ file=fopen(name, attr);
if( file==NULL ) return false;
int i=0;
do{ Name[i]=name[i];
i=i+1;
if( i==32 ){ Name[31]=0; break; }
}while( name[i]!=0 );
return true;
}
bool BitStream::Close()
{ if( file!=NULL )
{
if( BufferLength!=0 ) fputc(Buffer, file);
Buffer=0;
BufferLength=0;
fclose(file);
file=NULL;
return true;
}
return false;
}
bool BitStream::IsEndOfStream()
{ if( file==NULL ) return true;
if( BufferLength!=0 ) return false;
if( feof(file)==0 ) return false;
return true;
}
/*----------------------------------------------------------------------------*/
bool BitStream::Put(Byte val)
{ if( file==NULL ) return false;
if( BufferLength>=8 )
{ fputc(Buffer,file);
if( ByteStuffing==true && Buffer==(Byte)0xFF )
fputc(0x00,file);
Buffer=0;
BufferLength=0;
}
Byte mask=(0x80>>BufferLength);
switch( val )
{ case 0: mask=~mask; Buffer&=mask; break;
case 1: Buffer|=mask; break;
default: break;
}
BufferLength+=1;
return true;
}
bool BitStream::Get(Byte* val)
{ if( file==NULL ) return false;
if( BufferLength==0 )
{
Buffer=fgetc(file);
if( feof(file)!=0 ){ Buffer=0; BufferLength=0; return false; }
if( ByteStuffing==true && Buffer==(Byte)0xFF )
fgetc(file); /*
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -