Initial
This commit is contained in:
265
test/SCRAPS.TXT
Normal file
265
test/SCRAPS.TXT
Normal file
@@ -0,0 +1,265 @@
|
||||
HostEnt hostEntry;
|
||||
WSASystem wsaSystem;
|
||||
Socket socket;
|
||||
String strHost("li.net");
|
||||
|
||||
winConsole.writeLine("Initializing sockets...");
|
||||
if(!wsaSystem.isInitialized()){winConsole.writeLine("Cannot initialize sockets.");winConsole.read();return FALSE;}
|
||||
winConsole.writeLine("Socket initialization complete.");
|
||||
if(!wsaSystem.description().isNull())winConsole.writeLine(String("<")+wsaSystem.description()+String(">"));
|
||||
winConsole.writeLine("Opening socket...");
|
||||
socket.openSocket();
|
||||
if(!socket.isOkay()){winConsole.writeLine("Cannot open socket.");winConsole.read();return FALSE;}
|
||||
winConsole.writeLine("Open complete.");
|
||||
winConsole.writeLine(String("Trying ")+strHost+String(" ...."));
|
||||
InternetAddress internetAddress(strHost);
|
||||
if(!internetAddress.isZero()){if(!hostEntry.hostByAddress(internetAddress)){winConsole.writeLine(String("no DNS entry for ")+strHost);winConsole.read();return FALSE;}}
|
||||
else if(!hostEntry.hostByName(strHost)){winConsole.writeLine(String("no DNS entry for ")+strHost);winConsole.read();return FALSE;}
|
||||
winConsole.writeLine(String("connect...")+String("'")+hostEntry.hostName()+String("' (")+(String)(hostEntry.addresses())[0]+String(")"));
|
||||
socket.closeSocket();
|
||||
winConsole.writeLine("Internet is available on this machine. :) Later Peach.");
|
||||
// else if(!hostEntry.hostByName(hostName)){message(String("no DNS entry for ")+hostName);return FALSE;}
|
||||
// message(String("connect...")+String("'")+hostEntry.hostName()+String("' (")+(String)(hostEntry.addresses())[0]+String(")"));
|
||||
// INETSocketAddress::internetAddress((hostEntry.addresses())[0]);
|
||||
// if(!serverEntry.serviceByName("ftp","tcp")){message("cannot determine port number for ftp daemon.");return FALSE;}
|
||||
// if(!mFTPControl.openSocket()){message("unable to create socket.");return FALSE;}
|
||||
// INETSocketAddress::family(PF_INET);
|
||||
// INETSocketAddress::port(serverEntry.port());
|
||||
// if(!mFTPControl.connect((INETSocketAddress&)*this)){message("unable to connect to ftp daemon");return FALSE;}
|
||||
// if(!receive(mAckConnectionResponseStrings))return FALSE;
|
||||
// mFTPControl.getSocketName((INETSocketAddress&)*this);
|
||||
// return mFTPControl.isConnected();
|
||||
|
||||
|
||||
|
||||
|
||||
// InternetAddress internetAddress(void)const;
|
||||
// void internetAddress(const InternetAddress &someInternetAddress);
|
||||
|
||||
|
||||
|
||||
winConsole.read();
|
||||
|
||||
|
||||
#include <common/process.hpp>
|
||||
#include <common/console.hpp>
|
||||
#include <socket/socket.hpp>
|
||||
#include <socket/hostent.hpp>
|
||||
#include <socket/wsadata.hpp>
|
||||
#include <socket/inaddr.hpp>
|
||||
#include <socket/intsaddr.hpp>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int index;
|
||||
BYTE currByte;
|
||||
BYTE lastByte;
|
||||
BYTE repCount;
|
||||
bool reset;
|
||||
|
||||
index=0;
|
||||
reset=true;
|
||||
mInputCount=0;
|
||||
mOutputCount=0;
|
||||
while(index<inData.size())
|
||||
{
|
||||
currByte=inData[index];
|
||||
mInputCount++;
|
||||
if(reset)
|
||||
{
|
||||
lastByte=currByte;
|
||||
repCount=1;
|
||||
reset=false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(lastByte==currByte)
|
||||
{
|
||||
if(repCount==MaxRep)
|
||||
{
|
||||
output(repCount,lastByte);
|
||||
reset=true;
|
||||
}
|
||||
else repCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
reset=true;
|
||||
output(repCount,lastByte);
|
||||
}
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#include <common/gdata.hpp>
|
||||
#include <common/openfile.hpp>
|
||||
#include <common/console.hpp>
|
||||
|
||||
class RLECompress
|
||||
{
|
||||
public:
|
||||
RLECompress(void);
|
||||
virtual ~RLECompress();
|
||||
bool compress(GlobalData<unsigned char> &inData,GlobalData<unsigned char> &outData);
|
||||
bool decompress(GlobalData<unsigned char> &inData,GlobalData<unsigned char> &outData);
|
||||
float getCompressionRatio(void)const;
|
||||
unsigned getOutputCount(void)const;
|
||||
unsigned getInputCount(void)const;
|
||||
private:
|
||||
enum{MaxRep=128};
|
||||
unsigned compress(unsigned char *pInData,unsigned char *pOutData,unsigned inLength,bool emit);
|
||||
void output(unsigned char repCount,unsigned char charData,unsigned char *pOutData,unsigned &outIndex,bool emit);
|
||||
|
||||
unsigned mOutputCount;
|
||||
unsigned mInputCount;
|
||||
};
|
||||
|
||||
inline
|
||||
RLECompress::RLECompress(void)
|
||||
{
|
||||
}
|
||||
|
||||
inline
|
||||
RLECompress::~RLECompress()
|
||||
{
|
||||
}
|
||||
|
||||
inline
|
||||
bool RLECompress::compress(GlobalData<BYTE> &inData,GlobalData<BYTE> &outData)
|
||||
{
|
||||
unsigned requiredLength;
|
||||
unsigned char charData;
|
||||
|
||||
requiredLength=compress(&inData[0],&charData,inData.size(),false);
|
||||
outData.size(requiredLength+4);
|
||||
*((unsigned*)&outData[0])=mInputCount;
|
||||
compress(&inData[0],&outData[4],inData.size(),true);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline
|
||||
unsigned RLECompress::compress(unsigned char *pInData,unsigned char *pOutData,unsigned inLength,bool emit)
|
||||
{
|
||||
unsigned char currByte;
|
||||
unsigned char lastByte;
|
||||
unsigned char repCount;
|
||||
unsigned srcIndex;
|
||||
unsigned dstIndex;
|
||||
bool reset;
|
||||
|
||||
srcIndex=0;
|
||||
dstIndex=0;
|
||||
reset=true;
|
||||
mInputCount=0;
|
||||
mOutputCount=0;
|
||||
while(srcIndex<inLength)
|
||||
{
|
||||
currByte=pInData[srcIndex];
|
||||
mInputCount++;
|
||||
if(reset)
|
||||
{
|
||||
lastByte=currByte;
|
||||
repCount=1;
|
||||
reset=false;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(lastByte==currByte)
|
||||
{
|
||||
if(repCount==MaxRep)
|
||||
{
|
||||
output(repCount,lastByte,pOutData,dstIndex,emit);
|
||||
reset=true;
|
||||
}
|
||||
else repCount++;
|
||||
}
|
||||
else
|
||||
{
|
||||
reset=true;
|
||||
output(repCount,lastByte,pOutData,dstIndex,emit);
|
||||
}
|
||||
}
|
||||
srcIndex++;
|
||||
}
|
||||
return mOutputCount;
|
||||
}
|
||||
|
||||
inline
|
||||
void RLECompress::output(unsigned char repCount,unsigned char charData,unsigned char *pOutData,unsigned &outIndex,bool emit)
|
||||
{
|
||||
mOutputCount+=2;
|
||||
if(!emit)return;
|
||||
pOutData[outIndex++]=repCount;
|
||||
pOutData[outIndex++]=charData;
|
||||
}
|
||||
|
||||
inline
|
||||
float RLECompress::getCompressionRatio(void)const
|
||||
{
|
||||
if(!mInputCount)return 0.00;
|
||||
return 100.00-(((float)mOutputCount/(float)mInputCount)*100.00);
|
||||
}
|
||||
|
||||
inline
|
||||
bool RLECompress::decompress(GlobalData<BYTE> &inData,GlobalData<BYTE> &outData)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
inline
|
||||
unsigned RLECompress::getOutputCount(void)const
|
||||
{
|
||||
return mOutputCount;
|
||||
}
|
||||
|
||||
inline
|
||||
unsigned RLECompress::getInputCount(void)const
|
||||
{
|
||||
return mInputCount;
|
||||
}
|
||||
|
||||
//int PASCAL WinMain(HINSTANCE /*hInstance*/,HINSTANCE /*hPrevInstance*/,LPSTR lpszCmdLine,int /*nCmdShow*/)
|
||||
int main(int argc,char **argv)
|
||||
{
|
||||
FileHandle openFile;
|
||||
GlobalData<BYTE> inData;
|
||||
GlobalData<BYTE> outData;
|
||||
RLECompress rleCompress;
|
||||
String strMessage;
|
||||
String strPathFileName;
|
||||
Console winConsole;
|
||||
|
||||
|
||||
winConsole.getConsoleScreenBufferInfo();
|
||||
if(1==argc)return 0;
|
||||
|
||||
// if(!lpszCmdLine)return 0;
|
||||
// strPathFileName=lpszCmdLine;
|
||||
strPathFileName=argv[1];
|
||||
if(!openFile.open(strPathFileName))
|
||||
{
|
||||
winConsole.writeLine(String("can't open '")+strPathFileName+String("'"));
|
||||
winConsole.read();
|
||||
return 0;
|
||||
}
|
||||
inData.size(openFile.size());
|
||||
openFile.read(&inData[0],inData.size());
|
||||
rleCompress.compress(inData,outData);
|
||||
::sprintf(strMessage,"compression ratio:%lf",rleCompress.getCompressionRatio());
|
||||
winConsole.writeLine(strMessage);
|
||||
::sprintf(strMessage,"input length:%d",rleCompress.getInputCount());
|
||||
winConsole.writeLine(strMessage);
|
||||
::sprintf(strMessage,"output length:%d",rleCompress.getOutputCount());
|
||||
winConsole.writeLine(strMessage);
|
||||
winConsole.read();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user