| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1104 人关注过本帖
标题:高手进来,给个算法吧!
只看楼主 加入收藏
FLXDS
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-7-4
收藏
 问题点数:0 回复次数:0 
高手进来,给个算法吧!
请哪位高手好心帮我翻译以下代码的算法:
#include <vcl.h>
#pragma hdrstop

#include "Extract.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------

//   Important: Methods and properties of objects in VCL can only be
//   used in a method called using Synchronize, for example:
//
//      Synchronize(UpdateCaption);
//
//   where UpdateCaption could look like:
//
//      void __fastcall TExtract::UpdateCaption()
//      {
//        Form1->Caption = "Updated in a thread";
//      }
//---------------------------------------------------------------------------

__fastcall TExtract::TExtract(bool CreateSuspended)
: TThread(CreateSuspended)
{
}
#include "Unit1.h"
//---------------------------------------------------------------------------
void __fastcall TExtract::Execute()
{
    String BaseDir = ExtractFileDir(FileName)+"\\out_hxf";
    MkDir(BaseDir);
    int fp = FileOpen(FileName,fmOpenRead);
    if( fp < 0 ){
     MessageBox(Application->Handle,"文件打开的失败","出错",MB_OK);
        return ;
    }
    int FileLen = FileSeek(fp,0,2);
    FileSeek(fp,0x40,0);
    while( FileSeek(fp,0,1) < FileLen ){
     ExtractFile(fp,BaseDir);
    }
    strMsg = "解包完成";
    this->Synchronize(AddLog);
    FileClose(fp);
}
void __fastcall TExtract::AddLog(void)
{
//TODO: Add your source code here
    Form1->Memo1->Lines->Insert(0,strMsg);
}
void TExtract::ExtractFile( int fp , String Base){


DWORD length,rCount;
    char s[400],buf[1024000];
    int n = 0 ;

    FileRead(fp,&length,4);
    if( length > 400 || length <= 0 ){
     strMsg = "文件名长度不对";
        this->Synchronize(AddLog);
     return;
    }

FileRead(fp,s,length);
    s[length]=0;
    Base = Base + "\\";
for( char * t = s ; *t != 0 ; t++){
if( *t == '\\'){
         *t=0;
            MkDir(Base+String(s));
            *t='\\';
        }
    }
    String FileName =  Base+String(s);
    strMsg = FileName ;
    //this->Synchronize(AddLog);
    if( FileExists(FileName)){
     DeleteFile(FileName);
    }
    int fout = FileCreate(FileName,0777);
    if( fout <= 0 ) return ;
FileRead(fp,buf,1);
    FileRead(fp,&length,4);
    for( rCount = length ; rCount>0;rCount-=n){
n = FileRead(fp,buf,rCount>1024000?1024000:rCount);
FileWrite(fout,buf,n);
    }
    FileClose(fout);
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------

#include
#include "Unit1.h"
#include "stdio.h"
#include
using namespace std;
#pragma hdrstop

#include "Package.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------

// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall TPackage::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------

__fastcall TPackage::TPackage(bool CreateSuspended)
: TThread(CreateSuspended)
{
}

bool compare(String first, String second)
{
unsigned int i=0;
while ( (i<FIRST.LENGTH())
{
if (tolower(first.c_str())<TOLOWER(SECOND.C_STR()))
else if (tolower(first.c_str())>tolower(second.c_str())) return false;
++i;
}
if (first.Length()<SECOND.LENGTH())
else return false;
}


void TPackage::PackageFile(int fp , String BaseDir){

char sBuf[100],*buf;
String FullName;
int dwSize,n;
int iAttributes = faAnyFile | faDirectory;
TSearchRec sr;
int hxfPos , fin ;
hxfPos = BaseDir.Pos("hxf");
hxfPos+=4;
if(FindFirst(BaseDir+"*.*", iAttributes, sr) == 0){
list mylist;
list mydir;
do{
if( sr.Name.c_str()[0] == '.' )continue;
if( (sr.Attr & faDirectory) > 0){
mylist.push_back("D"+sr.Name);
continue;
}
mylist.push_back("d"+sr.Name);
strMsg = "开始为" + sr.Name + "打包"; this->Synchronize(AddLog);
}while (FindNext(sr) == 0);
mylist.sort(compare);
list::iterator it;
if( mylist.size() > 0 )
for (it=mylist.begin(); it!=mylist.end(); ++it){
char c = (*it).c_str()[0];
String Name = (*it).SubString(2,(*it).Length()-1);
if( c == 'D'){
PackageFile(fp,BaseDir+Name+"\\");
continue;
}
FullName = BaseDir+Name;
FullName = FullName.SubString(hxfPos,FullName.Length()-hxfPos+1);
fin = FileOpen(BaseDir+Name, fmOpenRead);
if( fin<0){
strMsg = "文件错误"; this->Synchronize(AddLog);
continue;
}
int FileLen = FileSeek(fin,0,2);

dwSize = FullName.Length();FileWrite(fp,&dwSize,4);

FileWrite(fp,FullName.c_str(),dwSize);
 
FileWrite(fp," ",1);

FileWrite(fp,&FileLen,4);

FileSeek(fin,0,0);
buf = new char[102400];
for( int rCount = FileLen , n = 0 ; rCount>0;rCount-=n){
n = FileRead(fin,buf,rCount>102400?102400:rCount);
FileWrite(fp,buf,n);
}
free(buf);
FileClose(fin);
}
}
/*
if(FindFirst(BaseDir+"*.*", iAttributes, sr) == 0)
do
{
if( sr.Name.c_str()[0] == '.' )continue;
if( (sr.Attr & faDirectory) > 0){
PackageFile(fp,BaseDir+sr.Name+"\\");
continue;
}
//if( sr.Name == "Thumbs.db" )continue;
FullName = BaseDir+sr.Name;
FullName = FullName.SubString(hxfPos,FullName.Length()-hxfPos+1);
fin = FileOpen(BaseDir+sr.Name, fmOpenRead);
if( fin<0){
strMsg =
可能有点不完整!!
搜索更多相关主题的帖子: 算法 Synchronize pragma include 
2008-07-04 19:55
快速回复:高手进来,给个算法吧!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014657 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved