Tuesday, January 6, 2015

LZW implementation with Qt5

One simple implementation ... just for fun ...

Please refactor as you like ...



ALZW0.h


/*ALZW0.h*/
#pragma once
/*1:eCV eCI eCC*//*VC++ Intel Clang*/
#define eCG/*GCC*/
#if defined eCG||defined eCC
#define okN(p)__attribute__((aligned(p)))
#elif defined eCV
#define okN(p)__declspec(align(p))
#endif
#define rKz const
#define rRt return
#define rOp operator
#define rLn inline
#define ig else if
#define ih else
#define rEp noexcept
#define rEV throw(tHr)
#define rKx constexpr
#define rKE const noexcept
#define rKV const throw(tHr)
#define rTh(p)throw(tHr)p
#define rFV catch(tHr)
#define rFW catch(tHr){throw;}
#define rFV catch(tHr)
#define rSt static
#define rXp explicit
#include<stdint.h>
#include<stddef.h>
typedef uint8_t t1u;typedef int8_t t1s;
typedef uint16_t t2u;typedef int16_t t2s;
typedef uint32_t t4u;typedef int32_t t4s;
typedef uint64_t t8u;typedef int64_t t8s;
typedef float t4f;typedef double t8f;
typedef char t1k;
typedef char16_t t2k;typedef char32_t t4k;typedef wchar_t twk;
typedef size_t tpu;typedef ptrdiff_t tps;
typedef tps tHr;
typedef t1u tWU1;
typedef t4u tWU4;
typedef char const*tSQ;
typedef bool tBu;
typedef void voi;
static rKx tBu const cBuA=1;
static rKx tBu const cBuB=0;
#define oOpDFS(_)_&rOp=(_ rKz&)rEp=default;_&rOp=(_&&)rEp=default;constexpr _(_ rKz&)rEp=default;constexpr _(_&&)rEp=default;_()rEp=default;

ALZWFi.h


/*ALZWFi.h*/
#pragma once
#include<ALZW0.h>
#include<fstream>
struct tFi{private:mutable FILE*m;public:
oOpDFS(tFi)
typedef t8u tL;
tBu ____gh()rKE{rRt(tBu)!!m;}
voi fh(FILE*p)rEp{m=(FILE*)p;}
voi ARA_h(t1k const&)rEV;voi ARA_h(twk const&)rEV;
voi ARB_h(t1k const&)rEV;voi ARB_h(twk const&)rEV;
voi AWA_h(t1k const&)rEV;voi AWA_h(twk const&)rEV;
voi AWB_h(t1k const&)rEV;voi AWB_h(twk const&)rEV;
voi AWT_h(t1k const&)rEV;voi AWT_h(twk const&)rEV;
voi AA__h(t1k const&)rEV;voi AA__h(twk const&)rEV;
voi AAP_h(t1k const&)rEV;voi AAP_h(twk const&)rEV;
voi A__(t1k const&,t1k const&)rEV;
voi B()rEp{fclose(m);}
FILE*____gP()rEp{rRt m;}
voi uK_h__(t1u)rKV;
t4u kRd(voi*,t4u)rKE;
t4u kRi(voi const*,t4u)rKE;
t4u kRd_h(voi*,t4u)rKV;
t4u kRi_h(voi const*,t4u)rKV;
tBu kbRd(voi*,t4u)rKE;
tBu kbRi(voi const*,t4u)rKE;
voi uRd_h(voi*,t4u)rKV;
voi uRi_h(voi const*,t4u)rKV;
voi uSkSt_h(tL)rKV;
voi uSkCr_h(tL)rKV;
voi uSkBF_h(tL)rKV;/*
voi uSkSt_h(tII8)rKV;
voi uSkCr_h(tII8)rKV;
voi uSkBF_h(tII8)rKV;*/
#ifdef eCV
template<typename...t>tBu kbSn(t1k const&p,t...q)rKE{rRt(tBu)(std::fscanf_s(m,&p,q...)!=EOF);}
#else
template<typename...t>tBu kbSn(t1k const&p,t...q)rKE{rRt(tBu)(std::fscanf(m,&p,q...)!=EOF);}
#endif
tL kTl_h()rKV;
tL kLh_h()rKV;
rSt tL xLh_h(t1k const&)rEV;rSt tBu xbb(t1k const&)rEp;
rSt tL xLh_h(twk const&)rEV;rSt tBu xbb(twk const&)rEp;
voi uK_h(t1u)rKV;
voi uK_h(tSQ)rKV;
t1u kK_h()rKV;
t4u gN_h()rKV;
template<typename...t>t4s kPn(t1k const&p,t...q)rKE{rRt fprintf(m,&p,q...);}};

ALZWFi.cpp


/*ALZWFi.cpp*/
#include<ALZWFi.h>
#include<cstring>
#ifdef eCV
#define o_FO1(m,A,F)fopen_s(&m,A,F)
#else
#define o_FO1(m,A,F)m=fopen(A,F)
#endif
#define o_r_ "r"
#define o_rb "rb"
#define o_w_ "w"
#define o_wb "wb"
#define o_wt "wt"
#define o_a_ "a"
#define o_aP "a+"
#define o_FO2(m,A,F){t1k l[0x1000];std::memset(l,0,0x1000),wcstombs((t1k*)&l,&A,0xFFF);o_FO1(m,(t1k const*)&l,F);}
/*iconv_t i=iconv_open("UTF-8","UTF-16");
iconv(i,&a,&c,&b,&d);
iconv_close(i);*/
voi tFi::ARA_h(t1k const&p)rEV{o_FO1(m,&p,o_r_);if(!m){throw(tHr)0x45;}}voi tFi::ARA_h(twk const&p)rEV{o_FO2(m,p,o_r_);if(!m){throw(tHr)0x45;}}
voi tFi::ARB_h(t1k const&p)rEV{o_FO1(m,&p,o_rb);if(!m){throw(tHr)0x45;}}voi tFi::ARB_h(twk const&p)rEV{o_FO2(m,p,o_rb);if(!m){throw(tHr)0x45;}}
voi tFi::AWA_h(t1k const&p)rEV{o_FO1(m,&p,o_w_);if(!m){throw(tHr)0x45;}}voi tFi::AWA_h(twk const&p)rEV{o_FO2(m,p,o_w_);if(!m){throw(tHr)0x45;}}
voi tFi::AWB_h(t1k const&p)rEV{o_FO1(m,&p,o_wb);if(!m){throw(tHr)0x45;}}voi tFi::AWB_h(twk const&p)rEV{o_FO2(m,p,o_wb);if(!m){throw(tHr)0x45;}}
voi tFi::AWT_h(t1k const&p)rEV{o_FO1(m,&p,o_wt);if(!m){throw(tHr)0x45;}}voi tFi::AWT_h(twk const&p)rEV{o_FO2(m,p,o_wt);if(!m){throw(tHr)0x45;}}
voi tFi::AA__h(t1k const&p)rEV{o_FO1(m,&p,o_a_);if(!m){throw(tHr)0x45;}}voi tFi::AA__h(twk const&p)rEV{o_FO2(m,p,o_a_);if(!m){throw(tHr)0x45;}}
voi tFi::AAP_h(t1k const&p)rEV{o_FO1(m,&p,o_aP);if(!m){throw(tHr)0x45;}}voi tFi::AAP_h(twk const&p)rEV{o_FO2(m,p,o_aP);if(!m){throw(tHr)0x45;}}
#undef o_r_
#undef o_rb
#undef o_w_
#undef o_wb
#undef o_wt
#undef o_a_
#undef o_aP
voi tFi::A__(t1k const&p,t1k const&q)rEV{o_FO1(m,&p,&q);if(!m){throw(tHr)0x45;}}
voi tFi::uK_h__(t1u p)rKV{if(ungetc(p,m)==EOF){rTh(0);}}
t4u tFi::kRd(voi*p,t4u q)rKE{rRt fread(p,1,q,m);}
t4u tFi::kRi(voi const*p,t4u q)rKE{rRt fwrite(p,1,q,m);}
tBu tFi::kbRd(voi*p,t4u q)rKE{rRt(tBu)(kRd(p,q)==q);}
tBu tFi::kbRi(voi const*p,t4u q)rKE{rRt(tBu)(kRi(p,q)==q);}
voi tFi::uRd_h(voi*p,t4u q)rKV{if(kRd(p,q)!=q){throw;}}
voi tFi::uRi_h(voi const*p,t4u q)rKV{if(kRi(p,q)!=q){throw;}}
voi tFi::uSkSt_h(tL p)rKV{if(fseek(m,(t8u)p,SEEK_SET)){rTh(0);}}
voi tFi::uSkCr_h(tL p)rKV{if(fseek(m,(t8u)p,SEEK_CUR)){rTh(0);}}
voi tFi::uSkBF_h(tL p)rKV{if(fseek(m,(t8u)p,SEEK_END)){rTh(0);}}/*
voi tFi::uSkSt_h(tII8 p)rKV{if(fseek(m,(t8s)p,SEEK_SET)){rTh(0);}}
voi tFi::uSkCr_h(tII8 p)rKV{if(fseek(m,(t8s)p,SEEK_CUR)){rTh(0);}}
voi tFi::uSkBF_h(tII8 p)rKV{if(fseek(m,(t8s)p,SEEK_END)){rTh(0);}}*/
tFi::tL tFi::kTl_h()rKV{t8s l=ftell(m);if(l==-1L){rTh(0);}rRt(tL)(t8u)l;}
tFi::tL tFi::kLh_h()rKV{tL l;try{uSkBF_h((tL)0),l=kTl_h();}rFW rRt l;}
tFi::tL tFi::xLh_h(t1k const&p)rEV{tFi F;tL l;try{F.ARB_h(p),l=F.kLh_h();}rFW F.B();rRt l;}
tFi::tL tFi::xLh_h(twk const&p)rEV{tFi F;tL l;try{F.ARB_h(p),l=F.kLh_h();}rFW F.B();rRt l;}
tBu tFi::xbb(t1k const&p)rEp{tFi l;try{l.ARB_h(p);}rFV{rRt cBuB;}l.B();rRt cBuA;}
tBu tFi::xbb(twk const&p)rEp{tFi l;try{l.ARB_h(p);}rFV{rRt cBuB;}l.B();rRt cBuA;}
voi tFi::uK_h(t1u p)rKV{if(fputc(p,m)==EOF){rTh(0);}}
voi tFi::uK_h(tSQ p)rKV{if(fputs(p,m)==EOF){rTh(0);}}
t1u tFi::kK_h()rKV{t4s l=fgetc(m);if(l==EOF){rTh(0);}rRt(t1u)l;}
t4u tFi::gN_h()rKV{t4s l=fileno(m);if(l==-1){rTh(0);}rRt(t4u)l;}
#undef o_FO1
#undef o_FO2

#if 0
int main_file(){
tFi l;
if(tFi::xbb(*"GAGAGA")){return 0;}
try{l.AWB_h(*"GAGAGA");}rFV{goto la1;}
try{
l.uRi_h("GAGAGA",6);}rFV{goto la2;}
la2:
l.B();
la1:
return 0;}
#endif

ALZWP.h


struct taLZW{public:taLZW()rEp=delete;~taLZW()rEp=delete;
rSt voi A_h(tFi rKz&,tFi&)rEV;
rSt voi B_h(tFi rKz&,tFi&)rEV;};

ALZWP.cpp


/*ALZWP.cpp*/
#include<ALZWP.h>
rSt rKx tWU1 rKz cB_=(tWU1)12;
rSt rKx tWU1 rKz cH_=cB_-(tWU1)8;
rSt rKx tWU1 rKz cN_=(tWU1)32-cB_;
rSt rKx tWU4 rKz cV_=((tWU4)1U<<cB_)-(tWU4)1;/*nte dat MS-DOS machines ANd to ACpl der cd in lrj mdl if14 bt r ASel#$n*/
rSt rKx tWU4 rKz cC_=cV_-(tWU4)1;
rSt rKx tWU4 rKz cZ_=cB_==0xE?(tWU4)0x4679:cB_==0xD?(tWU4)0x2345:(tWU4)0x139D;/*sr tbl sz ANd to $ a prime no dat $ smwat lrjr dan 2**u_Bits cB_<=12*/
rSt tWU4 _x_F(tWU4 p,tWU1 K,tWU4 V[],tWU4 P[],tWU1 A[])rEp{tWU4 I=(tWU4(K)<<cH_)^p;tWU4 rKz F=I?tWU4(cZ_-I):(tWU4)1;
laRp:
if(V[I]==~0U){rRt I;}
if(P[I]==p&&A[I]==K){rRt I;}
if(F>=I){I+=cZ_;}I-=F;goto laRp;}
rSt tWU1*xsB_h(tWU1*p,tWU4 K,tWU4 P[],tWU1 A[])rEV{t4u i=0;while(K>0xFF){*p++=A[K];K=P[K];if(i++>=cC_){throw;}}*p=(tWU1)K;rRt p;}
rSt voi _w_K_h(tFi&p,tWU4 K,tWU1&N,tWU4&B)rEV{B|=K<<(cN_-N);N+=cB_;while(N>=0x08){try{p.uK_h(t1u(B>>0x18U));}rFW B<<=0x08U;N-=(tWU1)0x08;}}
rSt tWU4 _x_K_h(tFi rKz&p,tWU1&N,tWU4&B)rEV{while(N<=0x18){tWU1 l;try{l=(tWU1)p.kK_h();}rFW B|=((tWU4)l)<<tWU4((tWU1)0x18-N);N+=(tWU1)0x08;}tWU4 rKz K=B>>cN_;B<<=(tWU4)cB_;N-=cB_;rRt K;}
rSt tWU4 nlVA[cZ_];rSt tWU4 nlPA[cZ_];
rSt tWU1 nlAA[cZ_];rSt tWU1 nlBA[0x1000];
voi taLZW::A_h(tFi rKz&p,tFi&q)rEV{t4u i=cZ_;while(i--){nlVA[i]=~(tWU4)0U;}
tWU4 S;try{S=(tWU4)p.kK_h();}rFW
tWU4 D=(tWU4)0,X=(tWU4)256;
tWU1 N=(tWU1)0,K;
la:try{K=(tWU1)p.kK_h();}rFV{goto la2;}{tWU4 rKz I=_x_F(S,K,nlVA,nlPA,nlAA);
if(nlVA[I]==~(tWU4)0U){
if(X<=cC_){nlVA[I]=X++;nlPA[I]=S;nlAA[I]=K;}try{_w_K_h(q,S,N,D);}rFW S=(tWU4)K;}ih{S=nlVA[I];}goto la;}
la2:try{_w_K_h(q,S,N,D),_w_K_h(q,cV_,N,D),_w_K_h(q,(tWU4)0,N,D);}rFW}
voi taLZW::B_h(tFi rKz&p,tFi&q)rEV{tWU4 D=(tWU4)0,X=(tWU4)256,O;tWU1 N=(tWU1)0;
try{O=(tWU4)_x_K_h(p,N,D),q.uK_h((t1u)O);}rFW
tWU1 K=(tWU1)O;tWU4 W;
la:try{W=(tWU4)_x_K_h(p,N,D);}rFV{rRt;}if((W)!=cV_){tWU1 rKz*P;
if(W>=X){nlBA[0]=K;try{P=xsB_h(nlBA+1,O,nlPA,nlAA);}rFW}ih{try{P=xsB_h(nlBA,W,nlPA,nlAA);}rFW}
K=*P;while(P>=nlBA){try{q.uK_h(*P--);}rFW}if(X<=cC_){nlPA[X]=O;nlAA[X++]=K;}O=W;goto la;}}
#if 0
int main_compress(){
tFi lRd,lRi;
if(tFi::xbb(*"FUFUFU")||tFi::xbb(*"FUFUFU.bin")){return 0;}
try{lRd.AWB_h(*"FUFUFU");}rFV{goto la1;}
try{lRi.AWB_h(*"FUFUFU.bin");}rFV{goto la2;}
try{taLZW::A_h(lRd,lRi);}rFV{goto la3;}
la3:
lRi.B();
la2:
lRd.B();
la1:
return 0;}
int main_decompress(){
tFi lRd,lRi;
if(tFi::xbb(*"FUFUFU")||tFi::xbb(*"FUFUFU.out")){return 0;}
try{lRd.AWB_h(*"FUFUFU");}rFV{goto la1;}
try{lRi.AWB_h(*"FUFUFU.out");}rFV{goto la2;}
try{taLZW::B_h(lRd,lRi);}rFV{goto la3;}
la3:
lRi.B();
la2:
lRd.B();
la1:
return 0;}
#endif

ALZW.h


#pragma once
#include<QtWidgets/QComboBox>
#include<QtWidgets/QLabel>
#include<QtWidgets/QHBoxLayout>
#include<QtWidgets/QMainWindow>
class tMa:public QMainWindow{Q_OBJECT
public:
explicit tMa(QWidget* =0);
private slots:
void ACps();
void BCps();
private:
QLabel*mLb1,*mLb2;
QLineEdit*mLnEd;};


ALZW.cpp


/*ALZW.cpp*/
#include<ALZW.h>
#include<ALZWP.h>
#include<QtWidgets/qapplication.h>
#include<QtWidgets/qpushbutton.h>
#include<QtWidgets/qfiledialog.h>
#include<QtWidgets/qlineedit.h>
#include<unistd.h>
/*You ANid to run   moc ALZW.h -o ALZW.moc  .
Firstly, 'cd' (change directory) to your header file (named "AWS.h"), then run "moc" to AGn AWS.moc.
For evry change you do to the header file, you ANid to regenerate this file named "AWS.moc".*/
#include"ALZW.moc"
rSt QRect _x_(t4s x,t4s y,t4u w,t4u h)rEp{rRt QRect(x-(t4s)(w>>1U),y-(t4s)(h>>1U),w,h);}
tMa::tMa(QWidget*p):QMainWindow(p){
this->setWindowTitle("LZW compressor");
this->setGeometry(QRect(120,120,0x2B0,0x0A0));
{mLb1=new QLabel("Welcome to LZW compressor!",this);mLb1->setFixedWidth(0x2B0),mLb1->move(0x10,0x060),mLb1->show();}
{mLb2=new QLabel("Result of operation is shown on this line, here.",this);mLb2->setFixedWidth(0x2B0),mLb2->move(0x10,0x070),mLb1->show();}
{QLabel*l=new QLabel("Copyleft R-Man 2014",this);l->setFixedWidth(0x2B0),l->move(0x10,0x080),l->show();}
{QPushButton*l=new QPushButton("Compress",this);l->setGeometry(_x_(0x180,0x018,0x60,0x20)),
QObject::connect(l,SIGNAL(clicked()),this,SLOT(ACps())),l->show();}
{QPushButton*l=new QPushButton("Decompress",this);l->setGeometry(_x_(0x220,0x018,0x60,0x20)),
QObject::connect(l,SIGNAL(clicked()),this,SLOT(BCps())),l->show();}}
void tMa::ACps(){
QString lFiIn=QFileDialog::getOpenFileName(this,tr("Open File"),"",tr("Files (*.*)"));
if(!lFiIn.length()){rRt;}
QString lFiOu=lFiIn+QString(".bin"),lFiR;
std::wstring
lA=lFiIn.toStdWString(),
lB=lFiOu.toStdWString();
tFi l,k;bool R;
if(tFi::xbb(*lB.data())){R=0;lFiR="File already exists! Please firstly resolve.";goto la1;}
try{l.ARB_h(*lA.data());}rFV{R=0;lFiR="Now cannot open file! Something just changed?";goto la1;}
try{k.AWB_h(*lB.data());}rFV{R=0;lFiR="Cannot start writing file! I/O error?";goto la2;}
mLb1->setText(QString("Compressing: Program will freeze for a while."));
mLb2->setText(QString("Please wait .."));
try{taLZW::A_h(l,k);R=1;}rFV{R=0;lFiR="Cannot write file during encrypting! I/O error?";}
k.B();
la2:
l.B();
la1:;
if(R){
mLb1->setText(QString("Compressed: OK.")),mLb2->setText(QString("Output: ")+lFiOu);}
ih{
mLb1->setText(QString("Compressing: FAIL .. !")),mLb2->setText(lFiR);}}
void tMa::BCps(){
QString lFiIn=QFileDialog::getOpenFileName(this,tr("Open File"),"",tr("Files (*.*)"));
if(!lFiIn.length()){rRt;}
QString lFiOu=lFiIn+QString(".out"),lFiR;
std::wstring
lA=lFiIn.toStdWString(),
lB=lFiOu.toStdWString();
mLb1->setText(lFiOu);
tFi l,k;
bool R;
if(tFi::xbb(*lB.data())){R=0;lFiR="File already exists! Please firstly resolve.";goto la1;}
try{l.ARB_h(*lA.data());}rFV{R=0;lFiR="Now cannot open file! Something just changed?";goto la1;}
try{k.AWB_h(*lB.data());}rFV{R=0;lFiR="Cannot start writing file! I/O error?";goto la2;}
mLb1->setText(QString("Decompressing: Program will freeze for a while."));
mLb2->setText(QString("Please wait .."));
try{taLZW::B_h(l,k);R=1;}rFV{R=0;lFiR="Cannot write file during decrypting! I/O error?";}
k.B();
la2:
l.B();
la1:;
if(R){
mLb1->setText(QString("Decompressed: OK.")),mLb2->setText(QString("Output: ")+lFiOu);}
ih{
mLb1->setText(QString("Decompressing: FAIL .. !")),mLb2->setText(lFiR);}}
t4s main(t4s argc,t1k*argv[]){QApplication lAp(argc,argv);tMa l;l.show();rRt lAp.exec();}


Screenshot:


Monday, January 5, 2015

Open file with Unicode name

It turns out to be rather simple ...


void f(wchar_t const*p){
char l[0x1000];
memset(l,0,0x1000);
wcstombs(l,p,0xFFF);
FILE*F=fopen(l,"rb");/*Open normally .. =D */

/*Et cetera ... */
fclose(F);/*Remember to close*/
}

Thursday, January 1, 2015

My naming convention

I developed it because it really helps. To me, this is the ergonomics of coding. But, I have yet to demonstrate the same good results in other people's projects ...

A non-comprehensive non-formal guide:

[functions inside class/struct/namespace]
f- mutator, non-static, trivial
u- mutator, non-static, non-trivial*
g- accessor, non-static, trivial
k- accessor, non-static, non-trivial
s- mutator, static, trivial
w- mutator, static, non-trivial
q- accessor, static, trivial
x- accessor, static, non-trivial

*(requires calculation, might cause side-effect (such as (but not limited to - ) change of other member variables, change of global variables)

[functions outside class/struct/namespace] (Notice that it is virtually symmetrical with the ones above)
_f_- mutator, non-static, trivial
_u_- mutator, non-static, non-trivial
_g_- accessor, non-static, trivial
_k_- accessor, non-static, non-trivial
_s_- mutator, static, trivial
_w_- mutator, static, non-trivial
_q_- accessor, static, trivial
_x_- accessor, static, non-trivial

p- - parameter
l- - local variable 
-b- - of boolean type
-c- - a threaded function, which runs asynchronously
o_- - macro
_h- - throws exception

m- - member, non-static
n- - member, static
c- - member, const, static
t- - type
-t- - template
tu- - type, union
tv- - type, polymorphic
ta- - type, empty, non-instantiatable, used as an interface
-r- - recursive (calls itself)

[Computer graphics]
to- - type, polymorphic, tangible instantiatable object, obeys engine's physics
th- - type, polymorphic, intangible instantiatable object, defies engine's physics
tk- - type, component, not polymorphic

[Data type]
t1u - 1 byte, unsigned integer
t1s - 1 byte, signed integer
t2u - 2 bytes, unsigned integer
t2s - 2 bytes, signed integer
t4u - 4 bytes, unsigned integer
t4s - 4 bytes, signed integer
t8u - 8 bytes, unsigned integer
t8s - 8 bytes, signed integer
t4f - 4 bytes, float
t8f - 8 bytes, float
tpu - address, unsigned (size_t)
tps - address, signed (ssize_t)
t1k - 1 byte, character
t2k - 2 bytes, character (char16_t)
t4k - 4 bytes, character (char32_t)
twk - wchar_t
tuk - TCHAR (an ASCII char or wchar_t, depending on user's setting)
tSQ - t1k const*
tUQ - tuk const*