CppUnit TDD之探索swf文件头(下)

前端之家收集整理的这篇文章主要介绍了CppUnit TDD之探索swf文件头(下)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
终于到了收尾的时候了。集结号你听到了吗?
实现核心的算法。
不是吧,C#的代码我还是没有怎么看明白,直接拷贝过来,把参数改改赋值就OK了。
#pragma once
static TCHAR szFlashSign1[]= _T("CWS");
static TCHAR szFlashSign2[]= _T("FWS");
typedef struct SwfHeader {
//DWORD mfType; // CWS,Signature 0x46,0x57,0x53 (“FWS”)
BYTE version;
DWORD fileLength;
RECT frameSize;// left = minX; right = maxX; top = minY; bottom = maxY;
WORD frameRate;
WORD frameCount;
SwfHeader()
{
fileLength = 0;
//frameSize = {0,0};
frameRate = 0;
frameCount = 0;
}
} SWFHEADER,*PSWFHEADER;
class CSwfReader
{
public:
CSwfReader(void){};
~CSwfReader(void)
{
if (NULL != m_byData)
{
delete[] m_byData;
}
}
void ParseHeader(void)
{
ReadSignature();
m_swfHeader.version = GetNextByte();
ReadFileLength();
ReadFrameSize();
ReadFrameRate();
ReadFrameCount();
}
SWFHEADER m_swfHeader;
BOOL m_bOpen;
BOOL m_bHeader;
private:
BYTE* m_byData;
CFile m_file;
int currentIndex;
void ReadSignature()
{
int a = memcmp(m_byData,szFlashSign2,3);
int b = memcmp(m_byData,szFlashSign1,3);
if (!memcmp(m_byData,3))
{
m_bHeader = TRUE;
}
else if(!memcmp(m_byData,3))
{
m_bHeader = TRUE;
}
else
{
m_bHeader = FALSE;
}
GetNextByte();
GetNextByte();
GetNextByte();
}
void ReadFileLength()
{
m_swfHeader.fileLength = GetNextDWord();
}
void ReadFrameSize()
{
int cByte = GetNextByte();
int NbBits = cByte>>3;
cByte&=7;
cByte<<=5;
int currentBit=2;
int currentValue;
// Must get all 4 values in the RECT
for (int numField=0;numField<4;numField++)
{
currentValue=0;
int bitcount = 0 ;
while (bitcount<NbBits)
{
if ((cByte&128)==128)
{
currentValue =currentValue + (1<<(NbBits-bitcount-1)) ;
}
cByte<<=1 ;
cByte &= 255 ;
currentBit-- ;
bitcount++ ;
// We will be needing a new byte if we run out of bits
if (currentBit<0)
{
cByte = GetNextByte() ;
currentBit = 7 ;
}
}
// TWIPS to PIXELS
currentValue/=20;
switch (numField)
{
case 0:
m_swfHeader.frameSize.left = currentValue ;
break ;
case 1:
m_swfHeader.frameSize.right = currentValue - m_swfHeader.frameSize.left;
break ;
case 2:
m_swfHeader.frameSize.top = currentValue ;
break ;
case 3:
m_swfHeader.frameSize.bottom = currentValue - m_swfHeader.frameSize.top;
break;
}
}
}
void ReadFrameRate()
{
// Frame rate
BYTEfps_decimal,fps_int ;
fps_decimal = GetNextByte();
fps_int = GetNextByte();
m_swfHeader.frameRate = fps_int+(fps_decimal)/100;
}
void ReadFrameCount()
{
for (int i=0;i<2;i++)
{
m_swfHeader.frameCount += (GetNextByte()<<(8*i)) ;
}
}
BYTE GetNextByte()
{
BYTE result;
result=m_byData[currentIndex];
currentIndex++;
return(result);
}
WORD GetNextWord()
{
WORD result;
result=m_byData[currentIndex];
currentIndex+=2;
return(result);
}
DWORD GetNextDWord()
{
DWORD result;
int temp = 0;
result = m_byData[currentIndex];
temp = m_byData[currentIndex+1];
result += temp<<8;
temp = m_byData[currentIndex+2];
result += temp<<16;
temp = m_byData[currentIndex+3];
result += temp<<24;
currentIndex+=4;
return(result);
}
public:
CSwfReader(CString aszName)
:currentIndex(0)
,m_bOpen(FALSE)
,m_bHeader(FALSE)
{
if(!m_file.Open(aszName,CFile::modeRead))
{
m_bOpen = FALSE;
return ;
}
int dwFileSize = m_file.GetLength();
m_byData = new BYTE[dwFileSize];
memset(m_byData,0x00,dwFileSize);
m_file.Read(m_byData,dwFileSize);
m_bOpen = TRUE;
}
};
现在这语言啊。重用性太高了。
修改相关测试程序
void CSwfReaderTest::TestSwfReader(void)
{
CSwfReader swfR(_T("g:/worksource/Robot/Twins02.swf"));
swfR.ParseHeader();
BYTE version = swfR.m_swfHeader.version;
int length = swfR.m_swfHeader.fileLength;
CPPUNIT_ASSERT_EQUAL((int)version,5);
CPPUNIT_ASSERT_EQUAL(length,81125);
CPPUNIT_ASSERT_EQUAL(swfR.m_bHeader,TRUE);
CPPUNIT_ASSERT_EQUAL((int)swfR.m_swfHeader.frameSize.right,320);
CPPUNIT_ASSERT_EQUAL((int)swfR.m_swfHeader.frameSize.bottom,340);
CPPUNIT_ASSERT_EQUAL((int)swfR.m_swfHeader.frameRate,12);
CPPUNIT_ASSERT_EQUAL((int)swfR.m_swfHeader.frameCount,1);
}
好了,所有测试通过,读取正常。可能你还需要一个flash反编译工具去看看读取的数据是不是正确,就是cppunit里面填写的那些固定数字。要不测试什么啊。
不行快饿昏了。得去吃点儿东西了。就这样,一个swf读取类,写完了。而且带有测试程序哦。
看看哪里不舒服,就rename一下。重构重构吧。
【完】
好了,终于使用cppunit完成了一次简单的TDD开发。
原文链接:/javaschema/288066.html

猜你在找的设计模式相关文章