添加lzw压缩算法
This commit is contained in:
104
huffman/hello.c
Normal file
104
huffman/hello.c
Normal file
@@ -0,0 +1,104 @@
|
||||
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
// #include "coder_lib.h"
|
||||
// #include "huffman.h"
|
||||
#include "huffman_.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
// 验证管壳码算法
|
||||
// int main(int num,char *str[]){
|
||||
|
||||
|
||||
// uint8_t d_[12]={0};
|
||||
// char uid[20]={0};
|
||||
|
||||
|
||||
// coder_shell_to_uid("2023","5830628A00004",uid);
|
||||
// printf("uid=%s\r\n",uid);
|
||||
// coder_uid_to_save(uid,d_);
|
||||
|
||||
// printf("slave:, uid_pw=%02X %02X %02X %02X %02X %02X %02X %02X "
|
||||
// "%02X %02X %02X %02X ",d_[0],d_[1],d_[2],d_[3],d_[4],d_[5],d_[6],d_[7],
|
||||
// d_[8],d_[9],d_[10],d_[11]);
|
||||
|
||||
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// 验证huffman算法
|
||||
|
||||
// int main(int argc, char *argv[])
|
||||
// {
|
||||
// uint8_t str_in[]="2023 5830628A000005830628A000015830628A000025830628A000035830628A000045830628A000055830628A000065830628A000075830628A000085830628A00009";
|
||||
// array_def *a=arr_creat();
|
||||
// arr_appends(a,str_in,strlen(str_in));
|
||||
// array_def *out=hm_encode(a);
|
||||
// printf("endode:%s\n",arr_string(out));
|
||||
// array_def *de=hm_decode(out);
|
||||
// printf("decde:%s\n",arr_data(de));
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
|
||||
|
||||
|
||||
long calc_file_size(FILE *stream)
|
||||
{
|
||||
long file_size = -1;
|
||||
long cur_offset = ftell(stream); // 获取当前偏移位置
|
||||
if (cur_offset == -1) {
|
||||
printf("ftell failed :%s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
if (fseek(stream, 0, SEEK_END) != 0) { // 移动文件指针到文件末尾
|
||||
printf("fseek failed: %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
file_size = ftell(stream); // 获取此时偏移值,即文件大小
|
||||
if (file_size == -1) {
|
||||
printf("ftell failed :%s\n", strerror(errno));
|
||||
}
|
||||
if (fseek(stream, cur_offset, SEEK_SET) != 0) { // 将文件指针恢复初始位置
|
||||
printf("fseek failed: %s\n", strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
return file_size;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int encode_file(const char *filename)
|
||||
{
|
||||
FILE *f=fopen(filename, "rb");
|
||||
int file_size=calc_file_size(f);
|
||||
uint8_t *file_data=calloc(file_size+1,sizeof(uint8_t));
|
||||
uint8_t *encode_data=0;
|
||||
int encode_size;
|
||||
fread(file_data,1,file_size,f);
|
||||
hm_encode(file_data,file_size,&encode_data,&encode_size);
|
||||
free(file_data);
|
||||
free(encode_data);
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
encode_file(argv[1]);
|
||||
// const uint8_t file_data[]="2023 5830628A000005830628A000015830628A000025830628A000035830628A000045830628A000055830628A000065830628A000075830628A000085830628A00009";
|
||||
// uint8_t *encode_data=0;
|
||||
// int encode_size;
|
||||
// hm_encode(file_data,sizeof(file_data),&encode_data,&encode_size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user