`
butnet
  • 浏览: 84658 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

哈夫曼编译码中中使用的二进制位式流

 
阅读更多
class BITFILE
{
private:
    char *name;
    FILE *file;
    unsigned char buf;
    int writeBit;
    long size;
    bool isRead;
//    unsigned long fileLength;
//    unsigned long readed;
public:
    bool haveFile(){if(file==NULL)return false;return true;};
    BITFILE(char* pname,char rw);
    int readInt();
    char readChar();
    bool write(char bit);
    bool write(int bit);
    void close();
    long Size();
    bool Eof();
};
bool BITFILE::Eof()
{
    if(isRead&&size==1)return true;
    if(feof(file)&&isRead)return true;
    return false;
}
long BITFILE::Size()
{return size;}
char BITFILE::readChar()
{
    if(file==NULL)return -1;
    if(isRead){
        if(size==1)return -1;
        if(writeBit=0)
        {
            if(feof(file))return -1;
            buf=fgetc(file);
//            readed++;
            if(buf==EOF)return -1;
            writeBit=8;
        }

            int temp=buf;
            temp>>=(writeBit-1);
            switch(writeBit)
            {
            case 1:buf=0;break;
            case 2:buf&=0x01;break;
            case 3:buf&=0x03;break;
            case 4:buf&=0x07;break;
            case 5:buf&=0x0f;break;
            case 6:buf&=0x1f;break;
            case 7:buf&=0x3f;break;
            case 8:buf&=0x7f;break;
            default:return -1;
            }
            writeBit--;
            size--;
            return '0'+temp;
    }else return -1;
}
int BITFILE::readInt()
{
    if(file==NULL)return -1;
    if(isRead){
        if(size==1)return -1;
        if(writeBit==0)
        {
            if(feof(file))return -1;
            buf=fgetc(file);
//            readed++;
            if(buf==EOF)return -1;
            writeBit=8;
        }

            int temp=buf;
            temp>>=(writeBit-1);
            switch(writeBit)
            {
            case 1:buf=0;break;
            case 2:buf&=0x01;break;
            case 3:buf&=0x03;break;
            case 4:buf&=0x07;break;
            case 5:buf&=0x0f;break;
            case 6:buf&=0x1f;break;
            case 7:buf&=0x3f;break;
            case 8:buf&=0x7f;break;
            default:return -1;
            }
            writeBit--;
            size--;
            return temp;
    }else return -1;
}
BITFILE::BITFILE(char* pname,char rw)
{
    if(rw=='r')isRead=true;
    if(rw=='w')isRead=false;
    size=0;
    buf=0;
    writeBit=0;
    name=pname;
//    readed=0;
    if(rw=='r')
    {
        file=fopen(pname,"rb");
        fseek(file,-sizeof(long),SEEK_END);
        fread(&size,sizeof(long),1,file);
        rewind(file);
//        cout<<"file size: "<<size<<endl;
        return;
    }
    if(rw=='w')
        file=fopen(pname,"wb");
    else
        file=NULL;
}
bool BITFILE::write(int bit)
{
    if(isRead)return false;
    if(file==NULL)return false;
//    cout<<bit;
    if(bit==1)
    {
        buf<<=1;
        buf|=1;
        writeBit++;
        if(writeBit%8==0){
            fputc(buf,file);
            buf=0;
            writeBit=0;
        }
        size++;
        return true;
    }if(bit==0)
    {
        buf<<=1;
        writeBit++;
        if(writeBit%8==0)
        {
            fputc(buf,file);
            buf=0;
            writeBit=0;
        }
        size++;
        return true;
    }return false;
}
void BITFILE::close(){
    if(file==NULL)return;
    if(isRead==false)
    {
        int left=8-writeBit;
        if(writeBit==0){fwrite(&size,sizeof(long),1,file);fclose(file);return;}
        if(writeBit==8){fwrite(&size,sizeof(long),1,file);fclose(file);return;}
        if(writeBit<8)
        for(int i=0;i<left;i++)
        {
            write(1);
            size--;
        }
//        cout<<"FILESIZE: "<<size<<endl;
        fwrite(&size,sizeof(long),1,file);
        fclose(file);
    }else{
        buf=0;
        writeBit=0;
        size=0;
        fclose(file);
    }
}
//bit只能为 '1'或'0'
bool BITFILE::write(char bit)
{
    if(isRead)return false;
    if(file==NULL)return false;
    if(bit=='1')
    {
        buf<<=1;
        buf|=1;
        writeBit++;
        if(writeBit%8==0){
            fputc(buf,file);
            buf=0;
            writeBit=0;
        }
        size++;
        return true;
    }if(bit=='0')
    {
        buf<<=1;
        writeBit++;
        if(writeBit%8==0)
        {
            fputc(buf,file);
            buf=0;
            writeBit=0;
        }
        size++;
        return true;
    }return false;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics