void __fastcall TForm1::RLECompressClick(TObject *Sender) { // File: RLE, Project v1.2 // Author: M P Kubiszyn // Date: 30.12.2007 // select file // if(OD->Execute()){ // vars // int last=-1; int c; long bin,bout=0; AnsiString extractFN = ""; AnsiString extFN = ""; // file streams - redirect stdin, stdout // const char *fin=OD->FileName.c_str(); extractFN = ExtractFileName(OD->FileName.c_str()); extFN = ExtractFileExt(extractFN); extractFN = extractFN.SubString(1, extractFN.Length()-extFN.Length()); AnsiString myFile=GetCurrentDir(); myFile+="\\"; myFile+=extractFN; myFile+="#CMP"; myFile+=extFN; const char *fout=myFile.c_str(); freopen( fin, "rb", stdin ); freopen( fout, "wb", stdout ); while ((c=getc(stdin))>=0){ putc((char)c,stdout); if (c==last){ int count=0; while ( ( c = getc( stdin ) ) >= 0 ){ if(c==last) count++; else break; } if ( c < 0 ) { /* EOF handling */ if ( ( count >= 256 ) && ( count % 256 == 0 ) ) count -=256; else if ( count == 0 ) count = -1; } if ( count >=0 ) putc( (char) count, stdout); while ( (count = count - 256) >= 0) putc( (char) last, stdout ); if ( c >= 0 ) putc( (char) c, stdout ); } last = c; } bin=ftell(stdin); bout=ftell(stdout); fclose(stdin); fclose(stdout); Caption="RLE Compressed, "+(AnsiString)ExtractFileName(OD->FileName)+", Before ("+IntToStr(bin)+(AnsiString)" bytes)"+" Now ("+IntToStr(bout)+(AnsiString)" bytes)"; } } void __fastcall TForm1::RLEDecompressClick(TObject *Sender) { if(OD->Execute()){ int last = -1; int c; int count; long bin,bout=0; AnsiString extractFN = ""; AnsiString extFN = ""; // file streams - redirect stdin, stdout // const char *fin=OD->FileName.c_str(); extractFN = ExtractFileName(OD->FileName.c_str()); extFN = ExtractFileExt(extractFN); extractFN = extractFN.SubString(1, extractFN.Length()-extFN.Length()-4); AnsiString myFile=GetCurrentDir(); myFile+="\\"; myFile+=extractFN; myFile+=extFN; const char *fout=myFile.c_str(); freopen( fin, "rb", stdin ); freopen( fout, "wb", stdout ); while ( ( c = getc( stdin ) ) >= 0 ) { putc( (char) c, stdout ); if ( c == last ) { count = getc( stdin ); if ( count < 0 ) break; //eof while ( ( c = getc( stdin ) ) == last ) count += 256; if ( c < 0 ) count += ( count % 256 ) == 0 ? 256: 0; while ( count-- > 0 ) putc( (char) last, stdout ); if ( c < 0 ) break; // more EOF processing putc( (char) c, stdout ); } last = c; } bin=ftell(stdin); bout=ftell(stdout); fclose(stdin); fclose(stdout); Caption="RLE Decompressed, "+(AnsiString)ExtractFileName(OD->FileName)+", Before ("+IntToStr(bin)+(AnsiString)" bytes)"+" Now ("+IntToStr(bout)+(AnsiString)" bytes)"; } }