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

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Buttons.hpp>
#include <Dialogs.hpp>
#include <ExtCtrls.hpp>
#include <Menus.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TPanel *Panel1;
        TSpeedButton *SpeedButton1;
        TOpenDialog *OpenDialog1;
        TMainMenu *MainMenu1;
        TMenuItem *File1;
        TMenuItem *Open1;
        TMenuItem *N2;
        TMenuItem *Exit1;
        TMenuItem *About1;
        TMenuItem *N1;
        TMenuItem *Option1;
        TMenuItem *PureOutPut;
        void __fastcall FormCreate(TObject *Sender);
        void __fastcall FormActivate(TObject *Sender);
        void __fastcall FormClose(TObject *Sender, TCloseAction &Action);
        void __fastcall SpeedButton1Click(TObject *Sender);
        void __fastcall N1Click(TObject *Sender);
        void __fastcall Open1Click(TObject *Sender);
        void __fastcall Exit1Click(TObject *Sender);
        void __fastcall PureOutPutClick(TObject *Sender);
private:	// User declarations
        void __fastcall _CppToHtm();
        void __fastcall _ReadRecivedData();
        String __fastcall _TranCpp(String Str);
        String __fastcall _TagEncode(String ATagStr);
        String __fastcall _GetFuncName(String Str);
        String __fastcall _LeftSpace(String Str);
        String __fastcall _Left(String Str , int n);
        String __fastcall _Right(String Str , int n);
        String __fastcall _CutLeft(String Str , int n);
        String __fastcall _CutRight(String Str , int n);

public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

 


 

//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Unit1.h" #include "IniFiles.hpp" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; TIniFile *MyIni; TStringList *MyH; TStringList *MyBuffer; TStringList *MyBuffer2; TStringList *TranDataList; bool flag_marked,myh_exist; String flag_marked_type; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { MyIni = new TIniFile(ChangeFileExt(Application->ExeName,".ini")); MyH = new TStringList; MyBuffer = new TStringList; MyBuffer2 = new TStringList; TranDataList = new TStringList; TranDataList->Clear(); _ReadRecivedData(); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormActivate(TObject *Sender) { PureOutPut->Checked=MyIni->ReadBool("Parameter","PureOutPut",false); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { delete TranDataList; delete MyH; delete MyBuffer; delete MyBuffer2; MyIni->WriteBool("Parameter","PureOutPut",PureOutPut->Checked); delete MyIni; } //--------------------------------------------------------------------------- void __fastcall TForm1::SpeedButton1Click(TObject *Sender) { myh_exist=false; if (OpenDialog1->Execute()) { MyBuffer->LoadFromFile(OpenDialog1->FileName); if (FileExists(ChangeFileExt(ExtractFileName(OpenDialog1->FileName),".h"))) { myh_exist=true; MyH->LoadFromFile(ChangeFileExt(ExtractFileName(OpenDialog1->FileName),".h")); } //for express Panel1->Caption="Process..."; Refresh(); _CppToHtm(); Panel1->Caption=""; } } //--------------------------------------------------------------------------- void __fastcall TForm1::_CppToHtm() { String www,pre_str,flo_str,lnk_str; flag_marked=false; MyBuffer2->Clear(); MyBuffer2->Add("<html>"); MyBuffer2->Add("<head>"); MyBuffer2->Add("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=big5\">"); MyBuffer2->Add("<title>"+ChangeFileExt(ExtractFileName(OpenDialog1->FileName),".html")+"</title>"); MyBuffer2->Add("</head>"); MyBuffer2->Add("<!-- Generated by bruce0829@yahoo.com.tw HTML exporter -->"); MyBuffer2->Add("<body text=\"#000000\" bgcolor=\"#FFFFFF\" link=\"#FF00FF\" vlink=\"#FF00FF\" alink=\"#FF00FF\">"); //#0000FF(藍色), #FF00FF(桃紅), #008000(綠色) //2003/12/17 MyBuffer2->Add("<pre><code>"); MyBuffer2->Add("<font size=2 face=\"Courier New\"><font color=\"#000000\">"); //處理 *.h 檔 if (myh_exist) { for (int i=0 ; i<= MyH->Count-1 ; i++) { pre_str=""; flo_str=""; lnk_str=""; if (!PureOutPut->Checked) { if (_Left(MyH->Strings[i],7)=="#ifndef") { pre_str="<a name=\"#0\">"; flo_str="</a\">"; } if (flag_marked_type!="/*" && _GetFuncName(MyH->Strings[i])!="") lnk_str=" (<a href=\"#"+_GetFuncName(MyH->Strings[i])+"\">Content</a>)"; else lnk_str=""; } MyBuffer2->Add(pre_str+_TranCpp(MyH->Strings[i])+flo_str+lnk_str); } //水平線 MyBuffer2->Add("<p> </p>"); MyBuffer2->Add("<hr color=\"#0000FF\">"); MyBuffer2->Add("<p> </p>"); } //處理 *.cpp 檔 for (int i=0 ; i<= MyBuffer->Count-1 ; i++) { pre_str=""; flo_str=""; lnk_str=""; if (!PureOutPut->Checked) { if ((flag_marked_type!="/*") && (MyBuffer->Strings[i].Pos("__fastcall")>0) && (MyBuffer->Strings[i].Pos("__fastcall")<=8)) pre_str="<p><a name=\""+_GetFuncName(MyBuffer->Strings[i])+"\">Content</a> , (<a href=\"#0\">Go Header</a>)<br></p>"+ "<table border=\"1\" width=\"100%\" bgcolor=\"#FFFFCC\"><tr><td><pre><font face=\"Courier New\">"; //#F7F7FF(淡藍), #E1F5FF(藍), #EFEFEF(灰), #FFFFD7(黃), #E8FFE8(綠), #FFFFCC(參考黃), #E0E0F0(KTop 藍) else pre_str=""; if (flag_marked_type!="/*" && _Left(MyBuffer->Strings[i],1)=="}") flo_str="</font></pre></td></tr></table><p> </p>"; else flo_str=""; } MyBuffer2->Add(pre_str+_TranCpp(MyBuffer->Strings[i])+flo_str+lnk_str); } MyBuffer2->Add("</font>"); MyBuffer2->Add("</code></pre>"); MyBuffer2->Add("</body>"); MyBuffer2->Add("</html>"); MyBuffer2->SaveToFile(ChangeFileExt(OpenDialog1->FileName,".htm")); www=ChangeFileExt(OpenDialog1->FileName,".htm"); ShellExecute(0,NULL,www.c_str(),NULL,NULL,SW_SHOWDEFAULT); } //--------------------------------------------------------------------------- String __fastcall TForm1::_TranCpp(String Str) { String r,tmp,tran_str,test,comp_str,Seg; int o,p,index1,index2; int c,s; //for bug_fix s=1; //for bug_fix if (flag_marked_type=="//" || flag_marked_type=="\"") { flag_marked=false; flag_marked_type=""; } if (Trim(Str)=="") { return (""); } r=_LeftSpace(Str); //保留字串左邊所有的空白 tmp=Trim(Str); Seg=""; while (tmp!="") { if (flag_marked) //進入被 marked 的區塊 , 都不用轉碼 { if (flag_marked_type=="//") //後面通通不用轉 { r=r+_TagEncode(tmp)+"</i></font>"; tmp=""; continue; } if (flag_marked_type=="/*") { p=tmp.Pos("*/"); if (p==0) { r=r+_TagEncode(tmp); tmp=""; continue; } else { r=r+_TagEncode(_Left(tmp,p-1))+"*/</i></font>"; //較特殊 !! tmp=_CutLeft(tmp,p+1); flag_marked=false; flag_marked_type=""; //2003/12/17 continue; } } if (flag_marked_type=="'") { p=tmp.Pos("'"); if (p==0) { r=r+_TagEncode(tmp); tmp=""; continue; } else { r=r+_TagEncode(_Left(tmp,p-1))+"'</font>"; //較特殊 !! tmp=_CutLeft(tmp,p); flag_marked=false; flag_marked_type=""; //2003/12/17 continue; } } if (flag_marked_type=="\"") { o=tmp.Pos("\\"); p=tmp.Pos("\""); if (o!=0 && p!=0) //用來對付雙引號中帶有 \ 者(指定特殊符號) { if (o<p) { r=r+_TagEncode(_Left(tmp,o+1)); tmp=_CutLeft(tmp,o+1); continue; } } switch (p) { case 0 : r=r+_TagEncode(tmp); tmp=""; continue; break; case 1 : r=r+_TagEncode(_Left(tmp,p-1))+"\"</font>"; tmp=_CutLeft(tmp,p); flag_marked=false; flag_marked_type=""; continue; break; default: if (tmp[p-1]=='\\') { r=r+_TagEncode(_Left(tmp,p-1)); tmp=_CutLeft(tmp,p); continue; } else { r=r+_TagEncode(_Left(tmp,p-1))+"\"</font>"; //較特殊 !! tmp=_CutLeft(tmp,p); flag_marked=false; flag_marked_type=""; //2003/12/17 continue; } } } } //編譯器符號 片語比對 if (_Left(tmp,8)=="#include") { r=r+"<font color=\"#008000\">"+_TagEncode(tmp)+"</font>"; tmp=""; continue; } if (_Left(tmp,7)=="#pragma") { r=r+"<font color=\"#008000\">"+_TagEncode(tmp)+"</font>"; tmp=""; continue; } if (_Left(tmp,7)=="#ifndef") { r=r+"<font color=\"#008000\">"+_TagEncode(tmp)+"</font>"; tmp=""; continue; } if (_Left(tmp,7)=="#define") { r=r+"<font color=\"#008000\">"+_TagEncode(tmp)+"</font>"; tmp=""; continue; } if (_Left(tmp,6)=="#endif") { r=r+"<font color=\"#008000\">"+_TagEncode(tmp)+"</font>"; tmp=""; continue; } //預先比對 if (_Left(tmp,2)=="//") { r=r+"<font color=\"#0000FF\"><i>//"; //#0000FF(藍), #000080(深藍), #008000(綠) 2003/12/17 tmp=_CutLeft(tmp,2); if (tmp=="") r=r+"</i></font>"; //2003/12/09 flag_marked=true; //進入被 marked 的區塊 flag_marked_type="//"; continue; } if (_Left(tmp,2)=="/*") { r=r+"<font color=\"#0000FF\"><i>/*"; //#0000FF(藍), #000080(深藍), #008000(綠) 2003/12/17 tmp=_CutLeft(tmp,2); flag_marked=true; //進入被 marked 的區塊 flag_marked_type="/*"; continue; } if (_Left(tmp,1)=="\"") { r=r+"<font color=\"#FF0000\">\""; tmp=_CutLeft(tmp,1); flag_marked=true; //進入被 marked 的區塊 flag_marked_type="\""; continue; } if (_Left(tmp,1)=="'") { r=r+"<font color=\"#FF00FF\">'"; tmp=_CutLeft(tmp,1); flag_marked=true; //進入被 marked 的區塊 flag_marked_type="'"; continue; } if (_Left(tmp,1)==" ") { r=r+" "; s=1; //for bug_fix tmp=_CutLeft(tmp,1); continue; } if (_Left(tmp,1)=="<") { r=r+"<"; tmp=_CutLeft(tmp,1); continue; } if (_Left(tmp,1)==">") { r=r+">"; tmp=_CutLeft(tmp,1); continue; } //擷取適合的字串區段 index1=1; c=0; //for bug_fix for (int i=1 ; i<= tmp.Length() ; i++) { index2=i; //因為經過 預先比對 過濾過 , 此處找到的應該不會是在第一個位置 if (tmp[i]=='\"' || tmp[i]==' ' || tmp[i]==';' || tmp[i]=='/' || tmp[i]=='(' || tmp[i]==',' || tmp[i]=='=' || tmp[i]=='!' || tmp[i]=='+' || tmp[i]=='-' || tmp[i]=='*' || tmp[i]=='.' || tmp[i]==':' ) { c=1; //for bug_fix break; } } if (index2==tmp.Length() && c==0) index2=index2+1;//用來騙 字典比對程序 //2003/12/09 //字典比對 tran_str=TranDataList->Values[_Left(tmp,index2-1)]; if (tran_str!="" && s==1) { r=r+tran_str; tmp=_CutLeft(tmp,index2-1); continue; } s=0; //for bug_fix //空白已在前面被攔掉,所以此處多餘,故 Mark if (tmp[1]==' ' || tmp[1]==':' || tmp[1]==';' || tmp[1]==',' || tmp[1]=='(' || tmp[1]=='=') s=1; else s=0; //剩下沒人要的,只有一個一個原封不動的轉了 r=r+_Left(tmp,1); tmp=_CutLeft(tmp,1); } return(r); } //--------------------------------------------------------------------------- String __fastcall TForm1::_TagEncode(String ATagStr) { String r; r=""; for (int i=1 ; i <=ATagStr.Length() ; i++) { if (ATagStr[i]=='<') { r=r+"<"; continue; } if (ATagStr[i]=='>') { r=r+">"; continue; } r=r+(String)ATagStr[i]; } return(r); } //--------------------------------------------------------------------------- String __fastcall TForm1::_GetFuncName(String Str) { int n; String tmp,r; r=""; tmp=Trim(Str); if (tmp.Pos("__fastcall")==0) { return(r); } for (int i=tmp.Pos("__fastcall")+10 ; i<=tmp.Length() ; i++) { if (tmp[i]=='(' || tmp[i]==';' || ((r!="") && (tmp[i]==' '))) break; if (tmp[i]==':') { r=""; continue; } r=r+Trim((String)tmp[i]); } return(r); } //--------------------------------------------------------------------------- void __fastcall TForm1::_ReadRecivedData() { TranDataList->Clear(); TranDataList->Add("__except=<b>__except</b>"); TranDataList->Add("__fastcall=<b>__fastcall</b>"); TranDataList->Add("__finally=<b>__finally</b>"); TranDataList->Add("__published=<b>__published</b>"); TranDataList->Add("__stdcall=<b>__stdcall</b>"); TranDataList->Add("break=<b>break</b>"); TranDataList->Add("bool=<b>bool</b>"); TranDataList->Add("case=<b>case</b>"); TranDataList->Add("catch=<b>catch</b>"); TranDataList->Add("class=<b>class</b>"); TranDataList->Add("continue=<b>continue</b>"); TranDataList->Add("default=<b>default</b>"); TranDataList->Add("delete=<b>delete</b>"); TranDataList->Add("do=<b>do</b>"); TranDataList->Add("else=<b>else</b>"); TranDataList->Add("extern=<b>extern</b>"); TranDataList->Add("false=<b>false</b>"); TranDataList->Add("for=<b>for</b>"); TranDataList->Add("int=<b>int</b>"); TranDataList->Add("if=<b>if</b>"); TranDataList->Add("new=<b>new</b>"); TranDataList->Add("private=<b>private</b>"); TranDataList->Add("public=<b>public</b>"); TranDataList->Add("return=<b>return</b>"); TranDataList->Add("switch=<b>switch</b>"); TranDataList->Add("this=<b>this</b>"); TranDataList->Add("true=<b>true</b>"); TranDataList->Add("try=<b>try</b>"); TranDataList->Add("void=<b>void</b>"); TranDataList->Add("while=<b>while</b>"); //TranDataList->Add("<=<"); //TranDataList->Add(">=>"); } //--------------------------------------------------------------------------- //取出字串左邊的空白 //--------------------------------------------------------------------------- String __fastcall TForm1::_LeftSpace(String Str) { String r; r=""; for (int i=1 ; i <=Str.Length() ; i++) { if (Str[i]==' ') r=r+" "; else break; } return(r); } //--------------------------------------------------------------------------- //取出字串左邊 n 個字串 //--------------------------------------------------------------------------- String __fastcall TForm1::_Left(String Str , int n) { String r; r=""; if (n>Str.Length()) { return (Str); } r=Str.SubString(1,n); return (r); } //--------------------------------------------------------------------------- //取出字串右邊 n 個字串 //--------------------------------------------------------------------------- String __fastcall TForm1::_Right(String Str , int n) { String r; r=""; if (n>Str.Length()) { return (Str); } r=Str.SubString(Str.Length()-n+1,n); return (r); } //--------------------------------------------------------------------------- //減掉字串左邊 n 個字串 //--------------------------------------------------------------------------- String __fastcall TForm1::_CutLeft(String Str , int n) { String r; r=""; if (n>Str.Length()) { return (r); } r=Str.SubString(n+1,Str.Length()-n); return (r); } //--------------------------------------------------------------------------- //減掉字串右邊 n 個字串 //--------------------------------------------------------------------------- String __fastcall TForm1::_CutRight(String Str , int n) { String r; r=""; if (n>Str.Length()) { return (r); } r=Str.SubString(1,Str.Length()-n); return (r); } //--------------------------------------------------------------------------- void __fastcall TForm1::N1Click(TObject *Sender) { ShellExecute(0,NULL,"http://home.kimo.com.tw/bruce0829/",NULL,NULL,SW_SHOWDEFAULT); } //--------------------------------------------------------------------------- void __fastcall TForm1::Open1Click(TObject *Sender) { SpeedButton1Click(this); } //--------------------------------------------------------------------------- void __fastcall TForm1::Exit1Click(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::PureOutPutClick(TObject *Sender) { PureOutPut->Checked=!PureOutPut->Checked; } //---------------------------------------------------------------------------