Algorithm 版 (精华区)
发信人: lofe ()感激生活(), 信区: Algorithm
标 题: LZW - LZWCOM.C
发信站: 哈工大紫丁香 (Sun Sep 3 16:07:16 2000), 转信
/* Added "_fmode=O_BINARY;" to main NEV */
/* LZWCOM - FILE COMPRESSOR UTILITY */
#include "sgtty.h"
#include "stdio.h"
#include "fcntl.h"
#include "debug.h"
#define FALSE 0
#define TRUE !FALSE
#define TABSIZE 4096
#define NO_PRED 0xFFFF
#define EMPTY 0xFFFF
#define NOT_FND 0xFFFF
#define UEOF ((unsigned)EOF)
struct entry {
char used;
unsigned int next; /* hi bit is 'used' flag */
unsigned int predecessor; /* 12 bit code */
unsigned char follower;
} string_tab[TABSIZE];
char is_a_con = FALSE; /* flag to suppress 'dots' in writec
*/
/* routines common to compress and decompress, contained in CommLZW.c */
unsigned hash();
unsigned unhash();
unsigned getcode();
putcode();
init_tab();
upd_tab();
main(argc,argv)
int argc; char *argv[];
{
register unsigned int c, code, localcode;
int code_count = TABSIZE - 256;
int infd, outfd;
_fmode = O_BINARY;
if (3 != argc) {
printf("Usage : lzwcom oldfilename squeezefilename\n");
exit(0);
}
if ( -1 == (infd = open( *++argv, O_RDONLY )) ) {
printf("Cant open %s\n", *argv);
exit(0);
}
if ( -1 == (outfd = creat(*++argv,0666)) ) {
printf("Cant create %s\n",*argv);
exit(0);
}
init_tab(); /* initialize code table */
c = readc(infd);
code = unhash(NO_PRED,c); /* initial code for table */
DEBUGGER (\
if (c >= ' ' || c <= '~' || c == '\n' || c == '\r')\
putchar(c);\
else\
printf("[%2x]",c);\
)
while ( UEOF != (c = readc(infd)) )
{
DEBUGGER (\
if (c >= ' ' || c <= '~' || c == '\n' || c == '\r')\
putchar(c);\
else\
printf("[%2x]",c);\
)
if ( NOT_FND != (localcode = unhash(code,c)) )
{
code = localcode;
continue;
}
/* when the above clause comes false, you have found the last known code */
putcode(outfd,code); /* only update table if table isn't full */
DEBUGGER(printf( "\n%x\n",code);)
if ( code_count )
{
upd_tab(code,c);
DEBUGGER(printf("\nadding %x %c = %x\n",code,c,unhash(code,c));)
--code_count;
}
/* start loop again with the char that didn't fit into last string */
code = unhash(NO_PRED,c);
}
putcode(outfd,code); /* once EOF reached, always */
/* one code left unsent */
DEBUGGER(fprintf(stderr,"\n%x\n",code);)
flushout(outfd); /* make sure everything's written */
exit(0); /* make sure everything gets closed
*/
}
--
████████████████████████
█ █ http://inf.home.chinaren.net
█ HAPPY NEW MILLENNIUM! █ http://jtlab.163.net
█ Yours, █
█ Anya █ inf@chinaren.com
████████████████████████
※ 修改:.haojs 于 Sep 3 16:04:42 修改本文.[FROM: bbs.hit.edu.cn]
--
※ 转寄:.武汉白云黄鹤站 bbs.whnet.edu.cn.[FROM: bbs.hit.edu.cn]
--
☆ 来源:.哈工大紫丁香 bbs.hit.edu.cn.[FROM: haojs.bbs@bbs.whnet.]
※ 修改:·lofe 於 09月03日16:10:48 修改本文·[FROM: 202.118.226.15]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:3.063毫秒